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
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>
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"
35 #include "prminst44xx.h"
39 #include "powerdomain.h"
44 static LIST_HEAD(voltdm_list);
46 #define VOLTAGE_DIR_SIZE 16
47 static struct dentry *voltage_dir;
49 /* Init function pointers */
50 static int vp_forceupdate_scale_voltage(struct voltagedomain *voltdm,
51 unsigned long target_volt);
53 static u32 omap3_voltage_read_reg(u16 mod, u8 offset)
55 return omap2_prm_read_mod_reg(mod, offset);
58 static void omap3_voltage_write_reg(u32 val, u16 mod, u8 offset)
60 omap2_prm_write_mod_reg(val, mod, offset);
63 static u32 omap4_voltage_read_reg(u16 mod, u8 offset)
65 return omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
69 static void omap4_voltage_write_reg(u32 val, u16 mod, u8 offset)
71 omap4_prminst_write_inst_reg(val, OMAP4430_PRM_PARTITION, mod, offset);
74 static int __init _config_common_vdd_data(struct voltagedomain *voltdm)
78 u32 sys_clk_speed, timeout_val, waittime;
79 struct omap_vdd_info *vdd = voltdm->vdd;
82 * XXX Clockfw should handle this, or this should be in a
85 if (cpu_is_omap24xx() || cpu_is_omap34xx())
86 sys_ck_name = "sys_ck";
87 else if (cpu_is_omap44xx())
88 sys_ck_name = "sys_clkin_ck";
93 * Sys clk rate is require to calculate vp timeout value and
94 * smpswaittimemin and smpswaittimemax.
96 sys_ck = clk_get(NULL, sys_ck_name);
98 pr_warning("%s: Could not get the sys clk to calculate"
99 "various vdd_%s params\n", __func__, voltdm->name);
102 sys_clk_speed = clk_get_rate(sys_ck);
104 /* Divide to avoid overflow */
105 sys_clk_speed /= 1000;
107 /* Generic voltage parameters */
108 vdd->volt_scale = vp_forceupdate_scale_voltage;
109 vdd->vp_enabled = false;
111 vdd->vp_rt_data.vpconfig_erroroffset =
112 (vdd->pmic_info->vp_erroroffset <<
113 vdd->vp_data->vp_common->vpconfig_erroroffset_shift);
115 timeout_val = (sys_clk_speed * vdd->pmic_info->vp_timeout_us) / 1000;
116 vdd->vp_rt_data.vlimitto_timeout = timeout_val;
117 vdd->vp_rt_data.vlimitto_vddmin = vdd->pmic_info->vp_vddmin;
118 vdd->vp_rt_data.vlimitto_vddmax = vdd->pmic_info->vp_vddmax;
120 waittime = ((vdd->pmic_info->step_size / vdd->pmic_info->slew_rate) *
121 sys_clk_speed) / 1000;
122 vdd->vp_rt_data.vstepmin_smpswaittimemin = waittime;
123 vdd->vp_rt_data.vstepmax_smpswaittimemax = waittime;
124 vdd->vp_rt_data.vstepmin_stepmin = vdd->pmic_info->vp_vstepmin;
125 vdd->vp_rt_data.vstepmax_stepmax = vdd->pmic_info->vp_vstepmax;
130 /* Voltage debugfs support */
131 static int vp_volt_debug_get(void *data, u64 *val)
133 struct voltagedomain *voltdm = (struct voltagedomain *)data;
134 struct omap_vdd_info *vdd = voltdm->vdd;
138 pr_warning("Wrong paramater passed\n");
142 vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
144 if (!vdd->pmic_info->vsel_to_uv) {
145 pr_warning("PMIC function to convert vsel to voltage"
146 "in uV not registerd\n");
150 *val = vdd->pmic_info->vsel_to_uv(vsel);
154 static int nom_volt_debug_get(void *data, u64 *val)
156 struct voltagedomain *voltdm = (struct voltagedomain *)data;
159 pr_warning("Wrong paramater passed\n");
163 *val = omap_voltage_get_nom_volt(voltdm);
168 DEFINE_SIMPLE_ATTRIBUTE(vp_volt_debug_fops, vp_volt_debug_get, NULL, "%llu\n");
169 DEFINE_SIMPLE_ATTRIBUTE(nom_volt_debug_fops, nom_volt_debug_get, NULL,
171 static void vp_latch_vsel(struct voltagedomain *voltdm)
176 struct omap_vdd_info *vdd = voltdm->vdd;
178 uvdc = omap_voltage_get_nom_volt(voltdm);
180 pr_warning("%s: unable to find current voltage for vdd_%s\n",
181 __func__, voltdm->name);
185 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
186 pr_warning("%s: PMIC function to convert voltage in uV to"
187 " vsel not registered\n", __func__);
191 vsel = vdd->pmic_info->uv_to_vsel(uvdc);
193 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
194 vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvoltage_mask |
195 vdd->vp_data->vp_common->vpconfig_initvdd);
196 vpconfig |= vsel << vdd->vp_data->vp_common->vpconfig_initvoltage_shift;
198 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
200 /* Trigger initVDD value copy to voltage processor */
201 vdd->write_reg((vpconfig | vdd->vp_data->vp_common->vpconfig_initvdd),
202 vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
204 /* Clear initVDD copy trigger bit */
205 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
208 /* Generic voltage init functions */
209 static void __init vp_init(struct voltagedomain *voltdm)
211 struct omap_vdd_info *vdd = voltdm->vdd;
214 if (!vdd->read_reg || !vdd->write_reg) {
215 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
216 __func__, voltdm->name);
220 vp_val = vdd->vp_rt_data.vpconfig_erroroffset |
221 (vdd->vp_rt_data.vpconfig_errorgain <<
222 vdd->vp_data->vp_common->vpconfig_errorgain_shift) |
223 vdd->vp_data->vp_common->vpconfig_timeouten;
224 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
226 vp_val = ((vdd->vp_rt_data.vstepmin_smpswaittimemin <<
227 vdd->vp_data->vp_common->vstepmin_smpswaittimemin_shift) |
228 (vdd->vp_rt_data.vstepmin_stepmin <<
229 vdd->vp_data->vp_common->vstepmin_stepmin_shift));
230 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vstepmin);
232 vp_val = ((vdd->vp_rt_data.vstepmax_smpswaittimemax <<
233 vdd->vp_data->vp_common->vstepmax_smpswaittimemax_shift) |
234 (vdd->vp_rt_data.vstepmax_stepmax <<
235 vdd->vp_data->vp_common->vstepmax_stepmax_shift));
236 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vstepmax);
238 vp_val = ((vdd->vp_rt_data.vlimitto_vddmax <<
239 vdd->vp_data->vp_common->vlimitto_vddmax_shift) |
240 (vdd->vp_rt_data.vlimitto_vddmin <<
241 vdd->vp_data->vp_common->vlimitto_vddmin_shift) |
242 (vdd->vp_rt_data.vlimitto_timeout <<
243 vdd->vp_data->vp_common->vlimitto_timeout_shift));
244 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vlimitto);
247 static void __init vdd_debugfs_init(struct voltagedomain *voltdm)
250 struct omap_vdd_info *vdd = voltdm->vdd;
252 name = kzalloc(VOLTAGE_DIR_SIZE, GFP_KERNEL);
254 pr_warning("%s: Unable to allocate memory for debugfs"
255 " directory name for vdd_%s",
256 __func__, voltdm->name);
259 strcpy(name, "vdd_");
260 strcat(name, voltdm->name);
262 vdd->debug_dir = debugfs_create_dir(name, voltage_dir);
264 if (IS_ERR(vdd->debug_dir)) {
265 pr_warning("%s: Unable to create debugfs directory for"
266 " vdd_%s\n", __func__, voltdm->name);
267 vdd->debug_dir = NULL;
271 (void) debugfs_create_x16("vp_errorgain", S_IRUGO, vdd->debug_dir,
272 &(vdd->vp_rt_data.vpconfig_errorgain));
273 (void) debugfs_create_x16("vp_smpswaittimemin", S_IRUGO,
275 &(vdd->vp_rt_data.vstepmin_smpswaittimemin));
276 (void) debugfs_create_x8("vp_stepmin", S_IRUGO, vdd->debug_dir,
277 &(vdd->vp_rt_data.vstepmin_stepmin));
278 (void) debugfs_create_x16("vp_smpswaittimemax", S_IRUGO,
280 &(vdd->vp_rt_data.vstepmax_smpswaittimemax));
281 (void) debugfs_create_x8("vp_stepmax", S_IRUGO, vdd->debug_dir,
282 &(vdd->vp_rt_data.vstepmax_stepmax));
283 (void) debugfs_create_x8("vp_vddmax", S_IRUGO, vdd->debug_dir,
284 &(vdd->vp_rt_data.vlimitto_vddmax));
285 (void) debugfs_create_x8("vp_vddmin", S_IRUGO, vdd->debug_dir,
286 &(vdd->vp_rt_data.vlimitto_vddmin));
287 (void) debugfs_create_x16("vp_timeout", S_IRUGO, vdd->debug_dir,
288 &(vdd->vp_rt_data.vlimitto_timeout));
289 (void) debugfs_create_file("curr_vp_volt", S_IRUGO, vdd->debug_dir,
290 (void *) voltdm, &vp_volt_debug_fops);
291 (void) debugfs_create_file("curr_nominal_volt", S_IRUGO,
292 vdd->debug_dir, (void *) voltdm,
293 &nom_volt_debug_fops);
296 /* Voltage scale and accessory APIs */
297 static int _pre_volt_scale(struct voltagedomain *voltdm,
298 unsigned long target_volt, u8 *target_vsel, u8 *current_vsel)
300 struct omap_vdd_info *vdd = voltdm->vdd;
301 struct omap_volt_data *volt_data;
302 const struct omap_vc_common_data *vc_common;
303 const struct omap_vp_common_data *vp_common;
304 u32 vc_cmdval, vp_errgain_val;
306 vc_common = vdd->vc_data->vc_common;
307 vp_common = vdd->vp_data->vp_common;
309 /* Check if suffiecient pmic info is available for this vdd */
310 if (!vdd->pmic_info) {
311 pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
312 __func__, voltdm->name);
316 if (!vdd->pmic_info->uv_to_vsel) {
317 pr_err("%s: PMIC function to convert voltage in uV to"
318 "vsel not registered. Hence unable to scale voltage"
319 "for vdd_%s\n", __func__, voltdm->name);
323 if (!vdd->read_reg || !vdd->write_reg) {
324 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
325 __func__, voltdm->name);
329 /* Get volt_data corresponding to target_volt */
330 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
331 if (IS_ERR(volt_data))
334 *target_vsel = vdd->pmic_info->uv_to_vsel(target_volt);
335 *current_vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
337 /* Setting the ON voltage to the new target voltage */
338 vc_cmdval = vdd->read_reg(vdd->vc_data->vc_common->prm_mod, vdd->vc_data->cmdval_reg);
339 vc_cmdval &= ~vc_common->cmd_on_mask;
340 vc_cmdval |= (*target_vsel << vc_common->cmd_on_shift);
341 vdd->write_reg(vc_cmdval, vdd->vc_data->vc_common->prm_mod, vdd->vc_data->cmdval_reg);
343 /* Setting vp errorgain based on the voltage */
345 vp_errgain_val = vdd->read_reg(vdd->vp_data->vp_common->prm_mod,
346 vdd->vp_data->vpconfig);
347 vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
348 vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
349 vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
350 vp_common->vpconfig_errorgain_shift;
351 vdd->write_reg(vp_errgain_val, vdd->vp_data->vp_common->prm_mod,
352 vdd->vp_data->vpconfig);
358 static void _post_volt_scale(struct voltagedomain *voltdm,
359 unsigned long target_volt, u8 target_vsel, u8 current_vsel)
361 struct omap_vdd_info *vdd = voltdm->vdd;
362 u32 smps_steps = 0, smps_delay = 0;
364 smps_steps = abs(target_vsel - current_vsel);
365 /* SMPS slew rate / step size. 2us added as buffer. */
366 smps_delay = ((smps_steps * vdd->pmic_info->step_size) /
367 vdd->pmic_info->slew_rate) + 2;
370 vdd->curr_volt = target_volt;
373 /* vc_bypass_scale_voltage - VC bypass method of voltage scaling */
374 static int vc_bypass_scale_voltage(struct voltagedomain *voltdm,
375 unsigned long target_volt)
377 struct omap_vdd_info *vdd = voltdm->vdd;
378 u32 loop_cnt = 0, retries_cnt = 0;
379 u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
380 u8 target_vsel, current_vsel;
383 ret = _pre_volt_scale(voltdm, target_volt, &target_vsel, ¤t_vsel);
387 vc_valid = vdd->vc_data->vc_common->valid;
388 vc_bypass_val_reg = vdd->vc_data->vc_common->bypass_val_reg;
389 vc_bypass_value = (target_vsel << vdd->vc_data->vc_common->data_shift) |
390 (vdd->pmic_info->pmic_reg <<
391 vdd->vc_data->vc_common->regaddr_shift) |
392 (vdd->pmic_info->i2c_slave_addr <<
393 vdd->vc_data->vc_common->slaveaddr_shift);
395 vdd->write_reg(vc_bypass_value, vdd->vc_data->vc_common->prm_mod, vc_bypass_val_reg);
396 vdd->write_reg(vc_bypass_value | vc_valid, vdd->vc_data->vc_common->prm_mod,
399 vc_bypass_value = vdd->read_reg(vdd->vc_data->vc_common->prm_mod, vc_bypass_val_reg);
401 * Loop till the bypass command is acknowledged from the SMPS.
402 * NOTE: This is legacy code. The loop count and retry count needs
405 while (!(vc_bypass_value & vc_valid)) {
408 if (retries_cnt > 10) {
409 pr_warning("%s: Retry count exceeded\n", __func__);
418 vc_bypass_value = vdd->read_reg(vdd->vc_data->vc_common->prm_mod,
422 _post_volt_scale(voltdm, target_volt, target_vsel, current_vsel);
426 /* VP force update method of voltage scaling */
427 static int vp_forceupdate_scale_voltage(struct voltagedomain *voltdm,
428 unsigned long target_volt)
430 struct omap_vdd_info *vdd = voltdm->vdd;
432 u8 target_vsel, current_vsel;
433 int ret, timeout = 0;
435 ret = _pre_volt_scale(voltdm, target_volt, &target_vsel, ¤t_vsel);
440 * Clear all pending TransactionDone interrupt/status. Typical latency
443 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
444 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
445 vdd->prm_irqst_mod, vdd->prm_irqst_reg);
446 if (!(vdd->read_reg(vdd->prm_irqst_mod, vdd->prm_irqst_reg) &
447 vdd->vp_data->prm_irqst_data->tranxdone_status))
451 if (timeout >= VP_TRANXDONE_TIMEOUT) {
452 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
453 "Voltage change aborted", __func__, voltdm->name);
457 /* Configure for VP-Force Update */
458 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
459 vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvdd |
460 vdd->vp_data->vp_common->vpconfig_forceupdate |
461 vdd->vp_data->vp_common->vpconfig_initvoltage_mask);
462 vpconfig |= ((target_vsel <<
463 vdd->vp_data->vp_common->vpconfig_initvoltage_shift));
464 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
466 /* Trigger initVDD value copy to voltage processor */
467 vpconfig |= vdd->vp_data->vp_common->vpconfig_initvdd;
468 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
470 /* Force update of voltage */
471 vpconfig |= vdd->vp_data->vp_common->vpconfig_forceupdate;
472 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
475 * Wait for TransactionDone. Typical latency is <200us.
476 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
479 omap_test_timeout((vdd->read_reg(vdd->prm_irqst_mod,
480 vdd->prm_irqst_reg) &
481 vdd->vp_data->prm_irqst_data->tranxdone_status),
482 VP_TRANXDONE_TIMEOUT, timeout);
483 if (timeout >= VP_TRANXDONE_TIMEOUT)
484 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
485 "TRANXDONE never got set after the voltage update\n",
486 __func__, voltdm->name);
488 _post_volt_scale(voltdm, target_volt, target_vsel, current_vsel);
491 * Disable TransactionDone interrupt , clear all status, clear
495 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
496 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
497 vdd->prm_irqst_mod, vdd->prm_irqst_reg);
498 if (!(vdd->read_reg(vdd->prm_irqst_mod, vdd->prm_irqst_reg) &
499 vdd->vp_data->prm_irqst_data->tranxdone_status))
504 if (timeout >= VP_TRANXDONE_TIMEOUT)
505 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
506 "to clear the TRANXDONE status\n",
507 __func__, voltdm->name);
509 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
510 /* Clear initVDD copy trigger bit */
511 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_initvdd;
512 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
513 /* Clear force bit */
514 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_forceupdate;
515 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
520 static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
522 struct omap_vdd_info *vdd = voltdm->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, vdd->vc_data->vc_common->prm_mod, OMAP3_PRM_CLKSETUP_OFFSET);
529 vdd->write_reg(OMAP3_VOLTOFFSET, vdd->vc_data->vc_common->prm_mod,
530 OMAP3_PRM_VOLTOFFSET_OFFSET);
531 vdd->write_reg(OMAP3_VOLTSETUP2, vdd->vc_data->vc_common->prm_mod,
532 OMAP3_PRM_VOLTSETUP2_OFFSET);
535 static void __init omap3_vc_init(struct voltagedomain *voltdm)
537 struct omap_vdd_info *vdd = voltdm->vdd;
538 static bool is_initialized;
539 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
545 /* Set up the on, inactive, retention and off voltage */
546 on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
547 onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
548 ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
549 off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
550 vc_val = ((on_vsel << vdd->vc_data->vc_common->cmd_on_shift) |
551 (onlp_vsel << vdd->vc_data->vc_common->cmd_onlp_shift) |
552 (ret_vsel << vdd->vc_data->vc_common->cmd_ret_shift) |
553 (off_vsel << vdd->vc_data->vc_common->cmd_off_shift));
554 vdd->write_reg(vc_val, vdd->vc_data->vc_common->prm_mod, vdd->vc_data->cmdval_reg);
557 * Generic VC parameters init
558 * XXX This data should be abstracted out
560 vdd->write_reg(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, vdd->vc_data->vc_common->prm_mod,
561 OMAP3_PRM_VC_CH_CONF_OFFSET);
562 vdd->write_reg(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, vdd->vc_data->vc_common->prm_mod,
563 OMAP3_PRM_VC_I2C_CFG_OFFSET);
565 omap3_vfsm_init(voltdm);
567 is_initialized = true;
571 /* OMAP4 specific voltage init functions */
572 static void __init omap4_vc_init(struct voltagedomain *voltdm)
574 struct omap_vdd_info *vdd = voltdm->vdd;
575 static bool is_initialized;
581 /* TODO: Configure setup times and CMD_VAL values*/
584 * Generic VC parameters init
585 * XXX This data should be abstracted out
587 vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
588 OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
589 OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
590 vdd->write_reg(vc_val, vdd->vc_data->vc_common->prm_mod, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
592 /* XXX These are magic numbers and do not belong! */
593 vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
594 vdd->write_reg(vc_val, vdd->vc_data->vc_common->prm_mod, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
596 is_initialized = true;
599 static void __init omap_vc_init(struct voltagedomain *voltdm)
601 struct omap_vdd_info *vdd = voltdm->vdd;
604 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
605 pr_err("%s: PMIC info requried to configure vc for"
606 "vdd_%s not populated.Hence cannot initialize vc\n",
607 __func__, voltdm->name);
611 if (!vdd->read_reg || !vdd->write_reg) {
612 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
613 __func__, voltdm->name);
617 /* Set up the SMPS_SA(i2c slave address in VC */
618 vc_val = vdd->read_reg(vdd->vc_data->vc_common->prm_mod,
619 vdd->vc_data->vc_common->smps_sa_reg);
620 vc_val &= ~vdd->vc_data->smps_sa_mask;
621 vc_val |= vdd->pmic_info->i2c_slave_addr << vdd->vc_data->smps_sa_shift;
622 vdd->write_reg(vc_val, vdd->vc_data->vc_common->prm_mod,
623 vdd->vc_data->vc_common->smps_sa_reg);
625 /* Setup the VOLRA(pmic reg addr) in VC */
626 vc_val = vdd->read_reg(vdd->vc_data->vc_common->prm_mod,
627 vdd->vc_data->vc_common->smps_volra_reg);
628 vc_val &= ~vdd->vc_data->smps_volra_mask;
629 vc_val |= vdd->pmic_info->pmic_reg << vdd->vc_data->smps_volra_shift;
630 vdd->write_reg(vc_val, vdd->vc_data->vc_common->prm_mod,
631 vdd->vc_data->vc_common->smps_volra_reg);
633 /* Configure the setup times */
634 vc_val = vdd->read_reg(vdd->vc_data->vc_common->prm_mod, vdd->vfsm->voltsetup_reg);
635 vc_val &= ~vdd->vfsm->voltsetup_mask;
636 vc_val |= vdd->pmic_info->volt_setup_time <<
637 vdd->vfsm->voltsetup_shift;
638 vdd->write_reg(vc_val, vdd->vc_data->vc_common->prm_mod, vdd->vfsm->voltsetup_reg);
640 if (cpu_is_omap34xx())
641 omap3_vc_init(voltdm);
642 else if (cpu_is_omap44xx())
643 omap4_vc_init(voltdm);
646 static int __init omap_vdd_data_configure(struct voltagedomain *voltdm)
648 struct omap_vdd_info *vdd = voltdm->vdd;
651 if (!vdd->pmic_info) {
652 pr_err("%s: PMIC info requried to configure vdd_%s not"
653 "populated.Hence cannot initialize vdd_%s\n",
654 __func__, voltdm->name, voltdm->name);
658 if (IS_ERR_VALUE(_config_common_vdd_data(voltdm)))
661 if (cpu_is_omap34xx()) {
662 vdd->read_reg = omap3_voltage_read_reg;
663 vdd->write_reg = omap3_voltage_write_reg;
665 } else if (cpu_is_omap44xx()) {
666 vdd->read_reg = omap4_voltage_read_reg;
667 vdd->write_reg = omap4_voltage_write_reg;
675 /* Public functions */
677 * omap_voltage_get_nom_volt() - Gets the current non-auto-compensated voltage
678 * @voltdm: pointer to the VDD for which current voltage info is needed
680 * API to get the current non-auto-compensated voltage for a VDD.
681 * Returns 0 in case of error else returns the current voltage for the VDD.
683 unsigned long omap_voltage_get_nom_volt(struct voltagedomain *voltdm)
685 struct omap_vdd_info *vdd;
687 if (!voltdm || IS_ERR(voltdm)) {
688 pr_warning("%s: VDD specified does not exist!\n", __func__);
694 return vdd->curr_volt;
698 * omap_vp_get_curr_volt() - API to get the current vp voltage.
699 * @voltdm: pointer to the VDD.
701 * This API returns the current voltage for the specified voltage processor
703 unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
705 struct omap_vdd_info *vdd;
708 if (!voltdm || IS_ERR(voltdm)) {
709 pr_warning("%s: VDD specified does not exist!\n", __func__);
714 if (!vdd->read_reg) {
715 pr_err("%s: No read API for reading vdd_%s regs\n",
716 __func__, voltdm->name);
720 curr_vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
722 if (!vdd->pmic_info || !vdd->pmic_info->vsel_to_uv) {
723 pr_warning("%s: PMIC function to convert vsel to voltage"
724 "in uV not registerd\n", __func__);
728 return vdd->pmic_info->vsel_to_uv(curr_vsel);
732 * omap_vp_enable() - API to enable a particular VP
733 * @voltdm: pointer to the VDD whose VP is to be enabled.
735 * This API enables a particular voltage processor. Needed by the smartreflex
738 void omap_vp_enable(struct voltagedomain *voltdm)
740 struct omap_vdd_info *vdd;
743 if (!voltdm || IS_ERR(voltdm)) {
744 pr_warning("%s: VDD specified does not exist!\n", __func__);
749 if (!vdd->read_reg || !vdd->write_reg) {
750 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
751 __func__, voltdm->name);
755 /* If VP is already enabled, do nothing. Return */
759 vp_latch_vsel(voltdm);
762 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
763 vpconfig |= vdd->vp_data->vp_common->vpconfig_vpenable;
764 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
765 vdd->vp_enabled = true;
769 * omap_vp_disable() - API to disable a particular VP
770 * @voltdm: pointer to the VDD whose VP is to be disabled.
772 * This API disables a particular voltage processor. Needed by the smartreflex
775 void omap_vp_disable(struct voltagedomain *voltdm)
777 struct omap_vdd_info *vdd;
781 if (!voltdm || IS_ERR(voltdm)) {
782 pr_warning("%s: VDD specified does not exist!\n", __func__);
787 if (!vdd->read_reg || !vdd->write_reg) {
788 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
789 __func__, voltdm->name);
793 /* If VP is already disabled, do nothing. Return */
794 if (!vdd->vp_enabled) {
795 pr_warning("%s: Trying to disable VP for vdd_%s when"
796 "it is already disabled\n", __func__, voltdm->name);
801 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
802 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_vpenable;
803 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
806 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
808 omap_test_timeout((vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vstatus)),
809 VP_IDLE_TIMEOUT, timeout);
811 if (timeout >= VP_IDLE_TIMEOUT)
812 pr_warning("%s: vdd_%s idle timedout\n",
813 __func__, voltdm->name);
815 vdd->vp_enabled = false;
821 * omap_voltage_scale_vdd() - API to scale voltage of a particular
823 * @voltdm: pointer to the VDD which is to be scaled.
824 * @target_volt: The target voltage of the voltage domain
826 * This API should be called by the kernel to do the voltage scaling
827 * for a particular voltage domain during dvfs or any other situation.
829 int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
830 unsigned long target_volt)
832 struct omap_vdd_info *vdd;
834 if (!voltdm || IS_ERR(voltdm)) {
835 pr_warning("%s: VDD specified does not exist!\n", __func__);
841 if (!vdd->volt_scale) {
842 pr_err("%s: No voltage scale API registered for vdd_%s\n",
843 __func__, voltdm->name);
847 return vdd->volt_scale(voltdm, target_volt);
851 * omap_voltage_reset() - Resets the voltage of a particular voltage domain
852 * to that of the current OPP.
853 * @voltdm: pointer to the VDD whose voltage is to be reset.
855 * This API finds out the correct voltage the voltage domain is supposed
856 * to be at and resets the voltage to that level. Should be used especially
857 * while disabling any voltage compensation modules.
859 void omap_voltage_reset(struct voltagedomain *voltdm)
861 unsigned long target_uvdc;
863 if (!voltdm || IS_ERR(voltdm)) {
864 pr_warning("%s: VDD specified does not exist!\n", __func__);
868 target_uvdc = omap_voltage_get_nom_volt(voltdm);
870 pr_err("%s: unable to find current voltage for vdd_%s\n",
871 __func__, voltdm->name);
875 omap_voltage_scale_vdd(voltdm, target_uvdc);
879 * omap_voltage_get_volttable() - API to get the voltage table associated with a
880 * particular voltage domain.
881 * @voltdm: pointer to the VDD for which the voltage table is required
882 * @volt_data: the voltage table for the particular vdd which is to be
883 * populated by this API
885 * This API populates the voltage table associated with a VDD into the
886 * passed parameter pointer. Returns the count of distinct voltages
887 * supported by this vdd.
890 void omap_voltage_get_volttable(struct voltagedomain *voltdm,
891 struct omap_volt_data **volt_data)
893 struct omap_vdd_info *vdd;
895 if (!voltdm || IS_ERR(voltdm)) {
896 pr_warning("%s: VDD specified does not exist!\n", __func__);
902 *volt_data = vdd->volt_data;
906 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
908 * @voltdm: pointer to the VDD whose voltage table has to be searched
909 * @volt: the voltage to be searched in the voltage table
911 * This API searches through the voltage table for the required voltage
912 * domain and tries to find a matching entry for the passed voltage volt.
913 * If a matching entry is found volt_data is populated with that entry.
914 * This API searches only through the non-compensated voltages int the
916 * Returns pointer to the voltage table entry corresponding to volt on
917 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
918 * domain or if there is no matching entry.
920 struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
923 struct omap_vdd_info *vdd;
926 if (!voltdm || IS_ERR(voltdm)) {
927 pr_warning("%s: VDD specified does not exist!\n", __func__);
928 return ERR_PTR(-EINVAL);
933 if (!vdd->volt_data) {
934 pr_warning("%s: voltage table does not exist for vdd_%s\n",
935 __func__, voltdm->name);
936 return ERR_PTR(-ENODATA);
939 for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
940 if (vdd->volt_data[i].volt_nominal == volt)
941 return &vdd->volt_data[i];
944 pr_notice("%s: Unable to match the current voltage with the voltage"
945 "table for vdd_%s\n", __func__, voltdm->name);
947 return ERR_PTR(-ENODATA);
951 * omap_voltage_register_pmic() - API to register PMIC specific data
952 * @voltdm: pointer to the VDD for which the PMIC specific data is
954 * @pmic_info: the structure containing pmic info
956 * This API is to be called by the SOC/PMIC file to specify the
957 * pmic specific info as present in omap_volt_pmic_info structure.
959 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
960 struct omap_volt_pmic_info *pmic_info)
962 struct omap_vdd_info *vdd;
964 if (!voltdm || IS_ERR(voltdm)) {
965 pr_warning("%s: VDD specified does not exist!\n", __func__);
971 vdd->pmic_info = pmic_info;
977 * omap_voltage_get_dbgdir() - API to get pointer to the debugfs directory
978 * corresponding to a voltage domain.
980 * @voltdm: pointer to the VDD whose debug directory is required.
982 * This API returns pointer to the debugfs directory corresponding
983 * to the voltage domain. Should be used by drivers requiring to
984 * add any debug entry for a particular voltage domain. Returns NULL
987 struct dentry *omap_voltage_get_dbgdir(struct voltagedomain *voltdm)
989 struct omap_vdd_info *vdd;
991 if (!voltdm || IS_ERR(voltdm)) {
992 pr_warning("%s: VDD specified does not exist!\n", __func__);
998 return vdd->debug_dir;
1002 * omap_change_voltscale_method() - API to change the voltage scaling method.
1003 * @voltdm: pointer to the VDD whose voltage scaling method
1004 * has to be changed.
1005 * @voltscale_method: the method to be used for voltage scaling.
1007 * This API can be used by the board files to change the method of voltage
1008 * scaling between vpforceupdate and vcbypass. The parameter values are
1009 * defined in voltage.h
1011 void omap_change_voltscale_method(struct voltagedomain *voltdm,
1012 int voltscale_method)
1014 struct omap_vdd_info *vdd;
1016 if (!voltdm || IS_ERR(voltdm)) {
1017 pr_warning("%s: VDD specified does not exist!\n", __func__);
1023 switch (voltscale_method) {
1024 case VOLTSCALE_VPFORCEUPDATE:
1025 vdd->volt_scale = vp_forceupdate_scale_voltage;
1027 case VOLTSCALE_VCBYPASS:
1028 vdd->volt_scale = vc_bypass_scale_voltage;
1031 pr_warning("%s: Trying to change the method of voltage scaling"
1032 "to an unsupported one!\n", __func__);
1037 * omap_voltage_late_init() - Init the various voltage parameters
1039 * This API is to be called in the later stages of the
1040 * system boot to init the voltage controller and
1041 * voltage processors.
1043 int __init omap_voltage_late_init(void)
1045 struct voltagedomain *voltdm;
1047 if (list_empty(&voltdm_list)) {
1048 pr_err("%s: Voltage driver support not added\n",
1053 voltage_dir = debugfs_create_dir("voltage", NULL);
1054 if (IS_ERR(voltage_dir))
1055 pr_err("%s: Unable to create voltage debugfs main dir\n",
1057 list_for_each_entry(voltdm, &voltdm_list, node) {
1058 if (!voltdm->scalable)
1062 if (omap_vdd_data_configure(voltdm))
1064 omap_vc_init(voltdm);
1066 vdd_debugfs_init(voltdm);
1073 static struct voltagedomain *_voltdm_lookup(const char *name)
1075 struct voltagedomain *voltdm, *temp_voltdm;
1079 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
1080 if (!strcmp(name, temp_voltdm->name)) {
1081 voltdm = temp_voltdm;
1090 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
1091 * @voltdm: struct voltagedomain * to add the powerdomain to
1092 * @pwrdm: struct powerdomain * to associate with a voltagedomain
1094 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
1095 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
1096 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
1097 * or 0 upon success.
1099 int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
1101 if (!voltdm || !pwrdm)
1104 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
1105 "%s\n", pwrdm->name, voltdm->name);
1107 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
1113 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
1114 * @voltdm: struct voltagedomain * to iterate over
1115 * @fn: callback function *
1117 * Call the supplied function @fn for each powerdomain in the
1118 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
1119 * pointers; or passes along the last return value of the callback
1120 * function, which should be 0 for success or anything else to
1123 int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
1124 int (*fn)(struct voltagedomain *voltdm,
1125 struct powerdomain *pwrdm))
1127 struct powerdomain *pwrdm;
1133 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
1134 ret = (*fn)(voltdm, pwrdm);
1140 * voltdm_for_each - call function on each registered voltagedomain
1141 * @fn: callback function *
1143 * Call the supplied function @fn for each registered voltagedomain.
1144 * The callback function @fn can return anything but 0 to bail out
1145 * early from the iterator. Returns the last return value of the
1146 * callback function, which should be 0 for success or anything else
1147 * to indicate failure; or -EINVAL if the function pointer is null.
1149 int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
1152 struct voltagedomain *temp_voltdm;
1158 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
1159 ret = (*fn)(temp_voltdm, user);
1167 static int _voltdm_register(struct voltagedomain *voltdm)
1169 if (!voltdm || !voltdm->name)
1172 INIT_LIST_HEAD(&voltdm->pwrdm_list);
1173 list_add(&voltdm->node, &voltdm_list);
1175 pr_debug("voltagedomain: registered %s\n", voltdm->name);
1181 * voltdm_lookup - look up a voltagedomain by name, return a pointer
1182 * @name: name of voltagedomain
1184 * Find a registered voltagedomain by its name @name. Returns a pointer
1185 * to the struct voltagedomain if found, or NULL otherwise.
1187 struct voltagedomain *voltdm_lookup(const char *name)
1189 struct voltagedomain *voltdm ;
1194 voltdm = _voltdm_lookup(name);
1200 * voltdm_init - set up the voltagedomain layer
1201 * @voltdm_list: array of struct voltagedomain pointers to register
1203 * Loop through the array of voltagedomains @voltdm_list, registering all
1204 * that are available on the current CPU. If voltdm_list is supplied
1205 * and not null, all of the referenced voltagedomains will be
1206 * registered. No return value.
1208 void voltdm_init(struct voltagedomain **voltdms)
1210 struct voltagedomain **v;
1213 for (v = voltdms; *v; v++)
1214 _voltdm_register(*v);