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.
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>
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;
52 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
53 if (!strcmp(name, temp_clkdm->name)) {
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)
77 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
79 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
80 clkdm->name, clkdm->pwrdm.name);
83 clkdm->pwrdm.ptr = pwrdm;
85 /* Verify that the clockdomain is not already registered */
86 if (_clkdm_lookup(clkdm->name))
89 list_add(&clkdm->node, &clkdm_list);
91 pwrdm_add_clkdm(pwrdm, clkdm);
93 spin_lock_init(&clkdm->lock);
95 pr_debug("clockdomain: registered %s\n", clkdm->name);
100 /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
101 static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
102 struct clkdm_dep *deps)
104 struct clkdm_dep *cd;
107 return ERR_PTR(-EINVAL);
109 for (cd = deps; cd->clkdm_name; cd++) {
110 if (!cd->clkdm && cd->clkdm_name)
111 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
113 if (cd->clkdm == clkdm)
118 return ERR_PTR(-ENOENT);
124 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
125 * @autodep: struct clkdm_autodep * to resolve
127 * Resolve autodep clockdomain names to clockdomain pointers via
128 * clkdm_lookup() and store the pointers in the autodep structure. An
129 * "autodep" is a clockdomain sleep/wakeup dependency that is
130 * automatically added and removed whenever clocks in the associated
131 * clockdomain are enabled or disabled (respectively) when the
132 * clockdomain is in hardware-supervised mode. Meant to be called
133 * once at clockdomain layer initialization, since these should remain
134 * fixed for a particular architecture. No return value.
136 * XXX autodeps are deprecated and should be removed at the earliest
139 static void _autodep_lookup(struct clkdm_autodep *autodep)
141 struct clockdomain *clkdm;
146 clkdm = clkdm_lookup(autodep->clkdm.name);
148 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
149 autodep->clkdm.name);
150 clkdm = ERR_PTR(-ENOENT);
152 autodep->clkdm.ptr = clkdm;
156 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
157 * @clkdm: struct clockdomain *
159 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
160 * in hardware-supervised mode. Meant to be called from clock framework
161 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
163 * XXX autodeps are deprecated and should be removed at the earliest
166 void _clkdm_add_autodeps(struct clockdomain *clkdm)
168 struct clkdm_autodep *autodep;
170 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
173 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
174 if (IS_ERR(autodep->clkdm.ptr))
177 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
178 "clkdm %s\n", autodep->clkdm.ptr->name,
181 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
182 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
187 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
188 * @clkdm: struct clockdomain *
190 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
191 * in hardware-supervised mode. Meant to be called from clock framework
192 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
194 * XXX autodeps are deprecated and should be removed at the earliest
197 void _clkdm_del_autodeps(struct clockdomain *clkdm)
199 struct clkdm_autodep *autodep;
201 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
204 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
205 if (IS_ERR(autodep->clkdm.ptr))
208 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
209 "clkdm %s\n", autodep->clkdm.ptr->name,
212 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
213 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
218 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
219 * @clkdm: clockdomain that we are resolving dependencies for
220 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
222 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
223 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
226 static void _resolve_clkdm_deps(struct clockdomain *clkdm,
227 struct clkdm_dep *clkdm_deps)
229 struct clkdm_dep *cd;
231 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
234 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
236 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
237 clkdm->name, cd->clkdm_name);
241 /* Public functions */
244 * clkdm_register_platform_funcs - register clockdomain implementation fns
245 * @co: func pointers for arch specific implementations
247 * Register the list of function pointers used to implement the
248 * clockdomain functions on different OMAP SoCs. Should be called
249 * before any other clkdm_register*() function. Returns -EINVAL if
250 * @co is null, -EEXIST if platform functions have already been
251 * registered, or 0 upon success.
253 int clkdm_register_platform_funcs(struct clkdm_ops *co)
267 * clkdm_register_clkdms - register SoC clockdomains
268 * @cs: pointer to an array of struct clockdomain to register
270 * Register the clockdomains available on a particular OMAP SoC. Must
271 * be called after clkdm_register_platform_funcs(). May be called
272 * multiple times. Returns -EACCES if called before
273 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
274 * null; or 0 upon success.
276 int clkdm_register_clkdms(struct clockdomain **cs)
278 struct clockdomain **c = NULL;
286 for (c = cs; *c; c++)
293 * clkdm_register_autodeps - register autodeps (if required)
294 * @ia: pointer to a static array of struct clkdm_autodep to register
296 * Register clockdomain "automatic dependencies." These are
297 * clockdomain wakeup and sleep dependencies that are automatically
298 * added whenever the first clock inside a clockdomain is enabled, and
299 * removed whenever the last clock inside a clockdomain is disabled.
300 * These are currently only used on OMAP3 devices, and are deprecated,
301 * since they waste energy. However, until the OMAP2/3 IP block
302 * enable/disable sequence can be converted to match the OMAP4
303 * sequence, they are needed.
305 * Must be called only after all of the SoC clockdomains are
306 * registered, since the function will resolve autodep clockdomain
307 * names into clockdomain pointers.
309 * The struct clkdm_autodep @ia array must be static, as this function
310 * does not copy the array elements.
312 * Returns -EACCES if called before any clockdomains have been
313 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
314 * autodeps have already been registered, or 0 upon success.
316 int clkdm_register_autodeps(struct clkdm_autodep *ia)
318 struct clkdm_autodep *a = NULL;
320 if (list_empty(&clkdm_list))
330 for (a = autodeps; a->clkdm.ptr; a++)
337 * clkdm_complete_init - set up the clockdomain layer
339 * Put all clockdomains into software-supervised mode; PM code should
340 * later enable hardware-supervised mode as appropriate. Must be
341 * called after clkdm_register_clkdms(). Returns -EACCES if called
342 * before clkdm_register_clkdms(), or 0 upon success.
344 int clkdm_complete_init(void)
346 struct clockdomain *clkdm;
348 if (list_empty(&clkdm_list))
351 list_for_each_entry(clkdm, &clkdm_list, node) {
352 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
354 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
355 clkdm_deny_idle(clkdm);
357 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
358 clkdm_clear_all_wkdeps(clkdm);
360 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
361 clkdm_clear_all_sleepdeps(clkdm);
368 * clkdm_lookup - look up a clockdomain by name, return a pointer
369 * @name: name of clockdomain
371 * Find a registered clockdomain by its name @name. Returns a pointer
372 * to the struct clockdomain if found, or NULL otherwise.
374 struct clockdomain *clkdm_lookup(const char *name)
376 struct clockdomain *clkdm, *temp_clkdm;
383 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
384 if (!strcmp(name, temp_clkdm->name)) {
394 * clkdm_for_each - call function on each registered clockdomain
395 * @fn: callback function *
397 * Call the supplied function @fn for each registered clockdomain.
398 * The callback function @fn can return anything but 0 to bail
399 * out early from the iterator. The callback function is called with
400 * the clkdm_mutex held, so no clockdomain structure manipulation
401 * functions should be called from the callback, although hardware
402 * clockdomain control functions are fine. Returns the last return
403 * value of the callback function, which should be 0 for success or
404 * anything else to indicate failure; or -EINVAL if the function pointer
407 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
410 struct clockdomain *clkdm;
416 list_for_each_entry(clkdm, &clkdm_list, node) {
417 ret = (*fn)(clkdm, user);
427 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
428 * @clkdm: struct clockdomain *
430 * Return a pointer to the struct powerdomain that the specified clockdomain
431 * @clkdm exists in, or returns NULL if @clkdm is NULL.
433 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
438 return clkdm->pwrdm.ptr;
442 /* Hardware clockdomain control */
445 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
446 * @clkdm1: wake this struct clockdomain * up (dependent)
447 * @clkdm2: when this struct clockdomain * wakes up (source)
449 * When the clockdomain represented by @clkdm2 wakes up, wake up
450 * @clkdm1. Implemented in hardware on the OMAP, this feature is
451 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
452 * Returns -EINVAL if presented with invalid clockdomain pointers,
453 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
456 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
458 struct clkdm_dep *cd;
461 if (!clkdm1 || !clkdm2)
464 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
468 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
472 pr_debug("clockdomain: hardware cannot set/clear wake up of "
473 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
477 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
478 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
479 "up\n", clkdm1->name, clkdm2->name);
481 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
488 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
489 * @clkdm1: wake this struct clockdomain * up (dependent)
490 * @clkdm2: when this struct clockdomain * wakes up (source)
492 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
493 * wakes up. Returns -EINVAL if presented with invalid clockdomain
494 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
497 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
499 struct clkdm_dep *cd;
502 if (!clkdm1 || !clkdm2)
505 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
509 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
513 pr_debug("clockdomain: hardware cannot set/clear wake up of "
514 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
518 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
519 pr_debug("clockdomain: hardware will no longer wake up %s "
520 "after %s wakes up\n", clkdm1->name, clkdm2->name);
522 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
529 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
530 * @clkdm1: wake this struct clockdomain * up (dependent)
531 * @clkdm2: when this struct clockdomain * wakes up (source)
533 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
534 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
535 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
538 * REVISIT: Currently this function only represents software-controllable
539 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
542 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
544 struct clkdm_dep *cd;
547 if (!clkdm1 || !clkdm2)
550 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
554 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
558 pr_debug("clockdomain: hardware cannot set/clear wake up of "
559 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
563 /* XXX It's faster to return the atomic wkdep_usecount */
564 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
568 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
569 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
571 * Remove all inter-clockdomain wakeup dependencies that could cause
572 * @clkdm to wake. Intended to be used during boot to initialize the
573 * PRCM to a known state, after all clockdomains are put into swsup idle
574 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
577 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
582 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
585 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
589 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
590 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
591 * @clkdm2: when this struct clockdomain * is active (source)
593 * Prevent @clkdm1 from automatically going inactive (and then to
594 * retention or off) if @clkdm2 is active. Returns -EINVAL if
595 * presented with invalid clockdomain pointers or called on a machine
596 * that does not support software-configurable hardware sleep
597 * dependencies, -ENOENT if the specified dependency cannot be set in
598 * hardware, or 0 upon success.
600 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
602 struct clkdm_dep *cd;
605 if (!clkdm1 || !clkdm2)
608 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
612 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
616 pr_debug("clockdomain: hardware cannot set/clear sleep "
617 "dependency affecting %s from %s\n", clkdm1->name,
622 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
623 pr_debug("clockdomain: will prevent %s from sleeping if %s "
624 "is active\n", clkdm1->name, clkdm2->name);
626 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
633 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
634 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
635 * @clkdm2: when this struct clockdomain * is active (source)
637 * Allow @clkdm1 to automatically go inactive (and then to retention or
638 * off), independent of the activity state of @clkdm2. Returns -EINVAL
639 * if presented with invalid clockdomain pointers or called on a machine
640 * that does not support software-configurable hardware sleep dependencies,
641 * -ENOENT if the specified dependency cannot be cleared in hardware, or
644 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
646 struct clkdm_dep *cd;
649 if (!clkdm1 || !clkdm2)
652 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
656 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
660 pr_debug("clockdomain: hardware cannot set/clear sleep "
661 "dependency affecting %s from %s\n", clkdm1->name,
666 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
667 pr_debug("clockdomain: will no longer prevent %s from "
668 "sleeping if %s is active\n", clkdm1->name,
671 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
678 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
679 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
680 * @clkdm2: when this struct clockdomain * is active (source)
682 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
683 * not be allowed to automatically go inactive if @clkdm2 is active;
684 * 0 if @clkdm1's automatic power state inactivity transition is independent
685 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
686 * on a machine that does not support software-configurable hardware sleep
687 * dependencies; or -ENOENT if the hardware is incapable.
689 * REVISIT: Currently this function only represents software-controllable
690 * sleep dependencies. Sleep dependencies fixed in hardware are not
693 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
695 struct clkdm_dep *cd;
698 if (!clkdm1 || !clkdm2)
701 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
705 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
709 pr_debug("clockdomain: hardware cannot set/clear sleep "
710 "dependency affecting %s from %s\n", clkdm1->name,
715 /* XXX It's faster to return the atomic sleepdep_usecount */
716 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
720 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
721 * @clkdm: struct clockdomain * to remove all sleep dependencies from
723 * Remove all inter-clockdomain sleep dependencies that could prevent
724 * @clkdm from idling. Intended to be used during boot to initialize the
725 * PRCM to a known state, after all clockdomains are put into swsup idle
726 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
729 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
734 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
737 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
741 * clkdm_sleep - force clockdomain sleep transition
742 * @clkdm: struct clockdomain *
744 * Instruct the CM to force a sleep transition on the specified
745 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
746 * clockdomain does not support software-initiated sleep; 0 upon
749 int clkdm_sleep(struct clockdomain *clkdm)
757 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
758 pr_debug("clockdomain: %s does not support forcing "
759 "sleep via software\n", clkdm->name);
763 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
766 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
768 spin_lock_irqsave(&clkdm->lock, flags);
769 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
770 ret = arch_clkdm->clkdm_sleep(clkdm);
771 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
772 spin_unlock_irqrestore(&clkdm->lock, flags);
777 * clkdm_wakeup - force clockdomain wakeup transition
778 * @clkdm: struct clockdomain *
780 * Instruct the CM to force a wakeup transition on the specified
781 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
782 * clockdomain does not support software-controlled wakeup; 0 upon
785 int clkdm_wakeup(struct clockdomain *clkdm)
793 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
794 pr_debug("clockdomain: %s does not support forcing "
795 "wakeup via software\n", clkdm->name);
799 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
802 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
804 spin_lock_irqsave(&clkdm->lock, flags);
805 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
806 ret = arch_clkdm->clkdm_wakeup(clkdm);
807 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
808 spin_unlock_irqrestore(&clkdm->lock, flags);
813 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
814 * @clkdm: struct clockdomain *
816 * Allow the hardware to automatically switch the clockdomain @clkdm into
817 * active or idle states, as needed by downstream clocks. If the
818 * clockdomain has any downstream clocks enabled in the clock
819 * framework, wkdep/sleepdep autodependencies are added; this is so
820 * device drivers can read and write to the device. No return value.
822 void clkdm_allow_idle(struct clockdomain *clkdm)
829 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
830 pr_debug("clock: automatic idle transitions cannot be enabled "
831 "on clockdomain %s\n", clkdm->name);
835 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
838 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
841 spin_lock_irqsave(&clkdm->lock, flags);
842 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
843 arch_clkdm->clkdm_allow_idle(clkdm);
844 pwrdm_clkdm_state_switch(clkdm);
845 spin_unlock_irqrestore(&clkdm->lock, flags);
849 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
850 * @clkdm: struct clockdomain *
852 * Prevent the hardware from automatically switching the clockdomain
853 * @clkdm into inactive or idle states. If the clockdomain has
854 * downstream clocks enabled in the clock framework, wkdep/sleepdep
855 * autodependencies are removed. No return value.
857 void clkdm_deny_idle(struct clockdomain *clkdm)
864 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
865 pr_debug("clockdomain: automatic idle transitions cannot be "
866 "disabled on %s\n", clkdm->name);
870 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
873 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
876 spin_lock_irqsave(&clkdm->lock, flags);
877 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
878 arch_clkdm->clkdm_deny_idle(clkdm);
879 pwrdm_state_switch(clkdm->pwrdm.ptr);
880 spin_unlock_irqrestore(&clkdm->lock, flags);
884 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
885 * @clkdm: struct clockdomain *
887 * Returns true if clockdomain @clkdm currently has
888 * hardware-supervised idle enabled, or false if it does not or if
889 * @clkdm is NULL. It is only valid to call this function after
890 * clkdm_init() has been called. This function does not actually read
891 * bits from the hardware; it instead tests an in-memory flag that is
892 * changed whenever the clockdomain code changes the auto-idle mode.
894 bool clkdm_in_hwsup(struct clockdomain *clkdm)
902 spin_lock_irqsave(&clkdm->lock, flags);
903 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
904 spin_unlock_irqrestore(&clkdm->lock, flags);
909 /* Clockdomain-to-clock/hwmod framework interface code */
911 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
915 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
919 * For arch's with no autodeps, clkcm_clk_enable
920 * should be called for every clock instance or hwmod that is
921 * enabled, so the clkdm can be force woken up.
923 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
926 spin_lock_irqsave(&clkdm->lock, flags);
927 arch_clkdm->clkdm_clk_enable(clkdm);
928 pwrdm_wait_transition(clkdm->pwrdm.ptr);
929 pwrdm_clkdm_state_switch(clkdm);
930 spin_unlock_irqrestore(&clkdm->lock, flags);
932 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
937 static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
941 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
944 if (atomic_read(&clkdm->usecount) == 0) {
945 WARN_ON(1); /* underflow */
949 if (atomic_dec_return(&clkdm->usecount) > 0)
952 spin_lock_irqsave(&clkdm->lock, flags);
953 arch_clkdm->clkdm_clk_disable(clkdm);
954 pwrdm_clkdm_state_switch(clkdm);
955 spin_unlock_irqrestore(&clkdm->lock, flags);
957 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
963 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
964 * @clkdm: struct clockdomain *
965 * @clk: struct clk * of the enabled downstream clock
967 * Increment the usecount of the clockdomain @clkdm and ensure that it
968 * is awake before @clk is enabled. Intended to be called by
969 * clk_enable() code. If the clockdomain is in software-supervised
970 * idle mode, force the clockdomain to wake. If the clockdomain is in
971 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
972 * ensure that devices in the clockdomain can be read from/written to
973 * by on-chip processors. Returns -EINVAL if passed null pointers;
974 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
976 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
979 * XXX Rewrite this code to maintain a list of enabled
980 * downstream clocks for debugging purposes?
986 return _clkdm_clk_hwmod_enable(clkdm);
990 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
991 * @clkdm: struct clockdomain *
992 * @clk: struct clk * of the disabled downstream clock
994 * Decrement the usecount of this clockdomain @clkdm when @clk is
995 * disabled. Intended to be called by clk_disable() code. If the
996 * clockdomain usecount goes to 0, put the clockdomain to sleep
997 * (software-supervised mode) or remove the clkdm autodependencies
998 * (hardware-supervised mode). Returns -EINVAL if passed null
999 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1000 * upon success or if the clockdomain is in hwsup idle mode.
1002 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
1005 * XXX Rewrite this code to maintain a list of enabled
1006 * downstream clocks for debugging purposes?
1012 return _clkdm_clk_hwmod_disable(clkdm);
1016 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1017 * @clkdm: struct clockdomain *
1018 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1020 * Increment the usecount of the clockdomain @clkdm and ensure that it
1021 * is awake before @oh is enabled. Intended to be called by
1022 * module_enable() code.
1023 * If the clockdomain is in software-supervised idle mode, force the
1024 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1025 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1026 * clockdomain can be read from/written to by on-chip processors.
1027 * Returns -EINVAL if passed null pointers;
1028 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1030 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1032 /* The clkdm attribute does not exist yet prior OMAP4 */
1033 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1037 * XXX Rewrite this code to maintain a list of enabled
1038 * downstream hwmods for debugging purposes?
1044 return _clkdm_clk_hwmod_enable(clkdm);
1048 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1049 * @clkdm: struct clockdomain *
1050 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1052 * Decrement the usecount of this clockdomain @clkdm when @oh is
1053 * disabled. Intended to be called by module_disable() code.
1054 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1055 * (software-supervised mode) or remove the clkdm autodependencies
1056 * (hardware-supervised mode).
1057 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1058 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1061 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1063 /* The clkdm attribute does not exist yet prior OMAP4 */
1064 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1068 * XXX Rewrite this code to maintain a list of enabled
1069 * downstream hwmods for debugging purposes?
1075 return _clkdm_clk_hwmod_disable(clkdm);