vp.c 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <linux/kernel.h>
#include <linux/init.h>

#include <plat/common.h>

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

static void vp_latch_vsel(struct voltagedomain *voltdm)
{
14
	struct omap_vp_instance *vp = voltdm->vp;
15 16 17 18 19 20 21 22 23 24 25
	u32 vpconfig;
	unsigned long uvdc;
	char vsel;

	uvdc = omap_voltage_get_nom_volt(voltdm);
	if (!uvdc) {
		pr_warning("%s: unable to find current voltage for vdd_%s\n",
			__func__, voltdm->name);
		return;
	}

26
	if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
27 28 29 30 31
		pr_warning("%s: PMIC function to convert voltage in uV to"
			" vsel not registered\n", __func__);
		return;
	}

32
	vsel = voltdm->pmic->uv_to_vsel(uvdc);
33

34
	vpconfig = voltdm->read(vp->vpconfig);
35 36
	vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
			vp->common->vpconfig_initvdd);
37
	vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
38
	voltdm->write(vpconfig, vp->vpconfig);
39 40

	/* Trigger initVDD value copy to voltage processor */
41
	voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
42
		       vp->vpconfig);
43 44

	/* Clear initVDD copy trigger bit */
45
	voltdm->write(vpconfig, vp->vpconfig);
46 47 48 49 50
}

/* Generic voltage init functions */
void __init omap_vp_init(struct voltagedomain *voltdm)
{
51
	struct omap_vp_instance *vp = voltdm->vp;
52 53
	u32 val, sys_clk_rate, timeout, waittime;
	u32 vddmin, vddmax, vstepmin, vstepmax;
54

55
	if (!voltdm->read || !voltdm->write) {
56 57 58 59 60
		pr_err("%s: No read/write API for accessing vdd_%s regs\n",
			__func__, voltdm->name);
		return;
	}

61 62 63 64 65
	vp->enabled = false;

	/* Divide to avoid overflow */
	sys_clk_rate = voltdm->sys_clk.rate / 1000;

66 67 68
	timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
	vddmin = voltdm->pmic->vp_vddmin;
	vddmax = voltdm->pmic->vp_vddmax;
69 70 71

	waittime = ((voltdm->pmic->step_size / voltdm->pmic->slew_rate) *
		    sys_clk_rate) / 1000;
72 73 74 75 76 77 78 79 80
	vstepmin = voltdm->pmic->vp_vstepmin;
	vstepmax = voltdm->pmic->vp_vstepmax;

	/*
	 * VP_CONFIG: error gain is not set here, it will be updated
	 * on each scale, based on OPP.
	 */
	val = (voltdm->pmic->vp_erroroffset <<
	       __ffs(voltdm->vp->common->vpconfig_erroroffset_mask)) |
81
		vp->common->vpconfig_timeouten;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	voltdm->write(val, vp->vpconfig);

	/* VSTEPMIN */
	val = (waittime << vp->common->vstepmin_smpswaittimemin_shift) |
		(vstepmin <<  vp->common->vstepmin_stepmin_shift);
	voltdm->write(val, vp->vstepmin);

	/* VSTEPMAX */
	val = (vstepmax << vp->common->vstepmax_stepmax_shift) |
		(waittime << vp->common->vstepmax_smpswaittimemax_shift);
	voltdm->write(val, vp->vstepmax);

	/* VLIMITTO */
	val = (vddmax << vp->common->vlimitto_vddmax_shift) |
		(vddmin << vp->common->vlimitto_vddmin_shift) |
		(timeout <<  vp->common->vlimitto_timeout_shift);
	voltdm->write(val, vp->vlimitto);
99 100
}

101 102 103 104 105
int omap_vp_update_errorgain(struct voltagedomain *voltdm,
			     unsigned long target_volt)
{
	struct omap_volt_data *volt_data;

106 107 108
	if (!voltdm->vp)
		return -EINVAL;

109 110 111 112 113 114 115 116 117 118 119 120 121 122
	/* Get volt_data corresponding to target_volt */
	volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
	if (IS_ERR(volt_data))
		return -EINVAL;

	/* Setting vp errorgain based on the voltage */
	voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
		    volt_data->vp_errgain <<
		    __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
		    voltdm->vp->vpconfig);

	return 0;
}

123 124 125 126
/* VP force update method of voltage scaling */
int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
			      unsigned long target_volt)
{
127
	struct omap_vp_instance *vp = voltdm->vp;
128 129 130 131 132 133 134 135 136 137 138 139 140
	u32 vpconfig;
	u8 target_vsel, current_vsel;
	int ret, timeout = 0;

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

	/*
	 * Clear all pending TransactionDone interrupt/status. Typical latency
	 * is <3us
	 */
	while (timeout++ < VP_TRANXDONE_TIMEOUT) {
141 142
		vp->common->ops->clear_txdone(vp->id);
		if (!vp->common->ops->check_txdone(vp->id))
143 144 145 146 147 148 149 150 151 152
			break;
		udelay(1);
	}
	if (timeout >= VP_TRANXDONE_TIMEOUT) {
		pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
			"Voltage change aborted", __func__, voltdm->name);
		return -ETIMEDOUT;
	}

	/* Configure for VP-Force Update */
153
	vpconfig = voltdm->read(vp->vpconfig);
154 155 156
	vpconfig &= ~(vp->common->vpconfig_initvdd |
			vp->common->vpconfig_forceupdate |
			vp->common->vpconfig_initvoltage_mask);
157
	vpconfig |= ((target_vsel <<
158
		      __ffs(vp->common->vpconfig_initvoltage_mask)));
159
	voltdm->write(vpconfig, vp->vpconfig);
160 161

	/* Trigger initVDD value copy to voltage processor */
162
	vpconfig |= vp->common->vpconfig_initvdd;
163
	voltdm->write(vpconfig, vp->vpconfig);
164 165

	/* Force update of voltage */
166
	vpconfig |= vp->common->vpconfig_forceupdate;
167
	voltdm->write(vpconfig, vp->vpconfig);
168 169 170 171 172 173

	/*
	 * Wait for TransactionDone. Typical latency is <200us.
	 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
	 */
	timeout = 0;
174
	omap_test_timeout(vp->common->ops->check_txdone(vp->id),
175 176 177 178 179 180 181 182 183 184 185 186 187 188
			  VP_TRANXDONE_TIMEOUT, timeout);
	if (timeout >= VP_TRANXDONE_TIMEOUT)
		pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
			"TRANXDONE never got set after the voltage update\n",
			__func__, voltdm->name);

	omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);

	/*
	 * Disable TransactionDone interrupt , clear all status, clear
	 * control registers
	 */
	timeout = 0;
	while (timeout++ < VP_TRANXDONE_TIMEOUT) {
189 190
		vp->common->ops->clear_txdone(vp->id);
		if (!vp->common->ops->check_txdone(vp->id))
191 192 193 194 195 196 197 198 199
			break;
		udelay(1);
	}

	if (timeout >= VP_TRANXDONE_TIMEOUT)
		pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
			"to clear the TRANXDONE status\n",
			__func__, voltdm->name);

200
	vpconfig = voltdm->read(vp->vpconfig);
201
	/* Clear initVDD copy trigger bit */
202
	vpconfig &= ~vp->common->vpconfig_initvdd;
203
	voltdm->write(vpconfig, vp->vpconfig);
204
	/* Clear force bit */
205
	vpconfig &= ~vp->common->vpconfig_forceupdate;
206
	voltdm->write(vpconfig, vp->vpconfig);
207 208 209 210 211 212 213 214 215 216 217 218

	return 0;
}

/**
 * omap_vp_get_curr_volt() - API to get the current vp voltage.
 * @voltdm:	pointer to the VDD.
 *
 * This API returns the current voltage for the specified voltage processor
 */
unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
{
219
	struct omap_vp_instance *vp = voltdm->vp;
220 221 222 223 224 225 226
	u8 curr_vsel;

	if (!voltdm || IS_ERR(voltdm)) {
		pr_warning("%s: VDD specified does not exist!\n", __func__);
		return 0;
	}

227
	if (!voltdm->read) {
228 229 230 231 232
		pr_err("%s: No read API for reading vdd_%s regs\n",
			__func__, voltdm->name);
		return 0;
	}

233 234
	curr_vsel = (voltdm->read(vp->voltage) & vp->common->vpvoltage_mask)
		>> __ffs(vp->common->vpvoltage_mask);
235

236
	if (!voltdm->pmic || !voltdm->pmic->vsel_to_uv) {
237 238 239 240 241
		pr_warning("%s: PMIC function to convert vsel to voltage"
			"in uV not registerd\n", __func__);
		return 0;
	}

242
	return voltdm->pmic->vsel_to_uv(curr_vsel);
243 244 245 246 247 248 249 250 251 252 253
}

/**
 * omap_vp_enable() - API to enable a particular VP
 * @voltdm:	pointer to the VDD whose VP is to be enabled.
 *
 * This API enables a particular voltage processor. Needed by the smartreflex
 * class drivers.
 */
void omap_vp_enable(struct voltagedomain *voltdm)
{
254
	struct omap_vp_instance *vp;
255 256 257 258 259 260 261
	u32 vpconfig;

	if (!voltdm || IS_ERR(voltdm)) {
		pr_warning("%s: VDD specified does not exist!\n", __func__);
		return;
	}

262
	vp = voltdm->vp;
263
	if (!voltdm->read || !voltdm->write) {
264 265 266 267 268 269
		pr_err("%s: No read/write API for accessing vdd_%s regs\n",
			__func__, voltdm->name);
		return;
	}

	/* If VP is already enabled, do nothing. Return */
270
	if (vp->enabled)
271 272 273 274 275
		return;

	vp_latch_vsel(voltdm);

	/* Enable VP */
276
	vpconfig = voltdm->read(vp->vpconfig);
277
	vpconfig |= vp->common->vpconfig_vpenable;
278
	voltdm->write(vpconfig, vp->vpconfig);
279
	vp->enabled = true;
280 281 282 283 284 285 286 287 288 289 290
}

/**
 * omap_vp_disable() - API to disable a particular VP
 * @voltdm:	pointer to the VDD whose VP is to be disabled.
 *
 * This API disables a particular voltage processor. Needed by the smartreflex
 * class drivers.
 */
void omap_vp_disable(struct voltagedomain *voltdm)
{
291
	struct omap_vp_instance *vp;
292 293 294 295 296 297 298 299
	u32 vpconfig;
	int timeout;

	if (!voltdm || IS_ERR(voltdm)) {
		pr_warning("%s: VDD specified does not exist!\n", __func__);
		return;
	}

300
	vp = voltdm->vp;
301
	if (!voltdm->read || !voltdm->write) {
302 303 304 305 306 307
		pr_err("%s: No read/write API for accessing vdd_%s regs\n",
			__func__, voltdm->name);
		return;
	}

	/* If VP is already disabled, do nothing. Return */
308
	if (!vp->enabled) {
309 310 311 312 313 314
		pr_warning("%s: Trying to disable VP for vdd_%s when"
			"it is already disabled\n", __func__, voltdm->name);
		return;
	}

	/* Disable VP */
315
	vpconfig = voltdm->read(vp->vpconfig);
316
	vpconfig &= ~vp->common->vpconfig_vpenable;
317
	voltdm->write(vpconfig, vp->vpconfig);
318 319 320 321

	/*
	 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
	 */
322 323
	omap_test_timeout((voltdm->read(vp->vstatus)),
			  VP_IDLE_TIMEOUT, timeout);
324 325 326 327 328

	if (timeout >= VP_IDLE_TIMEOUT)
		pr_warning("%s: vdd_%s idle timedout\n",
			__func__, voltdm->name);

329
	vp->enabled = false;
330 331 332

	return;
}