blob: 896cb4c5eb1a1c766453a04470c776859d842943 [file] [log] [blame]
Paul Walmsleyad67ef62008-08-19 11:08:40 +03001/*
2 * OMAP powerdomain control
3 *
Paul Walmsley129c65e2011-09-14 16:01:21 -06004 * Copyright (C) 2007-2008, 2011 Texas Instruments, Inc.
Paul Walmsley694606c2011-03-07 19:28:15 -07005 * Copyright (C) 2007-2011 Nokia Corporation
Paul Walmsleyad67ef62008-08-19 11:08:40 +03006 *
7 * Written by Paul Walmsley
Abhijit Pagare3a759f02010-01-26 20:12:53 -07008 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
Paul Walmsley4788da22010-05-18 20:24:05 -06009 * State counting code by Tero Kristo <tero.kristo@nokia.com>
Abhijit Pagare3a759f02010-01-26 20:12:53 -070010 *
Paul Walmsleyad67ef62008-08-19 11:08:40 +030011 * 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 */
Paul Walmsley33903eb2009-12-08 16:33:10 -070015#undef DEBUG
Paul Walmsleyad67ef62008-08-19 11:08:40 +030016
17#include <linux/kernel.h>
Paul Walmsleyad67ef62008-08-19 11:08:40 +030018#include <linux/types.h>
Paul Walmsleyad67ef62008-08-19 11:08:40 +030019#include <linux/list.h>
20#include <linux/errno.h>
Rajendra Nayak9b7fc902010-12-21 20:01:19 -070021#include <linux/string.h>
Jean Pihet5e7c58d2011-03-03 11:25:43 +010022#include <trace/events/power.h>
23
Paul Walmsley59fb6592010-12-21 15:30:55 -070024#include "cm2xxx_3xxx.h"
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070025#include "prcm44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -070026#include "cm44xx.h"
27#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -070028#include "prm44xx.h"
Paul Walmsleyad67ef62008-08-19 11:08:40 +030029
Jean Pihet5e7c58d2011-03-03 11:25:43 +010030#include <asm/cpu.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070031#include <plat/cpu.h>
Paul Walmsley72e06d02010-12-21 21:05:16 -070032#include "powerdomain.h"
Paul Walmsley1540f2142010-12-21 21:05:15 -070033#include "clockdomain.h"
Paul Walmsley55ed9692010-01-26 20:12:59 -070034#include <plat/prcm.h>
Paul Walmsleyad67ef62008-08-19 11:08:40 +030035
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +030036#include "pm.h"
37
Jean Pihet5e7c58d2011-03-03 11:25:43 +010038#define PWRDM_TRACE_STATES_FLAG (1<<31)
39
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +030040enum {
41 PWRDM_STATE_NOW = 0,
42 PWRDM_STATE_PREV,
43};
44
Abhijit Pagare3a759f02010-01-26 20:12:53 -070045
Paul Walmsleyad67ef62008-08-19 11:08:40 +030046/* pwrdm_list contains all registered struct powerdomains */
47static LIST_HEAD(pwrdm_list);
48
Rajendra Nayak3b1e8b22010-12-21 20:01:18 -070049static struct pwrdm_ops *arch_pwrdm;
50
Paul Walmsleyad67ef62008-08-19 11:08:40 +030051/* Private functions */
52
Paul Walmsleyad67ef62008-08-19 11:08:40 +030053static struct powerdomain *_pwrdm_lookup(const char *name)
54{
55 struct powerdomain *pwrdm, *temp_pwrdm;
56
57 pwrdm = NULL;
58
59 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
60 if (!strcmp(name, temp_pwrdm->name)) {
61 pwrdm = temp_pwrdm;
62 break;
63 }
64 }
65
66 return pwrdm;
67}
68
Paul Walmsleye909d62a82010-01-26 20:13:00 -070069/**
70 * _pwrdm_register - register a powerdomain
71 * @pwrdm: struct powerdomain * to register
72 *
73 * Adds a powerdomain to the internal powerdomain list. Returns
74 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
75 * already registered by the provided name, or 0 upon success.
76 */
77static int _pwrdm_register(struct powerdomain *pwrdm)
78{
79 int i;
80
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070081 if (!pwrdm || !pwrdm->name)
Paul Walmsleye909d62a82010-01-26 20:13:00 -070082 return -EINVAL;
83
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070084 if (cpu_is_omap44xx() &&
85 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
86 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
87 pwrdm->name);
88 return -EINVAL;
89 }
90
Paul Walmsleye909d62a82010-01-26 20:13:00 -070091 if (_pwrdm_lookup(pwrdm->name))
92 return -EEXIST;
93
94 list_add(&pwrdm->node, &pwrdm_list);
95
96 /* Initialize the powerdomain's state counter */
Paul Walmsleycf57aa72010-01-26 20:13:01 -070097 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
Paul Walmsleye909d62a82010-01-26 20:13:00 -070098 pwrdm->state_counter[i] = 0;
99
Thara Gopinathcde08f82010-02-24 12:05:50 -0700100 pwrdm->ret_logic_off_counter = 0;
101 for (i = 0; i < pwrdm->banks; i++)
102 pwrdm->ret_mem_off_counter[i] = 0;
103
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700104 pwrdm_wait_transition(pwrdm);
105 pwrdm->state = pwrdm_read_pwrst(pwrdm);
106 pwrdm->state_counter[pwrdm->state] = 1;
107
108 pr_debug("powerdomain: registered %s\n", pwrdm->name);
109
110 return 0;
111}
112
Thara Gopinathcde08f82010-02-24 12:05:50 -0700113static void _update_logic_membank_counters(struct powerdomain *pwrdm)
114{
115 int i;
116 u8 prev_logic_pwrst, prev_mem_pwrst;
117
118 prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
119 if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
120 (prev_logic_pwrst == PWRDM_POWER_OFF))
121 pwrdm->ret_logic_off_counter++;
122
123 for (i = 0; i < pwrdm->banks; i++) {
124 prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
125
126 if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
127 (prev_mem_pwrst == PWRDM_POWER_OFF))
128 pwrdm->ret_mem_off_counter[i]++;
129 }
130}
131
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300132static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
133{
134
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100135 int prev, state, trace_state = 0;
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300136
137 if (pwrdm == NULL)
138 return -EINVAL;
139
140 state = pwrdm_read_pwrst(pwrdm);
141
142 switch (flag) {
143 case PWRDM_STATE_NOW:
144 prev = pwrdm->state;
145 break;
146 case PWRDM_STATE_PREV:
147 prev = pwrdm_read_prev_pwrst(pwrdm);
148 if (pwrdm->state != prev)
149 pwrdm->state_counter[prev]++;
Thara Gopinathcde08f82010-02-24 12:05:50 -0700150 if (prev == PWRDM_POWER_RET)
151 _update_logic_membank_counters(pwrdm);
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100152 /*
153 * If the power domain did not hit the desired state,
154 * generate a trace event with both the desired and hit states
155 */
156 if (state != prev) {
157 trace_state = (PWRDM_TRACE_STATES_FLAG |
158 ((state & OMAP_POWERSTATE_MASK) << 8) |
159 ((prev & OMAP_POWERSTATE_MASK) << 0));
160 trace_power_domain_target(pwrdm->name, trace_state,
161 smp_processor_id());
162 }
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300163 break;
164 default:
165 return -EINVAL;
166 }
167
168 if (state != prev)
169 pwrdm->state_counter[state]++;
170
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300171 pm_dbg_update_time(pwrdm, prev);
172
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300173 pwrdm->state = state;
174
175 return 0;
176}
177
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300178static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300179{
180 pwrdm_clear_all_prev_pwrst(pwrdm);
181 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
182 return 0;
183}
184
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300185static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300186{
187 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
188 return 0;
189}
190
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300191/* Public functions */
192
193/**
Paul Walmsley129c65e2011-09-14 16:01:21 -0600194 * pwrdm_register_platform_funcs - register powerdomain implementation fns
195 * @po: func pointers for arch specific implementations
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300196 *
Paul Walmsley129c65e2011-09-14 16:01:21 -0600197 * Register the list of function pointers used to implement the
198 * powerdomain functions on different OMAP SoCs. Should be called
199 * before any other pwrdm_register*() function. Returns -EINVAL if
200 * @po is null, -EEXIST if platform functions have already been
201 * registered, or 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300202 */
Paul Walmsley129c65e2011-09-14 16:01:21 -0600203int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
204{
205 if (!po)
206 return -EINVAL;
207
208 if (arch_pwrdm)
209 return -EEXIST;
210
211 arch_pwrdm = po;
212
213 return 0;
214}
215
216/**
217 * pwrdm_register_pwrdms - register SoC powerdomains
218 * @ps: pointer to an array of struct powerdomain to register
219 *
220 * Register the powerdomains available on a particular OMAP SoC. Must
221 * be called after pwrdm_register_platform_funcs(). May be called
222 * multiple times. Returns -EACCES if called before
223 * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
224 * null; or 0 upon success.
225 */
226int pwrdm_register_pwrdms(struct powerdomain **ps)
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300227{
228 struct powerdomain **p = NULL;
Paul Walmsley129c65e2011-09-14 16:01:21 -0600229
230 if (!arch_pwrdm)
231 return -EEXIST;
232
233 if (!ps)
234 return -EINVAL;
235
236 for (p = ps; *p; p++)
237 _pwrdm_register(*p);
238
239 return 0;
240}
241
242/**
243 * pwrdm_complete_init - set up the powerdomain layer
244 *
245 * Do whatever is necessary to initialize registered powerdomains and
246 * powerdomain code. Currently, this programs the next power state
247 * for each powerdomain to ON. This prevents powerdomains from
248 * unexpectedly losing context or entering high wakeup latency modes
249 * with non-power-management-enabled kernels. Must be called after
250 * pwrdm_register_pwrdms(). Returns -EACCES if called before
251 * pwrdm_register_pwrdms(), or 0 upon success.
252 */
253int pwrdm_complete_init(void)
254{
Rajendra Nayakc956b752011-08-19 16:59:39 -0600255 struct powerdomain *temp_p;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300256
Paul Walmsley129c65e2011-09-14 16:01:21 -0600257 if (list_empty(&pwrdm_list))
258 return -EACCES;
Rajendra Nayakc956b752011-08-19 16:59:39 -0600259
260 list_for_each_entry(temp_p, &pwrdm_list, node)
261 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
Paul Walmsley129c65e2011-09-14 16:01:21 -0600262
263 return 0;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300264}
265
266/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300267 * pwrdm_lookup - look up a powerdomain by name, return a pointer
268 * @name: name of powerdomain
269 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700270 * Find a registered powerdomain by its name @name. Returns a pointer
271 * to the struct powerdomain if found, or NULL otherwise.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300272 */
273struct powerdomain *pwrdm_lookup(const char *name)
274{
275 struct powerdomain *pwrdm;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300276
277 if (!name)
278 return NULL;
279
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300280 pwrdm = _pwrdm_lookup(name);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300281
282 return pwrdm;
283}
284
285/**
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700286 * pwrdm_for_each - call function on each registered clockdomain
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300287 * @fn: callback function *
288 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700289 * Call the supplied function @fn for each registered powerdomain.
290 * The callback function @fn can return anything but 0 to bail out
291 * early from the iterator. Returns the last return value of the
292 * callback function, which should be 0 for success or anything else
293 * to indicate failure; or -EINVAL if the function pointer is null.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300294 */
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700295int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
296 void *user)
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300297{
298 struct powerdomain *temp_pwrdm;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300299 int ret = 0;
300
301 if (!fn)
302 return -EINVAL;
303
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300304 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300305 ret = (*fn)(temp_pwrdm, user);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300306 if (ret)
307 break;
308 }
Artem Bityutskiyee894b12009-10-01 10:01:55 +0300309
310 return ret;
311}
312
313/**
Paul Walmsley8420bb12008-08-19 11:08:44 +0300314 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
315 * @pwrdm: struct powerdomain * to add the clockdomain to
316 * @clkdm: struct clockdomain * to associate with a powerdomain
317 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700318 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
Paul Walmsley8420bb12008-08-19 11:08:44 +0300319 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
320 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
321 * or 0 upon success.
322 */
323int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
324{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300325 int i;
326 int ret = -EINVAL;
327
328 if (!pwrdm || !clkdm)
329 return -EINVAL;
330
331 pr_debug("powerdomain: associating clockdomain %s with powerdomain "
332 "%s\n", clkdm->name, pwrdm->name);
333
Paul Walmsley8420bb12008-08-19 11:08:44 +0300334 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
335 if (!pwrdm->pwrdm_clkdms[i])
336 break;
337#ifdef DEBUG
338 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
339 ret = -EINVAL;
340 goto pac_exit;
341 }
342#endif
343 }
344
345 if (i == PWRDM_MAX_CLKDMS) {
346 pr_debug("powerdomain: increase PWRDM_MAX_CLKDMS for "
347 "pwrdm %s clkdm %s\n", pwrdm->name, clkdm->name);
348 WARN_ON(1);
349 ret = -ENOMEM;
350 goto pac_exit;
351 }
352
353 pwrdm->pwrdm_clkdms[i] = clkdm;
354
355 ret = 0;
356
357pac_exit:
Paul Walmsley8420bb12008-08-19 11:08:44 +0300358 return ret;
359}
360
361/**
362 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
363 * @pwrdm: struct powerdomain * to add the clockdomain to
364 * @clkdm: struct clockdomain * to associate with a powerdomain
365 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700366 * Dissociate the clockdomain @clkdm from the powerdomain
367 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
368 * if @clkdm was not associated with the powerdomain, or 0 upon
369 * success.
Paul Walmsley8420bb12008-08-19 11:08:44 +0300370 */
371int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
372{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300373 int ret = -EINVAL;
374 int i;
375
376 if (!pwrdm || !clkdm)
377 return -EINVAL;
378
379 pr_debug("powerdomain: dissociating clockdomain %s from powerdomain "
380 "%s\n", clkdm->name, pwrdm->name);
381
Paul Walmsley8420bb12008-08-19 11:08:44 +0300382 for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
383 if (pwrdm->pwrdm_clkdms[i] == clkdm)
384 break;
385
386 if (i == PWRDM_MAX_CLKDMS) {
387 pr_debug("powerdomain: clkdm %s not associated with pwrdm "
388 "%s ?!\n", clkdm->name, pwrdm->name);
389 ret = -ENOENT;
390 goto pdc_exit;
391 }
392
393 pwrdm->pwrdm_clkdms[i] = NULL;
394
395 ret = 0;
396
397pdc_exit:
Paul Walmsley8420bb12008-08-19 11:08:44 +0300398 return ret;
399}
400
401/**
402 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
403 * @pwrdm: struct powerdomain * to iterate over
404 * @fn: callback function *
405 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700406 * Call the supplied function @fn for each clockdomain in the powerdomain
407 * @pwrdm. The callback function can return anything but 0 to bail
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700408 * out early from the iterator. Returns -EINVAL if presented with
409 * invalid pointers; or passes along the last return value of the
410 * callback function, which should be 0 for success or anything else
411 * to indicate failure.
Paul Walmsley8420bb12008-08-19 11:08:44 +0300412 */
413int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
414 int (*fn)(struct powerdomain *pwrdm,
415 struct clockdomain *clkdm))
416{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300417 int ret = 0;
418 int i;
419
420 if (!fn)
421 return -EINVAL;
422
Paul Walmsley8420bb12008-08-19 11:08:44 +0300423 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
424 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
425
Paul Walmsley8420bb12008-08-19 11:08:44 +0300426 return ret;
427}
428
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300429/**
430 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
431 * @pwrdm: struct powerdomain *
432 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700433 * Return the number of controllable memory banks in powerdomain @pwrdm,
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300434 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
435 */
436int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
437{
438 if (!pwrdm)
439 return -EINVAL;
440
441 return pwrdm->banks;
442}
443
444/**
445 * pwrdm_set_next_pwrst - set next powerdomain power state
446 * @pwrdm: struct powerdomain * to set
447 * @pwrst: one of the PWRDM_POWER_* macros
448 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700449 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300450 * may not enter this state immediately if the preconditions for this state
451 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
452 * null or if the power state is invalid for the powerdomin, or returns 0
453 * upon success.
454 */
455int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
456{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700457 int ret = -EINVAL;
458
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300459 if (!pwrdm)
460 return -EINVAL;
461
462 if (!(pwrdm->pwrsts & (1 << pwrst)))
463 return -EINVAL;
464
465 pr_debug("powerdomain: setting next powerstate for %s to %0x\n",
466 pwrdm->name, pwrst);
467
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100468 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
469 /* Trace the pwrdm desired target state */
470 trace_power_domain_target(pwrdm->name, pwrst,
471 smp_processor_id());
472 /* Program the pwrdm desired target state */
Rajendra Nayakf327e072010-12-21 20:01:18 -0700473 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100474 }
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300475
Rajendra Nayakf327e072010-12-21 20:01:18 -0700476 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300477}
478
479/**
480 * pwrdm_read_next_pwrst - get next powerdomain power state
481 * @pwrdm: struct powerdomain * to get power state
482 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700483 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300484 * if the powerdomain pointer is null or returns the next power state
485 * upon success.
486 */
487int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
488{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700489 int ret = -EINVAL;
490
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300491 if (!pwrdm)
492 return -EINVAL;
493
Rajendra Nayakf327e072010-12-21 20:01:18 -0700494 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
495 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
496
497 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300498}
499
500/**
501 * pwrdm_read_pwrst - get current powerdomain power state
502 * @pwrdm: struct powerdomain * to get power state
503 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700504 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300505 * if the powerdomain pointer is null or returns the current power state
506 * upon success.
507 */
508int pwrdm_read_pwrst(struct powerdomain *pwrdm)
509{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700510 int ret = -EINVAL;
511
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300512 if (!pwrdm)
513 return -EINVAL;
514
Rajendra Nayakf327e072010-12-21 20:01:18 -0700515 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
516 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
517
518 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300519}
520
521/**
522 * pwrdm_read_prev_pwrst - get previous powerdomain power state
523 * @pwrdm: struct powerdomain * to get previous power state
524 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700525 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300526 * if the powerdomain pointer is null or returns the previous power state
527 * upon success.
528 */
529int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
530{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700531 int ret = -EINVAL;
532
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300533 if (!pwrdm)
534 return -EINVAL;
535
Rajendra Nayakf327e072010-12-21 20:01:18 -0700536 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
537 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
538
539 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300540}
541
542/**
543 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
544 * @pwrdm: struct powerdomain * to set
545 * @pwrst: one of the PWRDM_POWER_* macros
546 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700547 * Set the next power state @pwrst that the logic portion of the
548 * powerdomain @pwrdm will enter when the powerdomain enters retention.
549 * This will be either RETENTION or OFF, if supported. Returns
550 * -EINVAL if the powerdomain pointer is null or the target power
551 * state is not not supported, or returns 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300552 */
553int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
554{
Rajendra Nayak12627572010-12-21 20:01:18 -0700555 int ret = -EINVAL;
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600556
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300557 if (!pwrdm)
558 return -EINVAL;
559
560 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
561 return -EINVAL;
562
563 pr_debug("powerdomain: setting next logic powerstate for %s to %0x\n",
564 pwrdm->name, pwrst);
565
Rajendra Nayak12627572010-12-21 20:01:18 -0700566 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
567 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300568
Rajendra Nayak12627572010-12-21 20:01:18 -0700569 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300570}
571
572/**
573 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
574 * @pwrdm: struct powerdomain * to set
575 * @bank: memory bank number to set (0-3)
576 * @pwrst: one of the PWRDM_POWER_* macros
577 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700578 * Set the next power state @pwrst that memory bank @bank of the
579 * powerdomain @pwrdm will enter when the powerdomain enters the ON
580 * state. @bank will be a number from 0 to 3, and represents different
581 * types of memory, depending on the powerdomain. Returns -EINVAL if
582 * the powerdomain pointer is null or the target power state is not
583 * not supported for this memory bank, -EEXIST if the target memory
584 * bank does not exist or is not controllable, or returns 0 upon
585 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300586 */
587int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
588{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700589 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300590
591 if (!pwrdm)
592 return -EINVAL;
593
594 if (pwrdm->banks < (bank + 1))
595 return -EEXIST;
596
597 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
598 return -EINVAL;
599
600 pr_debug("powerdomain: setting next memory powerstate for domain %s "
601 "bank %0x while pwrdm-ON to %0x\n", pwrdm->name, bank, pwrst);
602
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700603 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
604 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300605
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700606 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300607}
608
609/**
610 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
611 * @pwrdm: struct powerdomain * to set
612 * @bank: memory bank number to set (0-3)
613 * @pwrst: one of the PWRDM_POWER_* macros
614 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700615 * Set the next power state @pwrst that memory bank @bank of the
616 * powerdomain @pwrdm will enter when the powerdomain enters the
617 * RETENTION state. Bank will be a number from 0 to 3, and represents
618 * different types of memory, depending on the powerdomain. @pwrst
619 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
620 * the powerdomain pointer is null or the target power state is not
621 * not supported for this memory bank, -EEXIST if the target memory
622 * bank does not exist or is not controllable, or returns 0 upon
623 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300624 */
625int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
626{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700627 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300628
629 if (!pwrdm)
630 return -EINVAL;
631
632 if (pwrdm->banks < (bank + 1))
633 return -EEXIST;
634
635 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
636 return -EINVAL;
637
638 pr_debug("powerdomain: setting next memory powerstate for domain %s "
639 "bank %0x while pwrdm-RET to %0x\n", pwrdm->name, bank, pwrst);
640
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700641 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
642 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300643
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700644 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300645}
646
647/**
648 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
649 * @pwrdm: struct powerdomain * to get current logic retention power state
650 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700651 * Return the power state that the logic portion of powerdomain @pwrdm
652 * will enter when the powerdomain enters retention. Returns -EINVAL
653 * if the powerdomain pointer is null or returns the logic retention
654 * power state upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300655 */
656int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
657{
Rajendra Nayak12627572010-12-21 20:01:18 -0700658 int ret = -EINVAL;
659
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300660 if (!pwrdm)
661 return -EINVAL;
662
Rajendra Nayak12627572010-12-21 20:01:18 -0700663 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
664 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
665
666 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300667}
668
669/**
670 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
671 * @pwrdm: struct powerdomain * to get previous logic power state
672 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700673 * Return the powerdomain @pwrdm's previous logic power state. Returns
674 * -EINVAL if the powerdomain pointer is null or returns the previous
675 * logic power state upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300676 */
677int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
678{
Rajendra Nayak12627572010-12-21 20:01:18 -0700679 int ret = -EINVAL;
680
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300681 if (!pwrdm)
682 return -EINVAL;
683
Rajendra Nayak12627572010-12-21 20:01:18 -0700684 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
685 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
686
687 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300688}
689
690/**
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700691 * pwrdm_read_logic_retst - get next powerdomain logic power state
692 * @pwrdm: struct powerdomain * to get next logic power state
693 *
694 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
695 * if the powerdomain pointer is null or returns the next logic
696 * power state upon success.
697 */
698int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
699{
Rajendra Nayak12627572010-12-21 20:01:18 -0700700 int ret = -EINVAL;
701
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700702 if (!pwrdm)
703 return -EINVAL;
704
Rajendra Nayak12627572010-12-21 20:01:18 -0700705 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
706 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
707
708 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700709}
710
711/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300712 * pwrdm_read_mem_pwrst - get current memory bank power state
713 * @pwrdm: struct powerdomain * to get current memory bank power state
714 * @bank: memory bank number (0-3)
715 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700716 * Return the powerdomain @pwrdm's current memory power state for bank
717 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300718 * the target memory bank does not exist or is not controllable, or
719 * returns the current memory power state upon success.
720 */
721int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
722{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700723 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300724
725 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700726 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300727
728 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700729 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300730
Thara Gopinath3863c742009-12-08 16:33:15 -0700731 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
732 bank = 1;
733
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700734 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
735 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300736
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700737 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300738}
739
740/**
741 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
742 * @pwrdm: struct powerdomain * to get previous memory bank power state
743 * @bank: memory bank number (0-3)
744 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700745 * Return the powerdomain @pwrdm's previous memory power state for
746 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
747 * -EEXIST if the target memory bank does not exist or is not
748 * controllable, or returns the previous memory power state upon
749 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300750 */
751int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
752{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700753 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300754
755 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700756 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300757
758 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700759 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300760
Thara Gopinath3863c742009-12-08 16:33:15 -0700761 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
762 bank = 1;
763
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700764 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
765 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300766
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700767 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300768}
769
770/**
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700771 * pwrdm_read_mem_retst - get next memory bank power state
772 * @pwrdm: struct powerdomain * to get mext memory bank power state
773 * @bank: memory bank number (0-3)
774 *
775 * Return the powerdomain pwrdm's next memory power state for bank
776 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
777 * the target memory bank does not exist or is not controllable, or
778 * returns the next memory power state upon success.
779 */
780int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
781{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700782 int ret = -EINVAL;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700783
784 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700785 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700786
787 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700788 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700789
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700790 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
791 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700792
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700793 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700794}
795
796/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300797 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
798 * @pwrdm: struct powerdomain * to clear
799 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700800 * Clear the powerdomain's previous power state register @pwrdm.
801 * Clears the entire register, including logic and memory bank
802 * previous power states. Returns -EINVAL if the powerdomain pointer
803 * is null, or returns 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300804 */
805int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
806{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700807 int ret = -EINVAL;
808
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300809 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700810 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300811
812 /*
813 * XXX should get the powerdomain's current state here;
814 * warn & fail if it is not ON.
815 */
816
817 pr_debug("powerdomain: clearing previous power state reg for %s\n",
818 pwrdm->name);
819
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700820 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
821 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300822
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700823 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300824}
825
826/**
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600827 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
828 * @pwrdm: struct powerdomain *
829 *
830 * Enable automatic context save-and-restore upon power state change
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700831 * for some devices in the powerdomain @pwrdm. Warning: this only
832 * affects a subset of devices in a powerdomain; check the TRM
833 * closely. Returns -EINVAL if the powerdomain pointer is null or if
834 * the powerdomain does not support automatic save-and-restore, or
835 * returns 0 upon success.
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600836 */
837int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
838{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700839 int ret = -EINVAL;
840
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600841 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700842 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600843
844 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700845 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600846
847 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n",
848 pwrdm->name);
849
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700850 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
851 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600852
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700853 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600854}
855
856/**
857 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
858 * @pwrdm: struct powerdomain *
859 *
860 * Disable automatic context save-and-restore upon power state change
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700861 * for some devices in the powerdomain @pwrdm. Warning: this only
862 * affects a subset of devices in a powerdomain; check the TRM
863 * closely. Returns -EINVAL if the powerdomain pointer is null or if
864 * the powerdomain does not support automatic save-and-restore, or
865 * returns 0 upon success.
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600866 */
867int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
868{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700869 int ret = -EINVAL;
870
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600871 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700872 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600873
874 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700875 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600876
877 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n",
878 pwrdm->name);
879
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700880 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
881 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600882
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700883 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600884}
885
886/**
887 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
888 * @pwrdm: struct powerdomain *
889 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700890 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600891 * for some devices, or 0 if it does not.
892 */
893bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
894{
895 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
896}
897
898/**
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600899 * pwrdm_set_lowpwrstchange - Request a low power state change
900 * @pwrdm: struct powerdomain *
901 *
902 * Allows a powerdomain to transtion to a lower power sleep state
903 * from an existing sleep state without waking up the powerdomain.
904 * Returns -EINVAL if the powerdomain pointer is null or if the
905 * powerdomain does not support LOWPOWERSTATECHANGE, or returns 0
906 * upon success.
907 */
908int pwrdm_set_lowpwrstchange(struct powerdomain *pwrdm)
909{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700910 int ret = -EINVAL;
911
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600912 if (!pwrdm)
913 return -EINVAL;
914
915 if (!(pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE))
916 return -EINVAL;
917
918 pr_debug("powerdomain: %s: setting LOWPOWERSTATECHANGE bit\n",
919 pwrdm->name);
920
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700921 if (arch_pwrdm && arch_pwrdm->pwrdm_set_lowpwrstchange)
922 ret = arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600923
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700924 return ret;
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600925}
926
927/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300928 * pwrdm_wait_transition - wait for powerdomain power transition to finish
929 * @pwrdm: struct powerdomain * to wait for
930 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700931 * If the powerdomain @pwrdm is in the process of a state transition,
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300932 * spin until it completes the power transition, or until an iteration
933 * bailout value is reached. Returns -EINVAL if the powerdomain
934 * pointer is null, -EAGAIN if the bailout value was reached, or
935 * returns 0 upon success.
936 */
937int pwrdm_wait_transition(struct powerdomain *pwrdm)
938{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700939 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300940
941 if (!pwrdm)
942 return -EINVAL;
943
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700944 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
945 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300946
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700947 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300948}
949
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300950int pwrdm_state_switch(struct powerdomain *pwrdm)
951{
952 return _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
953}
954
955int pwrdm_clkdm_state_switch(struct clockdomain *clkdm)
956{
957 if (clkdm != NULL && clkdm->pwrdm.ptr != NULL) {
958 pwrdm_wait_transition(clkdm->pwrdm.ptr);
959 return pwrdm_state_switch(clkdm->pwrdm.ptr);
960 }
961
962 return -EINVAL;
963}
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300964
965int pwrdm_pre_transition(void)
966{
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300967 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300968 return 0;
969}
970
971int pwrdm_post_transition(void)
972{
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300973 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300974 return 0;
975}
Kevin Hilman7f595672010-12-21 21:31:55 -0700976
977/**
978 * pwrdm_get_context_loss_count - get powerdomain's context loss count
979 * @pwrdm: struct powerdomain * to wait for
980 *
981 * Context loss count is the sum of powerdomain off-mode counter, the
982 * logic off counter and the per-bank memory off counter. Returns 0
983 * (and WARNs) upon error, otherwise, returns the context loss count.
984 */
985u32 pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
986{
987 int i, count;
988
989 if (!pwrdm) {
990 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
991 return 0;
992 }
993
994 count = pwrdm->state_counter[PWRDM_POWER_OFF];
995 count += pwrdm->ret_logic_off_counter;
996
997 for (i = 0; i < pwrdm->banks; i++)
998 count += pwrdm->ret_mem_off_counter[i];
999
1000 pr_debug("powerdomain: %s: context loss count = %u\n",
1001 pwrdm->name, count);
1002
1003 return count;
1004}
Paul Walmsley694606c2011-03-07 19:28:15 -07001005
1006/**
1007 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1008 * @pwrdm: struct powerdomain *
1009 *
1010 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1011 * can lose either memory or logic context or if @pwrdm is invalid, or
1012 * returns 0 otherwise. This function is not concerned with how the
1013 * powerdomain registers are programmed (i.e., to go off or not); it's
1014 * concerned with whether it's ever possible for this powerdomain to
1015 * go off while some other part of the chip is active. This function
1016 * assumes that every powerdomain can go to either ON or INACTIVE.
1017 */
1018bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
1019{
1020 int i;
1021
1022 if (IS_ERR_OR_NULL(pwrdm)) {
1023 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1024 __func__);
1025 return 1;
1026 }
1027
1028 if (pwrdm->pwrsts & PWRSTS_OFF)
1029 return 1;
1030
1031 if (pwrdm->pwrsts & PWRSTS_RET) {
1032 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1033 return 1;
1034
1035 for (i = 0; i < pwrdm->banks; i++)
1036 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1037 return 1;
1038 }
1039
1040 for (i = 0; i < pwrdm->banks; i++)
1041 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1042 return 1;
1043
1044 return 0;
1045}