ARM: OMAP2+: PM: share clkdms_setup() across OMAP2, 3, 4
[linux-2.6/cjktty.git] / arch / arm / mach-omap2 / pm.c
blobf9e807958f3e9940e71c4a7620a583a883e354b2
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 "common.h"
23 #include "voltage.h"
24 #include "powerdomain.h"
25 #include "clockdomain.h"
26 #include "pm.h"
27 #include "twl-common.h"
29 static struct omap_device_pm_latency *pm_lats;
31 static int __init _init_omap_device(char *name)
33 struct omap_hwmod *oh;
34 struct platform_device *pdev;
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;
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;
46 return 0;
50 * Build omap_devices for processors and bus.
52 static void omap2_init_processor_devices(void)
54 _init_omap_device("mpu");
55 if (omap3_has_iva())
56 _init_omap_device("iva");
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");
67 /* Types of sleep_switch used in omap_set_pwrdm_state */
68 #define FORCEWAKEUP_SWITCH 0
69 #define LOWPOWERSTATE_SWITCH 1
71 int __init omap_pm_clkdms_setup(struct clockdomain *clkdm, void *unused)
73 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
74 clkdm_allow_idle(clkdm);
75 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
76 atomic_read(&clkdm->usecount) == 0)
77 clkdm_sleep(clkdm);
78 return 0;
82 * This sets pwrdm state (other than mpu & core. Currently only ON &
83 * RET are supported.
85 int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 pwrst)
87 u8 curr_pwrst, next_pwrst;
88 int sleep_switch = -1, ret = 0, hwsup = 0;
90 if (!pwrdm || IS_ERR(pwrdm))
91 return -EINVAL;
93 while (!(pwrdm->pwrsts & (1 << pwrst))) {
94 if (pwrst == PWRDM_POWER_OFF)
95 return ret;
96 pwrst--;
99 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
100 if (next_pwrst == pwrst)
101 return ret;
103 curr_pwrst = pwrdm_read_pwrst(pwrdm);
104 if (curr_pwrst < PWRDM_POWER_ON) {
105 if ((curr_pwrst > pwrst) &&
106 (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
107 sleep_switch = LOWPOWERSTATE_SWITCH;
108 } else {
109 hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
110 clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
111 sleep_switch = FORCEWAKEUP_SWITCH;
115 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
116 if (ret)
117 pr_err("%s: unable to set power state of powerdomain: %s\n",
118 __func__, pwrdm->name);
120 switch (sleep_switch) {
121 case FORCEWAKEUP_SWITCH:
122 if (hwsup)
123 clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
124 else
125 clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
126 break;
127 case LOWPOWERSTATE_SWITCH:
128 pwrdm_set_lowpwrstchange(pwrdm);
129 pwrdm_wait_transition(pwrdm);
130 pwrdm_state_switch(pwrdm);
131 break;
134 return ret;
138 * This API is to be called during init to set the various voltage
139 * domains to the voltage as per the opp table. Typically we boot up
140 * at the nominal voltage. So this function finds out the rate of
141 * the clock associated with the voltage domain, finds out the correct
142 * opp entry and sets the voltage domain to the voltage specified
143 * in the opp entry
145 static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
146 const char *oh_name)
148 struct voltagedomain *voltdm;
149 struct clk *clk;
150 struct opp *opp;
151 unsigned long freq, bootup_volt;
152 struct device *dev;
154 if (!vdd_name || !clk_name || !oh_name) {
155 pr_err("%s: invalid parameters\n", __func__);
156 goto exit;
159 dev = omap_device_get_by_hwmod_name(oh_name);
160 if (IS_ERR(dev)) {
161 pr_err("%s: Unable to get dev pointer for hwmod %s\n",
162 __func__, oh_name);
163 goto exit;
166 voltdm = voltdm_lookup(vdd_name);
167 if (IS_ERR(voltdm)) {
168 pr_err("%s: unable to get vdd pointer for vdd_%s\n",
169 __func__, vdd_name);
170 goto exit;
173 clk = clk_get(NULL, clk_name);
174 if (IS_ERR(clk)) {
175 pr_err("%s: unable to get clk %s\n", __func__, clk_name);
176 goto exit;
179 freq = clk->rate;
180 clk_put(clk);
182 opp = opp_find_freq_ceil(dev, &freq);
183 if (IS_ERR(opp)) {
184 pr_err("%s: unable to find boot up OPP for vdd_%s\n",
185 __func__, vdd_name);
186 goto exit;
189 bootup_volt = opp_get_voltage(opp);
190 if (!bootup_volt) {
191 pr_err("%s: unable to find voltage corresponding "
192 "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
193 goto exit;
196 voltdm_scale(voltdm, bootup_volt);
197 return 0;
199 exit:
200 pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
201 return -EINVAL;
204 static void __init omap3_init_voltages(void)
206 if (!cpu_is_omap34xx())
207 return;
209 omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
210 omap2_set_init_voltage("core", "l3_ick", "l3_main");
213 static void __init omap4_init_voltages(void)
215 if (!cpu_is_omap44xx())
216 return;
218 omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
219 omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
220 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
223 static int __init omap2_common_pm_init(void)
225 if (!of_have_populated_dt())
226 omap2_init_processor_devices();
227 omap_pm_if_init();
229 return 0;
231 postcore_initcall(omap2_common_pm_init);
233 static int __init omap2_common_pm_late_init(void)
235 /* Init the voltage layer */
236 omap_pmic_late_init();
237 omap_voltage_late_init();
239 /* Initialize the voltages */
240 omap3_init_voltages();
241 omap4_init_voltages();
243 /* Smartreflex device init */
244 omap_devinit_smartreflex();
246 return 0;
248 late_initcall(omap2_common_pm_late_init);