Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / arch / arm / mach-omap2 / clockdomain.c
blob764666f9c547c85add430ba99a3c3be5ec45a2d2
1 /*
2 * OMAP2/3/4 clockdomain framework functions
4 * Copyright (C) 2008-2011 Texas Instruments, Inc.
5 * Copyright (C) 2008-2011 Nokia Corporation
7 * Written by Paul Walmsley and Jouni Högander
8 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
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 #undef DEBUG
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/list.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/limits.h>
24 #include <linux/err.h>
26 #include <linux/io.h>
28 #include <linux/bitops.h>
30 #include <plat/clock.h>
31 #include "clockdomain.h"
33 /* clkdm_list contains all registered struct clockdomains */
34 static LIST_HEAD(clkdm_list);
36 /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
37 static struct clkdm_autodep *autodeps;
39 static struct clkdm_ops *arch_clkdm;
41 /* Private functions */
43 static struct clockdomain *_clkdm_lookup(const char *name)
45 struct clockdomain *clkdm, *temp_clkdm;
47 if (!name)
48 return NULL;
50 clkdm = NULL;
52 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
53 if (!strcmp(name, temp_clkdm->name)) {
54 clkdm = temp_clkdm;
55 break;
59 return clkdm;
62 /**
63 * _clkdm_register - register a clockdomain
64 * @clkdm: struct clockdomain * to register
66 * Adds a clockdomain to the internal clockdomain list.
67 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
68 * already registered by the provided name, or 0 upon success.
70 static int _clkdm_register(struct clockdomain *clkdm)
72 struct powerdomain *pwrdm;
74 if (!clkdm || !clkdm->name)
75 return -EINVAL;
77 if (!omap_chip_is(clkdm->omap_chip))
78 return -EINVAL;
80 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
81 if (!pwrdm) {
82 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
83 clkdm->name, clkdm->pwrdm.name);
84 return -EINVAL;
86 clkdm->pwrdm.ptr = pwrdm;
88 /* Verify that the clockdomain is not already registered */
89 if (_clkdm_lookup(clkdm->name))
90 return -EEXIST;
92 list_add(&clkdm->node, &clkdm_list);
94 pwrdm_add_clkdm(pwrdm, clkdm);
96 spin_lock_init(&clkdm->lock);
98 pr_debug("clockdomain: registered %s\n", clkdm->name);
100 return 0;
103 /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
104 static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
105 struct clkdm_dep *deps)
107 struct clkdm_dep *cd;
109 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
110 return ERR_PTR(-EINVAL);
112 for (cd = deps; cd->clkdm_name; cd++) {
113 if (!omap_chip_is(cd->omap_chip))
114 continue;
116 if (!cd->clkdm && cd->clkdm_name)
117 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
119 if (cd->clkdm == clkdm)
120 break;
123 if (!cd->clkdm_name)
124 return ERR_PTR(-ENOENT);
126 return cd;
130 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
131 * @autodep: struct clkdm_autodep * to resolve
133 * Resolve autodep clockdomain names to clockdomain pointers via
134 * clkdm_lookup() and store the pointers in the autodep structure. An
135 * "autodep" is a clockdomain sleep/wakeup dependency that is
136 * automatically added and removed whenever clocks in the associated
137 * clockdomain are enabled or disabled (respectively) when the
138 * clockdomain is in hardware-supervised mode. Meant to be called
139 * once at clockdomain layer initialization, since these should remain
140 * fixed for a particular architecture. No return value.
142 * XXX autodeps are deprecated and should be removed at the earliest
143 * opportunity
145 static void _autodep_lookup(struct clkdm_autodep *autodep)
147 struct clockdomain *clkdm;
149 if (!autodep)
150 return;
152 if (!omap_chip_is(autodep->omap_chip))
153 return;
155 clkdm = clkdm_lookup(autodep->clkdm.name);
156 if (!clkdm) {
157 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
158 autodep->clkdm.name);
159 clkdm = ERR_PTR(-ENOENT);
161 autodep->clkdm.ptr = clkdm;
165 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
166 * @clkdm: struct clockdomain *
168 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
169 * in hardware-supervised mode. Meant to be called from clock framework
170 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
172 * XXX autodeps are deprecated and should be removed at the earliest
173 * opportunity
175 void _clkdm_add_autodeps(struct clockdomain *clkdm)
177 struct clkdm_autodep *autodep;
179 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
180 return;
182 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
183 if (IS_ERR(autodep->clkdm.ptr))
184 continue;
186 if (!omap_chip_is(autodep->omap_chip))
187 continue;
189 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
190 "clkdm %s\n", autodep->clkdm.ptr->name,
191 clkdm->name);
193 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
194 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
199 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
200 * @clkdm: struct clockdomain *
202 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
203 * in hardware-supervised mode. Meant to be called from clock framework
204 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
206 * XXX autodeps are deprecated and should be removed at the earliest
207 * opportunity
209 void _clkdm_del_autodeps(struct clockdomain *clkdm)
211 struct clkdm_autodep *autodep;
213 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
214 return;
216 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
217 if (IS_ERR(autodep->clkdm.ptr))
218 continue;
220 if (!omap_chip_is(autodep->omap_chip))
221 continue;
223 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
224 "clkdm %s\n", autodep->clkdm.ptr->name,
225 clkdm->name);
227 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
228 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
233 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
234 * @clkdm: clockdomain that we are resolving dependencies for
235 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
237 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
238 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
239 * No return value.
241 static void _resolve_clkdm_deps(struct clockdomain *clkdm,
242 struct clkdm_dep *clkdm_deps)
244 struct clkdm_dep *cd;
246 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
247 if (!omap_chip_is(cd->omap_chip))
248 continue;
249 if (cd->clkdm)
250 continue;
251 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
253 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
254 clkdm->name, cd->clkdm_name);
258 /* Public functions */
261 * clkdm_init - set up the clockdomain layer
262 * @clkdms: optional pointer to an array of clockdomains to register
263 * @init_autodeps: optional pointer to an array of autodeps to register
264 * @custom_funcs: func pointers for arch specific implementations
266 * Set up internal state. If a pointer to an array of clockdomains
267 * @clkdms was supplied, loop through the list of clockdomains,
268 * register all that are available on the current platform. Similarly,
269 * if a pointer to an array of clockdomain autodependencies
270 * @init_autodeps was provided, register those. No return value.
272 void clkdm_init(struct clockdomain **clkdms,
273 struct clkdm_autodep *init_autodeps,
274 struct clkdm_ops *custom_funcs)
276 struct clockdomain **c = NULL;
277 struct clockdomain *clkdm;
278 struct clkdm_autodep *autodep = NULL;
280 if (!custom_funcs)
281 WARN(1, "No custom clkdm functions registered\n");
282 else
283 arch_clkdm = custom_funcs;
285 if (clkdms)
286 for (c = clkdms; *c; c++)
287 _clkdm_register(*c);
289 autodeps = init_autodeps;
290 if (autodeps)
291 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
292 _autodep_lookup(autodep);
295 * Put all clockdomains into software-supervised mode; PM code
296 * should later enable hardware-supervised mode as appropriate
298 list_for_each_entry(clkdm, &clkdm_list, node) {
299 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
300 clkdm_wakeup(clkdm);
301 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
302 clkdm_deny_idle(clkdm);
304 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
305 clkdm_clear_all_wkdeps(clkdm);
307 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
308 clkdm_clear_all_sleepdeps(clkdm);
313 * clkdm_lookup - look up a clockdomain by name, return a pointer
314 * @name: name of clockdomain
316 * Find a registered clockdomain by its name @name. Returns a pointer
317 * to the struct clockdomain if found, or NULL otherwise.
319 struct clockdomain *clkdm_lookup(const char *name)
321 struct clockdomain *clkdm, *temp_clkdm;
323 if (!name)
324 return NULL;
326 clkdm = NULL;
328 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
329 if (!strcmp(name, temp_clkdm->name)) {
330 clkdm = temp_clkdm;
331 break;
335 return clkdm;
339 * clkdm_for_each - call function on each registered clockdomain
340 * @fn: callback function *
342 * Call the supplied function @fn for each registered clockdomain.
343 * The callback function @fn can return anything but 0 to bail
344 * out early from the iterator. The callback function is called with
345 * the clkdm_mutex held, so no clockdomain structure manipulation
346 * functions should be called from the callback, although hardware
347 * clockdomain control functions are fine. Returns the last return
348 * value of the callback function, which should be 0 for success or
349 * anything else to indicate failure; or -EINVAL if the function pointer
350 * is null.
352 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
353 void *user)
355 struct clockdomain *clkdm;
356 int ret = 0;
358 if (!fn)
359 return -EINVAL;
361 list_for_each_entry(clkdm, &clkdm_list, node) {
362 ret = (*fn)(clkdm, user);
363 if (ret)
364 break;
367 return ret;
372 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
373 * @clkdm: struct clockdomain *
375 * Return a pointer to the struct powerdomain that the specified clockdomain
376 * @clkdm exists in, or returns NULL if @clkdm is NULL.
378 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
380 if (!clkdm)
381 return NULL;
383 return clkdm->pwrdm.ptr;
387 /* Hardware clockdomain control */
390 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
391 * @clkdm1: wake this struct clockdomain * up (dependent)
392 * @clkdm2: when this struct clockdomain * wakes up (source)
394 * When the clockdomain represented by @clkdm2 wakes up, wake up
395 * @clkdm1. Implemented in hardware on the OMAP, this feature is
396 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
397 * Returns -EINVAL if presented with invalid clockdomain pointers,
398 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
399 * success.
401 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
403 struct clkdm_dep *cd;
404 int ret = 0;
406 if (!clkdm1 || !clkdm2)
407 return -EINVAL;
409 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
410 if (IS_ERR(cd))
411 ret = PTR_ERR(cd);
413 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
414 ret = -EINVAL;
416 if (ret) {
417 pr_debug("clockdomain: hardware cannot set/clear wake up of "
418 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
419 return ret;
422 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
423 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
424 "up\n", clkdm1->name, clkdm2->name);
426 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
429 return ret;
433 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
434 * @clkdm1: wake this struct clockdomain * up (dependent)
435 * @clkdm2: when this struct clockdomain * wakes up (source)
437 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
438 * wakes up. Returns -EINVAL if presented with invalid clockdomain
439 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
440 * 0 upon success.
442 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
444 struct clkdm_dep *cd;
445 int ret = 0;
447 if (!clkdm1 || !clkdm2)
448 return -EINVAL;
450 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
451 if (IS_ERR(cd))
452 ret = PTR_ERR(cd);
454 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
455 ret = -EINVAL;
457 if (ret) {
458 pr_debug("clockdomain: hardware cannot set/clear wake up of "
459 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
460 return ret;
463 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
464 pr_debug("clockdomain: hardware will no longer wake up %s "
465 "after %s wakes up\n", clkdm1->name, clkdm2->name);
467 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
470 return ret;
474 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
475 * @clkdm1: wake this struct clockdomain * up (dependent)
476 * @clkdm2: when this struct clockdomain * wakes up (source)
478 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
479 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
480 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
481 * is incapable.
483 * REVISIT: Currently this function only represents software-controllable
484 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
485 * yet handled here.
487 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
489 struct clkdm_dep *cd;
490 int ret = 0;
492 if (!clkdm1 || !clkdm2)
493 return -EINVAL;
495 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
496 if (IS_ERR(cd))
497 ret = PTR_ERR(cd);
499 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
500 ret = -EINVAL;
502 if (ret) {
503 pr_debug("clockdomain: hardware cannot set/clear wake up of "
504 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
505 return ret;
508 /* XXX It's faster to return the atomic wkdep_usecount */
509 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
513 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
514 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
516 * Remove all inter-clockdomain wakeup dependencies that could cause
517 * @clkdm to wake. Intended to be used during boot to initialize the
518 * PRCM to a known state, after all clockdomains are put into swsup idle
519 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
520 * 0 upon success.
522 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
524 if (!clkdm)
525 return -EINVAL;
527 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
528 return -EINVAL;
530 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
534 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
535 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
536 * @clkdm2: when this struct clockdomain * is active (source)
538 * Prevent @clkdm1 from automatically going inactive (and then to
539 * retention or off) if @clkdm2 is active. Returns -EINVAL if
540 * presented with invalid clockdomain pointers or called on a machine
541 * that does not support software-configurable hardware sleep
542 * dependencies, -ENOENT if the specified dependency cannot be set in
543 * hardware, or 0 upon success.
545 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
547 struct clkdm_dep *cd;
548 int ret = 0;
550 if (!clkdm1 || !clkdm2)
551 return -EINVAL;
553 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
554 if (IS_ERR(cd))
555 ret = PTR_ERR(cd);
557 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
558 ret = -EINVAL;
560 if (ret) {
561 pr_debug("clockdomain: hardware cannot set/clear sleep "
562 "dependency affecting %s from %s\n", clkdm1->name,
563 clkdm2->name);
564 return ret;
567 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
568 pr_debug("clockdomain: will prevent %s from sleeping if %s "
569 "is active\n", clkdm1->name, clkdm2->name);
571 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
574 return ret;
578 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
579 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
580 * @clkdm2: when this struct clockdomain * is active (source)
582 * Allow @clkdm1 to automatically go inactive (and then to retention or
583 * off), independent of the activity state of @clkdm2. Returns -EINVAL
584 * if presented with invalid clockdomain pointers or called on a machine
585 * that does not support software-configurable hardware sleep dependencies,
586 * -ENOENT if the specified dependency cannot be cleared in hardware, or
587 * 0 upon success.
589 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
591 struct clkdm_dep *cd;
592 int ret = 0;
594 if (!clkdm1 || !clkdm2)
595 return -EINVAL;
597 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
598 if (IS_ERR(cd))
599 ret = PTR_ERR(cd);
601 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
602 ret = -EINVAL;
604 if (ret) {
605 pr_debug("clockdomain: hardware cannot set/clear sleep "
606 "dependency affecting %s from %s\n", clkdm1->name,
607 clkdm2->name);
608 return ret;
611 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
612 pr_debug("clockdomain: will no longer prevent %s from "
613 "sleeping if %s is active\n", clkdm1->name,
614 clkdm2->name);
616 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
619 return ret;
623 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
624 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
625 * @clkdm2: when this struct clockdomain * is active (source)
627 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
628 * not be allowed to automatically go inactive if @clkdm2 is active;
629 * 0 if @clkdm1's automatic power state inactivity transition is independent
630 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
631 * on a machine that does not support software-configurable hardware sleep
632 * dependencies; or -ENOENT if the hardware is incapable.
634 * REVISIT: Currently this function only represents software-controllable
635 * sleep dependencies. Sleep dependencies fixed in hardware are not
636 * yet handled here.
638 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
640 struct clkdm_dep *cd;
641 int ret = 0;
643 if (!clkdm1 || !clkdm2)
644 return -EINVAL;
646 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
647 if (IS_ERR(cd))
648 ret = PTR_ERR(cd);
650 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
651 ret = -EINVAL;
653 if (ret) {
654 pr_debug("clockdomain: hardware cannot set/clear sleep "
655 "dependency affecting %s from %s\n", clkdm1->name,
656 clkdm2->name);
657 return ret;
660 /* XXX It's faster to return the atomic sleepdep_usecount */
661 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
665 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
666 * @clkdm: struct clockdomain * to remove all sleep dependencies from
668 * Remove all inter-clockdomain sleep dependencies that could prevent
669 * @clkdm from idling. Intended to be used during boot to initialize the
670 * PRCM to a known state, after all clockdomains are put into swsup idle
671 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
672 * 0 upon success.
674 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
676 if (!clkdm)
677 return -EINVAL;
679 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
680 return -EINVAL;
682 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
686 * clkdm_sleep - force clockdomain sleep transition
687 * @clkdm: struct clockdomain *
689 * Instruct the CM to force a sleep transition on the specified
690 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
691 * clockdomain does not support software-initiated sleep; 0 upon
692 * success.
694 int clkdm_sleep(struct clockdomain *clkdm)
696 int ret;
697 unsigned long flags;
699 if (!clkdm)
700 return -EINVAL;
702 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
703 pr_debug("clockdomain: %s does not support forcing "
704 "sleep via software\n", clkdm->name);
705 return -EINVAL;
708 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
709 return -EINVAL;
711 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
713 spin_lock_irqsave(&clkdm->lock, flags);
714 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
715 ret = arch_clkdm->clkdm_sleep(clkdm);
716 spin_unlock_irqrestore(&clkdm->lock, flags);
717 return ret;
721 * clkdm_wakeup - force clockdomain wakeup transition
722 * @clkdm: struct clockdomain *
724 * Instruct the CM to force a wakeup transition on the specified
725 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
726 * clockdomain does not support software-controlled wakeup; 0 upon
727 * success.
729 int clkdm_wakeup(struct clockdomain *clkdm)
731 int ret;
732 unsigned long flags;
734 if (!clkdm)
735 return -EINVAL;
737 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
738 pr_debug("clockdomain: %s does not support forcing "
739 "wakeup via software\n", clkdm->name);
740 return -EINVAL;
743 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
744 return -EINVAL;
746 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
748 spin_lock_irqsave(&clkdm->lock, flags);
749 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
750 ret = arch_clkdm->clkdm_wakeup(clkdm);
751 spin_unlock_irqrestore(&clkdm->lock, flags);
752 return ret;
756 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
757 * @clkdm: struct clockdomain *
759 * Allow the hardware to automatically switch the clockdomain @clkdm into
760 * active or idle states, as needed by downstream clocks. If the
761 * clockdomain has any downstream clocks enabled in the clock
762 * framework, wkdep/sleepdep autodependencies are added; this is so
763 * device drivers can read and write to the device. No return value.
765 void clkdm_allow_idle(struct clockdomain *clkdm)
767 unsigned long flags;
769 if (!clkdm)
770 return;
772 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
773 pr_debug("clock: automatic idle transitions cannot be enabled "
774 "on clockdomain %s\n", clkdm->name);
775 return;
778 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
779 return;
781 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
782 clkdm->name);
784 spin_lock_irqsave(&clkdm->lock, flags);
785 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
786 arch_clkdm->clkdm_allow_idle(clkdm);
787 pwrdm_clkdm_state_switch(clkdm);
788 spin_unlock_irqrestore(&clkdm->lock, flags);
792 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
793 * @clkdm: struct clockdomain *
795 * Prevent the hardware from automatically switching the clockdomain
796 * @clkdm into inactive or idle states. If the clockdomain has
797 * downstream clocks enabled in the clock framework, wkdep/sleepdep
798 * autodependencies are removed. No return value.
800 void clkdm_deny_idle(struct clockdomain *clkdm)
802 unsigned long flags;
804 if (!clkdm)
805 return;
807 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
808 pr_debug("clockdomain: automatic idle transitions cannot be "
809 "disabled on %s\n", clkdm->name);
810 return;
813 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
814 return;
816 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
817 clkdm->name);
819 spin_lock_irqsave(&clkdm->lock, flags);
820 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
821 arch_clkdm->clkdm_deny_idle(clkdm);
822 spin_unlock_irqrestore(&clkdm->lock, flags);
826 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
827 * @clkdm: struct clockdomain *
829 * Returns true if clockdomain @clkdm currently has
830 * hardware-supervised idle enabled, or false if it does not or if
831 * @clkdm is NULL. It is only valid to call this function after
832 * clkdm_init() has been called. This function does not actually read
833 * bits from the hardware; it instead tests an in-memory flag that is
834 * changed whenever the clockdomain code changes the auto-idle mode.
836 bool clkdm_in_hwsup(struct clockdomain *clkdm)
838 bool ret;
839 unsigned long flags;
841 if (!clkdm)
842 return false;
844 spin_lock_irqsave(&clkdm->lock, flags);
845 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
846 spin_unlock_irqrestore(&clkdm->lock, flags);
848 return ret;
851 /* Clockdomain-to-clock/hwmod framework interface code */
853 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
855 unsigned long flags;
857 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
858 return -EINVAL;
861 * For arch's with no autodeps, clkcm_clk_enable
862 * should be called for every clock instance or hwmod that is
863 * enabled, so the clkdm can be force woken up.
865 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
866 return 0;
868 spin_lock_irqsave(&clkdm->lock, flags);
869 arch_clkdm->clkdm_clk_enable(clkdm);
870 pwrdm_wait_transition(clkdm->pwrdm.ptr);
871 pwrdm_clkdm_state_switch(clkdm);
872 spin_unlock_irqrestore(&clkdm->lock, flags);
874 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
876 return 0;
879 static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
881 unsigned long flags;
883 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
884 return -EINVAL;
886 if (atomic_read(&clkdm->usecount) == 0) {
887 WARN_ON(1); /* underflow */
888 return -ERANGE;
891 if (atomic_dec_return(&clkdm->usecount) > 0)
892 return 0;
894 spin_lock_irqsave(&clkdm->lock, flags);
895 arch_clkdm->clkdm_clk_disable(clkdm);
896 pwrdm_clkdm_state_switch(clkdm);
897 spin_unlock_irqrestore(&clkdm->lock, flags);
899 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
901 return 0;
905 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
906 * @clkdm: struct clockdomain *
907 * @clk: struct clk * of the enabled downstream clock
909 * Increment the usecount of the clockdomain @clkdm and ensure that it
910 * is awake before @clk is enabled. Intended to be called by
911 * clk_enable() code. If the clockdomain is in software-supervised
912 * idle mode, force the clockdomain to wake. If the clockdomain is in
913 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
914 * ensure that devices in the clockdomain can be read from/written to
915 * by on-chip processors. Returns -EINVAL if passed null pointers;
916 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
918 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
921 * XXX Rewrite this code to maintain a list of enabled
922 * downstream clocks for debugging purposes?
925 if (!clk)
926 return -EINVAL;
928 return _clkdm_clk_hwmod_enable(clkdm);
932 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
933 * @clkdm: struct clockdomain *
934 * @clk: struct clk * of the disabled downstream clock
936 * Decrement the usecount of this clockdomain @clkdm when @clk is
937 * disabled. Intended to be called by clk_disable() code. If the
938 * clockdomain usecount goes to 0, put the clockdomain to sleep
939 * (software-supervised mode) or remove the clkdm autodependencies
940 * (hardware-supervised mode). Returns -EINVAL if passed null
941 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
942 * upon success or if the clockdomain is in hwsup idle mode.
944 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
947 * XXX Rewrite this code to maintain a list of enabled
948 * downstream clocks for debugging purposes?
951 if (!clk)
952 return -EINVAL;
954 return _clkdm_clk_hwmod_disable(clkdm);
958 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
959 * @clkdm: struct clockdomain *
960 * @oh: struct omap_hwmod * of the enabled downstream hwmod
962 * Increment the usecount of the clockdomain @clkdm and ensure that it
963 * is awake before @oh is enabled. Intended to be called by
964 * module_enable() code.
965 * If the clockdomain is in software-supervised idle mode, force the
966 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
967 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
968 * clockdomain can be read from/written to by on-chip processors.
969 * Returns -EINVAL if passed null pointers;
970 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
972 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
974 /* The clkdm attribute does not exist yet prior OMAP4 */
975 if (cpu_is_omap24xx() || cpu_is_omap34xx())
976 return 0;
979 * XXX Rewrite this code to maintain a list of enabled
980 * downstream hwmods for debugging purposes?
983 if (!oh)
984 return -EINVAL;
986 return _clkdm_clk_hwmod_enable(clkdm);
990 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
991 * @clkdm: struct clockdomain *
992 * @oh: struct omap_hwmod * of the disabled downstream hwmod
994 * Decrement the usecount of this clockdomain @clkdm when @oh is
995 * disabled. Intended to be called by module_disable() code.
996 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
997 * (software-supervised mode) or remove the clkdm autodependencies
998 * (hardware-supervised mode).
999 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1000 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1001 * idle mode.
1003 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1005 /* The clkdm attribute does not exist yet prior OMAP4 */
1006 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1007 return 0;
1010 * XXX Rewrite this code to maintain a list of enabled
1011 * downstream hwmods for debugging purposes?
1014 if (!oh)
1015 return -EINVAL;
1017 return _clkdm_clk_hwmod_disable(clkdm);