2 * OMAP powerdomain control
4 * Copyright (C) 2007-2008, 2011 Texas Instruments, Inc.
5 * Copyright (C) 2007-2011 Nokia Corporation
7 * Written by Paul Walmsley
8 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
9 * State counting code by Tero Kristo <tero.kristo@nokia.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/list.h>
20 #include <linux/errno.h>
21 #include <linux/string.h>
22 #include <linux/spinlock.h>
23 #include <trace/events/power.h>
25 #include "cm2xxx_3xxx.h"
28 #include "prm2xxx_3xxx.h"
33 #include "powerdomain.h"
34 #include "clockdomain.h"
39 #define PWRDM_TRACE_STATES_FLAG (1<<31)
47 * Types of sleep_switch used internally in omap_set_pwrdm_state()
48 * and its associated static functions
50 * XXX Better documentation is needed here
52 #define ALREADYACTIVE_SWITCH 0
53 #define FORCEWAKEUP_SWITCH 1
54 #define LOWPOWERSTATE_SWITCH 2
56 /* pwrdm_list contains all registered struct powerdomains */
57 static LIST_HEAD(pwrdm_list
);
59 static struct pwrdm_ops
*arch_pwrdm
;
61 /* Private functions */
63 static struct powerdomain
*_pwrdm_lookup(const char *name
)
65 struct powerdomain
*pwrdm
, *temp_pwrdm
;
69 list_for_each_entry(temp_pwrdm
, &pwrdm_list
, node
) {
70 if (!strcmp(name
, temp_pwrdm
->name
)) {
80 * _pwrdm_register - register a powerdomain
81 * @pwrdm: struct powerdomain * to register
83 * Adds a powerdomain to the internal powerdomain list. Returns
84 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
85 * already registered by the provided name, or 0 upon success.
87 static int _pwrdm_register(struct powerdomain
*pwrdm
)
90 struct voltagedomain
*voltdm
;
92 if (!pwrdm
|| !pwrdm
->name
)
95 if (cpu_is_omap44xx() &&
96 pwrdm
->prcm_partition
== OMAP4430_INVALID_PRCM_PARTITION
) {
97 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
102 if (_pwrdm_lookup(pwrdm
->name
))
105 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_has_voltdm
)
106 if (!arch_pwrdm
->pwrdm_has_voltdm())
109 voltdm
= voltdm_lookup(pwrdm
->voltdm
.name
);
111 pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
112 pwrdm
->name
, pwrdm
->voltdm
.name
);
115 pwrdm
->voltdm
.ptr
= voltdm
;
116 INIT_LIST_HEAD(&pwrdm
->voltdm_node
);
117 voltdm_add_pwrdm(voltdm
, pwrdm
);
119 spin_lock_init(&pwrdm
->_lock
);
121 list_add(&pwrdm
->node
, &pwrdm_list
);
123 /* Initialize the powerdomain's state counter */
124 for (i
= 0; i
< PWRDM_MAX_PWRSTS
; i
++)
125 pwrdm
->state_counter
[i
] = 0;
127 pwrdm
->ret_logic_off_counter
= 0;
128 for (i
= 0; i
< pwrdm
->banks
; i
++)
129 pwrdm
->ret_mem_off_counter
[i
] = 0;
131 arch_pwrdm
->pwrdm_wait_transition(pwrdm
);
132 pwrdm
->state
= pwrdm_read_pwrst(pwrdm
);
133 pwrdm
->state_counter
[pwrdm
->state
] = 1;
135 pr_debug("powerdomain: registered %s\n", pwrdm
->name
);
140 static void _update_logic_membank_counters(struct powerdomain
*pwrdm
)
143 u8 prev_logic_pwrst
, prev_mem_pwrst
;
145 prev_logic_pwrst
= pwrdm_read_prev_logic_pwrst(pwrdm
);
146 if ((pwrdm
->pwrsts_logic_ret
== PWRSTS_OFF_RET
) &&
147 (prev_logic_pwrst
== PWRDM_POWER_OFF
))
148 pwrdm
->ret_logic_off_counter
++;
150 for (i
= 0; i
< pwrdm
->banks
; i
++) {
151 prev_mem_pwrst
= pwrdm_read_prev_mem_pwrst(pwrdm
, i
);
153 if ((pwrdm
->pwrsts_mem_ret
[i
] == PWRSTS_OFF_RET
) &&
154 (prev_mem_pwrst
== PWRDM_POWER_OFF
))
155 pwrdm
->ret_mem_off_counter
[i
]++;
159 static int _pwrdm_state_switch(struct powerdomain
*pwrdm
, int flag
)
162 int prev
, next
, state
, trace_state
= 0;
167 state
= pwrdm_read_pwrst(pwrdm
);
170 case PWRDM_STATE_NOW
:
173 case PWRDM_STATE_PREV
:
174 prev
= pwrdm_read_prev_pwrst(pwrdm
);
175 if (pwrdm
->state
!= prev
)
176 pwrdm
->state_counter
[prev
]++;
177 if (prev
== PWRDM_POWER_RET
)
178 _update_logic_membank_counters(pwrdm
);
180 * If the power domain did not hit the desired state,
181 * generate a trace event with both the desired and hit states
183 next
= pwrdm_read_next_pwrst(pwrdm
);
185 trace_state
= (PWRDM_TRACE_STATES_FLAG
|
186 ((next
& OMAP_POWERSTATE_MASK
) << 8) |
187 ((prev
& OMAP_POWERSTATE_MASK
) << 0));
188 trace_power_domain_target(pwrdm
->name
, trace_state
,
197 pwrdm
->state_counter
[state
]++;
199 pm_dbg_update_time(pwrdm
, prev
);
201 pwrdm
->state
= state
;
206 static int _pwrdm_pre_transition_cb(struct powerdomain
*pwrdm
, void *unused
)
208 pwrdm_clear_all_prev_pwrst(pwrdm
);
209 _pwrdm_state_switch(pwrdm
, PWRDM_STATE_NOW
);
213 static int _pwrdm_post_transition_cb(struct powerdomain
*pwrdm
, void *unused
)
215 _pwrdm_state_switch(pwrdm
, PWRDM_STATE_PREV
);
220 * _pwrdm_save_clkdm_state_and_activate - prepare for power state change
221 * @pwrdm: struct powerdomain * to operate on
222 * @curr_pwrst: current power state of @pwrdm
223 * @pwrst: power state to switch to
224 * @hwsup: ptr to a bool to return whether the clkdm is hardware-supervised
226 * Determine whether the powerdomain needs to be turned on before
227 * attempting to switch power states. Called by
228 * omap_set_pwrdm_state(). NOTE that if the powerdomain contains
229 * multiple clockdomains, this code assumes that the first clockdomain
230 * supports software-supervised wakeup mode - potentially a problem.
231 * Returns the power state switch mode currently in use (see the
232 * "Types of sleep_switch" comment above).
234 static u8
_pwrdm_save_clkdm_state_and_activate(struct powerdomain
*pwrdm
,
235 u8 curr_pwrst
, u8 pwrst
,
240 if (curr_pwrst
< PWRDM_POWER_ON
) {
241 if (curr_pwrst
> pwrst
&&
242 pwrdm
->flags
& PWRDM_HAS_LOWPOWERSTATECHANGE
&&
243 arch_pwrdm
->pwrdm_set_lowpwrstchange
) {
244 sleep_switch
= LOWPOWERSTATE_SWITCH
;
246 *hwsup
= clkdm_in_hwsup(pwrdm
->pwrdm_clkdms
[0]);
247 clkdm_wakeup_nolock(pwrdm
->pwrdm_clkdms
[0]);
248 sleep_switch
= FORCEWAKEUP_SWITCH
;
251 sleep_switch
= ALREADYACTIVE_SWITCH
;
258 * _pwrdm_restore_clkdm_state - restore the clkdm hwsup state after pwrst change
259 * @pwrdm: struct powerdomain * to operate on
260 * @sleep_switch: return value from _pwrdm_save_clkdm_state_and_activate()
261 * @hwsup: should @pwrdm's first clockdomain be set to hardware-supervised mode?
263 * Restore the clockdomain state perturbed by
264 * _pwrdm_save_clkdm_state_and_activate(), and call the power state
265 * bookkeeping code. Called by omap_set_pwrdm_state(). NOTE that if
266 * the powerdomain contains multiple clockdomains, this assumes that
267 * the first associated clockdomain supports either
268 * hardware-supervised idle control in the register, or
269 * software-supervised sleep. No return value.
271 static void _pwrdm_restore_clkdm_state(struct powerdomain
*pwrdm
,
272 u8 sleep_switch
, bool hwsup
)
274 switch (sleep_switch
) {
275 case FORCEWAKEUP_SWITCH
:
277 clkdm_allow_idle_nolock(pwrdm
->pwrdm_clkdms
[0]);
279 clkdm_sleep_nolock(pwrdm
->pwrdm_clkdms
[0]);
281 case LOWPOWERSTATE_SWITCH
:
282 if (pwrdm
->flags
& PWRDM_HAS_LOWPOWERSTATECHANGE
&&
283 arch_pwrdm
->pwrdm_set_lowpwrstchange
)
284 arch_pwrdm
->pwrdm_set_lowpwrstchange(pwrdm
);
285 pwrdm_state_switch_nolock(pwrdm
);
290 /* Public functions */
293 * pwrdm_register_platform_funcs - register powerdomain implementation fns
294 * @po: func pointers for arch specific implementations
296 * Register the list of function pointers used to implement the
297 * powerdomain functions on different OMAP SoCs. Should be called
298 * before any other pwrdm_register*() function. Returns -EINVAL if
299 * @po is null, -EEXIST if platform functions have already been
300 * registered, or 0 upon success.
302 int pwrdm_register_platform_funcs(struct pwrdm_ops
*po
)
316 * pwrdm_register_pwrdms - register SoC powerdomains
317 * @ps: pointer to an array of struct powerdomain to register
319 * Register the powerdomains available on a particular OMAP SoC. Must
320 * be called after pwrdm_register_platform_funcs(). May be called
321 * multiple times. Returns -EACCES if called before
322 * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
323 * null; or 0 upon success.
325 int pwrdm_register_pwrdms(struct powerdomain
**ps
)
327 struct powerdomain
**p
= NULL
;
335 for (p
= ps
; *p
; p
++)
342 * pwrdm_complete_init - set up the powerdomain layer
344 * Do whatever is necessary to initialize registered powerdomains and
345 * powerdomain code. Currently, this programs the next power state
346 * for each powerdomain to ON. This prevents powerdomains from
347 * unexpectedly losing context or entering high wakeup latency modes
348 * with non-power-management-enabled kernels. Must be called after
349 * pwrdm_register_pwrdms(). Returns -EACCES if called before
350 * pwrdm_register_pwrdms(), or 0 upon success.
352 int pwrdm_complete_init(void)
354 struct powerdomain
*temp_p
;
356 if (list_empty(&pwrdm_list
))
359 list_for_each_entry(temp_p
, &pwrdm_list
, node
)
360 pwrdm_set_next_pwrst(temp_p
, PWRDM_POWER_ON
);
366 * pwrdm_lock - acquire a Linux spinlock on a powerdomain
367 * @pwrdm: struct powerdomain * to lock
369 * Acquire the powerdomain spinlock on @pwrdm. No return value.
371 void pwrdm_lock(struct powerdomain
*pwrdm
)
372 __acquires(&pwrdm
->_lock
)
374 spin_lock_irqsave(&pwrdm
->_lock
, pwrdm
->_lock_flags
);
378 * pwrdm_unlock - release a Linux spinlock on a powerdomain
379 * @pwrdm: struct powerdomain * to unlock
381 * Release the powerdomain spinlock on @pwrdm. No return value.
383 void pwrdm_unlock(struct powerdomain
*pwrdm
)
384 __releases(&pwrdm
->_lock
)
386 spin_unlock_irqrestore(&pwrdm
->_lock
, pwrdm
->_lock_flags
);
390 * pwrdm_lookup - look up a powerdomain by name, return a pointer
391 * @name: name of powerdomain
393 * Find a registered powerdomain by its name @name. Returns a pointer
394 * to the struct powerdomain if found, or NULL otherwise.
396 struct powerdomain
*pwrdm_lookup(const char *name
)
398 struct powerdomain
*pwrdm
;
403 pwrdm
= _pwrdm_lookup(name
);
409 * pwrdm_for_each - call function on each registered clockdomain
410 * @fn: callback function *
412 * Call the supplied function @fn for each registered powerdomain.
413 * The callback function @fn can return anything but 0 to bail out
414 * early from the iterator. Returns the last return value of the
415 * callback function, which should be 0 for success or anything else
416 * to indicate failure; or -EINVAL if the function pointer is null.
418 int pwrdm_for_each(int (*fn
)(struct powerdomain
*pwrdm
, void *user
),
421 struct powerdomain
*temp_pwrdm
;
427 list_for_each_entry(temp_pwrdm
, &pwrdm_list
, node
) {
428 ret
= (*fn
)(temp_pwrdm
, user
);
437 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
438 * @pwrdm: struct powerdomain * to add the clockdomain to
439 * @clkdm: struct clockdomain * to associate with a powerdomain
441 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
442 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
443 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
446 int pwrdm_add_clkdm(struct powerdomain
*pwrdm
, struct clockdomain
*clkdm
)
451 if (!pwrdm
|| !clkdm
)
454 pr_debug("powerdomain: %s: associating clockdomain %s\n",
455 pwrdm
->name
, clkdm
->name
);
457 for (i
= 0; i
< PWRDM_MAX_CLKDMS
; i
++) {
458 if (!pwrdm
->pwrdm_clkdms
[i
])
461 if (pwrdm
->pwrdm_clkdms
[i
] == clkdm
) {
468 if (i
== PWRDM_MAX_CLKDMS
) {
469 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
470 pwrdm
->name
, clkdm
->name
);
476 pwrdm
->pwrdm_clkdms
[i
] = clkdm
;
485 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
486 * @pwrdm: struct powerdomain * to add the clockdomain to
487 * @clkdm: struct clockdomain * to associate with a powerdomain
489 * Dissociate the clockdomain @clkdm from the powerdomain
490 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
491 * if @clkdm was not associated with the powerdomain, or 0 upon
494 int pwrdm_del_clkdm(struct powerdomain
*pwrdm
, struct clockdomain
*clkdm
)
499 if (!pwrdm
|| !clkdm
)
502 pr_debug("powerdomain: %s: dissociating clockdomain %s\n",
503 pwrdm
->name
, clkdm
->name
);
505 for (i
= 0; i
< PWRDM_MAX_CLKDMS
; i
++)
506 if (pwrdm
->pwrdm_clkdms
[i
] == clkdm
)
509 if (i
== PWRDM_MAX_CLKDMS
) {
510 pr_debug("powerdomain: %s: clkdm %s not associated?!\n",
511 pwrdm
->name
, clkdm
->name
);
516 pwrdm
->pwrdm_clkdms
[i
] = NULL
;
525 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
526 * @pwrdm: struct powerdomain * to iterate over
527 * @fn: callback function *
529 * Call the supplied function @fn for each clockdomain in the powerdomain
530 * @pwrdm. The callback function can return anything but 0 to bail
531 * out early from the iterator. Returns -EINVAL if presented with
532 * invalid pointers; or passes along the last return value of the
533 * callback function, which should be 0 for success or anything else
534 * to indicate failure.
536 int pwrdm_for_each_clkdm(struct powerdomain
*pwrdm
,
537 int (*fn
)(struct powerdomain
*pwrdm
,
538 struct clockdomain
*clkdm
))
546 for (i
= 0; i
< PWRDM_MAX_CLKDMS
&& !ret
; i
++)
547 ret
= (*fn
)(pwrdm
, pwrdm
->pwrdm_clkdms
[i
]);
553 * pwrdm_get_voltdm - return a ptr to the voltdm that this pwrdm resides in
554 * @pwrdm: struct powerdomain *
556 * Return a pointer to the struct voltageomain that the specified powerdomain
559 struct voltagedomain
*pwrdm_get_voltdm(struct powerdomain
*pwrdm
)
561 return pwrdm
->voltdm
.ptr
;
565 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
566 * @pwrdm: struct powerdomain *
568 * Return the number of controllable memory banks in powerdomain @pwrdm,
569 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
571 int pwrdm_get_mem_bank_count(struct powerdomain
*pwrdm
)
580 * pwrdm_set_next_pwrst - set next powerdomain power state
581 * @pwrdm: struct powerdomain * to set
582 * @pwrst: one of the PWRDM_POWER_* macros
584 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
585 * may not enter this state immediately if the preconditions for this state
586 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
587 * null or if the power state is invalid for the powerdomin, or returns 0
590 int pwrdm_set_next_pwrst(struct powerdomain
*pwrdm
, u8 pwrst
)
597 if (!(pwrdm
->pwrsts
& (1 << pwrst
)))
600 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
603 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_next_pwrst
) {
604 /* Trace the pwrdm desired target state */
605 trace_power_domain_target(pwrdm
->name
, pwrst
,
607 /* Program the pwrdm desired target state */
608 ret
= arch_pwrdm
->pwrdm_set_next_pwrst(pwrdm
, pwrst
);
615 * pwrdm_read_next_pwrst - get next powerdomain power state
616 * @pwrdm: struct powerdomain * to get power state
618 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
619 * if the powerdomain pointer is null or returns the next power state
622 int pwrdm_read_next_pwrst(struct powerdomain
*pwrdm
)
629 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_next_pwrst
)
630 ret
= arch_pwrdm
->pwrdm_read_next_pwrst(pwrdm
);
636 * pwrdm_read_pwrst - get current powerdomain power state
637 * @pwrdm: struct powerdomain * to get power state
639 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
640 * if the powerdomain pointer is null or returns the current power state
641 * upon success. Note that if the power domain only supports the ON state
642 * then just return ON as the current state.
644 int pwrdm_read_pwrst(struct powerdomain
*pwrdm
)
651 if (pwrdm
->pwrsts
== PWRSTS_ON
)
652 return PWRDM_POWER_ON
;
654 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_pwrst
)
655 ret
= arch_pwrdm
->pwrdm_read_pwrst(pwrdm
);
661 * pwrdm_read_prev_pwrst - get previous powerdomain power state
662 * @pwrdm: struct powerdomain * to get previous power state
664 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
665 * if the powerdomain pointer is null or returns the previous power state
668 int pwrdm_read_prev_pwrst(struct powerdomain
*pwrdm
)
675 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_prev_pwrst
)
676 ret
= arch_pwrdm
->pwrdm_read_prev_pwrst(pwrdm
);
682 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
683 * @pwrdm: struct powerdomain * to set
684 * @pwrst: one of the PWRDM_POWER_* macros
686 * Set the next power state @pwrst that the logic portion of the
687 * powerdomain @pwrdm will enter when the powerdomain enters retention.
688 * This will be either RETENTION or OFF, if supported. Returns
689 * -EINVAL if the powerdomain pointer is null or the target power
690 * state is not not supported, or returns 0 upon success.
692 int pwrdm_set_logic_retst(struct powerdomain
*pwrdm
, u8 pwrst
)
699 if (!(pwrdm
->pwrsts_logic_ret
& (1 << pwrst
)))
702 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
705 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_logic_retst
)
706 ret
= arch_pwrdm
->pwrdm_set_logic_retst(pwrdm
, pwrst
);
712 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
713 * @pwrdm: struct powerdomain * to set
714 * @bank: memory bank number to set (0-3)
715 * @pwrst: one of the PWRDM_POWER_* macros
717 * Set the next power state @pwrst that memory bank @bank of the
718 * powerdomain @pwrdm will enter when the powerdomain enters the ON
719 * state. @bank will be a number from 0 to 3, and represents different
720 * types of memory, depending on the powerdomain. Returns -EINVAL if
721 * the powerdomain pointer is null or the target power state is not
722 * not supported for this memory bank, -EEXIST if the target memory
723 * bank does not exist or is not controllable, or returns 0 upon
726 int pwrdm_set_mem_onst(struct powerdomain
*pwrdm
, u8 bank
, u8 pwrst
)
733 if (pwrdm
->banks
< (bank
+ 1))
736 if (!(pwrdm
->pwrsts_mem_on
[bank
] & (1 << pwrst
)))
739 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
740 pwrdm
->name
, bank
, pwrst
);
742 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_mem_onst
)
743 ret
= arch_pwrdm
->pwrdm_set_mem_onst(pwrdm
, bank
, pwrst
);
749 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
750 * @pwrdm: struct powerdomain * to set
751 * @bank: memory bank number to set (0-3)
752 * @pwrst: one of the PWRDM_POWER_* macros
754 * Set the next power state @pwrst that memory bank @bank of the
755 * powerdomain @pwrdm will enter when the powerdomain enters the
756 * RETENTION state. Bank will be a number from 0 to 3, and represents
757 * different types of memory, depending on the powerdomain. @pwrst
758 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
759 * the powerdomain pointer is null or the target power state is not
760 * not supported for this memory bank, -EEXIST if the target memory
761 * bank does not exist or is not controllable, or returns 0 upon
764 int pwrdm_set_mem_retst(struct powerdomain
*pwrdm
, u8 bank
, u8 pwrst
)
771 if (pwrdm
->banks
< (bank
+ 1))
774 if (!(pwrdm
->pwrsts_mem_ret
[bank
] & (1 << pwrst
)))
777 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
778 pwrdm
->name
, bank
, pwrst
);
780 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_mem_retst
)
781 ret
= arch_pwrdm
->pwrdm_set_mem_retst(pwrdm
, bank
, pwrst
);
787 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
788 * @pwrdm: struct powerdomain * to get current logic retention power state
790 * Return the power state that the logic portion of powerdomain @pwrdm
791 * will enter when the powerdomain enters retention. Returns -EINVAL
792 * if the powerdomain pointer is null or returns the logic retention
793 * power state upon success.
795 int pwrdm_read_logic_pwrst(struct powerdomain
*pwrdm
)
802 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_logic_pwrst
)
803 ret
= arch_pwrdm
->pwrdm_read_logic_pwrst(pwrdm
);
809 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
810 * @pwrdm: struct powerdomain * to get previous logic power state
812 * Return the powerdomain @pwrdm's previous logic power state. Returns
813 * -EINVAL if the powerdomain pointer is null or returns the previous
814 * logic power state upon success.
816 int pwrdm_read_prev_logic_pwrst(struct powerdomain
*pwrdm
)
823 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_prev_logic_pwrst
)
824 ret
= arch_pwrdm
->pwrdm_read_prev_logic_pwrst(pwrdm
);
830 * pwrdm_read_logic_retst - get next powerdomain logic power state
831 * @pwrdm: struct powerdomain * to get next logic power state
833 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
834 * if the powerdomain pointer is null or returns the next logic
835 * power state upon success.
837 int pwrdm_read_logic_retst(struct powerdomain
*pwrdm
)
844 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_logic_retst
)
845 ret
= arch_pwrdm
->pwrdm_read_logic_retst(pwrdm
);
851 * pwrdm_read_mem_pwrst - get current memory bank power state
852 * @pwrdm: struct powerdomain * to get current memory bank power state
853 * @bank: memory bank number (0-3)
855 * Return the powerdomain @pwrdm's current memory power state for bank
856 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
857 * the target memory bank does not exist or is not controllable, or
858 * returns the current memory power state upon success.
860 int pwrdm_read_mem_pwrst(struct powerdomain
*pwrdm
, u8 bank
)
867 if (pwrdm
->banks
< (bank
+ 1))
870 if (pwrdm
->flags
& PWRDM_HAS_MPU_QUIRK
)
873 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_mem_pwrst
)
874 ret
= arch_pwrdm
->pwrdm_read_mem_pwrst(pwrdm
, bank
);
880 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
881 * @pwrdm: struct powerdomain * to get previous memory bank power state
882 * @bank: memory bank number (0-3)
884 * Return the powerdomain @pwrdm's previous memory power state for
885 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
886 * -EEXIST if the target memory bank does not exist or is not
887 * controllable, or returns the previous memory power state upon
890 int pwrdm_read_prev_mem_pwrst(struct powerdomain
*pwrdm
, u8 bank
)
897 if (pwrdm
->banks
< (bank
+ 1))
900 if (pwrdm
->flags
& PWRDM_HAS_MPU_QUIRK
)
903 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_prev_mem_pwrst
)
904 ret
= arch_pwrdm
->pwrdm_read_prev_mem_pwrst(pwrdm
, bank
);
910 * pwrdm_read_mem_retst - get next memory bank power state
911 * @pwrdm: struct powerdomain * to get mext memory bank power state
912 * @bank: memory bank number (0-3)
914 * Return the powerdomain pwrdm's next memory power state for bank
915 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
916 * the target memory bank does not exist or is not controllable, or
917 * returns the next memory power state upon success.
919 int pwrdm_read_mem_retst(struct powerdomain
*pwrdm
, u8 bank
)
926 if (pwrdm
->banks
< (bank
+ 1))
929 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_mem_retst
)
930 ret
= arch_pwrdm
->pwrdm_read_mem_retst(pwrdm
, bank
);
936 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
937 * @pwrdm: struct powerdomain * to clear
939 * Clear the powerdomain's previous power state register @pwrdm.
940 * Clears the entire register, including logic and memory bank
941 * previous power states. Returns -EINVAL if the powerdomain pointer
942 * is null, or returns 0 upon success.
944 int pwrdm_clear_all_prev_pwrst(struct powerdomain
*pwrdm
)
952 * XXX should get the powerdomain's current state here;
953 * warn & fail if it is not ON.
956 pr_debug("powerdomain: %s: clearing previous power state reg\n",
959 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_clear_all_prev_pwrst
)
960 ret
= arch_pwrdm
->pwrdm_clear_all_prev_pwrst(pwrdm
);
966 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
967 * @pwrdm: struct powerdomain *
969 * Enable automatic context save-and-restore upon power state change
970 * for some devices in the powerdomain @pwrdm. Warning: this only
971 * affects a subset of devices in a powerdomain; check the TRM
972 * closely. Returns -EINVAL if the powerdomain pointer is null or if
973 * the powerdomain does not support automatic save-and-restore, or
974 * returns 0 upon success.
976 int pwrdm_enable_hdwr_sar(struct powerdomain
*pwrdm
)
983 if (!(pwrdm
->flags
& PWRDM_HAS_HDWR_SAR
))
986 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm
->name
);
988 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_enable_hdwr_sar
)
989 ret
= arch_pwrdm
->pwrdm_enable_hdwr_sar(pwrdm
);
995 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
996 * @pwrdm: struct powerdomain *
998 * Disable automatic context save-and-restore upon power state change
999 * for some devices in the powerdomain @pwrdm. Warning: this only
1000 * affects a subset of devices in a powerdomain; check the TRM
1001 * closely. Returns -EINVAL if the powerdomain pointer is null or if
1002 * the powerdomain does not support automatic save-and-restore, or
1003 * returns 0 upon success.
1005 int pwrdm_disable_hdwr_sar(struct powerdomain
*pwrdm
)
1012 if (!(pwrdm
->flags
& PWRDM_HAS_HDWR_SAR
))
1015 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm
->name
);
1017 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_disable_hdwr_sar
)
1018 ret
= arch_pwrdm
->pwrdm_disable_hdwr_sar(pwrdm
);
1024 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
1025 * @pwrdm: struct powerdomain *
1027 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
1028 * for some devices, or 0 if it does not.
1030 bool pwrdm_has_hdwr_sar(struct powerdomain
*pwrdm
)
1032 return (pwrdm
&& pwrdm
->flags
& PWRDM_HAS_HDWR_SAR
) ? 1 : 0;
1035 int pwrdm_state_switch_nolock(struct powerdomain
*pwrdm
)
1039 if (!pwrdm
|| !arch_pwrdm
)
1042 ret
= arch_pwrdm
->pwrdm_wait_transition(pwrdm
);
1044 ret
= _pwrdm_state_switch(pwrdm
, PWRDM_STATE_NOW
);
1049 int __deprecated
pwrdm_state_switch(struct powerdomain
*pwrdm
)
1054 ret
= pwrdm_state_switch_nolock(pwrdm
);
1055 pwrdm_unlock(pwrdm
);
1060 int pwrdm_pre_transition(struct powerdomain
*pwrdm
)
1063 _pwrdm_pre_transition_cb(pwrdm
, NULL
);
1065 pwrdm_for_each(_pwrdm_pre_transition_cb
, NULL
);
1070 int pwrdm_post_transition(struct powerdomain
*pwrdm
)
1073 _pwrdm_post_transition_cb(pwrdm
, NULL
);
1075 pwrdm_for_each(_pwrdm_post_transition_cb
, NULL
);
1081 * omap_set_pwrdm_state - change a powerdomain's current power state
1082 * @pwrdm: struct powerdomain * to change the power state of
1083 * @pwrst: power state to change to
1085 * Change the current hardware power state of the powerdomain
1086 * represented by @pwrdm to the power state represented by @pwrst.
1087 * Returns -EINVAL if @pwrdm is null or invalid or if the
1088 * powerdomain's current power state could not be read, or returns 0
1089 * upon success or if @pwrdm does not support @pwrst or any
1090 * lower-power state. XXX Should not return 0 if the @pwrdm does not
1091 * support @pwrst or any lower-power state: this should be an error.
1093 int omap_set_pwrdm_state(struct powerdomain
*pwrdm
, u8 pwrst
)
1095 u8 next_pwrst
, sleep_switch
;
1100 if (!pwrdm
|| IS_ERR(pwrdm
))
1103 while (!(pwrdm
->pwrsts
& (1 << pwrst
))) {
1104 if (pwrst
== PWRDM_POWER_OFF
)
1111 curr_pwrst
= pwrdm_read_pwrst(pwrdm
);
1112 if (curr_pwrst
< 0) {
1117 next_pwrst
= pwrdm_read_next_pwrst(pwrdm
);
1118 if (curr_pwrst
== pwrst
&& next_pwrst
== pwrst
)
1121 sleep_switch
= _pwrdm_save_clkdm_state_and_activate(pwrdm
, curr_pwrst
,
1124 ret
= pwrdm_set_next_pwrst(pwrdm
, pwrst
);
1126 pr_err("%s: unable to set power state of powerdomain: %s\n",
1127 __func__
, pwrdm
->name
);
1129 _pwrdm_restore_clkdm_state(pwrdm
, sleep_switch
, hwsup
);
1132 pwrdm_unlock(pwrdm
);
1138 * pwrdm_get_context_loss_count - get powerdomain's context loss count
1139 * @pwrdm: struct powerdomain * to wait for
1141 * Context loss count is the sum of powerdomain off-mode counter, the
1142 * logic off counter and the per-bank memory off counter. Returns negative
1143 * (and WARNs) upon error, otherwise, returns the context loss count.
1145 int pwrdm_get_context_loss_count(struct powerdomain
*pwrdm
)
1150 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__
);
1154 count
= pwrdm
->state_counter
[PWRDM_POWER_OFF
];
1155 count
+= pwrdm
->ret_logic_off_counter
;
1157 for (i
= 0; i
< pwrdm
->banks
; i
++)
1158 count
+= pwrdm
->ret_mem_off_counter
[i
];
1161 * Context loss count has to be a non-negative value. Clear the sign
1162 * bit to get a value range from 0 to INT_MAX.
1166 pr_debug("powerdomain: %s: context loss count = %d\n",
1167 pwrdm
->name
, count
);
1173 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1174 * @pwrdm: struct powerdomain *
1176 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1177 * can lose either memory or logic context or if @pwrdm is invalid, or
1178 * returns 0 otherwise. This function is not concerned with how the
1179 * powerdomain registers are programmed (i.e., to go off or not); it's
1180 * concerned with whether it's ever possible for this powerdomain to
1181 * go off while some other part of the chip is active. This function
1182 * assumes that every powerdomain can go to either ON or INACTIVE.
1184 bool pwrdm_can_ever_lose_context(struct powerdomain
*pwrdm
)
1189 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1194 if (pwrdm
->pwrsts
& PWRSTS_OFF
)
1197 if (pwrdm
->pwrsts
& PWRSTS_RET
) {
1198 if (pwrdm
->pwrsts_logic_ret
& PWRSTS_OFF
)
1201 for (i
= 0; i
< pwrdm
->banks
; i
++)
1202 if (pwrdm
->pwrsts_mem_ret
[i
] & PWRSTS_OFF
)
1206 for (i
= 0; i
< pwrdm
->banks
; i
++)
1207 if (pwrdm
->pwrsts_mem_on
[i
] & PWRSTS_OFF
)