blob: ea61a47bd199a977d0c531fe1dc36c5427d56f0b [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 Hilman01f48d32011-03-21 14:29:13 -070052 struct omap_vdd_info *vdd = voltdm->vdd;
Kevin Hilman6f567272011-07-14 11:10:27 -070053 u32 vp_val, sys_clk_rate, timeout_val, waittime;
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
66 vdd->vp_rt_data.vpconfig_erroroffset =
67 (voltdm->pmic->vp_erroroffset <<
68 __ffs(voltdm->vp->common->vpconfig_erroroffset_mask));
69
70 timeout_val = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
71 vdd->vp_rt_data.vlimitto_timeout = timeout_val;
72 vdd->vp_rt_data.vlimitto_vddmin = voltdm->pmic->vp_vddmin;
73 vdd->vp_rt_data.vlimitto_vddmax = voltdm->pmic->vp_vddmax;
74
75 waittime = ((voltdm->pmic->step_size / voltdm->pmic->slew_rate) *
76 sys_clk_rate) / 1000;
77 vdd->vp_rt_data.vstepmin_smpswaittimemin = waittime;
78 vdd->vp_rt_data.vstepmax_smpswaittimemax = waittime;
79 vdd->vp_rt_data.vstepmin_stepmin = voltdm->pmic->vp_vstepmin;
80 vdd->vp_rt_data.vstepmax_stepmax = voltdm->pmic->vp_vstepmax;
81
Kevin Hilman01f48d32011-03-21 14:29:13 -070082 vp_val = vdd->vp_rt_data.vpconfig_erroroffset |
83 (vdd->vp_rt_data.vpconfig_errorgain <<
Kevin Hilman0ec30412011-04-04 16:02:28 -070084 __ffs(vp->common->vpconfig_errorgain_mask)) |
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070085 vp->common->vpconfig_timeouten;
Kevin Hilman4bcc4752011-03-28 10:40:15 -070086 voltdm->write(vp_val, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070087
88 vp_val = ((vdd->vp_rt_data.vstepmin_smpswaittimemin <<
Kevin Hilman0ec30412011-04-04 16:02:28 -070089 vp->common->vstepmin_smpswaittimemin_shift) |
90 (vdd->vp_rt_data.vstepmin_stepmin <<
91 vp->common->vstepmin_stepmin_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -070092 voltdm->write(vp_val, vp->vstepmin);
Kevin Hilman01f48d32011-03-21 14:29:13 -070093
94 vp_val = ((vdd->vp_rt_data.vstepmax_smpswaittimemax <<
Kevin Hilman0ec30412011-04-04 16:02:28 -070095 vp->common->vstepmax_smpswaittimemax_shift) |
96 (vdd->vp_rt_data.vstepmax_stepmax <<
97 vp->common->vstepmax_stepmax_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -070098 voltdm->write(vp_val, vp->vstepmax);
Kevin Hilman01f48d32011-03-21 14:29:13 -070099
100 vp_val = ((vdd->vp_rt_data.vlimitto_vddmax <<
Kevin Hilman0ec30412011-04-04 16:02:28 -0700101 vp->common->vlimitto_vddmax_shift) |
102 (vdd->vp_rt_data.vlimitto_vddmin <<
103 vp->common->vlimitto_vddmin_shift) |
104 (vdd->vp_rt_data.vlimitto_timeout <<
105 vp->common->vlimitto_timeout_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700106 voltdm->write(vp_val, vp->vlimitto);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700107}
108
109/* VP force update method of voltage scaling */
110int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
111 unsigned long target_volt)
112{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700113 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700114 u32 vpconfig;
115 u8 target_vsel, current_vsel;
116 int ret, timeout = 0;
117
118 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
119 if (ret)
120 return ret;
121
122 /*
123 * Clear all pending TransactionDone interrupt/status. Typical latency
124 * is <3us
125 */
126 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700127 vp->common->ops->clear_txdone(vp->id);
128 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700129 break;
130 udelay(1);
131 }
132 if (timeout >= VP_TRANXDONE_TIMEOUT) {
133 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
134 "Voltage change aborted", __func__, voltdm->name);
135 return -ETIMEDOUT;
136 }
137
138 /* Configure for VP-Force Update */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700139 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700140 vpconfig &= ~(vp->common->vpconfig_initvdd |
141 vp->common->vpconfig_forceupdate |
142 vp->common->vpconfig_initvoltage_mask);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700143 vpconfig |= ((target_vsel <<
Kevin Hilman0ec30412011-04-04 16:02:28 -0700144 __ffs(vp->common->vpconfig_initvoltage_mask)));
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700145 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700146
147 /* Trigger initVDD value copy to voltage processor */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700148 vpconfig |= vp->common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700149 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700150
151 /* Force update of voltage */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700152 vpconfig |= vp->common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700153 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700154
155 /*
156 * Wait for TransactionDone. Typical latency is <200us.
157 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
158 */
159 timeout = 0;
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700160 omap_test_timeout(vp->common->ops->check_txdone(vp->id),
Kevin Hilman01f48d32011-03-21 14:29:13 -0700161 VP_TRANXDONE_TIMEOUT, timeout);
162 if (timeout >= VP_TRANXDONE_TIMEOUT)
163 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
164 "TRANXDONE never got set after the voltage update\n",
165 __func__, voltdm->name);
166
167 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
168
169 /*
170 * Disable TransactionDone interrupt , clear all status, clear
171 * control registers
172 */
173 timeout = 0;
174 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700175 vp->common->ops->clear_txdone(vp->id);
176 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700177 break;
178 udelay(1);
179 }
180
181 if (timeout >= VP_TRANXDONE_TIMEOUT)
182 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
183 "to clear the TRANXDONE status\n",
184 __func__, voltdm->name);
185
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700186 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700187 /* Clear initVDD copy trigger bit */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700188 vpconfig &= ~vp->common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700189 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700190 /* Clear force bit */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700191 vpconfig &= ~vp->common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700192 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700193
194 return 0;
195}
196
197/**
198 * omap_vp_get_curr_volt() - API to get the current vp voltage.
199 * @voltdm: pointer to the VDD.
200 *
201 * This API returns the current voltage for the specified voltage processor
202 */
203unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
204{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700205 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700206 u8 curr_vsel;
207
208 if (!voltdm || IS_ERR(voltdm)) {
209 pr_warning("%s: VDD specified does not exist!\n", __func__);
210 return 0;
211 }
212
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700213 if (!voltdm->read) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700214 pr_err("%s: No read API for reading vdd_%s regs\n",
215 __func__, voltdm->name);
216 return 0;
217 }
218
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700219 curr_vsel = voltdm->read(vp->voltage);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700220
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700221 if (!voltdm->pmic || !voltdm->pmic->vsel_to_uv) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700222 pr_warning("%s: PMIC function to convert vsel to voltage"
223 "in uV not registerd\n", __func__);
224 return 0;
225 }
226
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700227 return voltdm->pmic->vsel_to_uv(curr_vsel);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700228}
229
230/**
231 * omap_vp_enable() - API to enable a particular VP
232 * @voltdm: pointer to the VDD whose VP is to be enabled.
233 *
234 * This API enables a particular voltage processor. Needed by the smartreflex
235 * class drivers.
236 */
237void omap_vp_enable(struct voltagedomain *voltdm)
238{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700239 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700240 u32 vpconfig;
241
242 if (!voltdm || IS_ERR(voltdm)) {
243 pr_warning("%s: VDD specified does not exist!\n", __func__);
244 return;
245 }
246
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700247 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700248 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700249 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
250 __func__, voltdm->name);
251 return;
252 }
253
254 /* If VP is already enabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700255 if (vp->enabled)
Kevin Hilman01f48d32011-03-21 14:29:13 -0700256 return;
257
258 vp_latch_vsel(voltdm);
259
260 /* Enable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700261 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700262 vpconfig |= vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700263 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700264 vp->enabled = true;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700265}
266
267/**
268 * omap_vp_disable() - API to disable a particular VP
269 * @voltdm: pointer to the VDD whose VP is to be disabled.
270 *
271 * This API disables a particular voltage processor. Needed by the smartreflex
272 * class drivers.
273 */
274void omap_vp_disable(struct voltagedomain *voltdm)
275{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700276 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700277 u32 vpconfig;
278 int timeout;
279
280 if (!voltdm || IS_ERR(voltdm)) {
281 pr_warning("%s: VDD specified does not exist!\n", __func__);
282 return;
283 }
284
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700285 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700286 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700287 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
288 __func__, voltdm->name);
289 return;
290 }
291
292 /* If VP is already disabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700293 if (!vp->enabled) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700294 pr_warning("%s: Trying to disable VP for vdd_%s when"
295 "it is already disabled\n", __func__, voltdm->name);
296 return;
297 }
298
299 /* Disable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700300 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700301 vpconfig &= ~vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700302 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700303
304 /*
305 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
306 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700307 omap_test_timeout((voltdm->read(vp->vstatus)),
308 VP_IDLE_TIMEOUT, timeout);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700309
310 if (timeout >= VP_IDLE_TIMEOUT)
311 pr_warning("%s: vdd_%s idle timedout\n",
312 __func__, voltdm->name);
313
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700314 vp->enabled = false;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700315
316 return;
317}