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 <trace/events/power.h>
24 #include "cm2xxx_3xxx.h"
27 #include "prm2xxx_3xxx.h"
32 #include "powerdomain.h"
33 #include "clockdomain.h"
38 #define PWRDM_TRACE_STATES_FLAG (1<<31)
46 /* pwrdm_list contains all registered struct powerdomains */
47 static LIST_HEAD(pwrdm_list
);
49 static struct pwrdm_ops
*arch_pwrdm
;
51 /* Private functions */
53 static struct powerdomain
*_pwrdm_lookup(const char *name
)
55 struct powerdomain
*pwrdm
, *temp_pwrdm
;
59 list_for_each_entry(temp_pwrdm
, &pwrdm_list
, node
) {
60 if (!strcmp(name
, temp_pwrdm
->name
)) {
70 * _pwrdm_register - register a powerdomain
71 * @pwrdm: struct powerdomain * to register
73 * Adds a powerdomain to the internal powerdomain list. Returns
74 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
75 * already registered by the provided name, or 0 upon success.
77 static int _pwrdm_register(struct powerdomain
*pwrdm
)
80 struct voltagedomain
*voltdm
;
82 if (!pwrdm
|| !pwrdm
->name
)
85 if (cpu_is_omap44xx() &&
86 pwrdm
->prcm_partition
== OMAP4430_INVALID_PRCM_PARTITION
) {
87 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
92 if (_pwrdm_lookup(pwrdm
->name
))
95 voltdm
= voltdm_lookup(pwrdm
->voltdm
.name
);
97 pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
98 pwrdm
->name
, pwrdm
->voltdm
.name
);
101 pwrdm
->voltdm
.ptr
= voltdm
;
102 INIT_LIST_HEAD(&pwrdm
->voltdm_node
);
103 voltdm_add_pwrdm(voltdm
, pwrdm
);
105 list_add(&pwrdm
->node
, &pwrdm_list
);
107 /* Initialize the powerdomain's state counter */
108 for (i
= 0; i
< PWRDM_MAX_PWRSTS
; i
++)
109 pwrdm
->state_counter
[i
] = 0;
111 pwrdm
->ret_logic_off_counter
= 0;
112 for (i
= 0; i
< pwrdm
->banks
; i
++)
113 pwrdm
->ret_mem_off_counter
[i
] = 0;
115 pwrdm_wait_transition(pwrdm
);
116 pwrdm
->state
= pwrdm_read_pwrst(pwrdm
);
117 pwrdm
->state_counter
[pwrdm
->state
] = 1;
119 pr_debug("powerdomain: registered %s\n", pwrdm
->name
);
124 static void _update_logic_membank_counters(struct powerdomain
*pwrdm
)
127 u8 prev_logic_pwrst
, prev_mem_pwrst
;
129 prev_logic_pwrst
= pwrdm_read_prev_logic_pwrst(pwrdm
);
130 if ((pwrdm
->pwrsts_logic_ret
== PWRSTS_OFF_RET
) &&
131 (prev_logic_pwrst
== PWRDM_POWER_OFF
))
132 pwrdm
->ret_logic_off_counter
++;
134 for (i
= 0; i
< pwrdm
->banks
; i
++) {
135 prev_mem_pwrst
= pwrdm_read_prev_mem_pwrst(pwrdm
, i
);
137 if ((pwrdm
->pwrsts_mem_ret
[i
] == PWRSTS_OFF_RET
) &&
138 (prev_mem_pwrst
== PWRDM_POWER_OFF
))
139 pwrdm
->ret_mem_off_counter
[i
]++;
143 static int _pwrdm_state_switch(struct powerdomain
*pwrdm
, int flag
)
146 int prev
, state
, trace_state
= 0;
151 state
= pwrdm_read_pwrst(pwrdm
);
154 case PWRDM_STATE_NOW
:
157 case PWRDM_STATE_PREV
:
158 prev
= pwrdm_read_prev_pwrst(pwrdm
);
159 if (pwrdm
->state
!= prev
)
160 pwrdm
->state_counter
[prev
]++;
161 if (prev
== PWRDM_POWER_RET
)
162 _update_logic_membank_counters(pwrdm
);
164 * If the power domain did not hit the desired state,
165 * generate a trace event with both the desired and hit states
168 trace_state
= (PWRDM_TRACE_STATES_FLAG
|
169 ((state
& OMAP_POWERSTATE_MASK
) << 8) |
170 ((prev
& OMAP_POWERSTATE_MASK
) << 0));
171 trace_power_domain_target(pwrdm
->name
, trace_state
,
180 pwrdm
->state_counter
[state
]++;
182 pm_dbg_update_time(pwrdm
, prev
);
184 pwrdm
->state
= state
;
189 static int _pwrdm_pre_transition_cb(struct powerdomain
*pwrdm
, void *unused
)
191 pwrdm_clear_all_prev_pwrst(pwrdm
);
192 _pwrdm_state_switch(pwrdm
, PWRDM_STATE_NOW
);
196 static int _pwrdm_post_transition_cb(struct powerdomain
*pwrdm
, void *unused
)
198 _pwrdm_state_switch(pwrdm
, PWRDM_STATE_PREV
);
202 /* Public functions */
205 * pwrdm_register_platform_funcs - register powerdomain implementation fns
206 * @po: func pointers for arch specific implementations
208 * Register the list of function pointers used to implement the
209 * powerdomain functions on different OMAP SoCs. Should be called
210 * before any other pwrdm_register*() function. Returns -EINVAL if
211 * @po is null, -EEXIST if platform functions have already been
212 * registered, or 0 upon success.
214 int pwrdm_register_platform_funcs(struct pwrdm_ops
*po
)
228 * pwrdm_register_pwrdms - register SoC powerdomains
229 * @ps: pointer to an array of struct powerdomain to register
231 * Register the powerdomains available on a particular OMAP SoC. Must
232 * be called after pwrdm_register_platform_funcs(). May be called
233 * multiple times. Returns -EACCES if called before
234 * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
235 * null; or 0 upon success.
237 int pwrdm_register_pwrdms(struct powerdomain
**ps
)
239 struct powerdomain
**p
= NULL
;
247 for (p
= ps
; *p
; p
++)
254 * pwrdm_complete_init - set up the powerdomain layer
256 * Do whatever is necessary to initialize registered powerdomains and
257 * powerdomain code. Currently, this programs the next power state
258 * for each powerdomain to ON. This prevents powerdomains from
259 * unexpectedly losing context or entering high wakeup latency modes
260 * with non-power-management-enabled kernels. Must be called after
261 * pwrdm_register_pwrdms(). Returns -EACCES if called before
262 * pwrdm_register_pwrdms(), or 0 upon success.
264 int pwrdm_complete_init(void)
266 struct powerdomain
*temp_p
;
268 if (list_empty(&pwrdm_list
))
271 list_for_each_entry(temp_p
, &pwrdm_list
, node
)
272 pwrdm_set_next_pwrst(temp_p
, PWRDM_POWER_ON
);
278 * pwrdm_lookup - look up a powerdomain by name, return a pointer
279 * @name: name of powerdomain
281 * Find a registered powerdomain by its name @name. Returns a pointer
282 * to the struct powerdomain if found, or NULL otherwise.
284 struct powerdomain
*pwrdm_lookup(const char *name
)
286 struct powerdomain
*pwrdm
;
291 pwrdm
= _pwrdm_lookup(name
);
297 * pwrdm_for_each - call function on each registered clockdomain
298 * @fn: callback function *
300 * Call the supplied function @fn for each registered powerdomain.
301 * The callback function @fn can return anything but 0 to bail out
302 * early from the iterator. Returns the last return value of the
303 * callback function, which should be 0 for success or anything else
304 * to indicate failure; or -EINVAL if the function pointer is null.
306 int pwrdm_for_each(int (*fn
)(struct powerdomain
*pwrdm
, void *user
),
309 struct powerdomain
*temp_pwrdm
;
315 list_for_each_entry(temp_pwrdm
, &pwrdm_list
, node
) {
316 ret
= (*fn
)(temp_pwrdm
, user
);
325 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
326 * @pwrdm: struct powerdomain * to add the clockdomain to
327 * @clkdm: struct clockdomain * to associate with a powerdomain
329 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
330 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
331 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
334 int pwrdm_add_clkdm(struct powerdomain
*pwrdm
, struct clockdomain
*clkdm
)
339 if (!pwrdm
|| !clkdm
)
342 pr_debug("powerdomain: %s: associating clockdomain %s\n",
343 pwrdm
->name
, clkdm
->name
);
345 for (i
= 0; i
< PWRDM_MAX_CLKDMS
; i
++) {
346 if (!pwrdm
->pwrdm_clkdms
[i
])
349 if (pwrdm
->pwrdm_clkdms
[i
] == clkdm
) {
356 if (i
== PWRDM_MAX_CLKDMS
) {
357 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
358 pwrdm
->name
, clkdm
->name
);
364 pwrdm
->pwrdm_clkdms
[i
] = clkdm
;
373 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
374 * @pwrdm: struct powerdomain * to add the clockdomain to
375 * @clkdm: struct clockdomain * to associate with a powerdomain
377 * Dissociate the clockdomain @clkdm from the powerdomain
378 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
379 * if @clkdm was not associated with the powerdomain, or 0 upon
382 int pwrdm_del_clkdm(struct powerdomain
*pwrdm
, struct clockdomain
*clkdm
)
387 if (!pwrdm
|| !clkdm
)
390 pr_debug("powerdomain: %s: dissociating clockdomain %s\n",
391 pwrdm
->name
, clkdm
->name
);
393 for (i
= 0; i
< PWRDM_MAX_CLKDMS
; i
++)
394 if (pwrdm
->pwrdm_clkdms
[i
] == clkdm
)
397 if (i
== PWRDM_MAX_CLKDMS
) {
398 pr_debug("powerdomain: %s: clkdm %s not associated?!\n",
399 pwrdm
->name
, clkdm
->name
);
404 pwrdm
->pwrdm_clkdms
[i
] = NULL
;
413 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
414 * @pwrdm: struct powerdomain * to iterate over
415 * @fn: callback function *
417 * Call the supplied function @fn for each clockdomain in the powerdomain
418 * @pwrdm. The callback function can return anything but 0 to bail
419 * out early from the iterator. Returns -EINVAL if presented with
420 * invalid pointers; or passes along the last return value of the
421 * callback function, which should be 0 for success or anything else
422 * to indicate failure.
424 int pwrdm_for_each_clkdm(struct powerdomain
*pwrdm
,
425 int (*fn
)(struct powerdomain
*pwrdm
,
426 struct clockdomain
*clkdm
))
434 for (i
= 0; i
< PWRDM_MAX_CLKDMS
&& !ret
; i
++)
435 ret
= (*fn
)(pwrdm
, pwrdm
->pwrdm_clkdms
[i
]);
441 * pwrdm_get_voltdm - return a ptr to the voltdm that this pwrdm resides in
442 * @pwrdm: struct powerdomain *
444 * Return a pointer to the struct voltageomain that the specified powerdomain
447 struct voltagedomain
*pwrdm_get_voltdm(struct powerdomain
*pwrdm
)
449 return pwrdm
->voltdm
.ptr
;
453 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
454 * @pwrdm: struct powerdomain *
456 * Return the number of controllable memory banks in powerdomain @pwrdm,
457 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
459 int pwrdm_get_mem_bank_count(struct powerdomain
*pwrdm
)
468 * pwrdm_set_next_pwrst - set next powerdomain power state
469 * @pwrdm: struct powerdomain * to set
470 * @pwrst: one of the PWRDM_POWER_* macros
472 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
473 * may not enter this state immediately if the preconditions for this state
474 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
475 * null or if the power state is invalid for the powerdomin, or returns 0
478 int pwrdm_set_next_pwrst(struct powerdomain
*pwrdm
, u8 pwrst
)
485 if (!(pwrdm
->pwrsts
& (1 << pwrst
)))
488 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
491 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_next_pwrst
) {
492 /* Trace the pwrdm desired target state */
493 trace_power_domain_target(pwrdm
->name
, pwrst
,
495 /* Program the pwrdm desired target state */
496 ret
= arch_pwrdm
->pwrdm_set_next_pwrst(pwrdm
, pwrst
);
503 * pwrdm_read_next_pwrst - get next powerdomain power state
504 * @pwrdm: struct powerdomain * to get power state
506 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
507 * if the powerdomain pointer is null or returns the next power state
510 int pwrdm_read_next_pwrst(struct powerdomain
*pwrdm
)
517 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_next_pwrst
)
518 ret
= arch_pwrdm
->pwrdm_read_next_pwrst(pwrdm
);
524 * pwrdm_read_pwrst - get current powerdomain power state
525 * @pwrdm: struct powerdomain * to get power state
527 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
528 * if the powerdomain pointer is null or returns the current power state
529 * upon success. Note that if the power domain only supports the ON state
530 * then just return ON as the current state.
532 int pwrdm_read_pwrst(struct powerdomain
*pwrdm
)
539 if (pwrdm
->pwrsts
== PWRSTS_ON
)
540 return PWRDM_POWER_ON
;
542 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_pwrst
)
543 ret
= arch_pwrdm
->pwrdm_read_pwrst(pwrdm
);
549 * pwrdm_read_prev_pwrst - get previous powerdomain power state
550 * @pwrdm: struct powerdomain * to get previous power state
552 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
553 * if the powerdomain pointer is null or returns the previous power state
556 int pwrdm_read_prev_pwrst(struct powerdomain
*pwrdm
)
563 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_prev_pwrst
)
564 ret
= arch_pwrdm
->pwrdm_read_prev_pwrst(pwrdm
);
570 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
571 * @pwrdm: struct powerdomain * to set
572 * @pwrst: one of the PWRDM_POWER_* macros
574 * Set the next power state @pwrst that the logic portion of the
575 * powerdomain @pwrdm will enter when the powerdomain enters retention.
576 * This will be either RETENTION or OFF, if supported. Returns
577 * -EINVAL if the powerdomain pointer is null or the target power
578 * state is not not supported, or returns 0 upon success.
580 int pwrdm_set_logic_retst(struct powerdomain
*pwrdm
, u8 pwrst
)
587 if (!(pwrdm
->pwrsts_logic_ret
& (1 << pwrst
)))
590 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
593 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_logic_retst
)
594 ret
= arch_pwrdm
->pwrdm_set_logic_retst(pwrdm
, pwrst
);
600 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
601 * @pwrdm: struct powerdomain * to set
602 * @bank: memory bank number to set (0-3)
603 * @pwrst: one of the PWRDM_POWER_* macros
605 * Set the next power state @pwrst that memory bank @bank of the
606 * powerdomain @pwrdm will enter when the powerdomain enters the ON
607 * state. @bank will be a number from 0 to 3, and represents different
608 * types of memory, depending on the powerdomain. Returns -EINVAL if
609 * the powerdomain pointer is null or the target power state is not
610 * not supported for this memory bank, -EEXIST if the target memory
611 * bank does not exist or is not controllable, or returns 0 upon
614 int pwrdm_set_mem_onst(struct powerdomain
*pwrdm
, u8 bank
, u8 pwrst
)
621 if (pwrdm
->banks
< (bank
+ 1))
624 if (!(pwrdm
->pwrsts_mem_on
[bank
] & (1 << pwrst
)))
627 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
628 pwrdm
->name
, bank
, pwrst
);
630 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_mem_onst
)
631 ret
= arch_pwrdm
->pwrdm_set_mem_onst(pwrdm
, bank
, pwrst
);
637 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
638 * @pwrdm: struct powerdomain * to set
639 * @bank: memory bank number to set (0-3)
640 * @pwrst: one of the PWRDM_POWER_* macros
642 * Set the next power state @pwrst that memory bank @bank of the
643 * powerdomain @pwrdm will enter when the powerdomain enters the
644 * RETENTION state. Bank will be a number from 0 to 3, and represents
645 * different types of memory, depending on the powerdomain. @pwrst
646 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
647 * the powerdomain pointer is null or the target power state is not
648 * not supported for this memory bank, -EEXIST if the target memory
649 * bank does not exist or is not controllable, or returns 0 upon
652 int pwrdm_set_mem_retst(struct powerdomain
*pwrdm
, u8 bank
, u8 pwrst
)
659 if (pwrdm
->banks
< (bank
+ 1))
662 if (!(pwrdm
->pwrsts_mem_ret
[bank
] & (1 << pwrst
)))
665 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
666 pwrdm
->name
, bank
, pwrst
);
668 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_mem_retst
)
669 ret
= arch_pwrdm
->pwrdm_set_mem_retst(pwrdm
, bank
, pwrst
);
675 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
676 * @pwrdm: struct powerdomain * to get current logic retention power state
678 * Return the power state that the logic portion of powerdomain @pwrdm
679 * will enter when the powerdomain enters retention. Returns -EINVAL
680 * if the powerdomain pointer is null or returns the logic retention
681 * power state upon success.
683 int pwrdm_read_logic_pwrst(struct powerdomain
*pwrdm
)
690 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_logic_pwrst
)
691 ret
= arch_pwrdm
->pwrdm_read_logic_pwrst(pwrdm
);
697 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
698 * @pwrdm: struct powerdomain * to get previous logic power state
700 * Return the powerdomain @pwrdm's previous logic power state. Returns
701 * -EINVAL if the powerdomain pointer is null or returns the previous
702 * logic power state upon success.
704 int pwrdm_read_prev_logic_pwrst(struct powerdomain
*pwrdm
)
711 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_prev_logic_pwrst
)
712 ret
= arch_pwrdm
->pwrdm_read_prev_logic_pwrst(pwrdm
);
718 * pwrdm_read_logic_retst - get next powerdomain logic power state
719 * @pwrdm: struct powerdomain * to get next logic power state
721 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
722 * if the powerdomain pointer is null or returns the next logic
723 * power state upon success.
725 int pwrdm_read_logic_retst(struct powerdomain
*pwrdm
)
732 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_logic_retst
)
733 ret
= arch_pwrdm
->pwrdm_read_logic_retst(pwrdm
);
739 * pwrdm_read_mem_pwrst - get current memory bank power state
740 * @pwrdm: struct powerdomain * to get current memory bank power state
741 * @bank: memory bank number (0-3)
743 * Return the powerdomain @pwrdm's current memory power state for bank
744 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
745 * the target memory bank does not exist or is not controllable, or
746 * returns the current memory power state upon success.
748 int pwrdm_read_mem_pwrst(struct powerdomain
*pwrdm
, u8 bank
)
755 if (pwrdm
->banks
< (bank
+ 1))
758 if (pwrdm
->flags
& PWRDM_HAS_MPU_QUIRK
)
761 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_mem_pwrst
)
762 ret
= arch_pwrdm
->pwrdm_read_mem_pwrst(pwrdm
, bank
);
768 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
769 * @pwrdm: struct powerdomain * to get previous memory bank power state
770 * @bank: memory bank number (0-3)
772 * Return the powerdomain @pwrdm's previous memory power state for
773 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
774 * -EEXIST if the target memory bank does not exist or is not
775 * controllable, or returns the previous memory power state upon
778 int pwrdm_read_prev_mem_pwrst(struct powerdomain
*pwrdm
, u8 bank
)
785 if (pwrdm
->banks
< (bank
+ 1))
788 if (pwrdm
->flags
& PWRDM_HAS_MPU_QUIRK
)
791 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_prev_mem_pwrst
)
792 ret
= arch_pwrdm
->pwrdm_read_prev_mem_pwrst(pwrdm
, bank
);
798 * pwrdm_read_mem_retst - get next memory bank power state
799 * @pwrdm: struct powerdomain * to get mext memory bank power state
800 * @bank: memory bank number (0-3)
802 * Return the powerdomain pwrdm's next memory power state for bank
803 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
804 * the target memory bank does not exist or is not controllable, or
805 * returns the next memory power state upon success.
807 int pwrdm_read_mem_retst(struct powerdomain
*pwrdm
, u8 bank
)
814 if (pwrdm
->banks
< (bank
+ 1))
817 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_read_mem_retst
)
818 ret
= arch_pwrdm
->pwrdm_read_mem_retst(pwrdm
, bank
);
824 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
825 * @pwrdm: struct powerdomain * to clear
827 * Clear the powerdomain's previous power state register @pwrdm.
828 * Clears the entire register, including logic and memory bank
829 * previous power states. Returns -EINVAL if the powerdomain pointer
830 * is null, or returns 0 upon success.
832 int pwrdm_clear_all_prev_pwrst(struct powerdomain
*pwrdm
)
840 * XXX should get the powerdomain's current state here;
841 * warn & fail if it is not ON.
844 pr_debug("powerdomain: %s: clearing previous power state reg\n",
847 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_clear_all_prev_pwrst
)
848 ret
= arch_pwrdm
->pwrdm_clear_all_prev_pwrst(pwrdm
);
854 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
855 * @pwrdm: struct powerdomain *
857 * Enable automatic context save-and-restore upon power state change
858 * for some devices in the powerdomain @pwrdm. Warning: this only
859 * affects a subset of devices in a powerdomain; check the TRM
860 * closely. Returns -EINVAL if the powerdomain pointer is null or if
861 * the powerdomain does not support automatic save-and-restore, or
862 * returns 0 upon success.
864 int pwrdm_enable_hdwr_sar(struct powerdomain
*pwrdm
)
871 if (!(pwrdm
->flags
& PWRDM_HAS_HDWR_SAR
))
874 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm
->name
);
876 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_enable_hdwr_sar
)
877 ret
= arch_pwrdm
->pwrdm_enable_hdwr_sar(pwrdm
);
883 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
884 * @pwrdm: struct powerdomain *
886 * Disable automatic context save-and-restore upon power state change
887 * for some devices in the powerdomain @pwrdm. Warning: this only
888 * affects a subset of devices in a powerdomain; check the TRM
889 * closely. Returns -EINVAL if the powerdomain pointer is null or if
890 * the powerdomain does not support automatic save-and-restore, or
891 * returns 0 upon success.
893 int pwrdm_disable_hdwr_sar(struct powerdomain
*pwrdm
)
900 if (!(pwrdm
->flags
& PWRDM_HAS_HDWR_SAR
))
903 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm
->name
);
905 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_disable_hdwr_sar
)
906 ret
= arch_pwrdm
->pwrdm_disable_hdwr_sar(pwrdm
);
912 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
913 * @pwrdm: struct powerdomain *
915 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
916 * for some devices, or 0 if it does not.
918 bool pwrdm_has_hdwr_sar(struct powerdomain
*pwrdm
)
920 return (pwrdm
&& pwrdm
->flags
& PWRDM_HAS_HDWR_SAR
) ? 1 : 0;
924 * pwrdm_set_lowpwrstchange - Request a low power state change
925 * @pwrdm: struct powerdomain *
927 * Allows a powerdomain to transtion to a lower power sleep state
928 * from an existing sleep state without waking up the powerdomain.
929 * Returns -EINVAL if the powerdomain pointer is null or if the
930 * powerdomain does not support LOWPOWERSTATECHANGE, or returns 0
933 int pwrdm_set_lowpwrstchange(struct powerdomain
*pwrdm
)
940 if (!(pwrdm
->flags
& PWRDM_HAS_LOWPOWERSTATECHANGE
))
943 pr_debug("powerdomain: %s: setting LOWPOWERSTATECHANGE bit\n",
946 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_set_lowpwrstchange
)
947 ret
= arch_pwrdm
->pwrdm_set_lowpwrstchange(pwrdm
);
953 * pwrdm_wait_transition - wait for powerdomain power transition to finish
954 * @pwrdm: struct powerdomain * to wait for
956 * If the powerdomain @pwrdm is in the process of a state transition,
957 * spin until it completes the power transition, or until an iteration
958 * bailout value is reached. Returns -EINVAL if the powerdomain
959 * pointer is null, -EAGAIN if the bailout value was reached, or
960 * returns 0 upon success.
962 int pwrdm_wait_transition(struct powerdomain
*pwrdm
)
969 if (arch_pwrdm
&& arch_pwrdm
->pwrdm_wait_transition
)
970 ret
= arch_pwrdm
->pwrdm_wait_transition(pwrdm
);
975 int pwrdm_state_switch(struct powerdomain
*pwrdm
)
979 ret
= pwrdm_wait_transition(pwrdm
);
981 ret
= _pwrdm_state_switch(pwrdm
, PWRDM_STATE_NOW
);
986 int pwrdm_pre_transition(struct powerdomain
*pwrdm
)
989 _pwrdm_pre_transition_cb(pwrdm
, NULL
);
991 pwrdm_for_each(_pwrdm_pre_transition_cb
, NULL
);
996 int pwrdm_post_transition(struct powerdomain
*pwrdm
)
999 _pwrdm_post_transition_cb(pwrdm
, NULL
);
1001 pwrdm_for_each(_pwrdm_post_transition_cb
, NULL
);
1007 * pwrdm_get_context_loss_count - get powerdomain's context loss count
1008 * @pwrdm: struct powerdomain * to wait for
1010 * Context loss count is the sum of powerdomain off-mode counter, the
1011 * logic off counter and the per-bank memory off counter. Returns negative
1012 * (and WARNs) upon error, otherwise, returns the context loss count.
1014 int pwrdm_get_context_loss_count(struct powerdomain
*pwrdm
)
1019 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__
);
1023 count
= pwrdm
->state_counter
[PWRDM_POWER_OFF
];
1024 count
+= pwrdm
->ret_logic_off_counter
;
1026 for (i
= 0; i
< pwrdm
->banks
; i
++)
1027 count
+= pwrdm
->ret_mem_off_counter
[i
];
1030 * Context loss count has to be a non-negative value. Clear the sign
1031 * bit to get a value range from 0 to INT_MAX.
1035 pr_debug("powerdomain: %s: context loss count = %d\n",
1036 pwrdm
->name
, count
);
1042 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1043 * @pwrdm: struct powerdomain *
1045 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1046 * can lose either memory or logic context or if @pwrdm is invalid, or
1047 * returns 0 otherwise. This function is not concerned with how the
1048 * powerdomain registers are programmed (i.e., to go off or not); it's
1049 * concerned with whether it's ever possible for this powerdomain to
1050 * go off while some other part of the chip is active. This function
1051 * assumes that every powerdomain can go to either ON or INACTIVE.
1053 bool pwrdm_can_ever_lose_context(struct powerdomain
*pwrdm
)
1057 if (IS_ERR_OR_NULL(pwrdm
)) {
1058 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1063 if (pwrdm
->pwrsts
& PWRSTS_OFF
)
1066 if (pwrdm
->pwrsts
& PWRSTS_RET
) {
1067 if (pwrdm
->pwrsts_logic_ret
& PWRSTS_OFF
)
1070 for (i
= 0; i
< pwrdm
->banks
; i
++)
1071 if (pwrdm
->pwrsts_mem_ret
[i
] & PWRSTS_OFF
)
1075 for (i
= 0; i
< pwrdm
->banks
; i
++)
1076 if (pwrdm
->pwrsts_mem_on
[i
] & PWRSTS_OFF
)