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/export.h>
17 #include <linux/device.h>
18 #include <linux/string.h>
19 #include <linux/list.h>
21 #include <mach/chip.h>
26 static LIST_HEAD(at32_clock_list);
28 static DEFINE_SPINLOCK(clk_lock);
29 static DEFINE_SPINLOCK(clk_list_lock);
31 void at32_clk_register(struct clk *clk)
33 spin_lock(&clk_list_lock);
34 /* add the new item to the end of the list */
35 list_add_tail(&clk->list, &at32_clock_list);
36 spin_unlock(&clk_list_lock);
39 static struct clk *__clk_get(struct device *dev, const char *id)
43 list_for_each_entry(clk, &at32_clock_list, list) {
44 if (clk->dev == dev && strcmp(id, clk->name) == 0) {
49 return ERR_PTR(-ENOENT);
52 struct clk *clk_get(struct device *dev, const char *id)
56 spin_lock(&clk_list_lock);
57 clk = __clk_get(dev, id);
58 spin_unlock(&clk_list_lock);
63 EXPORT_SYMBOL(clk_get);
65 void clk_put(struct clk *clk)
67 /* clocks are static for now, we can't free them */
69 EXPORT_SYMBOL(clk_put);
71 static void __clk_enable(struct clk *clk)
74 __clk_enable(clk->parent);
75 if (clk->users++ == 0 && clk->mode)
79 int clk_enable(struct clk *clk)
86 spin_lock_irqsave(&clk_lock, flags);
88 spin_unlock_irqrestore(&clk_lock, flags);
92 EXPORT_SYMBOL(clk_enable);
94 static void __clk_disable(struct clk *clk)
96 if (clk->users == 0) {
97 printk(KERN_ERR "%s: mismatched disable\n", clk->name);
102 if (--clk->users == 0 && clk->mode)
105 __clk_disable(clk->parent);
108 void clk_disable(struct clk *clk)
112 if (IS_ERR_OR_NULL(clk))
115 spin_lock_irqsave(&clk_lock, flags);
117 spin_unlock_irqrestore(&clk_lock, flags);
119 EXPORT_SYMBOL(clk_disable);
121 unsigned long clk_get_rate(struct clk *clk)
129 spin_lock_irqsave(&clk_lock, flags);
130 rate = clk->get_rate(clk);
131 spin_unlock_irqrestore(&clk_lock, flags);
135 EXPORT_SYMBOL(clk_get_rate);
137 long clk_round_rate(struct clk *clk, unsigned long rate)
139 unsigned long flags, actual_rate;
147 spin_lock_irqsave(&clk_lock, flags);
148 actual_rate = clk->set_rate(clk, rate, 0);
149 spin_unlock_irqrestore(&clk_lock, flags);
153 EXPORT_SYMBOL(clk_round_rate);
155 int clk_set_rate(struct clk *clk, unsigned long rate)
166 spin_lock_irqsave(&clk_lock, flags);
167 ret = clk->set_rate(clk, rate, 1);
168 spin_unlock_irqrestore(&clk_lock, flags);
170 return (ret < 0) ? ret : 0;
172 EXPORT_SYMBOL(clk_set_rate);
174 int clk_set_parent(struct clk *clk, struct clk *parent)
182 if (!clk->set_parent)
185 spin_lock_irqsave(&clk_lock, flags);
186 ret = clk->set_parent(clk, parent);
187 spin_unlock_irqrestore(&clk_lock, flags);
191 EXPORT_SYMBOL(clk_set_parent);
193 struct clk *clk_get_parent(struct clk *clk)
195 return !clk ? NULL : clk->parent;
197 EXPORT_SYMBOL(clk_get_parent);
201 #ifdef CONFIG_DEBUG_FS
203 /* /sys/kernel/debug/at32ap_clk */
205 #include <linux/io.h>
206 #include <linux/debugfs.h>
207 #include <linux/seq_file.h>
220 dump_clock(struct clk *parent, struct clkinf *r)
222 unsigned nest = r->nest;
223 char buf[16 + NEST_MAX];
227 /* skip clocks coupled to devices that aren't registered */
228 if (parent->dev && !dev_name(parent->dev) && !parent->users)
231 /* <nest spaces> name <pad to end> */
232 memset(buf, ' ', sizeof(buf) - 1);
233 buf[sizeof(buf) - 1] = 0;
234 i = strlen(parent->name);
235 memcpy(buf + nest, parent->name,
236 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
238 seq_printf(r->s, "%s%c users=%2d %-3s %9ld Hz",
239 buf, parent->set_parent ? '*' : ' ',
241 parent->users ? "on" : "off", /* NOTE: not-paranoid!! */
242 clk_get_rate(parent));
244 seq_printf(r->s, ", for %s", dev_name(parent->dev));
245 seq_printf(r->s, "\n");
247 /* cost of this scan is small, but not linear... */
248 r->nest = nest + NEST_DELTA;
250 list_for_each_entry(clk, &at32_clock_list, list) {
251 if (clk->parent == parent)
257 static int clk_show(struct seq_file *s, void *unused)
263 /* show all the power manager registers */
264 seq_printf(s, "MCCTRL = %8x\n", pm_readl(MCCTRL));
265 seq_printf(s, "CKSEL = %8x\n", pm_readl(CKSEL));
266 seq_printf(s, "CPUMASK = %8x\n", pm_readl(CPU_MASK));
267 seq_printf(s, "HSBMASK = %8x\n", pm_readl(HSB_MASK));
268 seq_printf(s, "PBAMASK = %8x\n", pm_readl(PBA_MASK));
269 seq_printf(s, "PBBMASK = %8x\n", pm_readl(PBB_MASK));
270 seq_printf(s, "PLL0 = %8x\n", pm_readl(PLL0));
271 seq_printf(s, "PLL1 = %8x\n", pm_readl(PLL1));
272 seq_printf(s, "IMR = %8x\n", pm_readl(IMR));
273 for (i = 0; i < 8; i++) {
276 seq_printf(s, "GCCTRL%d = %8x\n", i, pm_readl(GCCTRL(i)));
283 /* protected from changes on the list while dumping */
284 spin_lock(&clk_list_lock);
286 /* show clock tree as derived from the three oscillators */
287 clk = __clk_get(NULL, "osc32k");
291 clk = __clk_get(NULL, "osc0");
295 clk = __clk_get(NULL, "osc1");
299 spin_unlock(&clk_list_lock);
304 static int clk_open(struct inode *inode, struct file *file)
306 return single_open(file, clk_show, NULL);
309 static const struct file_operations clk_operations = {
313 .release = single_release,
316 static int __init clk_debugfs_init(void)
318 (void) debugfs_create_file("at32ap_clk", S_IFREG | S_IRUGO,
319 NULL, NULL, &clk_operations);
323 postcore_initcall(clk_debugfs_init);