Merge remote-tracking branch 'upstream/master' into kvm-devel
[linux-2.6/kvm.git] / arch / arm / mach-omap2 / voltage.c
blob9ef3789ded4b0f1c5e48d1782c7004067a03d8cd
1 /*
2 * OMAP3/OMAP4 Voltage Management Routines
4 * Author: Thara Gopinath <thara@ti.com>
6 * Copyright (C) 2007 Texas Instruments, Inc.
7 * Rajendra Nayak <rnayak@ti.com>
8 * Lesly A M <x0080970@ti.com>
10 * Copyright (C) 2008, 2011 Nokia Corporation
11 * Kalle Jokiniemi
12 * Paul Walmsley
14 * Copyright (C) 2010 Texas Instruments, Inc.
15 * Thara Gopinath <thara@ti.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
22 #include <linux/delay.h>
23 #include <linux/io.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/debugfs.h>
27 #include <linux/slab.h>
29 #include <plat/common.h>
31 #include "prm-regbits-34xx.h"
32 #include "prm-regbits-44xx.h"
33 #include "prm44xx.h"
34 #include "prcm44xx.h"
35 #include "prminst44xx.h"
36 #include "control.h"
38 #include "voltage.h"
40 #include "vc.h"
41 #include "vp.h"
43 #define VOLTAGE_DIR_SIZE 16
46 static struct omap_vdd_info **vdd_info;
49 * Number of scalable voltage domains.
51 static int nr_scalable_vdd;
53 /* XXX document */
54 static s16 prm_mod_offs;
55 static s16 prm_irqst_ocp_mod_offs;
57 static struct dentry *voltage_dir;
59 /* Init function pointers */
60 static int vp_forceupdate_scale_voltage(struct omap_vdd_info *vdd,
61 unsigned long target_volt);
63 static u32 omap3_voltage_read_reg(u16 mod, u8 offset)
65 return omap2_prm_read_mod_reg(mod, offset);
68 static void omap3_voltage_write_reg(u32 val, u16 mod, u8 offset)
70 omap2_prm_write_mod_reg(val, mod, offset);
73 static u32 omap4_voltage_read_reg(u16 mod, u8 offset)
75 return omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
76 mod, offset);
79 static void omap4_voltage_write_reg(u32 val, u16 mod, u8 offset)
81 omap4_prminst_write_inst_reg(val, OMAP4430_PRM_PARTITION, mod, offset);
84 static int __init _config_common_vdd_data(struct omap_vdd_info *vdd)
86 char *sys_ck_name;
87 struct clk *sys_ck;
88 u32 sys_clk_speed, timeout_val, waittime;
91 * XXX Clockfw should handle this, or this should be in a
92 * struct record
94 if (cpu_is_omap24xx() || cpu_is_omap34xx())
95 sys_ck_name = "sys_ck";
96 else if (cpu_is_omap44xx())
97 sys_ck_name = "sys_clkin_ck";
98 else
99 return -EINVAL;
102 * Sys clk rate is require to calculate vp timeout value and
103 * smpswaittimemin and smpswaittimemax.
105 sys_ck = clk_get(NULL, sys_ck_name);
106 if (IS_ERR(sys_ck)) {
107 pr_warning("%s: Could not get the sys clk to calculate"
108 "various vdd_%s params\n", __func__, vdd->voltdm.name);
109 return -EINVAL;
111 sys_clk_speed = clk_get_rate(sys_ck);
112 clk_put(sys_ck);
113 /* Divide to avoid overflow */
114 sys_clk_speed /= 1000;
116 /* Generic voltage parameters */
117 vdd->volt_scale = vp_forceupdate_scale_voltage;
118 vdd->vp_enabled = false;
120 vdd->vp_rt_data.vpconfig_erroroffset =
121 (vdd->pmic_info->vp_erroroffset <<
122 vdd->vp_data->vp_common->vpconfig_erroroffset_shift);
124 timeout_val = (sys_clk_speed * vdd->pmic_info->vp_timeout_us) / 1000;
125 vdd->vp_rt_data.vlimitto_timeout = timeout_val;
126 vdd->vp_rt_data.vlimitto_vddmin = vdd->pmic_info->vp_vddmin;
127 vdd->vp_rt_data.vlimitto_vddmax = vdd->pmic_info->vp_vddmax;
129 waittime = ((vdd->pmic_info->step_size / vdd->pmic_info->slew_rate) *
130 sys_clk_speed) / 1000;
131 vdd->vp_rt_data.vstepmin_smpswaittimemin = waittime;
132 vdd->vp_rt_data.vstepmax_smpswaittimemax = waittime;
133 vdd->vp_rt_data.vstepmin_stepmin = vdd->pmic_info->vp_vstepmin;
134 vdd->vp_rt_data.vstepmax_stepmax = vdd->pmic_info->vp_vstepmax;
136 return 0;
139 /* Voltage debugfs support */
140 static int vp_volt_debug_get(void *data, u64 *val)
142 struct omap_vdd_info *vdd = (struct omap_vdd_info *) data;
143 u8 vsel;
145 if (!vdd) {
146 pr_warning("Wrong paramater passed\n");
147 return -EINVAL;
150 vsel = vdd->read_reg(prm_mod_offs, vdd->vp_data->voltage);
152 if (!vdd->pmic_info->vsel_to_uv) {
153 pr_warning("PMIC function to convert vsel to voltage"
154 "in uV not registerd\n");
155 return -EINVAL;
158 *val = vdd->pmic_info->vsel_to_uv(vsel);
159 return 0;
162 static int nom_volt_debug_get(void *data, u64 *val)
164 struct omap_vdd_info *vdd = (struct omap_vdd_info *) data;
166 if (!vdd) {
167 pr_warning("Wrong paramater passed\n");
168 return -EINVAL;
171 *val = omap_voltage_get_nom_volt(&vdd->voltdm);
173 return 0;
176 DEFINE_SIMPLE_ATTRIBUTE(vp_volt_debug_fops, vp_volt_debug_get, NULL, "%llu\n");
177 DEFINE_SIMPLE_ATTRIBUTE(nom_volt_debug_fops, nom_volt_debug_get, NULL,
178 "%llu\n");
179 static void vp_latch_vsel(struct omap_vdd_info *vdd)
181 u32 vpconfig;
182 unsigned long uvdc;
183 char vsel;
185 uvdc = omap_voltage_get_nom_volt(&vdd->voltdm);
186 if (!uvdc) {
187 pr_warning("%s: unable to find current voltage for vdd_%s\n",
188 __func__, vdd->voltdm.name);
189 return;
192 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
193 pr_warning("%s: PMIC function to convert voltage in uV to"
194 " vsel not registered\n", __func__);
195 return;
198 vsel = vdd->pmic_info->uv_to_vsel(uvdc);
200 vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
201 vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvoltage_mask |
202 vdd->vp_data->vp_common->vpconfig_initvdd);
203 vpconfig |= vsel << vdd->vp_data->vp_common->vpconfig_initvoltage_shift;
205 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
207 /* Trigger initVDD value copy to voltage processor */
208 vdd->write_reg((vpconfig | vdd->vp_data->vp_common->vpconfig_initvdd),
209 prm_mod_offs, vdd->vp_data->vpconfig);
211 /* Clear initVDD copy trigger bit */
212 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
215 /* Generic voltage init functions */
216 static void __init vp_init(struct omap_vdd_info *vdd)
218 u32 vp_val;
220 if (!vdd->read_reg || !vdd->write_reg) {
221 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
222 __func__, vdd->voltdm.name);
223 return;
226 vp_val = vdd->vp_rt_data.vpconfig_erroroffset |
227 (vdd->vp_rt_data.vpconfig_errorgain <<
228 vdd->vp_data->vp_common->vpconfig_errorgain_shift) |
229 vdd->vp_data->vp_common->vpconfig_timeouten;
230 vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vpconfig);
232 vp_val = ((vdd->vp_rt_data.vstepmin_smpswaittimemin <<
233 vdd->vp_data->vp_common->vstepmin_smpswaittimemin_shift) |
234 (vdd->vp_rt_data.vstepmin_stepmin <<
235 vdd->vp_data->vp_common->vstepmin_stepmin_shift));
236 vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vstepmin);
238 vp_val = ((vdd->vp_rt_data.vstepmax_smpswaittimemax <<
239 vdd->vp_data->vp_common->vstepmax_smpswaittimemax_shift) |
240 (vdd->vp_rt_data.vstepmax_stepmax <<
241 vdd->vp_data->vp_common->vstepmax_stepmax_shift));
242 vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vstepmax);
244 vp_val = ((vdd->vp_rt_data.vlimitto_vddmax <<
245 vdd->vp_data->vp_common->vlimitto_vddmax_shift) |
246 (vdd->vp_rt_data.vlimitto_vddmin <<
247 vdd->vp_data->vp_common->vlimitto_vddmin_shift) |
248 (vdd->vp_rt_data.vlimitto_timeout <<
249 vdd->vp_data->vp_common->vlimitto_timeout_shift));
250 vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vlimitto);
253 static void __init vdd_debugfs_init(struct omap_vdd_info *vdd)
255 char *name;
257 name = kzalloc(VOLTAGE_DIR_SIZE, GFP_KERNEL);
258 if (!name) {
259 pr_warning("%s: Unable to allocate memory for debugfs"
260 " directory name for vdd_%s",
261 __func__, vdd->voltdm.name);
262 return;
264 strcpy(name, "vdd_");
265 strcat(name, vdd->voltdm.name);
267 vdd->debug_dir = debugfs_create_dir(name, voltage_dir);
268 kfree(name);
269 if (IS_ERR(vdd->debug_dir)) {
270 pr_warning("%s: Unable to create debugfs directory for"
271 " vdd_%s\n", __func__, vdd->voltdm.name);
272 vdd->debug_dir = NULL;
273 return;
276 (void) debugfs_create_x16("vp_errorgain", S_IRUGO, vdd->debug_dir,
277 &(vdd->vp_rt_data.vpconfig_errorgain));
278 (void) debugfs_create_x16("vp_smpswaittimemin", S_IRUGO,
279 vdd->debug_dir,
280 &(vdd->vp_rt_data.vstepmin_smpswaittimemin));
281 (void) debugfs_create_x8("vp_stepmin", S_IRUGO, vdd->debug_dir,
282 &(vdd->vp_rt_data.vstepmin_stepmin));
283 (void) debugfs_create_x16("vp_smpswaittimemax", S_IRUGO,
284 vdd->debug_dir,
285 &(vdd->vp_rt_data.vstepmax_smpswaittimemax));
286 (void) debugfs_create_x8("vp_stepmax", S_IRUGO, vdd->debug_dir,
287 &(vdd->vp_rt_data.vstepmax_stepmax));
288 (void) debugfs_create_x8("vp_vddmax", S_IRUGO, vdd->debug_dir,
289 &(vdd->vp_rt_data.vlimitto_vddmax));
290 (void) debugfs_create_x8("vp_vddmin", S_IRUGO, vdd->debug_dir,
291 &(vdd->vp_rt_data.vlimitto_vddmin));
292 (void) debugfs_create_x16("vp_timeout", S_IRUGO, vdd->debug_dir,
293 &(vdd->vp_rt_data.vlimitto_timeout));
294 (void) debugfs_create_file("curr_vp_volt", S_IRUGO, vdd->debug_dir,
295 (void *) vdd, &vp_volt_debug_fops);
296 (void) debugfs_create_file("curr_nominal_volt", S_IRUGO,
297 vdd->debug_dir, (void *) vdd,
298 &nom_volt_debug_fops);
301 /* Voltage scale and accessory APIs */
302 static int _pre_volt_scale(struct omap_vdd_info *vdd,
303 unsigned long target_volt, u8 *target_vsel, u8 *current_vsel)
305 struct omap_volt_data *volt_data;
306 const struct omap_vc_common_data *vc_common;
307 const struct omap_vp_common_data *vp_common;
308 u32 vc_cmdval, vp_errgain_val;
310 vc_common = vdd->vc_data->vc_common;
311 vp_common = vdd->vp_data->vp_common;
313 /* Check if suffiecient pmic info is available for this vdd */
314 if (!vdd->pmic_info) {
315 pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
316 __func__, vdd->voltdm.name);
317 return -EINVAL;
320 if (!vdd->pmic_info->uv_to_vsel) {
321 pr_err("%s: PMIC function to convert voltage in uV to"
322 "vsel not registered. Hence unable to scale voltage"
323 "for vdd_%s\n", __func__, vdd->voltdm.name);
324 return -ENODATA;
327 if (!vdd->read_reg || !vdd->write_reg) {
328 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
329 __func__, vdd->voltdm.name);
330 return -EINVAL;
333 /* Get volt_data corresponding to target_volt */
334 volt_data = omap_voltage_get_voltdata(&vdd->voltdm, target_volt);
335 if (IS_ERR(volt_data))
336 volt_data = NULL;
338 *target_vsel = vdd->pmic_info->uv_to_vsel(target_volt);
339 *current_vsel = vdd->read_reg(prm_mod_offs, vdd->vp_data->voltage);
341 /* Setting the ON voltage to the new target voltage */
342 vc_cmdval = vdd->read_reg(prm_mod_offs, vdd->vc_data->cmdval_reg);
343 vc_cmdval &= ~vc_common->cmd_on_mask;
344 vc_cmdval |= (*target_vsel << vc_common->cmd_on_shift);
345 vdd->write_reg(vc_cmdval, prm_mod_offs, vdd->vc_data->cmdval_reg);
347 /* Setting vp errorgain based on the voltage */
348 if (volt_data) {
349 vp_errgain_val = vdd->read_reg(prm_mod_offs,
350 vdd->vp_data->vpconfig);
351 vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
352 vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
353 vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
354 vp_common->vpconfig_errorgain_shift;
355 vdd->write_reg(vp_errgain_val, prm_mod_offs,
356 vdd->vp_data->vpconfig);
359 return 0;
362 static void _post_volt_scale(struct omap_vdd_info *vdd,
363 unsigned long target_volt, u8 target_vsel, u8 current_vsel)
365 u32 smps_steps = 0, smps_delay = 0;
367 smps_steps = abs(target_vsel - current_vsel);
368 /* SMPS slew rate / step size. 2us added as buffer. */
369 smps_delay = ((smps_steps * vdd->pmic_info->step_size) /
370 vdd->pmic_info->slew_rate) + 2;
371 udelay(smps_delay);
373 vdd->curr_volt = target_volt;
376 /* vc_bypass_scale_voltage - VC bypass method of voltage scaling */
377 static int vc_bypass_scale_voltage(struct omap_vdd_info *vdd,
378 unsigned long target_volt)
380 u32 loop_cnt = 0, retries_cnt = 0;
381 u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
382 u8 target_vsel, current_vsel;
383 int ret;
385 ret = _pre_volt_scale(vdd, target_volt, &target_vsel, &current_vsel);
386 if (ret)
387 return ret;
389 vc_valid = vdd->vc_data->vc_common->valid;
390 vc_bypass_val_reg = vdd->vc_data->vc_common->bypass_val_reg;
391 vc_bypass_value = (target_vsel << vdd->vc_data->vc_common->data_shift) |
392 (vdd->pmic_info->pmic_reg <<
393 vdd->vc_data->vc_common->regaddr_shift) |
394 (vdd->pmic_info->i2c_slave_addr <<
395 vdd->vc_data->vc_common->slaveaddr_shift);
397 vdd->write_reg(vc_bypass_value, prm_mod_offs, vc_bypass_val_reg);
398 vdd->write_reg(vc_bypass_value | vc_valid, prm_mod_offs,
399 vc_bypass_val_reg);
401 vc_bypass_value = vdd->read_reg(prm_mod_offs, vc_bypass_val_reg);
403 * Loop till the bypass command is acknowledged from the SMPS.
404 * NOTE: This is legacy code. The loop count and retry count needs
405 * to be revisited.
407 while (!(vc_bypass_value & vc_valid)) {
408 loop_cnt++;
410 if (retries_cnt > 10) {
411 pr_warning("%s: Retry count exceeded\n", __func__);
412 return -ETIMEDOUT;
415 if (loop_cnt > 50) {
416 retries_cnt++;
417 loop_cnt = 0;
418 udelay(10);
420 vc_bypass_value = vdd->read_reg(prm_mod_offs,
421 vc_bypass_val_reg);
424 _post_volt_scale(vdd, target_volt, target_vsel, current_vsel);
425 return 0;
428 /* VP force update method of voltage scaling */
429 static int vp_forceupdate_scale_voltage(struct omap_vdd_info *vdd,
430 unsigned long target_volt)
432 u32 vpconfig;
433 u8 target_vsel, current_vsel, prm_irqst_reg;
434 int ret, timeout = 0;
436 ret = _pre_volt_scale(vdd, target_volt, &target_vsel, &current_vsel);
437 if (ret)
438 return ret;
440 prm_irqst_reg = vdd->vp_data->prm_irqst_data->prm_irqst_reg;
443 * Clear all pending TransactionDone interrupt/status. Typical latency
444 * is <3us
446 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
447 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
448 prm_irqst_ocp_mod_offs, prm_irqst_reg);
449 if (!(vdd->read_reg(prm_irqst_ocp_mod_offs, prm_irqst_reg) &
450 vdd->vp_data->prm_irqst_data->tranxdone_status))
451 break;
452 udelay(1);
454 if (timeout >= VP_TRANXDONE_TIMEOUT) {
455 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
456 "Voltage change aborted", __func__, vdd->voltdm.name);
457 return -ETIMEDOUT;
460 /* Configure for VP-Force Update */
461 vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
462 vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvdd |
463 vdd->vp_data->vp_common->vpconfig_forceupdate |
464 vdd->vp_data->vp_common->vpconfig_initvoltage_mask);
465 vpconfig |= ((target_vsel <<
466 vdd->vp_data->vp_common->vpconfig_initvoltage_shift));
467 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
469 /* Trigger initVDD value copy to voltage processor */
470 vpconfig |= vdd->vp_data->vp_common->vpconfig_initvdd;
471 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
473 /* Force update of voltage */
474 vpconfig |= vdd->vp_data->vp_common->vpconfig_forceupdate;
475 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
478 * Wait for TransactionDone. Typical latency is <200us.
479 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
481 timeout = 0;
482 omap_test_timeout((vdd->read_reg(prm_irqst_ocp_mod_offs, prm_irqst_reg) &
483 vdd->vp_data->prm_irqst_data->tranxdone_status),
484 VP_TRANXDONE_TIMEOUT, timeout);
485 if (timeout >= VP_TRANXDONE_TIMEOUT)
486 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
487 "TRANXDONE never got set after the voltage update\n",
488 __func__, vdd->voltdm.name);
490 _post_volt_scale(vdd, target_volt, target_vsel, current_vsel);
493 * Disable TransactionDone interrupt , clear all status, clear
494 * control registers
496 timeout = 0;
497 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
498 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
499 prm_irqst_ocp_mod_offs, prm_irqst_reg);
500 if (!(vdd->read_reg(prm_irqst_ocp_mod_offs, prm_irqst_reg) &
501 vdd->vp_data->prm_irqst_data->tranxdone_status))
502 break;
503 udelay(1);
506 if (timeout >= VP_TRANXDONE_TIMEOUT)
507 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
508 "to clear the TRANXDONE status\n",
509 __func__, vdd->voltdm.name);
511 vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
512 /* Clear initVDD copy trigger bit */
513 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_initvdd;
514 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
515 /* Clear force bit */
516 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_forceupdate;
517 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
519 return 0;
522 static void __init omap3_vfsm_init(struct omap_vdd_info *vdd)
525 * Voltage Manager FSM parameters init
526 * XXX This data should be passed in from the board file
528 vdd->write_reg(OMAP3_CLKSETUP, prm_mod_offs, OMAP3_PRM_CLKSETUP_OFFSET);
529 vdd->write_reg(OMAP3_VOLTOFFSET, prm_mod_offs,
530 OMAP3_PRM_VOLTOFFSET_OFFSET);
531 vdd->write_reg(OMAP3_VOLTSETUP2, prm_mod_offs,
532 OMAP3_PRM_VOLTSETUP2_OFFSET);
535 static void __init omap3_vc_init(struct omap_vdd_info *vdd)
537 static bool is_initialized;
538 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
539 u32 vc_val;
541 if (is_initialized)
542 return;
544 /* Set up the on, inactive, retention and off voltage */
545 on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
546 onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
547 ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
548 off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
549 vc_val = ((on_vsel << vdd->vc_data->vc_common->cmd_on_shift) |
550 (onlp_vsel << vdd->vc_data->vc_common->cmd_onlp_shift) |
551 (ret_vsel << vdd->vc_data->vc_common->cmd_ret_shift) |
552 (off_vsel << vdd->vc_data->vc_common->cmd_off_shift));
553 vdd->write_reg(vc_val, prm_mod_offs, vdd->vc_data->cmdval_reg);
556 * Generic VC parameters init
557 * XXX This data should be abstracted out
559 vdd->write_reg(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, prm_mod_offs,
560 OMAP3_PRM_VC_CH_CONF_OFFSET);
561 vdd->write_reg(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, prm_mod_offs,
562 OMAP3_PRM_VC_I2C_CFG_OFFSET);
564 omap3_vfsm_init(vdd);
566 is_initialized = true;
570 /* OMAP4 specific voltage init functions */
571 static void __init omap4_vc_init(struct omap_vdd_info *vdd)
573 static bool is_initialized;
574 u32 vc_val;
576 if (is_initialized)
577 return;
579 /* TODO: Configure setup times and CMD_VAL values*/
582 * Generic VC parameters init
583 * XXX This data should be abstracted out
585 vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
586 OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
587 OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
588 vdd->write_reg(vc_val, prm_mod_offs, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
590 /* XXX These are magic numbers and do not belong! */
591 vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
592 vdd->write_reg(vc_val, prm_mod_offs, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
594 is_initialized = true;
597 static void __init omap_vc_init(struct omap_vdd_info *vdd)
599 u32 vc_val;
601 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
602 pr_err("%s: PMIC info requried to configure vc for"
603 "vdd_%s not populated.Hence cannot initialize vc\n",
604 __func__, vdd->voltdm.name);
605 return;
608 if (!vdd->read_reg || !vdd->write_reg) {
609 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
610 __func__, vdd->voltdm.name);
611 return;
614 /* Set up the SMPS_SA(i2c slave address in VC */
615 vc_val = vdd->read_reg(prm_mod_offs,
616 vdd->vc_data->vc_common->smps_sa_reg);
617 vc_val &= ~vdd->vc_data->smps_sa_mask;
618 vc_val |= vdd->pmic_info->i2c_slave_addr << vdd->vc_data->smps_sa_shift;
619 vdd->write_reg(vc_val, prm_mod_offs,
620 vdd->vc_data->vc_common->smps_sa_reg);
622 /* Setup the VOLRA(pmic reg addr) in VC */
623 vc_val = vdd->read_reg(prm_mod_offs,
624 vdd->vc_data->vc_common->smps_volra_reg);
625 vc_val &= ~vdd->vc_data->smps_volra_mask;
626 vc_val |= vdd->pmic_info->pmic_reg << vdd->vc_data->smps_volra_shift;
627 vdd->write_reg(vc_val, prm_mod_offs,
628 vdd->vc_data->vc_common->smps_volra_reg);
630 /* Configure the setup times */
631 vc_val = vdd->read_reg(prm_mod_offs, vdd->vfsm->voltsetup_reg);
632 vc_val &= ~vdd->vfsm->voltsetup_mask;
633 vc_val |= vdd->pmic_info->volt_setup_time <<
634 vdd->vfsm->voltsetup_shift;
635 vdd->write_reg(vc_val, prm_mod_offs, vdd->vfsm->voltsetup_reg);
637 if (cpu_is_omap34xx())
638 omap3_vc_init(vdd);
639 else if (cpu_is_omap44xx())
640 omap4_vc_init(vdd);
643 static int __init omap_vdd_data_configure(struct omap_vdd_info *vdd)
645 int ret = -EINVAL;
647 if (!vdd->pmic_info) {
648 pr_err("%s: PMIC info requried to configure vdd_%s not"
649 "populated.Hence cannot initialize vdd_%s\n",
650 __func__, vdd->voltdm.name, vdd->voltdm.name);
651 goto ovdc_out;
654 if (IS_ERR_VALUE(_config_common_vdd_data(vdd)))
655 goto ovdc_out;
657 if (cpu_is_omap34xx()) {
658 vdd->read_reg = omap3_voltage_read_reg;
659 vdd->write_reg = omap3_voltage_write_reg;
660 ret = 0;
661 } else if (cpu_is_omap44xx()) {
662 vdd->read_reg = omap4_voltage_read_reg;
663 vdd->write_reg = omap4_voltage_write_reg;
664 ret = 0;
667 ovdc_out:
668 return ret;
671 /* Public functions */
673 * omap_voltage_get_nom_volt() - Gets the current non-auto-compensated voltage
674 * @voltdm: pointer to the VDD for which current voltage info is needed
676 * API to get the current non-auto-compensated voltage for a VDD.
677 * Returns 0 in case of error else returns the current voltage for the VDD.
679 unsigned long omap_voltage_get_nom_volt(struct voltagedomain *voltdm)
681 struct omap_vdd_info *vdd;
683 if (!voltdm || IS_ERR(voltdm)) {
684 pr_warning("%s: VDD specified does not exist!\n", __func__);
685 return 0;
688 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
690 return vdd->curr_volt;
694 * omap_vp_get_curr_volt() - API to get the current vp voltage.
695 * @voltdm: pointer to the VDD.
697 * This API returns the current voltage for the specified voltage processor
699 unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
701 struct omap_vdd_info *vdd;
702 u8 curr_vsel;
704 if (!voltdm || IS_ERR(voltdm)) {
705 pr_warning("%s: VDD specified does not exist!\n", __func__);
706 return 0;
709 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
710 if (!vdd->read_reg) {
711 pr_err("%s: No read API for reading vdd_%s regs\n",
712 __func__, voltdm->name);
713 return 0;
716 curr_vsel = vdd->read_reg(prm_mod_offs, vdd->vp_data->voltage);
718 if (!vdd->pmic_info || !vdd->pmic_info->vsel_to_uv) {
719 pr_warning("%s: PMIC function to convert vsel to voltage"
720 "in uV not registerd\n", __func__);
721 return 0;
724 return vdd->pmic_info->vsel_to_uv(curr_vsel);
728 * omap_vp_enable() - API to enable a particular VP
729 * @voltdm: pointer to the VDD whose VP is to be enabled.
731 * This API enables a particular voltage processor. Needed by the smartreflex
732 * class drivers.
734 void omap_vp_enable(struct voltagedomain *voltdm)
736 struct omap_vdd_info *vdd;
737 u32 vpconfig;
739 if (!voltdm || IS_ERR(voltdm)) {
740 pr_warning("%s: VDD specified does not exist!\n", __func__);
741 return;
744 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
745 if (!vdd->read_reg || !vdd->write_reg) {
746 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
747 __func__, voltdm->name);
748 return;
751 /* If VP is already enabled, do nothing. Return */
752 if (vdd->vp_enabled)
753 return;
755 vp_latch_vsel(vdd);
757 /* Enable VP */
758 vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
759 vpconfig |= vdd->vp_data->vp_common->vpconfig_vpenable;
760 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
761 vdd->vp_enabled = true;
765 * omap_vp_disable() - API to disable a particular VP
766 * @voltdm: pointer to the VDD whose VP is to be disabled.
768 * This API disables a particular voltage processor. Needed by the smartreflex
769 * class drivers.
771 void omap_vp_disable(struct voltagedomain *voltdm)
773 struct omap_vdd_info *vdd;
774 u32 vpconfig;
775 int timeout;
777 if (!voltdm || IS_ERR(voltdm)) {
778 pr_warning("%s: VDD specified does not exist!\n", __func__);
779 return;
782 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
783 if (!vdd->read_reg || !vdd->write_reg) {
784 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
785 __func__, voltdm->name);
786 return;
789 /* If VP is already disabled, do nothing. Return */
790 if (!vdd->vp_enabled) {
791 pr_warning("%s: Trying to disable VP for vdd_%s when"
792 "it is already disabled\n", __func__, voltdm->name);
793 return;
796 /* Disable VP */
797 vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
798 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_vpenable;
799 vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
802 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
804 omap_test_timeout((vdd->read_reg(prm_mod_offs, vdd->vp_data->vstatus)),
805 VP_IDLE_TIMEOUT, timeout);
807 if (timeout >= VP_IDLE_TIMEOUT)
808 pr_warning("%s: vdd_%s idle timedout\n",
809 __func__, voltdm->name);
811 vdd->vp_enabled = false;
813 return;
817 * omap_voltage_scale_vdd() - API to scale voltage of a particular
818 * voltage domain.
819 * @voltdm: pointer to the VDD which is to be scaled.
820 * @target_volt: The target voltage of the voltage domain
822 * This API should be called by the kernel to do the voltage scaling
823 * for a particular voltage domain during dvfs or any other situation.
825 int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
826 unsigned long target_volt)
828 struct omap_vdd_info *vdd;
830 if (!voltdm || IS_ERR(voltdm)) {
831 pr_warning("%s: VDD specified does not exist!\n", __func__);
832 return -EINVAL;
835 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
837 if (!vdd->volt_scale) {
838 pr_err("%s: No voltage scale API registered for vdd_%s\n",
839 __func__, voltdm->name);
840 return -ENODATA;
843 return vdd->volt_scale(vdd, target_volt);
847 * omap_voltage_reset() - Resets the voltage of a particular voltage domain
848 * to that of the current OPP.
849 * @voltdm: pointer to the VDD whose voltage is to be reset.
851 * This API finds out the correct voltage the voltage domain is supposed
852 * to be at and resets the voltage to that level. Should be used especially
853 * while disabling any voltage compensation modules.
855 void omap_voltage_reset(struct voltagedomain *voltdm)
857 unsigned long target_uvdc;
859 if (!voltdm || IS_ERR(voltdm)) {
860 pr_warning("%s: VDD specified does not exist!\n", __func__);
861 return;
864 target_uvdc = omap_voltage_get_nom_volt(voltdm);
865 if (!target_uvdc) {
866 pr_err("%s: unable to find current voltage for vdd_%s\n",
867 __func__, voltdm->name);
868 return;
871 omap_voltage_scale_vdd(voltdm, target_uvdc);
875 * omap_voltage_get_volttable() - API to get the voltage table associated with a
876 * particular voltage domain.
877 * @voltdm: pointer to the VDD for which the voltage table is required
878 * @volt_data: the voltage table for the particular vdd which is to be
879 * populated by this API
881 * This API populates the voltage table associated with a VDD into the
882 * passed parameter pointer. Returns the count of distinct voltages
883 * supported by this vdd.
886 void omap_voltage_get_volttable(struct voltagedomain *voltdm,
887 struct omap_volt_data **volt_data)
889 struct omap_vdd_info *vdd;
891 if (!voltdm || IS_ERR(voltdm)) {
892 pr_warning("%s: VDD specified does not exist!\n", __func__);
893 return;
896 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
898 *volt_data = vdd->volt_data;
902 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
903 * particular voltage
904 * @voltdm: pointer to the VDD whose voltage table has to be searched
905 * @volt: the voltage to be searched in the voltage table
907 * This API searches through the voltage table for the required voltage
908 * domain and tries to find a matching entry for the passed voltage volt.
909 * If a matching entry is found volt_data is populated with that entry.
910 * This API searches only through the non-compensated voltages int the
911 * voltage table.
912 * Returns pointer to the voltage table entry corresponding to volt on
913 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
914 * domain or if there is no matching entry.
916 struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
917 unsigned long volt)
919 struct omap_vdd_info *vdd;
920 int i;
922 if (!voltdm || IS_ERR(voltdm)) {
923 pr_warning("%s: VDD specified does not exist!\n", __func__);
924 return ERR_PTR(-EINVAL);
927 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
929 if (!vdd->volt_data) {
930 pr_warning("%s: voltage table does not exist for vdd_%s\n",
931 __func__, voltdm->name);
932 return ERR_PTR(-ENODATA);
935 for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
936 if (vdd->volt_data[i].volt_nominal == volt)
937 return &vdd->volt_data[i];
940 pr_notice("%s: Unable to match the current voltage with the voltage"
941 "table for vdd_%s\n", __func__, voltdm->name);
943 return ERR_PTR(-ENODATA);
947 * omap_voltage_register_pmic() - API to register PMIC specific data
948 * @voltdm: pointer to the VDD for which the PMIC specific data is
949 * to be registered
950 * @pmic_info: the structure containing pmic info
952 * This API is to be called by the SOC/PMIC file to specify the
953 * pmic specific info as present in omap_volt_pmic_info structure.
955 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
956 struct omap_volt_pmic_info *pmic_info)
958 struct omap_vdd_info *vdd;
960 if (!voltdm || IS_ERR(voltdm)) {
961 pr_warning("%s: VDD specified does not exist!\n", __func__);
962 return -EINVAL;
965 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
967 vdd->pmic_info = pmic_info;
969 return 0;
973 * omap_voltage_get_dbgdir() - API to get pointer to the debugfs directory
974 * corresponding to a voltage domain.
976 * @voltdm: pointer to the VDD whose debug directory is required.
978 * This API returns pointer to the debugfs directory corresponding
979 * to the voltage domain. Should be used by drivers requiring to
980 * add any debug entry for a particular voltage domain. Returns NULL
981 * in case of error.
983 struct dentry *omap_voltage_get_dbgdir(struct voltagedomain *voltdm)
985 struct omap_vdd_info *vdd;
987 if (!voltdm || IS_ERR(voltdm)) {
988 pr_warning("%s: VDD specified does not exist!\n", __func__);
989 return NULL;
992 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
994 return vdd->debug_dir;
998 * omap_change_voltscale_method() - API to change the voltage scaling method.
999 * @voltdm: pointer to the VDD whose voltage scaling method
1000 * has to be changed.
1001 * @voltscale_method: the method to be used for voltage scaling.
1003 * This API can be used by the board files to change the method of voltage
1004 * scaling between vpforceupdate and vcbypass. The parameter values are
1005 * defined in voltage.h
1007 void omap_change_voltscale_method(struct voltagedomain *voltdm,
1008 int voltscale_method)
1010 struct omap_vdd_info *vdd;
1012 if (!voltdm || IS_ERR(voltdm)) {
1013 pr_warning("%s: VDD specified does not exist!\n", __func__);
1014 return;
1017 vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
1019 switch (voltscale_method) {
1020 case VOLTSCALE_VPFORCEUPDATE:
1021 vdd->volt_scale = vp_forceupdate_scale_voltage;
1022 return;
1023 case VOLTSCALE_VCBYPASS:
1024 vdd->volt_scale = vc_bypass_scale_voltage;
1025 return;
1026 default:
1027 pr_warning("%s: Trying to change the method of voltage scaling"
1028 "to an unsupported one!\n", __func__);
1033 * omap_voltage_domain_lookup() - API to get the voltage domain pointer
1034 * @name: Name of the voltage domain
1036 * This API looks up in the global vdd_info struct for the
1037 * existence of voltage domain <name>. If it exists, the API returns
1038 * a pointer to the voltage domain structure corresponding to the
1039 * VDD<name>. Else retuns error pointer.
1041 struct voltagedomain *omap_voltage_domain_lookup(char *name)
1043 int i;
1045 if (!vdd_info) {
1046 pr_err("%s: Voltage driver init not yet happened.Faulting!\n",
1047 __func__);
1048 return ERR_PTR(-EINVAL);
1051 if (!name) {
1052 pr_err("%s: No name to get the votage domain!\n", __func__);
1053 return ERR_PTR(-EINVAL);
1056 for (i = 0; i < nr_scalable_vdd; i++) {
1057 if (!(strcmp(name, vdd_info[i]->voltdm.name)))
1058 return &vdd_info[i]->voltdm;
1061 return ERR_PTR(-EINVAL);
1065 * omap_voltage_late_init() - Init the various voltage parameters
1067 * This API is to be called in the later stages of the
1068 * system boot to init the voltage controller and
1069 * voltage processors.
1071 int __init omap_voltage_late_init(void)
1073 int i;
1075 if (!vdd_info) {
1076 pr_err("%s: Voltage driver support not added\n",
1077 __func__);
1078 return -EINVAL;
1081 voltage_dir = debugfs_create_dir("voltage", NULL);
1082 if (IS_ERR(voltage_dir))
1083 pr_err("%s: Unable to create voltage debugfs main dir\n",
1084 __func__);
1085 for (i = 0; i < nr_scalable_vdd; i++) {
1086 if (omap_vdd_data_configure(vdd_info[i]))
1087 continue;
1088 omap_vc_init(vdd_info[i]);
1089 vp_init(vdd_info[i]);
1090 vdd_debugfs_init(vdd_info[i]);
1093 return 0;
1096 /* XXX document */
1097 int __init omap_voltage_early_init(s16 prm_mod, s16 prm_irqst_ocp_mod,
1098 struct omap_vdd_info *omap_vdd_array[],
1099 u8 omap_vdd_count)
1101 prm_mod_offs = prm_mod;
1102 prm_irqst_ocp_mod_offs = prm_irqst_ocp_mod;
1103 vdd_info = omap_vdd_array;
1104 nr_scalable_vdd = omap_vdd_count;
1105 return 0;