blob: 85241b828c029e45b2f494925af3a3296cd92982 [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
Tony Lindgren4e653312011-11-10 22:45:17 +01004#include "common.h"
Kevin Hilman01f48d32011-03-21 14:29:13 -07005
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 Hilmanb666b472011-07-15 17:05:48 -070012static u32 _vp_set_init_voltage(struct voltagedomain *voltdm, u32 volt)
Kevin Hilman01f48d32011-03-21 14:29:13 -070013{
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;
Kevin Hilman01f48d32011-03-21 14:29:13 -070016 char vsel;
Kevin Hilman01f48d32011-03-21 14:29:13 -070017
Kevin Hilmanb666b472011-07-15 17:05:48 -070018 vsel = voltdm->pmic->uv_to_vsel(volt);
Kevin Hilman01f48d32011-03-21 14:29:13 -070019
Kevin Hilman4bcc4752011-03-28 10:40:15 -070020 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070021 vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
Kevin Hilmanb666b472011-07-15 17:05:48 -070022 vp->common->vpconfig_forceupdate |
23 vp->common->vpconfig_initvdd);
Kevin Hilman0ec30412011-04-04 16:02:28 -070024 vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
Kevin Hilman4bcc4752011-03-28 10:40:15 -070025 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070026
27 /* Trigger initVDD value copy to voltage processor */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070028 voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
Kevin Hilman4bcc4752011-03-28 10:40:15 -070029 vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070030
31 /* Clear initVDD copy trigger bit */
Kevin Hilman4bcc4752011-03-28 10:40:15 -070032 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb666b472011-07-15 17:05:48 -070033
34 return vpconfig;
Kevin Hilman01f48d32011-03-21 14:29:13 -070035}
36
37/* Generic voltage init functions */
38void __init omap_vp_init(struct voltagedomain *voltdm)
39{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070040 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman667216d2011-04-04 17:58:21 -070041 u32 val, sys_clk_rate, timeout, waittime;
42 u32 vddmin, vddmax, vstepmin, vstepmax;
Kevin Hilman01f48d32011-03-21 14:29:13 -070043
Russell Kingd980e0f2012-02-07 09:42:11 +000044 if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
45 pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
46 return;
47 }
48
Kevin Hilman4bcc4752011-03-28 10:40:15 -070049 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -070050 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
51 __func__, voltdm->name);
52 return;
53 }
54
Kevin Hilman6f567272011-07-14 11:10:27 -070055 vp->enabled = false;
56
57 /* Divide to avoid overflow */
58 sys_clk_rate = voltdm->sys_clk.rate / 1000;
59
Kevin Hilman667216d2011-04-04 17:58:21 -070060 timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
61 vddmin = voltdm->pmic->vp_vddmin;
62 vddmax = voltdm->pmic->vp_vddmax;
Kevin Hilman6f567272011-07-14 11:10:27 -070063
Yuan Jiangli3223d002012-03-06 12:51:12 -060064 waittime = DIV_ROUND_UP(voltdm->pmic->step_size * sys_clk_rate,
65 1000 * voltdm->pmic->slew_rate);
Kevin Hilman667216d2011-04-04 17:58:21 -070066 vstepmin = voltdm->pmic->vp_vstepmin;
67 vstepmax = voltdm->pmic->vp_vstepmax;
Kevin Hilman6f567272011-07-14 11:10:27 -070068
Kevin Hilman667216d2011-04-04 17:58:21 -070069 /*
70 * VP_CONFIG: error gain is not set here, it will be updated
71 * on each scale, based on OPP.
72 */
73 val = (voltdm->pmic->vp_erroroffset <<
74 __ffs(voltdm->vp->common->vpconfig_erroroffset_mask)) |
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070075 vp->common->vpconfig_timeouten;
Kevin Hilman667216d2011-04-04 17:58:21 -070076 voltdm->write(val, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070077
Kevin Hilman667216d2011-04-04 17:58:21 -070078 /* VSTEPMIN */
79 val = (waittime << vp->common->vstepmin_smpswaittimemin_shift) |
80 (vstepmin << vp->common->vstepmin_stepmin_shift);
81 voltdm->write(val, vp->vstepmin);
Kevin Hilman01f48d32011-03-21 14:29:13 -070082
Kevin Hilman667216d2011-04-04 17:58:21 -070083 /* VSTEPMAX */
84 val = (vstepmax << vp->common->vstepmax_stepmax_shift) |
85 (waittime << vp->common->vstepmax_smpswaittimemax_shift);
86 voltdm->write(val, vp->vstepmax);
Kevin Hilman01f48d32011-03-21 14:29:13 -070087
Kevin Hilman667216d2011-04-04 17:58:21 -070088 /* VLIMITTO */
89 val = (vddmax << vp->common->vlimitto_vddmax_shift) |
90 (vddmin << vp->common->vlimitto_vddmin_shift) |
91 (timeout << vp->common->vlimitto_timeout_shift);
92 voltdm->write(val, vp->vlimitto);
Kevin Hilman01f48d32011-03-21 14:29:13 -070093}
94
Kevin Hilman76ea7422011-04-05 15:15:31 -070095int omap_vp_update_errorgain(struct voltagedomain *voltdm,
96 unsigned long target_volt)
97{
98 struct omap_volt_data *volt_data;
99
Kevin Hilman8798c4a2011-07-18 15:31:43 -0700100 if (!voltdm->vp)
101 return -EINVAL;
102
Kevin Hilman76ea7422011-04-05 15:15:31 -0700103 /* Get volt_data corresponding to target_volt */
104 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
105 if (IS_ERR(volt_data))
106 return -EINVAL;
107
108 /* Setting vp errorgain based on the voltage */
109 voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
110 volt_data->vp_errgain <<
111 __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
112 voltdm->vp->vpconfig);
113
114 return 0;
115}
116
Kevin Hilman01f48d32011-03-21 14:29:13 -0700117/* VP force update method of voltage scaling */
118int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
119 unsigned long target_volt)
120{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700121 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700122 u32 vpconfig;
123 u8 target_vsel, current_vsel;
124 int ret, timeout = 0;
125
126 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
127 if (ret)
128 return ret;
129
130 /*
131 * Clear all pending TransactionDone interrupt/status. Typical latency
132 * is <3us
133 */
134 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700135 vp->common->ops->clear_txdone(vp->id);
136 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700137 break;
138 udelay(1);
139 }
140 if (timeout >= VP_TRANXDONE_TIMEOUT) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600141 pr_warn("%s: vdd_%s TRANXDONE timeout exceeded. Voltage change aborted",
142 __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700143 return -ETIMEDOUT;
144 }
145
Kevin Hilmanb666b472011-07-15 17:05:48 -0700146 vpconfig = _vp_set_init_voltage(voltdm, target_volt);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700147
148 /* Force update of voltage */
Kevin Hilmanb666b472011-07-15 17:05:48 -0700149 voltdm->write(vpconfig | vp->common->vpconfig_forceupdate,
150 voltdm->vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700151
152 /*
153 * Wait for TransactionDone. Typical latency is <200us.
154 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
155 */
156 timeout = 0;
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700157 omap_test_timeout(vp->common->ops->check_txdone(vp->id),
Kevin Hilman01f48d32011-03-21 14:29:13 -0700158 VP_TRANXDONE_TIMEOUT, timeout);
159 if (timeout >= VP_TRANXDONE_TIMEOUT)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600160 pr_err("%s: vdd_%s TRANXDONE timeout exceeded. TRANXDONE never got set after the voltage update\n",
161 __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700162
163 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
164
165 /*
166 * Disable TransactionDone interrupt , clear all status, clear
167 * control registers
168 */
169 timeout = 0;
170 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700171 vp->common->ops->clear_txdone(vp->id);
172 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700173 break;
174 udelay(1);
175 }
176
177 if (timeout >= VP_TRANXDONE_TIMEOUT)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600178 pr_warn("%s: vdd_%s TRANXDONE timeout exceeded while trying to clear the TRANXDONE status\n",
Kevin Hilman01f48d32011-03-21 14:29:13 -0700179 __func__, voltdm->name);
180
Kevin Hilman01f48d32011-03-21 14:29:13 -0700181 /* Clear force bit */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700182 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700183
184 return 0;
185}
186
187/**
Kevin Hilman01f48d32011-03-21 14:29:13 -0700188 * omap_vp_enable() - API to enable a particular VP
189 * @voltdm: pointer to the VDD whose VP is to be enabled.
190 *
191 * This API enables a particular voltage processor. Needed by the smartreflex
192 * class drivers.
193 */
194void omap_vp_enable(struct voltagedomain *voltdm)
195{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700196 struct omap_vp_instance *vp;
Kevin Hilmanb666b472011-07-15 17:05:48 -0700197 u32 vpconfig, volt;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700198
199 if (!voltdm || IS_ERR(voltdm)) {
200 pr_warning("%s: VDD specified does not exist!\n", __func__);
201 return;
202 }
203
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700204 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700205 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700206 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
207 __func__, voltdm->name);
208 return;
209 }
210
211 /* If VP is already enabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700212 if (vp->enabled)
Kevin Hilman01f48d32011-03-21 14:29:13 -0700213 return;
214
Kevin Hilmanb666b472011-07-15 17:05:48 -0700215 volt = voltdm_get_voltage(voltdm);
216 if (!volt) {
217 pr_warning("%s: unable to find current voltage for %s\n",
218 __func__, voltdm->name);
219 return;
220 }
221
222 vpconfig = _vp_set_init_voltage(voltdm, volt);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700223
224 /* Enable VP */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700225 vpconfig |= vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700226 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb666b472011-07-15 17:05:48 -0700227
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700228 vp->enabled = true;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700229}
230
231/**
232 * omap_vp_disable() - API to disable a particular VP
233 * @voltdm: pointer to the VDD whose VP is to be disabled.
234 *
235 * This API disables a particular voltage processor. Needed by the smartreflex
236 * class drivers.
237 */
238void omap_vp_disable(struct voltagedomain *voltdm)
239{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700240 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700241 u32 vpconfig;
242 int timeout;
243
244 if (!voltdm || IS_ERR(voltdm)) {
245 pr_warning("%s: VDD specified does not exist!\n", __func__);
246 return;
247 }
248
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700249 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700250 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700251 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
252 __func__, voltdm->name);
253 return;
254 }
255
256 /* If VP is already disabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700257 if (!vp->enabled) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600258 pr_warn("%s: Trying to disable VP for vdd_%s when it is already disabled\n",
259 __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700260 return;
261 }
262
263 /* Disable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700264 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700265 vpconfig &= ~vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700266 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700267
268 /*
269 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
270 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700271 omap_test_timeout((voltdm->read(vp->vstatus)),
272 VP_IDLE_TIMEOUT, timeout);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700273
274 if (timeout >= VP_IDLE_TIMEOUT)
275 pr_warning("%s: vdd_%s idle timedout\n",
276 __func__, voltdm->name);
277
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700278 vp->enabled = false;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700279
280 return;
281}