blob: 1678a3284233ec02b424bca2eea207396c32f2c7 [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 Lindgrendbc04162012-08-31 10:59:07 -070031
Paul Walmsley55ed9692010-01-26 20:12:59 -070032#include <plat/prcm.h>
Paul Walmsleyad67ef62008-08-19 11:08:40 +030033
Tony Lindgrendbc04162012-08-31 10:59:07 -070034#include "powerdomain.h"
35#include "clockdomain.h"
36
37#include "soc.h"
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +030038#include "pm.h"
39
Jean Pihet5e7c58d2011-03-03 11:25:43 +010040#define PWRDM_TRACE_STATES_FLAG (1<<31)
41
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +030042enum {
43 PWRDM_STATE_NOW = 0,
44 PWRDM_STATE_PREV,
45};
46
Abhijit Pagare3a759f02010-01-26 20:12:53 -070047
Paul Walmsleyad67ef62008-08-19 11:08:40 +030048/* pwrdm_list contains all registered struct powerdomains */
49static LIST_HEAD(pwrdm_list);
50
Rajendra Nayak3b1e8b22010-12-21 20:01:18 -070051static struct pwrdm_ops *arch_pwrdm;
52
Paul Walmsleyad67ef62008-08-19 11:08:40 +030053/* Private functions */
54
Paul Walmsleyad67ef62008-08-19 11:08:40 +030055static struct powerdomain *_pwrdm_lookup(const char *name)
56{
57 struct powerdomain *pwrdm, *temp_pwrdm;
58
59 pwrdm = NULL;
60
61 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
62 if (!strcmp(name, temp_pwrdm->name)) {
63 pwrdm = temp_pwrdm;
64 break;
65 }
66 }
67
68 return pwrdm;
69}
70
Paul Walmsleye909d62a82010-01-26 20:13:00 -070071/**
72 * _pwrdm_register - register a powerdomain
73 * @pwrdm: struct powerdomain * to register
74 *
75 * Adds a powerdomain to the internal powerdomain list. Returns
76 * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
77 * already registered by the provided name, or 0 upon success.
78 */
79static int _pwrdm_register(struct powerdomain *pwrdm)
80{
81 int i;
Kevin Hilman048a7032011-03-16 15:52:47 -070082 struct voltagedomain *voltdm;
Paul Walmsleye909d62a82010-01-26 20:13:00 -070083
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070084 if (!pwrdm || !pwrdm->name)
Paul Walmsleye909d62a82010-01-26 20:13:00 -070085 return -EINVAL;
86
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070087 if (cpu_is_omap44xx() &&
88 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
89 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
90 pwrdm->name);
91 return -EINVAL;
92 }
93
Paul Walmsleye909d62a82010-01-26 20:13:00 -070094 if (_pwrdm_lookup(pwrdm->name))
95 return -EEXIST;
96
Kevin Hilman048a7032011-03-16 15:52:47 -070097 voltdm = voltdm_lookup(pwrdm->voltdm.name);
98 if (!voltdm) {
99 pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
100 pwrdm->name, pwrdm->voltdm.name);
101 return -EINVAL;
102 }
103 pwrdm->voltdm.ptr = voltdm;
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700104 INIT_LIST_HEAD(&pwrdm->voltdm_node);
105 voltdm_add_pwrdm(voltdm, pwrdm);
Kevin Hilman048a7032011-03-16 15:52:47 -0700106
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700107 list_add(&pwrdm->node, &pwrdm_list);
108
109 /* Initialize the powerdomain's state counter */
Paul Walmsleycf57aa72010-01-26 20:13:01 -0700110 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700111 pwrdm->state_counter[i] = 0;
112
Thara Gopinathcde08f82010-02-24 12:05:50 -0700113 pwrdm->ret_logic_off_counter = 0;
114 for (i = 0; i < pwrdm->banks; i++)
115 pwrdm->ret_mem_off_counter[i] = 0;
116
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700117 pwrdm_wait_transition(pwrdm);
118 pwrdm->state = pwrdm_read_pwrst(pwrdm);
119 pwrdm->state_counter[pwrdm->state] = 1;
120
121 pr_debug("powerdomain: registered %s\n", pwrdm->name);
122
123 return 0;
124}
125
Thara Gopinathcde08f82010-02-24 12:05:50 -0700126static void _update_logic_membank_counters(struct powerdomain *pwrdm)
127{
128 int i;
129 u8 prev_logic_pwrst, prev_mem_pwrst;
130
131 prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
132 if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
133 (prev_logic_pwrst == PWRDM_POWER_OFF))
134 pwrdm->ret_logic_off_counter++;
135
136 for (i = 0; i < pwrdm->banks; i++) {
137 prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
138
139 if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
140 (prev_mem_pwrst == PWRDM_POWER_OFF))
141 pwrdm->ret_mem_off_counter[i]++;
142 }
143}
144
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300145static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
146{
147
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100148 int prev, state, trace_state = 0;
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300149
150 if (pwrdm == NULL)
151 return -EINVAL;
152
153 state = pwrdm_read_pwrst(pwrdm);
154
155 switch (flag) {
156 case PWRDM_STATE_NOW:
157 prev = pwrdm->state;
158 break;
159 case PWRDM_STATE_PREV:
160 prev = pwrdm_read_prev_pwrst(pwrdm);
161 if (pwrdm->state != prev)
162 pwrdm->state_counter[prev]++;
Thara Gopinathcde08f82010-02-24 12:05:50 -0700163 if (prev == PWRDM_POWER_RET)
164 _update_logic_membank_counters(pwrdm);
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100165 /*
166 * If the power domain did not hit the desired state,
167 * generate a trace event with both the desired and hit states
168 */
169 if (state != prev) {
170 trace_state = (PWRDM_TRACE_STATES_FLAG |
171 ((state & OMAP_POWERSTATE_MASK) << 8) |
172 ((prev & OMAP_POWERSTATE_MASK) << 0));
173 trace_power_domain_target(pwrdm->name, trace_state,
174 smp_processor_id());
175 }
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300176 break;
177 default:
178 return -EINVAL;
179 }
180
181 if (state != prev)
182 pwrdm->state_counter[state]++;
183
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300184 pm_dbg_update_time(pwrdm, prev);
185
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300186 pwrdm->state = state;
187
188 return 0;
189}
190
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300191static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300192{
193 pwrdm_clear_all_prev_pwrst(pwrdm);
194 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
195 return 0;
196}
197
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300198static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300199{
200 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
201 return 0;
202}
203
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300204/* Public functions */
205
206/**
Paul Walmsley129c65e2011-09-14 16:01:21 -0600207 * pwrdm_register_platform_funcs - register powerdomain implementation fns
208 * @po: func pointers for arch specific implementations
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300209 *
Paul Walmsley129c65e2011-09-14 16:01:21 -0600210 * Register the list of function pointers used to implement the
211 * powerdomain functions on different OMAP SoCs. Should be called
212 * before any other pwrdm_register*() function. Returns -EINVAL if
213 * @po is null, -EEXIST if platform functions have already been
214 * registered, or 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300215 */
Paul Walmsley129c65e2011-09-14 16:01:21 -0600216int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
217{
218 if (!po)
219 return -EINVAL;
220
221 if (arch_pwrdm)
222 return -EEXIST;
223
224 arch_pwrdm = po;
225
226 return 0;
227}
228
229/**
230 * pwrdm_register_pwrdms - register SoC powerdomains
231 * @ps: pointer to an array of struct powerdomain to register
232 *
233 * Register the powerdomains available on a particular OMAP SoC. Must
234 * be called after pwrdm_register_platform_funcs(). May be called
235 * multiple times. Returns -EACCES if called before
236 * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
237 * null; or 0 upon success.
238 */
239int pwrdm_register_pwrdms(struct powerdomain **ps)
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300240{
241 struct powerdomain **p = NULL;
Paul Walmsley129c65e2011-09-14 16:01:21 -0600242
243 if (!arch_pwrdm)
244 return -EEXIST;
245
246 if (!ps)
247 return -EINVAL;
248
249 for (p = ps; *p; p++)
250 _pwrdm_register(*p);
251
252 return 0;
253}
254
255/**
256 * pwrdm_complete_init - set up the powerdomain layer
257 *
258 * Do whatever is necessary to initialize registered powerdomains and
259 * powerdomain code. Currently, this programs the next power state
260 * for each powerdomain to ON. This prevents powerdomains from
261 * unexpectedly losing context or entering high wakeup latency modes
262 * with non-power-management-enabled kernels. Must be called after
263 * pwrdm_register_pwrdms(). Returns -EACCES if called before
264 * pwrdm_register_pwrdms(), or 0 upon success.
265 */
266int pwrdm_complete_init(void)
267{
Rajendra Nayakc956b752011-08-19 16:59:39 -0600268 struct powerdomain *temp_p;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300269
Paul Walmsley129c65e2011-09-14 16:01:21 -0600270 if (list_empty(&pwrdm_list))
271 return -EACCES;
Rajendra Nayakc956b752011-08-19 16:59:39 -0600272
273 list_for_each_entry(temp_p, &pwrdm_list, node)
274 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
Paul Walmsley129c65e2011-09-14 16:01:21 -0600275
276 return 0;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300277}
278
279/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300280 * pwrdm_lookup - look up a powerdomain by name, return a pointer
281 * @name: name of powerdomain
282 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700283 * Find a registered powerdomain by its name @name. Returns a pointer
284 * to the struct powerdomain if found, or NULL otherwise.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300285 */
286struct powerdomain *pwrdm_lookup(const char *name)
287{
288 struct powerdomain *pwrdm;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300289
290 if (!name)
291 return NULL;
292
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300293 pwrdm = _pwrdm_lookup(name);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300294
295 return pwrdm;
296}
297
298/**
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700299 * pwrdm_for_each - call function on each registered clockdomain
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300300 * @fn: callback function *
301 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700302 * Call the supplied function @fn for each registered powerdomain.
303 * The callback function @fn can return anything but 0 to bail out
304 * early from the iterator. Returns the last return value of the
305 * callback function, which should be 0 for success or anything else
306 * to indicate failure; or -EINVAL if the function pointer is null.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300307 */
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700308int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
309 void *user)
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300310{
311 struct powerdomain *temp_pwrdm;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300312 int ret = 0;
313
314 if (!fn)
315 return -EINVAL;
316
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300317 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300318 ret = (*fn)(temp_pwrdm, user);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300319 if (ret)
320 break;
321 }
Artem Bityutskiyee894b12009-10-01 10:01:55 +0300322
323 return ret;
324}
325
326/**
Paul Walmsley8420bb12008-08-19 11:08:44 +0300327 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
328 * @pwrdm: struct powerdomain * to add the clockdomain to
329 * @clkdm: struct clockdomain * to associate with a powerdomain
330 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700331 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
Paul Walmsley8420bb12008-08-19 11:08:44 +0300332 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
333 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
334 * or 0 upon success.
335 */
336int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
337{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300338 int i;
339 int ret = -EINVAL;
340
341 if (!pwrdm || !clkdm)
342 return -EINVAL;
343
Paul Walmsley7852ec02012-07-26 00:54:26 -0600344 pr_debug("powerdomain: %s: associating clockdomain %s\n",
345 pwrdm->name, clkdm->name);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300346
Paul Walmsley8420bb12008-08-19 11:08:44 +0300347 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
348 if (!pwrdm->pwrdm_clkdms[i])
349 break;
350#ifdef DEBUG
351 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
352 ret = -EINVAL;
353 goto pac_exit;
354 }
355#endif
356 }
357
358 if (i == PWRDM_MAX_CLKDMS) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600359 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
360 pwrdm->name, clkdm->name);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300361 WARN_ON(1);
362 ret = -ENOMEM;
363 goto pac_exit;
364 }
365
366 pwrdm->pwrdm_clkdms[i] = clkdm;
367
368 ret = 0;
369
370pac_exit:
Paul Walmsley8420bb12008-08-19 11:08:44 +0300371 return ret;
372}
373
374/**
375 * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
376 * @pwrdm: struct powerdomain * to add the clockdomain to
377 * @clkdm: struct clockdomain * to associate with a powerdomain
378 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700379 * Dissociate the clockdomain @clkdm from the powerdomain
380 * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
381 * if @clkdm was not associated with the powerdomain, or 0 upon
382 * success.
Paul Walmsley8420bb12008-08-19 11:08:44 +0300383 */
384int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
385{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300386 int ret = -EINVAL;
387 int i;
388
389 if (!pwrdm || !clkdm)
390 return -EINVAL;
391
Paul Walmsley7852ec02012-07-26 00:54:26 -0600392 pr_debug("powerdomain: %s: dissociating clockdomain %s\n",
393 pwrdm->name, clkdm->name);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300394
Paul Walmsley8420bb12008-08-19 11:08:44 +0300395 for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
396 if (pwrdm->pwrdm_clkdms[i] == clkdm)
397 break;
398
399 if (i == PWRDM_MAX_CLKDMS) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600400 pr_debug("powerdomain: %s: clkdm %s not associated?!\n",
401 pwrdm->name, clkdm->name);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300402 ret = -ENOENT;
403 goto pdc_exit;
404 }
405
406 pwrdm->pwrdm_clkdms[i] = NULL;
407
408 ret = 0;
409
410pdc_exit:
Paul Walmsley8420bb12008-08-19 11:08:44 +0300411 return ret;
412}
413
414/**
415 * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
416 * @pwrdm: struct powerdomain * to iterate over
417 * @fn: callback function *
418 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700419 * Call the supplied function @fn for each clockdomain in the powerdomain
420 * @pwrdm. The callback function can return anything but 0 to bail
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700421 * out early from the iterator. Returns -EINVAL if presented with
422 * invalid pointers; or passes along the last return value of the
423 * callback function, which should be 0 for success or anything else
424 * to indicate failure.
Paul Walmsley8420bb12008-08-19 11:08:44 +0300425 */
426int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
427 int (*fn)(struct powerdomain *pwrdm,
428 struct clockdomain *clkdm))
429{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300430 int ret = 0;
431 int i;
432
433 if (!fn)
434 return -EINVAL;
435
Paul Walmsley8420bb12008-08-19 11:08:44 +0300436 for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
437 ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
438
Paul Walmsley8420bb12008-08-19 11:08:44 +0300439 return ret;
440}
441
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300442/**
Kevin Hilman048a7032011-03-16 15:52:47 -0700443 * pwrdm_get_voltdm - return a ptr to the voltdm that this pwrdm resides in
444 * @pwrdm: struct powerdomain *
445 *
446 * Return a pointer to the struct voltageomain that the specified powerdomain
447 * @pwrdm exists in.
448 */
449struct voltagedomain *pwrdm_get_voltdm(struct powerdomain *pwrdm)
450{
451 return pwrdm->voltdm.ptr;
452}
453
454/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300455 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
456 * @pwrdm: struct powerdomain *
457 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700458 * Return the number of controllable memory banks in powerdomain @pwrdm,
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300459 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
460 */
461int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
462{
463 if (!pwrdm)
464 return -EINVAL;
465
466 return pwrdm->banks;
467}
468
469/**
470 * pwrdm_set_next_pwrst - set next powerdomain power state
471 * @pwrdm: struct powerdomain * to set
472 * @pwrst: one of the PWRDM_POWER_* macros
473 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700474 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300475 * may not enter this state immediately if the preconditions for this state
476 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
477 * null or if the power state is invalid for the powerdomin, or returns 0
478 * upon success.
479 */
480int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
481{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700482 int ret = -EINVAL;
483
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300484 if (!pwrdm)
485 return -EINVAL;
486
487 if (!(pwrdm->pwrsts & (1 << pwrst)))
488 return -EINVAL;
489
Paul Walmsley7852ec02012-07-26 00:54:26 -0600490 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300491 pwrdm->name, pwrst);
492
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100493 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
494 /* Trace the pwrdm desired target state */
495 trace_power_domain_target(pwrdm->name, pwrst,
496 smp_processor_id());
497 /* Program the pwrdm desired target state */
Rajendra Nayakf327e072010-12-21 20:01:18 -0700498 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100499 }
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300500
Rajendra Nayakf327e072010-12-21 20:01:18 -0700501 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300502}
503
504/**
505 * pwrdm_read_next_pwrst - get next powerdomain power state
506 * @pwrdm: struct powerdomain * to get power state
507 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700508 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300509 * if the powerdomain pointer is null or returns the next power state
510 * upon success.
511 */
512int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
513{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700514 int ret = -EINVAL;
515
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300516 if (!pwrdm)
517 return -EINVAL;
518
Rajendra Nayakf327e072010-12-21 20:01:18 -0700519 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
520 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
521
522 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300523}
524
525/**
526 * pwrdm_read_pwrst - get current powerdomain power state
527 * @pwrdm: struct powerdomain * to get power state
528 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700529 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300530 * if the powerdomain pointer is null or returns the current power state
Jon Hunterd49cae92012-06-29 16:04:52 -0500531 * upon success. Note that if the power domain only supports the ON state
532 * then just return ON as the current state.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300533 */
534int pwrdm_read_pwrst(struct powerdomain *pwrdm)
535{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700536 int ret = -EINVAL;
537
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300538 if (!pwrdm)
539 return -EINVAL;
540
Jon Hunterd49cae92012-06-29 16:04:52 -0500541 if (pwrdm->pwrsts == PWRSTS_ON)
542 return PWRDM_POWER_ON;
543
Rajendra Nayakf327e072010-12-21 20:01:18 -0700544 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
545 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
546
547 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300548}
549
550/**
551 * pwrdm_read_prev_pwrst - get previous powerdomain power state
552 * @pwrdm: struct powerdomain * to get previous power state
553 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700554 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300555 * if the powerdomain pointer is null or returns the previous power state
556 * upon success.
557 */
558int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
559{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700560 int ret = -EINVAL;
561
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300562 if (!pwrdm)
563 return -EINVAL;
564
Rajendra Nayakf327e072010-12-21 20:01:18 -0700565 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
566 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
567
568 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300569}
570
571/**
572 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
573 * @pwrdm: struct powerdomain * to set
574 * @pwrst: one of the PWRDM_POWER_* macros
575 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700576 * Set the next power state @pwrst that the logic portion of the
577 * powerdomain @pwrdm will enter when the powerdomain enters retention.
578 * This will be either RETENTION or OFF, if supported. Returns
579 * -EINVAL if the powerdomain pointer is null or the target power
580 * state is not not supported, or returns 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300581 */
582int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
583{
Rajendra Nayak12627572010-12-21 20:01:18 -0700584 int ret = -EINVAL;
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600585
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300586 if (!pwrdm)
587 return -EINVAL;
588
589 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
590 return -EINVAL;
591
Paul Walmsley7852ec02012-07-26 00:54:26 -0600592 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300593 pwrdm->name, pwrst);
594
Rajendra Nayak12627572010-12-21 20:01:18 -0700595 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
596 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300597
Rajendra Nayak12627572010-12-21 20:01:18 -0700598 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300599}
600
601/**
602 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
603 * @pwrdm: struct powerdomain * to set
604 * @bank: memory bank number to set (0-3)
605 * @pwrst: one of the PWRDM_POWER_* macros
606 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700607 * Set the next power state @pwrst that memory bank @bank of the
608 * powerdomain @pwrdm will enter when the powerdomain enters the ON
609 * state. @bank will be a number from 0 to 3, and represents different
610 * types of memory, depending on the powerdomain. Returns -EINVAL if
611 * the powerdomain pointer is null or the target power state is not
612 * not supported for this memory bank, -EEXIST if the target memory
613 * bank does not exist or is not controllable, or returns 0 upon
614 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300615 */
616int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
617{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700618 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300619
620 if (!pwrdm)
621 return -EINVAL;
622
623 if (pwrdm->banks < (bank + 1))
624 return -EEXIST;
625
626 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
627 return -EINVAL;
628
Paul Walmsley7852ec02012-07-26 00:54:26 -0600629 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
630 pwrdm->name, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300631
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700632 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
633 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300634
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700635 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300636}
637
638/**
639 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
640 * @pwrdm: struct powerdomain * to set
641 * @bank: memory bank number to set (0-3)
642 * @pwrst: one of the PWRDM_POWER_* macros
643 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700644 * Set the next power state @pwrst that memory bank @bank of the
645 * powerdomain @pwrdm will enter when the powerdomain enters the
646 * RETENTION state. Bank will be a number from 0 to 3, and represents
647 * different types of memory, depending on the powerdomain. @pwrst
648 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
649 * the powerdomain pointer is null or the target power state is not
650 * not supported for this memory bank, -EEXIST if the target memory
651 * bank does not exist or is not controllable, or returns 0 upon
652 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300653 */
654int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
655{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700656 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300657
658 if (!pwrdm)
659 return -EINVAL;
660
661 if (pwrdm->banks < (bank + 1))
662 return -EEXIST;
663
664 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
665 return -EINVAL;
666
Paul Walmsley7852ec02012-07-26 00:54:26 -0600667 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
668 pwrdm->name, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300669
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700670 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
671 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300672
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700673 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300674}
675
676/**
677 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
678 * @pwrdm: struct powerdomain * to get current logic retention power state
679 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700680 * Return the power state that the logic portion of powerdomain @pwrdm
681 * will enter when the powerdomain enters retention. Returns -EINVAL
682 * if the powerdomain pointer is null or returns the logic retention
683 * power state upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300684 */
685int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
686{
Rajendra Nayak12627572010-12-21 20:01:18 -0700687 int ret = -EINVAL;
688
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300689 if (!pwrdm)
690 return -EINVAL;
691
Rajendra Nayak12627572010-12-21 20:01:18 -0700692 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
693 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
694
695 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300696}
697
698/**
699 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
700 * @pwrdm: struct powerdomain * to get previous logic power state
701 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700702 * Return the powerdomain @pwrdm's previous logic power state. Returns
703 * -EINVAL if the powerdomain pointer is null or returns the previous
704 * logic power state upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300705 */
706int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
707{
Rajendra Nayak12627572010-12-21 20:01:18 -0700708 int ret = -EINVAL;
709
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300710 if (!pwrdm)
711 return -EINVAL;
712
Rajendra Nayak12627572010-12-21 20:01:18 -0700713 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
714 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
715
716 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300717}
718
719/**
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700720 * pwrdm_read_logic_retst - get next powerdomain logic power state
721 * @pwrdm: struct powerdomain * to get next logic power state
722 *
723 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
724 * if the powerdomain pointer is null or returns the next logic
725 * power state upon success.
726 */
727int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
728{
Rajendra Nayak12627572010-12-21 20:01:18 -0700729 int ret = -EINVAL;
730
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700731 if (!pwrdm)
732 return -EINVAL;
733
Rajendra Nayak12627572010-12-21 20:01:18 -0700734 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
735 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
736
737 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700738}
739
740/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300741 * pwrdm_read_mem_pwrst - get current memory bank power state
742 * @pwrdm: struct powerdomain * to get current memory bank power state
743 * @bank: memory bank number (0-3)
744 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700745 * Return the powerdomain @pwrdm's current memory power state for bank
746 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300747 * the target memory bank does not exist or is not controllable, or
748 * returns the current memory power state upon success.
749 */
750int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
751{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700752 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300753
754 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700755 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300756
757 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700758 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300759
Thara Gopinath3863c742009-12-08 16:33:15 -0700760 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
761 bank = 1;
762
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700763 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
764 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300765
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700766 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300767}
768
769/**
770 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
771 * @pwrdm: struct powerdomain * to get previous memory bank power state
772 * @bank: memory bank number (0-3)
773 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700774 * Return the powerdomain @pwrdm's previous memory power state for
775 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
776 * -EEXIST if the target memory bank does not exist or is not
777 * controllable, or returns the previous memory power state upon
778 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300779 */
780int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
781{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700782 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300783
784 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700785 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300786
787 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700788 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300789
Thara Gopinath3863c742009-12-08 16:33:15 -0700790 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
791 bank = 1;
792
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700793 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
794 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300795
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700796 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300797}
798
799/**
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700800 * pwrdm_read_mem_retst - get next memory bank power state
801 * @pwrdm: struct powerdomain * to get mext memory bank power state
802 * @bank: memory bank number (0-3)
803 *
804 * Return the powerdomain pwrdm's next memory power state for bank
805 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
806 * the target memory bank does not exist or is not controllable, or
807 * returns the next memory power state upon success.
808 */
809int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
810{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700811 int ret = -EINVAL;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700812
813 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700814 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700815
816 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700817 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700818
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700819 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
820 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700821
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700822 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700823}
824
825/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300826 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
827 * @pwrdm: struct powerdomain * to clear
828 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700829 * Clear the powerdomain's previous power state register @pwrdm.
830 * Clears the entire register, including logic and memory bank
831 * previous power states. Returns -EINVAL if the powerdomain pointer
832 * is null, or returns 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300833 */
834int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
835{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700836 int ret = -EINVAL;
837
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300838 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700839 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300840
841 /*
842 * XXX should get the powerdomain's current state here;
843 * warn & fail if it is not ON.
844 */
845
Paul Walmsley7852ec02012-07-26 00:54:26 -0600846 pr_debug("powerdomain: %s: clearing previous power state reg\n",
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300847 pwrdm->name);
848
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700849 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
850 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300851
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700852 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300853}
854
855/**
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600856 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
857 * @pwrdm: struct powerdomain *
858 *
859 * Enable automatic context save-and-restore upon power state change
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700860 * for some devices in the powerdomain @pwrdm. Warning: this only
861 * affects a subset of devices in a powerdomain; check the TRM
862 * closely. Returns -EINVAL if the powerdomain pointer is null or if
863 * the powerdomain does not support automatic save-and-restore, or
864 * returns 0 upon success.
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600865 */
866int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
867{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700868 int ret = -EINVAL;
869
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600870 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700871 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600872
873 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700874 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600875
Paul Walmsley7852ec02012-07-26 00:54:26 -0600876 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600877
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700878 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
879 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600880
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700881 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600882}
883
884/**
885 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
886 * @pwrdm: struct powerdomain *
887 *
888 * Disable automatic context save-and-restore upon power state change
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700889 * for some devices in the powerdomain @pwrdm. Warning: this only
890 * affects a subset of devices in a powerdomain; check the TRM
891 * closely. Returns -EINVAL if the powerdomain pointer is null or if
892 * the powerdomain does not support automatic save-and-restore, or
893 * returns 0 upon success.
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600894 */
895int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
896{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700897 int ret = -EINVAL;
898
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600899 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700900 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600901
902 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700903 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600904
Paul Walmsley7852ec02012-07-26 00:54:26 -0600905 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600906
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700907 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
908 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600909
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700910 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600911}
912
913/**
914 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
915 * @pwrdm: struct powerdomain *
916 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700917 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600918 * for some devices, or 0 if it does not.
919 */
920bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
921{
922 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
923}
924
925/**
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600926 * pwrdm_set_lowpwrstchange - Request a low power state change
927 * @pwrdm: struct powerdomain *
928 *
929 * Allows a powerdomain to transtion to a lower power sleep state
930 * from an existing sleep state without waking up the powerdomain.
931 * Returns -EINVAL if the powerdomain pointer is null or if the
932 * powerdomain does not support LOWPOWERSTATECHANGE, or returns 0
933 * upon success.
934 */
935int pwrdm_set_lowpwrstchange(struct powerdomain *pwrdm)
936{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700937 int ret = -EINVAL;
938
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600939 if (!pwrdm)
940 return -EINVAL;
941
942 if (!(pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE))
943 return -EINVAL;
944
945 pr_debug("powerdomain: %s: setting LOWPOWERSTATECHANGE bit\n",
946 pwrdm->name);
947
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700948 if (arch_pwrdm && arch_pwrdm->pwrdm_set_lowpwrstchange)
949 ret = arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600950
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700951 return ret;
Rajendra Nayak90dbc7b2010-05-18 20:24:03 -0600952}
953
954/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300955 * pwrdm_wait_transition - wait for powerdomain power transition to finish
956 * @pwrdm: struct powerdomain * to wait for
957 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700958 * If the powerdomain @pwrdm is in the process of a state transition,
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300959 * spin until it completes the power transition, or until an iteration
960 * bailout value is reached. Returns -EINVAL if the powerdomain
961 * pointer is null, -EAGAIN if the bailout value was reached, or
962 * returns 0 upon success.
963 */
964int pwrdm_wait_transition(struct powerdomain *pwrdm)
965{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700966 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300967
968 if (!pwrdm)
969 return -EINVAL;
970
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700971 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
972 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300973
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700974 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300975}
976
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300977int pwrdm_state_switch(struct powerdomain *pwrdm)
978{
Santosh Shilimkar8b8c3c72012-03-12 20:04:32 +0530979 int ret;
980
981 ret = pwrdm_wait_transition(pwrdm);
982 if (!ret)
983 ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
984
985 return ret;
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300986}
987
Kevin Hilmane0555482012-05-11 16:00:24 -0700988int pwrdm_pre_transition(struct powerdomain *pwrdm)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300989{
Kevin Hilmane0555482012-05-11 16:00:24 -0700990 if (pwrdm)
991 _pwrdm_pre_transition_cb(pwrdm, NULL);
992 else
993 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
994
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300995 return 0;
996}
997
Kevin Hilmane0555482012-05-11 16:00:24 -0700998int pwrdm_post_transition(struct powerdomain *pwrdm)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300999{
Kevin Hilmane0555482012-05-11 16:00:24 -07001000 if (pwrdm)
1001 _pwrdm_post_transition_cb(pwrdm, NULL);
1002 else
1003 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
1004
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +03001005 return 0;
1006}
Kevin Hilman7f595672010-12-21 21:31:55 -07001007
1008/**
1009 * pwrdm_get_context_loss_count - get powerdomain's context loss count
1010 * @pwrdm: struct powerdomain * to wait for
1011 *
1012 * Context loss count is the sum of powerdomain off-mode counter, the
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001013 * logic off counter and the per-bank memory off counter. Returns negative
Kevin Hilman7f595672010-12-21 21:31:55 -07001014 * (and WARNs) upon error, otherwise, returns the context loss count.
1015 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001016int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
Kevin Hilman7f595672010-12-21 21:31:55 -07001017{
1018 int i, count;
1019
1020 if (!pwrdm) {
1021 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001022 return -ENODEV;
Kevin Hilman7f595672010-12-21 21:31:55 -07001023 }
1024
1025 count = pwrdm->state_counter[PWRDM_POWER_OFF];
1026 count += pwrdm->ret_logic_off_counter;
1027
1028 for (i = 0; i < pwrdm->banks; i++)
1029 count += pwrdm->ret_mem_off_counter[i];
1030
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001031 /*
1032 * Context loss count has to be a non-negative value. Clear the sign
1033 * bit to get a value range from 0 to INT_MAX.
1034 */
1035 count &= INT_MAX;
1036
1037 pr_debug("powerdomain: %s: context loss count = %d\n",
Kevin Hilman7f595672010-12-21 21:31:55 -07001038 pwrdm->name, count);
1039
1040 return count;
1041}
Paul Walmsley694606c2011-03-07 19:28:15 -07001042
1043/**
1044 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1045 * @pwrdm: struct powerdomain *
1046 *
1047 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1048 * can lose either memory or logic context or if @pwrdm is invalid, or
1049 * returns 0 otherwise. This function is not concerned with how the
1050 * powerdomain registers are programmed (i.e., to go off or not); it's
1051 * concerned with whether it's ever possible for this powerdomain to
1052 * go off while some other part of the chip is active. This function
1053 * assumes that every powerdomain can go to either ON or INACTIVE.
1054 */
1055bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
1056{
1057 int i;
1058
1059 if (IS_ERR_OR_NULL(pwrdm)) {
1060 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1061 __func__);
1062 return 1;
1063 }
1064
1065 if (pwrdm->pwrsts & PWRSTS_OFF)
1066 return 1;
1067
1068 if (pwrdm->pwrsts & PWRSTS_RET) {
1069 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1070 return 1;
1071
1072 for (i = 0; i < pwrdm->banks; i++)
1073 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1074 return 1;
1075 }
1076
1077 for (i = 0; i < pwrdm->banks; i++)
1078 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1079 return 1;
1080
1081 return 0;
1082}