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