Merge branch 'prcm_scm_misc_fixes_3.2' of git://git.pwsan.com/linux-2.6 into fixes
[pandora-kernel.git] / arch / arm / mach-omap2 / pm.c
1 /*
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>
16 #include <linux/opp.h>
17
18 #include <plat/omap-pm.h>
19 #include <plat/omap_device.h>
20 #include <plat/common.h>
21
22 #include "voltage.h"
23 #include "powerdomain.h"
24 #include "clockdomain.h"
25 #include "pm.h"
26
27 static struct omap_device_pm_latency *pm_lats;
28
29 static int _init_omap_device(char *name)
30 {
31         struct omap_hwmod *oh;
32         struct platform_device *pdev;
33
34         oh = omap_hwmod_lookup(name);
35         if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
36                  __func__, name))
37                 return -ENODEV;
38
39         pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
40         if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
41                  __func__, name))
42                 return -ENODEV;
43
44         return 0;
45 }
46
47 /*
48  * Build omap_devices for processors and bus.
49  */
50 static void omap2_init_processor_devices(void)
51 {
52         _init_omap_device("mpu");
53         if (omap3_has_iva())
54                 _init_omap_device("iva");
55
56         if (cpu_is_omap44xx()) {
57                 _init_omap_device("l3_main_1");
58                 _init_omap_device("dsp");
59                 _init_omap_device("iva");
60         } else {
61                 _init_omap_device("l3_main");
62         }
63 }
64
65 /* Types of sleep_switch used in omap_set_pwrdm_state */
66 #define FORCEWAKEUP_SWITCH      0
67 #define LOWPOWERSTATE_SWITCH    1
68
69 /*
70  * This sets pwrdm state (other than mpu & core. Currently only ON &
71  * RET are supported.
72  */
73 int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
74 {
75         u32 cur_state;
76         int sleep_switch = -1;
77         int ret = 0;
78         int hwsup = 0;
79
80         if (pwrdm == NULL || IS_ERR(pwrdm))
81                 return -EINVAL;
82
83         while (!(pwrdm->pwrsts & (1 << state))) {
84                 if (state == PWRDM_POWER_OFF)
85                         return ret;
86                 state--;
87         }
88
89         cur_state = pwrdm_read_next_pwrst(pwrdm);
90         if (cur_state == state)
91                 return ret;
92
93         if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
94                 if ((pwrdm_read_pwrst(pwrdm) > state) &&
95                         (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
96                         sleep_switch = LOWPOWERSTATE_SWITCH;
97                 } else {
98                         hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
99                         clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
100                         sleep_switch = FORCEWAKEUP_SWITCH;
101                 }
102         }
103
104         ret = pwrdm_set_next_pwrst(pwrdm, state);
105         if (ret) {
106                 pr_err("%s: unable to set state of powerdomain: %s\n",
107                        __func__, pwrdm->name);
108                 goto err;
109         }
110
111         switch (sleep_switch) {
112         case FORCEWAKEUP_SWITCH:
113                 if (hwsup)
114                         clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
115                 else
116                         clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
117                 break;
118         case LOWPOWERSTATE_SWITCH:
119                 pwrdm_set_lowpwrstchange(pwrdm);
120                 break;
121         default:
122                 return ret;
123         }
124
125         pwrdm_state_switch(pwrdm);
126 err:
127         return ret;
128 }
129
130 /*
131  * This API is to be called during init to set the various voltage
132  * domains to the voltage as per the opp table. Typically we boot up
133  * at the nominal voltage. So this function finds out the rate of
134  * the clock associated with the voltage domain, finds out the correct
135  * opp entry and sets the voltage domain to the voltage specified
136  * in the opp entry
137  */
138 static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
139                                          const char *oh_name)
140 {
141         struct voltagedomain *voltdm;
142         struct clk *clk;
143         struct opp *opp;
144         unsigned long freq, bootup_volt;
145         struct device *dev;
146
147         if (!vdd_name || !clk_name || !oh_name) {
148                 pr_err("%s: invalid parameters\n", __func__);
149                 goto exit;
150         }
151
152         dev = omap_device_get_by_hwmod_name(oh_name);
153         if (IS_ERR(dev)) {
154                 pr_err("%s: Unable to get dev pointer for hwmod %s\n",
155                         __func__, oh_name);
156                 goto exit;
157         }
158
159         voltdm = voltdm_lookup(vdd_name);
160         if (IS_ERR(voltdm)) {
161                 pr_err("%s: unable to get vdd pointer for vdd_%s\n",
162                         __func__, vdd_name);
163                 goto exit;
164         }
165
166         clk =  clk_get(NULL, clk_name);
167         if (IS_ERR(clk)) {
168                 pr_err("%s: unable to get clk %s\n", __func__, clk_name);
169                 goto exit;
170         }
171
172         freq = clk->rate;
173         clk_put(clk);
174
175         opp = opp_find_freq_ceil(dev, &freq);
176         if (IS_ERR(opp)) {
177                 pr_err("%s: unable to find boot up OPP for vdd_%s\n",
178                         __func__, vdd_name);
179                 goto exit;
180         }
181
182         bootup_volt = opp_get_voltage(opp);
183         if (!bootup_volt) {
184                 pr_err("%s: unable to find voltage corresponding "
185                         "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
186                 goto exit;
187         }
188
189         voltdm_scale(voltdm, bootup_volt);
190         return 0;
191
192 exit:
193         pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
194         return -EINVAL;
195 }
196
197 static void __init omap3_init_voltages(void)
198 {
199         if (!cpu_is_omap34xx())
200                 return;
201
202         omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
203         omap2_set_init_voltage("core", "l3_ick", "l3_main");
204 }
205
206 static void __init omap4_init_voltages(void)
207 {
208         if (!cpu_is_omap44xx())
209                 return;
210
211         omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
212         omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
213         omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
214 }
215
216 static int __init omap2_common_pm_init(void)
217 {
218         if (!of_have_populated_dt())
219                 omap2_init_processor_devices();
220         omap_pm_if_init();
221
222         return 0;
223 }
224 postcore_initcall(omap2_common_pm_init);
225
226 static int __init omap2_common_pm_late_init(void)
227 {
228         /* Init the OMAP TWL parameters */
229         omap3_twl_init();
230         omap4_twl_init();
231
232         /* Init the voltage layer */
233         omap_voltage_late_init();
234
235         /* Initialize the voltages */
236         omap3_init_voltages();
237         omap4_init_voltages();
238
239         /* Smartreflex device init */
240         omap_devinit_smartreflex();
241
242         return 0;
243 }
244 late_initcall(omap2_common_pm_late_init);