[ARM] lh7a40x: clocks - cleanup
[linux-2.6] / arch / arm / mach-lh7a40x / clocks.c
1 /* arch/arm/mach-lh7a40x/clocks.c
2  *
3  *  Copyright (C) 2004 Marc Singer
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License
7  *  version 2 as published by the Free Software Foundation.
8  *
9  */
10
11 #include <linux/cpufreq.h>
12 #include <mach/hardware.h>
13 #include <mach/clocks.h>
14 #include <linux/err.h>
15
16 struct module;
17
18 struct clk {
19         struct list_head node;
20         unsigned long rate;
21         struct module *owner;
22         const char *name;
23 };
24
25 /* ----- */
26
27 #define MAINDIV1(c)     (((c) >>  7) & 0x0f)
28 #define MAINDIV2(c)     (((c) >> 11) & 0x1f)
29 #define PS(c)           (((c) >> 18) & 0x03)
30 #define PREDIV(c)       (((c) >>  2) & 0x1f)
31 #define HCLKDIV(c)      (((c) >>  0) & 0x02)
32 #define PCLKDIV(c)      (((c) >> 16) & 0x03)
33
34 unsigned int cpufreq_get (unsigned int cpu) /* in kHz */
35 {
36         return fclkfreq_get ()/1000;
37 }
38 EXPORT_SYMBOL(cpufreq_get);
39
40 unsigned int fclkfreq_get (void)
41 {
42         unsigned int clkset = CSC_CLKSET;
43         unsigned int gclk
44                 = XTAL_IN
45                 / (1 << PS(clkset))
46                 * (MAINDIV1(clkset) + 2)
47                 / (PREDIV(clkset)   + 2)
48                 * (MAINDIV2(clkset) + 2)
49                 ;
50         return gclk;
51 }
52
53 unsigned int hclkfreq_get (void)
54 {
55         unsigned int clkset = CSC_CLKSET;
56         unsigned int hclk = fclkfreq_get () / (HCLKDIV(clkset) + 1);
57
58         return hclk;
59 }
60
61 unsigned int pclkfreq_get (void)
62 {
63         unsigned int clkset = CSC_CLKSET;
64         int pclkdiv = PCLKDIV(clkset);
65         unsigned int pclk;
66         if (pclkdiv == 0x3)
67                 pclkdiv = 0x2;
68         pclk = hclkfreq_get () / (1 << pclkdiv);
69
70         return pclk;
71 }
72
73 /* ----- */
74
75 static LIST_HEAD(clocks);
76 static DECLARE_MUTEX(clocks_sem);
77
78 struct clk *clk_get (struct device *dev, const char *id)
79 {
80         struct clk *p;
81         struct clk *clk = ERR_PTR(-ENOENT);
82
83         down (&clocks_sem);
84         list_for_each_entry (p, &clocks, node) {
85                 if (strcmp (id, p->name) == 0
86                     && try_module_get(p->owner)) {
87                         clk = p;
88                         break;
89                 }
90         }
91         up (&clocks_sem);
92
93         return clk;
94 }
95 EXPORT_SYMBOL(clk_get);
96
97 void clk_put (struct clk *clk)
98 {
99         module_put(clk->owner);
100 }
101 EXPORT_SYMBOL(clk_put);
102
103 int clk_enable (struct clk *clk)
104 {
105         return 0;
106 }
107 EXPORT_SYMBOL(clk_enable);
108
109 void clk_disable (struct clk *clk)
110 {
111 }
112 EXPORT_SYMBOL(clk_disable);
113
114 int clk_use (struct clk *clk)
115 {
116         return 0;
117 }
118 EXPORT_SYMBOL(clk_use);
119
120 void clk_unuse (struct clk *clk)
121 {
122 }
123 EXPORT_SYMBOL(clk_unuse);
124
125 unsigned long clk_get_rate (struct clk *clk)
126 {
127         return clk->rate;
128 }
129 EXPORT_SYMBOL(clk_get_rate);
130
131 long clk_round_rate (struct clk *clk, unsigned long rate)
132 {
133         return rate;
134 }
135 EXPORT_SYMBOL(clk_round_rate);
136
137 int clk_set_rate (struct clk *clk, unsigned long rate)
138 {
139         int ret = -EIO;
140         return ret;
141 }
142 EXPORT_SYMBOL(clk_set_rate);
143
144 static struct clk clcd_clk = {
145         .name   = "CLCDCLK",
146         .rate   = 0,
147 };
148
149 int clk_register (struct clk *clk)
150 {
151         down (&clocks_sem);
152         list_add (&clk->node, &clocks);
153         up (&clocks_sem);
154         return 0;
155 }
156 EXPORT_SYMBOL(clk_register);
157
158 void clk_unregister (struct clk *clk)
159 {
160         down (&clocks_sem);
161         list_del (&clk->node);
162         up (&clocks_sem);
163 }
164 EXPORT_SYMBOL(clk_unregister);
165
166 static int __init clk_init (void)
167 {
168         clk_register(&clcd_clk);
169         return 0;
170 }
171 arch_initcall(clk_init);