blob: ef668c756db74b44ed9c67a3fe1d80807df51a66 [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
Kevin Hilman74d29162012-11-14 17:13:04 -080042#ifdef CONFIG_PM
Tero Kristo908b75e2012-09-25 19:33:39 +030043/**
44 * struct omap2_oscillator - Describe the board main oscillator latencies
45 * @startup_time: oscillator startup latency
46 * @shutdown_time: oscillator shutdown latency
47 */
48struct omap2_oscillator {
49 u32 startup_time;
50 u32 shutdown_time;
51};
52
53static struct omap2_oscillator oscillator = {
54 .startup_time = ULONG_MAX,
55 .shutdown_time = ULONG_MAX,
56};
57
58void omap_pm_setup_oscillator(u32 tstart, u32 tshut)
59{
60 oscillator.startup_time = tstart;
61 oscillator.shutdown_time = tshut;
62}
63
64void omap_pm_get_oscillator(u32 *tstart, u32 *tshut)
65{
66 if (!tstart || !tshut)
67 return;
68
69 *tstart = oscillator.startup_time;
70 *tshut = oscillator.shutdown_time;
71}
Kevin Hilman74d29162012-11-14 17:13:04 -080072#endif
Tero Kristo908b75e2012-09-25 19:33:39 +030073
Kevin Hilman9cf793f2012-02-20 09:43:30 -080074static int __init _init_omap_device(char *name)
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060075{
76 struct omap_hwmod *oh;
Kevin Hilman3528c582011-07-21 13:48:45 -070077 struct platform_device *pdev;
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060078
79 oh = omap_hwmod_lookup(name);
80 if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
81 __func__, name))
82 return -ENODEV;
83
Kevin Hilman3528c582011-07-21 13:48:45 -070084 pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
85 if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060086 __func__, name))
87 return -ENODEV;
88
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060089 return 0;
90}
91
92/*
93 * Build omap_devices for processors and bus.
94 */
Kevin Hilman1f3b3722012-03-06 11:38:01 -080095static void __init omap2_init_processor_devices(void)
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060096{
Benoit Cousson766e7af2011-08-16 15:03:59 +020097 _init_omap_device("mpu");
Sanjeev Premi2de0bae2011-02-25 18:57:20 +053098 if (omap3_has_iva())
Benoit Cousson766e7af2011-08-16 15:03:59 +020099 _init_omap_device("iva");
Sanjeev Premi2de0bae2011-02-25 18:57:20 +0530100
Benoit Coussoncbf27662010-08-05 15:22:35 +0200101 if (cpu_is_omap44xx()) {
Benoit Cousson766e7af2011-08-16 15:03:59 +0200102 _init_omap_device("l3_main_1");
103 _init_omap_device("dsp");
104 _init_omap_device("iva");
Benoit Coussoncbf27662010-08-05 15:22:35 +0200105 } else {
Benoit Cousson766e7af2011-08-16 15:03:59 +0200106 _init_omap_device("l3_main");
Benoit Coussoncbf27662010-08-05 15:22:35 +0200107 }
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600108}
109
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700110/* Types of sleep_switch used in omap_set_pwrdm_state */
111#define FORCEWAKEUP_SWITCH 0
112#define LOWPOWERSTATE_SWITCH 1
113
Paul Walmsley92206fd2012-02-02 02:38:50 -0700114int __init omap_pm_clkdms_setup(struct clockdomain *clkdm, void *unused)
115{
Paul Walmsleyb71c7212012-09-23 17:28:28 -0600116 if ((clkdm->flags & CLKDM_CAN_ENABLE_AUTO) &&
117 !(clkdm->flags & CLKDM_MISSING_IDLE_REPORTING))
Paul Walmsley92206fd2012-02-02 02:38:50 -0700118 clkdm_allow_idle(clkdm);
119 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
120 atomic_read(&clkdm->usecount) == 0)
121 clkdm_sleep(clkdm);
122 return 0;
123}
124
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530125/*
126 * This sets pwrdm state (other than mpu & core. Currently only ON &
Rajendra Nayak33de32b2010-12-21 22:37:28 -0700127 * RET are supported.
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530128 */
Paul Walmsleye68e80932012-01-30 02:47:24 -0700129int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 pwrst)
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530130{
Paul Walmsleye68e80932012-01-30 02:47:24 -0700131 u8 curr_pwrst, next_pwrst;
132 int sleep_switch = -1, ret = 0, hwsup = 0;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530133
Paul Walmsleye68e80932012-01-30 02:47:24 -0700134 if (!pwrdm || IS_ERR(pwrdm))
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530135 return -EINVAL;
136
Paul Walmsleye68e80932012-01-30 02:47:24 -0700137 while (!(pwrdm->pwrsts & (1 << pwrst))) {
138 if (pwrst == PWRDM_POWER_OFF)
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530139 return ret;
Paul Walmsleye68e80932012-01-30 02:47:24 -0700140 pwrst--;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530141 }
142
Paul Walmsleye68e80932012-01-30 02:47:24 -0700143 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
144 if (next_pwrst == pwrst)
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530145 return ret;
146
Paul Walmsleye68e80932012-01-30 02:47:24 -0700147 curr_pwrst = pwrdm_read_pwrst(pwrdm);
148 if (curr_pwrst < PWRDM_POWER_ON) {
149 if ((curr_pwrst > pwrst) &&
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700150 (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
151 sleep_switch = LOWPOWERSTATE_SWITCH;
152 } else {
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600153 hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700154 clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700155 sleep_switch = FORCEWAKEUP_SWITCH;
156 }
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530157 }
158
Paul Walmsleye68e80932012-01-30 02:47:24 -0700159 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
160 if (ret)
161 pr_err("%s: unable to set power state of powerdomain: %s\n",
Johan Hovolde9a51902011-08-30 18:48:17 +0200162 __func__, pwrdm->name);
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530163
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700164 switch (sleep_switch) {
165 case FORCEWAKEUP_SWITCH:
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600166 if (hwsup)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700167 clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak33de32b2010-12-21 22:37:28 -0700168 else
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700169 clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700170 break;
171 case LOWPOWERSTATE_SWITCH:
172 pwrdm_set_lowpwrstchange(pwrdm);
Paul Walmsleye68e80932012-01-30 02:47:24 -0700173 pwrdm_wait_transition(pwrdm);
174 pwrdm_state_switch(pwrdm);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700175 break;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530176 }
177
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530178 return ret;
179}
180
Paul Walmsley14164082012-02-02 02:30:50 -0700181
182
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530183/*
Johan Hovold1e2d2df2011-08-30 18:48:16 +0200184 * This API is to be called during init to set the various voltage
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530185 * domains to the voltage as per the opp table. Typically we boot up
186 * at the nominal voltage. So this function finds out the rate of
187 * the clock associated with the voltage domain, finds out the correct
Johan Hovold1e2d2df2011-08-30 18:48:16 +0200188 * opp entry and sets the voltage domain to the voltage specified
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530189 * in the opp entry
190 */
191static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200192 const char *oh_name)
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530193{
194 struct voltagedomain *voltdm;
195 struct clk *clk;
196 struct opp *opp;
197 unsigned long freq, bootup_volt;
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200198 struct device *dev;
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530199
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200200 if (!vdd_name || !clk_name || !oh_name) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200201 pr_err("%s: invalid parameters\n", __func__);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530202 goto exit;
203 }
204
Kevin Hilman24d7b402012-09-06 14:03:08 -0700205 if (!strncmp(oh_name, "mpu", 3))
206 /*
207 * All current OMAPs share voltage rail and clock
208 * source, so CPU0 is used to represent the MPU-SS.
209 */
210 dev = get_cpu_device(0);
211 else
212 dev = omap_device_get_by_hwmod_name(oh_name);
213
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200214 if (IS_ERR(dev)) {
215 pr_err("%s: Unable to get dev pointer for hwmod %s\n",
216 __func__, oh_name);
217 goto exit;
218 }
219
Kevin Hilman81a60482011-03-16 14:25:45 -0700220 voltdm = voltdm_lookup(vdd_name);
Wei Yongjun93b44be2012-09-27 13:54:36 +0800221 if (!voltdm) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200222 pr_err("%s: unable to get vdd pointer for vdd_%s\n",
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530223 __func__, vdd_name);
224 goto exit;
225 }
226
227 clk = clk_get(NULL, clk_name);
228 if (IS_ERR(clk)) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200229 pr_err("%s: unable to get clk %s\n", __func__, clk_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530230 goto exit;
231 }
232
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600233 freq = clk_get_rate(clk);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530234 clk_put(clk);
235
NeilBrown6369fd42012-01-09 13:14:12 +1100236 rcu_read_lock();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530237 opp = opp_find_freq_ceil(dev, &freq);
238 if (IS_ERR(opp)) {
NeilBrown6369fd42012-01-09 13:14:12 +1100239 rcu_read_unlock();
Johan Hovolde9a51902011-08-30 18:48:17 +0200240 pr_err("%s: unable to find boot up OPP for vdd_%s\n",
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530241 __func__, vdd_name);
242 goto exit;
243 }
244
245 bootup_volt = opp_get_voltage(opp);
NeilBrown6369fd42012-01-09 13:14:12 +1100246 rcu_read_unlock();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530247 if (!bootup_volt) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600248 pr_err("%s: unable to find voltage corresponding to the bootup OPP for vdd_%s\n",
249 __func__, vdd_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530250 goto exit;
251 }
252
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700253 voltdm_scale(voltdm, bootup_volt);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530254 return 0;
255
256exit:
Johan Hovolde9a51902011-08-30 18:48:17 +0200257 pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530258 return -EINVAL;
259}
260
Paul Walmsley14164082012-02-02 02:30:50 -0700261#ifdef CONFIG_SUSPEND
262static int omap_pm_enter(suspend_state_t suspend_state)
263{
264 int ret = 0;
265
266 if (!omap_pm_suspend)
267 return -ENOENT; /* XXX doublecheck */
268
269 switch (suspend_state) {
270 case PM_SUSPEND_STANDBY:
271 case PM_SUSPEND_MEM:
272 ret = omap_pm_suspend();
273 break;
274 default:
275 ret = -EINVAL;
276 }
277
278 return ret;
279}
280
281static int omap_pm_begin(suspend_state_t state)
282{
283 disable_hlt();
284 if (cpu_is_omap34xx())
285 omap_prcm_irq_prepare();
286 return 0;
287}
288
289static void omap_pm_end(void)
290{
291 enable_hlt();
292 return;
293}
294
295static void omap_pm_finish(void)
296{
297 if (cpu_is_omap34xx())
298 omap_prcm_irq_complete();
299}
300
301static const struct platform_suspend_ops omap_pm_ops = {
302 .begin = omap_pm_begin,
303 .end = omap_pm_end,
304 .enter = omap_pm_enter,
305 .finish = omap_pm_finish,
306 .valid = suspend_valid_only_mem,
307};
308
309#endif /* CONFIG_SUSPEND */
310
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530311static void __init omap3_init_voltages(void)
312{
313 if (!cpu_is_omap34xx())
314 return;
315
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200316 omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
317 omap2_set_init_voltage("core", "l3_ick", "l3_main");
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530318}
319
Thara Gopinath1376ee12010-05-29 22:02:25 +0530320static void __init omap4_init_voltages(void)
321{
322 if (!cpu_is_omap44xx())
323 return;
324
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200325 omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
326 omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
327 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
Thara Gopinath1376ee12010-05-29 22:02:25 +0530328}
329
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600330static int __init omap2_common_pm_init(void)
331{
Benoit Cousson476b6792011-08-16 11:49:08 +0200332 if (!of_have_populated_dt())
333 omap2_init_processor_devices();
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600334 omap_pm_if_init();
335
336 return 0;
337}
Thara Gopinath1cbbe372010-12-20 21:17:21 +0530338postcore_initcall(omap2_common_pm_init);
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600339
Shawn Guobbd707a2012-04-26 16:06:50 +0800340int __init omap2_common_pm_late_init(void)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530341{
Benoit Cousson506d81e2011-12-08 16:47:39 +0100342 /*
343 * In the case of DT, the PMIC and SR initialization will be done using
344 * a completely different mechanism.
345 * Disable this part if a DT blob is available.
346 */
347 if (of_have_populated_dt())
348 return 0;
349
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530350 /* Init the voltage layer */
Kevin Hilman46232a32011-11-23 14:43:01 -0800351 omap_pmic_late_init();
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530352 omap_voltage_late_init();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530353
354 /* Initialize the voltages */
355 omap3_init_voltages();
Thara Gopinath1376ee12010-05-29 22:02:25 +0530356 omap4_init_voltages();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530357
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530358 /* Smartreflex device init */
Thara Gopinath0c0a5d62010-05-29 22:02:23 +0530359 omap_devinit_smartreflex();
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530360
Paul Walmsley14164082012-02-02 02:30:50 -0700361#ifdef CONFIG_SUSPEND
362 suspend_set_ops(&omap_pm_ops);
363#endif
364
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530365 return 0;
366}