blob: e97dc59813c86af131d2b86395a5326cbb49fc7f [file] [log] [blame]
Russell King97d654f2006-03-15 15:54:37 +00001/*
2 * linux/arch/arm/mach-sa1100/clock.c
3 */
4#include <linux/module.h>
5#include <linux/kernel.h>
6#include <linux/list.h>
7#include <linux/errno.h>
8#include <linux/err.h>
9#include <linux/string.h>
10#include <linux/clk.h>
11#include <linux/spinlock.h>
Russell Kinga6dba202007-08-20 10:18:02 +010012#include <linux/platform_device.h>
13#include <linux/delay.h>
Russell King97d654f2006-03-15 15:54:37 +000014
15#include <asm/arch/pxa-regs.h>
eric miaoa683b142008-03-03 09:44:25 +080016#include <asm/arch/pxa2xx-gpio.h>
Russell King97d654f2006-03-15 15:54:37 +000017#include <asm/hardware.h>
Russell King97d654f2006-03-15 15:54:37 +000018
Russell Kinga6dba202007-08-20 10:18:02 +010019#include "devices.h"
20#include "generic.h"
21#include "clock.h"
Russell King97d654f2006-03-15 15:54:37 +000022
23static LIST_HEAD(clocks);
Russell Kingf4b6a0a2007-05-15 16:49:02 +010024static DEFINE_MUTEX(clocks_mutex);
Russell King97d654f2006-03-15 15:54:37 +000025static DEFINE_SPINLOCK(clocks_lock);
26
Russell Kinga0dd0052008-02-17 10:35:15 +000027static struct clk *clk_lookup(struct device *dev, const char *id)
28{
29 struct clk *p;
30
31 list_for_each_entry(p, &clocks, node)
32 if (strcmp(id, p->name) == 0 && p->dev == dev)
33 return p;
34
35 return NULL;
36}
37
Russell King97d654f2006-03-15 15:54:37 +000038struct clk *clk_get(struct device *dev, const char *id)
39{
40 struct clk *p, *clk = ERR_PTR(-ENOENT);
41
Russell Kingf4b6a0a2007-05-15 16:49:02 +010042 mutex_lock(&clocks_mutex);
Russell Kinga0dd0052008-02-17 10:35:15 +000043 p = clk_lookup(dev, id);
44 if (!p)
45 p = clk_lookup(NULL, id);
46 if (p)
47 clk = p;
Russell Kingf4b6a0a2007-05-15 16:49:02 +010048 mutex_unlock(&clocks_mutex);
Russell King97d654f2006-03-15 15:54:37 +000049
50 return clk;
51}
52EXPORT_SYMBOL(clk_get);
53
54void clk_put(struct clk *clk)
55{
Russell King97d654f2006-03-15 15:54:37 +000056}
57EXPORT_SYMBOL(clk_put);
58
59int clk_enable(struct clk *clk)
60{
61 unsigned long flags;
62
63 spin_lock_irqsave(&clocks_lock, flags);
64 if (clk->enabled++ == 0)
Russell Kinga6dba202007-08-20 10:18:02 +010065 clk->ops->enable(clk);
Russell King97d654f2006-03-15 15:54:37 +000066 spin_unlock_irqrestore(&clocks_lock, flags);
Russell Kinga6dba202007-08-20 10:18:02 +010067
68 if (clk->delay)
69 udelay(clk->delay);
70
Russell King97d654f2006-03-15 15:54:37 +000071 return 0;
72}
73EXPORT_SYMBOL(clk_enable);
74
75void clk_disable(struct clk *clk)
76{
77 unsigned long flags;
78
79 WARN_ON(clk->enabled == 0);
80
81 spin_lock_irqsave(&clocks_lock, flags);
82 if (--clk->enabled == 0)
Russell Kinga6dba202007-08-20 10:18:02 +010083 clk->ops->disable(clk);
Russell King97d654f2006-03-15 15:54:37 +000084 spin_unlock_irqrestore(&clocks_lock, flags);
85}
86EXPORT_SYMBOL(clk_disable);
87
88unsigned long clk_get_rate(struct clk *clk)
89{
Russell Kinga6dba202007-08-20 10:18:02 +010090 unsigned long rate;
91
92 rate = clk->rate;
93 if (clk->ops->getrate)
94 rate = clk->ops->getrate(clk);
95
96 return rate;
Russell King97d654f2006-03-15 15:54:37 +000097}
98EXPORT_SYMBOL(clk_get_rate);
99
100
Russell Kinga6dba202007-08-20 10:18:02 +0100101static void clk_gpio27_enable(struct clk *clk)
Russell King97d654f2006-03-15 15:54:37 +0000102{
103 pxa_gpio_mode(GPIO11_3_6MHz_MD);
104}
105
Russell Kinga6dba202007-08-20 10:18:02 +0100106static void clk_gpio27_disable(struct clk *clk)
Russell King97d654f2006-03-15 15:54:37 +0000107{
108}
109
Russell Kinga6dba202007-08-20 10:18:02 +0100110static const struct clkops clk_gpio27_ops = {
Russell King97d654f2006-03-15 15:54:37 +0000111 .enable = clk_gpio27_enable,
112 .disable = clk_gpio27_disable,
113};
114
Russell King97d654f2006-03-15 15:54:37 +0000115
Russell Kinga6dba202007-08-20 10:18:02 +0100116void clk_cken_enable(struct clk *clk)
Russell King97d654f2006-03-15 15:54:37 +0000117{
Russell Kinga6dba202007-08-20 10:18:02 +0100118 CKEN |= 1 << clk->cken;
119}
120
121void clk_cken_disable(struct clk *clk)
122{
123 CKEN &= ~(1 << clk->cken);
124}
125
126const struct clkops clk_cken_ops = {
127 .enable = clk_cken_enable,
128 .disable = clk_cken_disable,
129};
130
131static struct clk common_clks[] = {
132 {
133 .name = "GPIO27_CLK",
134 .ops = &clk_gpio27_ops,
135 .rate = 3686400,
136 },
137};
138
139void clks_register(struct clk *clks, size_t num)
140{
141 int i;
142
Russell Kingf4b6a0a2007-05-15 16:49:02 +0100143 mutex_lock(&clocks_mutex);
Russell Kinga6dba202007-08-20 10:18:02 +0100144 for (i = 0; i < num; i++)
145 list_add(&clks[i].node, &clocks);
Russell Kingf4b6a0a2007-05-15 16:49:02 +0100146 mutex_unlock(&clocks_mutex);
Russell King97d654f2006-03-15 15:54:37 +0000147}
Russell King97d654f2006-03-15 15:54:37 +0000148
149static int __init clk_init(void)
150{
Russell Kinga6dba202007-08-20 10:18:02 +0100151 clks_register(common_clks, ARRAY_SIZE(common_clks));
Russell King97d654f2006-03-15 15:54:37 +0000152 return 0;
153}
154arch_initcall(clk_init);