Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / arch / arm / mach-omap2 / pm.c
blobd507278837d9b7dd53ed31d2410e1a2ebe8b4d5e
1 /*
2 * pm.c - Common OMAP2+ power management-related code
4 * Copyright (C) 2010 Texas Instruments, Inc.
5 * Copyright (C) 2010 Nokia Corporation
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.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/err.h>
16 #include <linux/opp.h>
17 #include <linux/export.h>
19 #include <plat/omap-pm.h>
20 #include <plat/omap_device.h>
21 #include <plat/common.h>
23 #include "voltage.h"
24 #include "powerdomain.h"
25 #include "clockdomain.h"
26 #include "pm.h"
28 static struct omap_device_pm_latency *pm_lats;
30 static struct device *mpu_dev;
31 static struct device *iva_dev;
32 static struct device *l3_dev;
33 static struct device *dsp_dev;
35 struct device *omap2_get_mpuss_device(void)
37 WARN_ON_ONCE(!mpu_dev);
38 return mpu_dev;
41 struct device *omap2_get_iva_device(void)
43 WARN_ON_ONCE(!iva_dev);
44 return iva_dev;
47 struct device *omap2_get_l3_device(void)
49 WARN_ON_ONCE(!l3_dev);
50 return l3_dev;
53 struct device *omap4_get_dsp_device(void)
55 WARN_ON_ONCE(!dsp_dev);
56 return dsp_dev;
58 EXPORT_SYMBOL(omap4_get_dsp_device);
60 /* static int _init_omap_device(struct omap_hwmod *oh, void *user) */
61 static int _init_omap_device(char *name, struct device **new_dev)
63 struct omap_hwmod *oh;
64 struct omap_device *od;
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;
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;
76 *new_dev = &od->pdev.dev;
78 return 0;
82 * Build omap_devices for processors and bus.
84 static void omap2_init_processor_devices(void)
86 _init_omap_device("mpu", &mpu_dev);
87 if (omap3_has_iva())
88 _init_omap_device("iva", &iva_dev);
90 if (cpu_is_omap44xx()) {
91 _init_omap_device("l3_main_1", &l3_dev);
92 _init_omap_device("dsp", &dsp_dev);
93 _init_omap_device("iva", &iva_dev);
94 } else {
95 _init_omap_device("l3_main", &l3_dev);
99 /* Types of sleep_switch used in omap_set_pwrdm_state */
100 #define FORCEWAKEUP_SWITCH 0
101 #define LOWPOWERSTATE_SWITCH 1
104 * This sets pwrdm state (other than mpu & core. Currently only ON &
105 * RET are supported.
107 int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
109 u32 cur_state;
110 int sleep_switch = -1;
111 int ret = 0;
112 int hwsup = 0;
114 if (pwrdm == NULL || IS_ERR(pwrdm))
115 return -EINVAL;
117 while (!(pwrdm->pwrsts & (1 << state))) {
118 if (state == PWRDM_POWER_OFF)
119 return ret;
120 state--;
123 cur_state = pwrdm_read_next_pwrst(pwrdm);
124 if (cur_state == state)
125 return ret;
127 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
128 if ((pwrdm_read_pwrst(pwrdm) > state) &&
129 (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
130 sleep_switch = LOWPOWERSTATE_SWITCH;
131 } else {
132 hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
133 clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
134 pwrdm_wait_transition(pwrdm);
135 sleep_switch = FORCEWAKEUP_SWITCH;
139 ret = pwrdm_set_next_pwrst(pwrdm, state);
140 if (ret) {
141 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
142 pwrdm->name);
143 goto err;
146 switch (sleep_switch) {
147 case FORCEWAKEUP_SWITCH:
148 if (hwsup)
149 clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
150 else
151 clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
152 break;
153 case LOWPOWERSTATE_SWITCH:
154 pwrdm_set_lowpwrstchange(pwrdm);
155 break;
156 default:
157 return ret;
160 pwrdm_wait_transition(pwrdm);
161 pwrdm_state_switch(pwrdm);
162 err:
163 return ret;
167 * This API is to be called during init to put the various voltage
168 * domains to the voltage as per the opp table. Typically we boot up
169 * at the nominal voltage. So this function finds out the rate of
170 * the clock associated with the voltage domain, finds out the correct
171 * opp entry and puts the voltage domain to the voltage specifies
172 * in the opp entry
174 static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
175 struct device *dev)
177 struct voltagedomain *voltdm;
178 struct clk *clk;
179 struct opp *opp;
180 unsigned long freq, bootup_volt;
182 if (!vdd_name || !clk_name || !dev) {
183 printk(KERN_ERR "%s: Invalid parameters!\n", __func__);
184 goto exit;
187 voltdm = omap_voltage_domain_lookup(vdd_name);
188 if (IS_ERR(voltdm)) {
189 printk(KERN_ERR "%s: Unable to get vdd pointer for vdd_%s\n",
190 __func__, vdd_name);
191 goto exit;
194 clk = clk_get(NULL, clk_name);
195 if (IS_ERR(clk)) {
196 printk(KERN_ERR "%s: unable to get clk %s\n",
197 __func__, clk_name);
198 goto exit;
201 freq = clk->rate;
202 clk_put(clk);
204 opp = opp_find_freq_ceil(dev, &freq);
205 if (IS_ERR(opp)) {
206 printk(KERN_ERR "%s: unable to find boot up OPP for vdd_%s\n",
207 __func__, vdd_name);
208 goto exit;
211 bootup_volt = opp_get_voltage(opp);
212 if (!bootup_volt) {
213 printk(KERN_ERR "%s: unable to find voltage corresponding"
214 "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
215 goto exit;
218 omap_voltage_scale_vdd(voltdm, bootup_volt);
219 return 0;
221 exit:
222 printk(KERN_ERR "%s: Unable to put vdd_%s to its init voltage\n\n",
223 __func__, vdd_name);
224 return -EINVAL;
227 static void __init omap3_init_voltages(void)
229 if (!cpu_is_omap34xx())
230 return;
232 omap2_set_init_voltage("mpu", "dpll1_ck", mpu_dev);
233 omap2_set_init_voltage("core", "l3_ick", l3_dev);
236 static void __init omap4_init_voltages(void)
238 if (!cpu_is_omap44xx())
239 return;
241 omap2_set_init_voltage("mpu", "dpll_mpu_ck", mpu_dev);
242 omap2_set_init_voltage("core", "l3_div_ck", l3_dev);
243 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", iva_dev);
246 static int __init omap2_common_pm_init(void)
248 omap2_init_processor_devices();
249 omap_pm_if_init();
251 return 0;
253 postcore_initcall(omap2_common_pm_init);
255 static int __init omap2_common_pm_late_init(void)
257 /* Init the OMAP TWL parameters */
258 omap3_twl_init();
259 omap4_twl_init();
261 /* Init the voltage layer */
262 omap_voltage_late_init();
264 /* Initialize the voltages */
265 omap3_init_voltages();
266 omap4_init_voltages();
268 /* Smartreflex device init */
269 omap_devinit_smartreflex();
271 return 0;
273 late_initcall(omap2_common_pm_late_init);