vp.c 7.2 KB
Newer Older
1 2 3
#include <linux/kernel.h>
#include <linux/init.h>

4
#include "common.h"
5 6 7 8 9 10 11

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

12
static u32 _vp_set_init_voltage(struct voltagedomain *voltdm, u32 volt)
13
{
14
	struct omap_vp_instance *vp = voltdm->vp;
15 16 17
	u32 vpconfig;
	char vsel;

18
	vsel = voltdm->pmic->uv_to_vsel(volt);
19

20
	vpconfig = voltdm->read(vp->vpconfig);
21
	vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
22 23
		      vp->common->vpconfig_forceupdate |
		      vp->common->vpconfig_initvdd);
24
	vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
25
	voltdm->write(vpconfig, vp->vpconfig);
26 27

	/* Trigger initVDD value copy to voltage processor */
28
	voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
29
		       vp->vpconfig);
30 31

	/* Clear initVDD copy trigger bit */
32
	voltdm->write(vpconfig, vp->vpconfig);
33 34

	return vpconfig;
35 36 37 38 39
}

/* Generic voltage init functions */
void __init omap_vp_init(struct voltagedomain *voltdm)
{
40
	struct omap_vp_instance *vp = voltdm->vp;
41 42
	u32 val, sys_clk_rate, timeout, waittime;
	u32 vddmin, vddmax, vstepmin, vstepmax;
43

44 45 46 47 48
	if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
		pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
		return;
	}

49
	if (!voltdm->read || !voltdm->write) {
50 51 52 53 54
		pr_err("%s: No read/write API for accessing vdd_%s regs\n",
			__func__, voltdm->name);
		return;
	}

55 56 57 58 59
	vp->enabled = false;

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

60 61 62
	timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
	vddmin = voltdm->pmic->vp_vddmin;
	vddmax = voltdm->pmic->vp_vddmax;
63

64 65
	waittime = DIV_ROUND_UP(voltdm->pmic->step_size * sys_clk_rate,
				1000 * voltdm->pmic->slew_rate);
66 67 68 69 70 71 72 73 74
	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)) |
75
		vp->common->vpconfig_timeouten;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	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);
93 94
}

95 96 97 98 99
int omap_vp_update_errorgain(struct voltagedomain *voltdm,
			     unsigned long target_volt)
{
	struct omap_volt_data *volt_data;

100 101 102
	if (!voltdm->vp)
		return -EINVAL;

103 104 105 106 107 108 109 110 111 112 113 114 115 116
	/* 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;
}

117 118 119 120
/* VP force update method of voltage scaling */
int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
			      unsigned long target_volt)
{
121
	struct omap_vp_instance *vp = voltdm->vp;
122 123 124 125 126 127 128 129 130 131 132 133 134
	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) {
135 136
		vp->common->ops->clear_txdone(vp->id);
		if (!vp->common->ops->check_txdone(vp->id))
137 138 139 140 141 142 143 144 145
			break;
		udelay(1);
	}
	if (timeout >= VP_TRANXDONE_TIMEOUT) {
		pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
			"Voltage change aborted", __func__, voltdm->name);
		return -ETIMEDOUT;
	}

146
	vpconfig = _vp_set_init_voltage(voltdm, target_volt);
147 148

	/* Force update of voltage */
149 150
	voltdm->write(vpconfig | vp->common->vpconfig_forceupdate,
		      voltdm->vp->vpconfig);
151 152 153 154 155 156

	/*
	 * Wait for TransactionDone. Typical latency is <200us.
	 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
	 */
	timeout = 0;
157
	omap_test_timeout(vp->common->ops->check_txdone(vp->id),
158 159 160 161 162 163 164 165 166 167 168 169 170 171
			  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) {
172 173
		vp->common->ops->clear_txdone(vp->id);
		if (!vp->common->ops->check_txdone(vp->id))
174 175 176 177 178 179 180 181 182 183
			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);

	/* Clear force bit */
184
	voltdm->write(vpconfig, vp->vpconfig);
185 186 187 188 189 190 191 192 193 194 195 196 197

	return 0;
}

/**
 * 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)
{
198
	struct omap_vp_instance *vp;
199
	u32 vpconfig, volt;
200 201 202 203 204 205

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

206
	vp = voltdm->vp;
207
	if (!voltdm->read || !voltdm->write) {
208 209 210 211 212 213
		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 */
214
	if (vp->enabled)
215 216
		return;

217 218 219 220 221 222 223 224
	volt = voltdm_get_voltage(voltdm);
	if (!volt) {
		pr_warning("%s: unable to find current voltage for %s\n",
			   __func__, voltdm->name);
		return;
	}

	vpconfig = _vp_set_init_voltage(voltdm, volt);
225 226

	/* Enable VP */
227
	vpconfig |= vp->common->vpconfig_vpenable;
228
	voltdm->write(vpconfig, vp->vpconfig);
229

230
	vp->enabled = true;
231 232 233 234 235 236 237 238 239 240 241
}

/**
 * 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)
{
242
	struct omap_vp_instance *vp;
243 244 245 246 247 248 249 250
	u32 vpconfig;
	int timeout;

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

251
	vp = voltdm->vp;
252
	if (!voltdm->read || !voltdm->write) {
253 254 255 256 257 258
		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 */
259
	if (!vp->enabled) {
260 261 262 263 264 265
		pr_warning("%s: Trying to disable VP for vdd_%s when"
			"it is already disabled\n", __func__, voltdm->name);
		return;
	}

	/* Disable VP */
266
	vpconfig = voltdm->read(vp->vpconfig);
267
	vpconfig &= ~vp->common->vpconfig_vpenable;
268
	voltdm->write(vpconfig, vp->vpconfig);
269 270 271 272

	/*
	 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
	 */
273 274
	omap_test_timeout((voltdm->read(vp->vstatus)),
			  VP_IDLE_TIMEOUT, timeout);
275 276 277 278 279

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

280
	vp->enabled = false;
281 282 283

	return;
}