blob: daf2753de7aa2a4681c690e49fb00d874c856a90 [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>
Paul Walmsley3a090282013-01-26 00:58:16 -070022#include <linux/spinlock.h>
Jean Pihet5e7c58d2011-03-03 11:25:43 +010023#include <trace/events/power.h>
24
Paul Walmsley59fb6592010-12-21 15:30:55 -070025#include "cm2xxx_3xxx.h"
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070026#include "prcm44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -070027#include "cm44xx.h"
28#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -070029#include "prm44xx.h"
Paul Walmsleyad67ef62008-08-19 11:08:40 +030030
Jean Pihet5e7c58d2011-03-03 11:25:43 +010031#include <asm/cpu.h>
Tony Lindgrendbc04162012-08-31 10:59:07 -070032
Tony Lindgrendbc04162012-08-31 10:59:07 -070033#include "powerdomain.h"
34#include "clockdomain.h"
Tero Kristo47942082014-05-11 19:41:50 -060035#include "voltage.h"
Tony Lindgrendbc04162012-08-31 10:59:07 -070036
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
Paul Walmsleyc4978fb2013-01-29 13:45:09 -070047/*
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
Abhijit Pagare3a759f02010-01-26 20:12:53 -070056
Paul Walmsleyad67ef62008-08-19 11:08:40 +030057/* pwrdm_list contains all registered struct powerdomains */
58static LIST_HEAD(pwrdm_list);
59
Rajendra Nayak3b1e8b22010-12-21 20:01:18 -070060static struct pwrdm_ops *arch_pwrdm;
61
Paul Walmsleyad67ef62008-08-19 11:08:40 +030062/* Private functions */
63
Paul Walmsleyad67ef62008-08-19 11:08:40 +030064static 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
Paul Walmsleye909d62a82010-01-26 20:13:00 -070080/**
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 */
88static int _pwrdm_register(struct powerdomain *pwrdm)
89{
90 int i;
Kevin Hilman048a7032011-03-16 15:52:47 -070091 struct voltagedomain *voltdm;
Paul Walmsleye909d62a82010-01-26 20:13:00 -070092
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070093 if (!pwrdm || !pwrdm->name)
Paul Walmsleye909d62a82010-01-26 20:13:00 -070094 return -EINVAL;
95
Paul Walmsleya64bb9c2010-12-21 21:05:14 -070096 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
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700103 if (_pwrdm_lookup(pwrdm->name))
104 return -EEXIST;
105
Rajendra Nayakcd8abed2013-06-17 18:46:22 +0530106 if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
107 if (!arch_pwrdm->pwrdm_has_voltdm())
108 goto skip_voltdm;
109
Kevin Hilman048a7032011-03-16 15:52:47 -0700110 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;
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700117 INIT_LIST_HEAD(&pwrdm->voltdm_node);
Rajendra Nayakcd8abed2013-06-17 18:46:22 +0530118skip_voltdm:
Paul Walmsley3a090282013-01-26 00:58:16 -0700119 spin_lock_init(&pwrdm->_lock);
Kevin Hilman048a7032011-03-16 15:52:47 -0700120
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700121 list_add(&pwrdm->node, &pwrdm_list);
122
123 /* Initialize the powerdomain's state counter */
Paul Walmsleycf57aa72010-01-26 20:13:01 -0700124 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700125 pwrdm->state_counter[i] = 0;
126
Thara Gopinathcde08f82010-02-24 12:05:50 -0700127 pwrdm->ret_logic_off_counter = 0;
128 for (i = 0; i < pwrdm->banks; i++)
129 pwrdm->ret_mem_off_counter[i] = 0;
130
Rajendra Nayak1cfc4bd2013-12-02 11:38:14 -0800131 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
132 arch_pwrdm->pwrdm_wait_transition(pwrdm);
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700133 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
Thara Gopinathcde08f82010-02-24 12:05:50 -0700141static 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
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300160static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
161{
162
Jean Pihetc165a142013-01-26 00:58:13 -0700163 int prev, next, state, trace_state = 0;
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300164
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]++;
Thara Gopinathcde08f82010-02-24 12:05:50 -0700178 if (prev == PWRDM_POWER_RET)
179 _update_logic_membank_counters(pwrdm);
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100180 /*
181 * If the power domain did not hit the desired state,
182 * generate a trace event with both the desired and hit states
183 */
Jean Pihetc165a142013-01-26 00:58:13 -0700184 next = pwrdm_read_next_pwrst(pwrdm);
185 if (next != prev) {
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100186 trace_state = (PWRDM_TRACE_STATES_FLAG |
Jean Pihetc165a142013-01-26 00:58:13 -0700187 ((next & OMAP_POWERSTATE_MASK) << 8) |
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100188 ((prev & OMAP_POWERSTATE_MASK) << 0));
Paul E. McKenney0b9a29a2016-04-24 14:30:16 -0700189 trace_power_domain_target_rcuidle(pwrdm->name,
190 trace_state,
191 smp_processor_id());
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100192 }
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300193 break;
194 default:
195 return -EINVAL;
196 }
197
198 if (state != prev)
199 pwrdm->state_counter[state]++;
200
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300201 pm_dbg_update_time(pwrdm, prev);
202
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300203 pwrdm->state = state;
204
205 return 0;
206}
207
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300208static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300209{
210 pwrdm_clear_all_prev_pwrst(pwrdm);
211 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
212 return 0;
213}
214
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300215static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300216{
217 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
218 return 0;
219}
220
Paul Walmsleyc4978fb2013-01-29 13:45:09 -0700221/**
222 * _pwrdm_save_clkdm_state_and_activate - prepare for power state change
223 * @pwrdm: struct powerdomain * to operate on
224 * @curr_pwrst: current power state of @pwrdm
225 * @pwrst: power state to switch to
226 * @hwsup: ptr to a bool to return whether the clkdm is hardware-supervised
227 *
228 * Determine whether the powerdomain needs to be turned on before
229 * attempting to switch power states. Called by
230 * omap_set_pwrdm_state(). NOTE that if the powerdomain contains
231 * multiple clockdomains, this code assumes that the first clockdomain
232 * supports software-supervised wakeup mode - potentially a problem.
233 * Returns the power state switch mode currently in use (see the
234 * "Types of sleep_switch" comment above).
235 */
236static u8 _pwrdm_save_clkdm_state_and_activate(struct powerdomain *pwrdm,
237 u8 curr_pwrst, u8 pwrst,
238 bool *hwsup)
239{
240 u8 sleep_switch;
241
Paul Walmsleybd70f6e2013-03-31 20:22:22 -0600242 if (curr_pwrst < PWRDM_POWER_ON) {
Paul Walmsleyc4978fb2013-01-29 13:45:09 -0700243 if (curr_pwrst > pwrst &&
244 pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
245 arch_pwrdm->pwrdm_set_lowpwrstchange) {
246 sleep_switch = LOWPOWERSTATE_SWITCH;
247 } else {
248 *hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
Paul Walmsley3a090282013-01-26 00:58:16 -0700249 clkdm_wakeup_nolock(pwrdm->pwrdm_clkdms[0]);
Paul Walmsleyc4978fb2013-01-29 13:45:09 -0700250 sleep_switch = FORCEWAKEUP_SWITCH;
251 }
252 } else {
253 sleep_switch = ALREADYACTIVE_SWITCH;
254 }
255
256 return sleep_switch;
257}
258
259/**
260 * _pwrdm_restore_clkdm_state - restore the clkdm hwsup state after pwrst change
261 * @pwrdm: struct powerdomain * to operate on
262 * @sleep_switch: return value from _pwrdm_save_clkdm_state_and_activate()
263 * @hwsup: should @pwrdm's first clockdomain be set to hardware-supervised mode?
264 *
265 * Restore the clockdomain state perturbed by
266 * _pwrdm_save_clkdm_state_and_activate(), and call the power state
267 * bookkeeping code. Called by omap_set_pwrdm_state(). NOTE that if
268 * the powerdomain contains multiple clockdomains, this assumes that
269 * the first associated clockdomain supports either
270 * hardware-supervised idle control in the register, or
271 * software-supervised sleep. No return value.
272 */
273static void _pwrdm_restore_clkdm_state(struct powerdomain *pwrdm,
274 u8 sleep_switch, bool hwsup)
275{
276 switch (sleep_switch) {
277 case FORCEWAKEUP_SWITCH:
278 if (hwsup)
Paul Walmsley3a090282013-01-26 00:58:16 -0700279 clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
Paul Walmsleyc4978fb2013-01-29 13:45:09 -0700280 else
Paul Walmsley3a090282013-01-26 00:58:16 -0700281 clkdm_sleep_nolock(pwrdm->pwrdm_clkdms[0]);
Paul Walmsleyc4978fb2013-01-29 13:45:09 -0700282 break;
283 case LOWPOWERSTATE_SWITCH:
284 if (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
285 arch_pwrdm->pwrdm_set_lowpwrstchange)
286 arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700287 pwrdm_state_switch_nolock(pwrdm);
Paul Walmsleyc4978fb2013-01-29 13:45:09 -0700288 break;
289 }
290}
291
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300292/* Public functions */
293
294/**
Paul Walmsley129c65e2011-09-14 16:01:21 -0600295 * pwrdm_register_platform_funcs - register powerdomain implementation fns
296 * @po: func pointers for arch specific implementations
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300297 *
Paul Walmsley129c65e2011-09-14 16:01:21 -0600298 * Register the list of function pointers used to implement the
299 * powerdomain functions on different OMAP SoCs. Should be called
300 * before any other pwrdm_register*() function. Returns -EINVAL if
301 * @po is null, -EEXIST if platform functions have already been
302 * registered, or 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300303 */
Paul Walmsley129c65e2011-09-14 16:01:21 -0600304int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
305{
306 if (!po)
307 return -EINVAL;
308
309 if (arch_pwrdm)
310 return -EEXIST;
311
312 arch_pwrdm = po;
313
314 return 0;
315}
316
317/**
318 * pwrdm_register_pwrdms - register SoC powerdomains
319 * @ps: pointer to an array of struct powerdomain to register
320 *
321 * Register the powerdomains available on a particular OMAP SoC. Must
322 * be called after pwrdm_register_platform_funcs(). May be called
323 * multiple times. Returns -EACCES if called before
324 * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
325 * null; or 0 upon success.
326 */
327int pwrdm_register_pwrdms(struct powerdomain **ps)
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300328{
329 struct powerdomain **p = NULL;
Paul Walmsley129c65e2011-09-14 16:01:21 -0600330
331 if (!arch_pwrdm)
332 return -EEXIST;
333
334 if (!ps)
335 return -EINVAL;
336
337 for (p = ps; *p; p++)
338 _pwrdm_register(*p);
339
340 return 0;
341}
342
343/**
344 * pwrdm_complete_init - set up the powerdomain layer
345 *
346 * Do whatever is necessary to initialize registered powerdomains and
347 * powerdomain code. Currently, this programs the next power state
348 * for each powerdomain to ON. This prevents powerdomains from
349 * unexpectedly losing context or entering high wakeup latency modes
350 * with non-power-management-enabled kernels. Must be called after
351 * pwrdm_register_pwrdms(). Returns -EACCES if called before
352 * pwrdm_register_pwrdms(), or 0 upon success.
353 */
354int pwrdm_complete_init(void)
355{
Rajendra Nayakc956b752011-08-19 16:59:39 -0600356 struct powerdomain *temp_p;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300357
Paul Walmsley129c65e2011-09-14 16:01:21 -0600358 if (list_empty(&pwrdm_list))
359 return -EACCES;
Rajendra Nayakc956b752011-08-19 16:59:39 -0600360
361 list_for_each_entry(temp_p, &pwrdm_list, node)
362 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
Paul Walmsley129c65e2011-09-14 16:01:21 -0600363
364 return 0;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300365}
366
367/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700368 * pwrdm_lock - acquire a Linux spinlock on a powerdomain
369 * @pwrdm: struct powerdomain * to lock
370 *
371 * Acquire the powerdomain spinlock on @pwrdm. No return value.
372 */
373void pwrdm_lock(struct powerdomain *pwrdm)
374 __acquires(&pwrdm->_lock)
375{
376 spin_lock_irqsave(&pwrdm->_lock, pwrdm->_lock_flags);
377}
378
379/**
380 * pwrdm_unlock - release a Linux spinlock on a powerdomain
381 * @pwrdm: struct powerdomain * to unlock
382 *
383 * Release the powerdomain spinlock on @pwrdm. No return value.
384 */
385void pwrdm_unlock(struct powerdomain *pwrdm)
386 __releases(&pwrdm->_lock)
387{
388 spin_unlock_irqrestore(&pwrdm->_lock, pwrdm->_lock_flags);
389}
390
391/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300392 * pwrdm_lookup - look up a powerdomain by name, return a pointer
393 * @name: name of powerdomain
394 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700395 * Find a registered powerdomain by its name @name. Returns a pointer
396 * to the struct powerdomain if found, or NULL otherwise.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300397 */
398struct powerdomain *pwrdm_lookup(const char *name)
399{
400 struct powerdomain *pwrdm;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300401
402 if (!name)
403 return NULL;
404
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300405 pwrdm = _pwrdm_lookup(name);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300406
407 return pwrdm;
408}
409
410/**
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700411 * pwrdm_for_each - call function on each registered clockdomain
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300412 * @fn: callback function *
413 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700414 * Call the supplied function @fn for each registered powerdomain.
415 * The callback function @fn can return anything but 0 to bail out
416 * early from the iterator. Returns the last return value of the
417 * callback function, which should be 0 for success or anything else
418 * to indicate failure; or -EINVAL if the function pointer is null.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300419 */
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700420int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
421 void *user)
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300422{
423 struct powerdomain *temp_pwrdm;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300424 int ret = 0;
425
426 if (!fn)
427 return -EINVAL;
428
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300429 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
Peter 'p2' De Schrijver6199ab22008-10-15 18:13:49 +0300430 ret = (*fn)(temp_pwrdm, user);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300431 if (ret)
432 break;
433 }
Artem Bityutskiyee894b12009-10-01 10:01:55 +0300434
435 return ret;
436}
437
438/**
Paul Walmsley8420bb12008-08-19 11:08:44 +0300439 * pwrdm_add_clkdm - add a clockdomain to a powerdomain
440 * @pwrdm: struct powerdomain * to add the clockdomain to
441 * @clkdm: struct clockdomain * to associate with a powerdomain
442 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700443 * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
Paul Walmsley8420bb12008-08-19 11:08:44 +0300444 * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
445 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
446 * or 0 upon success.
447 */
448int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
449{
Paul Walmsley8420bb12008-08-19 11:08:44 +0300450 int i;
451 int ret = -EINVAL;
452
453 if (!pwrdm || !clkdm)
454 return -EINVAL;
455
Paul Walmsley7852ec02012-07-26 00:54:26 -0600456 pr_debug("powerdomain: %s: associating clockdomain %s\n",
457 pwrdm->name, clkdm->name);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300458
Paul Walmsley8420bb12008-08-19 11:08:44 +0300459 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
460 if (!pwrdm->pwrdm_clkdms[i])
461 break;
462#ifdef DEBUG
463 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
464 ret = -EINVAL;
465 goto pac_exit;
466 }
467#endif
468 }
469
470 if (i == PWRDM_MAX_CLKDMS) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600471 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
472 pwrdm->name, clkdm->name);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300473 WARN_ON(1);
474 ret = -ENOMEM;
475 goto pac_exit;
476 }
477
478 pwrdm->pwrdm_clkdms[i] = clkdm;
479
480 ret = 0;
481
482pac_exit:
Paul Walmsley8420bb12008-08-19 11:08:44 +0300483 return ret;
484}
485
486/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300487 * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
488 * @pwrdm: struct powerdomain *
489 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700490 * Return the number of controllable memory banks in powerdomain @pwrdm,
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300491 * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
492 */
493int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
494{
495 if (!pwrdm)
496 return -EINVAL;
497
498 return pwrdm->banks;
499}
500
501/**
502 * pwrdm_set_next_pwrst - set next powerdomain power state
503 * @pwrdm: struct powerdomain * to set
504 * @pwrst: one of the PWRDM_POWER_* macros
505 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700506 * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300507 * may not enter this state immediately if the preconditions for this state
508 * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
509 * null or if the power state is invalid for the powerdomin, or returns 0
510 * upon success.
511 */
512int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
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
519 if (!(pwrdm->pwrsts & (1 << pwrst)))
520 return -EINVAL;
521
Paul Walmsley7852ec02012-07-26 00:54:26 -0600522 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300523 pwrdm->name, pwrst);
524
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100525 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
526 /* Trace the pwrdm desired target state */
Paul E. McKenney0b9a29a2016-04-24 14:30:16 -0700527 trace_power_domain_target_rcuidle(pwrdm->name, pwrst,
528 smp_processor_id());
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100529 /* Program the pwrdm desired target state */
Rajendra Nayakf327e072010-12-21 20:01:18 -0700530 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
Jean Pihet5e7c58d2011-03-03 11:25:43 +0100531 }
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300532
Rajendra Nayakf327e072010-12-21 20:01:18 -0700533 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300534}
535
536/**
537 * pwrdm_read_next_pwrst - get next powerdomain power state
538 * @pwrdm: struct powerdomain * to get power state
539 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700540 * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300541 * if the powerdomain pointer is null or returns the next power state
542 * upon success.
543 */
544int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
545{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700546 int ret = -EINVAL;
547
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300548 if (!pwrdm)
549 return -EINVAL;
550
Rajendra Nayakf327e072010-12-21 20:01:18 -0700551 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
552 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
553
554 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300555}
556
557/**
558 * pwrdm_read_pwrst - get current powerdomain power state
559 * @pwrdm: struct powerdomain * to get power state
560 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700561 * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300562 * if the powerdomain pointer is null or returns the current power state
Jon Hunterd49cae92012-06-29 16:04:52 -0500563 * upon success. Note that if the power domain only supports the ON state
564 * then just return ON as the current state.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300565 */
566int pwrdm_read_pwrst(struct powerdomain *pwrdm)
567{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700568 int ret = -EINVAL;
569
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300570 if (!pwrdm)
571 return -EINVAL;
572
Jon Hunterd49cae92012-06-29 16:04:52 -0500573 if (pwrdm->pwrsts == PWRSTS_ON)
574 return PWRDM_POWER_ON;
575
Rajendra Nayakf327e072010-12-21 20:01:18 -0700576 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
577 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
578
579 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300580}
581
582/**
583 * pwrdm_read_prev_pwrst - get previous powerdomain power state
584 * @pwrdm: struct powerdomain * to get previous power state
585 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700586 * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300587 * if the powerdomain pointer is null or returns the previous power state
588 * upon success.
589 */
590int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
591{
Rajendra Nayakf327e072010-12-21 20:01:18 -0700592 int ret = -EINVAL;
593
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300594 if (!pwrdm)
595 return -EINVAL;
596
Rajendra Nayakf327e072010-12-21 20:01:18 -0700597 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
598 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
599
600 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300601}
602
603/**
604 * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
605 * @pwrdm: struct powerdomain * to set
606 * @pwrst: one of the PWRDM_POWER_* macros
607 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700608 * Set the next power state @pwrst that the logic portion of the
609 * powerdomain @pwrdm will enter when the powerdomain enters retention.
610 * This will be either RETENTION or OFF, if supported. Returns
611 * -EINVAL if the powerdomain pointer is null or the target power
612 * state is not not supported, or returns 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300613 */
614int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
615{
Rajendra Nayak12627572010-12-21 20:01:18 -0700616 int ret = -EINVAL;
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600617
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300618 if (!pwrdm)
619 return -EINVAL;
620
621 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
622 return -EINVAL;
623
Paul Walmsley7852ec02012-07-26 00:54:26 -0600624 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300625 pwrdm->name, pwrst);
626
Rajendra Nayak12627572010-12-21 20:01:18 -0700627 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
628 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300629
Rajendra Nayak12627572010-12-21 20:01:18 -0700630 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300631}
632
633/**
634 * pwrdm_set_mem_onst - set memory power state while powerdomain ON
635 * @pwrdm: struct powerdomain * to set
636 * @bank: memory bank number to set (0-3)
637 * @pwrst: one of the PWRDM_POWER_* macros
638 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700639 * Set the next power state @pwrst that memory bank @bank of the
640 * powerdomain @pwrdm will enter when the powerdomain enters the ON
641 * state. @bank will be a number from 0 to 3, and represents different
642 * types of memory, depending on the powerdomain. Returns -EINVAL if
643 * the powerdomain pointer is null or the target power state is not
644 * not supported for this memory bank, -EEXIST if the target memory
645 * bank does not exist or is not controllable, or returns 0 upon
646 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300647 */
648int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
649{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700650 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300651
652 if (!pwrdm)
653 return -EINVAL;
654
655 if (pwrdm->banks < (bank + 1))
656 return -EEXIST;
657
658 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
659 return -EINVAL;
660
Paul Walmsley7852ec02012-07-26 00:54:26 -0600661 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
662 pwrdm->name, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300663
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700664 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
665 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300666
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700667 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300668}
669
670/**
671 * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
672 * @pwrdm: struct powerdomain * to set
673 * @bank: memory bank number to set (0-3)
674 * @pwrst: one of the PWRDM_POWER_* macros
675 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700676 * Set the next power state @pwrst that memory bank @bank of the
677 * powerdomain @pwrdm will enter when the powerdomain enters the
678 * RETENTION state. Bank will be a number from 0 to 3, and represents
679 * different types of memory, depending on the powerdomain. @pwrst
680 * will be either RETENTION or OFF, if supported. Returns -EINVAL if
681 * the powerdomain pointer is null or the target power state is not
682 * not supported for this memory bank, -EEXIST if the target memory
683 * bank does not exist or is not controllable, or returns 0 upon
684 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300685 */
686int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
687{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700688 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300689
690 if (!pwrdm)
691 return -EINVAL;
692
693 if (pwrdm->banks < (bank + 1))
694 return -EEXIST;
695
696 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
697 return -EINVAL;
698
Paul Walmsley7852ec02012-07-26 00:54:26 -0600699 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
700 pwrdm->name, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300701
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700702 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
703 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300704
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700705 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300706}
707
708/**
709 * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
710 * @pwrdm: struct powerdomain * to get current logic retention power state
711 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700712 * Return the power state that the logic portion of powerdomain @pwrdm
713 * will enter when the powerdomain enters retention. Returns -EINVAL
714 * if the powerdomain pointer is null or returns the logic retention
715 * power state upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300716 */
717int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
718{
Rajendra Nayak12627572010-12-21 20:01:18 -0700719 int ret = -EINVAL;
720
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300721 if (!pwrdm)
722 return -EINVAL;
723
Rajendra Nayak12627572010-12-21 20:01:18 -0700724 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
725 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
726
727 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300728}
729
730/**
731 * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
732 * @pwrdm: struct powerdomain * to get previous logic power state
733 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700734 * Return the powerdomain @pwrdm's previous logic power state. Returns
735 * -EINVAL if the powerdomain pointer is null or returns the previous
736 * logic power state upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300737 */
738int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
739{
Rajendra Nayak12627572010-12-21 20:01:18 -0700740 int ret = -EINVAL;
741
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300742 if (!pwrdm)
743 return -EINVAL;
744
Rajendra Nayak12627572010-12-21 20:01:18 -0700745 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
746 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
747
748 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300749}
750
751/**
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700752 * pwrdm_read_logic_retst - get next powerdomain logic power state
753 * @pwrdm: struct powerdomain * to get next logic power state
754 *
755 * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
756 * if the powerdomain pointer is null or returns the next logic
757 * power state upon success.
758 */
759int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
760{
Rajendra Nayak12627572010-12-21 20:01:18 -0700761 int ret = -EINVAL;
762
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700763 if (!pwrdm)
764 return -EINVAL;
765
Rajendra Nayak12627572010-12-21 20:01:18 -0700766 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
767 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
768
769 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700770}
771
772/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300773 * pwrdm_read_mem_pwrst - get current memory bank power state
774 * @pwrdm: struct powerdomain * to get current memory bank power state
775 * @bank: memory bank number (0-3)
776 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700777 * Return the powerdomain @pwrdm's current memory power state for bank
778 * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300779 * the target memory bank does not exist or is not controllable, or
780 * returns the current memory power state upon success.
781 */
782int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
783{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700784 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300785
786 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700787 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300788
789 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700790 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300791
Thara Gopinath3863c742009-12-08 16:33:15 -0700792 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
793 bank = 1;
794
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700795 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
796 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300797
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700798 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300799}
800
801/**
802 * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
803 * @pwrdm: struct powerdomain * to get previous memory bank power state
804 * @bank: memory bank number (0-3)
805 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700806 * Return the powerdomain @pwrdm's previous memory power state for
807 * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
808 * -EEXIST if the target memory bank does not exist or is not
809 * controllable, or returns the previous memory power state upon
810 * success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300811 */
812int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
813{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700814 int ret = -EINVAL;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300815
816 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700817 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300818
819 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700820 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300821
Thara Gopinath3863c742009-12-08 16:33:15 -0700822 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
823 bank = 1;
824
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700825 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
826 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300827
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700828 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300829}
830
831/**
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700832 * pwrdm_read_mem_retst - get next memory bank power state
833 * @pwrdm: struct powerdomain * to get mext memory bank power state
834 * @bank: memory bank number (0-3)
835 *
836 * Return the powerdomain pwrdm's next memory power state for bank
837 * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
838 * the target memory bank does not exist or is not controllable, or
839 * returns the next memory power state upon success.
840 */
841int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
842{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700843 int ret = -EINVAL;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700844
845 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700846 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700847
848 if (pwrdm->banks < (bank + 1))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700849 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700850
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700851 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
852 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700853
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700854 return ret;
Thara Gopinath1e3d0d22010-02-24 12:05:49 -0700855}
856
857/**
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300858 * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
859 * @pwrdm: struct powerdomain * to clear
860 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700861 * Clear the powerdomain's previous power state register @pwrdm.
862 * Clears the entire register, including logic and memory bank
863 * previous power states. Returns -EINVAL if the powerdomain pointer
864 * is null, or returns 0 upon success.
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300865 */
866int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
867{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700868 int ret = -EINVAL;
869
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300870 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700871 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300872
873 /*
874 * XXX should get the powerdomain's current state here;
875 * warn & fail if it is not ON.
876 */
877
Paul Walmsley7852ec02012-07-26 00:54:26 -0600878 pr_debug("powerdomain: %s: clearing previous power state reg\n",
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300879 pwrdm->name);
880
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700881 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
882 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300883
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700884 return ret;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300885}
886
887/**
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600888 * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
889 * @pwrdm: struct powerdomain *
890 *
891 * Enable automatic context save-and-restore upon power state change
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700892 * for some devices in the powerdomain @pwrdm. Warning: this only
893 * affects a subset of devices in a powerdomain; check the TRM
894 * closely. Returns -EINVAL if the powerdomain pointer is null or if
895 * the powerdomain does not support automatic save-and-restore, or
896 * returns 0 upon success.
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600897 */
898int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
899{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700900 int ret = -EINVAL;
901
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600902 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700903 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600904
905 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700906 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600907
Paul Walmsley7852ec02012-07-26 00:54:26 -0600908 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600909
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700910 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
911 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600912
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700913 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600914}
915
916/**
917 * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
918 * @pwrdm: struct powerdomain *
919 *
920 * Disable automatic context save-and-restore upon power state change
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700921 * for some devices in the powerdomain @pwrdm. Warning: this only
922 * affects a subset of devices in a powerdomain; check the TRM
923 * closely. Returns -EINVAL if the powerdomain pointer is null or if
924 * the powerdomain does not support automatic save-and-restore, or
925 * returns 0 upon success.
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600926 */
927int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
928{
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700929 int ret = -EINVAL;
930
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600931 if (!pwrdm)
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700932 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600933
934 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700935 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600936
Paul Walmsley7852ec02012-07-26 00:54:26 -0600937 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600938
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700939 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
940 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600941
Rajendra Nayak9b7fc902010-12-21 20:01:19 -0700942 return ret;
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600943}
944
945/**
946 * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
947 * @pwrdm: struct powerdomain *
948 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700949 * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
Paul Walmsley0b7cbfb2008-06-25 18:09:37 -0600950 * for some devices, or 0 if it does not.
951 */
952bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
953{
954 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
955}
956
Paul Walmsley3a090282013-01-26 00:58:16 -0700957int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300958{
Santosh Shilimkar8b8c3c72012-03-12 20:04:32 +0530959 int ret;
960
Paul Walmsleyf8457c22013-01-26 00:58:15 -0700961 if (!pwrdm || !arch_pwrdm)
962 return -EINVAL;
963
964 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
Santosh Shilimkar8b8c3c72012-03-12 20:04:32 +0530965 if (!ret)
966 ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
967
968 return ret;
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300969}
970
Paul Walmsley3a090282013-01-26 00:58:16 -0700971int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
972{
973 int ret;
974
975 pwrdm_lock(pwrdm);
976 ret = pwrdm_state_switch_nolock(pwrdm);
977 pwrdm_unlock(pwrdm);
978
979 return ret;
980}
981
Kevin Hilmane0555482012-05-11 16:00:24 -0700982int pwrdm_pre_transition(struct powerdomain *pwrdm)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300983{
Kevin Hilmane0555482012-05-11 16:00:24 -0700984 if (pwrdm)
985 _pwrdm_pre_transition_cb(pwrdm, NULL);
986 else
987 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
988
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300989 return 0;
990}
991
Kevin Hilmane0555482012-05-11 16:00:24 -0700992int pwrdm_post_transition(struct powerdomain *pwrdm)
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300993{
Kevin Hilmane0555482012-05-11 16:00:24 -0700994 if (pwrdm)
995 _pwrdm_post_transition_cb(pwrdm, NULL);
996 else
997 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
998
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300999 return 0;
1000}
Kevin Hilman7f595672010-12-21 21:31:55 -07001001
1002/**
Nishanth Menonbd002d72014-06-06 01:04:20 -05001003 * pwrdm_get_valid_lp_state() - Find best match deep power state
1004 * @pwrdm: power domain for which we want to find best match
1005 * @is_logic_state: Are we looking for logic state match here? Should
1006 * be one of PWRDM_xxx macro values
1007 * @req_state: requested power state
1008 *
1009 * Returns: closest match for requested power state. default fallback
1010 * is RET for logic state and ON for power state.
1011 *
1012 * This does a search from the power domain data looking for the
1013 * closest valid power domain state that the hardware can achieve.
1014 * PRCM definitions for PWRSTCTRL allows us to program whatever
1015 * configuration we'd like, and PRCM will actually attempt such
1016 * a transition, however if the powerdomain does not actually support it,
1017 * we endup with a hung system. The valid power domain states are already
1018 * available in our powerdomain data files. So this function tries to do
1019 * the following:
1020 * a) find if we have an exact match to the request - no issues.
1021 * b) else find if a deeper power state is possible.
1022 * c) failing which, it tries to find closest higher power state for the
1023 * request.
1024 */
1025u8 pwrdm_get_valid_lp_state(struct powerdomain *pwrdm,
1026 bool is_logic_state, u8 req_state)
1027{
1028 u8 pwrdm_states = is_logic_state ? pwrdm->pwrsts_logic_ret :
1029 pwrdm->pwrsts;
1030 /* For logic, ret is highest and others, ON is highest */
1031 u8 default_pwrst = is_logic_state ? PWRDM_POWER_RET : PWRDM_POWER_ON;
1032 u8 new_pwrst;
1033 bool found;
1034
1035 /* If it is already supported, nothing to search */
1036 if (pwrdm_states & BIT(req_state))
1037 return req_state;
1038
1039 if (!req_state)
1040 goto up_search;
1041
1042 /*
1043 * So, we dont have a exact match
1044 * Can we get a deeper power state match?
1045 */
1046 new_pwrst = req_state - 1;
1047 found = true;
1048 while (!(pwrdm_states & BIT(new_pwrst))) {
1049 /* No match even at OFF? Not available */
1050 if (new_pwrst == PWRDM_POWER_OFF) {
1051 found = false;
1052 break;
1053 }
1054 new_pwrst--;
1055 }
1056
1057 if (found)
1058 goto done;
1059
1060up_search:
1061 /* OK, no deeper ones, can we get a higher match? */
1062 new_pwrst = req_state + 1;
1063 while (!(pwrdm_states & BIT(new_pwrst))) {
1064 if (new_pwrst > PWRDM_POWER_ON) {
1065 WARN(1, "powerdomain: %s: Fix max powerstate to ON\n",
1066 pwrdm->name);
1067 return PWRDM_POWER_ON;
1068 }
1069
1070 if (new_pwrst == default_pwrst)
1071 break;
1072 new_pwrst++;
1073 }
1074done:
1075 return new_pwrst;
1076}
1077
1078/**
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001079 * omap_set_pwrdm_state - change a powerdomain's current power state
1080 * @pwrdm: struct powerdomain * to change the power state of
1081 * @pwrst: power state to change to
1082 *
1083 * Change the current hardware power state of the powerdomain
1084 * represented by @pwrdm to the power state represented by @pwrst.
1085 * Returns -EINVAL if @pwrdm is null or invalid or if the
1086 * powerdomain's current power state could not be read, or returns 0
1087 * upon success or if @pwrdm does not support @pwrst or any
1088 * lower-power state. XXX Should not return 0 if the @pwrdm does not
1089 * support @pwrst or any lower-power state: this should be an error.
1090 */
1091int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 pwrst)
1092{
Paul Walmsleybd70f6e2013-03-31 20:22:22 -06001093 u8 next_pwrst, sleep_switch;
1094 int curr_pwrst;
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001095 int ret = 0;
1096 bool hwsup = false;
1097
1098 if (!pwrdm || IS_ERR(pwrdm))
1099 return -EINVAL;
1100
1101 while (!(pwrdm->pwrsts & (1 << pwrst))) {
1102 if (pwrst == PWRDM_POWER_OFF)
1103 return ret;
1104 pwrst--;
1105 }
1106
Paul Walmsley3a090282013-01-26 00:58:16 -07001107 pwrdm_lock(pwrdm);
1108
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001109 curr_pwrst = pwrdm_read_pwrst(pwrdm);
Paul Walmsleybd70f6e2013-03-31 20:22:22 -06001110 if (curr_pwrst < 0) {
1111 ret = -EINVAL;
1112 goto osps_out;
1113 }
1114
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001115 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
1116 if (curr_pwrst == pwrst && next_pwrst == pwrst)
Paul Walmsley3a090282013-01-26 00:58:16 -07001117 goto osps_out;
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001118
1119 sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
1120 pwrst, &hwsup);
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001121
1122 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
1123 if (ret)
1124 pr_err("%s: unable to set power state of powerdomain: %s\n",
1125 __func__, pwrdm->name);
1126
1127 _pwrdm_restore_clkdm_state(pwrdm, sleep_switch, hwsup);
1128
Paul Walmsley3a090282013-01-26 00:58:16 -07001129osps_out:
1130 pwrdm_unlock(pwrdm);
1131
Paul Walmsleyc4978fb2013-01-29 13:45:09 -07001132 return ret;
1133}
1134
1135/**
Kevin Hilman7f595672010-12-21 21:31:55 -07001136 * pwrdm_get_context_loss_count - get powerdomain's context loss count
1137 * @pwrdm: struct powerdomain * to wait for
1138 *
1139 * Context loss count is the sum of powerdomain off-mode counter, the
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001140 * logic off counter and the per-bank memory off counter. Returns negative
Kevin Hilman7f595672010-12-21 21:31:55 -07001141 * (and WARNs) upon error, otherwise, returns the context loss count.
1142 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001143int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
Kevin Hilman7f595672010-12-21 21:31:55 -07001144{
1145 int i, count;
1146
1147 if (!pwrdm) {
1148 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001149 return -ENODEV;
Kevin Hilman7f595672010-12-21 21:31:55 -07001150 }
1151
1152 count = pwrdm->state_counter[PWRDM_POWER_OFF];
1153 count += pwrdm->ret_logic_off_counter;
1154
1155 for (i = 0; i < pwrdm->banks; i++)
1156 count += pwrdm->ret_mem_off_counter[i];
1157
Tomi Valkeinenfc013872011-06-09 16:56:23 +03001158 /*
1159 * Context loss count has to be a non-negative value. Clear the sign
1160 * bit to get a value range from 0 to INT_MAX.
1161 */
1162 count &= INT_MAX;
1163
1164 pr_debug("powerdomain: %s: context loss count = %d\n",
Kevin Hilman7f595672010-12-21 21:31:55 -07001165 pwrdm->name, count);
1166
1167 return count;
1168}
Paul Walmsley694606c2011-03-07 19:28:15 -07001169
1170/**
1171 * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
1172 * @pwrdm: struct powerdomain *
1173 *
1174 * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
1175 * can lose either memory or logic context or if @pwrdm is invalid, or
1176 * returns 0 otherwise. This function is not concerned with how the
1177 * powerdomain registers are programmed (i.e., to go off or not); it's
1178 * concerned with whether it's ever possible for this powerdomain to
1179 * go off while some other part of the chip is active. This function
1180 * assumes that every powerdomain can go to either ON or INACTIVE.
1181 */
1182bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
1183{
1184 int i;
1185
Russell King62f0f392013-02-24 10:55:24 +00001186 if (!pwrdm) {
Paul Walmsley694606c2011-03-07 19:28:15 -07001187 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1188 __func__);
1189 return 1;
1190 }
1191
1192 if (pwrdm->pwrsts & PWRSTS_OFF)
1193 return 1;
1194
1195 if (pwrdm->pwrsts & PWRSTS_RET) {
1196 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1197 return 1;
1198
1199 for (i = 0; i < pwrdm->banks; i++)
1200 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1201 return 1;
1202 }
1203
1204 for (i = 0; i < pwrdm->banks; i++)
1205 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1206 return 1;
1207
1208 return 0;
1209}