Merge tag 'stable/for-linus-3.16-rc7-tag' of git://git.kernel.org/pub/scm/linux/kerne...
[deliverable/linux.git] / arch / arm / mach-omap2 / powerdomain.c
CommitLineData
ad67ef68
PW
1/*
2 * OMAP powerdomain control
3 *
129c65ee 4 * Copyright (C) 2007-2008, 2011 Texas Instruments, Inc.
694606c4 5 * Copyright (C) 2007-2011 Nokia Corporation
ad67ef68
PW
6 *
7 * Written by Paul Walmsley
3a759f09 8 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
4788da26 9 * State counting code by Tero Kristo <tero.kristo@nokia.com>
3a759f09 10 *
ad67ef68
PW
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.
14 */
33903eb5 15#undef DEBUG
ad67ef68
PW
16
17#include <linux/kernel.h>
ad67ef68 18#include <linux/types.h>
ad67ef68
PW
19#include <linux/list.h>
20#include <linux/errno.h>
9b7fc907 21#include <linux/string.h>
3a090284 22#include <linux/spinlock.h>
5e7c58dc
JP
23#include <trace/events/power.h>
24
59fb659b 25#include "cm2xxx_3xxx.h"
a64bb9cd 26#include "prcm44xx.h"
59fb659b
PW
27#include "cm44xx.h"
28#include "prm2xxx_3xxx.h"
d198b514 29#include "prm44xx.h"
ad67ef68 30
5e7c58dc 31#include <asm/cpu.h>
dbc04161 32
72e06d08 33#include "powerdomain.h"
1540f214 34#include "clockdomain.h"
4794208c 35#include "voltage.h"
ad67ef68 36
dbc04161 37#include "soc.h"
6199ab26
PDS
38#include "pm.h"
39
5e7c58dc
JP
40#define PWRDM_TRACE_STATES_FLAG (1<<31)
41
ba20bb12
PDS
42enum {
43 PWRDM_STATE_NOW = 0,
44 PWRDM_STATE_PREV,
45};
46
c4978fba
PW
47/*
48 * Types of sleep_switch used internally in omap_set_pwrdm_state()
49 * and its associated static functions
50 *
51 * XXX Better documentation is needed here
52 */
53#define ALREADYACTIVE_SWITCH 0
54#define FORCEWAKEUP_SWITCH 1
55#define LOWPOWERSTATE_SWITCH 2
3a759f09 56
ad67ef68
PW
57/* pwrdm_list contains all registered struct powerdomains */
58static LIST_HEAD(pwrdm_list);
59
3b1e8b21
RN
60static struct pwrdm_ops *arch_pwrdm;
61
ad67ef68
PW
62/* Private functions */
63
ad67ef68
PW
64static struct powerdomain *_pwrdm_lookup(const char *name)
65{
66 struct powerdomain *pwrdm, *temp_pwrdm;
67
68 pwrdm = NULL;
69
70 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
71 if (!strcmp(name, temp_pwrdm->name)) {
72 pwrdm = temp_pwrdm;
73 break;
74 }
75 }
76
77 return pwrdm;
78}
79
e909d62a
PW
80/**
81 * _pwrdm_register - register a powerdomain
82 * @pwrdm: struct powerdomain * to register
83 *
84 * Adds a powerdomain to the internal powerdomain list. Returns
85 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
86 * already registered by the provided name, or 0 upon success.
87 */
88static int _pwrdm_register(struct powerdomain *pwrdm)
89{
90 int i;
048a7034 91 struct voltagedomain *voltdm;
e909d62a 92
a64bb9cd 93 if (!pwrdm || !pwrdm->name)
e909d62a
PW
94 return -EINVAL;
95
a64bb9cd
PW
96 if (cpu_is_omap44xx() &&
97 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
98 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
99 pwrdm->name);
100 return -EINVAL;
101 }
102
e909d62a
PW
103 if (_pwrdm_lookup(pwrdm->name))
104 return -EEXIST;
105
cd8abed1
RN
106 if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
107 if (!arch_pwrdm->pwrdm_has_voltdm())
108 goto skip_voltdm;
109
048a7034
KH
110 voltdm = voltdm_lookup(pwrdm->voltdm.name);
111 if (!voltdm) {
112 pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
113 pwrdm->name, pwrdm->voltdm.name);
114 return -EINVAL;
115 }
116 pwrdm->voltdm.ptr = voltdm;
e69c22b1
KH
117 INIT_LIST_HEAD(&pwrdm->voltdm_node);
118 voltdm_add_pwrdm(voltdm, pwrdm);
cd8abed1 119skip_voltdm:
3a090284 120 spin_lock_init(&pwrdm->_lock);
048a7034 121
e909d62a
PW
122 list_add(&pwrdm->node, &pwrdm_list);
123
124 /* Initialize the powerdomain's state counter */
cf57aa7c 125 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
e909d62a
PW
126 pwrdm->state_counter[i] = 0;
127
cde08f81
TG
128 pwrdm->ret_logic_off_counter = 0;
129 for (i = 0; i < pwrdm->banks; i++)
130 pwrdm->ret_mem_off_counter[i] = 0;
131
1cfc4bdd
RN
132 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
133 arch_pwrdm->pwrdm_wait_transition(pwrdm);
e909d62a
PW
134 pwrdm->state = pwrdm_read_pwrst(pwrdm);
135 pwrdm->state_counter[pwrdm->state] = 1;
136
137 pr_debug("powerdomain: registered %s\n", pwrdm->name);
138
139 return 0;
140}
141
cde08f81
TG
142static void _update_logic_membank_counters(struct powerdomain *pwrdm)
143{
144 int i;
145 u8 prev_logic_pwrst, prev_mem_pwrst;
146
147 prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
148 if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
149 (prev_logic_pwrst == PWRDM_POWER_OFF))
150 pwrdm->ret_logic_off_counter++;
151
152 for (i = 0; i < pwrdm->banks; i++) {
153 prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
154
155 if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
156 (prev_mem_pwrst == PWRDM_POWER_OFF))
157 pwrdm->ret_mem_off_counter[i]++;
158 }
159}
160
ba20bb12
PDS
161static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
162{
163
c165a140 164 int prev, next, state, trace_state = 0;
ba20bb12
PDS
165
166 if (pwrdm == NULL)
167 return -EINVAL;
168
169 state = pwrdm_read_pwrst(pwrdm);
170
171 switch (flag) {
172 case PWRDM_STATE_NOW:
173 prev = pwrdm->state;
174 break;
175 case PWRDM_STATE_PREV:
176 prev = pwrdm_read_prev_pwrst(pwrdm);
177 if (pwrdm->state != prev)
178 pwrdm->state_counter[prev]++;
cde08f81
TG
179 if (prev == PWRDM_POWER_RET)
180 _update_logic_membank_counters(pwrdm);
5e7c58dc
JP
181 /*
182 * If the power domain did not hit the desired state,
183 * generate a trace event with both the desired and hit states
184 */
c165a140
JP
185 next = pwrdm_read_next_pwrst(pwrdm);
186 if (next != prev) {
5e7c58dc 187 trace_state = (PWRDM_TRACE_STATES_FLAG |
c165a140 188 ((next & OMAP_POWERSTATE_MASK) << 8) |
5e7c58dc
JP
189 ((prev & OMAP_POWERSTATE_MASK) << 0));
190 trace_power_domain_target(pwrdm->name, trace_state,
191 smp_processor_id());
192 }
ba20bb12
PDS
193 break;
194 default:
195 return -EINVAL;
196 }
197
198 if (state != prev)
199 pwrdm->state_counter[state]++;
200
6199ab26
PDS
201 pm_dbg_update_time(pwrdm, prev);
202
ba20bb12
PDS
203 pwrdm->state = state;
204
205 return 0;
206}
207
6199ab26 208static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
ba20bb12
PDS
209{
210 pwrdm_clear_all_prev_pwrst(pwrdm);
211 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
212 return 0;
213}
214
6199ab26 215static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
ba20bb12
PDS
216{
217 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
218 return 0;
219}
220
c4978fba
PW
221/**
222 * _pwrdm_save_clkdm_state_and_activate - prepare for power state change
223 * @pwrdm: struct powerdomain * to operate on
224 * @curr_pwrst: current power state of @pwrdm
225 * @pwrst: power state to switch to
226 * @hwsup: ptr to a bool to return whether the clkdm is hardware-supervised
227 *
228 * Determine whether the powerdomain needs to be turned on before
229 * attempting to switch power states. Called by
230 * omap_set_pwrdm_state(). NOTE that if the powerdomain contains
231 * multiple clockdomains, this code assumes that the first clockdomain
232 * supports software-supervised wakeup mode - potentially a problem.
233 * Returns the power state switch mode currently in use (see the
234 * "Types of sleep_switch" comment above).
235 */
236static u8 _pwrdm_save_clkdm_state_and_activate(struct powerdomain *pwrdm,
237 u8 curr_pwrst, u8 pwrst,
238 bool *hwsup)
239{
240 u8 sleep_switch;
241
bd70f6eb 242 if (curr_pwrst < PWRDM_POWER_ON) {
c4978fba
PW
243 if (curr_pwrst > pwrst &&
244 pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
245 arch_pwrdm->pwrdm_set_lowpwrstchange) {
246 sleep_switch = LOWPOWERSTATE_SWITCH;
247 } else {
248 *hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
3a090284 249 clkdm_wakeup_nolock(pwrdm->pwrdm_clkdms[0]);
c4978fba
PW
250 sleep_switch = FORCEWAKEUP_SWITCH;
251 }
252 } else {
253 sleep_switch = ALREADYACTIVE_SWITCH;
254 }
255
256 return sleep_switch;
257}
258
259/**
260 * _pwrdm_restore_clkdm_state - restore the clkdm hwsup state after pwrst change
261 * @pwrdm: struct powerdomain * to operate on
262 * @sleep_switch: return value from _pwrdm_save_clkdm_state_and_activate()
263 * @hwsup: should @pwrdm's first clockdomain be set to hardware-supervised mode?
264 *
265 * Restore the clockdomain state perturbed by
266 * _pwrdm_save_clkdm_state_and_activate(), and call the power state
267 * bookkeeping code. Called by omap_set_pwrdm_state(). NOTE that if
268 * the powerdomain contains multiple clockdomains, this assumes that
269 * the first associated clockdomain supports either
270 * hardware-supervised idle control in the register, or
271 * software-supervised sleep. No return value.
272 */
273static void _pwrdm_restore_clkdm_state(struct powerdomain *pwrdm,
274 u8 sleep_switch, bool hwsup)
275{
276 switch (sleep_switch) {
277 case FORCEWAKEUP_SWITCH:
278 if (hwsup)
3a090284 279 clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
c4978fba 280 else
3a090284 281 clkdm_sleep_nolock(pwrdm->pwrdm_clkdms[0]);
c4978fba
PW
282 break;
283 case LOWPOWERSTATE_SWITCH:
284 if (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
285 arch_pwrdm->pwrdm_set_lowpwrstchange)
286 arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
3a090284 287 pwrdm_state_switch_nolock(pwrdm);
c4978fba
PW
288 break;
289 }
290}
291
ad67ef68
PW
292/* Public functions */
293
294/**
129c65ee
PW
295 * pwrdm_register_platform_funcs - register powerdomain implementation fns
296 * @po: func pointers for arch specific implementations
ad67ef68 297 *
129c65ee
PW
298 * Register the list of function pointers used to implement the
299 * powerdomain functions on different OMAP SoCs. Should be called
300 * before any other pwrdm_register*() function. Returns -EINVAL if
301 * @po is null, -EEXIST if platform functions have already been
302 * registered, or 0 upon success.
ad67ef68 303 */
129c65ee
PW
304int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
305{
306 if (!po)
307 return -EINVAL;
308
309 if (arch_pwrdm)
310 return -EEXIST;
311
312 arch_pwrdm = po;
313
314 return 0;
315}
316
317/**
318 * pwrdm_register_pwrdms - register SoC powerdomains
319 * @ps: pointer to an array of struct powerdomain to register
320 *
321 * Register the powerdomains available on a particular OMAP SoC. Must
322 * be called after pwrdm_register_platform_funcs(). May be called
323 * multiple times. Returns -EACCES if called before
324 * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
325 * null; or 0 upon success.
326 */
327int pwrdm_register_pwrdms(struct powerdomain **ps)
ad67ef68
PW
328{
329 struct powerdomain **p = NULL;
330
129c65ee
PW
331 if (!arch_pwrdm)
332 return -EEXIST;
3b1e8b21 333
129c65ee
PW
334 if (!ps)
335 return -EINVAL;
336
337 for (p = ps; *p; p++)
338 _pwrdm_register(*p);
339
340 return 0;
341}
342
343/**
344 * pwrdm_complete_init - set up the powerdomain layer
345 *
346 * Do whatever is necessary to initialize registered powerdomains and
347 * powerdomain code. Currently, this programs the next power state
348 * for each powerdomain to ON. This prevents powerdomains from
349 * unexpectedly losing context or entering high wakeup latency modes
350 * with non-power-management-enabled kernels. Must be called after
351 * pwrdm_register_pwrdms(). Returns -EACCES if called before
352 * pwrdm_register_pwrdms(), or 0 upon success.
353 */
354int pwrdm_complete_init(void)
355{
356 struct powerdomain *temp_p;
357
358 if (list_empty(&pwrdm_list))
359 return -EACCES;
c956b753
RN
360
361 list_for_each_entry(temp_p, &pwrdm_list, node)
362 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
129c65ee
PW
363
364 return 0;
ad67ef68
PW
365}
366
3a090284
PW
367/**
368 * pwrdm_lock - acquire a Linux spinlock on a powerdomain
369 * @pwrdm: struct powerdomain * to lock
370 *
371 * Acquire the powerdomain spinlock on @pwrdm. No return value.
372 */
373void pwrdm_lock(struct powerdomain *pwrdm)
374 __acquires(&pwrdm->_lock)
375{
376 spin_lock_irqsave(&pwrdm->_lock, pwrdm->_lock_flags);
377}
378
379/**
380 * pwrdm_unlock - release a Linux spinlock on a powerdomain
381 * @pwrdm: struct powerdomain * to unlock
382 *
383 * Release the powerdomain spinlock on @pwrdm. No return value.
384 */
385void pwrdm_unlock(struct powerdomain *pwrdm)
386 __releases(&pwrdm->_lock)
387{
388 spin_unlock_irqrestore(&pwrdm->_lock, pwrdm->_lock_flags);
389}
390
ad67ef68
PW
391/**
392 * pwrdm_lookup - look up a powerdomain by name, return a pointer
393 * @name: name of powerdomain
394 *
f0271d65
PW
395 * Find a registered powerdomain by its name @name. Returns a pointer
396 * to the struct powerdomain if found, or NULL otherwise.
ad67ef68
PW
397 */
398struct powerdomain *pwrdm_lookup(const char *name)
399{
400 struct powerdomain *pwrdm;
ad67ef68
PW
401
402 if (!name)
403 return NULL;
404
ad67ef68 405 pwrdm = _pwrdm_lookup(name);
ad67ef68
PW
406
407 return pwrdm;
408}
409
410/**
e909d62a 411 * pwrdm_for_each - call function on each registered clockdomain
ad67ef68
PW
412 * @fn: callback function *
413 *
f0271d65
PW
414 * Call the supplied function @fn for each registered powerdomain.
415 * The callback function @fn can return anything but 0 to bail out
416 * early from the iterator. Returns the last return value of the
417 * callback function, which should be 0 for success or anything else
418 * to indicate failure; or -EINVAL if the function pointer is null.
ad67ef68 419 */
e909d62a
PW
420int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
421 void *user)
ad67ef68
PW
422{
423 struct powerdomain *temp_pwrdm;
ad67ef68
PW
424 int ret = 0;
425
426 if (!fn)
427 return -EINVAL;
428
ad67ef68 429 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
6199ab26 430 ret = (*fn)(temp_pwrdm, user);
ad67ef68
PW
431 if (ret)
432 break;
433 }
ee894b18
AB
434
435 return ret;
436}
437
8420bb13
PW
438/**
439 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
440 * @pwrdm: struct powerdomain * to add the clockdomain to
441 * @clkdm: struct clockdomain * to associate with a powerdomain
442 *
f0271d65 443 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
8420bb13
PW
444 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
445 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
446 * or 0 upon success.
447 */
448int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
449{
8420bb13
PW
450 int i;
451 int ret = -EINVAL;
452
453 if (!pwrdm || !clkdm)
454 return -EINVAL;
455
7852ec05
PW
456 pr_debug("powerdomain: %s: associating clockdomain %s\n",
457 pwrdm->name, clkdm->name);
8420bb13 458
8420bb13
PW
459 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
460 if (!pwrdm->pwrdm_clkdms[i])
461 break;
462#ifdef DEBUG
463 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
464 ret = -EINVAL;
465 goto pac_exit;
466 }
467#endif
468 }
469
470 if (i == PWRDM_MAX_CLKDMS) {
7852ec05
PW
471 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
472 pwrdm->name, clkdm->name);
8420bb13
PW
473 WARN_ON(1);
474 ret = -ENOMEM;
475 goto pac_exit;
476 }
477
478 pwrdm->pwrdm_clkdms[i] = clkdm;
479
480 ret = 0;
481
482pac_exit:
8420bb13
PW
483 return ret;
484}
485
486/**
487 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
488 * @pwrdm: struct powerdomain * to add the clockdomain to
489 * @clkdm: struct clockdomain * to associate with a powerdomain
490 *
f0271d65
PW
491 * Dissociate the clockdomain @clkdm from the powerdomain
492 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
493 * if @clkdm was not associated with the powerdomain, or 0 upon
494 * success.
8420bb13
PW
495 */
496int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
497{
8420bb13
PW
498 int ret = -EINVAL;
499 int i;
500
501 if (!pwrdm || !clkdm)
502 return -EINVAL;
503
7852ec05
PW
504 pr_debug("powerdomain: %s: dissociating clockdomain %s\n",
505 pwrdm->name, clkdm->name);
8420bb13 506
8420bb13
PW
507 for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
508 if (pwrdm->pwrdm_clkdms[i] == clkdm)
509 break;
510
511 if (i == PWRDM_MAX_CLKDMS) {
7852ec05
PW
512 pr_debug("powerdomain: %s: clkdm %s not associated?!\n",
513 pwrdm->name, clkdm->name);
8420bb13
PW
514 ret = -ENOENT;
515 goto pdc_exit;
516 }
517
518 pwrdm->pwrdm_clkdms[i] = NULL;
519
520 ret = 0;
521
522pdc_exit:
8420bb13
PW
523 return ret;
524}
525
526/**
527 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
528 * @pwrdm: struct powerdomain * to iterate over
529 * @fn: callback function *
530 *
f0271d65
PW
531 * Call the supplied function @fn for each clockdomain in the powerdomain
532 * @pwrdm. The callback function can return anything but 0 to bail
e909d62a
PW
533 * out early from the iterator. Returns -EINVAL if presented with
534 * invalid pointers; or passes along the last return value of the
535 * callback function, which should be 0 for success or anything else
536 * to indicate failure.
8420bb13
PW
537 */
538int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
539 int (*fn)(struct powerdomain *pwrdm,
540 struct clockdomain *clkdm))
541{
8420bb13
PW
542 int ret = 0;
543 int i;
544
545 if (!fn)
546 return -EINVAL;
547
8420bb13
PW
548 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
549 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
550
8420bb13
PW
551 return ret;
552}
553
048a7034
KH
554/**
555 * pwrdm_get_voltdm - return a ptr to the voltdm that this pwrdm resides in
556 * @pwrdm: struct powerdomain *
557 *
558 * Return a pointer to the struct voltageomain that the specified powerdomain
559 * @pwrdm exists in.
560 */
561struct voltagedomain *pwrdm_get_voltdm(struct powerdomain *pwrdm)
562{
563 return pwrdm->voltdm.ptr;
564}
565
ad67ef68
PW
566/**
567 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
568 * @pwrdm: struct powerdomain *
569 *
f0271d65 570 * Return the number of controllable memory banks in powerdomain @pwrdm,
ad67ef68
PW
571 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
572 */
573int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
574{
575 if (!pwrdm)
576 return -EINVAL;
577
578 return pwrdm->banks;
579}
580
581/**
582 * pwrdm_set_next_pwrst - set next powerdomain power state
583 * @pwrdm: struct powerdomain * to set
584 * @pwrst: one of the PWRDM_POWER_* macros
585 *
f0271d65 586 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
ad67ef68
PW
587 * may not enter this state immediately if the preconditions for this state
588 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
589 * null or if the power state is invalid for the powerdomin, or returns 0
590 * upon success.
591 */
592int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
593{
f327e07b
RN
594 int ret = -EINVAL;
595
ad67ef68
PW
596 if (!pwrdm)
597 return -EINVAL;
598
599 if (!(pwrdm->pwrsts & (1 << pwrst)))
600 return -EINVAL;
601
7852ec05 602 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
ad67ef68
PW
603 pwrdm->name, pwrst);
604
5e7c58dc
JP
605 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
606 /* Trace the pwrdm desired target state */
607 trace_power_domain_target(pwrdm->name, pwrst,
608 smp_processor_id());
609 /* Program the pwrdm desired target state */
f327e07b 610 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
5e7c58dc 611 }
ad67ef68 612
f327e07b 613 return ret;
ad67ef68
PW
614}
615
616/**
617 * pwrdm_read_next_pwrst - get next powerdomain power state
618 * @pwrdm: struct powerdomain * to get power state
619 *
f0271d65 620 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
ad67ef68
PW
621 * if the powerdomain pointer is null or returns the next power state
622 * upon success.
623 */
624int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
625{
f327e07b
RN
626 int ret = -EINVAL;
627
ad67ef68
PW
628 if (!pwrdm)
629 return -EINVAL;
630
f327e07b
RN
631 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
632 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
633
634 return ret;
ad67ef68
PW
635}
636
637/**
638 * pwrdm_read_pwrst - get current powerdomain power state
639 * @pwrdm: struct powerdomain * to get power state
640 *
f0271d65 641 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
ad67ef68 642 * if the powerdomain pointer is null or returns the current power state
d49cae92
JH
643 * upon success. Note that if the power domain only supports the ON state
644 * then just return ON as the current state.
ad67ef68
PW
645 */
646int pwrdm_read_pwrst(struct powerdomain *pwrdm)
647{
f327e07b
RN
648 int ret = -EINVAL;
649
ad67ef68
PW
650 if (!pwrdm)
651 return -EINVAL;
652
d49cae92
JH
653 if (pwrdm->pwrsts == PWRSTS_ON)
654 return PWRDM_POWER_ON;
655
f327e07b
RN
656 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
657 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
658
659 return ret;
ad67ef68
PW
660}
661
662/**
663 * pwrdm_read_prev_pwrst - get previous powerdomain power state
664 * @pwrdm: struct powerdomain * to get previous power state
665 *
f0271d65 666 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
ad67ef68
PW
667 * if the powerdomain pointer is null or returns the previous power state
668 * upon success.
669 */
670int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
671{
f327e07b
RN
672 int ret = -EINVAL;
673
ad67ef68
PW
674 if (!pwrdm)
675 return -EINVAL;
676
f327e07b
RN
677 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
678 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
679
680 return ret;
ad67ef68
PW
681}
682
683/**
684 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
685 * @pwrdm: struct powerdomain * to set
686 * @pwrst: one of the PWRDM_POWER_* macros
687 *
f0271d65
PW
688 * Set the next power state @pwrst that the logic portion of the
689 * powerdomain @pwrdm will enter when the powerdomain enters retention.
690 * This will be either RETENTION or OFF, if supported. Returns
691 * -EINVAL if the powerdomain pointer is null or the target power
692 * state is not not supported, or returns 0 upon success.
ad67ef68
PW
693 */
694int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
695{
12627578 696 int ret = -EINVAL;
2bc4ef71 697
ad67ef68
PW
698 if (!pwrdm)
699 return -EINVAL;
700
701 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
702 return -EINVAL;
703
7852ec05 704 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
ad67ef68
PW
705 pwrdm->name, pwrst);
706
12627578
RN
707 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
708 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
ad67ef68 709
12627578 710 return ret;
ad67ef68
PW
711}
712
713/**
714 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
715 * @pwrdm: struct powerdomain * to set
716 * @bank: memory bank number to set (0-3)
717 * @pwrst: one of the PWRDM_POWER_* macros
718 *
f0271d65
PW
719 * Set the next power state @pwrst that memory bank @bank of the
720 * powerdomain @pwrdm will enter when the powerdomain enters the ON
721 * state. @bank will be a number from 0 to 3, and represents different
722 * types of memory, depending on the powerdomain. Returns -EINVAL if
723 * the powerdomain pointer is null or the target power state is not
724 * not supported for this memory bank, -EEXIST if the target memory
725 * bank does not exist or is not controllable, or returns 0 upon
726 * success.
ad67ef68
PW
727 */
728int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
729{
9b7fc907 730 int ret = -EINVAL;
ad67ef68
PW
731
732 if (!pwrdm)
733 return -EINVAL;
734
735 if (pwrdm->banks < (bank + 1))
736 return -EEXIST;
737
738 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
739 return -EINVAL;
740
7852ec05
PW
741 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
742 pwrdm->name, bank, pwrst);
ad67ef68 743
9b7fc907
RN
744 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
745 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
ad67ef68 746
9b7fc907 747 return ret;
ad67ef68
PW
748}
749
750/**
751 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
752 * @pwrdm: struct powerdomain * to set
753 * @bank: memory bank number to set (0-3)
754 * @pwrst: one of the PWRDM_POWER_* macros
755 *
f0271d65
PW
756 * Set the next power state @pwrst that memory bank @bank of the
757 * powerdomain @pwrdm will enter when the powerdomain enters the
758 * RETENTION state. Bank will be a number from 0 to 3, and represents
759 * different types of memory, depending on the powerdomain. @pwrst
760 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
761 * the powerdomain pointer is null or the target power state is not
762 * not supported for this memory bank, -EEXIST if the target memory
763 * bank does not exist or is not controllable, or returns 0 upon
764 * success.
ad67ef68
PW
765 */
766int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
767{
9b7fc907 768 int ret = -EINVAL;
ad67ef68
PW
769
770 if (!pwrdm)
771 return -EINVAL;
772
773 if (pwrdm->banks < (bank + 1))
774 return -EEXIST;
775
776 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
777 return -EINVAL;
778
7852ec05
PW
779 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
780 pwrdm->name, bank, pwrst);
ad67ef68 781
9b7fc907
RN
782 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
783 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
ad67ef68 784
9b7fc907 785 return ret;
ad67ef68
PW
786}
787
788/**
789 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
790 * @pwrdm: struct powerdomain * to get current logic retention power state
791 *
f0271d65
PW
792 * Return the power state that the logic portion of powerdomain @pwrdm
793 * will enter when the powerdomain enters retention. Returns -EINVAL
794 * if the powerdomain pointer is null or returns the logic retention
795 * power state upon success.
ad67ef68
PW
796 */
797int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
798{
12627578
RN
799 int ret = -EINVAL;
800
ad67ef68
PW
801 if (!pwrdm)
802 return -EINVAL;
803
12627578
RN
804 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
805 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
806
807 return ret;
ad67ef68
PW
808}
809
810/**
811 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
812 * @pwrdm: struct powerdomain * to get previous logic power state
813 *
f0271d65
PW
814 * Return the powerdomain @pwrdm's previous logic power state. Returns
815 * -EINVAL if the powerdomain pointer is null or returns the previous
816 * logic power state upon success.
ad67ef68
PW
817 */
818int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
819{
12627578
RN
820 int ret = -EINVAL;
821
ad67ef68
PW
822 if (!pwrdm)
823 return -EINVAL;
824
12627578
RN
825 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
826 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
827
828 return ret;
ad67ef68
PW
829}
830
1e3d0d2b
TG
831/**
832 * pwrdm_read_logic_retst - get next powerdomain logic power state
833 * @pwrdm: struct powerdomain * to get next logic power state
834 *
835 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
836 * if the powerdomain pointer is null or returns the next logic
837 * power state upon success.
838 */
839int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
840{
12627578
RN
841 int ret = -EINVAL;
842
1e3d0d2b
TG
843 if (!pwrdm)
844 return -EINVAL;
845
12627578
RN
846 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
847 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
848
849 return ret;
1e3d0d2b
TG
850}
851
ad67ef68
PW
852/**
853 * pwrdm_read_mem_pwrst - get current memory bank power state
854 * @pwrdm: struct powerdomain * to get current memory bank power state
855 * @bank: memory bank number (0-3)
856 *
f0271d65
PW
857 * Return the powerdomain @pwrdm's current memory power state for bank
858 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
ad67ef68
PW
859 * the target memory bank does not exist or is not controllable, or
860 * returns the current memory power state upon success.
861 */
862int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
863{
9b7fc907 864 int ret = -EINVAL;
ad67ef68
PW
865
866 if (!pwrdm)
9b7fc907 867 return ret;
ad67ef68
PW
868
869 if (pwrdm->banks < (bank + 1))
9b7fc907 870 return ret;
ad67ef68 871
3863c74b
TG
872 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
873 bank = 1;
874
9b7fc907
RN
875 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
876 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
ad67ef68 877
9b7fc907 878 return ret;
ad67ef68
PW
879}
880
881/**
882 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
883 * @pwrdm: struct powerdomain * to get previous memory bank power state
884 * @bank: memory bank number (0-3)
885 *
f0271d65
PW
886 * Return the powerdomain @pwrdm's previous memory power state for
887 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
888 * -EEXIST if the target memory bank does not exist or is not
889 * controllable, or returns the previous memory power state upon
890 * success.
ad67ef68
PW
891 */
892int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
893{
9b7fc907 894 int ret = -EINVAL;
ad67ef68
PW
895
896 if (!pwrdm)
9b7fc907 897 return ret;
ad67ef68
PW
898
899 if (pwrdm->banks < (bank + 1))
9b7fc907 900 return ret;
ad67ef68 901
3863c74b
TG
902 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
903 bank = 1;
904
9b7fc907
RN
905 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
906 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
ad67ef68 907
9b7fc907 908 return ret;
ad67ef68
PW
909}
910
1e3d0d2b
TG
911/**
912 * pwrdm_read_mem_retst - get next memory bank power state
913 * @pwrdm: struct powerdomain * to get mext memory bank power state
914 * @bank: memory bank number (0-3)
915 *
916 * Return the powerdomain pwrdm's next memory power state for bank
917 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
918 * the target memory bank does not exist or is not controllable, or
919 * returns the next memory power state upon success.
920 */
921int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
922{
9b7fc907 923 int ret = -EINVAL;
1e3d0d2b
TG
924
925 if (!pwrdm)
9b7fc907 926 return ret;
1e3d0d2b
TG
927
928 if (pwrdm->banks < (bank + 1))
9b7fc907 929 return ret;
1e3d0d2b 930
9b7fc907
RN
931 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
932 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
1e3d0d2b 933
9b7fc907 934 return ret;
1e3d0d2b
TG
935}
936
ad67ef68
PW
937/**
938 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
939 * @pwrdm: struct powerdomain * to clear
940 *
f0271d65
PW
941 * Clear the powerdomain's previous power state register @pwrdm.
942 * Clears the entire register, including logic and memory bank
943 * previous power states. Returns -EINVAL if the powerdomain pointer
944 * is null, or returns 0 upon success.
ad67ef68
PW
945 */
946int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
947{
9b7fc907
RN
948 int ret = -EINVAL;
949
ad67ef68 950 if (!pwrdm)
9b7fc907 951 return ret;
ad67ef68
PW
952
953 /*
954 * XXX should get the powerdomain's current state here;
955 * warn & fail if it is not ON.
956 */
957
7852ec05 958 pr_debug("powerdomain: %s: clearing previous power state reg\n",
ad67ef68
PW
959 pwrdm->name);
960
9b7fc907
RN
961 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
962 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
ad67ef68 963
9b7fc907 964 return ret;
ad67ef68
PW
965}
966
0b7cbfb5
PW
967/**
968 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
969 * @pwrdm: struct powerdomain *
970 *
971 * Enable automatic context save-and-restore upon power state change
f0271d65
PW
972 * for some devices in the powerdomain @pwrdm. Warning: this only
973 * affects a subset of devices in a powerdomain; check the TRM
974 * closely. Returns -EINVAL if the powerdomain pointer is null or if
975 * the powerdomain does not support automatic save-and-restore, or
976 * returns 0 upon success.
0b7cbfb5
PW
977 */
978int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
979{
9b7fc907
RN
980 int ret = -EINVAL;
981
0b7cbfb5 982 if (!pwrdm)
9b7fc907 983 return ret;
0b7cbfb5
PW
984
985 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
9b7fc907 986 return ret;
0b7cbfb5 987
7852ec05 988 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
0b7cbfb5 989
9b7fc907
RN
990 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
991 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
0b7cbfb5 992
9b7fc907 993 return ret;
0b7cbfb5
PW
994}
995
996/**
997 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
998 * @pwrdm: struct powerdomain *
999 *
1000 * Disable automatic context save-and-restore upon power state change
f0271d65
PW
1001 * for some devices in the powerdomain @pwrdm. Warning: this only
1002 * affects a subset of devices in a powerdomain; check the TRM
1003 * closely. Returns -EINVAL if the powerdomain pointer is null or if
1004 * the powerdomain does not support automatic save-and-restore, or
1005 * returns 0 upon success.
0b7cbfb5
PW
1006 */
1007int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
1008{
9b7fc907
RN
1009 int ret = -EINVAL;
1010
0b7cbfb5 1011 if (!pwrdm)
9b7fc907 1012 return ret;
0b7cbfb5
PW
1013
1014 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
9b7fc907 1015 return ret;
0b7cbfb5 1016
7852ec05 1017 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
0b7cbfb5 1018
9b7fc907
RN
1019 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
1020 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
0b7cbfb5 1021
9b7fc907 1022 return ret;
0b7cbfb5
PW
1023}
1024
1025/**
1026 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
1027 * @pwrdm: struct powerdomain *
1028 *
f0271d65 1029 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
0b7cbfb5
PW
1030 * for some devices, or 0 if it does not.
1031 */
1032bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
1033{
1034 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
1035}
1036
3a090284 1037int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
ba20bb12 1038{
8b8c3c78
SS
1039 int ret;
1040
f8457c2d
PW
1041 if (!pwrdm || !arch_pwrdm)
1042 return -EINVAL;
1043
1044 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
8b8c3c78
SS
1045 if (!ret)
1046 ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
1047
1048 return ret;
ba20bb12
PDS
1049}
1050
3a090284
PW
1051int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
1052{
1053 int ret;
1054
1055 pwrdm_lock(pwrdm);
1056 ret = pwrdm_state_switch_nolock(pwrdm);
1057 pwrdm_unlock(pwrdm);
1058
1059 return ret;
1060}
1061
e0555489 1062int pwrdm_pre_transition(struct powerdomain *pwrdm)
ba20bb12 1063{
e0555489
KH
1064 if (pwrdm)
1065 _pwrdm_pre_transition_cb(pwrdm, NULL);
1066 else
1067 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
1068
ba20bb12
PDS
1069 return 0;
1070}
1071
e0555489 1072int pwrdm_post_transition(struct powerdomain *pwrdm)
ba20bb12 1073{
e0555489
KH
1074 if (pwrdm)
1075 _pwrdm_post_transition_cb(pwrdm, NULL);
1076 else
1077 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
1078
ba20bb12
PDS
1079 return 0;
1080}
7f595674 1081
c4978fba
PW
1082/**
1083 * omap_set_pwrdm_state - change a powerdomain's current power state
1084 * @pwrdm: struct powerdomain * to change the power state of
1085 * @pwrst: power state to change to
1086 *
1087 * Change the current hardware power state of the powerdomain
1088 * represented by @pwrdm to the power state represented by @pwrst.
1089 * Returns -EINVAL if @pwrdm is null or invalid or if the
1090 * powerdomain's current power state could not be read, or returns 0
1091 * upon success or if @pwrdm does not support @pwrst or any
1092 * lower-power state. XXX Should not return 0 if the @pwrdm does not
1093 * support @pwrst or any lower-power state: this should be an error.
1094 */
1095int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 pwrst)
1096{
bd70f6eb
PW
1097 u8 next_pwrst, sleep_switch;
1098 int curr_pwrst;
c4978fba
PW
1099 int ret = 0;
1100 bool hwsup = false;
1101
1102 if (!pwrdm || IS_ERR(pwrdm))
1103 return -EINVAL;
1104
1105 while (!(pwrdm->pwrsts & (1 << pwrst))) {
1106 if (pwrst == PWRDM_POWER_OFF)
1107 return ret;
1108 pwrst--;
1109 }
1110
3a090284
PW
1111 pwrdm_lock(pwrdm);
1112
c4978fba 1113 curr_pwrst = pwrdm_read_pwrst(pwrdm);
bd70f6eb
PW
1114 if (curr_pwrst < 0) {
1115 ret = -EINVAL;
1116 goto osps_out;
1117 }
1118
c4978fba
PW
1119 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
1120 if (curr_pwrst == pwrst && next_pwrst == pwrst)
3a090284 1121 goto osps_out;
c4978fba
PW
1122
1123 sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
1124 pwrst, &hwsup);
c4978fba
PW
1125
1126 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
1127 if (ret)
1128 pr_err("%s: unable to set power state of powerdomain: %s\n",
1129 __func__, pwrdm->name);
1130
1131 _pwrdm_restore_clkdm_state(pwrdm, sleep_switch, hwsup);
1132
3a090284
PW
1133osps_out:
1134 pwrdm_unlock(pwrdm);
1135
c4978fba
PW
1136 return ret;
1137}
1138
7f595674
KH
1139/**
1140 * pwrdm_get_context_loss_count - get powerdomain's context loss count
1141 * @pwrdm: struct powerdomain * to wait for
1142 *
1143 * Context loss count is the sum of powerdomain off-mode counter, the
fc013873 1144 * logic off counter and the per-bank memory off counter. Returns negative
7f595674
KH
1145 * (and WARNs) upon error, otherwise, returns the context loss count.
1146 */
fc013873 1147int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
7f595674
KH
1148{
1149 int i, count;
1150
1151 if (!pwrdm) {
1152 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
fc013873 1153 return -ENODEV;
7f595674
KH
1154 }
1155
1156 count = pwrdm->state_counter[PWRDM_POWER_OFF];
1157 count += pwrdm->ret_logic_off_counter;
1158
1159 for (i = 0; i < pwrdm->banks; i++)
1160 count += pwrdm->ret_mem_off_counter[i];
1161
fc013873
TV
1162 /*
1163 * Context loss count has to be a non-negative value. Clear the sign
1164 * bit to get a value range from 0 to INT_MAX.
1165 */
1166 count &= INT_MAX;
1167
1168 pr_debug("powerdomain: %s: context loss count = %d\n",
7f595674
KH
1169 pwrdm->name, count);
1170
1171 return count;
1172}
694606c4
PW
1173
1174/**
1175 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1176 * @pwrdm: struct powerdomain *
1177 *
1178 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1179 * can lose either memory or logic context or if @pwrdm is invalid, or
1180 * returns 0 otherwise. This function is not concerned with how the
1181 * powerdomain registers are programmed (i.e., to go off or not); it's
1182 * concerned with whether it's ever possible for this powerdomain to
1183 * go off while some other part of the chip is active. This function
1184 * assumes that every powerdomain can go to either ON or INACTIVE.
1185 */
1186bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
1187{
1188 int i;
1189
62f0f39b 1190 if (!pwrdm) {
694606c4
PW
1191 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1192 __func__);
1193 return 1;
1194 }
1195
1196 if (pwrdm->pwrsts & PWRSTS_OFF)
1197 return 1;
1198
1199 if (pwrdm->pwrsts & PWRSTS_RET) {
1200 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1201 return 1;
1202
1203 for (i = 0; i < pwrdm->banks; i++)
1204 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1205 return 1;
1206 }
1207
1208 for (i = 0; i < pwrdm->banks; i++)
1209 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1210 return 1;
1211
1212 return 0;
1213}
This page took 0.385879 seconds and 5 git commands to generate.