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