x86/xen: resume timer irqs early
[linux/fpc-iii.git] / arch / arm / mach-omap2 / powerdomain.c
blobe233dfcbc18670ff6f0e507df07b6f9c09289534
1 /*
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.
15 #undef DEBUG
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"
26 #include "prcm44xx.h"
27 #include "cm44xx.h"
28 #include "prm2xxx_3xxx.h"
29 #include "prm44xx.h"
31 #include <asm/cpu.h>
33 #include "powerdomain.h"
34 #include "clockdomain.h"
36 #include "soc.h"
37 #include "pm.h"
39 #define PWRDM_TRACE_STATES_FLAG (1<<31)
41 enum {
42 PWRDM_STATE_NOW = 0,
43 PWRDM_STATE_PREV,
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;
67 pwrdm = NULL;
69 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
70 if (!strcmp(name, temp_pwrdm->name)) {
71 pwrdm = temp_pwrdm;
72 break;
76 return pwrdm;
79 /**
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)
89 int i;
90 struct voltagedomain *voltdm;
92 if (!pwrdm || !pwrdm->name)
93 return -EINVAL;
95 if (cpu_is_omap44xx() &&
96 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
97 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
98 pwrdm->name);
99 return -EINVAL;
102 if (_pwrdm_lookup(pwrdm->name))
103 return -EEXIST;
105 if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
106 if (!arch_pwrdm->pwrdm_has_voltdm())
107 goto skip_voltdm;
109 voltdm = voltdm_lookup(pwrdm->voltdm.name);
110 if (!voltdm) {
111 pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
112 pwrdm->name, pwrdm->voltdm.name);
113 return -EINVAL;
115 pwrdm->voltdm.ptr = voltdm;
116 INIT_LIST_HEAD(&pwrdm->voltdm_node);
117 voltdm_add_pwrdm(voltdm, pwrdm);
118 skip_voltdm:
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);
137 return 0;
140 static void _update_logic_membank_counters(struct powerdomain *pwrdm)
142 int i;
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;
164 if (pwrdm == NULL)
165 return -EINVAL;
167 state = pwrdm_read_pwrst(pwrdm);
169 switch (flag) {
170 case PWRDM_STATE_NOW:
171 prev = pwrdm->state;
172 break;
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);
184 if (next != prev) {
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,
189 smp_processor_id());
191 break;
192 default:
193 return -EINVAL;
196 if (state != prev)
197 pwrdm->state_counter[state]++;
199 pm_dbg_update_time(pwrdm, prev);
201 pwrdm->state = state;
203 return 0;
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);
210 return 0;
213 static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
215 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
216 return 0;
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,
236 bool *hwsup)
238 u8 sleep_switch;
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;
245 } else {
246 *hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
247 clkdm_wakeup_nolock(pwrdm->pwrdm_clkdms[0]);
248 sleep_switch = FORCEWAKEUP_SWITCH;
250 } else {
251 sleep_switch = ALREADYACTIVE_SWITCH;
254 return sleep_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:
276 if (hwsup)
277 clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
278 else
279 clkdm_sleep_nolock(pwrdm->pwrdm_clkdms[0]);
280 break;
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);
286 break;
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)
304 if (!po)
305 return -EINVAL;
307 if (arch_pwrdm)
308 return -EEXIST;
310 arch_pwrdm = po;
312 return 0;
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;
329 if (!arch_pwrdm)
330 return -EEXIST;
332 if (!ps)
333 return -EINVAL;
335 for (p = ps; *p; p++)
336 _pwrdm_register(*p);
338 return 0;
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))
357 return -EACCES;
359 list_for_each_entry(temp_p, &pwrdm_list, node)
360 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
362 return 0;
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;
400 if (!name)
401 return NULL;
403 pwrdm = _pwrdm_lookup(name);
405 return pwrdm;
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),
419 void *user)
421 struct powerdomain *temp_pwrdm;
422 int ret = 0;
424 if (!fn)
425 return -EINVAL;
427 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
428 ret = (*fn)(temp_pwrdm, user);
429 if (ret)
430 break;
433 return ret;
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;
444 * or 0 upon success.
446 int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
448 int i;
449 int ret = -EINVAL;
451 if (!pwrdm || !clkdm)
452 return -EINVAL;
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])
459 break;
460 #ifdef DEBUG
461 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
462 ret = -EINVAL;
463 goto pac_exit;
465 #endif
468 if (i == PWRDM_MAX_CLKDMS) {
469 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
470 pwrdm->name, clkdm->name);
471 WARN_ON(1);
472 ret = -ENOMEM;
473 goto pac_exit;
476 pwrdm->pwrdm_clkdms[i] = clkdm;
478 ret = 0;
480 pac_exit:
481 return ret;
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
492 * success.
494 int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
496 int ret = -EINVAL;
497 int i;
499 if (!pwrdm || !clkdm)
500 return -EINVAL;
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)
507 break;
509 if (i == PWRDM_MAX_CLKDMS) {
510 pr_debug("powerdomain: %s: clkdm %s not associated?!\n",
511 pwrdm->name, clkdm->name);
512 ret = -ENOENT;
513 goto pdc_exit;
516 pwrdm->pwrdm_clkdms[i] = NULL;
518 ret = 0;
520 pdc_exit:
521 return ret;
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))
540 int ret = 0;
541 int i;
543 if (!fn)
544 return -EINVAL;
546 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
547 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
549 return ret;
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
557 * @pwrdm exists in.
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)
573 if (!pwrdm)
574 return -EINVAL;
576 return pwrdm->banks;
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
588 * upon success.
590 int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
592 int ret = -EINVAL;
594 if (!pwrdm)
595 return -EINVAL;
597 if (!(pwrdm->pwrsts & (1 << pwrst)))
598 return -EINVAL;
600 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
601 pwrdm->name, pwrst);
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,
606 smp_processor_id());
607 /* Program the pwrdm desired target state */
608 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
611 return ret;
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
620 * upon success.
622 int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
624 int ret = -EINVAL;
626 if (!pwrdm)
627 return -EINVAL;
629 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
630 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
632 return ret;
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)
646 int ret = -EINVAL;
648 if (!pwrdm)
649 return -EINVAL;
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);
657 return ret;
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
666 * upon success.
668 int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
670 int ret = -EINVAL;
672 if (!pwrdm)
673 return -EINVAL;
675 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
676 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
678 return ret;
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)
694 int ret = -EINVAL;
696 if (!pwrdm)
697 return -EINVAL;
699 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
700 return -EINVAL;
702 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
703 pwrdm->name, pwrst);
705 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
706 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
708 return ret;
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
724 * success.
726 int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
728 int ret = -EINVAL;
730 if (!pwrdm)
731 return -EINVAL;
733 if (pwrdm->banks < (bank + 1))
734 return -EEXIST;
736 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
737 return -EINVAL;
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);
745 return ret;
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
762 * success.
764 int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
766 int ret = -EINVAL;
768 if (!pwrdm)
769 return -EINVAL;
771 if (pwrdm->banks < (bank + 1))
772 return -EEXIST;
774 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
775 return -EINVAL;
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);
783 return ret;
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)
797 int ret = -EINVAL;
799 if (!pwrdm)
800 return -EINVAL;
802 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
803 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
805 return ret;
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)
818 int ret = -EINVAL;
820 if (!pwrdm)
821 return -EINVAL;
823 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
824 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
826 return ret;
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)
839 int ret = -EINVAL;
841 if (!pwrdm)
842 return -EINVAL;
844 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
845 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
847 return ret;
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)
862 int ret = -EINVAL;
864 if (!pwrdm)
865 return ret;
867 if (pwrdm->banks < (bank + 1))
868 return ret;
870 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
871 bank = 1;
873 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
874 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
876 return ret;
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
888 * success.
890 int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
892 int ret = -EINVAL;
894 if (!pwrdm)
895 return ret;
897 if (pwrdm->banks < (bank + 1))
898 return ret;
900 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
901 bank = 1;
903 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
904 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
906 return ret;
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)
921 int ret = -EINVAL;
923 if (!pwrdm)
924 return ret;
926 if (pwrdm->banks < (bank + 1))
927 return ret;
929 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
930 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
932 return ret;
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)
946 int ret = -EINVAL;
948 if (!pwrdm)
949 return ret;
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",
957 pwrdm->name);
959 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
960 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
962 return ret;
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)
978 int ret = -EINVAL;
980 if (!pwrdm)
981 return ret;
983 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
984 return ret;
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);
991 return ret;
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)
1007 int ret = -EINVAL;
1009 if (!pwrdm)
1010 return ret;
1012 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
1013 return ret;
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);
1020 return ret;
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)
1037 int ret;
1039 if (!pwrdm || !arch_pwrdm)
1040 return -EINVAL;
1042 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
1043 if (!ret)
1044 ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
1046 return ret;
1049 int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
1051 int ret;
1053 pwrdm_lock(pwrdm);
1054 ret = pwrdm_state_switch_nolock(pwrdm);
1055 pwrdm_unlock(pwrdm);
1057 return ret;
1060 int pwrdm_pre_transition(struct powerdomain *pwrdm)
1062 if (pwrdm)
1063 _pwrdm_pre_transition_cb(pwrdm, NULL);
1064 else
1065 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
1067 return 0;
1070 int pwrdm_post_transition(struct powerdomain *pwrdm)
1072 if (pwrdm)
1073 _pwrdm_post_transition_cb(pwrdm, NULL);
1074 else
1075 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
1077 return 0;
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;
1096 int curr_pwrst;
1097 int ret = 0;
1098 bool hwsup = false;
1100 if (!pwrdm || IS_ERR(pwrdm))
1101 return -EINVAL;
1103 while (!(pwrdm->pwrsts & (1 << pwrst))) {
1104 if (pwrst == PWRDM_POWER_OFF)
1105 return ret;
1106 pwrst--;
1109 pwrdm_lock(pwrdm);
1111 curr_pwrst = pwrdm_read_pwrst(pwrdm);
1112 if (curr_pwrst < 0) {
1113 ret = -EINVAL;
1114 goto osps_out;
1117 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
1118 if (curr_pwrst == pwrst && next_pwrst == pwrst)
1119 goto osps_out;
1121 sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
1122 pwrst, &hwsup);
1124 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
1125 if (ret)
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);
1131 osps_out:
1132 pwrdm_unlock(pwrdm);
1134 return ret;
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)
1147 int i, count;
1149 if (!pwrdm) {
1150 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
1151 return -ENODEV;
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.
1164 count &= INT_MAX;
1166 pr_debug("powerdomain: %s: context loss count = %d\n",
1167 pwrdm->name, count);
1169 return 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)
1186 int i;
1188 if (!pwrdm) {
1189 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1190 __func__);
1191 return 1;
1194 if (pwrdm->pwrsts & PWRSTS_OFF)
1195 return 1;
1197 if (pwrdm->pwrsts & PWRSTS_RET) {
1198 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1199 return 1;
1201 for (i = 0; i < pwrdm->banks; i++)
1202 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1203 return 1;
1206 for (i = 0; i < pwrdm->banks; i++)
1207 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1208 return 1;
1210 return 0;