blob: 88ac742a38cff13cc0151dfd9841a568a3aac7ae [file] [log] [blame]
Kevin Hilman01f48d32011-03-21 14:29:13 -07001#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/debugfs.h>
4
5#include <plat/common.h>
6
7#include "voltage.h"
8#include "vp.h"
9#include "prm-regbits-34xx.h"
10#include "prm-regbits-44xx.h"
11#include "prm44xx.h"
12
13static void __init vp_debugfs_init(struct voltagedomain *voltdm);
14
15static void vp_latch_vsel(struct voltagedomain *voltdm)
16{
17 struct omap_vp_instance_data *vp = voltdm->vdd->vp_data;
18 u32 vpconfig;
19 unsigned long uvdc;
20 char vsel;
21 struct omap_vdd_info *vdd = voltdm->vdd;
22
23 uvdc = omap_voltage_get_nom_volt(voltdm);
24 if (!uvdc) {
25 pr_warning("%s: unable to find current voltage for vdd_%s\n",
26 __func__, voltdm->name);
27 return;
28 }
29
30 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
31 pr_warning("%s: PMIC function to convert voltage in uV to"
32 " vsel not registered\n", __func__);
33 return;
34 }
35
36 vsel = vdd->pmic_info->uv_to_vsel(uvdc);
37
Kevin Hilman4bcc4752011-03-28 10:40:15 -070038 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070039 vpconfig &= ~(vp->vp_common->vpconfig_initvoltage_mask |
40 vp->vp_common->vpconfig_initvdd);
41 vpconfig |= vsel << vp->vp_common->vpconfig_initvoltage_shift;
42
Kevin Hilman4bcc4752011-03-28 10:40:15 -070043 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070044
45 /* Trigger initVDD value copy to voltage processor */
Kevin Hilman4bcc4752011-03-28 10:40:15 -070046 voltdm->write((vpconfig | vp->vp_common->vpconfig_initvdd),
47 vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070048
49 /* Clear initVDD copy trigger bit */
Kevin Hilman4bcc4752011-03-28 10:40:15 -070050 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070051}
52
53/* Generic voltage init functions */
54void __init omap_vp_init(struct voltagedomain *voltdm)
55{
56 struct omap_vp_instance_data *vp = voltdm->vdd->vp_data;
57 struct omap_vdd_info *vdd = voltdm->vdd;
58 u32 vp_val;
59
Kevin Hilman4bcc4752011-03-28 10:40:15 -070060 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -070061 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
62 __func__, voltdm->name);
63 return;
64 }
65
66 vp_val = vdd->vp_rt_data.vpconfig_erroroffset |
67 (vdd->vp_rt_data.vpconfig_errorgain <<
68 vp->vp_common->vpconfig_errorgain_shift) |
69 vp->vp_common->vpconfig_timeouten;
Kevin Hilman4bcc4752011-03-28 10:40:15 -070070 voltdm->write(vp_val, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070071
72 vp_val = ((vdd->vp_rt_data.vstepmin_smpswaittimemin <<
73 vp->vp_common->vstepmin_smpswaittimemin_shift) |
74 (vdd->vp_rt_data.vstepmin_stepmin <<
75 vp->vp_common->vstepmin_stepmin_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -070076 voltdm->write(vp_val, vp->vstepmin);
Kevin Hilman01f48d32011-03-21 14:29:13 -070077
78 vp_val = ((vdd->vp_rt_data.vstepmax_smpswaittimemax <<
79 vp->vp_common->vstepmax_smpswaittimemax_shift) |
80 (vdd->vp_rt_data.vstepmax_stepmax <<
81 vp->vp_common->vstepmax_stepmax_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -070082 voltdm->write(vp_val, vp->vstepmax);
Kevin Hilman01f48d32011-03-21 14:29:13 -070083
84 vp_val = ((vdd->vp_rt_data.vlimitto_vddmax <<
85 vp->vp_common->vlimitto_vddmax_shift) |
86 (vdd->vp_rt_data.vlimitto_vddmin <<
87 vp->vp_common->vlimitto_vddmin_shift) |
88 (vdd->vp_rt_data.vlimitto_timeout <<
89 vp->vp_common->vlimitto_timeout_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -070090 voltdm->write(vp_val, vp->vlimitto);
Kevin Hilman01f48d32011-03-21 14:29:13 -070091
92 vp_debugfs_init(voltdm);
93}
94
95/* VP force update method of voltage scaling */
96int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
97 unsigned long target_volt)
98{
99 struct omap_vp_instance_data *vp = voltdm->vdd->vp_data;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700100 u32 vpconfig;
101 u8 target_vsel, current_vsel;
102 int ret, timeout = 0;
103
104 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
105 if (ret)
106 return ret;
107
108 /*
109 * Clear all pending TransactionDone interrupt/status. Typical latency
110 * is <3us
111 */
112 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilman842ec222011-03-28 11:57:18 -0700113 vp->vp_common->ops->clear_txdone(vp->id);
114 if (!vp->vp_common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700115 break;
116 udelay(1);
117 }
118 if (timeout >= VP_TRANXDONE_TIMEOUT) {
119 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
120 "Voltage change aborted", __func__, voltdm->name);
121 return -ETIMEDOUT;
122 }
123
124 /* Configure for VP-Force Update */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700125 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700126 vpconfig &= ~(vp->vp_common->vpconfig_initvdd |
127 vp->vp_common->vpconfig_forceupdate |
128 vp->vp_common->vpconfig_initvoltage_mask);
129 vpconfig |= ((target_vsel <<
130 vp->vp_common->vpconfig_initvoltage_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700131 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700132
133 /* Trigger initVDD value copy to voltage processor */
134 vpconfig |= vp->vp_common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700135 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700136
137 /* Force update of voltage */
138 vpconfig |= vp->vp_common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700139 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700140
141 /*
142 * Wait for TransactionDone. Typical latency is <200us.
143 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
144 */
145 timeout = 0;
Kevin Hilman842ec222011-03-28 11:57:18 -0700146 omap_test_timeout(vp->vp_common->ops->check_txdone(vp->id),
Kevin Hilman01f48d32011-03-21 14:29:13 -0700147 VP_TRANXDONE_TIMEOUT, timeout);
148 if (timeout >= VP_TRANXDONE_TIMEOUT)
149 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
150 "TRANXDONE never got set after the voltage update\n",
151 __func__, voltdm->name);
152
153 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
154
155 /*
156 * Disable TransactionDone interrupt , clear all status, clear
157 * control registers
158 */
159 timeout = 0;
160 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilman842ec222011-03-28 11:57:18 -0700161 vp->vp_common->ops->clear_txdone(vp->id);
162 if (!vp->vp_common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700163 break;
164 udelay(1);
165 }
166
167 if (timeout >= VP_TRANXDONE_TIMEOUT)
168 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
169 "to clear the TRANXDONE status\n",
170 __func__, voltdm->name);
171
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700172 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700173 /* Clear initVDD copy trigger bit */
174 vpconfig &= ~vp->vp_common->vpconfig_initvdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700175 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700176 /* Clear force bit */
177 vpconfig &= ~vp->vp_common->vpconfig_forceupdate;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700178 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700179
180 return 0;
181}
182
183/**
184 * omap_vp_get_curr_volt() - API to get the current vp voltage.
185 * @voltdm: pointer to the VDD.
186 *
187 * This API returns the current voltage for the specified voltage processor
188 */
189unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
190{
191 struct omap_vp_instance_data *vp = voltdm->vdd->vp_data;
192 struct omap_vdd_info *vdd;
193 u8 curr_vsel;
194
195 if (!voltdm || IS_ERR(voltdm)) {
196 pr_warning("%s: VDD specified does not exist!\n", __func__);
197 return 0;
198 }
199
200 vdd = voltdm->vdd;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700201 if (!voltdm->read) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700202 pr_err("%s: No read API for reading vdd_%s regs\n",
203 __func__, voltdm->name);
204 return 0;
205 }
206
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700207 curr_vsel = voltdm->read(vp->voltage);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700208
209 if (!vdd->pmic_info || !vdd->pmic_info->vsel_to_uv) {
210 pr_warning("%s: PMIC function to convert vsel to voltage"
211 "in uV not registerd\n", __func__);
212 return 0;
213 }
214
215 return vdd->pmic_info->vsel_to_uv(curr_vsel);
216}
217
218/**
219 * omap_vp_enable() - API to enable a particular VP
220 * @voltdm: pointer to the VDD whose VP is to be enabled.
221 *
222 * This API enables a particular voltage processor. Needed by the smartreflex
223 * class drivers.
224 */
225void omap_vp_enable(struct voltagedomain *voltdm)
226{
227 struct omap_vp_instance_data *vp;
228 struct omap_vdd_info *vdd;
229 u32 vpconfig;
230
231 if (!voltdm || IS_ERR(voltdm)) {
232 pr_warning("%s: VDD specified does not exist!\n", __func__);
233 return;
234 }
235
236 vdd = voltdm->vdd;
237 vp = voltdm->vdd->vp_data;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700238 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700239 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
240 __func__, voltdm->name);
241 return;
242 }
243
244 /* If VP is already enabled, do nothing. Return */
245 if (vdd->vp_enabled)
246 return;
247
248 vp_latch_vsel(voltdm);
249
250 /* Enable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700251 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700252 vpconfig |= vp->vp_common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700253 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700254 vdd->vp_enabled = true;
255}
256
257/**
258 * omap_vp_disable() - API to disable a particular VP
259 * @voltdm: pointer to the VDD whose VP is to be disabled.
260 *
261 * This API disables a particular voltage processor. Needed by the smartreflex
262 * class drivers.
263 */
264void omap_vp_disable(struct voltagedomain *voltdm)
265{
266 struct omap_vp_instance_data *vp;
267 struct omap_vdd_info *vdd;
268 u32 vpconfig;
269 int timeout;
270
271 if (!voltdm || IS_ERR(voltdm)) {
272 pr_warning("%s: VDD specified does not exist!\n", __func__);
273 return;
274 }
275
276 vdd = voltdm->vdd;
277 vp = voltdm->vdd->vp_data;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700278 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700279 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
280 __func__, voltdm->name);
281 return;
282 }
283
284 /* If VP is already disabled, do nothing. Return */
285 if (!vdd->vp_enabled) {
286 pr_warning("%s: Trying to disable VP for vdd_%s when"
287 "it is already disabled\n", __func__, voltdm->name);
288 return;
289 }
290
291 /* Disable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700292 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700293 vpconfig &= ~vp->vp_common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700294 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700295
296 /*
297 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
298 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700299 omap_test_timeout((voltdm->read(vp->vstatus)),
300 VP_IDLE_TIMEOUT, timeout);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700301
302 if (timeout >= VP_IDLE_TIMEOUT)
303 pr_warning("%s: vdd_%s idle timedout\n",
304 __func__, voltdm->name);
305
306 vdd->vp_enabled = false;
307
308 return;
309}
310
311/* Voltage debugfs support */
312static int vp_volt_debug_get(void *data, u64 *val)
313{
314 struct voltagedomain *voltdm = (struct voltagedomain *)data;
315 struct omap_vp_instance_data *vp = voltdm->vdd->vp_data;
316 struct omap_vdd_info *vdd = voltdm->vdd;
317 u8 vsel;
318
319 if (!vdd) {
320 pr_warning("Wrong paramater passed\n");
321 return -EINVAL;
322 }
323
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700324 vsel = voltdm->read(vp->voltage);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700325
326 if (!vdd->pmic_info->vsel_to_uv) {
327 pr_warning("PMIC function to convert vsel to voltage"
328 "in uV not registerd\n");
329 return -EINVAL;
330 }
331
332 *val = vdd->pmic_info->vsel_to_uv(vsel);
333 return 0;
334}
335
336DEFINE_SIMPLE_ATTRIBUTE(vp_volt_debug_fops, vp_volt_debug_get, NULL, "%llu\n");
337
338static void __init vp_debugfs_init(struct voltagedomain *voltdm)
339{
340 struct omap_vdd_info *vdd = voltdm->vdd;
341 struct dentry *debug_dir;
342
343 debug_dir = debugfs_create_dir("vp", vdd->debug_dir);
344 if (IS_ERR(debug_dir))
345 pr_err("%s: Unable to create VP debugfs dir dir\n", __func__);
346
347 (void) debugfs_create_x16("errorgain", S_IRUGO, debug_dir,
348 &(vdd->vp_rt_data.vpconfig_errorgain));
349 (void) debugfs_create_x16("smpswaittimemin", S_IRUGO,
350 debug_dir,
351 &(vdd->vp_rt_data.vstepmin_smpswaittimemin));
352 (void) debugfs_create_x8("stepmin", S_IRUGO, debug_dir,
353 &(vdd->vp_rt_data.vstepmin_stepmin));
354 (void) debugfs_create_x16("smpswaittimemax", S_IRUGO,
355 debug_dir,
356 &(vdd->vp_rt_data.vstepmax_smpswaittimemax));
357 (void) debugfs_create_x8("stepmax", S_IRUGO, debug_dir,
358 &(vdd->vp_rt_data.vstepmax_stepmax));
359 (void) debugfs_create_x8("vddmax", S_IRUGO, debug_dir,
360 &(vdd->vp_rt_data.vlimitto_vddmax));
361 (void) debugfs_create_x8("vddmin", S_IRUGO, debug_dir,
362 &(vdd->vp_rt_data.vlimitto_vddmin));
363 (void) debugfs_create_x16("timeout", S_IRUGO, debug_dir,
364 &(vdd->vp_rt_data.vlimitto_timeout));
365 (void) debugfs_create_file("curr_volt", S_IRUGO, debug_dir,
366 (void *) voltdm, &vp_volt_debug_fops);
367}