ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / arch / arm / mach-omap2 / pm.c
blob4464039a52a90bd59d3856159b8d9b5de24ccbad
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>
18 #include <plat/omap-pm.h>
19 #include <plat/omap_device.h>
20 #include <plat/common.h>
22 #include "voltage.h"
23 #include "powerdomain.h"
24 #include "clockdomain.h"
25 #include "pm.h"
27 /**
28 * struct omap2_pm_lp_description - Describe low power behavior of the system
29 * @oscillator_startup_time: Time rounded up to uSec for the oscillator to
30 * provide a stable clock from power on.
31 * @oscillator_shutdown_time: Time rounded up to uSec for oscillator to safely
32 * switch off.
33 * @pmic_startup_time: Time rounded up to uSec for the PMIC to
34 * provide be ready for operation from low power
35 * state. Note: this is not the same as voltage
36 * rampup time, instead, consider the PMIC to be
37 * in lowest power state(say OFF), this is the time
38 * required for it to become ready for it's DCDCs
39 * or LDOs to start operation.
40 * @pmic_shutdown_time: Time rounded up to uSec for the PMIC to
41 * go to low power after the LDOs are pulled to
42 * appropriate state. Note: this is not the same as
43 * voltage rampdown time, instead, consider the
44 * PMIC to have switched it's LDOs down, this is
45 * time taken to reach it's lowest power state(say
46 * sleep/OFF).
48 * With complex systems like OMAP, we need a generic description of system
49 * behavior beyond the normal description of device/peripheral operation
50 * which in conjunction with other parameters describe and control the low
51 * power operation of the device. This information tends to be specific
52 * to every board.
54 struct omap2_pm_lp_description {
55 u32 oscillator_startup_time;
56 u32 oscillator_shutdown_time;
57 u32 pmic_startup_time;
58 u32 pmic_shutdown_time;
62 * Setup time to be the max... we want to err towards the worst
63 * as default. rest of the system can populate these with more
64 * optimal values
66 static struct omap2_pm_lp_description _pm_lp_desc = {
67 .oscillator_startup_time = ULONG_MAX,
68 .oscillator_shutdown_time = ULONG_MAX,
69 .pmic_startup_time = ULONG_MAX,
70 .pmic_shutdown_time = ULONG_MAX,
73 static struct omap_device_pm_latency *pm_lats;
75 static struct device *mpu_dev;
76 static struct device *iva_dev;
77 static struct device *l3_dev;
78 static struct device *dsp_dev;
79 static struct device *fdif_dev;
81 bool omap_pm_is_ready_status;
83 struct device *omap2_get_mpuss_device(void)
85 WARN_ON_ONCE(!mpu_dev);
86 return mpu_dev;
88 EXPORT_SYMBOL(omap2_get_mpuss_device);
90 struct device *omap2_get_iva_device(void)
92 WARN_ON_ONCE(!iva_dev);
93 return iva_dev;
95 EXPORT_SYMBOL(omap2_get_iva_device);
97 struct device *omap2_get_l3_device(void)
99 WARN_ON_ONCE(!l3_dev);
100 return l3_dev;
102 EXPORT_SYMBOL(omap2_get_l3_device);
104 struct device *omap4_get_dsp_device(void)
106 WARN_ON_ONCE(!dsp_dev);
107 return dsp_dev;
109 EXPORT_SYMBOL(omap4_get_dsp_device);
111 struct device *omap4_get_fdif_device(void)
113 WARN_ON_ONCE(!fdif_dev);
114 return fdif_dev;
116 EXPORT_SYMBOL(omap4_get_fdif_device);
119 * omap_pm_get_pmic_lp_time() - retrieve the oscillator time
120 * @tstart: pointer to startup time in uSec
121 * @tshut: pointer to shutdown time in uSec
123 * if the pointers are invalid, returns error, else
124 * populates the tstart and tshut values with the currently
125 * stored values.
127 int omap_pm_get_osc_lp_time(u32 *tstart, u32 *tshut)
129 if (!tstart || !tshut)
130 return -EINVAL;
132 *tstart = _pm_lp_desc.oscillator_startup_time;
133 *tshut = _pm_lp_desc.oscillator_shutdown_time;
135 return 0;
139 * omap_pm_get_pmic_lp_time() - retrieve the PMIC time
140 * @tstart: pointer to startup time in uSec
141 * @tshut: pointer to shutdown time in uSec
143 * if the pointers are invalid, returns error, else
144 * populates the tstart and tshut values with the currently
145 * stored values.
147 int omap_pm_get_pmic_lp_time(u32 *tstart, u32 *tshut)
149 if (!tstart || !tshut)
150 return -EINVAL;
152 *tstart = _pm_lp_desc.pmic_startup_time;
153 *tshut = _pm_lp_desc.pmic_shutdown_time;
155 return 0;
159 * omap_pm_set_osc_lp_time() - setup the system oscillator time
160 * @tstart: startup time rounded up to uSec
161 * @tshut: shutdown time rounded up to uSec
163 * All boards do need an oscillator for the device to function.
164 * The startup and stop time of these oscillators vary. Populate
165 * from the board file to optimize the timing.
166 * This function is meant to be used at boot-time configuration.
168 * NOTE: This API is intended to be invoked from board file
170 void __init omap_pm_set_osc_lp_time(u32 tstart, u32 tshut)
172 _pm_lp_desc.oscillator_startup_time = tstart;
173 _pm_lp_desc.oscillator_shutdown_time = tshut;
177 * omap_pm_set_pmic_lp_time() - setup the pmic low power time
178 * @tstart: startup time rounded up to uSec
179 * @tshut: shutdown time rounded up to uSec
181 * Store the time for PMIC to enter to lowest state supported.
182 * in the case of multiple PMIC on a platform, choose the one
183 * that ends the sequence for LP state such as OFF and starts
184 * the sequence such as wakeup from OFF - e.g. a PMIC that
185 * controls core-domain.
186 * This function is meant to be used at boot-time configuration.
188 void __init omap_pm_set_pmic_lp_time(u32 tstart, u32 tshut)
190 _pm_lp_desc.pmic_startup_time = tstart;
191 _pm_lp_desc.pmic_shutdown_time = tshut;
194 /* static int _init_omap_device(struct omap_hwmod *oh, void *user) */
195 static int _init_omap_device(char *name, struct device **new_dev)
197 struct omap_hwmod *oh;
198 struct omap_device *od;
200 oh = omap_hwmod_lookup(name);
201 if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
202 __func__, name))
203 return -ENODEV;
205 od = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
206 if (WARN(IS_ERR(od), "%s: could not build omap_device for %s\n",
207 __func__, name))
208 return -ENODEV;
210 *new_dev = &od->pdev.dev;
212 return 0;
216 * Build omap_devices for processors and bus.
218 static void omap2_init_processor_devices(void)
220 _init_omap_device("mpu", &mpu_dev);
221 if (omap3_has_iva())
222 _init_omap_device("iva", &iva_dev);
224 if (cpu_is_omap44xx()) {
225 _init_omap_device("l3_main_1", &l3_dev);
226 _init_omap_device("dsp", &dsp_dev);
227 _init_omap_device("iva", &iva_dev);
228 _init_omap_device("fdif", &fdif_dev);
229 } else {
230 _init_omap_device("l3_main", &l3_dev);
234 /* Types of sleep_switch used in omap_set_pwrdm_state */
235 #define FORCEWAKEUP_SWITCH 0
236 #define LOWPOWERSTATE_SWITCH 1
239 * This sets pwrdm state (other than mpu & core. Currently only ON &
240 * RET are supported.
242 int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
244 u32 cur_state;
245 int sleep_switch = -1;
246 int ret = 0;
247 int hwsup = 0;
249 if (pwrdm == NULL || IS_ERR(pwrdm))
250 return -EINVAL;
252 while (!(pwrdm->pwrsts & (1 << state))) {
253 if (state == PWRDM_POWER_OFF)
254 return ret;
255 state--;
258 cur_state = pwrdm_read_next_pwrst(pwrdm);
259 if (cur_state == state)
260 return ret;
262 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
263 if ((pwrdm_read_pwrst(pwrdm) > state) &&
264 (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
265 sleep_switch = LOWPOWERSTATE_SWITCH;
266 } else {
267 hwsup = clkdm_is_idle(pwrdm->pwrdm_clkdms[0]);
268 clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
269 pwrdm_wait_transition(pwrdm);
270 sleep_switch = FORCEWAKEUP_SWITCH;
274 ret = pwrdm_set_next_pwrst(pwrdm, state);
275 if (ret) {
276 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
277 pwrdm->name);
278 goto err;
281 switch (sleep_switch) {
282 case FORCEWAKEUP_SWITCH:
283 if (hwsup)
284 clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
285 else
286 clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
287 break;
288 case LOWPOWERSTATE_SWITCH:
289 pwrdm_set_lowpwrstchange(pwrdm);
290 break;
291 default:
292 return ret;
295 pwrdm_wait_transition(pwrdm);
296 pwrdm_state_switch(pwrdm);
297 err:
298 return ret;
301 static int __init boot_volt_scale(struct voltagedomain *voltdm,
302 unsigned long boot_v)
304 struct omap_volt_data *vdata;
305 int ret = 0;
307 vdata = omap_voltage_get_voltdata(voltdm, boot_v);
308 if (IS_ERR_OR_NULL(vdata)) {
309 pr_err("%s:%s: Bad New voltage data for %ld\n",
310 __func__, voltdm->name, boot_v);
311 return PTR_ERR(vdata);
314 * DO NOT DO abb prescale -
315 * case 1: OPP needs FBB, bootloader configured FBB
316 * - doing a prescale results in bypass -> system fail
317 * case 2: OPP needs FBB, bootloader does not configure FBB
318 * - FBB will be configured in postscale
319 * case 3: OPP needs bypass, bootloader configures FBB
320 * - bypass will be configured in postscale
321 * case 4: OPP needs bypass, bootloader configured in bypass
322 * - bypass programming in postscale skipped
324 ret = voltdm_scale(voltdm, vdata);
325 if (ret) {
326 pr_err("%s: Fail set voltage(v=%ld)on vdd%s\n",
327 __func__, boot_v, voltdm->name);
328 return ret;
330 if (voltdm->abb) {
331 ret = omap_ldo_abb_post_scale(voltdm, vdata);
332 if (ret) {
333 pr_err("%s: Fail abb postscale(v=%ld)vdd%s\n",
334 __func__, boot_v, voltdm->name);
337 return ret;
341 * This API is to be called during init to put the various voltage
342 * domains to the voltage as per the opp table. Typically we boot up
343 * at the nominal voltage. So this function finds out the rate of
344 * the clock associated with the voltage domain, finds out the correct
345 * opp entry and puts the voltage domain to the voltage specifies
346 * in the opp entry
348 static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
349 struct device *dev)
351 struct voltagedomain *voltdm;
352 struct clk *clk;
353 struct opp *opp;
354 unsigned long freq_cur, freq_valid, bootup_volt;
355 int ret = -EINVAL;
357 if (!vdd_name || !clk_name || !dev) {
358 printk(KERN_ERR "%s: Invalid parameters!\n", __func__);
359 goto exit;
362 voltdm = voltdm_lookup(vdd_name);
363 if (IS_ERR(voltdm)) {
364 printk(KERN_ERR "%s: Unable to get vdd pointer for vdd_%s\n",
365 __func__, vdd_name);
366 goto exit;
369 clk = clk_get(NULL, clk_name);
370 if (IS_ERR(clk)) {
371 printk(KERN_ERR "%s: unable to get clk %s\n",
372 __func__, clk_name);
373 goto exit;
376 freq_cur = clk->rate;
377 freq_valid = freq_cur;
379 rcu_read_lock();
380 opp = opp_find_freq_ceil(dev, &freq_valid);
381 if (IS_ERR(opp)) {
382 opp = opp_find_freq_floor(dev, &freq_valid);
383 if (IS_ERR(opp)) {
384 rcu_read_unlock();
385 pr_err("%s: no boot OPP match for %ld on vdd_%s\n",
386 __func__, freq_cur, vdd_name);
387 ret = -ENOENT;
388 goto exit_ck;
392 bootup_volt = opp_get_voltage(opp);
393 rcu_read_unlock();
394 if (!bootup_volt) {
395 printk(KERN_ERR "%s: unable to find voltage corresponding"
396 "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
397 ret = -ENOENT;
398 goto exit_ck;
402 * Frequency and Voltage have to be sequenced: if we move from
403 * a lower frequency to higher frequency, raise voltage, followed by
404 * frequency, and vice versa. we assume that the voltage at boot
405 * is the required voltage for the frequency it was set for.
406 * NOTE:
407 * we can check the frequency, but there is numerous ways to set
408 * voltage. We play the safe path and just set the voltage.
411 if (freq_cur < freq_valid) {
412 ret = boot_volt_scale(voltdm, bootup_volt);
413 if (ret) {
414 pr_err("%s: Fail set voltage-%s(f=%ld v=%ld)on vdd%s\n",
415 __func__, vdd_name, freq_valid,
416 bootup_volt, vdd_name);
417 goto exit_ck;
421 /* Set freq only if there is a difference in freq */
422 if (freq_valid != freq_cur) {
423 ret = clk_set_rate(clk, freq_valid);
424 if (ret) {
425 pr_err("%s: Fail set clk-%s(f=%ld v=%ld)on vdd%s\n",
426 __func__, clk_name, freq_valid,
427 bootup_volt, vdd_name);
428 goto exit_ck;
432 if (freq_cur >= freq_valid) {
433 ret = boot_volt_scale(voltdm, bootup_volt);
434 if (ret) {
435 pr_err("%s: Fail set voltage-%s(f=%ld v=%ld)on vdd%s\n",
436 __func__, clk_name, freq_valid,
437 bootup_volt, vdd_name);
438 goto exit_ck;
442 ret = 0;
443 exit_ck:
444 clk_put(clk);
446 if (!ret)
447 return 0;
449 exit:
450 printk(KERN_ERR "%s: Unable to put vdd_%s to its init voltage\n\n",
451 __func__, vdd_name);
452 return -EINVAL;
455 static void __init omap3_init_voltages(void)
457 if (!cpu_is_omap34xx())
458 return;
460 omap2_set_init_voltage("mpu_iva", "dpll1_ck", mpu_dev);
461 omap2_set_init_voltage("core", "l3_ick", l3_dev);
464 static void __init omap4_init_voltages(void)
466 if (!cpu_is_omap44xx())
467 return;
469 if (cpu_is_omap446x()) {
470 omap2_set_init_voltage("mpu", "virt_dpll_mpu_ck", mpu_dev);
471 } else {
472 omap2_set_init_voltage("mpu", "dpll_mpu_ck", mpu_dev);
474 omap2_set_init_voltage("core", "virt_l3_ck", l3_dev);
475 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", iva_dev);
478 static int __init omap2_common_pm_init(void)
480 omap2_init_processor_devices();
481 omap_pm_if_init();
483 return 0;
485 postcore_initcall(omap2_common_pm_init);
487 static int __init omap2_common_pm_late_init(void)
489 /* Init the OMAP PMIC parameters */
490 omap_pmic_data_init();
492 /* Init the voltage layer */
493 omap_voltage_late_init();
495 /* Initialize the voltages */
496 omap3_init_voltages();
497 omap4_init_voltages();
499 /* Smartreflex device init */
500 omap_devinit_smartreflex();
502 return 0;
504 late_initcall(omap2_common_pm_late_init);