Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tony Lindgren | b915855 | 2005-07-10 19:58:14 +0100 | [diff] [blame] | 2 | * linux/arch/arm/plat-omap/clock.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 4 | * Copyright (C) 2004 - 2005 Nokia corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com> |
| 6 | * |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 7 | * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com> |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 13 | #include <linux/version.h> |
| 14 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/err.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 21 | #include <linux/string.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 22 | #include <linux/clk.h> |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Tony Lindgren | bb13b5f | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 25 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 28 | #include <asm/arch/clock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 30 | LIST_HEAD(clocks); |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 31 | static DEFINE_MUTEX(clocks_mutex); |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 32 | DEFINE_SPINLOCK(clockfw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 34 | static struct clk_functions *arch_clock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 36 | /*------------------------------------------------------------------------- |
Tony Lindgren | f07adc5 | 2006-01-17 15:27:09 -0800 | [diff] [blame] | 37 | * Standard clock functions defined in include/linux/clk.h |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 38 | *-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 40 | struct clk * clk_get(struct device *dev, const char *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | struct clk *p, *clk = ERR_PTR(-ENOENT); |
| 43 | |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 44 | mutex_lock(&clocks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | list_for_each_entry(p, &clocks, node) { |
| 46 | if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) { |
| 47 | clk = p; |
| 48 | break; |
| 49 | } |
| 50 | } |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 51 | mutex_unlock(&clocks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | return clk; |
| 54 | } |
| 55 | EXPORT_SYMBOL(clk_get); |
| 56 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 57 | int clk_enable(struct clk *clk) |
| 58 | { |
| 59 | unsigned long flags; |
| 60 | int ret = 0; |
| 61 | |
| 62 | spin_lock_irqsave(&clockfw_lock, flags); |
Tony Lindgren | f07adc5 | 2006-01-17 15:27:09 -0800 | [diff] [blame] | 63 | if (arch_clock->clk_enable) |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 64 | ret = arch_clock->clk_enable(clk); |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 65 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | EXPORT_SYMBOL(clk_enable); |
| 70 | |
| 71 | void clk_disable(struct clk *clk) |
| 72 | { |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(&clockfw_lock, flags); |
Tony Lindgren | f07adc5 | 2006-01-17 15:27:09 -0800 | [diff] [blame] | 76 | if (arch_clock->clk_disable) |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 77 | arch_clock->clk_disable(clk); |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 78 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 79 | } |
| 80 | EXPORT_SYMBOL(clk_disable); |
| 81 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 82 | int clk_get_usecount(struct clk *clk) |
| 83 | { |
| 84 | unsigned long flags; |
| 85 | int ret = 0; |
| 86 | |
| 87 | spin_lock_irqsave(&clockfw_lock, flags); |
| 88 | ret = clk->usecount; |
| 89 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | EXPORT_SYMBOL(clk_get_usecount); |
| 94 | |
| 95 | unsigned long clk_get_rate(struct clk *clk) |
| 96 | { |
| 97 | unsigned long flags; |
| 98 | unsigned long ret = 0; |
| 99 | |
| 100 | spin_lock_irqsave(&clockfw_lock, flags); |
| 101 | ret = clk->rate; |
| 102 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | EXPORT_SYMBOL(clk_get_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | void clk_put(struct clk *clk) |
| 109 | { |
| 110 | if (clk && !IS_ERR(clk)) |
| 111 | module_put(clk->owner); |
| 112 | } |
| 113 | EXPORT_SYMBOL(clk_put); |
| 114 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 115 | /*------------------------------------------------------------------------- |
Tony Lindgren | f07adc5 | 2006-01-17 15:27:09 -0800 | [diff] [blame] | 116 | * Optional clock functions defined in include/linux/clk.h |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 117 | *-------------------------------------------------------------------------*/ |
Tony Lindgren | bb13b5f | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 120 | { |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 121 | unsigned long flags; |
| 122 | long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 124 | spin_lock_irqsave(&clockfw_lock, flags); |
| 125 | if (arch_clock->clk_round_rate) |
| 126 | ret = arch_clock->clk_round_rate(clk, rate); |
| 127 | spin_unlock_irqrestore(&clockfw_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 129 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | EXPORT_SYMBOL(clk_round_rate); |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 134 | { |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 135 | unsigned long flags; |
| 136 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 138 | spin_lock_irqsave(&clockfw_lock, flags); |
| 139 | if (arch_clock->clk_set_rate) |
| 140 | ret = arch_clock->clk_set_rate(clk, rate); |
| 141 | spin_unlock_irqrestore(&clockfw_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | EXPORT_SYMBOL(clk_set_rate); |
| 146 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 147 | int clk_set_parent(struct clk *clk, struct clk *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 149 | unsigned long flags; |
| 150 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 152 | spin_lock_irqsave(&clockfw_lock, flags); |
| 153 | if (arch_clock->clk_set_parent) |
| 154 | ret = arch_clock->clk_set_parent(clk, parent); |
| 155 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | EXPORT_SYMBOL(clk_set_parent); |
| 160 | |
| 161 | struct clk *clk_get_parent(struct clk *clk) |
| 162 | { |
| 163 | unsigned long flags; |
| 164 | struct clk * ret = NULL; |
| 165 | |
| 166 | spin_lock_irqsave(&clockfw_lock, flags); |
| 167 | if (arch_clock->clk_get_parent) |
| 168 | ret = arch_clock->clk_get_parent(clk); |
| 169 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | EXPORT_SYMBOL(clk_get_parent); |
| 174 | |
| 175 | /*------------------------------------------------------------------------- |
| 176 | * OMAP specific clock functions shared between omap1 and omap2 |
| 177 | *-------------------------------------------------------------------------*/ |
| 178 | |
| 179 | unsigned int __initdata mpurate; |
| 180 | |
| 181 | /* |
| 182 | * By default we use the rate set by the bootloader. |
| 183 | * You can override this with mpurate= cmdline option. |
| 184 | */ |
| 185 | static int __init omap_clk_setup(char *str) |
| 186 | { |
| 187 | get_option(&str, &mpurate); |
| 188 | |
| 189 | if (!mpurate) |
| 190 | return 1; |
| 191 | |
| 192 | if (mpurate < 1000) |
| 193 | mpurate *= 1000000; |
| 194 | |
| 195 | return 1; |
| 196 | } |
| 197 | __setup("mpurate=", omap_clk_setup); |
| 198 | |
| 199 | /* Used for clocks that always have same value as the parent clock */ |
| 200 | void followparent_recalc(struct clk *clk) |
| 201 | { |
| 202 | clk->rate = clk->parent->rate; |
| 203 | } |
| 204 | |
| 205 | /* Propagate rate to children */ |
| 206 | void propagate_rate(struct clk * tclk) |
| 207 | { |
| 208 | struct clk *clkp; |
| 209 | |
| 210 | list_for_each_entry(clkp, &clocks, node) { |
| 211 | if (likely(clkp->parent != tclk)) |
| 212 | continue; |
| 213 | if (likely((u32)clkp->recalc)) |
| 214 | clkp->recalc(clkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int clk_register(struct clk *clk) |
| 219 | { |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 220 | mutex_lock(&clocks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | list_add(&clk->node, &clocks); |
| 222 | if (clk->init) |
| 223 | clk->init(clk); |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 224 | mutex_unlock(&clocks_mutex); |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | EXPORT_SYMBOL(clk_register); |
| 229 | |
| 230 | void clk_unregister(struct clk *clk) |
| 231 | { |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 232 | mutex_lock(&clocks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | list_del(&clk->node); |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 234 | mutex_unlock(&clocks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL(clk_unregister); |
| 237 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 238 | void clk_deny_idle(struct clk *clk) |
Tony Lindgren | bb13b5f | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 239 | { |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 240 | unsigned long flags; |
| 241 | |
| 242 | spin_lock_irqsave(&clockfw_lock, flags); |
| 243 | if (arch_clock->clk_deny_idle) |
| 244 | arch_clock->clk_deny_idle(clk); |
| 245 | spin_unlock_irqrestore(&clockfw_lock, flags); |
Tony Lindgren | bb13b5f | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 246 | } |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 247 | EXPORT_SYMBOL(clk_deny_idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 249 | void clk_allow_idle(struct clk *clk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 251 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 253 | spin_lock_irqsave(&clockfw_lock, flags); |
| 254 | if (arch_clock->clk_allow_idle) |
| 255 | arch_clock->clk_allow_idle(clk); |
| 256 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 257 | } |
| 258 | EXPORT_SYMBOL(clk_allow_idle); |
Tony Lindgren | bb13b5f | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 259 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 260 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 262 | int __init clk_init(struct clk_functions * custom_clocks) |
| 263 | { |
| 264 | if (!custom_clocks) { |
| 265 | printk(KERN_ERR "No custom clock functions registered\n"); |
| 266 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 269 | arch_clock = custom_clocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |