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