Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / arch / arm / mach-omap2 / powerdomain.c
blobef71fdd40fc47b05072821ede75365606d20ab61
1 /*
2 * OMAP powerdomain control
4 * Copyright (C) 2007-2008 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 <trace/events/power.h>
24 #include "cm2xxx_3xxx.h"
25 #include "prcm44xx.h"
26 #include "cm44xx.h"
27 #include "prm2xxx_3xxx.h"
28 #include "prm44xx.h"
30 #include <asm/cpu.h>
31 #include <plat/cpu.h>
32 #include "powerdomain.h"
33 #include "clockdomain.h"
34 #include <plat/prcm.h>
36 #include "pm.h"
38 #define PWRDM_TRACE_STATES_FLAG (1<<31)
40 enum {
41 PWRDM_STATE_NOW = 0,
42 PWRDM_STATE_PREV,
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;
57 pwrdm = NULL;
59 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
60 if (!strcmp(name, temp_pwrdm->name)) {
61 pwrdm = temp_pwrdm;
62 break;
66 return pwrdm;
69 /**
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)
79 int i;
81 if (!pwrdm || !pwrdm->name)
82 return -EINVAL;
84 if (!omap_chip_is(pwrdm->omap_chip))
85 return -EINVAL;
87 if (cpu_is_omap44xx() &&
88 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
89 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
90 pwrdm->name);
91 return -EINVAL;
94 if (_pwrdm_lookup(pwrdm->name))
95 return -EEXIST;
97 list_add(&pwrdm->node, &pwrdm_list);
99 /* Initialize the powerdomain's state counter */
100 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
101 pwrdm->state_counter[i] = 0;
103 pwrdm->ret_logic_off_counter = 0;
104 for (i = 0; i < pwrdm->banks; i++)
105 pwrdm->ret_mem_off_counter[i] = 0;
107 pwrdm_wait_transition(pwrdm);
108 pwrdm->state = pwrdm_read_pwrst(pwrdm);
109 pwrdm->state_counter[pwrdm->state] = 1;
111 pr_debug("powerdomain: registered %s\n", pwrdm->name);
113 return 0;
116 static void _update_logic_membank_counters(struct powerdomain *pwrdm)
118 int i;
119 u8 prev_logic_pwrst, prev_mem_pwrst;
121 prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
122 if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
123 (prev_logic_pwrst == PWRDM_POWER_OFF))
124 pwrdm->ret_logic_off_counter++;
126 for (i = 0; i < pwrdm->banks; i++) {
127 prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
129 if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
130 (prev_mem_pwrst == PWRDM_POWER_OFF))
131 pwrdm->ret_mem_off_counter[i]++;
135 static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
138 int prev, state, trace_state = 0;
140 if (pwrdm == NULL)
141 return -EINVAL;
143 state = pwrdm_read_pwrst(pwrdm);
145 switch (flag) {
146 case PWRDM_STATE_NOW:
147 prev = pwrdm->state;
148 break;
149 case PWRDM_STATE_PREV:
150 prev = pwrdm_read_prev_pwrst(pwrdm);
151 if (pwrdm->state != prev)
152 pwrdm->state_counter[prev]++;
153 if (prev == PWRDM_POWER_RET)
154 _update_logic_membank_counters(pwrdm);
156 * If the power domain did not hit the desired state,
157 * generate a trace event with both the desired and hit states
159 if (state != prev) {
160 trace_state = (PWRDM_TRACE_STATES_FLAG |
161 ((state & OMAP_POWERSTATE_MASK) << 8) |
162 ((prev & OMAP_POWERSTATE_MASK) << 0));
163 trace_power_domain_target(pwrdm->name, trace_state,
164 smp_processor_id());
166 break;
167 default:
168 return -EINVAL;
171 if (state != prev)
172 pwrdm->state_counter[state]++;
174 pm_dbg_update_time(pwrdm, prev);
176 pwrdm->state = state;
178 return 0;
181 static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
183 pwrdm_clear_all_prev_pwrst(pwrdm);
184 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
185 return 0;
188 static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
190 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
191 return 0;
194 /* Public functions */
197 * pwrdm_init - set up the powerdomain layer
198 * @pwrdms: array of struct powerdomain pointers to register
199 * @custom_funcs: func pointers for arch specific implementations
201 * Loop through the array of powerdomains @pwrdms, registering all
202 * that are available on the current CPU. Also, program all
203 * powerdomain target state as ON; this is to prevent domains from
204 * hitting low power states (if bootloader has target states set to
205 * something other than ON) and potentially even losing context while
206 * PM is not fully initialized. The PM late init code can then program
207 * the desired target state for all the power domains. No return
208 * value.
210 void pwrdm_init(struct powerdomain **pwrdms, struct pwrdm_ops *custom_funcs)
212 struct powerdomain **p = NULL;
213 struct powerdomain *temp_p;
215 if (!custom_funcs)
216 WARN(1, "powerdomain: No custom pwrdm functions registered\n");
217 else
218 arch_pwrdm = custom_funcs;
220 if (pwrdms) {
221 for (p = pwrdms; *p; p++)
222 _pwrdm_register(*p);
225 list_for_each_entry(temp_p, &pwrdm_list, node)
226 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
230 * pwrdm_lookup - look up a powerdomain by name, return a pointer
231 * @name: name of powerdomain
233 * Find a registered powerdomain by its name @name. Returns a pointer
234 * to the struct powerdomain if found, or NULL otherwise.
236 struct powerdomain *pwrdm_lookup(const char *name)
238 struct powerdomain *pwrdm;
240 if (!name)
241 return NULL;
243 pwrdm = _pwrdm_lookup(name);
245 return pwrdm;
249 * pwrdm_for_each - call function on each registered clockdomain
250 * @fn: callback function *
252 * Call the supplied function @fn for each registered powerdomain.
253 * The callback function @fn can return anything but 0 to bail out
254 * early from the iterator. Returns the last return value of the
255 * callback function, which should be 0 for success or anything else
256 * to indicate failure; or -EINVAL if the function pointer is null.
258 int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
259 void *user)
261 struct powerdomain *temp_pwrdm;
262 int ret = 0;
264 if (!fn)
265 return -EINVAL;
267 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
268 ret = (*fn)(temp_pwrdm, user);
269 if (ret)
270 break;
273 return ret;
277 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
278 * @pwrdm: struct powerdomain * to add the clockdomain to
279 * @clkdm: struct clockdomain * to associate with a powerdomain
281 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
282 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
283 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
284 * or 0 upon success.
286 int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
288 int i;
289 int ret = -EINVAL;
291 if (!pwrdm || !clkdm)
292 return -EINVAL;
294 pr_debug("powerdomain: associating clockdomain %s with powerdomain "
295 "%s\n", clkdm->name, pwrdm->name);
297 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
298 if (!pwrdm->pwrdm_clkdms[i])
299 break;
300 #ifdef DEBUG
301 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
302 ret = -EINVAL;
303 goto pac_exit;
305 #endif
308 if (i == PWRDM_MAX_CLKDMS) {
309 pr_debug("powerdomain: increase PWRDM_MAX_CLKDMS for "
310 "pwrdm %s clkdm %s\n", pwrdm->name, clkdm->name);
311 WARN_ON(1);
312 ret = -ENOMEM;
313 goto pac_exit;
316 pwrdm->pwrdm_clkdms[i] = clkdm;
318 ret = 0;
320 pac_exit:
321 return ret;
325 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
326 * @pwrdm: struct powerdomain * to add the clockdomain to
327 * @clkdm: struct clockdomain * to associate with a powerdomain
329 * Dissociate the clockdomain @clkdm from the powerdomain
330 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
331 * if @clkdm was not associated with the powerdomain, or 0 upon
332 * success.
334 int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
336 int ret = -EINVAL;
337 int i;
339 if (!pwrdm || !clkdm)
340 return -EINVAL;
342 pr_debug("powerdomain: dissociating clockdomain %s from powerdomain "
343 "%s\n", clkdm->name, pwrdm->name);
345 for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
346 if (pwrdm->pwrdm_clkdms[i] == clkdm)
347 break;
349 if (i == PWRDM_MAX_CLKDMS) {
350 pr_debug("powerdomain: clkdm %s not associated with pwrdm "
351 "%s ?!\n", clkdm->name, pwrdm->name);
352 ret = -ENOENT;
353 goto pdc_exit;
356 pwrdm->pwrdm_clkdms[i] = NULL;
358 ret = 0;
360 pdc_exit:
361 return ret;
365 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
366 * @pwrdm: struct powerdomain * to iterate over
367 * @fn: callback function *
369 * Call the supplied function @fn for each clockdomain in the powerdomain
370 * @pwrdm. The callback function can return anything but 0 to bail
371 * out early from the iterator. Returns -EINVAL if presented with
372 * invalid pointers; or passes along the last return value of the
373 * callback function, which should be 0 for success or anything else
374 * to indicate failure.
376 int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
377 int (*fn)(struct powerdomain *pwrdm,
378 struct clockdomain *clkdm))
380 int ret = 0;
381 int i;
383 if (!fn)
384 return -EINVAL;
386 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
387 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
389 return ret;
393 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
394 * @pwrdm: struct powerdomain *
396 * Return the number of controllable memory banks in powerdomain @pwrdm,
397 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
399 int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
401 if (!pwrdm)
402 return -EINVAL;
404 return pwrdm->banks;
408 * pwrdm_set_next_pwrst - set next powerdomain power state
409 * @pwrdm: struct powerdomain * to set
410 * @pwrst: one of the PWRDM_POWER_* macros
412 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
413 * may not enter this state immediately if the preconditions for this state
414 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
415 * null or if the power state is invalid for the powerdomin, or returns 0
416 * upon success.
418 int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
420 int ret = -EINVAL;
422 if (!pwrdm)
423 return -EINVAL;
425 if (!(pwrdm->pwrsts & (1 << pwrst)))
426 return -EINVAL;
428 pr_debug("powerdomain: setting next powerstate for %s to %0x\n",
429 pwrdm->name, pwrst);
431 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
432 /* Trace the pwrdm desired target state */
433 trace_power_domain_target(pwrdm->name, pwrst,
434 smp_processor_id());
435 /* Program the pwrdm desired target state */
436 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
439 return ret;
443 * pwrdm_read_next_pwrst - get next powerdomain power state
444 * @pwrdm: struct powerdomain * to get power state
446 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
447 * if the powerdomain pointer is null or returns the next power state
448 * upon success.
450 int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
452 int ret = -EINVAL;
454 if (!pwrdm)
455 return -EINVAL;
457 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
458 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
460 return ret;
464 * pwrdm_read_pwrst - get current powerdomain power state
465 * @pwrdm: struct powerdomain * to get power state
467 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
468 * if the powerdomain pointer is null or returns the current power state
469 * upon success.
471 int pwrdm_read_pwrst(struct powerdomain *pwrdm)
473 int ret = -EINVAL;
475 if (!pwrdm)
476 return -EINVAL;
478 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
479 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
481 return ret;
485 * pwrdm_read_prev_pwrst - get previous powerdomain power state
486 * @pwrdm: struct powerdomain * to get previous power state
488 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
489 * if the powerdomain pointer is null or returns the previous power state
490 * upon success.
492 int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
494 int ret = -EINVAL;
496 if (!pwrdm)
497 return -EINVAL;
499 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
500 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
502 return ret;
506 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
507 * @pwrdm: struct powerdomain * to set
508 * @pwrst: one of the PWRDM_POWER_* macros
510 * Set the next power state @pwrst that the logic portion of the
511 * powerdomain @pwrdm will enter when the powerdomain enters retention.
512 * This will be either RETENTION or OFF, if supported. Returns
513 * -EINVAL if the powerdomain pointer is null or the target power
514 * state is not not supported, or returns 0 upon success.
516 int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
518 int ret = -EINVAL;
520 if (!pwrdm)
521 return -EINVAL;
523 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
524 return -EINVAL;
526 pr_debug("powerdomain: setting next logic powerstate for %s to %0x\n",
527 pwrdm->name, pwrst);
529 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
530 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
532 return ret;
536 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
537 * @pwrdm: struct powerdomain * to set
538 * @bank: memory bank number to set (0-3)
539 * @pwrst: one of the PWRDM_POWER_* macros
541 * Set the next power state @pwrst that memory bank @bank of the
542 * powerdomain @pwrdm will enter when the powerdomain enters the ON
543 * state. @bank will be a number from 0 to 3, and represents different
544 * types of memory, depending on the powerdomain. Returns -EINVAL if
545 * the powerdomain pointer is null or the target power state is not
546 * not supported for this memory bank, -EEXIST if the target memory
547 * bank does not exist or is not controllable, or returns 0 upon
548 * success.
550 int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
552 int ret = -EINVAL;
554 if (!pwrdm)
555 return -EINVAL;
557 if (pwrdm->banks < (bank + 1))
558 return -EEXIST;
560 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
561 return -EINVAL;
563 pr_debug("powerdomain: setting next memory powerstate for domain %s "
564 "bank %0x while pwrdm-ON to %0x\n", pwrdm->name, bank, pwrst);
566 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
567 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
569 return ret;
573 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
574 * @pwrdm: struct powerdomain * to set
575 * @bank: memory bank number to set (0-3)
576 * @pwrst: one of the PWRDM_POWER_* macros
578 * Set the next power state @pwrst that memory bank @bank of the
579 * powerdomain @pwrdm will enter when the powerdomain enters the
580 * RETENTION state. Bank will be a number from 0 to 3, and represents
581 * different types of memory, depending on the powerdomain. @pwrst
582 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
583 * the powerdomain pointer is null or the target power state is not
584 * not supported for this memory bank, -EEXIST if the target memory
585 * bank does not exist or is not controllable, or returns 0 upon
586 * success.
588 int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
590 int ret = -EINVAL;
592 if (!pwrdm)
593 return -EINVAL;
595 if (pwrdm->banks < (bank + 1))
596 return -EEXIST;
598 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
599 return -EINVAL;
601 pr_debug("powerdomain: setting next memory powerstate for domain %s "
602 "bank %0x while pwrdm-RET to %0x\n", pwrdm->name, bank, pwrst);
604 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
605 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
607 return ret;
611 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
612 * @pwrdm: struct powerdomain * to get current logic retention power state
614 * Return the power state that the logic portion of powerdomain @pwrdm
615 * will enter when the powerdomain enters retention. Returns -EINVAL
616 * if the powerdomain pointer is null or returns the logic retention
617 * power state upon success.
619 int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
621 int ret = -EINVAL;
623 if (!pwrdm)
624 return -EINVAL;
626 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
627 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
629 return ret;
633 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
634 * @pwrdm: struct powerdomain * to get previous logic power state
636 * Return the powerdomain @pwrdm's previous logic power state. Returns
637 * -EINVAL if the powerdomain pointer is null or returns the previous
638 * logic power state upon success.
640 int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
642 int ret = -EINVAL;
644 if (!pwrdm)
645 return -EINVAL;
647 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
648 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
650 return ret;
654 * pwrdm_read_logic_retst - get next powerdomain logic power state
655 * @pwrdm: struct powerdomain * to get next logic power state
657 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
658 * if the powerdomain pointer is null or returns the next logic
659 * power state upon success.
661 int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
663 int ret = -EINVAL;
665 if (!pwrdm)
666 return -EINVAL;
668 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
669 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
671 return ret;
675 * pwrdm_read_mem_pwrst - get current memory bank power state
676 * @pwrdm: struct powerdomain * to get current memory bank power state
677 * @bank: memory bank number (0-3)
679 * Return the powerdomain @pwrdm's current memory power state for bank
680 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
681 * the target memory bank does not exist or is not controllable, or
682 * returns the current memory power state upon success.
684 int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
686 int ret = -EINVAL;
688 if (!pwrdm)
689 return ret;
691 if (pwrdm->banks < (bank + 1))
692 return ret;
694 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
695 bank = 1;
697 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
698 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
700 return ret;
704 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
705 * @pwrdm: struct powerdomain * to get previous memory bank power state
706 * @bank: memory bank number (0-3)
708 * Return the powerdomain @pwrdm's previous memory power state for
709 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
710 * -EEXIST if the target memory bank does not exist or is not
711 * controllable, or returns the previous memory power state upon
712 * success.
714 int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
716 int ret = -EINVAL;
718 if (!pwrdm)
719 return ret;
721 if (pwrdm->banks < (bank + 1))
722 return ret;
724 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
725 bank = 1;
727 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
728 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
730 return ret;
734 * pwrdm_read_mem_retst - get next memory bank power state
735 * @pwrdm: struct powerdomain * to get mext memory bank power state
736 * @bank: memory bank number (0-3)
738 * Return the powerdomain pwrdm's next memory power state for bank
739 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
740 * the target memory bank does not exist or is not controllable, or
741 * returns the next memory power state upon success.
743 int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
745 int ret = -EINVAL;
747 if (!pwrdm)
748 return ret;
750 if (pwrdm->banks < (bank + 1))
751 return ret;
753 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
754 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
756 return ret;
760 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
761 * @pwrdm: struct powerdomain * to clear
763 * Clear the powerdomain's previous power state register @pwrdm.
764 * Clears the entire register, including logic and memory bank
765 * previous power states. Returns -EINVAL if the powerdomain pointer
766 * is null, or returns 0 upon success.
768 int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
770 int ret = -EINVAL;
772 if (!pwrdm)
773 return ret;
776 * XXX should get the powerdomain's current state here;
777 * warn & fail if it is not ON.
780 pr_debug("powerdomain: clearing previous power state reg for %s\n",
781 pwrdm->name);
783 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
784 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
786 return ret;
790 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
791 * @pwrdm: struct powerdomain *
793 * Enable automatic context save-and-restore upon power state change
794 * for some devices in the powerdomain @pwrdm. Warning: this only
795 * affects a subset of devices in a powerdomain; check the TRM
796 * closely. Returns -EINVAL if the powerdomain pointer is null or if
797 * the powerdomain does not support automatic save-and-restore, or
798 * returns 0 upon success.
800 int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
802 int ret = -EINVAL;
804 if (!pwrdm)
805 return ret;
807 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
808 return ret;
810 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n",
811 pwrdm->name);
813 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
814 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
816 return ret;
820 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
821 * @pwrdm: struct powerdomain *
823 * Disable automatic context save-and-restore upon power state change
824 * for some devices in the powerdomain @pwrdm. Warning: this only
825 * affects a subset of devices in a powerdomain; check the TRM
826 * closely. Returns -EINVAL if the powerdomain pointer is null or if
827 * the powerdomain does not support automatic save-and-restore, or
828 * returns 0 upon success.
830 int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
832 int ret = -EINVAL;
834 if (!pwrdm)
835 return ret;
837 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
838 return ret;
840 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n",
841 pwrdm->name);
843 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
844 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
846 return ret;
850 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
851 * @pwrdm: struct powerdomain *
853 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
854 * for some devices, or 0 if it does not.
856 bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
858 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
862 * pwrdm_set_lowpwrstchange - Request a low power state change
863 * @pwrdm: struct powerdomain *
865 * Allows a powerdomain to transtion to a lower power sleep state
866 * from an existing sleep state without waking up the powerdomain.
867 * Returns -EINVAL if the powerdomain pointer is null or if the
868 * powerdomain does not support LOWPOWERSTATECHANGE, or returns 0
869 * upon success.
871 int pwrdm_set_lowpwrstchange(struct powerdomain *pwrdm)
873 int ret = -EINVAL;
875 if (!pwrdm)
876 return -EINVAL;
878 if (!(pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE))
879 return -EINVAL;
881 pr_debug("powerdomain: %s: setting LOWPOWERSTATECHANGE bit\n",
882 pwrdm->name);
884 if (arch_pwrdm && arch_pwrdm->pwrdm_set_lowpwrstchange)
885 ret = arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
887 return ret;
891 * pwrdm_wait_transition - wait for powerdomain power transition to finish
892 * @pwrdm: struct powerdomain * to wait for
894 * If the powerdomain @pwrdm is in the process of a state transition,
895 * spin until it completes the power transition, or until an iteration
896 * bailout value is reached. Returns -EINVAL if the powerdomain
897 * pointer is null, -EAGAIN if the bailout value was reached, or
898 * returns 0 upon success.
900 int pwrdm_wait_transition(struct powerdomain *pwrdm)
902 int ret = -EINVAL;
904 if (!pwrdm)
905 return -EINVAL;
907 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
908 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
910 return ret;
913 int pwrdm_state_switch(struct powerdomain *pwrdm)
915 return _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
918 int pwrdm_clkdm_state_switch(struct clockdomain *clkdm)
920 if (clkdm != NULL && clkdm->pwrdm.ptr != NULL) {
921 pwrdm_wait_transition(clkdm->pwrdm.ptr);
922 return pwrdm_state_switch(clkdm->pwrdm.ptr);
925 return -EINVAL;
928 int pwrdm_pre_transition(void)
930 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
931 return 0;
934 int pwrdm_post_transition(void)
936 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
937 return 0;
941 * pwrdm_get_context_loss_count - get powerdomain's context loss count
942 * @pwrdm: struct powerdomain * to wait for
944 * Context loss count is the sum of powerdomain off-mode counter, the
945 * logic off counter and the per-bank memory off counter. Returns 0
946 * (and WARNs) upon error, otherwise, returns the context loss count.
948 u32 pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
950 int i, count;
952 if (!pwrdm) {
953 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
954 return 0;
957 count = pwrdm->state_counter[PWRDM_POWER_OFF];
958 count += pwrdm->ret_logic_off_counter;
960 for (i = 0; i < pwrdm->banks; i++)
961 count += pwrdm->ret_mem_off_counter[i];
963 pr_debug("powerdomain: %s: context loss count = %u\n",
964 pwrdm->name, count);
966 return count;
970 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
971 * @pwrdm: struct powerdomain *
973 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
974 * can lose either memory or logic context or if @pwrdm is invalid, or
975 * returns 0 otherwise. This function is not concerned with how the
976 * powerdomain registers are programmed (i.e., to go off or not); it's
977 * concerned with whether it's ever possible for this powerdomain to
978 * go off while some other part of the chip is active. This function
979 * assumes that every powerdomain can go to either ON or INACTIVE.
981 bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
983 int i;
985 if (IS_ERR_OR_NULL(pwrdm)) {
986 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
987 __func__);
988 return 1;
991 if (pwrdm->pwrsts & PWRSTS_OFF)
992 return 1;
994 if (pwrdm->pwrsts & PWRSTS_RET) {
995 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
996 return 1;
998 for (i = 0; i < pwrdm->banks; i++)
999 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1000 return 1;
1003 for (i = 0; i < pwrdm->banks; i++)
1004 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1005 return 1;
1007 return 0;