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