ARM: OMAP2+: Export SoC information to userspace
[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 #include "twl-common.h"
28
29 static struct omap_device_pm_latency *pm_lats;
30
31 static int _init_omap_device(char *name)
32 {
33         struct omap_hwmod *oh;
34         struct platform_device *pdev;
35
36         oh = omap_hwmod_lookup(name);
37         if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
38                  __func__, name))
39                 return -ENODEV;
40
41         pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
42         if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
43                  __func__, name))
44                 return -ENODEV;
45
46         return 0;
47 }
48
49 /*
50  * Build omap_devices for processors and bus.
51  */
52 static void omap2_init_processor_devices(void)
53 {
54         _init_omap_device("mpu");
55         if (omap3_has_iva())
56                 _init_omap_device("iva");
57
58         if (cpu_is_omap44xx()) {
59                 _init_omap_device("l3_main_1");
60                 _init_omap_device("dsp");
61                 _init_omap_device("iva");
62         } else {
63                 _init_omap_device("l3_main");
64         }
65 }
66
67 /* Types of sleep_switch used in omap_set_pwrdm_state */
68 #define FORCEWAKEUP_SWITCH      0
69 #define LOWPOWERSTATE_SWITCH    1
70
71 /*
72  * This sets pwrdm state (other than mpu & core. Currently only ON &
73  * RET are supported.
74  */
75 int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 pwrst)
76 {
77         u8 curr_pwrst, next_pwrst;
78         int sleep_switch = -1, ret = 0, hwsup = 0;
79
80         if (!pwrdm || IS_ERR(pwrdm))
81                 return -EINVAL;
82
83         while (!(pwrdm->pwrsts & (1 << pwrst))) {
84                 if (pwrst == PWRDM_POWER_OFF)
85                         return ret;
86                 pwrst--;
87         }
88
89         next_pwrst = pwrdm_read_next_pwrst(pwrdm);
90         if (next_pwrst == pwrst)
91                 return ret;
92
93         curr_pwrst = pwrdm_read_pwrst(pwrdm);
94         if (curr_pwrst < PWRDM_POWER_ON) {
95                 if ((curr_pwrst > pwrst) &&
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, pwrst);
106         if (ret)
107                 pr_err("%s: unable to set power state of powerdomain: %s\n",
108                        __func__, pwrdm->name);
109
110         switch (sleep_switch) {
111         case FORCEWAKEUP_SWITCH:
112                 if (hwsup)
113                         clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
114                 else
115                         clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
116                 break;
117         case LOWPOWERSTATE_SWITCH:
118                 pwrdm_set_lowpwrstchange(pwrdm);
119                 pwrdm_wait_transition(pwrdm);
120                 pwrdm_state_switch(pwrdm);
121                 break;
122         }
123
124         return ret;
125 }
126
127 /*
128  * This API is to be called during init to set the various voltage
129  * domains to the voltage as per the opp table. Typically we boot up
130  * at the nominal voltage. So this function finds out the rate of
131  * the clock associated with the voltage domain, finds out the correct
132  * opp entry and sets the voltage domain to the voltage specified
133  * in the opp entry
134  */
135 static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
136                                          const char *oh_name)
137 {
138         struct voltagedomain *voltdm;
139         struct clk *clk;
140         struct opp *opp;
141         unsigned long freq, bootup_volt;
142         struct device *dev;
143
144         if (!vdd_name || !clk_name || !oh_name) {
145                 pr_err("%s: invalid parameters\n", __func__);
146                 goto exit;
147         }
148
149         dev = omap_device_get_by_hwmod_name(oh_name);
150         if (IS_ERR(dev)) {
151                 pr_err("%s: Unable to get dev pointer for hwmod %s\n",
152                         __func__, oh_name);
153                 goto exit;
154         }
155
156         voltdm = voltdm_lookup(vdd_name);
157         if (IS_ERR(voltdm)) {
158                 pr_err("%s: unable to get vdd pointer for vdd_%s\n",
159                         __func__, vdd_name);
160                 goto exit;
161         }
162
163         clk =  clk_get(NULL, clk_name);
164         if (IS_ERR(clk)) {
165                 pr_err("%s: unable to get clk %s\n", __func__, clk_name);
166                 goto exit;
167         }
168
169         freq = clk->rate;
170         clk_put(clk);
171
172         rcu_read_lock();
173         opp = opp_find_freq_ceil(dev, &freq);
174         if (IS_ERR(opp)) {
175                 rcu_read_unlock();
176                 pr_err("%s: unable to find boot up OPP for vdd_%s\n",
177                         __func__, vdd_name);
178                 goto exit;
179         }
180
181         bootup_volt = opp_get_voltage(opp);
182         rcu_read_unlock();
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 voltage layer */
229         omap_pmic_late_init();
230         omap_voltage_late_init();
231
232         /* Initialize the voltages */
233         omap3_init_voltages();
234         omap4_init_voltages();
235
236         /* Smartreflex device init */
237         omap_devinit_smartreflex();
238
239         return 0;
240 }
241 late_initcall(omap2_common_pm_late_init);