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