regulator, mc13xxx: Remove pointless test for unsigned less than zero
[zen-stable.git] / arch / arm / mach-omap2 / powerdomain.c
blobeaed0df166999ed2a9ea4f60e259cbca6ccc7b89
1 /*
2 * OMAP powerdomain control
4 * Copyright (C) 2007-2008 Texas Instruments, Inc.
5 * Copyright (C) 2007-2009 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 "cm2xxx_3xxx.h"
23 #include "prcm44xx.h"
24 #include "cm44xx.h"
25 #include "prm2xxx_3xxx.h"
26 #include "prm44xx.h"
28 #include <plat/cpu.h>
29 #include "powerdomain.h"
30 #include "clockdomain.h"
31 #include <plat/prcm.h>
33 #include "pm.h"
35 enum {
36 PWRDM_STATE_NOW = 0,
37 PWRDM_STATE_PREV,
41 /* pwrdm_list contains all registered struct powerdomains */
42 static LIST_HEAD(pwrdm_list);
44 static struct pwrdm_ops *arch_pwrdm;
46 /* Private functions */
48 static struct powerdomain *_pwrdm_lookup(const char *name)
50 struct powerdomain *pwrdm, *temp_pwrdm;
52 pwrdm = NULL;
54 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
55 if (!strcmp(name, temp_pwrdm->name)) {
56 pwrdm = temp_pwrdm;
57 break;
61 return pwrdm;
64 /**
65 * _pwrdm_register - register a powerdomain
66 * @pwrdm: struct powerdomain * to register
68 * Adds a powerdomain to the internal powerdomain list. Returns
69 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
70 * already registered by the provided name, or 0 upon success.
72 static int _pwrdm_register(struct powerdomain *pwrdm)
74 int i;
76 if (!pwrdm || !pwrdm->name)
77 return -EINVAL;
79 if (!omap_chip_is(pwrdm->omap_chip))
80 return -EINVAL;
82 if (cpu_is_omap44xx() &&
83 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
84 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
85 pwrdm->name);
86 return -EINVAL;
89 if (_pwrdm_lookup(pwrdm->name))
90 return -EEXIST;
92 list_add(&pwrdm->node, &pwrdm_list);
94 /* Initialize the powerdomain's state counter */
95 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
96 pwrdm->state_counter[i] = 0;
98 pwrdm->ret_logic_off_counter = 0;
99 for (i = 0; i < pwrdm->banks; i++)
100 pwrdm->ret_mem_off_counter[i] = 0;
102 pwrdm_wait_transition(pwrdm);
103 pwrdm->state = pwrdm_read_pwrst(pwrdm);
104 pwrdm->state_counter[pwrdm->state] = 1;
106 pr_debug("powerdomain: registered %s\n", pwrdm->name);
108 return 0;
111 static void _update_logic_membank_counters(struct powerdomain *pwrdm)
113 int i;
114 u8 prev_logic_pwrst, prev_mem_pwrst;
116 prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
117 if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
118 (prev_logic_pwrst == PWRDM_POWER_OFF))
119 pwrdm->ret_logic_off_counter++;
121 for (i = 0; i < pwrdm->banks; i++) {
122 prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
124 if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
125 (prev_mem_pwrst == PWRDM_POWER_OFF))
126 pwrdm->ret_mem_off_counter[i]++;
130 static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
133 int prev;
134 int state;
136 if (pwrdm == NULL)
137 return -EINVAL;
139 state = pwrdm_read_pwrst(pwrdm);
141 switch (flag) {
142 case PWRDM_STATE_NOW:
143 prev = pwrdm->state;
144 break;
145 case PWRDM_STATE_PREV:
146 prev = pwrdm_read_prev_pwrst(pwrdm);
147 if (pwrdm->state != prev)
148 pwrdm->state_counter[prev]++;
149 if (prev == PWRDM_POWER_RET)
150 _update_logic_membank_counters(pwrdm);
151 break;
152 default:
153 return -EINVAL;
156 if (state != prev)
157 pwrdm->state_counter[state]++;
159 pm_dbg_update_time(pwrdm, prev);
161 pwrdm->state = state;
163 return 0;
166 static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
168 pwrdm_clear_all_prev_pwrst(pwrdm);
169 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
170 return 0;
173 static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
175 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
176 return 0;
179 /* Public functions */
182 * pwrdm_init - set up the powerdomain layer
183 * @pwrdm_list: array of struct powerdomain pointers to register
184 * @custom_funcs: func pointers for arch specfic implementations
186 * Loop through the array of powerdomains @pwrdm_list, registering all
187 * that are available on the current CPU. If pwrdm_list is supplied
188 * and not null, all of the referenced powerdomains will be
189 * registered. No return value. XXX pwrdm_list is not really a
190 * "list"; it is an array. Rename appropriately.
192 void pwrdm_init(struct powerdomain **pwrdm_list, struct pwrdm_ops *custom_funcs)
194 struct powerdomain **p = NULL;
196 if (!custom_funcs)
197 WARN(1, "powerdomain: No custom pwrdm functions registered\n");
198 else
199 arch_pwrdm = custom_funcs;
201 if (pwrdm_list) {
202 for (p = pwrdm_list; *p; p++)
203 _pwrdm_register(*p);
208 * pwrdm_lookup - look up a powerdomain by name, return a pointer
209 * @name: name of powerdomain
211 * Find a registered powerdomain by its name @name. Returns a pointer
212 * to the struct powerdomain if found, or NULL otherwise.
214 struct powerdomain *pwrdm_lookup(const char *name)
216 struct powerdomain *pwrdm;
218 if (!name)
219 return NULL;
221 pwrdm = _pwrdm_lookup(name);
223 return pwrdm;
227 * pwrdm_for_each - call function on each registered clockdomain
228 * @fn: callback function *
230 * Call the supplied function @fn for each registered powerdomain.
231 * The callback function @fn can return anything but 0 to bail out
232 * early from the iterator. Returns the last return value of the
233 * callback function, which should be 0 for success or anything else
234 * to indicate failure; or -EINVAL if the function pointer is null.
236 int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
237 void *user)
239 struct powerdomain *temp_pwrdm;
240 int ret = 0;
242 if (!fn)
243 return -EINVAL;
245 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
246 ret = (*fn)(temp_pwrdm, user);
247 if (ret)
248 break;
251 return ret;
255 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
256 * @pwrdm: struct powerdomain * to add the clockdomain to
257 * @clkdm: struct clockdomain * to associate with a powerdomain
259 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
260 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
261 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
262 * or 0 upon success.
264 int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
266 int i;
267 int ret = -EINVAL;
269 if (!pwrdm || !clkdm)
270 return -EINVAL;
272 pr_debug("powerdomain: associating clockdomain %s with powerdomain "
273 "%s\n", clkdm->name, pwrdm->name);
275 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
276 if (!pwrdm->pwrdm_clkdms[i])
277 break;
278 #ifdef DEBUG
279 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
280 ret = -EINVAL;
281 goto pac_exit;
283 #endif
286 if (i == PWRDM_MAX_CLKDMS) {
287 pr_debug("powerdomain: increase PWRDM_MAX_CLKDMS for "
288 "pwrdm %s clkdm %s\n", pwrdm->name, clkdm->name);
289 WARN_ON(1);
290 ret = -ENOMEM;
291 goto pac_exit;
294 pwrdm->pwrdm_clkdms[i] = clkdm;
296 ret = 0;
298 pac_exit:
299 return ret;
303 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
304 * @pwrdm: struct powerdomain * to add the clockdomain to
305 * @clkdm: struct clockdomain * to associate with a powerdomain
307 * Dissociate the clockdomain @clkdm from the powerdomain
308 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
309 * if @clkdm was not associated with the powerdomain, or 0 upon
310 * success.
312 int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
314 int ret = -EINVAL;
315 int i;
317 if (!pwrdm || !clkdm)
318 return -EINVAL;
320 pr_debug("powerdomain: dissociating clockdomain %s from powerdomain "
321 "%s\n", clkdm->name, pwrdm->name);
323 for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
324 if (pwrdm->pwrdm_clkdms[i] == clkdm)
325 break;
327 if (i == PWRDM_MAX_CLKDMS) {
328 pr_debug("powerdomain: clkdm %s not associated with pwrdm "
329 "%s ?!\n", clkdm->name, pwrdm->name);
330 ret = -ENOENT;
331 goto pdc_exit;
334 pwrdm->pwrdm_clkdms[i] = NULL;
336 ret = 0;
338 pdc_exit:
339 return ret;
343 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
344 * @pwrdm: struct powerdomain * to iterate over
345 * @fn: callback function *
347 * Call the supplied function @fn for each clockdomain in the powerdomain
348 * @pwrdm. The callback function can return anything but 0 to bail
349 * out early from the iterator. Returns -EINVAL if presented with
350 * invalid pointers; or passes along the last return value of the
351 * callback function, which should be 0 for success or anything else
352 * to indicate failure.
354 int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
355 int (*fn)(struct powerdomain *pwrdm,
356 struct clockdomain *clkdm))
358 int ret = 0;
359 int i;
361 if (!fn)
362 return -EINVAL;
364 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
365 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
367 return ret;
371 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
372 * @pwrdm: struct powerdomain *
374 * Return the number of controllable memory banks in powerdomain @pwrdm,
375 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
377 int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
379 if (!pwrdm)
380 return -EINVAL;
382 return pwrdm->banks;
386 * pwrdm_set_next_pwrst - set next powerdomain power state
387 * @pwrdm: struct powerdomain * to set
388 * @pwrst: one of the PWRDM_POWER_* macros
390 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
391 * may not enter this state immediately if the preconditions for this state
392 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
393 * null or if the power state is invalid for the powerdomin, or returns 0
394 * upon success.
396 int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
398 int ret = -EINVAL;
400 if (!pwrdm)
401 return -EINVAL;
403 if (!(pwrdm->pwrsts & (1 << pwrst)))
404 return -EINVAL;
406 pr_debug("powerdomain: setting next powerstate for %s to %0x\n",
407 pwrdm->name, pwrst);
409 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst)
410 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
412 return ret;
416 * pwrdm_read_next_pwrst - get next powerdomain power state
417 * @pwrdm: struct powerdomain * to get power state
419 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
420 * if the powerdomain pointer is null or returns the next power state
421 * upon success.
423 int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
425 int ret = -EINVAL;
427 if (!pwrdm)
428 return -EINVAL;
430 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
431 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
433 return ret;
437 * pwrdm_read_pwrst - get current powerdomain power state
438 * @pwrdm: struct powerdomain * to get power state
440 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
441 * if the powerdomain pointer is null or returns the current power state
442 * upon success.
444 int pwrdm_read_pwrst(struct powerdomain *pwrdm)
446 int ret = -EINVAL;
448 if (!pwrdm)
449 return -EINVAL;
451 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
452 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
454 return ret;
458 * pwrdm_read_prev_pwrst - get previous powerdomain power state
459 * @pwrdm: struct powerdomain * to get previous power state
461 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
462 * if the powerdomain pointer is null or returns the previous power state
463 * upon success.
465 int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
467 int ret = -EINVAL;
469 if (!pwrdm)
470 return -EINVAL;
472 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
473 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
475 return ret;
479 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
480 * @pwrdm: struct powerdomain * to set
481 * @pwrst: one of the PWRDM_POWER_* macros
483 * Set the next power state @pwrst that the logic portion of the
484 * powerdomain @pwrdm will enter when the powerdomain enters retention.
485 * This will be either RETENTION or OFF, if supported. Returns
486 * -EINVAL if the powerdomain pointer is null or the target power
487 * state is not not supported, or returns 0 upon success.
489 int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
491 int ret = -EINVAL;
493 if (!pwrdm)
494 return -EINVAL;
496 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
497 return -EINVAL;
499 pr_debug("powerdomain: setting next logic powerstate for %s to %0x\n",
500 pwrdm->name, pwrst);
502 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
503 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
505 return ret;
509 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
510 * @pwrdm: struct powerdomain * to set
511 * @bank: memory bank number to set (0-3)
512 * @pwrst: one of the PWRDM_POWER_* macros
514 * Set the next power state @pwrst that memory bank @bank of the
515 * powerdomain @pwrdm will enter when the powerdomain enters the ON
516 * state. @bank will be a number from 0 to 3, and represents different
517 * types of memory, depending on the powerdomain. Returns -EINVAL if
518 * the powerdomain pointer is null or the target power state is not
519 * not supported for this memory bank, -EEXIST if the target memory
520 * bank does not exist or is not controllable, or returns 0 upon
521 * success.
523 int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
525 int ret = -EINVAL;
527 if (!pwrdm)
528 return -EINVAL;
530 if (pwrdm->banks < (bank + 1))
531 return -EEXIST;
533 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
534 return -EINVAL;
536 pr_debug("powerdomain: setting next memory powerstate for domain %s "
537 "bank %0x while pwrdm-ON to %0x\n", pwrdm->name, bank, pwrst);
539 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
540 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
542 return ret;
546 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
547 * @pwrdm: struct powerdomain * to set
548 * @bank: memory bank number to set (0-3)
549 * @pwrst: one of the PWRDM_POWER_* macros
551 * Set the next power state @pwrst that memory bank @bank of the
552 * powerdomain @pwrdm will enter when the powerdomain enters the
553 * RETENTION state. Bank will be a number from 0 to 3, and represents
554 * different types of memory, depending on the powerdomain. @pwrst
555 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
556 * the powerdomain pointer is null or the target power state is not
557 * not supported for this memory bank, -EEXIST if the target memory
558 * bank does not exist or is not controllable, or returns 0 upon
559 * success.
561 int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
563 int ret = -EINVAL;
565 if (!pwrdm)
566 return -EINVAL;
568 if (pwrdm->banks < (bank + 1))
569 return -EEXIST;
571 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
572 return -EINVAL;
574 pr_debug("powerdomain: setting next memory powerstate for domain %s "
575 "bank %0x while pwrdm-RET to %0x\n", pwrdm->name, bank, pwrst);
577 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
578 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
580 return ret;
584 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
585 * @pwrdm: struct powerdomain * to get current logic retention power state
587 * Return the power state that the logic portion of powerdomain @pwrdm
588 * will enter when the powerdomain enters retention. Returns -EINVAL
589 * if the powerdomain pointer is null or returns the logic retention
590 * power state upon success.
592 int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
594 int ret = -EINVAL;
596 if (!pwrdm)
597 return -EINVAL;
599 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
600 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
602 return ret;
606 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
607 * @pwrdm: struct powerdomain * to get previous logic power state
609 * Return the powerdomain @pwrdm's previous logic power state. Returns
610 * -EINVAL if the powerdomain pointer is null or returns the previous
611 * logic power state upon success.
613 int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
615 int ret = -EINVAL;
617 if (!pwrdm)
618 return -EINVAL;
620 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
621 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
623 return ret;
627 * pwrdm_read_logic_retst - get next powerdomain logic power state
628 * @pwrdm: struct powerdomain * to get next logic power state
630 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
631 * if the powerdomain pointer is null or returns the next logic
632 * power state upon success.
634 int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
636 int ret = -EINVAL;
638 if (!pwrdm)
639 return -EINVAL;
641 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
642 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
644 return ret;
648 * pwrdm_read_mem_pwrst - get current memory bank power state
649 * @pwrdm: struct powerdomain * to get current memory bank power state
650 * @bank: memory bank number (0-3)
652 * Return the powerdomain @pwrdm's current memory power state for bank
653 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
654 * the target memory bank does not exist or is not controllable, or
655 * returns the current memory power state upon success.
657 int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
659 int ret = -EINVAL;
661 if (!pwrdm)
662 return ret;
664 if (pwrdm->banks < (bank + 1))
665 return ret;
667 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
668 bank = 1;
670 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
671 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
673 return ret;
677 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
678 * @pwrdm: struct powerdomain * to get previous memory bank power state
679 * @bank: memory bank number (0-3)
681 * Return the powerdomain @pwrdm's previous memory power state for
682 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
683 * -EEXIST if the target memory bank does not exist or is not
684 * controllable, or returns the previous memory power state upon
685 * success.
687 int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
689 int ret = -EINVAL;
691 if (!pwrdm)
692 return ret;
694 if (pwrdm->banks < (bank + 1))
695 return ret;
697 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
698 bank = 1;
700 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
701 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
703 return ret;
707 * pwrdm_read_mem_retst - get next memory bank power state
708 * @pwrdm: struct powerdomain * to get mext memory bank power state
709 * @bank: memory bank number (0-3)
711 * Return the powerdomain pwrdm's next memory power state for bank
712 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
713 * the target memory bank does not exist or is not controllable, or
714 * returns the next memory power state upon success.
716 int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
718 int ret = -EINVAL;
720 if (!pwrdm)
721 return ret;
723 if (pwrdm->banks < (bank + 1))
724 return ret;
726 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
727 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
729 return ret;
733 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
734 * @pwrdm: struct powerdomain * to clear
736 * Clear the powerdomain's previous power state register @pwrdm.
737 * Clears the entire register, including logic and memory bank
738 * previous power states. Returns -EINVAL if the powerdomain pointer
739 * is null, or returns 0 upon success.
741 int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
743 int ret = -EINVAL;
745 if (!pwrdm)
746 return ret;
749 * XXX should get the powerdomain's current state here;
750 * warn & fail if it is not ON.
753 pr_debug("powerdomain: clearing previous power state reg for %s\n",
754 pwrdm->name);
756 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
757 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
759 return ret;
763 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
764 * @pwrdm: struct powerdomain *
766 * Enable automatic context save-and-restore upon power state change
767 * for some devices in the powerdomain @pwrdm. Warning: this only
768 * affects a subset of devices in a powerdomain; check the TRM
769 * closely. Returns -EINVAL if the powerdomain pointer is null or if
770 * the powerdomain does not support automatic save-and-restore, or
771 * returns 0 upon success.
773 int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
775 int ret = -EINVAL;
777 if (!pwrdm)
778 return ret;
780 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
781 return ret;
783 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n",
784 pwrdm->name);
786 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
787 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
789 return ret;
793 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
794 * @pwrdm: struct powerdomain *
796 * Disable automatic context save-and-restore upon power state change
797 * for some devices in the powerdomain @pwrdm. Warning: this only
798 * affects a subset of devices in a powerdomain; check the TRM
799 * closely. Returns -EINVAL if the powerdomain pointer is null or if
800 * the powerdomain does not support automatic save-and-restore, or
801 * returns 0 upon success.
803 int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
805 int ret = -EINVAL;
807 if (!pwrdm)
808 return ret;
810 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
811 return ret;
813 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n",
814 pwrdm->name);
816 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
817 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
819 return ret;
823 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
824 * @pwrdm: struct powerdomain *
826 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
827 * for some devices, or 0 if it does not.
829 bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
831 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
835 * pwrdm_set_lowpwrstchange - Request a low power state change
836 * @pwrdm: struct powerdomain *
838 * Allows a powerdomain to transtion to a lower power sleep state
839 * from an existing sleep state without waking up the powerdomain.
840 * Returns -EINVAL if the powerdomain pointer is null or if the
841 * powerdomain does not support LOWPOWERSTATECHANGE, or returns 0
842 * upon success.
844 int pwrdm_set_lowpwrstchange(struct powerdomain *pwrdm)
846 int ret = -EINVAL;
848 if (!pwrdm)
849 return -EINVAL;
851 if (!(pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE))
852 return -EINVAL;
854 pr_debug("powerdomain: %s: setting LOWPOWERSTATECHANGE bit\n",
855 pwrdm->name);
857 if (arch_pwrdm && arch_pwrdm->pwrdm_set_lowpwrstchange)
858 ret = arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
860 return ret;
864 * pwrdm_wait_transition - wait for powerdomain power transition to finish
865 * @pwrdm: struct powerdomain * to wait for
867 * If the powerdomain @pwrdm is in the process of a state transition,
868 * spin until it completes the power transition, or until an iteration
869 * bailout value is reached. Returns -EINVAL if the powerdomain
870 * pointer is null, -EAGAIN if the bailout value was reached, or
871 * returns 0 upon success.
873 int pwrdm_wait_transition(struct powerdomain *pwrdm)
875 int ret = -EINVAL;
877 if (!pwrdm)
878 return -EINVAL;
880 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
881 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
883 return ret;
886 int pwrdm_state_switch(struct powerdomain *pwrdm)
888 return _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
891 int pwrdm_clkdm_state_switch(struct clockdomain *clkdm)
893 if (clkdm != NULL && clkdm->pwrdm.ptr != NULL) {
894 pwrdm_wait_transition(clkdm->pwrdm.ptr);
895 return pwrdm_state_switch(clkdm->pwrdm.ptr);
898 return -EINVAL;
901 int pwrdm_pre_transition(void)
903 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
904 return 0;
907 int pwrdm_post_transition(void)
909 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
910 return 0;
914 * pwrdm_get_context_loss_count - get powerdomain's context loss count
915 * @pwrdm: struct powerdomain * to wait for
917 * Context loss count is the sum of powerdomain off-mode counter, the
918 * logic off counter and the per-bank memory off counter. Returns 0
919 * (and WARNs) upon error, otherwise, returns the context loss count.
921 u32 pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
923 int i, count;
925 if (!pwrdm) {
926 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
927 return 0;
930 count = pwrdm->state_counter[PWRDM_POWER_OFF];
931 count += pwrdm->ret_logic_off_counter;
933 for (i = 0; i < pwrdm->banks; i++)
934 count += pwrdm->ret_mem_off_counter[i];
936 pr_debug("powerdomain: %s: context loss count = %u\n",
937 pwrdm->name, count);
939 return count;