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