OMAP3: PM: introduce a new powerdomain walk helper
[linux-ginger.git] / arch / arm / mach-omap2 / powerdomain.c
blobf00289abd30f6f51d015f6f3cf932d77eb868ffc
1 /*
2 * OMAP powerdomain control
4 * Copyright (C) 2007-2008 Texas Instruments, Inc.
5 * Copyright (C) 2007-2008 Nokia Corporation
7 * Written by Paul Walmsley
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #ifdef CONFIG_OMAP_DEBUG_POWERDOMAIN
14 # define DEBUG
15 #endif
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/delay.h>
21 #include <linux/spinlock.h>
22 #include <linux/list.h>
23 #include <linux/errno.h>
24 #include <linux/err.h>
25 #include <linux/io.h>
27 #include <asm/atomic.h>
29 #include "cm.h"
30 #include "cm-regbits-34xx.h"
31 #include "prm.h"
32 #include "prm-regbits-34xx.h"
34 #include <mach/cpu.h>
35 #include <mach/powerdomain.h>
36 #include <mach/clockdomain.h>
38 #include "pm.h"
40 enum {
41 PWRDM_STATE_NOW = 0,
42 PWRDM_STATE_PREV,
45 /* pwrdm_list contains all registered struct powerdomains */
46 static LIST_HEAD(pwrdm_list);
49 * pwrdm_rwlock protects pwrdm_list add and del ops - also reused to
50 * protect pwrdm_clkdms[] during clkdm add/del ops
52 static DEFINE_RWLOCK(pwrdm_rwlock);
55 /* Private functions */
57 static u32 prm_read_mod_bits_shift(s16 domain, s16 idx, u32 mask)
59 u32 v;
61 v = prm_read_mod_reg(domain, idx);
62 v &= mask;
63 v >>= __ffs(mask);
65 return v;
68 static struct powerdomain *_pwrdm_lookup(const char *name)
70 struct powerdomain *pwrdm, *temp_pwrdm;
72 pwrdm = NULL;
74 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
75 if (!strcmp(name, temp_pwrdm->name)) {
76 pwrdm = temp_pwrdm;
77 break;
81 return pwrdm;
84 /* _pwrdm_deps_lookup - look up the specified powerdomain in a pwrdm list */
85 static struct powerdomain *_pwrdm_deps_lookup(struct powerdomain *pwrdm,
86 struct pwrdm_dep *deps)
88 struct pwrdm_dep *pd;
90 if (!pwrdm || !deps || !omap_chip_is(pwrdm->omap_chip))
91 return ERR_PTR(-EINVAL);
93 for (pd = deps; pd->pwrdm_name; pd++) {
95 if (!omap_chip_is(pd->omap_chip))
96 continue;
98 if (!pd->pwrdm && pd->pwrdm_name)
99 pd->pwrdm = pwrdm_lookup(pd->pwrdm_name);
101 if (pd->pwrdm == pwrdm)
102 break;
106 if (!pd->pwrdm_name)
107 return ERR_PTR(-ENOENT);
109 return pd->pwrdm;
112 static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
115 int prev;
116 int state;
118 if (pwrdm == NULL)
119 return -EINVAL;
121 state = pwrdm_read_pwrst(pwrdm);
123 switch (flag) {
124 case PWRDM_STATE_NOW:
125 prev = pwrdm->state;
126 break;
127 case PWRDM_STATE_PREV:
128 prev = pwrdm_read_prev_pwrst(pwrdm);
129 if (pwrdm->state != prev)
130 pwrdm->state_counter[prev]++;
131 break;
132 default:
133 return -EINVAL;
136 if (state != prev)
137 pwrdm->state_counter[state]++;
139 pm_dbg_update_time(pwrdm, prev);
141 pwrdm->state = state;
143 return 0;
146 static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
148 pwrdm_clear_all_prev_pwrst(pwrdm);
149 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
150 return 0;
153 static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
155 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
156 return 0;
159 static __init void _pwrdm_setup(struct powerdomain *pwrdm)
161 int i;
163 for (i = 0; i < 4; i++)
164 pwrdm->state_counter[i] = 0;
166 pwrdm_wait_transition(pwrdm);
167 pwrdm->state = pwrdm_read_pwrst(pwrdm);
168 pwrdm->state_counter[pwrdm->state] = 1;
172 /* Public functions */
175 * pwrdm_init - set up the powerdomain layer
177 * Loop through the list of powerdomains, registering all that are
178 * available on the current CPU. If pwrdm_list is supplied and not
179 * null, all of the referenced powerdomains will be registered. No
180 * return value.
182 void pwrdm_init(struct powerdomain **pwrdm_list)
184 struct powerdomain **p = NULL;
186 if (pwrdm_list) {
187 for (p = pwrdm_list; *p; p++) {
188 pwrdm_register(*p);
189 _pwrdm_setup(*p);
195 * pwrdm_register - register a powerdomain
196 * @pwrdm: struct powerdomain * to register
198 * Adds a powerdomain to the internal powerdomain list. Returns
199 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
200 * already registered by the provided name, or 0 upon success.
202 int pwrdm_register(struct powerdomain *pwrdm)
204 unsigned long flags;
205 int ret = -EINVAL;
207 if (!pwrdm)
208 return -EINVAL;
210 if (!omap_chip_is(pwrdm->omap_chip))
211 return -EINVAL;
213 write_lock_irqsave(&pwrdm_rwlock, flags);
214 if (_pwrdm_lookup(pwrdm->name)) {
215 ret = -EEXIST;
216 goto pr_unlock;
219 list_add(&pwrdm->node, &pwrdm_list);
221 pr_debug("powerdomain: registered %s\n", pwrdm->name);
222 ret = 0;
224 pr_unlock:
225 write_unlock_irqrestore(&pwrdm_rwlock, flags);
227 return ret;
231 * pwrdm_unregister - unregister a powerdomain
232 * @pwrdm: struct powerdomain * to unregister
234 * Removes a powerdomain from the internal powerdomain list. Returns
235 * -EINVAL if pwrdm argument is NULL.
237 int pwrdm_unregister(struct powerdomain *pwrdm)
239 unsigned long flags;
241 if (!pwrdm)
242 return -EINVAL;
244 write_lock_irqsave(&pwrdm_rwlock, flags);
245 list_del(&pwrdm->node);
246 write_unlock_irqrestore(&pwrdm_rwlock, flags);
248 pr_debug("powerdomain: unregistered %s\n", pwrdm->name);
250 return 0;
254 * pwrdm_lookup - look up a powerdomain by name, return a pointer
255 * @name: name of powerdomain
257 * Find a registered powerdomain by its name. Returns a pointer to the
258 * struct powerdomain if found, or NULL otherwise.
260 struct powerdomain *pwrdm_lookup(const char *name)
262 struct powerdomain *pwrdm;
263 unsigned long flags;
265 if (!name)
266 return NULL;
268 read_lock_irqsave(&pwrdm_rwlock, flags);
269 pwrdm = _pwrdm_lookup(name);
270 read_unlock_irqrestore(&pwrdm_rwlock, flags);
272 return pwrdm;
276 * pwrdm_for_each_nolock - call function on each registered clockdomain
277 * @fn: callback function *
279 * Call the supplied function for each registered powerdomain. The
280 * callback function can return anything but 0 to bail out early from
281 * the iterator. Returns the last return value of the callback function, which
282 * should be 0 for success or anything else to indicate failure; or -EINVAL if
283 * the function pointer is null.
285 int pwrdm_for_each_nolock(int (*fn)(struct powerdomain *pwrdm, void *user),
286 void *user)
288 struct powerdomain *temp_pwrdm;
289 int ret = 0;
291 if (!fn)
292 return -EINVAL;
294 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
295 ret = (*fn)(temp_pwrdm, user);
296 if (ret)
297 break;
300 return ret;
304 * pwrdm_for_each - call function on each registered clockdomain
305 * @fn: callback function *
307 * This function is the same as 'pwrdm_for_each_nolock()', but keeps the
308 * &pwrdm_rwlock locked for reading, so no powerdomain structure manipulation
309 * functions should be called from the callback, although hardware powerdomain
310 * control functions are fine.
312 int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
313 void *user)
315 unsigned long flags;
316 int ret;
318 read_lock_irqsave(&pwrdm_rwlock, flags);
319 ret = pwrdm_for_each_nolock(fn, user);
320 read_unlock_irqrestore(&pwrdm_rwlock, flags);
322 return ret;
326 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
327 * @pwrdm: struct powerdomain * to add the clockdomain to
328 * @clkdm: struct clockdomain * to associate with a powerdomain
330 * Associate the clockdomain 'clkdm' with a powerdomain 'pwrdm'. This
331 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
332 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
333 * or 0 upon success.
335 int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
337 unsigned long flags;
338 int i;
339 int ret = -EINVAL;
341 if (!pwrdm || !clkdm)
342 return -EINVAL;
344 pr_debug("powerdomain: associating clockdomain %s with powerdomain "
345 "%s\n", clkdm->name, pwrdm->name);
347 write_lock_irqsave(&pwrdm_rwlock, flags);
349 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
350 if (!pwrdm->pwrdm_clkdms[i])
351 break;
352 #ifdef DEBUG
353 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
354 ret = -EINVAL;
355 goto pac_exit;
357 #endif
360 if (i == PWRDM_MAX_CLKDMS) {
361 pr_debug("powerdomain: increase PWRDM_MAX_CLKDMS for "
362 "pwrdm %s clkdm %s\n", pwrdm->name, clkdm->name);
363 WARN_ON(1);
364 ret = -ENOMEM;
365 goto pac_exit;
368 pwrdm->pwrdm_clkdms[i] = clkdm;
370 ret = 0;
372 pac_exit:
373 write_unlock_irqrestore(&pwrdm_rwlock, flags);
375 return ret;
379 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
380 * @pwrdm: struct powerdomain * to add the clockdomain to
381 * @clkdm: struct clockdomain * to associate with a powerdomain
383 * Dissociate the clockdomain 'clkdm' from the powerdomain
384 * 'pwrdm'. Returns -EINVAL if presented with invalid pointers;
385 * -ENOENT if the clkdm was not associated with the powerdomain, or 0
386 * upon success.
388 int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
390 unsigned long flags;
391 int ret = -EINVAL;
392 int i;
394 if (!pwrdm || !clkdm)
395 return -EINVAL;
397 pr_debug("powerdomain: dissociating clockdomain %s from powerdomain "
398 "%s\n", clkdm->name, pwrdm->name);
400 write_lock_irqsave(&pwrdm_rwlock, flags);
402 for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
403 if (pwrdm->pwrdm_clkdms[i] == clkdm)
404 break;
406 if (i == PWRDM_MAX_CLKDMS) {
407 pr_debug("powerdomain: clkdm %s not associated with pwrdm "
408 "%s ?!\n", clkdm->name, pwrdm->name);
409 ret = -ENOENT;
410 goto pdc_exit;
413 pwrdm->pwrdm_clkdms[i] = NULL;
415 ret = 0;
417 pdc_exit:
418 write_unlock_irqrestore(&pwrdm_rwlock, flags);
420 return ret;
424 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
425 * @pwrdm: struct powerdomain * to iterate over
426 * @fn: callback function *
428 * Call the supplied function for each clockdomain in the powerdomain
429 * 'pwrdm'. The callback function can return anything but 0 to bail
430 * out early from the iterator. The callback function is called with
431 * the pwrdm_rwlock held for reading, so no powerdomain structure
432 * manipulation functions should be called from the callback, although
433 * hardware powerdomain control functions are fine. Returns -EINVAL
434 * if presented with invalid pointers; or passes along the last return
435 * value of the callback function, which should be 0 for success or
436 * anything else to indicate failure.
438 int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
439 int (*fn)(struct powerdomain *pwrdm,
440 struct clockdomain *clkdm))
442 unsigned long flags;
443 int ret = 0;
444 int i;
446 if (!fn)
447 return -EINVAL;
449 read_lock_irqsave(&pwrdm_rwlock, flags);
451 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
452 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
454 read_unlock_irqrestore(&pwrdm_rwlock, flags);
456 return ret;
461 * pwrdm_add_wkdep - add a wakeup dependency from pwrdm2 to pwrdm1
462 * @pwrdm1: wake this struct powerdomain * up (dependent)
463 * @pwrdm2: when this struct powerdomain * wakes up (source)
465 * When the powerdomain represented by pwrdm2 wakes up (due to an
466 * interrupt), wake up pwrdm1. Implemented in hardware on the OMAP,
467 * this feature is designed to reduce wakeup latency of the dependent
468 * powerdomain. Returns -EINVAL if presented with invalid powerdomain
469 * pointers, -ENOENT if pwrdm2 cannot wake up pwrdm1 in hardware, or
470 * 0 upon success.
472 int pwrdm_add_wkdep(struct powerdomain *pwrdm1, struct powerdomain *pwrdm2)
474 struct powerdomain *p;
476 if (!pwrdm1)
477 return -EINVAL;
479 p = _pwrdm_deps_lookup(pwrdm2, pwrdm1->wkdep_srcs);
480 if (IS_ERR(p)) {
481 pr_debug("powerdomain: hardware cannot set/clear wake up of "
482 "%s when %s wakes up\n", pwrdm1->name, pwrdm2->name);
483 return IS_ERR(p);
486 pr_debug("powerdomain: hardware will wake up %s when %s wakes up\n",
487 pwrdm1->name, pwrdm2->name);
489 prm_set_mod_reg_bits((1 << pwrdm2->dep_bit),
490 pwrdm1->prcm_offs, PM_WKDEP);
492 return 0;
496 * pwrdm_del_wkdep - remove a wakeup dependency from pwrdm2 to pwrdm1
497 * @pwrdm1: wake this struct powerdomain * up (dependent)
498 * @pwrdm2: when this struct powerdomain * wakes up (source)
500 * Remove a wakeup dependency that causes pwrdm1 to wake up when pwrdm2
501 * wakes up. Returns -EINVAL if presented with invalid powerdomain
502 * pointers, -ENOENT if pwrdm2 cannot wake up pwrdm1 in hardware, or
503 * 0 upon success.
505 int pwrdm_del_wkdep(struct powerdomain *pwrdm1, struct powerdomain *pwrdm2)
507 struct powerdomain *p;
509 if (!pwrdm1)
510 return -EINVAL;
512 p = _pwrdm_deps_lookup(pwrdm2, pwrdm1->wkdep_srcs);
513 if (IS_ERR(p)) {
514 pr_debug("powerdomain: hardware cannot set/clear wake up of "
515 "%s when %s wakes up\n", pwrdm1->name, pwrdm2->name);
516 return IS_ERR(p);
519 pr_debug("powerdomain: hardware will no longer wake up %s after %s "
520 "wakes up\n", pwrdm1->name, pwrdm2->name);
522 prm_clear_mod_reg_bits((1 << pwrdm2->dep_bit),
523 pwrdm1->prcm_offs, PM_WKDEP);
525 return 0;
529 * pwrdm_read_wkdep - read wakeup dependency state from pwrdm2 to pwrdm1
530 * @pwrdm1: wake this struct powerdomain * up (dependent)
531 * @pwrdm2: when this struct powerdomain * wakes up (source)
533 * Return 1 if a hardware wakeup dependency exists wherein pwrdm1 will be
534 * awoken when pwrdm2 wakes up; 0 if dependency is not set; -EINVAL
535 * if either powerdomain pointer is invalid; or -ENOENT if the hardware
536 * is incapable.
538 * REVISIT: Currently this function only represents software-controllable
539 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
540 * yet handled here.
542 int pwrdm_read_wkdep(struct powerdomain *pwrdm1, struct powerdomain *pwrdm2)
544 struct powerdomain *p;
546 if (!pwrdm1)
547 return -EINVAL;
549 p = _pwrdm_deps_lookup(pwrdm2, pwrdm1->wkdep_srcs);
550 if (IS_ERR(p)) {
551 pr_debug("powerdomain: hardware cannot set/clear wake up of "
552 "%s when %s wakes up\n", pwrdm1->name, pwrdm2->name);
553 return IS_ERR(p);
556 return prm_read_mod_bits_shift(pwrdm1->prcm_offs, PM_WKDEP,
557 (1 << pwrdm2->dep_bit));
561 * pwrdm_add_sleepdep - add a sleep dependency from pwrdm2 to pwrdm1
562 * @pwrdm1: prevent this struct powerdomain * from sleeping (dependent)
563 * @pwrdm2: when this struct powerdomain * is active (source)
565 * Prevent pwrdm1 from automatically going inactive (and then to
566 * retention or off) if pwrdm2 is still active. Returns -EINVAL if
567 * presented with invalid powerdomain pointers or called on a machine
568 * that does not support software-configurable hardware sleep dependencies,
569 * -ENOENT if the specified dependency cannot be set in hardware, or
570 * 0 upon success.
572 int pwrdm_add_sleepdep(struct powerdomain *pwrdm1, struct powerdomain *pwrdm2)
574 struct powerdomain *p;
576 if (!pwrdm1)
577 return -EINVAL;
579 if (!cpu_is_omap34xx())
580 return -EINVAL;
582 p = _pwrdm_deps_lookup(pwrdm2, pwrdm1->sleepdep_srcs);
583 if (IS_ERR(p)) {
584 pr_debug("powerdomain: hardware cannot set/clear sleep "
585 "dependency affecting %s from %s\n", pwrdm1->name,
586 pwrdm2->name);
587 return IS_ERR(p);
590 pr_debug("powerdomain: will prevent %s from sleeping if %s is active\n",
591 pwrdm1->name, pwrdm2->name);
593 cm_set_mod_reg_bits((1 << pwrdm2->dep_bit),
594 pwrdm1->prcm_offs, OMAP3430_CM_SLEEPDEP);
596 return 0;
600 * pwrdm_del_sleepdep - remove a sleep dependency from pwrdm2 to pwrdm1
601 * @pwrdm1: prevent this struct powerdomain * from sleeping (dependent)
602 * @pwrdm2: when this struct powerdomain * is active (source)
604 * Allow pwrdm1 to automatically go inactive (and then to retention or
605 * off), independent of the activity state of pwrdm2. Returns -EINVAL
606 * if presented with invalid powerdomain pointers or called on a machine
607 * that does not support software-configurable hardware sleep dependencies,
608 * -ENOENT if the specified dependency cannot be cleared in hardware, or
609 * 0 upon success.
611 int pwrdm_del_sleepdep(struct powerdomain *pwrdm1, struct powerdomain *pwrdm2)
613 struct powerdomain *p;
615 if (!pwrdm1)
616 return -EINVAL;
618 if (!cpu_is_omap34xx())
619 return -EINVAL;
621 p = _pwrdm_deps_lookup(pwrdm2, pwrdm1->sleepdep_srcs);
622 if (IS_ERR(p)) {
623 pr_debug("powerdomain: hardware cannot set/clear sleep "
624 "dependency affecting %s from %s\n", pwrdm1->name,
625 pwrdm2->name);
626 return IS_ERR(p);
629 pr_debug("powerdomain: will no longer prevent %s from sleeping if "
630 "%s is active\n", pwrdm1->name, pwrdm2->name);
632 cm_clear_mod_reg_bits((1 << pwrdm2->dep_bit),
633 pwrdm1->prcm_offs, OMAP3430_CM_SLEEPDEP);
635 return 0;
639 * pwrdm_read_sleepdep - read sleep dependency state from pwrdm2 to pwrdm1
640 * @pwrdm1: prevent this struct powerdomain * from sleeping (dependent)
641 * @pwrdm2: when this struct powerdomain * is active (source)
643 * Return 1 if a hardware sleep dependency exists wherein pwrdm1 will
644 * not be allowed to automatically go inactive if pwrdm2 is active;
645 * 0 if pwrdm1's automatic power state inactivity transition is independent
646 * of pwrdm2's; -EINVAL if either powerdomain pointer is invalid or called
647 * on a machine that does not support software-configurable hardware sleep
648 * dependencies; or -ENOENT if the hardware is incapable.
650 * REVISIT: Currently this function only represents software-controllable
651 * sleep dependencies. Sleep dependencies fixed in hardware are not
652 * yet handled here.
654 int pwrdm_read_sleepdep(struct powerdomain *pwrdm1, struct powerdomain *pwrdm2)
656 struct powerdomain *p;
658 if (!pwrdm1)
659 return -EINVAL;
661 if (!cpu_is_omap34xx())
662 return -EINVAL;
664 p = _pwrdm_deps_lookup(pwrdm2, pwrdm1->sleepdep_srcs);
665 if (IS_ERR(p)) {
666 pr_debug("powerdomain: hardware cannot set/clear sleep "
667 "dependency affecting %s from %s\n", pwrdm1->name,
668 pwrdm2->name);
669 return IS_ERR(p);
672 return prm_read_mod_bits_shift(pwrdm1->prcm_offs, OMAP3430_CM_SLEEPDEP,
673 (1 << pwrdm2->dep_bit));
677 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
678 * @pwrdm: struct powerdomain *
680 * Return the number of controllable memory banks in powerdomain pwrdm,
681 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
683 int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
685 if (!pwrdm)
686 return -EINVAL;
688 return pwrdm->banks;
692 * pwrdm_set_next_pwrst - set next powerdomain power state
693 * @pwrdm: struct powerdomain * to set
694 * @pwrst: one of the PWRDM_POWER_* macros
696 * Set the powerdomain pwrdm's next power state to pwrst. The powerdomain
697 * may not enter this state immediately if the preconditions for this state
698 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
699 * null or if the power state is invalid for the powerdomin, or returns 0
700 * upon success.
702 int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
704 if (!pwrdm)
705 return -EINVAL;
707 if (!(pwrdm->pwrsts & (1 << pwrst)))
708 return -EINVAL;
710 pr_debug("powerdomain: setting next powerstate for %s to %0x\n",
711 pwrdm->name, pwrst);
713 prm_rmw_mod_reg_bits(OMAP_POWERSTATE_MASK,
714 (pwrst << OMAP_POWERSTATE_SHIFT),
715 pwrdm->prcm_offs, PM_PWSTCTRL);
717 return 0;
721 * pwrdm_read_next_pwrst - get next powerdomain power state
722 * @pwrdm: struct powerdomain * to get power state
724 * Return the powerdomain pwrdm's next power state. Returns -EINVAL
725 * if the powerdomain pointer is null or returns the next power state
726 * upon success.
728 int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
730 if (!pwrdm)
731 return -EINVAL;
733 return prm_read_mod_bits_shift(pwrdm->prcm_offs, PM_PWSTCTRL,
734 OMAP_POWERSTATE_MASK);
738 * pwrdm_read_pwrst - get current powerdomain power state
739 * @pwrdm: struct powerdomain * to get power state
741 * Return the powerdomain pwrdm's current power state. Returns -EINVAL
742 * if the powerdomain pointer is null or returns the current power state
743 * upon success.
745 int pwrdm_read_pwrst(struct powerdomain *pwrdm)
747 if (!pwrdm)
748 return -EINVAL;
750 return prm_read_mod_bits_shift(pwrdm->prcm_offs, PM_PWSTST,
751 OMAP_POWERSTATEST_MASK);
755 * pwrdm_read_prev_pwrst - get previous powerdomain power state
756 * @pwrdm: struct powerdomain * to get previous power state
758 * Return the powerdomain pwrdm's previous power state. Returns -EINVAL
759 * if the powerdomain pointer is null or returns the previous power state
760 * upon success.
762 int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
764 if (!pwrdm)
765 return -EINVAL;
767 return prm_read_mod_bits_shift(pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST,
768 OMAP3430_LASTPOWERSTATEENTERED_MASK);
772 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
773 * @pwrdm: struct powerdomain * to set
774 * @pwrst: one of the PWRDM_POWER_* macros
776 * Set the next power state that the logic portion of the powerdomain
777 * pwrdm will enter when the powerdomain enters retention. This will
778 * be either RETENTION or OFF, if supported. Returns -EINVAL if the
779 * powerdomain pointer is null or the target power state is not not
780 * supported, or returns 0 upon success.
782 int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
784 if (!pwrdm)
785 return -EINVAL;
787 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
788 return -EINVAL;
790 pr_debug("powerdomain: setting next logic powerstate for %s to %0x\n",
791 pwrdm->name, pwrst);
794 * The register bit names below may not correspond to the
795 * actual names of the bits in each powerdomain's register,
796 * but the type of value returned is the same for each
797 * powerdomain.
799 prm_rmw_mod_reg_bits(OMAP3430_LOGICL1CACHERETSTATE,
800 (pwrst << __ffs(OMAP3430_LOGICL1CACHERETSTATE)),
801 pwrdm->prcm_offs, PM_PWSTCTRL);
803 return 0;
807 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
808 * @pwrdm: struct powerdomain * to set
809 * @bank: memory bank number to set (0-3)
810 * @pwrst: one of the PWRDM_POWER_* macros
812 * Set the next power state that memory bank x of the powerdomain
813 * pwrdm will enter when the powerdomain enters the ON state. Bank
814 * will be a number from 0 to 3, and represents different types of
815 * memory, depending on the powerdomain. Returns -EINVAL if the
816 * powerdomain pointer is null or the target power state is not not
817 * supported for this memory bank, -EEXIST if the target memory bank
818 * does not exist or is not controllable, or returns 0 upon success.
820 int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
822 u32 m;
824 if (!pwrdm)
825 return -EINVAL;
827 if (pwrdm->banks < (bank + 1))
828 return -EEXIST;
830 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
831 return -EINVAL;
833 pr_debug("powerdomain: setting next memory powerstate for domain %s "
834 "bank %0x while pwrdm-ON to %0x\n", pwrdm->name, bank, pwrst);
837 * The register bit names below may not correspond to the
838 * actual names of the bits in each powerdomain's register,
839 * but the type of value returned is the same for each
840 * powerdomain.
842 switch (bank) {
843 case 0:
844 m = OMAP3430_SHAREDL1CACHEFLATONSTATE_MASK;
845 break;
846 case 1:
847 m = OMAP3430_L1FLATMEMONSTATE_MASK;
848 break;
849 case 2:
850 m = OMAP3430_SHAREDL2CACHEFLATONSTATE_MASK;
851 break;
852 case 3:
853 m = OMAP3430_L2FLATMEMONSTATE_MASK;
854 break;
855 default:
856 WARN_ON(1); /* should never happen */
857 return -EEXIST;
860 prm_rmw_mod_reg_bits(m, (pwrst << __ffs(m)),
861 pwrdm->prcm_offs, PM_PWSTCTRL);
863 return 0;
867 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
868 * @pwrdm: struct powerdomain * to set
869 * @bank: memory bank number to set (0-3)
870 * @pwrst: one of the PWRDM_POWER_* macros
872 * Set the next power state that memory bank x of the powerdomain
873 * pwrdm will enter when the powerdomain enters the RETENTION state.
874 * Bank will be a number from 0 to 3, and represents different types
875 * of memory, depending on the powerdomain. pwrst will be either
876 * RETENTION or OFF, if supported. Returns -EINVAL if the powerdomain
877 * pointer is null or the target power state is not not supported for
878 * this memory bank, -EEXIST if the target memory bank does not exist
879 * or is not controllable, or returns 0 upon success.
881 int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
883 u32 m;
885 if (!pwrdm)
886 return -EINVAL;
888 if (pwrdm->banks < (bank + 1))
889 return -EEXIST;
891 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
892 return -EINVAL;
894 pr_debug("powerdomain: setting next memory powerstate for domain %s "
895 "bank %0x while pwrdm-RET to %0x\n", pwrdm->name, bank, pwrst);
898 * The register bit names below may not correspond to the
899 * actual names of the bits in each powerdomain's register,
900 * but the type of value returned is the same for each
901 * powerdomain.
903 switch (bank) {
904 case 0:
905 m = OMAP3430_SHAREDL1CACHEFLATRETSTATE;
906 break;
907 case 1:
908 m = OMAP3430_L1FLATMEMRETSTATE;
909 break;
910 case 2:
911 m = OMAP3430_SHAREDL2CACHEFLATRETSTATE;
912 break;
913 case 3:
914 m = OMAP3430_L2FLATMEMRETSTATE;
915 break;
916 default:
917 WARN_ON(1); /* should never happen */
918 return -EEXIST;
921 prm_rmw_mod_reg_bits(m, (pwrst << __ffs(m)), pwrdm->prcm_offs,
922 PM_PWSTCTRL);
924 return 0;
928 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
929 * @pwrdm: struct powerdomain * to get current logic retention power state
931 * Return the current power state that the logic portion of
932 * powerdomain pwrdm will enter
933 * Returns -EINVAL if the powerdomain pointer is null or returns the
934 * current logic retention power state upon success.
936 int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
938 if (!pwrdm)
939 return -EINVAL;
941 return prm_read_mod_bits_shift(pwrdm->prcm_offs, PM_PWSTST,
942 OMAP3430_LOGICSTATEST);
946 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
947 * @pwrdm: struct powerdomain * to get previous logic power state
949 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
950 * if the powerdomain pointer is null or returns the previous logic
951 * power state upon success.
953 int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
955 if (!pwrdm)
956 return -EINVAL;
959 * The register bit names below may not correspond to the
960 * actual names of the bits in each powerdomain's register,
961 * but the type of value returned is the same for each
962 * powerdomain.
964 return prm_read_mod_bits_shift(pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST,
965 OMAP3430_LASTLOGICSTATEENTERED);
969 * pwrdm_read_mem_pwrst - get current memory bank power state
970 * @pwrdm: struct powerdomain * to get current memory bank power state
971 * @bank: memory bank number (0-3)
973 * Return the powerdomain pwrdm's current memory power state for bank
974 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
975 * the target memory bank does not exist or is not controllable, or
976 * returns the current memory power state upon success.
978 int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
980 u32 m;
982 if (!pwrdm)
983 return -EINVAL;
985 if (pwrdm->banks < (bank + 1))
986 return -EEXIST;
989 * The register bit names below may not correspond to the
990 * actual names of the bits in each powerdomain's register,
991 * but the type of value returned is the same for each
992 * powerdomain.
994 switch (bank) {
995 case 0:
996 m = OMAP3430_SHAREDL1CACHEFLATSTATEST_MASK;
997 break;
998 case 1:
999 m = OMAP3430_L1FLATMEMSTATEST_MASK;
1000 break;
1001 case 2:
1002 m = OMAP3430_SHAREDL2CACHEFLATSTATEST_MASK;
1003 break;
1004 case 3:
1005 m = OMAP3430_L2FLATMEMSTATEST_MASK;
1006 break;
1007 default:
1008 WARN_ON(1); /* should never happen */
1009 return -EEXIST;
1012 return prm_read_mod_bits_shift(pwrdm->prcm_offs, PM_PWSTST, m);
1016 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
1017 * @pwrdm: struct powerdomain * to get previous memory bank power state
1018 * @bank: memory bank number (0-3)
1020 * Return the powerdomain pwrdm's previous memory power state for bank
1021 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
1022 * the target memory bank does not exist or is not controllable, or
1023 * returns the previous memory power state upon success.
1025 int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
1027 u32 m;
1029 if (!pwrdm)
1030 return -EINVAL;
1032 if (pwrdm->banks < (bank + 1))
1033 return -EEXIST;
1036 * The register bit names below may not correspond to the
1037 * actual names of the bits in each powerdomain's register,
1038 * but the type of value returned is the same for each
1039 * powerdomain.
1041 switch (bank) {
1042 case 0:
1043 m = OMAP3430_LASTMEM1STATEENTERED_MASK;
1044 break;
1045 case 1:
1046 m = OMAP3430_LASTMEM2STATEENTERED_MASK;
1047 break;
1048 case 2:
1049 m = OMAP3430_LASTSHAREDL2CACHEFLATSTATEENTERED_MASK;
1050 break;
1051 case 3:
1052 m = OMAP3430_LASTL2FLATMEMSTATEENTERED_MASK;
1053 break;
1054 default:
1055 WARN_ON(1); /* should never happen */
1056 return -EEXIST;
1059 return prm_read_mod_bits_shift(pwrdm->prcm_offs,
1060 OMAP3430_PM_PREPWSTST, m);
1064 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
1065 * @pwrdm: struct powerdomain * to clear
1067 * Clear the powerdomain's previous power state register. Clears the
1068 * entire register, including logic and memory bank previous power states.
1069 * Returns -EINVAL if the powerdomain pointer is null, or returns 0 upon
1070 * success.
1072 int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
1074 if (!pwrdm)
1075 return -EINVAL;
1078 * XXX should get the powerdomain's current state here;
1079 * warn & fail if it is not ON.
1082 pr_debug("powerdomain: clearing previous power state reg for %s\n",
1083 pwrdm->name);
1085 prm_write_mod_reg(0, pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST);
1087 return 0;
1091 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
1092 * @pwrdm: struct powerdomain *
1094 * Enable automatic context save-and-restore upon power state change
1095 * for some devices in a powerdomain. Warning: this only affects a
1096 * subset of devices in a powerdomain; check the TRM closely. Returns
1097 * -EINVAL if the powerdomain pointer is null or if the powerdomain
1098 * does not support automatic save-and-restore, or returns 0 upon
1099 * success.
1101 int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
1103 if (!pwrdm)
1104 return -EINVAL;
1106 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
1107 return -EINVAL;
1109 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n",
1110 pwrdm->name);
1112 prm_rmw_mod_reg_bits(0, 1 << OMAP3430ES2_SAVEANDRESTORE_SHIFT,
1113 pwrdm->prcm_offs, PM_PWSTCTRL);
1115 return 0;
1119 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
1120 * @pwrdm: struct powerdomain *
1122 * Disable automatic context save-and-restore upon power state change
1123 * for some devices in a powerdomain. Warning: this only affects a
1124 * subset of devices in a powerdomain; check the TRM closely. Returns
1125 * -EINVAL if the powerdomain pointer is null or if the powerdomain
1126 * does not support automatic save-and-restore, or returns 0 upon
1127 * success.
1129 int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
1131 if (!pwrdm)
1132 return -EINVAL;
1134 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
1135 return -EINVAL;
1137 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n",
1138 pwrdm->name);
1140 prm_rmw_mod_reg_bits(1 << OMAP3430ES2_SAVEANDRESTORE_SHIFT, 0,
1141 pwrdm->prcm_offs, PM_PWSTCTRL);
1143 return 0;
1147 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
1148 * @pwrdm: struct powerdomain *
1150 * Returns 1 if powerdomain 'pwrdm' supports hardware save-and-restore
1151 * for some devices, or 0 if it does not.
1153 bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
1155 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
1159 * pwrdm_wait_transition - wait for powerdomain power transition to finish
1160 * @pwrdm: struct powerdomain * to wait for
1162 * If the powerdomain pwrdm is in the process of a state transition,
1163 * spin until it completes the power transition, or until an iteration
1164 * bailout value is reached. Returns -EINVAL if the powerdomain
1165 * pointer is null, -EAGAIN if the bailout value was reached, or
1166 * returns 0 upon success.
1168 int pwrdm_wait_transition(struct powerdomain *pwrdm)
1170 u32 c = 0;
1172 if (!pwrdm)
1173 return -EINVAL;
1176 * REVISIT: pwrdm_wait_transition() may be better implemented
1177 * via a callback and a periodic timer check -- how long do we expect
1178 * powerdomain transitions to take?
1181 /* XXX Is this udelay() value meaningful? */
1182 while ((prm_read_mod_reg(pwrdm->prcm_offs, PM_PWSTST) &
1183 OMAP_INTRANSITION) &&
1184 (c++ < PWRDM_TRANSITION_BAILOUT))
1185 udelay(1);
1187 if (c > PWRDM_TRANSITION_BAILOUT) {
1188 printk(KERN_ERR "powerdomain: waited too long for "
1189 "powerdomain %s to complete transition\n", pwrdm->name);
1190 return -EAGAIN;
1193 pr_debug("powerdomain: completed transition in %d loops\n", c);
1195 return 0;
1198 int pwrdm_state_switch(struct powerdomain *pwrdm)
1200 return _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
1203 int pwrdm_clkdm_state_switch(struct clockdomain *clkdm)
1205 if (clkdm != NULL && clkdm->pwrdm.ptr != NULL) {
1206 pwrdm_wait_transition(clkdm->pwrdm.ptr);
1207 return pwrdm_state_switch(clkdm->pwrdm.ptr);
1210 return -EINVAL;
1212 int pwrdm_clk_state_switch(struct clk *clk)
1214 if (clk != NULL && clk->clkdm != NULL)
1215 return pwrdm_clkdm_state_switch(clk->clkdm);
1216 return -EINVAL;
1219 int pwrdm_pre_transition(void)
1221 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
1222 return 0;
1225 int pwrdm_post_transition(void)
1227 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
1228 return 0;