Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * drivers/sh/clk.c - SuperH clock framework |
| 3 | * |
Paul Mundt | 960bc36 | 2010-08-20 19:10:38 +0900 | [diff] [blame] | 4 | * Copyright (C) 2005 - 2010 Paul Mundt |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 5 | * |
| 6 | * This clock framework is derived from the OMAP version by: |
| 7 | * |
| 8 | * Copyright (C) 2004 - 2008 Nokia Corporation |
| 9 | * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com> |
| 10 | * |
| 11 | * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com> |
| 12 | * |
| 13 | * This file is subject to the terms and conditions of the GNU General Public |
| 14 | * License. See the file "COPYING" in the main directory of this archive |
| 15 | * for more details. |
| 16 | */ |
Paul Mundt | 550a1ef | 2010-10-13 19:24:55 +0900 | [diff] [blame] | 17 | #define pr_fmt(fmt) "clock: " fmt |
| 18 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/kobject.h> |
| 25 | #include <linux/sysdev.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/err.h> |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 28 | #include <linux/io.h> |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 29 | #include <linux/debugfs.h> |
| 30 | #include <linux/cpufreq.h> |
| 31 | #include <linux/clk.h> |
| 32 | #include <linux/sh_clk.h> |
| 33 | |
| 34 | static LIST_HEAD(clock_list); |
| 35 | static DEFINE_SPINLOCK(clock_lock); |
| 36 | static DEFINE_MUTEX(clock_list_sem); |
| 37 | |
| 38 | void clk_rate_table_build(struct clk *clk, |
| 39 | struct cpufreq_frequency_table *freq_table, |
| 40 | int nr_freqs, |
| 41 | struct clk_div_mult_table *src_table, |
| 42 | unsigned long *bitmap) |
| 43 | { |
| 44 | unsigned long mult, div; |
| 45 | unsigned long freq; |
| 46 | int i; |
| 47 | |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 48 | clk->nr_freqs = nr_freqs; |
| 49 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 50 | for (i = 0; i < nr_freqs; i++) { |
| 51 | div = 1; |
| 52 | mult = 1; |
| 53 | |
| 54 | if (src_table->divisors && i < src_table->nr_divisors) |
| 55 | div = src_table->divisors[i]; |
| 56 | |
| 57 | if (src_table->multipliers && i < src_table->nr_multipliers) |
| 58 | mult = src_table->multipliers[i]; |
| 59 | |
| 60 | if (!div || !mult || (bitmap && !test_bit(i, bitmap))) |
| 61 | freq = CPUFREQ_ENTRY_INVALID; |
| 62 | else |
| 63 | freq = clk->parent->rate * mult / div; |
| 64 | |
| 65 | freq_table[i].index = i; |
| 66 | freq_table[i].frequency = freq; |
| 67 | } |
| 68 | |
| 69 | /* Termination entry */ |
| 70 | freq_table[i].index = i; |
| 71 | freq_table[i].frequency = CPUFREQ_TABLE_END; |
| 72 | } |
| 73 | |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 74 | struct clk_rate_round_data; |
| 75 | |
| 76 | struct clk_rate_round_data { |
| 77 | unsigned long rate; |
| 78 | unsigned int min, max; |
| 79 | long (*func)(unsigned int pos, struct clk_rate_round_data *arg); |
| 80 | void *arg; |
| 81 | }; |
| 82 | |
| 83 | #define for_each_frequency(pos, r, freq) \ |
| 84 | for (pos = r->min, freq = r->func(pos, r->arg); \ |
| 85 | pos < r->max; pos++, freq = r->func(pos, r)) \ |
| 86 | if (unlikely(freq == 0)) \ |
| 87 | ; \ |
| 88 | else |
| 89 | |
| 90 | static long clk_rate_round_helper(struct clk_rate_round_data *rounder) |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 91 | { |
| 92 | unsigned long rate_error, rate_error_prev = ~0UL; |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 93 | unsigned long rate_best_fit = rounder->rate; |
| 94 | unsigned long highest, lowest, freq; |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 95 | int i; |
| 96 | |
Paul Mundt | 960bc36 | 2010-08-20 19:10:38 +0900 | [diff] [blame] | 97 | highest = 0; |
| 98 | lowest = ~0UL; |
| 99 | |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 100 | for_each_frequency(i, rounder, freq) { |
Paul Mundt | 960bc36 | 2010-08-20 19:10:38 +0900 | [diff] [blame] | 101 | if (freq > highest) |
| 102 | highest = freq; |
| 103 | if (freq < lowest) |
| 104 | lowest = freq; |
| 105 | |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 106 | rate_error = abs(freq - rounder->rate); |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 107 | if (rate_error < rate_error_prev) { |
| 108 | rate_best_fit = freq; |
| 109 | rate_error_prev = rate_error; |
| 110 | } |
| 111 | |
| 112 | if (rate_error == 0) |
| 113 | break; |
| 114 | } |
| 115 | |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 116 | if (rounder->rate >= highest) |
Paul Mundt | 960bc36 | 2010-08-20 19:10:38 +0900 | [diff] [blame] | 117 | rate_best_fit = highest; |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 118 | if (rounder->rate <= lowest) |
Paul Mundt | 960bc36 | 2010-08-20 19:10:38 +0900 | [diff] [blame] | 119 | rate_best_fit = lowest; |
| 120 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 121 | return rate_best_fit; |
| 122 | } |
| 123 | |
Paul Mundt | f586903 | 2010-10-15 18:17:35 +0900 | [diff] [blame^] | 124 | static long clk_rate_table_iter(unsigned int pos, |
| 125 | struct clk_rate_round_data *rounder) |
| 126 | { |
| 127 | struct cpufreq_frequency_table *freq_table = rounder->arg; |
| 128 | unsigned long freq = freq_table[pos].frequency; |
| 129 | |
| 130 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 131 | freq = 0; |
| 132 | |
| 133 | return freq; |
| 134 | } |
| 135 | |
| 136 | long clk_rate_table_round(struct clk *clk, |
| 137 | struct cpufreq_frequency_table *freq_table, |
| 138 | unsigned long rate) |
| 139 | { |
| 140 | struct clk_rate_round_data table_round = { |
| 141 | .min = 0, |
| 142 | .max = clk->nr_freqs, |
| 143 | .func = clk_rate_table_iter, |
| 144 | .arg = freq_table, |
| 145 | .rate = rate, |
| 146 | }; |
| 147 | |
| 148 | return clk_rate_round_helper(&table_round); |
| 149 | } |
| 150 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 151 | int clk_rate_table_find(struct clk *clk, |
| 152 | struct cpufreq_frequency_table *freq_table, |
| 153 | unsigned long rate) |
| 154 | { |
| 155 | int i; |
| 156 | |
| 157 | for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) { |
| 158 | unsigned long freq = freq_table[i].frequency; |
| 159 | |
| 160 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 161 | continue; |
| 162 | |
| 163 | if (freq == rate) |
| 164 | return i; |
| 165 | } |
| 166 | |
| 167 | return -ENOENT; |
| 168 | } |
| 169 | |
| 170 | /* Used for clocks that always have same value as the parent clock */ |
| 171 | unsigned long followparent_recalc(struct clk *clk) |
| 172 | { |
| 173 | return clk->parent ? clk->parent->rate : 0; |
| 174 | } |
| 175 | |
| 176 | int clk_reparent(struct clk *child, struct clk *parent) |
| 177 | { |
| 178 | list_del_init(&child->sibling); |
| 179 | if (parent) |
| 180 | list_add(&child->sibling, &parent->children); |
| 181 | child->parent = parent; |
| 182 | |
| 183 | /* now do the debugfs renaming to reattach the child |
| 184 | to the proper parent */ |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* Propagate rate to children */ |
| 190 | void propagate_rate(struct clk *tclk) |
| 191 | { |
| 192 | struct clk *clkp; |
| 193 | |
| 194 | list_for_each_entry(clkp, &tclk->children, sibling) { |
| 195 | if (clkp->ops && clkp->ops->recalc) |
| 196 | clkp->rate = clkp->ops->recalc(clkp); |
| 197 | |
| 198 | propagate_rate(clkp); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static void __clk_disable(struct clk *clk) |
| 203 | { |
Magnus Damm | 6939539 | 2010-10-13 07:44:36 +0000 | [diff] [blame] | 204 | if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n", |
| 205 | clk)) |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 206 | return; |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 207 | |
| 208 | if (!(--clk->usecount)) { |
| 209 | if (likely(clk->ops && clk->ops->disable)) |
| 210 | clk->ops->disable(clk); |
| 211 | if (likely(clk->parent)) |
| 212 | __clk_disable(clk->parent); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void clk_disable(struct clk *clk) |
| 217 | { |
| 218 | unsigned long flags; |
| 219 | |
| 220 | if (!clk) |
| 221 | return; |
| 222 | |
| 223 | spin_lock_irqsave(&clock_lock, flags); |
| 224 | __clk_disable(clk); |
| 225 | spin_unlock_irqrestore(&clock_lock, flags); |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(clk_disable); |
| 228 | |
| 229 | static int __clk_enable(struct clk *clk) |
| 230 | { |
| 231 | int ret = 0; |
| 232 | |
| 233 | if (clk->usecount++ == 0) { |
| 234 | if (clk->parent) { |
| 235 | ret = __clk_enable(clk->parent); |
| 236 | if (unlikely(ret)) |
| 237 | goto err; |
| 238 | } |
| 239 | |
| 240 | if (clk->ops && clk->ops->enable) { |
| 241 | ret = clk->ops->enable(clk); |
| 242 | if (ret) { |
| 243 | if (clk->parent) |
| 244 | __clk_disable(clk->parent); |
| 245 | goto err; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return ret; |
| 251 | err: |
| 252 | clk->usecount--; |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | int clk_enable(struct clk *clk) |
| 257 | { |
| 258 | unsigned long flags; |
| 259 | int ret; |
| 260 | |
| 261 | if (!clk) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | spin_lock_irqsave(&clock_lock, flags); |
| 265 | ret = __clk_enable(clk); |
| 266 | spin_unlock_irqrestore(&clock_lock, flags); |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | EXPORT_SYMBOL_GPL(clk_enable); |
| 271 | |
| 272 | static LIST_HEAD(root_clks); |
| 273 | |
| 274 | /** |
| 275 | * recalculate_root_clocks - recalculate and propagate all root clocks |
| 276 | * |
| 277 | * Recalculates all root clocks (clocks with no parent), which if the |
| 278 | * clock's .recalc is set correctly, should also propagate their rates. |
| 279 | * Called at init. |
| 280 | */ |
| 281 | void recalculate_root_clocks(void) |
| 282 | { |
| 283 | struct clk *clkp; |
| 284 | |
| 285 | list_for_each_entry(clkp, &root_clks, sibling) { |
| 286 | if (clkp->ops && clkp->ops->recalc) |
| 287 | clkp->rate = clkp->ops->recalc(clkp); |
| 288 | propagate_rate(clkp); |
| 289 | } |
| 290 | } |
| 291 | |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 292 | static struct clk_mapping dummy_mapping; |
| 293 | |
| 294 | static struct clk *lookup_root_clock(struct clk *clk) |
| 295 | { |
| 296 | while (clk->parent) |
| 297 | clk = clk->parent; |
| 298 | |
| 299 | return clk; |
| 300 | } |
| 301 | |
| 302 | static int clk_establish_mapping(struct clk *clk) |
| 303 | { |
| 304 | struct clk_mapping *mapping = clk->mapping; |
| 305 | |
| 306 | /* |
| 307 | * Propagate mappings. |
| 308 | */ |
| 309 | if (!mapping) { |
| 310 | struct clk *clkp; |
| 311 | |
| 312 | /* |
| 313 | * dummy mapping for root clocks with no specified ranges |
| 314 | */ |
| 315 | if (!clk->parent) { |
| 316 | clk->mapping = &dummy_mapping; |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * If we're on a child clock and it provides no mapping of its |
| 322 | * own, inherit the mapping from its root clock. |
| 323 | */ |
| 324 | clkp = lookup_root_clock(clk); |
| 325 | mapping = clkp->mapping; |
| 326 | BUG_ON(!mapping); |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Establish initial mapping. |
| 331 | */ |
| 332 | if (!mapping->base && mapping->phys) { |
| 333 | kref_init(&mapping->ref); |
| 334 | |
| 335 | mapping->base = ioremap_nocache(mapping->phys, mapping->len); |
| 336 | if (unlikely(!mapping->base)) |
| 337 | return -ENXIO; |
| 338 | } else if (mapping->base) { |
| 339 | /* |
| 340 | * Bump the refcount for an existing mapping |
| 341 | */ |
| 342 | kref_get(&mapping->ref); |
| 343 | } |
| 344 | |
| 345 | clk->mapping = mapping; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static void clk_destroy_mapping(struct kref *kref) |
| 350 | { |
| 351 | struct clk_mapping *mapping; |
| 352 | |
| 353 | mapping = container_of(kref, struct clk_mapping, ref); |
| 354 | |
| 355 | iounmap(mapping->base); |
| 356 | } |
| 357 | |
| 358 | static void clk_teardown_mapping(struct clk *clk) |
| 359 | { |
| 360 | struct clk_mapping *mapping = clk->mapping; |
| 361 | |
| 362 | /* Nothing to do */ |
| 363 | if (mapping == &dummy_mapping) |
| 364 | return; |
| 365 | |
| 366 | kref_put(&mapping->ref, clk_destroy_mapping); |
| 367 | clk->mapping = NULL; |
| 368 | } |
| 369 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 370 | int clk_register(struct clk *clk) |
| 371 | { |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 372 | int ret; |
| 373 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 374 | if (clk == NULL || IS_ERR(clk)) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | /* |
| 378 | * trap out already registered clocks |
| 379 | */ |
| 380 | if (clk->node.next || clk->node.prev) |
| 381 | return 0; |
| 382 | |
| 383 | mutex_lock(&clock_list_sem); |
| 384 | |
| 385 | INIT_LIST_HEAD(&clk->children); |
| 386 | clk->usecount = 0; |
| 387 | |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 388 | ret = clk_establish_mapping(clk); |
| 389 | if (unlikely(ret)) |
| 390 | goto out_unlock; |
| 391 | |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 392 | if (clk->parent) |
| 393 | list_add(&clk->sibling, &clk->parent->children); |
| 394 | else |
| 395 | list_add(&clk->sibling, &root_clks); |
| 396 | |
| 397 | list_add(&clk->node, &clock_list); |
| 398 | if (clk->ops && clk->ops->init) |
| 399 | clk->ops->init(clk); |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 400 | |
| 401 | out_unlock: |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 402 | mutex_unlock(&clock_list_sem); |
| 403 | |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 404 | return ret; |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 405 | } |
| 406 | EXPORT_SYMBOL_GPL(clk_register); |
| 407 | |
| 408 | void clk_unregister(struct clk *clk) |
| 409 | { |
| 410 | mutex_lock(&clock_list_sem); |
| 411 | list_del(&clk->sibling); |
| 412 | list_del(&clk->node); |
Paul Mundt | 28085bc | 2010-10-15 16:46:37 +0900 | [diff] [blame] | 413 | clk_teardown_mapping(clk); |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 414 | mutex_unlock(&clock_list_sem); |
| 415 | } |
| 416 | EXPORT_SYMBOL_GPL(clk_unregister); |
| 417 | |
| 418 | void clk_enable_init_clocks(void) |
| 419 | { |
| 420 | struct clk *clkp; |
| 421 | |
| 422 | list_for_each_entry(clkp, &clock_list, node) |
| 423 | if (clkp->flags & CLK_ENABLE_ON_INIT) |
| 424 | clk_enable(clkp); |
| 425 | } |
| 426 | |
| 427 | unsigned long clk_get_rate(struct clk *clk) |
| 428 | { |
| 429 | return clk->rate; |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(clk_get_rate); |
| 432 | |
| 433 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 434 | { |
| 435 | return clk_set_rate_ex(clk, rate, 0); |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(clk_set_rate); |
| 438 | |
| 439 | int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id) |
| 440 | { |
| 441 | int ret = -EOPNOTSUPP; |
| 442 | unsigned long flags; |
| 443 | |
| 444 | spin_lock_irqsave(&clock_lock, flags); |
| 445 | |
| 446 | if (likely(clk->ops && clk->ops->set_rate)) { |
| 447 | ret = clk->ops->set_rate(clk, rate, algo_id); |
| 448 | if (ret != 0) |
| 449 | goto out_unlock; |
| 450 | } else { |
| 451 | clk->rate = rate; |
| 452 | ret = 0; |
| 453 | } |
| 454 | |
| 455 | if (clk->ops && clk->ops->recalc) |
| 456 | clk->rate = clk->ops->recalc(clk); |
| 457 | |
| 458 | propagate_rate(clk); |
| 459 | |
| 460 | out_unlock: |
| 461 | spin_unlock_irqrestore(&clock_lock, flags); |
| 462 | |
| 463 | return ret; |
| 464 | } |
| 465 | EXPORT_SYMBOL_GPL(clk_set_rate_ex); |
| 466 | |
| 467 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 468 | { |
| 469 | unsigned long flags; |
| 470 | int ret = -EINVAL; |
| 471 | |
| 472 | if (!parent || !clk) |
| 473 | return ret; |
| 474 | if (clk->parent == parent) |
| 475 | return 0; |
| 476 | |
| 477 | spin_lock_irqsave(&clock_lock, flags); |
| 478 | if (clk->usecount == 0) { |
| 479 | if (clk->ops->set_parent) |
| 480 | ret = clk->ops->set_parent(clk, parent); |
| 481 | else |
| 482 | ret = clk_reparent(clk, parent); |
| 483 | |
| 484 | if (ret == 0) { |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 485 | if (clk->ops->recalc) |
| 486 | clk->rate = clk->ops->recalc(clk); |
Paul Mundt | 550a1ef | 2010-10-13 19:24:55 +0900 | [diff] [blame] | 487 | pr_debug("set parent of %p to %p (new rate %ld)\n", |
Magnus Damm | 6939539 | 2010-10-13 07:44:36 +0000 | [diff] [blame] | 488 | clk, clk->parent, clk->rate); |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 489 | propagate_rate(clk); |
| 490 | } |
| 491 | } else |
| 492 | ret = -EBUSY; |
| 493 | spin_unlock_irqrestore(&clock_lock, flags); |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(clk_set_parent); |
| 498 | |
| 499 | struct clk *clk_get_parent(struct clk *clk) |
| 500 | { |
| 501 | return clk->parent; |
| 502 | } |
| 503 | EXPORT_SYMBOL_GPL(clk_get_parent); |
| 504 | |
| 505 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 506 | { |
| 507 | if (likely(clk->ops && clk->ops->round_rate)) { |
| 508 | unsigned long flags, rounded; |
| 509 | |
| 510 | spin_lock_irqsave(&clock_lock, flags); |
| 511 | rounded = clk->ops->round_rate(clk, rate); |
| 512 | spin_unlock_irqrestore(&clock_lock, flags); |
| 513 | |
| 514 | return rounded; |
| 515 | } |
| 516 | |
| 517 | return clk_get_rate(clk); |
| 518 | } |
| 519 | EXPORT_SYMBOL_GPL(clk_round_rate); |
| 520 | |
| 521 | #ifdef CONFIG_PM |
| 522 | static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state) |
| 523 | { |
| 524 | static pm_message_t prev_state; |
| 525 | struct clk *clkp; |
| 526 | |
| 527 | switch (state.event) { |
| 528 | case PM_EVENT_ON: |
| 529 | /* Resumeing from hibernation */ |
| 530 | if (prev_state.event != PM_EVENT_FREEZE) |
| 531 | break; |
| 532 | |
| 533 | list_for_each_entry(clkp, &clock_list, node) { |
| 534 | if (likely(clkp->ops)) { |
| 535 | unsigned long rate = clkp->rate; |
| 536 | |
| 537 | if (likely(clkp->ops->set_parent)) |
| 538 | clkp->ops->set_parent(clkp, |
| 539 | clkp->parent); |
| 540 | if (likely(clkp->ops->set_rate)) |
| 541 | clkp->ops->set_rate(clkp, |
| 542 | rate, NO_CHANGE); |
| 543 | else if (likely(clkp->ops->recalc)) |
| 544 | clkp->rate = clkp->ops->recalc(clkp); |
| 545 | } |
| 546 | } |
| 547 | break; |
| 548 | case PM_EVENT_FREEZE: |
| 549 | break; |
| 550 | case PM_EVENT_SUSPEND: |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | prev_state = state; |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int clks_sysdev_resume(struct sys_device *dev) |
| 559 | { |
| 560 | return clks_sysdev_suspend(dev, PMSG_ON); |
| 561 | } |
| 562 | |
| 563 | static struct sysdev_class clks_sysdev_class = { |
| 564 | .name = "clks", |
| 565 | }; |
| 566 | |
| 567 | static struct sysdev_driver clks_sysdev_driver = { |
| 568 | .suspend = clks_sysdev_suspend, |
| 569 | .resume = clks_sysdev_resume, |
| 570 | }; |
| 571 | |
| 572 | static struct sys_device clks_sysdev_dev = { |
| 573 | .cls = &clks_sysdev_class, |
| 574 | }; |
| 575 | |
| 576 | static int __init clk_sysdev_init(void) |
| 577 | { |
| 578 | sysdev_class_register(&clks_sysdev_class); |
| 579 | sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver); |
| 580 | sysdev_register(&clks_sysdev_dev); |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | subsys_initcall(clk_sysdev_init); |
| 585 | #endif |
| 586 | |
| 587 | /* |
| 588 | * debugfs support to trace clock tree hierarchy and attributes |
| 589 | */ |
| 590 | static struct dentry *clk_debugfs_root; |
| 591 | |
| 592 | static int clk_debugfs_register_one(struct clk *c) |
| 593 | { |
| 594 | int err; |
| 595 | struct dentry *d, *child, *child_tmp; |
| 596 | struct clk *pa = c->parent; |
| 597 | char s[255]; |
| 598 | char *p = s; |
| 599 | |
Magnus Damm | 6939539 | 2010-10-13 07:44:36 +0000 | [diff] [blame] | 600 | p += sprintf(p, "%p", c); |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 601 | d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root); |
| 602 | if (!d) |
| 603 | return -ENOMEM; |
| 604 | c->dentry = d; |
| 605 | |
| 606 | d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount); |
| 607 | if (!d) { |
| 608 | err = -ENOMEM; |
| 609 | goto err_out; |
| 610 | } |
| 611 | d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate); |
| 612 | if (!d) { |
| 613 | err = -ENOMEM; |
| 614 | goto err_out; |
| 615 | } |
| 616 | d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags); |
| 617 | if (!d) { |
| 618 | err = -ENOMEM; |
| 619 | goto err_out; |
| 620 | } |
| 621 | return 0; |
| 622 | |
| 623 | err_out: |
| 624 | d = c->dentry; |
| 625 | list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child) |
| 626 | debugfs_remove(child); |
| 627 | debugfs_remove(c->dentry); |
| 628 | return err; |
| 629 | } |
| 630 | |
| 631 | static int clk_debugfs_register(struct clk *c) |
| 632 | { |
| 633 | int err; |
| 634 | struct clk *pa = c->parent; |
| 635 | |
| 636 | if (pa && !pa->dentry) { |
| 637 | err = clk_debugfs_register(pa); |
| 638 | if (err) |
| 639 | return err; |
| 640 | } |
| 641 | |
Magnus Damm | 6939539 | 2010-10-13 07:44:36 +0000 | [diff] [blame] | 642 | if (!c->dentry) { |
Magnus Damm | 8b5ee11 | 2010-05-11 13:29:25 +0000 | [diff] [blame] | 643 | err = clk_debugfs_register_one(c); |
| 644 | if (err) |
| 645 | return err; |
| 646 | } |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int __init clk_debugfs_init(void) |
| 651 | { |
| 652 | struct clk *c; |
| 653 | struct dentry *d; |
| 654 | int err; |
| 655 | |
| 656 | d = debugfs_create_dir("clock", NULL); |
| 657 | if (!d) |
| 658 | return -ENOMEM; |
| 659 | clk_debugfs_root = d; |
| 660 | |
| 661 | list_for_each_entry(c, &clock_list, node) { |
| 662 | err = clk_debugfs_register(c); |
| 663 | if (err) |
| 664 | goto err_out; |
| 665 | } |
| 666 | return 0; |
| 667 | err_out: |
| 668 | debugfs_remove_recursive(clk_debugfs_root); |
| 669 | return err; |
| 670 | } |
| 671 | late_initcall(clk_debugfs_init); |