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