vc.c 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * OMAP Voltage Controller (VC) interface
 *
 * Copyright (C) 2011 Texas Instruments, Inc.
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>

#include <plat/cpu.h>

#include "voltage.h"
#include "vc.h"
#include "prm-regbits-34xx.h"
#include "prm-regbits-44xx.h"
#include "prm44xx.h"

/* Voltage scale and accessory APIs */
int omap_vc_pre_scale(struct voltagedomain *voltdm,
		      unsigned long target_volt,
		      u8 *target_vsel, u8 *current_vsel)
{
27
	struct omap_vc_channel *vc = voltdm->vc;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	struct omap_vdd_info *vdd = voltdm->vdd;
	struct omap_volt_data *volt_data;
	const struct omap_vp_common_data *vp_common;
	u32 vc_cmdval, vp_errgain_val;

	vp_common = vdd->vp_data->vp_common;

	/* Check if sufficient pmic info is available for this vdd */
	if (!vdd->pmic_info) {
		pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
			__func__, voltdm->name);
		return -EINVAL;
	}

	if (!vdd->pmic_info->uv_to_vsel) {
		pr_err("%s: PMIC function to convert voltage in uV to"
			"vsel not registered. Hence unable to scale voltage"
			"for vdd_%s\n", __func__, voltdm->name);
		return -ENODATA;
	}

	if (!vdd->read_reg || !vdd->write_reg) {
		pr_err("%s: No read/write API for accessing vdd_%s regs\n",
			__func__, voltdm->name);
		return -EINVAL;
	}

	/* Get volt_data corresponding to target_volt */
	volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
	if (IS_ERR(volt_data))
		volt_data = NULL;

	*target_vsel = vdd->pmic_info->uv_to_vsel(target_volt);
	*current_vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);

	/* Setting the ON voltage to the new target voltage */
64 65 66 67
	vc_cmdval = vdd->read_reg(vc->common->prm_mod, vc->cmdval_reg);
	vc_cmdval &= ~vc->common->cmd_on_mask;
	vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
	vdd->write_reg(vc_cmdval, vc->common->prm_mod, vc->cmdval_reg);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

	/* Setting vp errorgain based on the voltage */
	if (volt_data) {
		vp_errgain_val = vdd->read_reg(vdd->vp_data->vp_common->prm_mod,
					       vdd->vp_data->vpconfig);
		vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
		vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
		vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
			vp_common->vpconfig_errorgain_shift;
		vdd->write_reg(vp_errgain_val, vdd->vp_data->vp_common->prm_mod,
			       vdd->vp_data->vpconfig);
	}

	return 0;
}

void omap_vc_post_scale(struct voltagedomain *voltdm,
			unsigned long target_volt,
			u8 target_vsel, u8 current_vsel)
{
	struct omap_vdd_info *vdd = voltdm->vdd;
	u32 smps_steps = 0, smps_delay = 0;

	smps_steps = abs(target_vsel - current_vsel);
	/* SMPS slew rate / step size. 2us added as buffer. */
	smps_delay = ((smps_steps * vdd->pmic_info->step_size) /
			vdd->pmic_info->slew_rate) + 2;
	udelay(smps_delay);

	vdd->curr_volt = target_volt;
}

100 101 102
/* vc_bypass_scale - VC bypass method of voltage scaling */
int omap_vc_bypass_scale(struct voltagedomain *voltdm,
			 unsigned long target_volt)
103
{
104
	struct omap_vc_channel *vc = voltdm->vc;
105 106 107 108 109 110 111 112 113 114
	struct omap_vdd_info *vdd = voltdm->vdd;
	u32 loop_cnt = 0, retries_cnt = 0;
	u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
	u8 target_vsel, current_vsel;
	int ret;

	ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
	if (ret)
		return ret;

115 116 117
	vc_valid = vc->common->valid;
	vc_bypass_val_reg = vc->common->bypass_val_reg;
	vc_bypass_value = (target_vsel << vc->common->data_shift) |
118
			(vdd->pmic_info->pmic_reg <<
119
			vc->common->regaddr_shift) |
120
			(vdd->pmic_info->i2c_slave_addr <<
121
			vc->common->slaveaddr_shift);
122

123 124
	vdd->write_reg(vc_bypass_value, vc->common->prm_mod, vc_bypass_val_reg);
	vdd->write_reg(vc_bypass_value | vc_valid, vc->common->prm_mod,
125 126
		       vc_bypass_val_reg);

127
	vc_bypass_value = vdd->read_reg(vc->common->prm_mod, vc_bypass_val_reg);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	/*
	 * Loop till the bypass command is acknowledged from the SMPS.
	 * NOTE: This is legacy code. The loop count and retry count needs
	 * to be revisited.
	 */
	while (!(vc_bypass_value & vc_valid)) {
		loop_cnt++;

		if (retries_cnt > 10) {
			pr_warning("%s: Retry count exceeded\n", __func__);
			return -ETIMEDOUT;
		}

		if (loop_cnt > 50) {
			retries_cnt++;
			loop_cnt = 0;
			udelay(10);
		}
146
		vc_bypass_value = vdd->read_reg(vc->common->prm_mod,
147 148 149 150 151 152 153 154 155
						vc_bypass_val_reg);
	}

	omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
	return 0;
}

static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
{
156
	struct omap_vc_channel *vc = voltdm->vc;
157 158 159 160 161 162
	struct omap_vdd_info *vdd = voltdm->vdd;

	/*
	 * Voltage Manager FSM parameters init
	 * XXX This data should be passed in from the board file
	 */
163 164
	vdd->write_reg(OMAP3_CLKSETUP, vc->common->prm_mod, OMAP3_PRM_CLKSETUP_OFFSET);
	vdd->write_reg(OMAP3_VOLTOFFSET, vc->common->prm_mod,
165
		       OMAP3_PRM_VOLTOFFSET_OFFSET);
166
	vdd->write_reg(OMAP3_VOLTSETUP2, vc->common->prm_mod,
167 168 169 170 171
		       OMAP3_PRM_VOLTSETUP2_OFFSET);
}

static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
{
172
	struct omap_vc_channel *vc = voltdm->vc;
173 174 175 176 177 178 179 180 181 182 183 184 185
	struct omap_vdd_info *vdd = voltdm->vdd;
	static bool is_initialized;
	u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
	u32 vc_val;

	if (is_initialized)
		return;

	/* Set up the on, inactive, retention and off voltage */
	on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
	onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
	ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
	off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
186 187 188 189 190
	vc_val	= ((on_vsel << vc->common->cmd_on_shift) |
		(onlp_vsel << vc->common->cmd_onlp_shift) |
		(ret_vsel << vc->common->cmd_ret_shift) |
		(off_vsel << vc->common->cmd_off_shift));
	vdd->write_reg(vc_val, vc->common->prm_mod, vc->cmdval_reg);
191 192 193 194 195

	/*
	 * Generic VC parameters init
	 * XXX This data should be abstracted out
	 */
196
	vdd->write_reg(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, vc->common->prm_mod,
197
			OMAP3_PRM_VC_CH_CONF_OFFSET);
198
	vdd->write_reg(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, vc->common->prm_mod,
199 200 201 202 203 204 205 206 207 208 209
			OMAP3_PRM_VC_I2C_CFG_OFFSET);

	omap3_vfsm_init(voltdm);

	is_initialized = true;
}


/* OMAP4 specific voltage init functions */
static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
{
210
	struct omap_vc_channel *vc = voltdm->vc;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	struct omap_vdd_info *vdd = voltdm->vdd;
	static bool is_initialized;
	u32 vc_val;

	if (is_initialized)
		return;

	/* TODO: Configure setup times and CMD_VAL values*/

	/*
	 * Generic VC parameters init
	 * XXX This data should be abstracted out
	 */
	vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
		  OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
		  OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
227
	vdd->write_reg(vc_val, vc->common->prm_mod, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
228 229 230

	/* XXX These are magic numbers and do not belong! */
	vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
231
	vdd->write_reg(vc_val, vc->common->prm_mod, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
232 233 234 235 236 237

	is_initialized = true;
}

void __init omap_vc_init_channel(struct voltagedomain *voltdm)
{
238
	struct omap_vc_channel *vc = voltdm->vc;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	struct omap_vdd_info *vdd = voltdm->vdd;
	u32 vc_val;

	if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
		pr_err("%s: PMIC info requried to configure vc for"
			"vdd_%s not populated.Hence cannot initialize vc\n",
			__func__, voltdm->name);
		return;
	}

	if (!vdd->read_reg || !vdd->write_reg) {
		pr_err("%s: No read/write API for accessing vdd_%s regs\n",
			__func__, voltdm->name);
		return;
	}

	/* Set up the SMPS_SA(i2c slave address in VC */
256 257
	vc_val = vdd->read_reg(vc->common->prm_mod,
			       vc->common->smps_sa_reg);
258 259
	vc_val &= ~vc->smps_sa_mask;
	vc_val |= vdd->pmic_info->i2c_slave_addr << vc->smps_sa_shift;
260 261
	vdd->write_reg(vc_val, vc->common->prm_mod,
		       vc->common->smps_sa_reg);
262 263

	/* Setup the VOLRA(pmic reg addr) in VC */
264 265
	vc_val = vdd->read_reg(vc->common->prm_mod,
			       vc->common->smps_volra_reg);
266 267
	vc_val &= ~vc->smps_volra_mask;
	vc_val |= vdd->pmic_info->pmic_reg << vc->smps_volra_shift;
268 269
	vdd->write_reg(vc_val, vc->common->prm_mod,
		       vc->common->smps_volra_reg);
270 271

	/* Configure the setup times */
272
	vc_val = vdd->read_reg(vc->common->prm_mod, vdd->vfsm->voltsetup_reg);
273 274 275
	vc_val &= ~vdd->vfsm->voltsetup_mask;
	vc_val |= vdd->pmic_info->volt_setup_time <<
			vdd->vfsm->voltsetup_shift;
276
	vdd->write_reg(vc_val, vc->common->prm_mod, vdd->vfsm->voltsetup_reg);
277 278 279 280 281 282 283

	if (cpu_is_omap34xx())
		omap3_vc_init_channel(voltdm);
	else if (cpu_is_omap44xx())
		omap4_vc_init_channel(voltdm);
}