blob: 109a02e02d72eb6fa72d7e60d129e87a753081e0 [file] [log] [blame]
Kevin Hilman6f88e9b2010-07-26 16:34:31 -06001/*
2 * pm.c - Common OMAP2+ power management-related code
3 *
4 * Copyright (C) 2010 Texas Instruments, Inc.
5 * Copyright (C) 2010 Nokia Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/io.h>
15#include <linux/err.h>
Thara Gopinath1482d8b2010-05-29 22:02:25 +053016#include <linux/opp.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040017#include <linux/export.h>
Paul Walmsley14164082012-02-02 02:30:50 -070018#include <linux/suspend.h>
Kevin Hilman24d7b402012-09-06 14:03:08 -070019#include <linux/cpu.h>
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060020
Govindraj.R335aece2012-03-29 09:30:28 -070021#include <asm/system_misc.h>
22
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060023#include <plat/omap-pm.h>
24#include <plat/omap_device.h>
Tony Lindgren4e653312011-11-10 22:45:17 +010025#include "common.h"
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060026
Paul Walmsley14164082012-02-02 02:30:50 -070027#include "prcm-common.h"
Paul Walmsleye1d6f472011-02-25 15:54:33 -070028#include "voltage.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -070029#include "powerdomain.h"
Paul Walmsley1540f2142010-12-21 21:05:15 -070030#include "clockdomain.h"
Thara Gopinath0c0a5d62010-05-29 22:02:23 +053031#include "pm.h"
Kevin Hilman46232a32011-11-23 14:43:01 -080032#include "twl-common.h"
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +053033
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060034static struct omap_device_pm_latency *pm_lats;
35
Paul Walmsley14164082012-02-02 02:30:50 -070036/*
37 * omap_pm_suspend: points to a function that does the SoC-specific
38 * suspend work
39 */
40int (*omap_pm_suspend)(void);
41
Tero Kristo908b75e2012-09-25 19:33:39 +030042/**
43 * struct omap2_oscillator - Describe the board main oscillator latencies
44 * @startup_time: oscillator startup latency
45 * @shutdown_time: oscillator shutdown latency
46 */
47struct omap2_oscillator {
48 u32 startup_time;
49 u32 shutdown_time;
50};
51
52static struct omap2_oscillator oscillator = {
53 .startup_time = ULONG_MAX,
54 .shutdown_time = ULONG_MAX,
55};
56
57void omap_pm_setup_oscillator(u32 tstart, u32 tshut)
58{
59 oscillator.startup_time = tstart;
60 oscillator.shutdown_time = tshut;
61}
62
63void omap_pm_get_oscillator(u32 *tstart, u32 *tshut)
64{
65 if (!tstart || !tshut)
66 return;
67
68 *tstart = oscillator.startup_time;
69 *tshut = oscillator.shutdown_time;
70}
71
Kevin Hilman9cf793f2012-02-20 09:43:30 -080072static int __init _init_omap_device(char *name)
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060073{
74 struct omap_hwmod *oh;
Kevin Hilman3528c582011-07-21 13:48:45 -070075 struct platform_device *pdev;
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060076
77 oh = omap_hwmod_lookup(name);
78 if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
79 __func__, name))
80 return -ENODEV;
81
Kevin Hilman3528c582011-07-21 13:48:45 -070082 pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
83 if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060084 __func__, name))
85 return -ENODEV;
86
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060087 return 0;
88}
89
90/*
91 * Build omap_devices for processors and bus.
92 */
Kevin Hilman1f3b3722012-03-06 11:38:01 -080093static void __init omap2_init_processor_devices(void)
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060094{
Benoit Cousson766e7af2011-08-16 15:03:59 +020095 _init_omap_device("mpu");
Sanjeev Premi2de0bae2011-02-25 18:57:20 +053096 if (omap3_has_iva())
Benoit Cousson766e7af2011-08-16 15:03:59 +020097 _init_omap_device("iva");
Sanjeev Premi2de0bae2011-02-25 18:57:20 +053098
Benoit Coussoncbf27662010-08-05 15:22:35 +020099 if (cpu_is_omap44xx()) {
Benoit Cousson766e7af2011-08-16 15:03:59 +0200100 _init_omap_device("l3_main_1");
101 _init_omap_device("dsp");
102 _init_omap_device("iva");
Benoit Coussoncbf27662010-08-05 15:22:35 +0200103 } else {
Benoit Cousson766e7af2011-08-16 15:03:59 +0200104 _init_omap_device("l3_main");
Benoit Coussoncbf27662010-08-05 15:22:35 +0200105 }
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600106}
107
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700108/* Types of sleep_switch used in omap_set_pwrdm_state */
109#define FORCEWAKEUP_SWITCH 0
110#define LOWPOWERSTATE_SWITCH 1
111
Paul Walmsley92206fd2012-02-02 02:38:50 -0700112int __init omap_pm_clkdms_setup(struct clockdomain *clkdm, void *unused)
113{
Paul Walmsleyb71c7212012-09-23 17:28:28 -0600114 if ((clkdm->flags & CLKDM_CAN_ENABLE_AUTO) &&
115 !(clkdm->flags & CLKDM_MISSING_IDLE_REPORTING))
Paul Walmsley92206fd2012-02-02 02:38:50 -0700116 clkdm_allow_idle(clkdm);
117 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
118 atomic_read(&clkdm->usecount) == 0)
119 clkdm_sleep(clkdm);
120 return 0;
121}
122
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530123/*
124 * This sets pwrdm state (other than mpu & core. Currently only ON &
Rajendra Nayak33de32b2010-12-21 22:37:28 -0700125 * RET are supported.
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530126 */
Paul Walmsleye68e80932012-01-30 02:47:24 -0700127int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 pwrst)
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530128{
Paul Walmsleye68e80932012-01-30 02:47:24 -0700129 u8 curr_pwrst, next_pwrst;
130 int sleep_switch = -1, ret = 0, hwsup = 0;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530131
Paul Walmsleye68e80932012-01-30 02:47:24 -0700132 if (!pwrdm || IS_ERR(pwrdm))
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530133 return -EINVAL;
134
Paul Walmsleye68e80932012-01-30 02:47:24 -0700135 while (!(pwrdm->pwrsts & (1 << pwrst))) {
136 if (pwrst == PWRDM_POWER_OFF)
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530137 return ret;
Paul Walmsleye68e80932012-01-30 02:47:24 -0700138 pwrst--;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530139 }
140
Paul Walmsleye68e80932012-01-30 02:47:24 -0700141 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
142 if (next_pwrst == pwrst)
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530143 return ret;
144
Paul Walmsleye68e80932012-01-30 02:47:24 -0700145 curr_pwrst = pwrdm_read_pwrst(pwrdm);
146 if (curr_pwrst < PWRDM_POWER_ON) {
147 if ((curr_pwrst > pwrst) &&
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700148 (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
149 sleep_switch = LOWPOWERSTATE_SWITCH;
150 } else {
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600151 hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700152 clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700153 sleep_switch = FORCEWAKEUP_SWITCH;
154 }
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530155 }
156
Paul Walmsleye68e80932012-01-30 02:47:24 -0700157 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
158 if (ret)
159 pr_err("%s: unable to set power state of powerdomain: %s\n",
Johan Hovolde9a51902011-08-30 18:48:17 +0200160 __func__, pwrdm->name);
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530161
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700162 switch (sleep_switch) {
163 case FORCEWAKEUP_SWITCH:
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600164 if (hwsup)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700165 clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak33de32b2010-12-21 22:37:28 -0700166 else
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700167 clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700168 break;
169 case LOWPOWERSTATE_SWITCH:
170 pwrdm_set_lowpwrstchange(pwrdm);
Paul Walmsleye68e80932012-01-30 02:47:24 -0700171 pwrdm_wait_transition(pwrdm);
172 pwrdm_state_switch(pwrdm);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700173 break;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530174 }
175
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530176 return ret;
177}
178
Paul Walmsley14164082012-02-02 02:30:50 -0700179
180
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530181/*
Johan Hovold1e2d2df2011-08-30 18:48:16 +0200182 * This API is to be called during init to set the various voltage
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530183 * domains to the voltage as per the opp table. Typically we boot up
184 * at the nominal voltage. So this function finds out the rate of
185 * the clock associated with the voltage domain, finds out the correct
Johan Hovold1e2d2df2011-08-30 18:48:16 +0200186 * opp entry and sets the voltage domain to the voltage specified
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530187 * in the opp entry
188 */
189static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200190 const char *oh_name)
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530191{
192 struct voltagedomain *voltdm;
193 struct clk *clk;
194 struct opp *opp;
195 unsigned long freq, bootup_volt;
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200196 struct device *dev;
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530197
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200198 if (!vdd_name || !clk_name || !oh_name) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200199 pr_err("%s: invalid parameters\n", __func__);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530200 goto exit;
201 }
202
Kevin Hilman24d7b402012-09-06 14:03:08 -0700203 if (!strncmp(oh_name, "mpu", 3))
204 /*
205 * All current OMAPs share voltage rail and clock
206 * source, so CPU0 is used to represent the MPU-SS.
207 */
208 dev = get_cpu_device(0);
209 else
210 dev = omap_device_get_by_hwmod_name(oh_name);
211
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200212 if (IS_ERR(dev)) {
213 pr_err("%s: Unable to get dev pointer for hwmod %s\n",
214 __func__, oh_name);
215 goto exit;
216 }
217
Kevin Hilman81a60482011-03-16 14:25:45 -0700218 voltdm = voltdm_lookup(vdd_name);
Wei Yongjun93b44be2012-09-27 13:54:36 +0800219 if (!voltdm) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200220 pr_err("%s: unable to get vdd pointer for vdd_%s\n",
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530221 __func__, vdd_name);
222 goto exit;
223 }
224
225 clk = clk_get(NULL, clk_name);
226 if (IS_ERR(clk)) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200227 pr_err("%s: unable to get clk %s\n", __func__, clk_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530228 goto exit;
229 }
230
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600231 freq = clk_get_rate(clk);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530232 clk_put(clk);
233
NeilBrown6369fd42012-01-09 13:14:12 +1100234 rcu_read_lock();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530235 opp = opp_find_freq_ceil(dev, &freq);
236 if (IS_ERR(opp)) {
NeilBrown6369fd42012-01-09 13:14:12 +1100237 rcu_read_unlock();
Johan Hovolde9a51902011-08-30 18:48:17 +0200238 pr_err("%s: unable to find boot up OPP for vdd_%s\n",
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530239 __func__, vdd_name);
240 goto exit;
241 }
242
243 bootup_volt = opp_get_voltage(opp);
NeilBrown6369fd42012-01-09 13:14:12 +1100244 rcu_read_unlock();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530245 if (!bootup_volt) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600246 pr_err("%s: unable to find voltage corresponding to the bootup OPP for vdd_%s\n",
247 __func__, vdd_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530248 goto exit;
249 }
250
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700251 voltdm_scale(voltdm, bootup_volt);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530252 return 0;
253
254exit:
Johan Hovolde9a51902011-08-30 18:48:17 +0200255 pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530256 return -EINVAL;
257}
258
Paul Walmsley14164082012-02-02 02:30:50 -0700259#ifdef CONFIG_SUSPEND
260static int omap_pm_enter(suspend_state_t suspend_state)
261{
262 int ret = 0;
263
264 if (!omap_pm_suspend)
265 return -ENOENT; /* XXX doublecheck */
266
267 switch (suspend_state) {
268 case PM_SUSPEND_STANDBY:
269 case PM_SUSPEND_MEM:
270 ret = omap_pm_suspend();
271 break;
272 default:
273 ret = -EINVAL;
274 }
275
276 return ret;
277}
278
279static int omap_pm_begin(suspend_state_t state)
280{
281 disable_hlt();
282 if (cpu_is_omap34xx())
283 omap_prcm_irq_prepare();
284 return 0;
285}
286
287static void omap_pm_end(void)
288{
289 enable_hlt();
290 return;
291}
292
293static void omap_pm_finish(void)
294{
295 if (cpu_is_omap34xx())
296 omap_prcm_irq_complete();
297}
298
299static const struct platform_suspend_ops omap_pm_ops = {
300 .begin = omap_pm_begin,
301 .end = omap_pm_end,
302 .enter = omap_pm_enter,
303 .finish = omap_pm_finish,
304 .valid = suspend_valid_only_mem,
305};
306
307#endif /* CONFIG_SUSPEND */
308
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530309static void __init omap3_init_voltages(void)
310{
311 if (!cpu_is_omap34xx())
312 return;
313
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200314 omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
315 omap2_set_init_voltage("core", "l3_ick", "l3_main");
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530316}
317
Thara Gopinath1376ee12010-05-29 22:02:25 +0530318static void __init omap4_init_voltages(void)
319{
320 if (!cpu_is_omap44xx())
321 return;
322
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200323 omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
324 omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
325 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
Thara Gopinath1376ee12010-05-29 22:02:25 +0530326}
327
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600328static int __init omap2_common_pm_init(void)
329{
Benoit Cousson476b6792011-08-16 11:49:08 +0200330 if (!of_have_populated_dt())
331 omap2_init_processor_devices();
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600332 omap_pm_if_init();
333
334 return 0;
335}
Thara Gopinath1cbbe372010-12-20 21:17:21 +0530336postcore_initcall(omap2_common_pm_init);
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600337
Shawn Guobbd707a2012-04-26 16:06:50 +0800338int __init omap2_common_pm_late_init(void)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530339{
Benoit Cousson506d81e2011-12-08 16:47:39 +0100340 /*
341 * In the case of DT, the PMIC and SR initialization will be done using
342 * a completely different mechanism.
343 * Disable this part if a DT blob is available.
344 */
345 if (of_have_populated_dt())
346 return 0;
347
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530348 /* Init the voltage layer */
Kevin Hilman46232a32011-11-23 14:43:01 -0800349 omap_pmic_late_init();
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530350 omap_voltage_late_init();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530351
352 /* Initialize the voltages */
353 omap3_init_voltages();
Thara Gopinath1376ee12010-05-29 22:02:25 +0530354 omap4_init_voltages();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530355
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530356 /* Smartreflex device init */
Thara Gopinath0c0a5d62010-05-29 22:02:23 +0530357 omap_devinit_smartreflex();
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530358
Paul Walmsley14164082012-02-02 02:30:50 -0700359#ifdef CONFIG_SUSPEND
360 suspend_set_ops(&omap_pm_ops);
361#endif
362
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530363 return 0;
364}