pm.c 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * pm.c - Common OMAP2+ power management-related code
 *
 * Copyright (C) 2010 Texas Instruments, Inc.
 * Copyright (C) 2010 Nokia Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/err.h>
16
#include <linux/opp.h>
17 18 19 20 21

#include <plat/omap-pm.h>
#include <plat/omap_device.h>
#include <plat/common.h>

22
#include "voltage.h"
23
#include "powerdomain.h"
24
#include "clockdomain.h"
25
#include "pm.h"
26

27 28 29
static struct omap_device_pm_latency *pm_lats;

static struct device *mpu_dev;
30
static struct device *iva_dev;
31
static struct device *l3_dev;
32
static struct device *dsp_dev;
33 34 35 36 37 38 39

struct device *omap2_get_mpuss_device(void)
{
	WARN_ON_ONCE(!mpu_dev);
	return mpu_dev;
}

40
struct device *omap2_get_iva_device(void)
41
{
42 43
	WARN_ON_ONCE(!iva_dev);
	return iva_dev;
44 45 46 47 48 49 50 51
}

struct device *omap2_get_l3_device(void)
{
	WARN_ON_ONCE(!l3_dev);
	return l3_dev;
}

52 53 54 55 56 57 58
struct device *omap4_get_dsp_device(void)
{
	WARN_ON_ONCE(!dsp_dev);
	return dsp_dev;
}
EXPORT_SYMBOL(omap4_get_dsp_device);

59 60 61 62
/* static int _init_omap_device(struct omap_hwmod *oh, void *user) */
static int _init_omap_device(char *name, struct device **new_dev)
{
	struct omap_hwmod *oh;
63
	struct platform_device *pdev;
64 65 66 67 68 69

	oh = omap_hwmod_lookup(name);
	if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
		 __func__, name))
		return -ENODEV;

70 71
	pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
	if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
72 73 74
		 __func__, name))
		return -ENODEV;

75
	*new_dev = &pdev->dev;
76 77 78 79 80 81 82 83 84 85

	return 0;
}

/*
 * Build omap_devices for processors and bus.
 */
static void omap2_init_processor_devices(void)
{
	_init_omap_device("mpu", &mpu_dev);
86 87 88
	if (omap3_has_iva())
		_init_omap_device("iva", &iva_dev);

89 90
	if (cpu_is_omap44xx()) {
		_init_omap_device("l3_main_1", &l3_dev);
91
		_init_omap_device("dsp", &dsp_dev);
92
		_init_omap_device("iva", &iva_dev);
93 94 95
	} else {
		_init_omap_device("l3_main", &l3_dev);
	}
96 97
}

98 99 100 101
/* Types of sleep_switch used in omap_set_pwrdm_state */
#define FORCEWAKEUP_SWITCH	0
#define LOWPOWERSTATE_SWITCH	1

102 103
/*
 * This sets pwrdm state (other than mpu & core. Currently only ON &
104
 * RET are supported.
105 106 107 108
 */
int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
{
	u32 cur_state;
109
	int sleep_switch = -1;
110
	int ret = 0;
111
	int hwsup = 0;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

	if (pwrdm == NULL || IS_ERR(pwrdm))
		return -EINVAL;

	while (!(pwrdm->pwrsts & (1 << state))) {
		if (state == PWRDM_POWER_OFF)
			return ret;
		state--;
	}

	cur_state = pwrdm_read_next_pwrst(pwrdm);
	if (cur_state == state)
		return ret;

	if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
127 128 129 130
		if ((pwrdm_read_pwrst(pwrdm) > state) &&
			(pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
			sleep_switch = LOWPOWERSTATE_SWITCH;
		} else {
131
			hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
132
			clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
133 134
			sleep_switch = FORCEWAKEUP_SWITCH;
		}
135 136 137 138
	}

	ret = pwrdm_set_next_pwrst(pwrdm, state);
	if (ret) {
139 140
		pr_err("%s: unable to set state of powerdomain: %s\n",
		       __func__, pwrdm->name);
141 142 143
		goto err;
	}

144 145
	switch (sleep_switch) {
	case FORCEWAKEUP_SWITCH:
146
		if (hwsup)
147
			clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
148
		else
149
			clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
150 151 152 153 154 155
		break;
	case LOWPOWERSTATE_SWITCH:
		pwrdm_set_lowpwrstchange(pwrdm);
		break;
	default:
		return ret;
156 157
	}

158
	pwrdm_state_switch(pwrdm);
159 160 161 162
err:
	return ret;
}

163
/*
164
 * This API is to be called during init to set the various voltage
165 166 167
 * domains to the voltage as per the opp table. Typically we boot up
 * at the nominal voltage. So this function finds out the rate of
 * the clock associated with the voltage domain, finds out the correct
168
 * opp entry and sets the voltage domain to the voltage specified
169 170 171 172 173 174 175 176 177 178 179
 * in the opp entry
 */
static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
						struct device *dev)
{
	struct voltagedomain *voltdm;
	struct clk *clk;
	struct opp *opp;
	unsigned long freq, bootup_volt;

	if (!vdd_name || !clk_name || !dev) {
180
		pr_err("%s: invalid parameters\n", __func__);
181 182 183
		goto exit;
	}

184
	voltdm = voltdm_lookup(vdd_name);
185
	if (IS_ERR(voltdm)) {
186
		pr_err("%s: unable to get vdd pointer for vdd_%s\n",
187 188 189 190 191 192
			__func__, vdd_name);
		goto exit;
	}

	clk =  clk_get(NULL, clk_name);
	if (IS_ERR(clk)) {
193
		pr_err("%s: unable to get clk %s\n", __func__, clk_name);
194 195 196 197 198 199 200 201
		goto exit;
	}

	freq = clk->rate;
	clk_put(clk);

	opp = opp_find_freq_ceil(dev, &freq);
	if (IS_ERR(opp)) {
202
		pr_err("%s: unable to find boot up OPP for vdd_%s\n",
203 204 205 206 207 208
			__func__, vdd_name);
		goto exit;
	}

	bootup_volt = opp_get_voltage(opp);
	if (!bootup_volt) {
209
		pr_err("%s: unable to find voltage corresponding "
210 211 212 213
			"to the bootup OPP for vdd_%s\n", __func__, vdd_name);
		goto exit;
	}

214
	voltdm_scale(voltdm, bootup_volt);
215 216 217
	return 0;

exit:
218
	pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
219 220 221 222 223 224 225 226
	return -EINVAL;
}

static void __init omap3_init_voltages(void)
{
	if (!cpu_is_omap34xx())
		return;

227
	omap2_set_init_voltage("mpu_iva", "dpll1_ck", mpu_dev);
228 229 230
	omap2_set_init_voltage("core", "l3_ick", l3_dev);
}

231 232 233 234 235 236 237 238 239 240
static void __init omap4_init_voltages(void)
{
	if (!cpu_is_omap44xx())
		return;

	omap2_set_init_voltage("mpu", "dpll_mpu_ck", mpu_dev);
	omap2_set_init_voltage("core", "l3_div_ck", l3_dev);
	omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", iva_dev);
}

241 242 243 244 245 246 247
static int __init omap2_common_pm_init(void)
{
	omap2_init_processor_devices();
	omap_pm_if_init();

	return 0;
}
248
postcore_initcall(omap2_common_pm_init);
249

250 251
static int __init omap2_common_pm_late_init(void)
{
252 253
	/* Init the OMAP TWL parameters */
	omap3_twl_init();
254
	omap4_twl_init();
255

256
	/* Init the voltage layer */
257
	omap_voltage_late_init();
258 259 260

	/* Initialize the voltages */
	omap3_init_voltages();
261
	omap4_init_voltages();
262

263
	/* Smartreflex device init */
264
	omap_devinit_smartreflex();
265 266 267 268

	return 0;
}
late_initcall(omap2_common_pm_late_init);