blob: 8f0105a7bb4b988b15ef124f4e82e21e86c01254 [file] [log] [blame]
Kevin Hilmanccd5ca72011-03-21 14:08:55 -07001/*
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 */
23int omap_vc_pre_scale(struct voltagedomain *voltdm,
24 unsigned long target_volt,
25 u8 *target_vsel, u8 *current_vsel)
26{
Kevin Hilmand84adcf2011-03-22 16:14:57 -070027 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070028 struct omap_vdd_info *vdd = voltdm->vdd;
29 struct omap_volt_data *volt_data;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070030 const struct omap_vp_common_data *vp_common;
31 u32 vc_cmdval, vp_errgain_val;
32
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070033 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 Hilman4bcc4752011-03-28 10:40:15 -070049 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070050 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 Hilman4bcc4752011-03-28 10:40:15 -070061 *current_vsel = voltdm->read(vdd->vp_data->voltage);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070062
63 /* Setting the ON voltage to the new target voltage */
Kevin Hilman4bcc4752011-03-28 10:40:15 -070064 vc_cmdval = voltdm->read(vc->cmdval_reg);
Kevin Hilmand84adcf2011-03-22 16:14:57 -070065 vc_cmdval &= ~vc->common->cmd_on_mask;
66 vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
Kevin Hilman4bcc4752011-03-28 10:40:15 -070067 voltdm->write(vc_cmdval, vc->cmdval_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070068
69 /* Setting vp errorgain based on the voltage */
70 if (volt_data) {
Kevin Hilman4bcc4752011-03-28 10:40:15 -070071 vp_errgain_val = voltdm->read(vdd->vp_data->vpconfig);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070072 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 Hilman4bcc4752011-03-28 10:40:15 -070076 voltdm->write(vp_errgain_val, vdd->vp_data->vpconfig);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070077 }
78
79 return 0;
80}
81
82void 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 Hilmand84adcf2011-03-22 16:14:57 -070098/* vc_bypass_scale - VC bypass method of voltage scaling */
99int omap_vc_bypass_scale(struct voltagedomain *voltdm,
100 unsigned long target_volt)
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700101{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700102 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700103 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, &current_vsel);
109 if (ret)
110 return ret;
111
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700112 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 Hilman78614e02011-03-29 14:24:47 -0700115 (vc->volt_reg_addr << vc->common->regaddr_shift) |
116 (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700117
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700118 voltdm->write(vc_bypass_value, vc_bypass_val_reg);
119 voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700120
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700121 vc_bypass_value = voltdm->read(vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700122 /*
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 Hilman4bcc4752011-03-28 10:40:15 -0700140 vc_bypass_value = voltdm->read(vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700141 }
142
143 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
144 return 0;
145}
146
147static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
148{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700149 /*
150 * Voltage Manager FSM parameters init
151 * XXX This data should be passed in from the board file
152 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700153 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 Hilmanccd5ca72011-03-21 14:08:55 -0700156}
157
158static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
159{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700160 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700161 struct omap_vdd_info *vdd = voltdm->vdd;
162 static bool is_initialized;
163 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
164 u32 vc_val;
165
166 if (is_initialized)
167 return;
168
169 /* Set up the on, inactive, retention and off voltage */
170 on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
171 onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
172 ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
173 off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700174 vc_val = ((on_vsel << vc->common->cmd_on_shift) |
175 (onlp_vsel << vc->common->cmd_onlp_shift) |
176 (ret_vsel << vc->common->cmd_ret_shift) |
177 (off_vsel << vc->common->cmd_off_shift));
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700178 voltdm->write(vc_val, vc->cmdval_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700179
180 /*
181 * Generic VC parameters init
182 * XXX This data should be abstracted out
183 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700184 voltdm->write(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK,
185 OMAP3_PRM_VC_CH_CONF_OFFSET);
186 voltdm->write(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK,
187 OMAP3_PRM_VC_I2C_CFG_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700188
189 omap3_vfsm_init(voltdm);
190
191 is_initialized = true;
192}
193
194
195/* OMAP4 specific voltage init functions */
196static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
197{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700198 static bool is_initialized;
199 u32 vc_val;
200
201 if (is_initialized)
202 return;
203
204 /* TODO: Configure setup times and CMD_VAL values*/
205
206 /*
207 * Generic VC parameters init
208 * XXX This data should be abstracted out
209 */
210 vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
211 OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
212 OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700213 voltdm->write(vc_val, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700214
215 /* XXX These are magic numbers and do not belong! */
216 vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700217 voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700218
219 is_initialized = true;
220}
221
222void __init omap_vc_init_channel(struct voltagedomain *voltdm)
223{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700224 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700225 struct omap_vdd_info *vdd = voltdm->vdd;
226 u32 vc_val;
227
228 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
229 pr_err("%s: PMIC info requried to configure vc for"
230 "vdd_%s not populated.Hence cannot initialize vc\n",
231 __func__, voltdm->name);
232 return;
233 }
234
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700235 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700236 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
237 __func__, voltdm->name);
238 return;
239 }
240
Kevin Hilmanba112a42011-03-29 14:02:36 -0700241 /* get PMIC/board specific settings */
242 vc->i2c_slave_addr = vdd->pmic_info->i2c_slave_addr;
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700243 vc->volt_reg_addr = vdd->pmic_info->volt_reg_addr;
244 vc->cmd_reg_addr = vdd->pmic_info->cmd_reg_addr;
Kevin Hilmanba112a42011-03-29 14:02:36 -0700245
246 /* Configure the i2c slave address for this VC */
247 voltdm->rmw(vc->smps_sa_mask,
248 vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
249 vc->common->smps_sa_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700250
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700251 /*
252 * Configure the PMIC register addresses.
253 */
254 voltdm->rmw(vc->smps_volra_mask,
255 vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
256 vc->common->smps_volra_reg);
257 if (vc->cmd_reg_addr)
258 voltdm->rmw(vc->smps_cmdra_mask,
259 vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
260 vc->common->smps_cmdra_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700261
262 /* Configure the setup times */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700263 vc_val = voltdm->read(vdd->vfsm->voltsetup_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700264 vc_val &= ~vdd->vfsm->voltsetup_mask;
265 vc_val |= vdd->pmic_info->volt_setup_time <<
266 vdd->vfsm->voltsetup_shift;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700267 voltdm->write(vc_val, vdd->vfsm->voltsetup_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700268
269 if (cpu_is_omap34xx())
270 omap3_vc_init_channel(voltdm);
271 else if (cpu_is_omap44xx())
272 omap4_vc_init_channel(voltdm);
273}
274