blob: 72c9cb64a42bd7c4ed486f99a722f03d5551bd14 [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 Hilmanccd5ca72011-03-21 14:08:55 -0700160 static bool is_initialized;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700161
162 if (is_initialized)
163 return;
164
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700165 /*
166 * Generic VC parameters init
167 * XXX This data should be abstracted out
168 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700169 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 Hilmanccd5ca72011-03-21 14:08:55 -0700173
174 omap3_vfsm_init(voltdm);
175
176 is_initialized = true;
177}
178
179
180/* OMAP4 specific voltage init functions */
181static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
182{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700183 static bool is_initialized;
184 u32 vc_val;
185
186 if (is_initialized)
187 return;
188
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700189 /*
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 Hilman4bcc4752011-03-28 10:40:15 -0700196 voltdm->write(vc_val, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700197
198 /* XXX These are magic numbers and do not belong! */
199 vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700200 voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700201
202 is_initialized = true;
203}
204
205void __init omap_vc_init_channel(struct voltagedomain *voltdm)
206{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700207 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700208 struct omap_vdd_info *vdd = voltdm->vdd;
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700209 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
210 u32 val;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700211
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 Hilman4bcc4752011-03-28 10:40:15 -0700219 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700220 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
221 __func__, voltdm->name);
222 return;
223 }
224
Kevin Hilmanba112a42011-03-29 14:02:36 -0700225 /* get PMIC/board specific settings */
226 vc->i2c_slave_addr = vdd->pmic_info->i2c_slave_addr;
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700227 vc->volt_reg_addr = vdd->pmic_info->volt_reg_addr;
228 vc->cmd_reg_addr = vdd->pmic_info->cmd_reg_addr;
Kevin Hilman5892bb12011-03-29 14:36:04 -0700229 vc->setup_time = vdd->pmic_info->volt_setup_time;
Kevin Hilmanba112a42011-03-29 14:02:36 -0700230
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 Hilmanccd5ca72011-03-21 14:08:55 -0700235
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700236 /*
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 Hilmanccd5ca72011-03-21 14:08:55 -0700246
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700247 /* 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 Hilmanccd5ca72011-03-21 14:08:55 -0700258 /* Configure the setup times */
Kevin Hilman5892bb12011-03-29 14:36:04 -0700259 voltdm->rmw(voltdm->vfsm->voltsetup_mask,
260 vc->setup_time << __ffs(voltdm->vfsm->voltsetup_mask),
261 voltdm->vfsm->voltsetup_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700262
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