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/cpu_pm.h>
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/list.h>
21 #include <linux/errno.h>
22 #include <linux/string.h>
23 #include <linux/spinlock.h>
24 #include <trace/events/power.h>
25 
26 #include "cm2xxx_3xxx.h"
27 #include "prcm44xx.h"
28 #include "cm44xx.h"
29 #include "prm2xxx_3xxx.h"
30 #include "prm44xx.h"
31 
32 #include <asm/cpu.h>
33 
34 #include "powerdomain.h"
35 #include "clockdomain.h"
36 #include "voltage.h"
37 
38 #include "soc.h"
39 #include "pm.h"
40 
41 #define PWRDM_TRACE_STATES_FLAG	(1<<31)
42 
43 void pwrdms_save_context(void);
44 void pwrdms_restore_context(void);
45 
46 enum {
47 	PWRDM_STATE_NOW = 0,
48 	PWRDM_STATE_PREV,
49 };
50 
51 /*
52  * Types of sleep_switch used internally in omap_set_pwrdm_state()
53  * and its associated static functions
54  *
55  * XXX Better documentation is needed here
56  */
57 #define ALREADYACTIVE_SWITCH		0
58 #define FORCEWAKEUP_SWITCH		1
59 #define LOWPOWERSTATE_SWITCH		2
60 
61 /* pwrdm_list contains all registered struct powerdomains */
62 static LIST_HEAD(pwrdm_list);
63 
64 static struct pwrdm_ops *arch_pwrdm;
65 
66 /* Private functions */
67 
68 static struct powerdomain *_pwrdm_lookup(const char *name)
69 {
70 	struct powerdomain *pwrdm, *temp_pwrdm;
71 
72 	pwrdm = NULL;
73 
74 	list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
75 		if (!strcmp(name, temp_pwrdm->name)) {
76 			pwrdm = temp_pwrdm;
77 			break;
78 		}
79 	}
80 
81 	return pwrdm;
82 }
83 
84 /**
85  * _pwrdm_register - register a powerdomain
86  * @pwrdm: struct powerdomain * to register
87  *
88  * Adds a powerdomain to the internal powerdomain list.  Returns
89  * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
90  * already registered by the provided name, or 0 upon success.
91  */
92 static int _pwrdm_register(struct powerdomain *pwrdm)
93 {
94 	int i;
95 	struct voltagedomain *voltdm;
96 
97 	if (!pwrdm || !pwrdm->name)
98 		return -EINVAL;
99 
100 	if (cpu_is_omap44xx() &&
101 	    pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
102 		pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
103 		       pwrdm->name);
104 		return -EINVAL;
105 	}
106 
107 	if (_pwrdm_lookup(pwrdm->name))
108 		return -EEXIST;
109 
110 	if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
111 		if (!arch_pwrdm->pwrdm_has_voltdm())
112 			goto skip_voltdm;
113 
114 	voltdm = voltdm_lookup(pwrdm->voltdm.name);
115 	if (!voltdm) {
116 		pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
117 		       pwrdm->name, pwrdm->voltdm.name);
118 		return -EINVAL;
119 	}
120 	pwrdm->voltdm.ptr = voltdm;
121 	INIT_LIST_HEAD(&pwrdm->voltdm_node);
122 skip_voltdm:
123 	spin_lock_init(&pwrdm->_lock);
124 
125 	list_add(&pwrdm->node, &pwrdm_list);
126 
127 	/* Initialize the powerdomain's state counter */
128 	for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
129 		pwrdm->state_counter[i] = 0;
130 
131 	pwrdm->ret_logic_off_counter = 0;
132 	for (i = 0; i < pwrdm->banks; i++)
133 		pwrdm->ret_mem_off_counter[i] = 0;
134 
135 	if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
136 		arch_pwrdm->pwrdm_wait_transition(pwrdm);
137 	pwrdm->state = pwrdm_read_pwrst(pwrdm);
138 	pwrdm->state_counter[pwrdm->state] = 1;
139 
140 	pr_debug("powerdomain: registered %s\n", pwrdm->name);
141 
142 	return 0;
143 }
144 
145 static void _update_logic_membank_counters(struct powerdomain *pwrdm)
146 {
147 	int i;
148 	u8 prev_logic_pwrst, prev_mem_pwrst;
149 
150 	prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
151 	if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
152 	    (prev_logic_pwrst == PWRDM_POWER_OFF))
153 		pwrdm->ret_logic_off_counter++;
154 
155 	for (i = 0; i < pwrdm->banks; i++) {
156 		prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
157 
158 		if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
159 		    (prev_mem_pwrst == PWRDM_POWER_OFF))
160 			pwrdm->ret_mem_off_counter[i]++;
161 	}
162 }
163 
164 static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
165 {
166 
167 	int prev, next, state, trace_state = 0;
168 
169 	if (pwrdm == NULL)
170 		return -EINVAL;
171 
172 	state = pwrdm_read_pwrst(pwrdm);
173 
174 	switch (flag) {
175 	case PWRDM_STATE_NOW:
176 		prev = pwrdm->state;
177 		break;
178 	case PWRDM_STATE_PREV:
179 		prev = pwrdm_read_prev_pwrst(pwrdm);
180 		if (pwrdm->state != prev)
181 			pwrdm->state_counter[prev]++;
182 		if (prev == PWRDM_POWER_RET)
183 			_update_logic_membank_counters(pwrdm);
184 		/*
185 		 * If the power domain did not hit the desired state,
186 		 * generate a trace event with both the desired and hit states
187 		 */
188 		next = pwrdm_read_next_pwrst(pwrdm);
189 		if (next != prev) {
190 			trace_state = (PWRDM_TRACE_STATES_FLAG |
191 				       ((next & OMAP_POWERSTATE_MASK) << 8) |
192 				       ((prev & OMAP_POWERSTATE_MASK) << 0));
193 			trace_power_domain_target_rcuidle(pwrdm->name,
194 							  trace_state,
195 							  raw_smp_processor_id());
196 		}
197 		break;
198 	default:
199 		return -EINVAL;
200 	}
201 
202 	if (state != prev)
203 		pwrdm->state_counter[state]++;
204 
205 	pm_dbg_update_time(pwrdm, prev);
206 
207 	pwrdm->state = state;
208 
209 	return 0;
210 }
211 
212 static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
213 {
214 	pwrdm_clear_all_prev_pwrst(pwrdm);
215 	_pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
216 	return 0;
217 }
218 
219 static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
220 {
221 	_pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
222 	return 0;
223 }
224 
225 /**
226  * _pwrdm_save_clkdm_state_and_activate - prepare for power state change
227  * @pwrdm: struct powerdomain * to operate on
228  * @curr_pwrst: current power state of @pwrdm
229  * @pwrst: power state to switch to
230  *
231  * Determine whether the powerdomain needs to be turned on before
232  * attempting to switch power states.  Called by
233  * omap_set_pwrdm_state().  NOTE that if the powerdomain contains
234  * multiple clockdomains, this code assumes that the first clockdomain
235  * supports software-supervised wakeup mode - potentially a problem.
236  * Returns the power state switch mode currently in use (see the
237  * "Types of sleep_switch" comment above).
238  */
239 static u8 _pwrdm_save_clkdm_state_and_activate(struct powerdomain *pwrdm,
240 					       u8 curr_pwrst, u8 pwrst)
241 {
242 	u8 sleep_switch;
243 
244 	if (curr_pwrst < PWRDM_POWER_ON) {
245 		if (curr_pwrst > pwrst &&
246 		    pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
247 		    arch_pwrdm->pwrdm_set_lowpwrstchange) {
248 			sleep_switch = LOWPOWERSTATE_SWITCH;
249 		} else {
250 			clkdm_deny_idle_nolock(pwrdm->pwrdm_clkdms[0]);
251 			sleep_switch = FORCEWAKEUP_SWITCH;
252 		}
253 	} else {
254 		sleep_switch = ALREADYACTIVE_SWITCH;
255 	}
256 
257 	return sleep_switch;
258 }
259 
260 /**
261  * _pwrdm_restore_clkdm_state - restore the clkdm hwsup state after pwrst change
262  * @pwrdm: struct powerdomain * to operate on
263  * @sleep_switch: return value from _pwrdm_save_clkdm_state_and_activate()
264  *
265  * Restore the clockdomain state perturbed by
266  * _pwrdm_save_clkdm_state_and_activate(), and call the power state
267  * bookkeeping code.  Called by omap_set_pwrdm_state().  NOTE that if
268  * the powerdomain contains multiple clockdomains, this assumes that
269  * the first associated clockdomain supports either
270  * hardware-supervised idle control in the register, or
271  * software-supervised sleep.  No return value.
272  */
273 static void _pwrdm_restore_clkdm_state(struct powerdomain *pwrdm,
274 				       u8 sleep_switch)
275 {
276 	switch (sleep_switch) {
277 	case FORCEWAKEUP_SWITCH:
278 		clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
279 		break;
280 	case LOWPOWERSTATE_SWITCH:
281 		if (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
282 		    arch_pwrdm->pwrdm_set_lowpwrstchange)
283 			arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
284 		pwrdm_state_switch_nolock(pwrdm);
285 		break;
286 	}
287 }
288 
289 /* Public functions */
290 
291 /**
292  * pwrdm_register_platform_funcs - register powerdomain implementation fns
293  * @po: func pointers for arch specific implementations
294  *
295  * Register the list of function pointers used to implement the
296  * powerdomain functions on different OMAP SoCs.  Should be called
297  * before any other pwrdm_register*() function.  Returns -EINVAL if
298  * @po is null, -EEXIST if platform functions have already been
299  * registered, or 0 upon success.
300  */
301 int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
302 {
303 	if (!po)
304 		return -EINVAL;
305 
306 	if (arch_pwrdm)
307 		return -EEXIST;
308 
309 	arch_pwrdm = po;
310 
311 	return 0;
312 }
313 
314 /**
315  * pwrdm_register_pwrdms - register SoC powerdomains
316  * @ps: pointer to an array of struct powerdomain to register
317  *
318  * Register the powerdomains available on a particular OMAP SoC.  Must
319  * be called after pwrdm_register_platform_funcs().  May be called
320  * multiple times.  Returns -EACCES if called before
321  * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
322  * null; or 0 upon success.
323  */
324 int pwrdm_register_pwrdms(struct powerdomain **ps)
325 {
326 	struct powerdomain **p = NULL;
327 
328 	if (!arch_pwrdm)
329 		return -EEXIST;
330 
331 	if (!ps)
332 		return -EINVAL;
333 
334 	for (p = ps; *p; p++)
335 		_pwrdm_register(*p);
336 
337 	return 0;
338 }
339 
340 static int cpu_notifier(struct notifier_block *nb, unsigned long cmd, void *v)
341 {
342 	switch (cmd) {
343 	case CPU_CLUSTER_PM_ENTER:
344 		if (enable_off_mode)
345 			pwrdms_save_context();
346 		break;
347 	case CPU_CLUSTER_PM_EXIT:
348 		if (enable_off_mode)
349 			pwrdms_restore_context();
350 		break;
351 	}
352 
353 	return NOTIFY_OK;
354 }
355 
356 /**
357  * pwrdm_complete_init - set up the powerdomain layer
358  *
359  * Do whatever is necessary to initialize registered powerdomains and
360  * powerdomain code.  Currently, this programs the next power state
361  * for each powerdomain to ON.  This prevents powerdomains from
362  * unexpectedly losing context or entering high wakeup latency modes
363  * with non-power-management-enabled kernels.  Must be called after
364  * pwrdm_register_pwrdms().  Returns -EACCES if called before
365  * pwrdm_register_pwrdms(), or 0 upon success.
366  */
367 int pwrdm_complete_init(void)
368 {
369 	struct powerdomain *temp_p;
370 	static struct notifier_block nb;
371 
372 	if (list_empty(&pwrdm_list))
373 		return -EACCES;
374 
375 	list_for_each_entry(temp_p, &pwrdm_list, node)
376 		pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
377 
378 	/* Only AM43XX can lose pwrdm context during rtc-ddr suspend */
379 	if (soc_is_am43xx()) {
380 		nb.notifier_call = cpu_notifier;
381 		cpu_pm_register_notifier(&nb);
382 	}
383 
384 	return 0;
385 }
386 
387 /**
388  * pwrdm_lock - acquire a Linux spinlock on a powerdomain
389  * @pwrdm: struct powerdomain * to lock
390  *
391  * Acquire the powerdomain spinlock on @pwrdm.  No return value.
392  */
393 void pwrdm_lock(struct powerdomain *pwrdm)
394 	__acquires(&pwrdm->_lock)
395 {
396 	spin_lock_irqsave(&pwrdm->_lock, pwrdm->_lock_flags);
397 }
398 
399 /**
400  * pwrdm_unlock - release a Linux spinlock on a powerdomain
401  * @pwrdm: struct powerdomain * to unlock
402  *
403  * Release the powerdomain spinlock on @pwrdm.  No return value.
404  */
405 void pwrdm_unlock(struct powerdomain *pwrdm)
406 	__releases(&pwrdm->_lock)
407 {
408 	spin_unlock_irqrestore(&pwrdm->_lock, pwrdm->_lock_flags);
409 }
410 
411 /**
412  * pwrdm_lookup - look up a powerdomain by name, return a pointer
413  * @name: name of powerdomain
414  *
415  * Find a registered powerdomain by its name @name.  Returns a pointer
416  * to the struct powerdomain if found, or NULL otherwise.
417  */
418 struct powerdomain *pwrdm_lookup(const char *name)
419 {
420 	struct powerdomain *pwrdm;
421 
422 	if (!name)
423 		return NULL;
424 
425 	pwrdm = _pwrdm_lookup(name);
426 
427 	return pwrdm;
428 }
429 
430 /**
431  * pwrdm_for_each - call function on each registered clockdomain
432  * @fn: callback function *
433  *
434  * Call the supplied function @fn for each registered powerdomain.
435  * The callback function @fn can return anything but 0 to bail out
436  * early from the iterator.  Returns the last return value of the
437  * callback function, which should be 0 for success or anything else
438  * to indicate failure; or -EINVAL if the function pointer is null.
439  */
440 int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
441 		   void *user)
442 {
443 	struct powerdomain *temp_pwrdm;
444 	int ret = 0;
445 
446 	if (!fn)
447 		return -EINVAL;
448 
449 	list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
450 		ret = (*fn)(temp_pwrdm, user);
451 		if (ret)
452 			break;
453 	}
454 
455 	return ret;
456 }
457 
458 /**
459  * pwrdm_add_clkdm - add a clockdomain to a powerdomain
460  * @pwrdm: struct powerdomain * to add the clockdomain to
461  * @clkdm: struct clockdomain * to associate with a powerdomain
462  *
463  * Associate the clockdomain @clkdm with a powerdomain @pwrdm.  This
464  * enables the use of pwrdm_for_each_clkdm().  Returns -EINVAL if
465  * presented with invalid pointers; -ENOMEM if memory could not be allocated;
466  * or 0 upon success.
467  */
468 int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
469 {
470 	int i;
471 	int ret = -EINVAL;
472 
473 	if (!pwrdm || !clkdm)
474 		return -EINVAL;
475 
476 	pr_debug("powerdomain: %s: associating clockdomain %s\n",
477 		 pwrdm->name, clkdm->name);
478 
479 	for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
480 		if (!pwrdm->pwrdm_clkdms[i])
481 			break;
482 #ifdef DEBUG
483 		if (pwrdm->pwrdm_clkdms[i] == clkdm) {
484 			ret = -EINVAL;
485 			goto pac_exit;
486 		}
487 #endif
488 	}
489 
490 	if (i == PWRDM_MAX_CLKDMS) {
491 		pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
492 			 pwrdm->name, clkdm->name);
493 		WARN_ON(1);
494 		ret = -ENOMEM;
495 		goto pac_exit;
496 	}
497 
498 	pwrdm->pwrdm_clkdms[i] = clkdm;
499 
500 	ret = 0;
501 
502 pac_exit:
503 	return ret;
504 }
505 
506 /**
507  * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
508  * @pwrdm: struct powerdomain *
509  *
510  * Return the number of controllable memory banks in powerdomain @pwrdm,
511  * starting with 1.  Returns -EINVAL if the powerdomain pointer is null.
512  */
513 int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
514 {
515 	if (!pwrdm)
516 		return -EINVAL;
517 
518 	return pwrdm->banks;
519 }
520 
521 /**
522  * pwrdm_set_next_pwrst - set next powerdomain power state
523  * @pwrdm: struct powerdomain * to set
524  * @pwrst: one of the PWRDM_POWER_* macros
525  *
526  * Set the powerdomain @pwrdm's next power state to @pwrst.  The powerdomain
527  * may not enter this state immediately if the preconditions for this state
528  * have not been satisfied.  Returns -EINVAL if the powerdomain pointer is
529  * null or if the power state is invalid for the powerdomin, or returns 0
530  * upon success.
531  */
532 int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
533 {
534 	int ret = -EINVAL;
535 
536 	if (!pwrdm)
537 		return -EINVAL;
538 
539 	if (!(pwrdm->pwrsts & (1 << pwrst)))
540 		return -EINVAL;
541 
542 	pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
543 		 pwrdm->name, pwrst);
544 
545 	if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
546 		/* Trace the pwrdm desired target state */
547 		trace_power_domain_target_rcuidle(pwrdm->name, pwrst,
548 						  raw_smp_processor_id());
549 		/* Program the pwrdm desired target state */
550 		ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
551 	}
552 
553 	return ret;
554 }
555 
556 /**
557  * pwrdm_read_next_pwrst - get next powerdomain power state
558  * @pwrdm: struct powerdomain * to get power state
559  *
560  * Return the powerdomain @pwrdm's next power state.  Returns -EINVAL
561  * if the powerdomain pointer is null or returns the next power state
562  * upon success.
563  */
564 int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
565 {
566 	int ret = -EINVAL;
567 
568 	if (!pwrdm)
569 		return -EINVAL;
570 
571 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
572 		ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
573 
574 	return ret;
575 }
576 
577 /**
578  * pwrdm_read_pwrst - get current powerdomain power state
579  * @pwrdm: struct powerdomain * to get power state
580  *
581  * Return the powerdomain @pwrdm's current power state.	Returns -EINVAL
582  * if the powerdomain pointer is null or returns the current power state
583  * upon success. Note that if the power domain only supports the ON state
584  * then just return ON as the current state.
585  */
586 int pwrdm_read_pwrst(struct powerdomain *pwrdm)
587 {
588 	int ret = -EINVAL;
589 
590 	if (!pwrdm)
591 		return -EINVAL;
592 
593 	if (pwrdm->pwrsts == PWRSTS_ON)
594 		return PWRDM_POWER_ON;
595 
596 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
597 		ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
598 
599 	return ret;
600 }
601 
602 /**
603  * pwrdm_read_prev_pwrst - get previous powerdomain power state
604  * @pwrdm: struct powerdomain * to get previous power state
605  *
606  * Return the powerdomain @pwrdm's previous power state.  Returns -EINVAL
607  * if the powerdomain pointer is null or returns the previous power state
608  * upon success.
609  */
610 int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
611 {
612 	int ret = -EINVAL;
613 
614 	if (!pwrdm)
615 		return -EINVAL;
616 
617 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
618 		ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
619 
620 	return ret;
621 }
622 
623 /**
624  * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
625  * @pwrdm: struct powerdomain * to set
626  * @pwrst: one of the PWRDM_POWER_* macros
627  *
628  * Set the next power state @pwrst that the logic portion of the
629  * powerdomain @pwrdm will enter when the powerdomain enters retention.
630  * This will be either RETENTION or OFF, if supported.  Returns
631  * -EINVAL if the powerdomain pointer is null or the target power
632  * state is not not supported, or returns 0 upon success.
633  */
634 int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
635 {
636 	int ret = -EINVAL;
637 
638 	if (!pwrdm)
639 		return -EINVAL;
640 
641 	if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
642 		return -EINVAL;
643 
644 	pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
645 		 pwrdm->name, pwrst);
646 
647 	if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
648 		ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
649 
650 	return ret;
651 }
652 
653 /**
654  * pwrdm_set_mem_onst - set memory power state while powerdomain ON
655  * @pwrdm: struct powerdomain * to set
656  * @bank: memory bank number to set (0-3)
657  * @pwrst: one of the PWRDM_POWER_* macros
658  *
659  * Set the next power state @pwrst that memory bank @bank of the
660  * powerdomain @pwrdm will enter when the powerdomain enters the ON
661  * state.  @bank will be a number from 0 to 3, and represents different
662  * types of memory, depending on the powerdomain.  Returns -EINVAL if
663  * the powerdomain pointer is null or the target power state is not
664  * not supported for this memory bank, -EEXIST if the target memory
665  * bank does not exist or is not controllable, or returns 0 upon
666  * success.
667  */
668 int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
669 {
670 	int ret = -EINVAL;
671 
672 	if (!pwrdm)
673 		return -EINVAL;
674 
675 	if (pwrdm->banks < (bank + 1))
676 		return -EEXIST;
677 
678 	if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
679 		return -EINVAL;
680 
681 	pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
682 		 pwrdm->name, bank, pwrst);
683 
684 	if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
685 		ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
686 
687 	return ret;
688 }
689 
690 /**
691  * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
692  * @pwrdm: struct powerdomain * to set
693  * @bank: memory bank number to set (0-3)
694  * @pwrst: one of the PWRDM_POWER_* macros
695  *
696  * Set the next power state @pwrst that memory bank @bank of the
697  * powerdomain @pwrdm will enter when the powerdomain enters the
698  * RETENTION state.  Bank will be a number from 0 to 3, and represents
699  * different types of memory, depending on the powerdomain.  @pwrst
700  * will be either RETENTION or OFF, if supported.  Returns -EINVAL if
701  * the powerdomain pointer is null or the target power state is not
702  * not supported for this memory bank, -EEXIST if the target memory
703  * bank does not exist or is not controllable, or returns 0 upon
704  * success.
705  */
706 int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
707 {
708 	int ret = -EINVAL;
709 
710 	if (!pwrdm)
711 		return -EINVAL;
712 
713 	if (pwrdm->banks < (bank + 1))
714 		return -EEXIST;
715 
716 	if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
717 		return -EINVAL;
718 
719 	pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
720 		 pwrdm->name, bank, pwrst);
721 
722 	if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
723 		ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
724 
725 	return ret;
726 }
727 
728 /**
729  * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
730  * @pwrdm: struct powerdomain * to get current logic retention power state
731  *
732  * Return the power state that the logic portion of powerdomain @pwrdm
733  * will enter when the powerdomain enters retention.  Returns -EINVAL
734  * if the powerdomain pointer is null or returns the logic retention
735  * power state upon success.
736  */
737 int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
738 {
739 	int ret = -EINVAL;
740 
741 	if (!pwrdm)
742 		return -EINVAL;
743 
744 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
745 		ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
746 
747 	return ret;
748 }
749 
750 /**
751  * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
752  * @pwrdm: struct powerdomain * to get previous logic power state
753  *
754  * Return the powerdomain @pwrdm's previous logic power state.  Returns
755  * -EINVAL if the powerdomain pointer is null or returns the previous
756  * logic power state upon success.
757  */
758 int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
759 {
760 	int ret = -EINVAL;
761 
762 	if (!pwrdm)
763 		return -EINVAL;
764 
765 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
766 		ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
767 
768 	return ret;
769 }
770 
771 /**
772  * pwrdm_read_logic_retst - get next powerdomain logic power state
773  * @pwrdm: struct powerdomain * to get next logic power state
774  *
775  * Return the powerdomain pwrdm's logic power state.  Returns -EINVAL
776  * if the powerdomain pointer is null or returns the next logic
777  * power state upon success.
778  */
779 int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
780 {
781 	int ret = -EINVAL;
782 
783 	if (!pwrdm)
784 		return -EINVAL;
785 
786 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
787 		ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
788 
789 	return ret;
790 }
791 
792 /**
793  * pwrdm_read_mem_pwrst - get current memory bank power state
794  * @pwrdm: struct powerdomain * to get current memory bank power state
795  * @bank: memory bank number (0-3)
796  *
797  * Return the powerdomain @pwrdm's current memory power state for bank
798  * @bank.  Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
799  * the target memory bank does not exist or is not controllable, or
800  * returns the current memory power state upon success.
801  */
802 int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
803 {
804 	int ret = -EINVAL;
805 
806 	if (!pwrdm)
807 		return ret;
808 
809 	if (pwrdm->banks < (bank + 1))
810 		return ret;
811 
812 	if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
813 		bank = 1;
814 
815 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
816 		ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
817 
818 	return ret;
819 }
820 
821 /**
822  * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
823  * @pwrdm: struct powerdomain * to get previous memory bank power state
824  * @bank: memory bank number (0-3)
825  *
826  * Return the powerdomain @pwrdm's previous memory power state for
827  * bank @bank.  Returns -EINVAL if the powerdomain pointer is null,
828  * -EEXIST if the target memory bank does not exist or is not
829  * controllable, or returns the previous memory power state upon
830  * success.
831  */
832 int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
833 {
834 	int ret = -EINVAL;
835 
836 	if (!pwrdm)
837 		return ret;
838 
839 	if (pwrdm->banks < (bank + 1))
840 		return ret;
841 
842 	if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
843 		bank = 1;
844 
845 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
846 		ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
847 
848 	return ret;
849 }
850 
851 /**
852  * pwrdm_read_mem_retst - get next memory bank power state
853  * @pwrdm: struct powerdomain * to get mext memory bank power state
854  * @bank: memory bank number (0-3)
855  *
856  * Return the powerdomain pwrdm's next memory power state for bank
857  * x.  Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
858  * the target memory bank does not exist or is not controllable, or
859  * returns the next memory power state upon success.
860  */
861 int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
862 {
863 	int ret = -EINVAL;
864 
865 	if (!pwrdm)
866 		return ret;
867 
868 	if (pwrdm->banks < (bank + 1))
869 		return ret;
870 
871 	if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
872 		ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
873 
874 	return ret;
875 }
876 
877 /**
878  * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
879  * @pwrdm: struct powerdomain * to clear
880  *
881  * Clear the powerdomain's previous power state register @pwrdm.
882  * Clears the entire register, including logic and memory bank
883  * previous power states.  Returns -EINVAL if the powerdomain pointer
884  * is null, or returns 0 upon success.
885  */
886 int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
887 {
888 	int ret = -EINVAL;
889 
890 	if (!pwrdm)
891 		return ret;
892 
893 	/*
894 	 * XXX should get the powerdomain's current state here;
895 	 * warn & fail if it is not ON.
896 	 */
897 
898 	pr_debug("powerdomain: %s: clearing previous power state reg\n",
899 		 pwrdm->name);
900 
901 	if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
902 		ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
903 
904 	return ret;
905 }
906 
907 /**
908  * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
909  * @pwrdm: struct powerdomain *
910  *
911  * Enable automatic context save-and-restore upon power state change
912  * for some devices in the powerdomain @pwrdm.  Warning: this only
913  * affects a subset of devices in a powerdomain; check the TRM
914  * closely.  Returns -EINVAL if the powerdomain pointer is null or if
915  * the powerdomain does not support automatic save-and-restore, or
916  * returns 0 upon success.
917  */
918 int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
919 {
920 	int ret = -EINVAL;
921 
922 	if (!pwrdm)
923 		return ret;
924 
925 	if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
926 		return ret;
927 
928 	pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
929 
930 	if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
931 		ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
932 
933 	return ret;
934 }
935 
936 /**
937  * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
938  * @pwrdm: struct powerdomain *
939  *
940  * Disable automatic context save-and-restore upon power state change
941  * for some devices in the powerdomain @pwrdm.  Warning: this only
942  * affects a subset of devices in a powerdomain; check the TRM
943  * closely.  Returns -EINVAL if the powerdomain pointer is null or if
944  * the powerdomain does not support automatic save-and-restore, or
945  * returns 0 upon success.
946  */
947 int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
948 {
949 	int ret = -EINVAL;
950 
951 	if (!pwrdm)
952 		return ret;
953 
954 	if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
955 		return ret;
956 
957 	pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
958 
959 	if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
960 		ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
961 
962 	return ret;
963 }
964 
965 /**
966  * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
967  * @pwrdm: struct powerdomain *
968  *
969  * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
970  * for some devices, or 0 if it does not.
971  */
972 bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
973 {
974 	return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
975 }
976 
977 int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
978 {
979 	int ret;
980 
981 	if (!pwrdm || !arch_pwrdm)
982 		return -EINVAL;
983 
984 	ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
985 	if (!ret)
986 		ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
987 
988 	return ret;
989 }
990 
991 int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
992 {
993 	int ret;
994 
995 	pwrdm_lock(pwrdm);
996 	ret = pwrdm_state_switch_nolock(pwrdm);
997 	pwrdm_unlock(pwrdm);
998 
999 	return ret;
1000 }
1001 
1002 int pwrdm_pre_transition(struct powerdomain *pwrdm)
1003 {
1004 	if (pwrdm)
1005 		_pwrdm_pre_transition_cb(pwrdm, NULL);
1006 	else
1007 		pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
1008 
1009 	return 0;
1010 }
1011 
1012 int pwrdm_post_transition(struct powerdomain *pwrdm)
1013 {
1014 	if (pwrdm)
1015 		_pwrdm_post_transition_cb(pwrdm, NULL);
1016 	else
1017 		pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
1018 
1019 	return 0;
1020 }
1021 
1022 /**
1023  * pwrdm_get_valid_lp_state() - Find best match deep power state
1024  * @pwrdm:	power domain for which we want to find best match
1025  * @is_logic_state: Are we looking for logic state match here? Should
1026  *		    be one of PWRDM_xxx macro values
1027  * @req_state:	requested power state
1028  *
1029  * Returns: closest match for requested power state. default fallback
1030  * is RET for logic state and ON for power state.
1031  *
1032  * This does a search from the power domain data looking for the
1033  * closest valid power domain state that the hardware can achieve.
1034  * PRCM definitions for PWRSTCTRL allows us to program whatever
1035  * configuration we'd like, and PRCM will actually attempt such
1036  * a transition, however if the powerdomain does not actually support it,
1037  * we endup with a hung system. The valid power domain states are already
1038  * available in our powerdomain data files. So this function tries to do
1039  * the following:
1040  * a) find if we have an exact match to the request - no issues.
1041  * b) else find if a deeper power state is possible.
1042  * c) failing which, it tries to find closest higher power state for the
1043  * request.
1044  */
1045 u8 pwrdm_get_valid_lp_state(struct powerdomain *pwrdm,
1046 			    bool is_logic_state, u8 req_state)
1047 {
1048 	u8 pwrdm_states = is_logic_state ? pwrdm->pwrsts_logic_ret :
1049 			pwrdm->pwrsts;
1050 	/* For logic, ret is highest and others, ON is highest */
1051 	u8 default_pwrst = is_logic_state ? PWRDM_POWER_RET : PWRDM_POWER_ON;
1052 	u8 new_pwrst;
1053 	bool found;
1054 
1055 	/* If it is already supported, nothing to search */
1056 	if (pwrdm_states & BIT(req_state))
1057 		return req_state;
1058 
1059 	if (!req_state)
1060 		goto up_search;
1061 
1062 	/*
1063 	 * So, we dont have a exact match
1064 	 * Can we get a deeper power state match?
1065 	 */
1066 	new_pwrst = req_state - 1;
1067 	found = true;
1068 	while (!(pwrdm_states & BIT(new_pwrst))) {
1069 		/* No match even at OFF? Not available */
1070 		if (new_pwrst == PWRDM_POWER_OFF) {
1071 			found = false;
1072 			break;
1073 		}
1074 		new_pwrst--;
1075 	}
1076 
1077 	if (found)
1078 		goto done;
1079 
1080 up_search:
1081 	/* OK, no deeper ones, can we get a higher match? */
1082 	new_pwrst = req_state + 1;
1083 	while (!(pwrdm_states & BIT(new_pwrst))) {
1084 		if (new_pwrst > PWRDM_POWER_ON) {
1085 			WARN(1, "powerdomain: %s: Fix max powerstate to ON\n",
1086 			     pwrdm->name);
1087 			return PWRDM_POWER_ON;
1088 		}
1089 
1090 		if (new_pwrst == default_pwrst)
1091 			break;
1092 		new_pwrst++;
1093 	}
1094 done:
1095 	return new_pwrst;
1096 }
1097 
1098 /**
1099  * omap_set_pwrdm_state - change a powerdomain's current power state
1100  * @pwrdm: struct powerdomain * to change the power state of
1101  * @pwrst: power state to change to
1102  *
1103  * Change the current hardware power state of the powerdomain
1104  * represented by @pwrdm to the power state represented by @pwrst.
1105  * Returns -EINVAL if @pwrdm is null or invalid or if the
1106  * powerdomain's current power state could not be read, or returns 0
1107  * upon success or if @pwrdm does not support @pwrst or any
1108  * lower-power state.  XXX Should not return 0 if the @pwrdm does not
1109  * support @pwrst or any lower-power state: this should be an error.
1110  */
1111 int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 pwrst)
1112 {
1113 	u8 next_pwrst, sleep_switch;
1114 	int curr_pwrst;
1115 	int ret = 0;
1116 
1117 	if (!pwrdm || IS_ERR(pwrdm))
1118 		return -EINVAL;
1119 
1120 	while (!(pwrdm->pwrsts & (1 << pwrst))) {
1121 		if (pwrst == PWRDM_POWER_OFF)
1122 			return ret;
1123 		pwrst--;
1124 	}
1125 
1126 	pwrdm_lock(pwrdm);
1127 
1128 	curr_pwrst = pwrdm_read_pwrst(pwrdm);
1129 	if (curr_pwrst < 0) {
1130 		ret = -EINVAL;
1131 		goto osps_out;
1132 	}
1133 
1134 	next_pwrst = pwrdm_read_next_pwrst(pwrdm);
1135 	if (curr_pwrst == pwrst && next_pwrst == pwrst)
1136 		goto osps_out;
1137 
1138 	sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
1139 							    pwrst);
1140 
1141 	ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
1142 	if (ret)
1143 		pr_err("%s: unable to set power state of powerdomain: %s\n",
1144 		       __func__, pwrdm->name);
1145 
1146 	_pwrdm_restore_clkdm_state(pwrdm, sleep_switch);
1147 
1148 osps_out:
1149 	pwrdm_unlock(pwrdm);
1150 
1151 	return ret;
1152 }
1153 
1154 /**
1155  * pwrdm_get_context_loss_count - get powerdomain's context loss count
1156  * @pwrdm: struct powerdomain * to wait for
1157  *
1158  * Context loss count is the sum of powerdomain off-mode counter, the
1159  * logic off counter and the per-bank memory off counter.  Returns negative
1160  * (and WARNs) upon error, otherwise, returns the context loss count.
1161  */
1162 int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
1163 {
1164 	int i, count;
1165 
1166 	if (!pwrdm) {
1167 		WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
1168 		return -ENODEV;
1169 	}
1170 
1171 	count = pwrdm->state_counter[PWRDM_POWER_OFF];
1172 	count += pwrdm->ret_logic_off_counter;
1173 
1174 	for (i = 0; i < pwrdm->banks; i++)
1175 		count += pwrdm->ret_mem_off_counter[i];
1176 
1177 	/*
1178 	 * Context loss count has to be a non-negative value. Clear the sign
1179 	 * bit to get a value range from 0 to INT_MAX.
1180 	 */
1181 	count &= INT_MAX;
1182 
1183 	pr_debug("powerdomain: %s: context loss count = %d\n",
1184 		 pwrdm->name, count);
1185 
1186 	return count;
1187 }
1188 
1189 /**
1190  * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1191  * @pwrdm: struct powerdomain *
1192  *
1193  * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1194  * can lose either memory or logic context or if @pwrdm is invalid, or
1195  * returns 0 otherwise.  This function is not concerned with how the
1196  * powerdomain registers are programmed (i.e., to go off or not); it's
1197  * concerned with whether it's ever possible for this powerdomain to
1198  * go off while some other part of the chip is active.  This function
1199  * assumes that every powerdomain can go to either ON or INACTIVE.
1200  */
1201 bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
1202 {
1203 	int i;
1204 
1205 	if (!pwrdm) {
1206 		pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1207 			 __func__);
1208 		return 1;
1209 	}
1210 
1211 	if (pwrdm->pwrsts & PWRSTS_OFF)
1212 		return 1;
1213 
1214 	if (pwrdm->pwrsts & PWRSTS_RET) {
1215 		if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1216 			return 1;
1217 
1218 		for (i = 0; i < pwrdm->banks; i++)
1219 			if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1220 				return 1;
1221 	}
1222 
1223 	for (i = 0; i < pwrdm->banks; i++)
1224 		if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1225 			return 1;
1226 
1227 	return 0;
1228 }
1229 
1230 /**
1231  * pwrdm_save_context - save powerdomain registers
1232  *
1233  * Register state is going to be lost due to a suspend or hibernate
1234  * event. Save the powerdomain registers.
1235  */
1236 static int pwrdm_save_context(struct powerdomain *pwrdm, void *unused)
1237 {
1238 	if (arch_pwrdm && arch_pwrdm->pwrdm_save_context)
1239 		arch_pwrdm->pwrdm_save_context(pwrdm);
1240 	return 0;
1241 }
1242 
1243 /**
1244  * pwrdm_save_context - restore powerdomain registers
1245  *
1246  * Restore powerdomain control registers after a suspend or resume
1247  * event.
1248  */
1249 static int pwrdm_restore_context(struct powerdomain *pwrdm, void *unused)
1250 {
1251 	if (arch_pwrdm && arch_pwrdm->pwrdm_restore_context)
1252 		arch_pwrdm->pwrdm_restore_context(pwrdm);
1253 	return 0;
1254 }
1255 
1256 static int pwrdm_lost_power(struct powerdomain *pwrdm, void *unused)
1257 {
1258 	int state;
1259 
1260 	/*
1261 	 * Power has been lost across all powerdomains, increment the
1262 	 * counter.
1263 	 */
1264 
1265 	state = pwrdm_read_pwrst(pwrdm);
1266 	if (state != PWRDM_POWER_OFF) {
1267 		pwrdm->state_counter[state]++;
1268 		pwrdm->state_counter[PWRDM_POWER_OFF]++;
1269 	}
1270 	pwrdm->state = state;
1271 
1272 	return 0;
1273 }
1274 
1275 void pwrdms_save_context(void)
1276 {
1277 	pwrdm_for_each(pwrdm_save_context, NULL);
1278 }
1279 
1280 void pwrdms_restore_context(void)
1281 {
1282 	pwrdm_for_each(pwrdm_restore_context, NULL);
1283 }
1284 
1285 void pwrdms_lost_power(void)
1286 {
1287 	pwrdm_for_each(pwrdm_lost_power, NULL);
1288 }
1289