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> |
Tony Lindgren | 4647ca5 | 2012-03-08 10:20:14 -0800 | [diff] [blame] | 13 | #include <linux/bug.h> |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 14 | #include <linux/io.h> |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 15 | |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 16 | #include <asm/div64.h> |
| 17 | |
| 18 | #include "iomap.h" |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 19 | #include "soc.h" |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 20 | #include "voltage.h" |
| 21 | #include "vc.h" |
| 22 | #include "prm-regbits-34xx.h" |
| 23 | #include "prm-regbits-44xx.h" |
| 24 | #include "prm44xx.h" |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 25 | #include "pm.h" |
| 26 | #include "scrm44xx.h" |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 27 | |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 28 | /** |
| 29 | * struct omap_vc_channel_cfg - describe the cfg_channel bitfield |
| 30 | * @sa: bit for slave address |
| 31 | * @rav: bit for voltage configuration register |
| 32 | * @rac: bit for command configuration register |
| 33 | * @racen: enable bit for RAC |
| 34 | * @cmd: bit for command value set selection |
| 35 | * |
| 36 | * Channel configuration bits, common for OMAP3+ |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 37 | * OMAP3 register: PRM_VC_CH_CONF |
| 38 | * OMAP4 register: PRM_VC_CFG_CHANNEL |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 39 | * OMAP5 register: PRM_VC_SMPS_<voltdm>_CONFIG |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 40 | */ |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 41 | struct omap_vc_channel_cfg { |
| 42 | u8 sa; |
| 43 | u8 rav; |
| 44 | u8 rac; |
| 45 | u8 racen; |
| 46 | u8 cmd; |
| 47 | }; |
| 48 | |
| 49 | static struct omap_vc_channel_cfg vc_default_channel_cfg = { |
| 50 | .sa = BIT(0), |
| 51 | .rav = BIT(1), |
| 52 | .rac = BIT(2), |
| 53 | .racen = BIT(3), |
| 54 | .cmd = BIT(4), |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * On OMAP3+, all VC channels have the above default bitfield |
| 59 | * configuration, except the OMAP4 MPU channel. This appears |
| 60 | * to be a freak accident as every other VC channel has the |
| 61 | * default configuration, thus creating a mutant channel config. |
| 62 | */ |
| 63 | static struct omap_vc_channel_cfg vc_mutant_channel_cfg = { |
| 64 | .sa = BIT(0), |
| 65 | .rav = BIT(2), |
| 66 | .rac = BIT(3), |
| 67 | .racen = BIT(4), |
| 68 | .cmd = BIT(1), |
| 69 | }; |
| 70 | |
| 71 | static struct omap_vc_channel_cfg *vc_cfg_bits; |
| 72 | #define CFG_CHANNEL_MASK 0x1f |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * omap_vc_config_channel - configure VC channel to PMIC mappings |
| 76 | * @voltdm: pointer to voltagdomain defining the desired VC channel |
| 77 | * |
| 78 | * Configures the VC channel to PMIC mappings for the following |
| 79 | * PMIC settings |
| 80 | * - i2c slave address (SA) |
| 81 | * - voltage configuration address (RAV) |
| 82 | * - command configuration address (RAC) and enable bit (RACEN) |
| 83 | * - command values for ON, ONLP, RET and OFF (CMD) |
| 84 | * |
| 85 | * This function currently only allows flexible configuration of the |
| 86 | * non-default channel. Starting with OMAP4, there are more than 2 |
| 87 | * channels, with one defined as the default (on OMAP4, it's MPU.) |
| 88 | * Only the non-default channel can be configured. |
| 89 | */ |
| 90 | static int omap_vc_config_channel(struct voltagedomain *voltdm) |
| 91 | { |
| 92 | struct omap_vc_channel *vc = voltdm->vc; |
| 93 | |
| 94 | /* |
| 95 | * For default channel, the only configurable bit is RACEN. |
| 96 | * All others must stay at zero (see function comment above.) |
| 97 | */ |
| 98 | if (vc->flags & OMAP_VC_CHANNEL_DEFAULT) |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 99 | vc->cfg_channel &= vc_cfg_bits->racen; |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 100 | |
| 101 | voltdm->rmw(CFG_CHANNEL_MASK << vc->cfg_channel_sa_shift, |
| 102 | vc->cfg_channel << vc->cfg_channel_sa_shift, |
Kevin Hilman | 5876c94 | 2011-07-20 16:35:46 -0700 | [diff] [blame] | 103 | vc->cfg_channel_reg); |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 108 | /* Voltage scale and accessory APIs */ |
| 109 | int omap_vc_pre_scale(struct voltagedomain *voltdm, |
| 110 | unsigned long target_volt, |
| 111 | u8 *target_vsel, u8 *current_vsel) |
| 112 | { |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 113 | struct omap_vc_channel *vc = voltdm->vc; |
Kevin Hilman | 76ea742 | 2011-04-05 15:15:31 -0700 | [diff] [blame] | 114 | u32 vc_cmdval; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 115 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 116 | /* Check if sufficient pmic info is available for this vdd */ |
Kevin Hilman | ce8ebe0 | 2011-03-30 11:01:10 -0700 | [diff] [blame] | 117 | if (!voltdm->pmic) { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 118 | pr_err("%s: Insufficient pmic info to scale the vdd_%s\n", |
| 119 | __func__, voltdm->name); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
Kevin Hilman | ce8ebe0 | 2011-03-30 11:01:10 -0700 | [diff] [blame] | 123 | if (!voltdm->pmic->uv_to_vsel) { |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 124 | pr_err("%s: PMIC function to convert voltage in uV to vsel not registered. Hence unable to scale voltage for vdd_%s\n", |
| 125 | __func__, voltdm->name); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 126 | return -ENODATA; |
| 127 | } |
| 128 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 129 | if (!voltdm->read || !voltdm->write) { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 130 | pr_err("%s: No read/write API for accessing vdd_%s regs\n", |
| 131 | __func__, voltdm->name); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
Kevin Hilman | ce8ebe0 | 2011-03-30 11:01:10 -0700 | [diff] [blame] | 135 | *target_vsel = voltdm->pmic->uv_to_vsel(target_volt); |
Kevin Hilman | 7590f60 | 2011-04-05 16:55:22 -0700 | [diff] [blame] | 136 | *current_vsel = voltdm->pmic->uv_to_vsel(voltdm->nominal_volt); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 137 | |
| 138 | /* Setting the ON voltage to the new target voltage */ |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 139 | vc_cmdval = voltdm->read(vc->cmdval_reg); |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 140 | vc_cmdval &= ~vc->common->cmd_on_mask; |
| 141 | vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift); |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 142 | voltdm->write(vc_cmdval, vc->cmdval_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 143 | |
Tero Kristo | 8b5d8c0 | 2012-09-25 19:33:35 +0300 | [diff] [blame] | 144 | voltdm->vc_param->on = target_volt; |
| 145 | |
Kevin Hilman | 76ea742 | 2011-04-05 15:15:31 -0700 | [diff] [blame] | 146 | omap_vp_update_errorgain(voltdm, target_volt); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | void omap_vc_post_scale(struct voltagedomain *voltdm, |
| 152 | unsigned long target_volt, |
| 153 | u8 target_vsel, u8 current_vsel) |
| 154 | { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 155 | u32 smps_steps = 0, smps_delay = 0; |
| 156 | |
| 157 | smps_steps = abs(target_vsel - current_vsel); |
| 158 | /* SMPS slew rate / step size. 2us added as buffer. */ |
Kevin Hilman | ce8ebe0 | 2011-03-30 11:01:10 -0700 | [diff] [blame] | 159 | smps_delay = ((smps_steps * voltdm->pmic->step_size) / |
| 160 | voltdm->pmic->slew_rate) + 2; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 161 | udelay(smps_delay); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 164 | /* vc_bypass_scale - VC bypass method of voltage scaling */ |
| 165 | int omap_vc_bypass_scale(struct voltagedomain *voltdm, |
| 166 | unsigned long target_volt) |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 167 | { |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 168 | struct omap_vc_channel *vc = voltdm->vc; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 169 | u32 loop_cnt = 0, retries_cnt = 0; |
| 170 | u32 vc_valid, vc_bypass_val_reg, vc_bypass_value; |
| 171 | u8 target_vsel, current_vsel; |
| 172 | int ret; |
| 173 | |
| 174 | ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, ¤t_vsel); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 178 | vc_valid = vc->common->valid; |
| 179 | vc_bypass_val_reg = vc->common->bypass_val_reg; |
| 180 | vc_bypass_value = (target_vsel << vc->common->data_shift) | |
Kevin Hilman | 78614e0 | 2011-03-29 14:24:47 -0700 | [diff] [blame] | 181 | (vc->volt_reg_addr << vc->common->regaddr_shift) | |
| 182 | (vc->i2c_slave_addr << vc->common->slaveaddr_shift); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 183 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 184 | voltdm->write(vc_bypass_value, vc_bypass_val_reg); |
| 185 | voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 186 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 187 | vc_bypass_value = voltdm->read(vc_bypass_val_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 188 | /* |
| 189 | * Loop till the bypass command is acknowledged from the SMPS. |
| 190 | * NOTE: This is legacy code. The loop count and retry count needs |
| 191 | * to be revisited. |
| 192 | */ |
| 193 | while (!(vc_bypass_value & vc_valid)) { |
| 194 | loop_cnt++; |
| 195 | |
| 196 | if (retries_cnt > 10) { |
| 197 | pr_warning("%s: Retry count exceeded\n", __func__); |
| 198 | return -ETIMEDOUT; |
| 199 | } |
| 200 | |
| 201 | if (loop_cnt > 50) { |
| 202 | retries_cnt++; |
| 203 | loop_cnt = 0; |
| 204 | udelay(10); |
| 205 | } |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 206 | vc_bypass_value = voltdm->read(vc_bypass_val_reg); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel); |
| 210 | return 0; |
| 211 | } |
| 212 | |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 213 | /* Convert microsecond value to number of 32kHz clock cycles */ |
| 214 | static inline u32 omap_usec_to_32k(u32 usec) |
| 215 | { |
| 216 | return DIV_ROUND_UP_ULL(32768ULL * (u64)usec, 1000000ULL); |
| 217 | } |
| 218 | |
| 219 | /* Set oscillator setup time for omap3 */ |
| 220 | static void omap3_set_clksetup(u32 usec, struct voltagedomain *voltdm) |
| 221 | { |
| 222 | voltdm->write(omap_usec_to_32k(usec), OMAP3_PRM_CLKSETUP_OFFSET); |
| 223 | } |
| 224 | |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 225 | /** |
| 226 | * omap3_set_i2c_timings - sets i2c sleep timings for a channel |
| 227 | * @voltdm: channel to configure |
| 228 | * @off_mode: select whether retention or off mode values used |
| 229 | * |
| 230 | * Calculates and sets up voltage controller to use I2C based |
| 231 | * voltage scaling for sleep modes. This can be used for either off mode |
| 232 | * or retention. Off mode has additionally an option to use sys_off_mode |
| 233 | * pad, which uses a global signal to program the whole power IC to |
| 234 | * off-mode. |
| 235 | */ |
| 236 | static void omap3_set_i2c_timings(struct voltagedomain *voltdm, bool off_mode) |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 237 | { |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 238 | unsigned long voltsetup1; |
| 239 | u32 tgt_volt; |
| 240 | |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 241 | /* |
| 242 | * Oscillator is shut down only if we are using sys_off_mode pad, |
| 243 | * thus we set a minimal setup time here |
| 244 | */ |
| 245 | omap3_set_clksetup(1, voltdm); |
| 246 | |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 247 | if (off_mode) |
| 248 | tgt_volt = voltdm->vc_param->off; |
| 249 | else |
| 250 | tgt_volt = voltdm->vc_param->ret; |
| 251 | |
| 252 | voltsetup1 = (voltdm->vc_param->on - tgt_volt) / |
| 253 | voltdm->pmic->slew_rate; |
| 254 | |
| 255 | voltsetup1 = voltsetup1 * voltdm->sys_clk.rate / 8 / 1000000 + 1; |
| 256 | |
| 257 | voltdm->rmw(voltdm->vfsm->voltsetup_mask, |
| 258 | voltsetup1 << __ffs(voltdm->vfsm->voltsetup_mask), |
| 259 | voltdm->vfsm->voltsetup_reg); |
| 260 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 261 | /* |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 262 | * pmic is not controlling the voltage scaling during retention, |
| 263 | * thus set voltsetup2 to 0 |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 264 | */ |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 265 | voltdm->write(0, OMAP3_PRM_VOLTSETUP2_OFFSET); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * omap3_set_off_timings - sets off-mode timings for a channel |
| 270 | * @voltdm: channel to configure |
| 271 | * |
| 272 | * Calculates and sets up off-mode timings for a channel. Off-mode |
| 273 | * can use either I2C based voltage scaling, or alternatively |
| 274 | * sys_off_mode pad can be used to send a global command to power IC. |
| 275 | * This function first checks which mode is being used, and calls |
| 276 | * omap3_set_i2c_timings() if the system is using I2C control mode. |
| 277 | * sys_off_mode has the additional benefit that voltages can be |
| 278 | * scaled to zero volt level with TWL4030 / TWL5030, I2C can only |
| 279 | * scale to 600mV. |
| 280 | */ |
| 281 | static void omap3_set_off_timings(struct voltagedomain *voltdm) |
| 282 | { |
| 283 | unsigned long clksetup; |
| 284 | unsigned long voltsetup2; |
| 285 | unsigned long voltsetup2_old; |
| 286 | u32 val; |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 287 | u32 tstart, tshut; |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 288 | |
| 289 | /* check if sys_off_mode is used to control off-mode voltages */ |
| 290 | val = voltdm->read(OMAP3_PRM_VOLTCTRL_OFFSET); |
| 291 | if (!(val & OMAP3430_SEL_OFF_MASK)) { |
| 292 | /* No, omap is controlling them over I2C */ |
| 293 | omap3_set_i2c_timings(voltdm, true); |
| 294 | return; |
| 295 | } |
| 296 | |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 297 | omap_pm_get_oscillator(&tstart, &tshut); |
| 298 | omap3_set_clksetup(tstart, voltdm); |
| 299 | |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 300 | clksetup = voltdm->read(OMAP3_PRM_CLKSETUP_OFFSET); |
| 301 | |
| 302 | /* voltsetup 2 in us */ |
| 303 | voltsetup2 = voltdm->vc_param->on / voltdm->pmic->slew_rate; |
| 304 | |
| 305 | /* convert to 32k clk cycles */ |
| 306 | voltsetup2 = DIV_ROUND_UP(voltsetup2 * 32768, 1000000); |
| 307 | |
| 308 | voltsetup2_old = voltdm->read(OMAP3_PRM_VOLTSETUP2_OFFSET); |
| 309 | |
| 310 | /* |
| 311 | * Update voltsetup2 if higher than current value (needed because |
| 312 | * we have multiple channels with different ramp times), also |
| 313 | * update voltoffset always to value recommended by TRM |
| 314 | */ |
| 315 | if (voltsetup2 > voltsetup2_old) { |
| 316 | voltdm->write(voltsetup2, OMAP3_PRM_VOLTSETUP2_OFFSET); |
| 317 | voltdm->write(clksetup - voltsetup2, |
| 318 | OMAP3_PRM_VOLTOFFSET_OFFSET); |
| 319 | } else |
| 320 | voltdm->write(clksetup - voltsetup2_old, |
| 321 | OMAP3_PRM_VOLTOFFSET_OFFSET); |
| 322 | |
| 323 | /* |
| 324 | * omap is not controlling voltage scaling during off-mode, |
| 325 | * thus set voltsetup1 to 0 |
| 326 | */ |
| 327 | voltdm->rmw(voltdm->vfsm->voltsetup_mask, 0, |
| 328 | voltdm->vfsm->voltsetup_reg); |
| 329 | |
| 330 | /* voltoffset must be clksetup minus voltsetup2 according to TRM */ |
| 331 | voltdm->write(clksetup - voltsetup2, OMAP3_PRM_VOLTOFFSET_OFFSET); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void __init omap3_vc_init_channel(struct voltagedomain *voltdm) |
| 335 | { |
Tero Kristo | c589eb3 | 2012-09-25 19:33:36 +0300 | [diff] [blame] | 336 | omap3_set_off_timings(voltdm); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Tero Kristo | 9a1729c | 2012-09-25 19:33:38 +0300 | [diff] [blame] | 339 | /** |
| 340 | * omap4_calc_volt_ramp - calculates voltage ramping delays on omap4 |
| 341 | * @voltdm: channel to calculate values for |
| 342 | * @voltage_diff: voltage difference in microvolts |
| 343 | * |
| 344 | * Calculates voltage ramp prescaler + counter values for a voltage |
| 345 | * difference on omap4. Returns a field value suitable for writing to |
| 346 | * VOLTSETUP register for a channel in following format: |
| 347 | * bits[8:9] prescaler ... bits[0:5] counter. See OMAP4 TRM for reference. |
| 348 | */ |
| 349 | static u32 omap4_calc_volt_ramp(struct voltagedomain *voltdm, u32 voltage_diff) |
| 350 | { |
| 351 | u32 prescaler; |
| 352 | u32 cycles; |
| 353 | u32 time; |
| 354 | |
| 355 | time = voltage_diff / voltdm->pmic->slew_rate; |
| 356 | |
| 357 | cycles = voltdm->sys_clk.rate / 1000 * time / 1000; |
| 358 | |
| 359 | cycles /= 64; |
| 360 | prescaler = 0; |
| 361 | |
| 362 | /* shift to next prescaler until no overflow */ |
| 363 | |
| 364 | /* scale for div 256 = 64 * 4 */ |
| 365 | if (cycles > 63) { |
| 366 | cycles /= 4; |
| 367 | prescaler++; |
| 368 | } |
| 369 | |
| 370 | /* scale for div 512 = 256 * 2 */ |
| 371 | if (cycles > 63) { |
| 372 | cycles /= 2; |
| 373 | prescaler++; |
| 374 | } |
| 375 | |
| 376 | /* scale for div 2048 = 512 * 4 */ |
| 377 | if (cycles > 63) { |
| 378 | cycles /= 4; |
| 379 | prescaler++; |
| 380 | } |
| 381 | |
| 382 | /* check for overflow => invalid ramp time */ |
| 383 | if (cycles > 63) { |
| 384 | pr_warn("%s: invalid setuptime for vdd_%s\n", __func__, |
| 385 | voltdm->name); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | cycles++; |
| 390 | |
| 391 | return (prescaler << OMAP4430_RAMP_UP_PRESCAL_SHIFT) | |
| 392 | (cycles << OMAP4430_RAMP_UP_COUNT_SHIFT); |
| 393 | } |
| 394 | |
| 395 | /** |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 396 | * omap4_usec_to_val_scrm - convert microsecond value to SCRM module bitfield |
| 397 | * @usec: microseconds |
| 398 | * @shift: number of bits to shift left |
| 399 | * @mask: bitfield mask |
| 400 | * |
| 401 | * Converts microsecond value to OMAP4 SCRM bitfield. Bitfield is |
| 402 | * shifted to requested position, and checked agains the mask value. |
| 403 | * If larger, forced to the max value of the field (i.e. the mask itself.) |
| 404 | * Returns the SCRM bitfield value. |
| 405 | */ |
| 406 | static u32 omap4_usec_to_val_scrm(u32 usec, int shift, u32 mask) |
| 407 | { |
| 408 | u32 val; |
| 409 | |
| 410 | val = omap_usec_to_32k(usec) << shift; |
| 411 | |
| 412 | /* Check for overflow, if yes, force to max value */ |
| 413 | if (val > mask) |
| 414 | val = mask; |
| 415 | |
| 416 | return val; |
| 417 | } |
| 418 | |
| 419 | /** |
Tero Kristo | 9a1729c | 2012-09-25 19:33:38 +0300 | [diff] [blame] | 420 | * omap4_set_timings - set voltage ramp timings for a channel |
| 421 | * @voltdm: channel to configure |
| 422 | * @off_mode: whether off-mode values are used |
| 423 | * |
| 424 | * Calculates and sets the voltage ramp up / down values for a channel. |
| 425 | */ |
| 426 | static void omap4_set_timings(struct voltagedomain *voltdm, bool off_mode) |
| 427 | { |
| 428 | u32 val; |
| 429 | u32 ramp; |
| 430 | int offset; |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 431 | u32 tstart, tshut; |
Tero Kristo | 9a1729c | 2012-09-25 19:33:38 +0300 | [diff] [blame] | 432 | |
| 433 | if (off_mode) { |
| 434 | ramp = omap4_calc_volt_ramp(voltdm, |
| 435 | voltdm->vc_param->on - voltdm->vc_param->off); |
| 436 | offset = voltdm->vfsm->voltsetup_off_reg; |
| 437 | } else { |
| 438 | ramp = omap4_calc_volt_ramp(voltdm, |
| 439 | voltdm->vc_param->on - voltdm->vc_param->ret); |
| 440 | offset = voltdm->vfsm->voltsetup_reg; |
| 441 | } |
| 442 | |
| 443 | if (!ramp) |
| 444 | return; |
| 445 | |
| 446 | val = voltdm->read(offset); |
| 447 | |
| 448 | val |= ramp << OMAP4430_RAMP_DOWN_COUNT_SHIFT; |
| 449 | |
| 450 | val |= ramp << OMAP4430_RAMP_UP_COUNT_SHIFT; |
| 451 | |
| 452 | voltdm->write(val, offset); |
Tero Kristo | d68ff97 | 2012-09-25 19:33:41 +0300 | [diff] [blame] | 453 | |
| 454 | omap_pm_get_oscillator(&tstart, &tshut); |
| 455 | |
| 456 | val = omap4_usec_to_val_scrm(tstart, OMAP4_SETUPTIME_SHIFT, |
| 457 | OMAP4_SETUPTIME_MASK); |
| 458 | val |= omap4_usec_to_val_scrm(tshut, OMAP4_DOWNTIME_SHIFT, |
| 459 | OMAP4_DOWNTIME_MASK); |
| 460 | |
| 461 | __raw_writel(val, OMAP4_SCRM_CLKSETUPTIME); |
Tero Kristo | 9a1729c | 2012-09-25 19:33:38 +0300 | [diff] [blame] | 462 | } |
| 463 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 464 | /* OMAP4 specific voltage init functions */ |
| 465 | static void __init omap4_vc_init_channel(struct voltagedomain *voltdm) |
| 466 | { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 467 | static bool is_initialized; |
| 468 | u32 vc_val; |
| 469 | |
Tero Kristo | 9a1729c | 2012-09-25 19:33:38 +0300 | [diff] [blame] | 470 | omap4_set_timings(voltdm, true); |
| 471 | omap4_set_timings(voltdm, false); |
| 472 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 473 | if (is_initialized) |
| 474 | return; |
| 475 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 476 | /* XXX These are magic numbers and do not belong! */ |
| 477 | vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT); |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 478 | voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 479 | |
| 480 | is_initialized = true; |
| 481 | } |
| 482 | |
Kevin Hilman | f539548 | 2011-03-30 16:36:30 -0700 | [diff] [blame] | 483 | /** |
| 484 | * omap_vc_i2c_init - initialize I2C interface to PMIC |
| 485 | * @voltdm: voltage domain containing VC data |
| 486 | * |
Russell King | 2d5b479 | 2012-02-07 10:13:02 +0000 | [diff] [blame] | 487 | * Use PMIC supplied settings for I2C high-speed mode and |
Kevin Hilman | f539548 | 2011-03-30 16:36:30 -0700 | [diff] [blame] | 488 | * master code (if set) and program the VC I2C configuration |
| 489 | * register. |
| 490 | * |
| 491 | * The VC I2C configuration is common to all VC channels, |
| 492 | * so this function only configures I2C for the first VC |
| 493 | * channel registers. All other VC channels will use the |
| 494 | * same configuration. |
| 495 | */ |
| 496 | static void __init omap_vc_i2c_init(struct voltagedomain *voltdm) |
| 497 | { |
| 498 | struct omap_vc_channel *vc = voltdm->vc; |
| 499 | static bool initialized; |
| 500 | static bool i2c_high_speed; |
| 501 | u8 mcode; |
| 502 | |
| 503 | if (initialized) { |
| 504 | if (voltdm->pmic->i2c_high_speed != i2c_high_speed) |
Russell King | 0bf68f5 | 2012-02-07 10:23:43 +0000 | [diff] [blame] | 505 | pr_warn("%s: I2C config for vdd_%s does not match other channels (%u).", |
| 506 | __func__, voltdm->name, i2c_high_speed); |
Kevin Hilman | f539548 | 2011-03-30 16:36:30 -0700 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
| 510 | i2c_high_speed = voltdm->pmic->i2c_high_speed; |
| 511 | if (i2c_high_speed) |
| 512 | voltdm->rmw(vc->common->i2c_cfg_hsen_mask, |
| 513 | vc->common->i2c_cfg_hsen_mask, |
| 514 | vc->common->i2c_cfg_reg); |
| 515 | |
| 516 | mcode = voltdm->pmic->i2c_mcode; |
| 517 | if (mcode) |
| 518 | voltdm->rmw(vc->common->i2c_mcode_mask, |
| 519 | mcode << __ffs(vc->common->i2c_mcode_mask), |
| 520 | vc->common->i2c_cfg_reg); |
| 521 | |
| 522 | initialized = true; |
| 523 | } |
| 524 | |
Tero Kristo | 8b5d8c0 | 2012-09-25 19:33:35 +0300 | [diff] [blame] | 525 | /** |
| 526 | * omap_vc_calc_vsel - calculate vsel value for a channel |
| 527 | * @voltdm: channel to calculate value for |
| 528 | * @uvolt: microvolt value to convert to vsel |
| 529 | * |
| 530 | * Converts a microvolt value to vsel value for the used PMIC. |
| 531 | * This checks whether the microvolt value is out of bounds, and |
| 532 | * adjusts the value accordingly. If unsupported value detected, |
| 533 | * warning is thrown. |
| 534 | */ |
| 535 | static u8 omap_vc_calc_vsel(struct voltagedomain *voltdm, u32 uvolt) |
| 536 | { |
| 537 | if (voltdm->pmic->vddmin > uvolt) |
| 538 | uvolt = voltdm->pmic->vddmin; |
| 539 | if (voltdm->pmic->vddmax < uvolt) { |
| 540 | WARN(1, "%s: voltage not supported by pmic: %u vs max %u\n", |
| 541 | __func__, uvolt, voltdm->pmic->vddmax); |
| 542 | /* Lets try maximum value anyway */ |
| 543 | uvolt = voltdm->pmic->vddmax; |
| 544 | } |
| 545 | |
| 546 | return voltdm->pmic->uv_to_vsel(uvolt); |
| 547 | } |
| 548 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 549 | void __init omap_vc_init_channel(struct voltagedomain *voltdm) |
| 550 | { |
Kevin Hilman | d84adcf | 2011-03-22 16:14:57 -0700 | [diff] [blame] | 551 | struct omap_vc_channel *vc = voltdm->vc; |
Kevin Hilman | 08d1c9a | 2011-03-29 15:14:38 -0700 | [diff] [blame] | 552 | u8 on_vsel, onlp_vsel, ret_vsel, off_vsel; |
| 553 | u32 val; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 554 | |
Kevin Hilman | ce8ebe0 | 2011-03-30 11:01:10 -0700 | [diff] [blame] | 555 | if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) { |
Russell King | 2d5b479 | 2012-02-07 10:13:02 +0000 | [diff] [blame] | 556 | pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name); |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 557 | return; |
| 558 | } |
| 559 | |
Kevin Hilman | 4bcc475 | 2011-03-28 10:40:15 -0700 | [diff] [blame] | 560 | if (!voltdm->read || !voltdm->write) { |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 561 | pr_err("%s: No read/write API for accessing vdd_%s regs\n", |
| 562 | __func__, voltdm->name); |
| 563 | return; |
| 564 | } |
| 565 | |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 566 | vc->cfg_channel = 0; |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 567 | if (vc->flags & OMAP_VC_CHANNEL_CFG_MUTANT) |
| 568 | vc_cfg_bits = &vc_mutant_channel_cfg; |
| 569 | else |
| 570 | vc_cfg_bits = &vc_default_channel_cfg; |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 571 | |
Kevin Hilman | ba112a4 | 2011-03-29 14:02:36 -0700 | [diff] [blame] | 572 | /* get PMIC/board specific settings */ |
Kevin Hilman | ce8ebe0 | 2011-03-30 11:01:10 -0700 | [diff] [blame] | 573 | vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr; |
| 574 | vc->volt_reg_addr = voltdm->pmic->volt_reg_addr; |
| 575 | vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr; |
Kevin Hilman | ba112a4 | 2011-03-29 14:02:36 -0700 | [diff] [blame] | 576 | |
| 577 | /* Configure the i2c slave address for this VC */ |
| 578 | voltdm->rmw(vc->smps_sa_mask, |
| 579 | vc->i2c_slave_addr << __ffs(vc->smps_sa_mask), |
Kevin Hilman | 5876c94 | 2011-07-20 16:35:46 -0700 | [diff] [blame] | 580 | vc->smps_sa_reg); |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 581 | vc->cfg_channel |= vc_cfg_bits->sa; |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 582 | |
Kevin Hilman | e4e021c | 2011-06-09 11:01:55 -0700 | [diff] [blame] | 583 | /* |
| 584 | * Configure the PMIC register addresses. |
| 585 | */ |
| 586 | voltdm->rmw(vc->smps_volra_mask, |
| 587 | vc->volt_reg_addr << __ffs(vc->smps_volra_mask), |
Kevin Hilman | 5876c94 | 2011-07-20 16:35:46 -0700 | [diff] [blame] | 588 | vc->smps_volra_reg); |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 589 | vc->cfg_channel |= vc_cfg_bits->rav; |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 590 | |
| 591 | if (vc->cmd_reg_addr) { |
Kevin Hilman | e4e021c | 2011-06-09 11:01:55 -0700 | [diff] [blame] | 592 | voltdm->rmw(vc->smps_cmdra_mask, |
| 593 | vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask), |
Kevin Hilman | 5876c94 | 2011-07-20 16:35:46 -0700 | [diff] [blame] | 594 | vc->smps_cmdra_reg); |
Tero Kristo | 2ceec7b | 2012-09-25 19:33:47 +0300 | [diff] [blame^] | 595 | vc->cfg_channel |= vc_cfg_bits->rac; |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 596 | } |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 597 | |
Tero Kristo | 2ceec7b | 2012-09-25 19:33:47 +0300 | [diff] [blame^] | 598 | if (vc->cmd_reg_addr == vc->volt_reg_addr) |
| 599 | vc->cfg_channel |= vc_cfg_bits->racen; |
| 600 | |
Kevin Hilman | 08d1c9a | 2011-03-29 15:14:38 -0700 | [diff] [blame] | 601 | /* Set up the on, inactive, retention and off voltage */ |
Tero Kristo | 8b5d8c0 | 2012-09-25 19:33:35 +0300 | [diff] [blame] | 602 | on_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->on); |
| 603 | onlp_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->onlp); |
| 604 | ret_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->ret); |
| 605 | off_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->off); |
| 606 | |
Kevin Hilman | 08d1c9a | 2011-03-29 15:14:38 -0700 | [diff] [blame] | 607 | val = ((on_vsel << vc->common->cmd_on_shift) | |
| 608 | (onlp_vsel << vc->common->cmd_onlp_shift) | |
| 609 | (ret_vsel << vc->common->cmd_ret_shift) | |
| 610 | (off_vsel << vc->common->cmd_off_shift)); |
| 611 | voltdm->write(val, vc->cmdval_reg); |
Kevin Hilman | 8abc0b5 | 2011-06-02 17:28:13 -0700 | [diff] [blame] | 612 | vc->cfg_channel |= vc_cfg_bits->cmd; |
Kevin Hilman | 24d3194 | 2011-03-29 15:57:16 -0700 | [diff] [blame] | 613 | |
| 614 | /* Channel configuration */ |
| 615 | omap_vc_config_channel(voltdm); |
Kevin Hilman | 08d1c9a | 2011-03-29 15:14:38 -0700 | [diff] [blame] | 616 | |
Kevin Hilman | f539548 | 2011-03-30 16:36:30 -0700 | [diff] [blame] | 617 | omap_vc_i2c_init(voltdm); |
| 618 | |
Kevin Hilman | ccd5ca7 | 2011-03-21 14:08:55 -0700 | [diff] [blame] | 619 | if (cpu_is_omap34xx()) |
| 620 | omap3_vc_init_channel(voltdm); |
| 621 | else if (cpu_is_omap44xx()) |
| 622 | omap4_vc_init_channel(voltdm); |
| 623 | } |
| 624 | |