blob: 29698acc2aeaf1e9fa4fb386dd6be9ed16e7cc10 [file] [log] [blame]
Kevin Hilman01f48d32011-03-21 14:29:13 -07001#include <linux/kernel.h>
2#include <linux/init.h>
Kevin Hilman01f48d32011-03-21 14:29:13 -07003
4#include <plat/common.h>
5
6#include "voltage.h"
7#include "vp.h"
8#include "prm-regbits-34xx.h"
9#include "prm-regbits-44xx.h"
10#include "prm44xx.h"
11
Kevin Hilman01f48d32011-03-21 14:29:13 -070012static void vp_latch_vsel(struct voltagedomain *voltdm)
13{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070014 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -070015 u32 vpconfig;
16 unsigned long uvdc;
17 char vsel;
Kevin Hilman01f48d32011-03-21 14:29:13 -070018
19 uvdc = omap_voltage_get_nom_volt(voltdm);
20 if (!uvdc) {
21 pr_warning("%s: unable to find current voltage for vdd_%s\n",
22 __func__, voltdm->name);
23 return;
24 }
25
Kevin Hilmance8ebe02011-03-30 11:01:10 -070026 if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
Kevin Hilman01f48d32011-03-21 14:29:13 -070027 pr_warning("%s: PMIC function to convert voltage in uV to"
28 " vsel not registered\n", __func__);
29 return;
30 }
31
Kevin Hilmance8ebe02011-03-30 11:01:10 -070032 vsel = voltdm->pmic->uv_to_vsel(uvdc);
Kevin Hilman01f48d32011-03-21 14:29:13 -070033
Kevin Hilman4bcc4752011-03-28 10:40:15 -070034 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070035 vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
36 vp->common->vpconfig_initvdd);
Kevin Hilman0ec30412011-04-04 16:02:28 -070037 vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
Kevin Hilman4bcc4752011-03-28 10:40:15 -070038 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070039
40 /* Trigger initVDD value copy to voltage processor */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070041 voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
Kevin Hilman4bcc4752011-03-28 10:40:15 -070042 vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070043
44 /* Clear initVDD copy trigger bit */
Kevin Hilman4bcc4752011-03-28 10:40:15 -070045 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070046}
47
48/* Generic voltage init functions */
49void __init omap_vp_init(struct voltagedomain *voltdm)
50{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070051 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman667216d2011-04-04 17:58:21 -070052 u32 val, sys_clk_rate, timeout, waittime;
53 u32 vddmin, vddmax, vstepmin, vstepmax;
Kevin Hilman01f48d32011-03-21 14:29:13 -070054
Kevin Hilman4bcc4752011-03-28 10:40:15 -070055 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -070056 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
57 __func__, voltdm->name);
58 return;
59 }
60
Kevin Hilman6f567272011-07-14 11:10:27 -070061 vp->enabled = false;
62
63 /* Divide to avoid overflow */
64 sys_clk_rate = voltdm->sys_clk.rate / 1000;
65
Kevin Hilman667216d2011-04-04 17:58:21 -070066 timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
67 vddmin = voltdm->pmic->vp_vddmin;
68 vddmax = voltdm->pmic->vp_vddmax;
Kevin Hilman6f567272011-07-14 11:10:27 -070069
70 waittime = ((voltdm->pmic->step_size / voltdm->pmic->slew_rate) *
71 sys_clk_rate) / 1000;
Kevin Hilman667216d2011-04-04 17:58:21 -070072 vstepmin = voltdm->pmic->vp_vstepmin;
73 vstepmax = voltdm->pmic->vp_vstepmax;
Kevin Hilman6f567272011-07-14 11:10:27 -070074
Kevin Hilman667216d2011-04-04 17:58:21 -070075 /*
76 * VP_CONFIG: error gain is not set here, it will be updated
77 * on each scale, based on OPP.
78 */
79 val = (voltdm->pmic->vp_erroroffset <<
80 __ffs(voltdm->vp->common->vpconfig_erroroffset_mask)) |
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070081 vp->common->vpconfig_timeouten;
Kevin Hilman667216d2011-04-04 17:58:21 -070082 voltdm->write(val, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070083
Kevin Hilman667216d2011-04-04 17:58:21 -070084 /* VSTEPMIN */
85 val = (waittime << vp->common->vstepmin_smpswaittimemin_shift) |
86 (vstepmin << vp->common->vstepmin_stepmin_shift);
87 voltdm->write(val, vp->vstepmin);
Kevin Hilman01f48d32011-03-21 14:29:13 -070088
Kevin Hilman667216d2011-04-04 17:58:21 -070089 /* VSTEPMAX */
90 val = (vstepmax << vp->common->vstepmax_stepmax_shift) |
91 (waittime << vp->common->vstepmax_smpswaittimemax_shift);
92 voltdm->write(val, vp->vstepmax);
Kevin Hilman01f48d32011-03-21 14:29:13 -070093
Kevin Hilman667216d2011-04-04 17:58:21 -070094 /* VLIMITTO */
95 val = (vddmax << vp->common->vlimitto_vddmax_shift) |
96 (vddmin << vp->common->vlimitto_vddmin_shift) |
97 (timeout << vp->common->vlimitto_timeout_shift);
98 voltdm->write(val, vp->vlimitto);
Kevin Hilman01f48d32011-03-21 14:29:13 -070099}
100
Kevin Hilman76ea7422011-04-05 15:15:31 -0700101int omap_vp_update_errorgain(struct voltagedomain *voltdm,
102 unsigned long target_volt)
103{
104 struct omap_volt_data *volt_data;
105
Kevin Hilman8798c4a2011-07-18 15:31:43 -0700106 if (!voltdm->vp)
107 return -EINVAL;
108
Kevin Hilman76ea7422011-04-05 15:15:31 -0700109 /* Get volt_data corresponding to target_volt */
110 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
111 if (IS_ERR(volt_data))
112 return -EINVAL;
113
114 /* Setting vp errorgain based on the voltage */
115 voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
116 volt_data->vp_errgain <<
117 __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
118 voltdm->vp->vpconfig);
119
120 return 0;
121}
122
Kevin Hilman01f48d32011-03-21 14:29:13 -0700123/* VP force update method of voltage scaling */
124int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
125 unsigned long target_volt)
126{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700127 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700128 u32 vpconfig;
129 u8 target_vsel, current_vsel;
130 int ret, timeout = 0;
131
132 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
133 if (ret)
134 return ret;
135
136 /*
137 * Clear all pending TransactionDone interrupt/status. Typical latency
138 * is <3us
139 */
140 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700141 vp->common->ops->clear_txdone(vp->id);
142 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700143 break;
144 udelay(1);
145 }
146 if (timeout >= VP_TRANXDONE_TIMEOUT) {
147 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
148 "Voltage change aborted", __func__, voltdm->name);
149 return -ETIMEDOUT;
150 }
151
152 /* Configure for VP-Force Update */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700153 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700154 vpconfig &= ~(vp->common->vpconfig_initvdd |
155 vp->common->vpconfig_forceupdate |
156 vp->common->vpconfig_initvoltage_mask);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700157 vpconfig |= ((target_vsel <<
Kevin Hilman0ec30412011-04-04 16:02:28 -0700158 __ffs(vp->common->vpconfig_initvoltage_mask)));
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700159 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700160
161 /* Trigger initVDD value copy to voltage processor */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700162 vpconfig |= vp->common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700163 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700164
165 /* Force update of voltage */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700166 vpconfig |= vp->common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700167 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700168
169 /*
170 * Wait for TransactionDone. Typical latency is <200us.
171 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
172 */
173 timeout = 0;
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700174 omap_test_timeout(vp->common->ops->check_txdone(vp->id),
Kevin Hilman01f48d32011-03-21 14:29:13 -0700175 VP_TRANXDONE_TIMEOUT, timeout);
176 if (timeout >= VP_TRANXDONE_TIMEOUT)
177 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
178 "TRANXDONE never got set after the voltage update\n",
179 __func__, voltdm->name);
180
181 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
182
183 /*
184 * Disable TransactionDone interrupt , clear all status, clear
185 * control registers
186 */
187 timeout = 0;
188 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700189 vp->common->ops->clear_txdone(vp->id);
190 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700191 break;
192 udelay(1);
193 }
194
195 if (timeout >= VP_TRANXDONE_TIMEOUT)
196 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
197 "to clear the TRANXDONE status\n",
198 __func__, voltdm->name);
199
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700200 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700201 /* Clear initVDD copy trigger bit */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700202 vpconfig &= ~vp->common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700203 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700204 /* Clear force bit */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700205 vpconfig &= ~vp->common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700206 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700207
208 return 0;
209}
210
211/**
212 * omap_vp_get_curr_volt() - API to get the current vp voltage.
213 * @voltdm: pointer to the VDD.
214 *
215 * This API returns the current voltage for the specified voltage processor
216 */
217unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
218{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700219 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700220 u8 curr_vsel;
221
222 if (!voltdm || IS_ERR(voltdm)) {
223 pr_warning("%s: VDD specified does not exist!\n", __func__);
224 return 0;
225 }
226
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700227 if (!voltdm->read) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700228 pr_err("%s: No read API for reading vdd_%s regs\n",
229 __func__, voltdm->name);
230 return 0;
231 }
232
Todd Poynorbea30ed2011-05-27 19:15:59 -0700233 curr_vsel = (voltdm->read(vp->voltage) & vp->common->vpvoltage_mask)
234 >> __ffs(vp->common->vpvoltage_mask);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700235
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700236 if (!voltdm->pmic || !voltdm->pmic->vsel_to_uv) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700237 pr_warning("%s: PMIC function to convert vsel to voltage"
238 "in uV not registerd\n", __func__);
239 return 0;
240 }
241
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700242 return voltdm->pmic->vsel_to_uv(curr_vsel);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700243}
244
245/**
246 * omap_vp_enable() - API to enable a particular VP
247 * @voltdm: pointer to the VDD whose VP is to be enabled.
248 *
249 * This API enables a particular voltage processor. Needed by the smartreflex
250 * class drivers.
251 */
252void omap_vp_enable(struct voltagedomain *voltdm)
253{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700254 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700255 u32 vpconfig;
256
257 if (!voltdm || IS_ERR(voltdm)) {
258 pr_warning("%s: VDD specified does not exist!\n", __func__);
259 return;
260 }
261
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700262 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700263 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700264 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
265 __func__, voltdm->name);
266 return;
267 }
268
269 /* If VP is already enabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700270 if (vp->enabled)
Kevin Hilman01f48d32011-03-21 14:29:13 -0700271 return;
272
273 vp_latch_vsel(voltdm);
274
275 /* Enable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700276 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700277 vpconfig |= vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700278 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700279 vp->enabled = true;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700280}
281
282/**
283 * omap_vp_disable() - API to disable a particular VP
284 * @voltdm: pointer to the VDD whose VP is to be disabled.
285 *
286 * This API disables a particular voltage processor. Needed by the smartreflex
287 * class drivers.
288 */
289void omap_vp_disable(struct voltagedomain *voltdm)
290{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700291 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700292 u32 vpconfig;
293 int timeout;
294
295 if (!voltdm || IS_ERR(voltdm)) {
296 pr_warning("%s: VDD specified does not exist!\n", __func__);
297 return;
298 }
299
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700300 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700301 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700302 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
303 __func__, voltdm->name);
304 return;
305 }
306
307 /* If VP is already disabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700308 if (!vp->enabled) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700309 pr_warning("%s: Trying to disable VP for vdd_%s when"
310 "it is already disabled\n", __func__, voltdm->name);
311 return;
312 }
313
314 /* Disable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700315 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700316 vpconfig &= ~vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700317 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700318
319 /*
320 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
321 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700322 omap_test_timeout((voltdm->read(vp->vstatus)),
323 VP_IDLE_TIMEOUT, timeout);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700324
325 if (timeout >= VP_IDLE_TIMEOUT)
326 pr_warning("%s: vdd_%s idle timedout\n",
327 __func__, voltdm->name);
328
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700329 vp->enabled = false;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700330
331 return;
332}