Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OMAP Voltage Controller (VC) interface |
| 3 | * |
| 4 | * Copyright (C) 2011 Texas Instruments, Inc. |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public |
| 7 | * License version 2. This program is licensed "as is" without any |
| 8 | * warranty of any kind, whether express or implied. |
| 9 | */ |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/init.h> |
| 13 | |
| 14 | #include <plat/cpu.h> |
| 15 | |
| 16 | #include "voltage.h" |
| 17 | #include "vc.h" |
| 18 | #include "prm-regbits-34xx.h" |
| 19 | #include "prm-regbits-44xx.h" |
| 20 | #include "prm44xx.h" |
| 21 | |
| 22 | /* Voltage scale and accessory APIs */ |
| 23 | int omap_vc_pre_scale(struct voltagedomain *voltdm, |
| 24 | unsigned long target_volt, |
| 25 | u8 *target_vsel, u8 *current_vsel) |
| 26 | { |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 27 | struct omap_vc_channel *vc = voltdm->vc; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 28 | struct omap_vdd_info *vdd = voltdm->vdd; |
| 29 | struct omap_volt_data *volt_data; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 30 | const struct omap_vp_common_data *vp_common; |
| 31 | u32 vc_cmdval, vp_errgain_val; |
| 32 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 33 | vp_common = vdd->vp_data->vp_common; |
| 34 | |
| 35 | /* Check if sufficient pmic info is available for this vdd */ |
| 36 | if (!vdd->pmic_info) { |
| 37 | pr_err("%s: Insufficient pmic info to scale the vdd_%s\n", |
| 38 | __func__, voltdm->name); |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | |
| 42 | if (!vdd->pmic_info->uv_to_vsel) { |
| 43 | pr_err("%s: PMIC function to convert voltage in uV to" |
| 44 | "vsel not registered. Hence unable to scale voltage" |
| 45 | "for vdd_%s\n", __func__, voltdm->name); |
| 46 | return -ENODATA; |
| 47 | } |
| 48 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 49 | if (!voltdm->read || !voltdm->write) { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 50 | pr_err("%s: No read/write API for accessing vdd_%s regs\n", |
| 51 | __func__, voltdm->name); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | /* Get volt_data corresponding to target_volt */ |
| 56 | volt_data = omap_voltage_get_voltdata(voltdm, target_volt); |
| 57 | if (IS_ERR(volt_data)) |
| 58 | volt_data = NULL; |
| 59 | |
| 60 | *target_vsel = vdd->pmic_info->uv_to_vsel(target_volt); |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 61 | *current_vsel = voltdm->read(vdd->vp_data->voltage); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 62 | |
| 63 | /* Setting the ON voltage to the new target voltage */ |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 64 | vc_cmdval = voltdm->read(vc->cmdval_reg); |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 65 | vc_cmdval &= ~vc->common->cmd_on_mask; |
| 66 | vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift); |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 67 | voltdm->write(vc_cmdval, vc->cmdval_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 68 | |
| 69 | /* Setting vp errorgain based on the voltage */ |
| 70 | if (volt_data) { |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 71 | vp_errgain_val = voltdm->read(vdd->vp_data->vpconfig); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 72 | vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain; |
| 73 | vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask; |
| 74 | vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain << |
| 75 | vp_common->vpconfig_errorgain_shift; |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 76 | voltdm->write(vp_errgain_val, vdd->vp_data->vpconfig); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | void omap_vc_post_scale(struct voltagedomain *voltdm, |
| 83 | unsigned long target_volt, |
| 84 | u8 target_vsel, u8 current_vsel) |
| 85 | { |
| 86 | struct omap_vdd_info *vdd = voltdm->vdd; |
| 87 | u32 smps_steps = 0, smps_delay = 0; |
| 88 | |
| 89 | smps_steps = abs(target_vsel - current_vsel); |
| 90 | /* SMPS slew rate / step size. 2us added as buffer. */ |
| 91 | smps_delay = ((smps_steps * vdd->pmic_info->step_size) / |
| 92 | vdd->pmic_info->slew_rate) + 2; |
| 93 | udelay(smps_delay); |
| 94 | |
| 95 | vdd->curr_volt = target_volt; |
| 96 | } |
| 97 | |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 98 | /* vc_bypass_scale - VC bypass method of voltage scaling */ |
| 99 | int omap_vc_bypass_scale(struct voltagedomain *voltdm, |
| 100 | unsigned long target_volt) |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 101 | { |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 102 | struct omap_vc_channel *vc = voltdm->vc; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 103 | u32 loop_cnt = 0, retries_cnt = 0; |
| 104 | u32 vc_valid, vc_bypass_val_reg, vc_bypass_value; |
| 105 | u8 target_vsel, current_vsel; |
| 106 | int ret; |
| 107 | |
| 108 | ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, ¤t_vsel); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 112 | vc_valid = vc->common->valid; |
| 113 | vc_bypass_val_reg = vc->common->bypass_val_reg; |
| 114 | vc_bypass_value = (target_vsel << vc->common->data_shift) | |
Kevin Hilman | 78614e0 | 2011-03-29 14:24:47 -0700 | [diff] [blame] | 115 | (vc->volt_reg_addr << vc->common->regaddr_shift) | |
| 116 | (vc->i2c_slave_addr << vc->common->slaveaddr_shift); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 117 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 118 | voltdm->write(vc_bypass_value, vc_bypass_val_reg); |
| 119 | voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 120 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 121 | vc_bypass_value = voltdm->read(vc_bypass_val_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Loop till the bypass command is acknowledged from the SMPS. |
| 124 | * NOTE: This is legacy code. The loop count and retry count needs |
| 125 | * to be revisited. |
| 126 | */ |
| 127 | while (!(vc_bypass_value & vc_valid)) { |
| 128 | loop_cnt++; |
| 129 | |
| 130 | if (retries_cnt > 10) { |
| 131 | pr_warning("%s: Retry count exceeded\n", __func__); |
| 132 | return -ETIMEDOUT; |
| 133 | } |
| 134 | |
| 135 | if (loop_cnt > 50) { |
| 136 | retries_cnt++; |
| 137 | loop_cnt = 0; |
| 138 | udelay(10); |
| 139 | } |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 140 | vc_bypass_value = voltdm->read(vc_bypass_val_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static void __init omap3_vfsm_init(struct voltagedomain *voltdm) |
| 148 | { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Voltage Manager FSM parameters init |
| 151 | * XXX This data should be passed in from the board file |
| 152 | */ |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 153 | voltdm->write(OMAP3_CLKSETUP, OMAP3_PRM_CLKSETUP_OFFSET); |
| 154 | voltdm->write(OMAP3_VOLTOFFSET, OMAP3_PRM_VOLTOFFSET_OFFSET); |
| 155 | voltdm->write(OMAP3_VOLTSETUP2, OMAP3_PRM_VOLTSETUP2_OFFSET); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void __init omap3_vc_init_channel(struct voltagedomain *voltdm) |
| 159 | { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 160 | static bool is_initialized; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 161 | |
| 162 | if (is_initialized) |
| 163 | return; |
| 164 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Generic VC parameters init |
| 167 | * XXX This data should be abstracted out |
| 168 | */ |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 169 | voltdm->write(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, |
| 170 | OMAP3_PRM_VC_CH_CONF_OFFSET); |
| 171 | voltdm->write(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, |
| 172 | OMAP3_PRM_VC_I2C_CFG_OFFSET); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 173 | |
| 174 | omap3_vfsm_init(voltdm); |
| 175 | |
| 176 | is_initialized = true; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* OMAP4 specific voltage init functions */ |
| 181 | static void __init omap4_vc_init_channel(struct voltagedomain *voltdm) |
| 182 | { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 183 | static bool is_initialized; |
| 184 | u32 vc_val; |
| 185 | |
| 186 | if (is_initialized) |
| 187 | return; |
| 188 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Generic VC parameters init |
| 191 | * XXX This data should be abstracted out |
| 192 | */ |
| 193 | vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK | |
| 194 | OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK | |
| 195 | OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK); |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 196 | voltdm->write(vc_val, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 197 | |
| 198 | /* XXX These are magic numbers and do not belong! */ |
| 199 | vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT); |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 200 | voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 201 | |
| 202 | is_initialized = true; |
| 203 | } |
| 204 | |
| 205 | void __init omap_vc_init_channel(struct voltagedomain *voltdm) |
| 206 | { |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 207 | struct omap_vc_channel *vc = voltdm->vc; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 208 | struct omap_vdd_info *vdd = voltdm->vdd; |
Kevin Hilman | 08d1c9a | 2011-03-29 15:14:38 -0700 | [diff] [blame^] | 209 | u8 on_vsel, onlp_vsel, ret_vsel, off_vsel; |
| 210 | u32 val; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 211 | |
| 212 | if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) { |
| 213 | pr_err("%s: PMIC info requried to configure vc for" |
| 214 | "vdd_%s not populated.Hence cannot initialize vc\n", |
| 215 | __func__, voltdm->name); |
| 216 | return; |
| 217 | } |
| 218 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 219 | if (!voltdm->read || !voltdm->write) { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 220 | pr_err("%s: No read/write API for accessing vdd_%s regs\n", |
| 221 | __func__, voltdm->name); |
| 222 | return; |
| 223 | } |
| 224 | |
Kevin Hilman | ba112a4 | 2011-03-29 14:02:36 -0700 | [diff] [blame] | 225 | /* get PMIC/board specific settings */ |
| 226 | vc->i2c_slave_addr = vdd->pmic_info->i2c_slave_addr; |
Kevin Hilman | e4e021c | 2011-06-09 11:01:55 -0700 | [diff] [blame] | 227 | vc->volt_reg_addr = vdd->pmic_info->volt_reg_addr; |
| 228 | vc->cmd_reg_addr = vdd->pmic_info->cmd_reg_addr; |
Kevin Hilman | 5892bb1 | 2011-03-29 14:36:04 -0700 | [diff] [blame] | 229 | vc->setup_time = vdd->pmic_info->volt_setup_time; |
Kevin Hilman | ba112a4 | 2011-03-29 14:02:36 -0700 | [diff] [blame] | 230 | |
| 231 | /* Configure the i2c slave address for this VC */ |
| 232 | voltdm->rmw(vc->smps_sa_mask, |
| 233 | vc->i2c_slave_addr << __ffs(vc->smps_sa_mask), |
| 234 | vc->common->smps_sa_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 235 | |
Kevin Hilman | e4e021c | 2011-06-09 11:01:55 -0700 | [diff] [blame] | 236 | /* |
| 237 | * Configure the PMIC register addresses. |
| 238 | */ |
| 239 | voltdm->rmw(vc->smps_volra_mask, |
| 240 | vc->volt_reg_addr << __ffs(vc->smps_volra_mask), |
| 241 | vc->common->smps_volra_reg); |
| 242 | if (vc->cmd_reg_addr) |
| 243 | voltdm->rmw(vc->smps_cmdra_mask, |
| 244 | vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask), |
| 245 | vc->common->smps_cmdra_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 246 | |
Kevin Hilman | 08d1c9a | 2011-03-29 15:14:38 -0700 | [diff] [blame^] | 247 | /* Set up the on, inactive, retention and off voltage */ |
| 248 | on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt); |
| 249 | onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt); |
| 250 | ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt); |
| 251 | off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt); |
| 252 | val = ((on_vsel << vc->common->cmd_on_shift) | |
| 253 | (onlp_vsel << vc->common->cmd_onlp_shift) | |
| 254 | (ret_vsel << vc->common->cmd_ret_shift) | |
| 255 | (off_vsel << vc->common->cmd_off_shift)); |
| 256 | voltdm->write(val, vc->cmdval_reg); |
| 257 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 258 | /* Configure the setup times */ |
Kevin Hilman | 5892bb1 | 2011-03-29 14:36:04 -0700 | [diff] [blame] | 259 | voltdm->rmw(voltdm->vfsm->voltsetup_mask, |
| 260 | vc->setup_time << __ffs(voltdm->vfsm->voltsetup_mask), |
| 261 | voltdm->vfsm->voltsetup_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 262 | |
| 263 | if (cpu_is_omap34xx()) |
| 264 | omap3_vc_init_channel(voltdm); |
| 265 | else if (cpu_is_omap44xx()) |
| 266 | omap4_vc_init_channel(voltdm); |
| 267 | } |
| 268 | |