2 * Clock management for AT32AP CPUs
4 * Copyright (C) 2006 Atmel Corporation
6 * Based on arch/arm/mach-at91/clock.c
7 * Copyright (C) 2005 David Brownell
8 * Copyright (C) 2005 Ivan Kokshaysky
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #include <linux/clk.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/string.h>
19 #include <mach/chip.h>
23 static DEFINE_SPINLOCK(clk_lock
);
25 struct clk
*clk_get(struct device
*dev
, const char *id
)
29 for (i
= 0; i
< at32_nr_clocks
; i
++) {
30 struct clk
*clk
= at32_clock_list
[i
];
32 if (clk
->dev
== dev
&& strcmp(id
, clk
->name
) == 0)
36 return ERR_PTR(-ENOENT
);
38 EXPORT_SYMBOL(clk_get
);
40 void clk_put(struct clk
*clk
)
42 /* clocks are static for now, we can't free them */
44 EXPORT_SYMBOL(clk_put
);
46 static void __clk_enable(struct clk
*clk
)
49 __clk_enable(clk
->parent
);
50 if (clk
->users
++ == 0 && clk
->mode
)
54 int clk_enable(struct clk
*clk
)
58 spin_lock_irqsave(&clk_lock
, flags
);
60 spin_unlock_irqrestore(&clk_lock
, flags
);
64 EXPORT_SYMBOL(clk_enable
);
66 static void __clk_disable(struct clk
*clk
)
68 if (clk
->users
== 0) {
69 printk(KERN_ERR
"%s: mismatched disable\n", clk
->name
);
74 if (--clk
->users
== 0 && clk
->mode
)
77 __clk_disable(clk
->parent
);
80 void clk_disable(struct clk
*clk
)
84 spin_lock_irqsave(&clk_lock
, flags
);
86 spin_unlock_irqrestore(&clk_lock
, flags
);
88 EXPORT_SYMBOL(clk_disable
);
90 unsigned long clk_get_rate(struct clk
*clk
)
95 spin_lock_irqsave(&clk_lock
, flags
);
96 rate
= clk
->get_rate(clk
);
97 spin_unlock_irqrestore(&clk_lock
, flags
);
101 EXPORT_SYMBOL(clk_get_rate
);
103 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
105 unsigned long flags
, actual_rate
;
110 spin_lock_irqsave(&clk_lock
, flags
);
111 actual_rate
= clk
->set_rate(clk
, rate
, 0);
112 spin_unlock_irqrestore(&clk_lock
, flags
);
116 EXPORT_SYMBOL(clk_round_rate
);
118 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
126 spin_lock_irqsave(&clk_lock
, flags
);
127 ret
= clk
->set_rate(clk
, rate
, 1);
128 spin_unlock_irqrestore(&clk_lock
, flags
);
130 return (ret
< 0) ? ret
: 0;
132 EXPORT_SYMBOL(clk_set_rate
);
134 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
139 if (!clk
->set_parent
)
142 spin_lock_irqsave(&clk_lock
, flags
);
143 ret
= clk
->set_parent(clk
, parent
);
144 spin_unlock_irqrestore(&clk_lock
, flags
);
148 EXPORT_SYMBOL(clk_set_parent
);
150 struct clk
*clk_get_parent(struct clk
*clk
)
154 EXPORT_SYMBOL(clk_get_parent
);
158 #ifdef CONFIG_DEBUG_FS
160 /* /sys/kernel/debug/at32ap_clk */
162 #include <linux/io.h>
163 #include <linux/debugfs.h>
164 #include <linux/seq_file.h>
177 dump_clock(struct clk
*parent
, struct clkinf
*r
)
179 unsigned nest
= r
->nest
;
180 char buf
[16 + NEST_MAX
];
184 /* skip clocks coupled to devices that aren't registered */
185 if (parent
->dev
&& !parent
->dev
->bus_id
[0] && !parent
->users
)
188 /* <nest spaces> name <pad to end> */
189 memset(buf
, ' ', sizeof(buf
) - 1);
190 buf
[sizeof(buf
) - 1] = 0;
191 i
= strlen(parent
->name
);
192 memcpy(buf
+ nest
, parent
->name
,
193 min(i
, (unsigned)(sizeof(buf
) - 1 - nest
)));
195 seq_printf(r
->s
, "%s%c users=%2d %-3s %9ld Hz",
196 buf
, parent
->set_parent
? '*' : ' ',
198 parent
->users
? "on" : "off", /* NOTE: not-paranoid!! */
199 clk_get_rate(parent
));
201 seq_printf(r
->s
, ", for %s", parent
->dev
->bus_id
);
202 seq_printf(r
->s
, "\n");
204 /* cost of this scan is small, but not linear... */
205 r
->nest
= nest
+ NEST_DELTA
;
206 for (i
= 3; i
< at32_nr_clocks
; i
++) {
207 clk
= at32_clock_list
[i
];
208 if (clk
->parent
== parent
)
214 static int clk_show(struct seq_file
*s
, void *unused
)
219 /* show all the power manager registers */
220 seq_printf(s
, "MCCTRL = %8x\n", pm_readl(MCCTRL
));
221 seq_printf(s
, "CKSEL = %8x\n", pm_readl(CKSEL
));
222 seq_printf(s
, "CPUMASK = %8x\n", pm_readl(CPU_MASK
));
223 seq_printf(s
, "HSBMASK = %8x\n", pm_readl(HSB_MASK
));
224 seq_printf(s
, "PBAMASK = %8x\n", pm_readl(PBA_MASK
));
225 seq_printf(s
, "PBBMASK = %8x\n", pm_readl(PBB_MASK
));
226 seq_printf(s
, "PLL0 = %8x\n", pm_readl(PLL0
));
227 seq_printf(s
, "PLL1 = %8x\n", pm_readl(PLL1
));
228 seq_printf(s
, "IMR = %8x\n", pm_readl(IMR
));
229 for (i
= 0; i
< 8; i
++) {
232 seq_printf(s
, "GCCTRL%d = %8x\n", i
, pm_readl(GCCTRL(i
)));
237 /* show clock tree as derived from the three oscillators
238 * we "know" are at the head of the list
242 dump_clock(at32_clock_list
[0], &r
);
243 dump_clock(at32_clock_list
[1], &r
);
244 dump_clock(at32_clock_list
[2], &r
);
249 static int clk_open(struct inode
*inode
, struct file
*file
)
251 return single_open(file
, clk_show
, NULL
);
254 static const struct file_operations clk_operations
= {
258 .release
= single_release
,
261 static int __init
clk_debugfs_init(void)
263 (void) debugfs_create_file("at32ap_clk", S_IFREG
| S_IRUGO
,
264 NULL
, NULL
, &clk_operations
);
268 postcore_initcall(clk_debugfs_init
);