blob: e7d38f67db87c196073f815eb850f465d48be867 [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
106 /* Get volt_data corresponding to target_volt */
107 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
108 if (IS_ERR(volt_data))
109 return -EINVAL;
110
111 /* Setting vp errorgain based on the voltage */
112 voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
113 volt_data->vp_errgain <<
114 __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
115 voltdm->vp->vpconfig);
116
117 return 0;
118}
119
Kevin Hilman01f48d32011-03-21 14:29:13 -0700120/* VP force update method of voltage scaling */
121int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
122 unsigned long target_volt)
123{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700124 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700125 u32 vpconfig;
126 u8 target_vsel, current_vsel;
127 int ret, timeout = 0;
128
129 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
130 if (ret)
131 return ret;
132
133 /*
134 * Clear all pending TransactionDone interrupt/status. Typical latency
135 * is <3us
136 */
137 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700138 vp->common->ops->clear_txdone(vp->id);
139 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700140 break;
141 udelay(1);
142 }
143 if (timeout >= VP_TRANXDONE_TIMEOUT) {
144 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
145 "Voltage change aborted", __func__, voltdm->name);
146 return -ETIMEDOUT;
147 }
148
149 /* Configure for VP-Force Update */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700150 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700151 vpconfig &= ~(vp->common->vpconfig_initvdd |
152 vp->common->vpconfig_forceupdate |
153 vp->common->vpconfig_initvoltage_mask);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700154 vpconfig |= ((target_vsel <<
Kevin Hilman0ec30412011-04-04 16:02:28 -0700155 __ffs(vp->common->vpconfig_initvoltage_mask)));
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700156 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700157
158 /* Trigger initVDD value copy to voltage processor */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700159 vpconfig |= vp->common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700160 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700161
162 /* Force update of voltage */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700163 vpconfig |= vp->common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700164 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700165
166 /*
167 * Wait for TransactionDone. Typical latency is <200us.
168 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
169 */
170 timeout = 0;
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700171 omap_test_timeout(vp->common->ops->check_txdone(vp->id),
Kevin Hilman01f48d32011-03-21 14:29:13 -0700172 VP_TRANXDONE_TIMEOUT, timeout);
173 if (timeout >= VP_TRANXDONE_TIMEOUT)
174 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
175 "TRANXDONE never got set after the voltage update\n",
176 __func__, voltdm->name);
177
178 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
179
180 /*
181 * Disable TransactionDone interrupt , clear all status, clear
182 * control registers
183 */
184 timeout = 0;
185 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700186 vp->common->ops->clear_txdone(vp->id);
187 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700188 break;
189 udelay(1);
190 }
191
192 if (timeout >= VP_TRANXDONE_TIMEOUT)
193 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
194 "to clear the TRANXDONE status\n",
195 __func__, voltdm->name);
196
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700197 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700198 /* Clear initVDD copy trigger bit */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700199 vpconfig &= ~vp->common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700200 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700201 /* Clear force bit */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700202 vpconfig &= ~vp->common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700203 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700204
205 return 0;
206}
207
208/**
209 * omap_vp_get_curr_volt() - API to get the current vp voltage.
210 * @voltdm: pointer to the VDD.
211 *
212 * This API returns the current voltage for the specified voltage processor
213 */
214unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
215{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700216 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700217 u8 curr_vsel;
218
219 if (!voltdm || IS_ERR(voltdm)) {
220 pr_warning("%s: VDD specified does not exist!\n", __func__);
221 return 0;
222 }
223
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700224 if (!voltdm->read) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700225 pr_err("%s: No read API for reading vdd_%s regs\n",
226 __func__, voltdm->name);
227 return 0;
228 }
229
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700230 curr_vsel = voltdm->read(vp->voltage);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700231
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700232 if (!voltdm->pmic || !voltdm->pmic->vsel_to_uv) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700233 pr_warning("%s: PMIC function to convert vsel to voltage"
234 "in uV not registerd\n", __func__);
235 return 0;
236 }
237
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700238 return voltdm->pmic->vsel_to_uv(curr_vsel);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700239}
240
241/**
242 * omap_vp_enable() - API to enable a particular VP
243 * @voltdm: pointer to the VDD whose VP is to be enabled.
244 *
245 * This API enables a particular voltage processor. Needed by the smartreflex
246 * class drivers.
247 */
248void omap_vp_enable(struct voltagedomain *voltdm)
249{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700250 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700251 u32 vpconfig;
252
253 if (!voltdm || IS_ERR(voltdm)) {
254 pr_warning("%s: VDD specified does not exist!\n", __func__);
255 return;
256 }
257
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700258 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700259 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700260 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
261 __func__, voltdm->name);
262 return;
263 }
264
265 /* If VP is already enabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700266 if (vp->enabled)
Kevin Hilman01f48d32011-03-21 14:29:13 -0700267 return;
268
269 vp_latch_vsel(voltdm);
270
271 /* Enable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700272 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700273 vpconfig |= vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700274 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700275 vp->enabled = true;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700276}
277
278/**
279 * omap_vp_disable() - API to disable a particular VP
280 * @voltdm: pointer to the VDD whose VP is to be disabled.
281 *
282 * This API disables a particular voltage processor. Needed by the smartreflex
283 * class drivers.
284 */
285void omap_vp_disable(struct voltagedomain *voltdm)
286{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700287 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700288 u32 vpconfig;
289 int timeout;
290
291 if (!voltdm || IS_ERR(voltdm)) {
292 pr_warning("%s: VDD specified does not exist!\n", __func__);
293 return;
294 }
295
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700296 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700297 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700298 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
299 __func__, voltdm->name);
300 return;
301 }
302
303 /* If VP is already disabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700304 if (!vp->enabled) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700305 pr_warning("%s: Trying to disable VP for vdd_%s when"
306 "it is already disabled\n", __func__, voltdm->name);
307 return;
308 }
309
310 /* Disable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700311 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700312 vpconfig &= ~vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700313 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700314
315 /*
316 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
317 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700318 omap_test_timeout((voltdm->read(vp->vstatus)),
319 VP_IDLE_TIMEOUT, timeout);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700320
321 if (timeout >= VP_IDLE_TIMEOUT)
322 pr_warning("%s: vdd_%s idle timedout\n",
323 __func__, voltdm->name);
324
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700325 vp->enabled = false;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700326
327 return;
328}