blob: 85eef16af22c3f89c203250b4686fc6152e45234 [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>
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060018
19#include <plat/omap-pm.h>
20#include <plat/omap_device.h>
21#include <plat/common.h>
22
Paul Walmsleye1d6f472011-02-25 15:54:33 -070023#include "voltage.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -070024#include "powerdomain.h"
Paul Walmsley1540f2142010-12-21 21:05:15 -070025#include "clockdomain.h"
Thara Gopinath0c0a5d62010-05-29 22:02:23 +053026#include "pm.h"
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +053027
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060028static struct omap_device_pm_latency *pm_lats;
29
30static struct device *mpu_dev;
Thara Gopinathb3294e22010-09-01 13:44:53 +053031static struct device *iva_dev;
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060032static struct device *l3_dev;
Thara Gopinathb3294e22010-09-01 13:44:53 +053033static struct device *dsp_dev;
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060034
35struct device *omap2_get_mpuss_device(void)
36{
37 WARN_ON_ONCE(!mpu_dev);
38 return mpu_dev;
39}
40
Thara Gopinathb3294e22010-09-01 13:44:53 +053041struct device *omap2_get_iva_device(void)
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060042{
Thara Gopinathb3294e22010-09-01 13:44:53 +053043 WARN_ON_ONCE(!iva_dev);
44 return iva_dev;
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060045}
46
47struct device *omap2_get_l3_device(void)
48{
49 WARN_ON_ONCE(!l3_dev);
50 return l3_dev;
51}
52
Thara Gopinathb3294e22010-09-01 13:44:53 +053053struct device *omap4_get_dsp_device(void)
54{
55 WARN_ON_ONCE(!dsp_dev);
56 return dsp_dev;
57}
58EXPORT_SYMBOL(omap4_get_dsp_device);
59
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060060/* static int _init_omap_device(struct omap_hwmod *oh, void *user) */
61static int _init_omap_device(char *name, struct device **new_dev)
62{
63 struct omap_hwmod *oh;
64 struct omap_device *od;
65
66 oh = omap_hwmod_lookup(name);
67 if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
68 __func__, name))
69 return -ENODEV;
70
71 od = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
72 if (WARN(IS_ERR(od), "%s: could not build omap_device for %s\n",
73 __func__, name))
74 return -ENODEV;
75
76 *new_dev = &od->pdev.dev;
77
78 return 0;
79}
80
81/*
82 * Build omap_devices for processors and bus.
83 */
84static void omap2_init_processor_devices(void)
85{
86 _init_omap_device("mpu", &mpu_dev);
Sanjeev Premi2de0bae2011-02-25 18:57:20 +053087 if (omap3_has_iva())
88 _init_omap_device("iva", &iva_dev);
89
Benoit Coussoncbf27662010-08-05 15:22:35 +020090 if (cpu_is_omap44xx()) {
91 _init_omap_device("l3_main_1", &l3_dev);
Thara Gopinathb3294e22010-09-01 13:44:53 +053092 _init_omap_device("dsp", &dsp_dev);
Shweta Gulati91968642011-04-26 02:32:26 -070093 _init_omap_device("iva", &iva_dev);
Benoit Coussoncbf27662010-08-05 15:22:35 +020094 } else {
95 _init_omap_device("l3_main", &l3_dev);
96 }
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060097}
98
Rajendra Nayak71a488d2010-12-21 22:37:27 -070099/* Types of sleep_switch used in omap_set_pwrdm_state */
100#define FORCEWAKEUP_SWITCH 0
101#define LOWPOWERSTATE_SWITCH 1
102
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530103/*
104 * This sets pwrdm state (other than mpu & core. Currently only ON &
Rajendra Nayak33de32b2010-12-21 22:37:28 -0700105 * RET are supported.
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530106 */
107int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
108{
109 u32 cur_state;
Rajendra Nayak6349b962011-07-09 20:42:11 -0600110 int sleep_switch = -1;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530111 int ret = 0;
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600112 int hwsup = 0;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530113
114 if (pwrdm == NULL || IS_ERR(pwrdm))
115 return -EINVAL;
116
117 while (!(pwrdm->pwrsts & (1 << state))) {
118 if (state == PWRDM_POWER_OFF)
119 return ret;
120 state--;
121 }
122
123 cur_state = pwrdm_read_next_pwrst(pwrdm);
124 if (cur_state == state)
125 return ret;
126
127 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700128 if ((pwrdm_read_pwrst(pwrdm) > state) &&
129 (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
130 sleep_switch = LOWPOWERSTATE_SWITCH;
131 } else {
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600132 hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700133 clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700134 sleep_switch = FORCEWAKEUP_SWITCH;
135 }
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530136 }
137
138 ret = pwrdm_set_next_pwrst(pwrdm, state);
139 if (ret) {
140 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
141 pwrdm->name);
142 goto err;
143 }
144
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700145 switch (sleep_switch) {
146 case FORCEWAKEUP_SWITCH:
Rajendra Nayakb86cfb52011-07-10 05:56:54 -0600147 if (hwsup)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700148 clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak33de32b2010-12-21 22:37:28 -0700149 else
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700150 clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700151 break;
152 case LOWPOWERSTATE_SWITCH:
153 pwrdm_set_lowpwrstchange(pwrdm);
154 break;
155 default:
156 return ret;
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530157 }
158
Rajendra Nayak71a488d2010-12-21 22:37:27 -0700159 pwrdm_state_switch(pwrdm);
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +0530160err:
161 return ret;
162}
163
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530164/*
165 * This API is to be called during init to put the various voltage
166 * domains to the voltage as per the opp table. Typically we boot up
167 * at the nominal voltage. So this function finds out the rate of
168 * the clock associated with the voltage domain, finds out the correct
169 * opp entry and puts the voltage domain to the voltage specifies
170 * in the opp entry
171 */
172static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
173 struct device *dev)
174{
175 struct voltagedomain *voltdm;
176 struct clk *clk;
177 struct opp *opp;
178 unsigned long freq, bootup_volt;
179
180 if (!vdd_name || !clk_name || !dev) {
181 printk(KERN_ERR "%s: Invalid parameters!\n", __func__);
182 goto exit;
183 }
184
185 voltdm = omap_voltage_domain_lookup(vdd_name);
186 if (IS_ERR(voltdm)) {
187 printk(KERN_ERR "%s: Unable to get vdd pointer for vdd_%s\n",
188 __func__, vdd_name);
189 goto exit;
190 }
191
192 clk = clk_get(NULL, clk_name);
193 if (IS_ERR(clk)) {
194 printk(KERN_ERR "%s: unable to get clk %s\n",
195 __func__, clk_name);
196 goto exit;
197 }
198
199 freq = clk->rate;
200 clk_put(clk);
201
202 opp = opp_find_freq_ceil(dev, &freq);
203 if (IS_ERR(opp)) {
204 printk(KERN_ERR "%s: unable to find boot up OPP for vdd_%s\n",
205 __func__, vdd_name);
206 goto exit;
207 }
208
209 bootup_volt = opp_get_voltage(opp);
210 if (!bootup_volt) {
211 printk(KERN_ERR "%s: unable to find voltage corresponding"
212 "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
213 goto exit;
214 }
215
216 omap_voltage_scale_vdd(voltdm, bootup_volt);
217 return 0;
218
219exit:
220 printk(KERN_ERR "%s: Unable to put vdd_%s to its init voltage\n\n",
221 __func__, vdd_name);
222 return -EINVAL;
223}
224
225static void __init omap3_init_voltages(void)
226{
227 if (!cpu_is_omap34xx())
228 return;
229
230 omap2_set_init_voltage("mpu", "dpll1_ck", mpu_dev);
231 omap2_set_init_voltage("core", "l3_ick", l3_dev);
232}
233
Thara Gopinath1376ee12010-05-29 22:02:25 +0530234static void __init omap4_init_voltages(void)
235{
236 if (!cpu_is_omap44xx())
237 return;
238
239 omap2_set_init_voltage("mpu", "dpll_mpu_ck", mpu_dev);
240 omap2_set_init_voltage("core", "l3_div_ck", l3_dev);
241 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", iva_dev);
242}
243
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600244static int __init omap2_common_pm_init(void)
245{
246 omap2_init_processor_devices();
247 omap_pm_if_init();
248
249 return 0;
250}
Thara Gopinath1cbbe372010-12-20 21:17:21 +0530251postcore_initcall(omap2_common_pm_init);
Kevin Hilman6f88e9b2010-07-26 16:34:31 -0600252
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530253static int __init omap2_common_pm_late_init(void)
254{
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530255 /* Init the OMAP TWL parameters */
256 omap3_twl_init();
Thara Gopinath7bc3ed92010-12-10 23:15:16 +0530257 omap4_twl_init();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530258
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530259 /* Init the voltage layer */
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530260 omap_voltage_late_init();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530261
262 /* Initialize the voltages */
263 omap3_init_voltages();
Thara Gopinath1376ee12010-05-29 22:02:25 +0530264 omap4_init_voltages();
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530265
Thara Gopinathfbc319f2010-12-10 22:51:05 +0530266 /* Smartreflex device init */
Thara Gopinath0c0a5d62010-05-29 22:02:23 +0530267 omap_devinit_smartreflex();
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530268
269 return 0;
270}
271late_initcall(omap2_common_pm_late_init);