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/delay.h>
21 #include <linux/clk.h>
22 #include <linux/limits.h>
23 #include <linux/err.h>
27 #include <linux/bitops.h>
29 #include <plat/clock.h>
30 #include "clockdomain.h"
32 /* clkdm_list contains all registered struct clockdomains */
33 static LIST_HEAD(clkdm_list
);
35 /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36 static struct clkdm_autodep
*autodeps
;
38 static struct clkdm_ops
*arch_clkdm
;
40 /* Private functions */
42 static struct clockdomain
*_clkdm_lookup(const char *name
)
44 struct clockdomain
*clkdm
, *temp_clkdm
;
51 list_for_each_entry(temp_clkdm
, &clkdm_list
, node
) {
52 if (!strcmp(name
, temp_clkdm
->name
)) {
62 * _clkdm_register - register a clockdomain
63 * @clkdm: struct clockdomain * to register
65 * Adds a clockdomain to the internal clockdomain list.
66 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
67 * already registered by the provided name, or 0 upon success.
69 static int _clkdm_register(struct clockdomain
*clkdm
)
71 struct powerdomain
*pwrdm
;
73 if (!clkdm
|| !clkdm
->name
)
76 if (!omap_chip_is(clkdm
->omap_chip
))
79 pwrdm
= pwrdm_lookup(clkdm
->pwrdm
.name
);
81 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
82 clkdm
->name
, clkdm
->pwrdm
.name
);
85 clkdm
->pwrdm
.ptr
= pwrdm
;
87 /* Verify that the clockdomain is not already registered */
88 if (_clkdm_lookup(clkdm
->name
))
91 list_add(&clkdm
->node
, &clkdm_list
);
93 pwrdm_add_clkdm(pwrdm
, clkdm
);
95 spin_lock_init(&clkdm
->lock
);
97 pr_debug("clockdomain: registered %s\n", clkdm
->name
);
102 /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
103 static struct clkdm_dep
*_clkdm_deps_lookup(struct clockdomain
*clkdm
,
104 struct clkdm_dep
*deps
)
106 struct clkdm_dep
*cd
;
108 if (!clkdm
|| !deps
|| !omap_chip_is(clkdm
->omap_chip
))
109 return ERR_PTR(-EINVAL
);
111 for (cd
= deps
; cd
->clkdm_name
; cd
++) {
112 if (!omap_chip_is(cd
->omap_chip
))
115 if (!cd
->clkdm
&& cd
->clkdm_name
)
116 cd
->clkdm
= _clkdm_lookup(cd
->clkdm_name
);
118 if (cd
->clkdm
== clkdm
)
123 return ERR_PTR(-ENOENT
);
129 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
130 * @autodep: struct clkdm_autodep * to resolve
132 * Resolve autodep clockdomain names to clockdomain pointers via
133 * clkdm_lookup() and store the pointers in the autodep structure. An
134 * "autodep" is a clockdomain sleep/wakeup dependency that is
135 * automatically added and removed whenever clocks in the associated
136 * clockdomain are enabled or disabled (respectively) when the
137 * clockdomain is in hardware-supervised mode. Meant to be called
138 * once at clockdomain layer initialization, since these should remain
139 * fixed for a particular architecture. No return value.
141 * XXX autodeps are deprecated and should be removed at the earliest
144 static void _autodep_lookup(struct clkdm_autodep
*autodep
)
146 struct clockdomain
*clkdm
;
151 if (!omap_chip_is(autodep
->omap_chip
))
154 clkdm
= clkdm_lookup(autodep
->clkdm
.name
);
156 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
157 autodep
->clkdm
.name
);
158 clkdm
= ERR_PTR(-ENOENT
);
160 autodep
->clkdm
.ptr
= clkdm
;
164 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
165 * @clkdm: struct clockdomain *
167 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
168 * in hardware-supervised mode. Meant to be called from clock framework
169 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
171 * XXX autodeps are deprecated and should be removed at the earliest
174 void _clkdm_add_autodeps(struct clockdomain
*clkdm
)
176 struct clkdm_autodep
*autodep
;
178 if (!autodeps
|| clkdm
->flags
& CLKDM_NO_AUTODEPS
)
181 for (autodep
= autodeps
; autodep
->clkdm
.ptr
; autodep
++) {
182 if (IS_ERR(autodep
->clkdm
.ptr
))
185 if (!omap_chip_is(autodep
->omap_chip
))
188 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
189 "clkdm %s\n", autodep
->clkdm
.ptr
->name
,
192 clkdm_add_sleepdep(clkdm
, autodep
->clkdm
.ptr
);
193 clkdm_add_wkdep(clkdm
, autodep
->clkdm
.ptr
);
198 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
199 * @clkdm: struct clockdomain *
201 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
202 * in hardware-supervised mode. Meant to be called from clock framework
203 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
205 * XXX autodeps are deprecated and should be removed at the earliest
208 void _clkdm_del_autodeps(struct clockdomain
*clkdm
)
210 struct clkdm_autodep
*autodep
;
212 if (!autodeps
|| clkdm
->flags
& CLKDM_NO_AUTODEPS
)
215 for (autodep
= autodeps
; autodep
->clkdm
.ptr
; autodep
++) {
216 if (IS_ERR(autodep
->clkdm
.ptr
))
219 if (!omap_chip_is(autodep
->omap_chip
))
222 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
223 "clkdm %s\n", autodep
->clkdm
.ptr
->name
,
226 clkdm_del_sleepdep(clkdm
, autodep
->clkdm
.ptr
);
227 clkdm_del_wkdep(clkdm
, autodep
->clkdm
.ptr
);
232 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
233 * @clkdm: clockdomain that we are resolving dependencies for
234 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
236 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
237 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
240 static void _resolve_clkdm_deps(struct clockdomain
*clkdm
,
241 struct clkdm_dep
*clkdm_deps
)
243 struct clkdm_dep
*cd
;
245 for (cd
= clkdm_deps
; cd
&& cd
->clkdm_name
; cd
++) {
246 if (!omap_chip_is(cd
->omap_chip
))
250 cd
->clkdm
= _clkdm_lookup(cd
->clkdm_name
);
252 WARN(!cd
->clkdm
, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
253 clkdm
->name
, cd
->clkdm_name
);
257 /* Public functions */
260 * clkdm_init - set up the clockdomain layer
261 * @clkdms: optional pointer to an array of clockdomains to register
262 * @init_autodeps: optional pointer to an array of autodeps to register
263 * @custom_funcs: func pointers for arch specific implementations
265 * Set up internal state. If a pointer to an array of clockdomains
266 * @clkdms was supplied, loop through the list of clockdomains,
267 * register all that are available on the current platform. Similarly,
268 * if a pointer to an array of clockdomain autodependencies
269 * @init_autodeps was provided, register those. No return value.
271 void clkdm_init(struct clockdomain
**clkdms
,
272 struct clkdm_autodep
*init_autodeps
,
273 struct clkdm_ops
*custom_funcs
)
275 struct clockdomain
**c
= NULL
;
276 struct clockdomain
*clkdm
;
277 struct clkdm_autodep
*autodep
= NULL
;
280 WARN(1, "No custom clkdm functions registered\n");
282 arch_clkdm
= custom_funcs
;
285 for (c
= clkdms
; *c
; c
++)
288 autodeps
= init_autodeps
;
290 for (autodep
= autodeps
; autodep
->clkdm
.ptr
; autodep
++)
291 _autodep_lookup(autodep
);
294 * Put all clockdomains into software-supervised mode; PM code
295 * should later enable hardware-supervised mode as appropriate
297 list_for_each_entry(clkdm
, &clkdm_list
, node
) {
298 if (clkdm
->flags
& CLKDM_CAN_FORCE_WAKEUP
)
300 else if (clkdm
->flags
& CLKDM_CAN_DISABLE_AUTO
)
301 clkdm_deny_idle(clkdm
);
303 _resolve_clkdm_deps(clkdm
, clkdm
->wkdep_srcs
);
304 clkdm_clear_all_wkdeps(clkdm
);
306 _resolve_clkdm_deps(clkdm
, clkdm
->sleepdep_srcs
);
307 clkdm_clear_all_sleepdeps(clkdm
);
312 * clkdm_lookup - look up a clockdomain by name, return a pointer
313 * @name: name of clockdomain
315 * Find a registered clockdomain by its name @name. Returns a pointer
316 * to the struct clockdomain if found, or NULL otherwise.
318 struct clockdomain
*clkdm_lookup(const char *name
)
320 struct clockdomain
*clkdm
, *temp_clkdm
;
327 list_for_each_entry(temp_clkdm
, &clkdm_list
, node
) {
328 if (!strcmp(name
, temp_clkdm
->name
)) {
338 * clkdm_for_each - call function on each registered clockdomain
339 * @fn: callback function *
341 * Call the supplied function @fn for each registered clockdomain.
342 * The callback function @fn can return anything but 0 to bail
343 * out early from the iterator. The callback function is called with
344 * the clkdm_mutex held, so no clockdomain structure manipulation
345 * functions should be called from the callback, although hardware
346 * clockdomain control functions are fine. Returns the last return
347 * value of the callback function, which should be 0 for success or
348 * anything else to indicate failure; or -EINVAL if the function pointer
351 int clkdm_for_each(int (*fn
)(struct clockdomain
*clkdm
, void *user
),
354 struct clockdomain
*clkdm
;
360 list_for_each_entry(clkdm
, &clkdm_list
, node
) {
361 ret
= (*fn
)(clkdm
, user
);
371 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
372 * @clkdm: struct clockdomain *
374 * Return a pointer to the struct powerdomain that the specified clockdomain
375 * @clkdm exists in, or returns NULL if @clkdm is NULL.
377 struct powerdomain
*clkdm_get_pwrdm(struct clockdomain
*clkdm
)
382 return clkdm
->pwrdm
.ptr
;
386 /* Hardware clockdomain control */
389 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
390 * @clkdm1: wake this struct clockdomain * up (dependent)
391 * @clkdm2: when this struct clockdomain * wakes up (source)
393 * When the clockdomain represented by @clkdm2 wakes up, wake up
394 * @clkdm1. Implemented in hardware on the OMAP, this feature is
395 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
396 * Returns -EINVAL if presented with invalid clockdomain pointers,
397 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
400 int clkdm_add_wkdep(struct clockdomain
*clkdm1
, struct clockdomain
*clkdm2
)
402 struct clkdm_dep
*cd
;
405 if (!clkdm1
|| !clkdm2
)
408 cd
= _clkdm_deps_lookup(clkdm2
, clkdm1
->wkdep_srcs
);
412 if (!arch_clkdm
|| !arch_clkdm
->clkdm_add_wkdep
)
416 pr_debug("clockdomain: hardware cannot set/clear wake up of "
417 "%s when %s wakes up\n", clkdm1
->name
, clkdm2
->name
);
421 if (atomic_inc_return(&cd
->wkdep_usecount
) == 1) {
422 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
423 "up\n", clkdm1
->name
, clkdm2
->name
);
425 ret
= arch_clkdm
->clkdm_add_wkdep(clkdm1
, clkdm2
);
432 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
433 * @clkdm1: wake this struct clockdomain * up (dependent)
434 * @clkdm2: when this struct clockdomain * wakes up (source)
436 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
437 * wakes up. Returns -EINVAL if presented with invalid clockdomain
438 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
441 int clkdm_del_wkdep(struct clockdomain
*clkdm1
, struct clockdomain
*clkdm2
)
443 struct clkdm_dep
*cd
;
446 if (!clkdm1
|| !clkdm2
)
449 cd
= _clkdm_deps_lookup(clkdm2
, clkdm1
->wkdep_srcs
);
453 if (!arch_clkdm
|| !arch_clkdm
->clkdm_del_wkdep
)
457 pr_debug("clockdomain: hardware cannot set/clear wake up of "
458 "%s when %s wakes up\n", clkdm1
->name
, clkdm2
->name
);
462 if (atomic_dec_return(&cd
->wkdep_usecount
) == 0) {
463 pr_debug("clockdomain: hardware will no longer wake up %s "
464 "after %s wakes up\n", clkdm1
->name
, clkdm2
->name
);
466 ret
= arch_clkdm
->clkdm_del_wkdep(clkdm1
, clkdm2
);
473 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
474 * @clkdm1: wake this struct clockdomain * up (dependent)
475 * @clkdm2: when this struct clockdomain * wakes up (source)
477 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
478 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
479 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
482 * REVISIT: Currently this function only represents software-controllable
483 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
486 int clkdm_read_wkdep(struct clockdomain
*clkdm1
, struct clockdomain
*clkdm2
)
488 struct clkdm_dep
*cd
;
491 if (!clkdm1
|| !clkdm2
)
494 cd
= _clkdm_deps_lookup(clkdm2
, clkdm1
->wkdep_srcs
);
498 if (!arch_clkdm
|| !arch_clkdm
->clkdm_read_wkdep
)
502 pr_debug("clockdomain: hardware cannot set/clear wake up of "
503 "%s when %s wakes up\n", clkdm1
->name
, clkdm2
->name
);
507 /* XXX It's faster to return the atomic wkdep_usecount */
508 return arch_clkdm
->clkdm_read_wkdep(clkdm1
, clkdm2
);
512 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
513 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
515 * Remove all inter-clockdomain wakeup dependencies that could cause
516 * @clkdm to wake. Intended to be used during boot to initialize the
517 * PRCM to a known state, after all clockdomains are put into swsup idle
518 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
521 int clkdm_clear_all_wkdeps(struct clockdomain
*clkdm
)
526 if (!arch_clkdm
|| !arch_clkdm
->clkdm_clear_all_wkdeps
)
529 return arch_clkdm
->clkdm_clear_all_wkdeps(clkdm
);
533 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
534 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
535 * @clkdm2: when this struct clockdomain * is active (source)
537 * Prevent @clkdm1 from automatically going inactive (and then to
538 * retention or off) if @clkdm2 is active. Returns -EINVAL if
539 * presented with invalid clockdomain pointers or called on a machine
540 * that does not support software-configurable hardware sleep
541 * dependencies, -ENOENT if the specified dependency cannot be set in
542 * hardware, or 0 upon success.
544 int clkdm_add_sleepdep(struct clockdomain
*clkdm1
, struct clockdomain
*clkdm2
)
546 struct clkdm_dep
*cd
;
549 if (!clkdm1
|| !clkdm2
)
552 cd
= _clkdm_deps_lookup(clkdm2
, clkdm1
->sleepdep_srcs
);
556 if (!arch_clkdm
|| !arch_clkdm
->clkdm_add_sleepdep
)
560 pr_debug("clockdomain: hardware cannot set/clear sleep "
561 "dependency affecting %s from %s\n", clkdm1
->name
,
566 if (atomic_inc_return(&cd
->sleepdep_usecount
) == 1) {
567 pr_debug("clockdomain: will prevent %s from sleeping if %s "
568 "is active\n", clkdm1
->name
, clkdm2
->name
);
570 ret
= arch_clkdm
->clkdm_add_sleepdep(clkdm1
, clkdm2
);
577 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
578 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
579 * @clkdm2: when this struct clockdomain * is active (source)
581 * Allow @clkdm1 to automatically go inactive (and then to retention or
582 * off), independent of the activity state of @clkdm2. Returns -EINVAL
583 * if presented with invalid clockdomain pointers or called on a machine
584 * that does not support software-configurable hardware sleep dependencies,
585 * -ENOENT if the specified dependency cannot be cleared in hardware, or
588 int clkdm_del_sleepdep(struct clockdomain
*clkdm1
, struct clockdomain
*clkdm2
)
590 struct clkdm_dep
*cd
;
593 if (!clkdm1
|| !clkdm2
)
596 cd
= _clkdm_deps_lookup(clkdm2
, clkdm1
->sleepdep_srcs
);
600 if (!arch_clkdm
|| !arch_clkdm
->clkdm_del_sleepdep
)
604 pr_debug("clockdomain: hardware cannot set/clear sleep "
605 "dependency affecting %s from %s\n", clkdm1
->name
,
610 if (atomic_dec_return(&cd
->sleepdep_usecount
) == 0) {
611 pr_debug("clockdomain: will no longer prevent %s from "
612 "sleeping if %s is active\n", clkdm1
->name
,
615 ret
= arch_clkdm
->clkdm_del_sleepdep(clkdm1
, clkdm2
);
622 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
623 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
624 * @clkdm2: when this struct clockdomain * is active (source)
626 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
627 * not be allowed to automatically go inactive if @clkdm2 is active;
628 * 0 if @clkdm1's automatic power state inactivity transition is independent
629 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
630 * on a machine that does not support software-configurable hardware sleep
631 * dependencies; or -ENOENT if the hardware is incapable.
633 * REVISIT: Currently this function only represents software-controllable
634 * sleep dependencies. Sleep dependencies fixed in hardware are not
637 int clkdm_read_sleepdep(struct clockdomain
*clkdm1
, struct clockdomain
*clkdm2
)
639 struct clkdm_dep
*cd
;
642 if (!clkdm1
|| !clkdm2
)
645 cd
= _clkdm_deps_lookup(clkdm2
, clkdm1
->sleepdep_srcs
);
649 if (!arch_clkdm
|| !arch_clkdm
->clkdm_read_sleepdep
)
653 pr_debug("clockdomain: hardware cannot set/clear sleep "
654 "dependency affecting %s from %s\n", clkdm1
->name
,
659 /* XXX It's faster to return the atomic sleepdep_usecount */
660 return arch_clkdm
->clkdm_read_sleepdep(clkdm1
, clkdm2
);
664 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
665 * @clkdm: struct clockdomain * to remove all sleep dependencies from
667 * Remove all inter-clockdomain sleep dependencies that could prevent
668 * @clkdm from idling. Intended to be used during boot to initialize the
669 * PRCM to a known state, after all clockdomains are put into swsup idle
670 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
673 int clkdm_clear_all_sleepdeps(struct clockdomain
*clkdm
)
678 if (!arch_clkdm
|| !arch_clkdm
->clkdm_clear_all_sleepdeps
)
681 return arch_clkdm
->clkdm_clear_all_sleepdeps(clkdm
);
685 * clkdm_sleep - force clockdomain sleep transition
686 * @clkdm: struct clockdomain *
688 * Instruct the CM to force a sleep transition on the specified
689 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
690 * clockdomain does not support software-initiated sleep; 0 upon
693 int clkdm_sleep(struct clockdomain
*clkdm
)
701 if (!(clkdm
->flags
& CLKDM_CAN_FORCE_SLEEP
)) {
702 pr_debug("clockdomain: %s does not support forcing "
703 "sleep via software\n", clkdm
->name
);
707 if (!arch_clkdm
|| !arch_clkdm
->clkdm_sleep
)
710 pr_debug("clockdomain: forcing sleep on %s\n", clkdm
->name
);
712 spin_lock_irqsave(&clkdm
->lock
, flags
);
713 clkdm
->_flags
&= ~_CLKDM_FLAG_HWSUP_ENABLED
;
714 ret
= arch_clkdm
->clkdm_sleep(clkdm
);
715 spin_unlock_irqrestore(&clkdm
->lock
, flags
);
720 * clkdm_wakeup - force clockdomain wakeup transition
721 * @clkdm: struct clockdomain *
723 * Instruct the CM to force a wakeup transition on the specified
724 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
725 * clockdomain does not support software-controlled wakeup; 0 upon
728 int clkdm_wakeup(struct clockdomain
*clkdm
)
736 if (!(clkdm
->flags
& CLKDM_CAN_FORCE_WAKEUP
)) {
737 pr_debug("clockdomain: %s does not support forcing "
738 "wakeup via software\n", clkdm
->name
);
742 if (!arch_clkdm
|| !arch_clkdm
->clkdm_wakeup
)
745 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm
->name
);
747 spin_lock_irqsave(&clkdm
->lock
, flags
);
748 clkdm
->_flags
&= ~_CLKDM_FLAG_HWSUP_ENABLED
;
749 ret
= arch_clkdm
->clkdm_wakeup(clkdm
);
750 ret
|= pwrdm_state_switch(clkdm
->pwrdm
.ptr
);
751 spin_unlock_irqrestore(&clkdm
->lock
, flags
);
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
)
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
);
778 if (!arch_clkdm
|| !arch_clkdm
->clkdm_allow_idle
)
781 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
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
)
807 if (!(clkdm
->flags
& CLKDM_CAN_DISABLE_AUTO
)) {
808 pr_debug("clockdomain: automatic idle transitions cannot be "
809 "disabled on %s\n", clkdm
->name
);
813 if (!arch_clkdm
|| !arch_clkdm
->clkdm_deny_idle
)
816 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
819 spin_lock_irqsave(&clkdm
->lock
, flags
);
820 clkdm
->_flags
&= ~_CLKDM_FLAG_HWSUP_ENABLED
;
821 arch_clkdm
->clkdm_deny_idle(clkdm
);
822 pwrdm_state_switch(clkdm
->pwrdm
.ptr
);
823 spin_unlock_irqrestore(&clkdm
->lock
, flags
);
827 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
828 * @clkdm: struct clockdomain *
830 * Returns true if clockdomain @clkdm currently has
831 * hardware-supervised idle enabled, or false if it does not or if
832 * @clkdm is NULL. It is only valid to call this function after
833 * clkdm_init() has been called. This function does not actually read
834 * bits from the hardware; it instead tests an in-memory flag that is
835 * changed whenever the clockdomain code changes the auto-idle mode.
837 bool clkdm_in_hwsup(struct clockdomain
*clkdm
)
845 spin_lock_irqsave(&clkdm
->lock
, flags
);
846 ret
= (clkdm
->_flags
& _CLKDM_FLAG_HWSUP_ENABLED
) ? true : false;
847 spin_unlock_irqrestore(&clkdm
->lock
, flags
);
852 /* Clockdomain-to-clock/hwmod framework interface code */
854 static int _clkdm_clk_hwmod_enable(struct clockdomain
*clkdm
)
858 if (!clkdm
|| !arch_clkdm
|| !arch_clkdm
->clkdm_clk_enable
)
862 * For arch's with no autodeps, clkcm_clk_enable
863 * should be called for every clock instance or hwmod that is
864 * enabled, so the clkdm can be force woken up.
866 if ((atomic_inc_return(&clkdm
->usecount
) > 1) && autodeps
)
869 spin_lock_irqsave(&clkdm
->lock
, flags
);
870 arch_clkdm
->clkdm_clk_enable(clkdm
);
871 pwrdm_wait_transition(clkdm
->pwrdm
.ptr
);
872 pwrdm_clkdm_state_switch(clkdm
);
873 spin_unlock_irqrestore(&clkdm
->lock
, flags
);
875 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm
->name
);
880 static int _clkdm_clk_hwmod_disable(struct clockdomain
*clkdm
)
884 if (!clkdm
|| !arch_clkdm
|| !arch_clkdm
->clkdm_clk_disable
)
887 if (atomic_read(&clkdm
->usecount
) == 0) {
888 WARN_ON(1); /* underflow */
892 if (atomic_dec_return(&clkdm
->usecount
) > 0)
895 spin_lock_irqsave(&clkdm
->lock
, flags
);
896 arch_clkdm
->clkdm_clk_disable(clkdm
);
897 pwrdm_clkdm_state_switch(clkdm
);
898 spin_unlock_irqrestore(&clkdm
->lock
, flags
);
900 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm
->name
);
906 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
907 * @clkdm: struct clockdomain *
908 * @clk: struct clk * of the enabled downstream clock
910 * Increment the usecount of the clockdomain @clkdm and ensure that it
911 * is awake before @clk is enabled. Intended to be called by
912 * clk_enable() code. If the clockdomain is in software-supervised
913 * idle mode, force the clockdomain to wake. If the clockdomain is in
914 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
915 * ensure that devices in the clockdomain can be read from/written to
916 * by on-chip processors. Returns -EINVAL if passed null pointers;
917 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
919 int clkdm_clk_enable(struct clockdomain
*clkdm
, struct clk
*clk
)
922 * XXX Rewrite this code to maintain a list of enabled
923 * downstream clocks for debugging purposes?
929 return _clkdm_clk_hwmod_enable(clkdm
);
933 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
934 * @clkdm: struct clockdomain *
935 * @clk: struct clk * of the disabled downstream clock
937 * Decrement the usecount of this clockdomain @clkdm when @clk is
938 * disabled. Intended to be called by clk_disable() code. If the
939 * clockdomain usecount goes to 0, put the clockdomain to sleep
940 * (software-supervised mode) or remove the clkdm autodependencies
941 * (hardware-supervised mode). Returns -EINVAL if passed null
942 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
943 * upon success or if the clockdomain is in hwsup idle mode.
945 int clkdm_clk_disable(struct clockdomain
*clkdm
, struct clk
*clk
)
948 * XXX Rewrite this code to maintain a list of enabled
949 * downstream clocks for debugging purposes?
955 return _clkdm_clk_hwmod_disable(clkdm
);
959 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
960 * @clkdm: struct clockdomain *
961 * @oh: struct omap_hwmod * of the enabled downstream hwmod
963 * Increment the usecount of the clockdomain @clkdm and ensure that it
964 * is awake before @oh is enabled. Intended to be called by
965 * module_enable() code.
966 * If the clockdomain is in software-supervised idle mode, force the
967 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
968 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
969 * clockdomain can be read from/written to by on-chip processors.
970 * Returns -EINVAL if passed null pointers;
971 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
973 int clkdm_hwmod_enable(struct clockdomain
*clkdm
, struct omap_hwmod
*oh
)
975 /* The clkdm attribute does not exist yet prior OMAP4 */
976 if (cpu_is_omap24xx() || cpu_is_omap34xx())
980 * XXX Rewrite this code to maintain a list of enabled
981 * downstream hwmods for debugging purposes?
987 return _clkdm_clk_hwmod_enable(clkdm
);
991 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
992 * @clkdm: struct clockdomain *
993 * @oh: struct omap_hwmod * of the disabled downstream hwmod
995 * Decrement the usecount of this clockdomain @clkdm when @oh is
996 * disabled. Intended to be called by module_disable() code.
997 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
998 * (software-supervised mode) or remove the clkdm autodependencies
999 * (hardware-supervised mode).
1000 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1001 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1004 int clkdm_hwmod_disable(struct clockdomain
*clkdm
, struct omap_hwmod
*oh
)
1006 /* The clkdm attribute does not exist yet prior OMAP4 */
1007 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1011 * XXX Rewrite this code to maintain a list of enabled
1012 * downstream hwmods for debugging purposes?
1018 return _clkdm_clk_hwmod_disable(clkdm
);