blob: d6df9f6c9f7e42d9d9d06a1401c89bc5a47b246d [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>
Russell King5e1dbdb42008-11-08 20:48:27 +00006#include <linux/device.h>
Russell King97d654f2006-03-15 15:54:37 +00007#include <linux/list.h>
8#include <linux/errno.h>
9#include <linux/err.h>
10#include <linux/string.h>
11#include <linux/clk.h>
12#include <linux/spinlock.h>
Russell Kingd0a9d752007-04-22 10:08:58 +010013#include <linux/mutex.h>
Jett.Zhouedf3ff52011-11-30 14:32:36 +080014#include <linux/io.h>
15#include <linux/clkdev.h>
Russell King97d654f2006-03-15 15:54:37 +000016
Russell Kinga09e64f2008-08-05 16:14:15 +010017#include <mach/hardware.h>
Russell King97d654f2006-03-15 15:54:37 +000018
Jett.Zhouedf3ff52011-11-30 14:32:36 +080019struct clkops {
20 void (*enable)(struct clk *);
21 void (*disable)(struct clk *);
22 unsigned long (*getrate)(struct clk *);
23};
24
Russell King97d654f2006-03-15 15:54:37 +000025struct clk {
Jett.Zhouedf3ff52011-11-30 14:32:36 +080026 const struct clkops *ops;
27 unsigned long rate;
Russell King97d654f2006-03-15 15:54:37 +000028 unsigned int enabled;
Russell King97d654f2006-03-15 15:54:37 +000029};
30
Jett.Zhouedf3ff52011-11-30 14:32:36 +080031#define INIT_CLKREG(_clk, _devname, _conname) \
32 { \
33 .clk = _clk, \
34 .dev_id = _devname, \
35 .con_id = _conname, \
36 }
37
38#define DEFINE_CLK(_name, _ops, _rate) \
39struct clk clk_##_name = { \
40 .ops = _ops, \
41 .rate = _rate, \
42 }
43
44static DEFINE_SPINLOCK(clocks_lock);
45
46static void clk_gpio27_enable(struct clk *clk)
Russell King97d654f2006-03-15 15:54:37 +000047{
48 /*
49 * First, set up the 3.6864MHz clock on GPIO 27 for the SA-1111:
50 * (SA-1110 Developer's Manual, section 9.1.2.1)
51 */
52 GAFR |= GPIO_32_768kHz;
53 GPDR |= GPIO_32_768kHz;
54 TUCR = TUCR_3_6864MHz;
55}
56
Jett.Zhouedf3ff52011-11-30 14:32:36 +080057static void clk_gpio27_disable(struct clk *clk)
Russell King97d654f2006-03-15 15:54:37 +000058{
59 TUCR = 0;
60 GPDR &= ~GPIO_32_768kHz;
61 GAFR &= ~GPIO_32_768kHz;
62}
63
Russell King5e1dbdb42008-11-08 20:48:27 +000064int clk_enable(struct clk *clk)
65{
66 unsigned long flags;
67
68 spin_lock_irqsave(&clocks_lock, flags);
69 if (clk->enabled++ == 0)
Jett.Zhouedf3ff52011-11-30 14:32:36 +080070 clk->ops->enable(clk);
Russell King5e1dbdb42008-11-08 20:48:27 +000071 spin_unlock_irqrestore(&clocks_lock, flags);
Jett.Zhouedf3ff52011-11-30 14:32:36 +080072
Russell King97d654f2006-03-15 15:54:37 +000073 return 0;
74}
Russell King5e1dbdb42008-11-08 20:48:27 +000075EXPORT_SYMBOL(clk_enable);
Russell King97d654f2006-03-15 15:54:37 +000076
Russell King5e1dbdb42008-11-08 20:48:27 +000077void clk_disable(struct clk *clk)
Russell King97d654f2006-03-15 15:54:37 +000078{
Russell King5e1dbdb42008-11-08 20:48:27 +000079 unsigned long flags;
Russell King97d654f2006-03-15 15:54:37 +000080
Russell King5e1dbdb42008-11-08 20:48:27 +000081 WARN_ON(clk->enabled == 0);
82
83 spin_lock_irqsave(&clocks_lock, flags);
84 if (--clk->enabled == 0)
Jett.Zhouedf3ff52011-11-30 14:32:36 +080085 clk->ops->disable(clk);
Russell King5e1dbdb42008-11-08 20:48:27 +000086 spin_unlock_irqrestore(&clocks_lock, flags);
Russell King97d654f2006-03-15 15:54:37 +000087}
Russell King5e1dbdb42008-11-08 20:48:27 +000088EXPORT_SYMBOL(clk_disable);
89
90unsigned long clk_get_rate(struct clk *clk)
91{
Jett.Zhouedf3ff52011-11-30 14:32:36 +080092 unsigned long rate;
93
94 rate = clk->rate;
95 if (clk->ops->getrate)
96 rate = clk->ops->getrate(clk);
97
98 return rate;
Russell King5e1dbdb42008-11-08 20:48:27 +000099}
100EXPORT_SYMBOL(clk_get_rate);
Jett.Zhouedf3ff52011-11-30 14:32:36 +0800101
102const struct clkops clk_gpio27_ops = {
103 .enable = clk_gpio27_enable,
104 .disable = clk_gpio27_disable,
105};
106
107static void clk_dummy_enable(struct clk *clk) { }
108static void clk_dummy_disable(struct clk *clk) { }
109
110const struct clkops clk_dummy_ops = {
111 .enable = clk_dummy_enable,
112 .disable = clk_dummy_disable,
113};
114
115static DEFINE_CLK(gpio27, &clk_gpio27_ops, 3686400);
116static DEFINE_CLK(dummy, &clk_dummy_ops, 0);
117
118static struct clk_lookup sa11xx_clkregs[] = {
119 INIT_CLKREG(&clk_gpio27, "sa1111.0", NULL),
120 INIT_CLKREG(&clk_dummy, "sa1100-rtc", NULL),
121};
122
123static int __init sa11xx_clk_init(void)
124{
125 clkdev_add_table(sa11xx_clkregs, ARRAY_SIZE(sa11xx_clkregs));
126 return 0;
127}
128
129postcore_initcall(sa11xx_clk_init);