blob: 098af2f3a63efa405cd8db4b966fe8d9f85abfac [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{
27 struct omap_vc_instance_data *vc = voltdm->vdd->vc_data;
28 struct omap_vdd_info *vdd = voltdm->vdd;
29 struct omap_volt_data *volt_data;
30 const struct omap_vc_common_data *vc_common;
31 const struct omap_vp_common_data *vp_common;
32 u32 vc_cmdval, vp_errgain_val;
33
34 vc_common = vc->vc_common;
35 vp_common = vdd->vp_data->vp_common;
36
37 /* Check if sufficient pmic info is available for this vdd */
38 if (!vdd->pmic_info) {
39 pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
40 __func__, voltdm->name);
41 return -EINVAL;
42 }
43
44 if (!vdd->pmic_info->uv_to_vsel) {
45 pr_err("%s: PMIC function to convert voltage in uV to"
46 "vsel not registered. Hence unable to scale voltage"
47 "for vdd_%s\n", __func__, voltdm->name);
48 return -ENODATA;
49 }
50
51 if (!vdd->read_reg || !vdd->write_reg) {
52 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
53 __func__, voltdm->name);
54 return -EINVAL;
55 }
56
57 /* Get volt_data corresponding to target_volt */
58 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
59 if (IS_ERR(volt_data))
60 volt_data = NULL;
61
62 *target_vsel = vdd->pmic_info->uv_to_vsel(target_volt);
63 *current_vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
64
65 /* Setting the ON voltage to the new target voltage */
66 vc_cmdval = vdd->read_reg(vc->vc_common->prm_mod, vc->cmdval_reg);
67 vc_cmdval &= ~vc_common->cmd_on_mask;
68 vc_cmdval |= (*target_vsel << vc_common->cmd_on_shift);
69 vdd->write_reg(vc_cmdval, vc->vc_common->prm_mod, vc->cmdval_reg);
70
71 /* Setting vp errorgain based on the voltage */
72 if (volt_data) {
73 vp_errgain_val = vdd->read_reg(vdd->vp_data->vp_common->prm_mod,
74 vdd->vp_data->vpconfig);
75 vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
76 vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
77 vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
78 vp_common->vpconfig_errorgain_shift;
79 vdd->write_reg(vp_errgain_val, vdd->vp_data->vp_common->prm_mod,
80 vdd->vp_data->vpconfig);
81 }
82
83 return 0;
84}
85
86void omap_vc_post_scale(struct voltagedomain *voltdm,
87 unsigned long target_volt,
88 u8 target_vsel, u8 current_vsel)
89{
90 struct omap_vdd_info *vdd = voltdm->vdd;
91 u32 smps_steps = 0, smps_delay = 0;
92
93 smps_steps = abs(target_vsel - current_vsel);
94 /* SMPS slew rate / step size. 2us added as buffer. */
95 smps_delay = ((smps_steps * vdd->pmic_info->step_size) /
96 vdd->pmic_info->slew_rate) + 2;
97 udelay(smps_delay);
98
99 vdd->curr_volt = target_volt;
100}
101
102/* vc_bypass_scale_voltage - VC bypass method of voltage scaling */
103int omap_vc_bypass_scale_voltage(struct voltagedomain *voltdm,
104 unsigned long target_volt)
105{
106 struct omap_vc_instance_data *vc = voltdm->vdd->vc_data;
107 struct omap_vdd_info *vdd = voltdm->vdd;
108 u32 loop_cnt = 0, retries_cnt = 0;
109 u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
110 u8 target_vsel, current_vsel;
111 int ret;
112
113 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
114 if (ret)
115 return ret;
116
117 vc_valid = vc->vc_common->valid;
118 vc_bypass_val_reg = vc->vc_common->bypass_val_reg;
119 vc_bypass_value = (target_vsel << vc->vc_common->data_shift) |
120 (vdd->pmic_info->pmic_reg <<
121 vc->vc_common->regaddr_shift) |
122 (vdd->pmic_info->i2c_slave_addr <<
123 vc->vc_common->slaveaddr_shift);
124
125 vdd->write_reg(vc_bypass_value, vc->vc_common->prm_mod, vc_bypass_val_reg);
126 vdd->write_reg(vc_bypass_value | vc_valid, vc->vc_common->prm_mod,
127 vc_bypass_val_reg);
128
129 vc_bypass_value = vdd->read_reg(vc->vc_common->prm_mod, vc_bypass_val_reg);
130 /*
131 * Loop till the bypass command is acknowledged from the SMPS.
132 * NOTE: This is legacy code. The loop count and retry count needs
133 * to be revisited.
134 */
135 while (!(vc_bypass_value & vc_valid)) {
136 loop_cnt++;
137
138 if (retries_cnt > 10) {
139 pr_warning("%s: Retry count exceeded\n", __func__);
140 return -ETIMEDOUT;
141 }
142
143 if (loop_cnt > 50) {
144 retries_cnt++;
145 loop_cnt = 0;
146 udelay(10);
147 }
148 vc_bypass_value = vdd->read_reg(vc->vc_common->prm_mod,
149 vc_bypass_val_reg);
150 }
151
152 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
153 return 0;
154}
155
156static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
157{
158 struct omap_vc_instance_data *vc = voltdm->vdd->vc_data;
159 struct omap_vdd_info *vdd = voltdm->vdd;
160
161 /*
162 * Voltage Manager FSM parameters init
163 * XXX This data should be passed in from the board file
164 */
165 vdd->write_reg(OMAP3_CLKSETUP, vc->vc_common->prm_mod, OMAP3_PRM_CLKSETUP_OFFSET);
166 vdd->write_reg(OMAP3_VOLTOFFSET, vc->vc_common->prm_mod,
167 OMAP3_PRM_VOLTOFFSET_OFFSET);
168 vdd->write_reg(OMAP3_VOLTSETUP2, vc->vc_common->prm_mod,
169 OMAP3_PRM_VOLTSETUP2_OFFSET);
170}
171
172static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
173{
174 struct omap_vc_instance_data *vc = voltdm->vdd->vc_data;
175 struct omap_vdd_info *vdd = voltdm->vdd;
176 static bool is_initialized;
177 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
178 u32 vc_val;
179
180 if (is_initialized)
181 return;
182
183 /* Set up the on, inactive, retention and off voltage */
184 on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
185 onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
186 ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
187 off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
188 vc_val = ((on_vsel << vc->vc_common->cmd_on_shift) |
189 (onlp_vsel << vc->vc_common->cmd_onlp_shift) |
190 (ret_vsel << vc->vc_common->cmd_ret_shift) |
191 (off_vsel << vc->vc_common->cmd_off_shift));
192 vdd->write_reg(vc_val, vc->vc_common->prm_mod, vc->cmdval_reg);
193
194 /*
195 * Generic VC parameters init
196 * XXX This data should be abstracted out
197 */
198 vdd->write_reg(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, vc->vc_common->prm_mod,
199 OMAP3_PRM_VC_CH_CONF_OFFSET);
200 vdd->write_reg(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, vc->vc_common->prm_mod,
201 OMAP3_PRM_VC_I2C_CFG_OFFSET);
202
203 omap3_vfsm_init(voltdm);
204
205 is_initialized = true;
206}
207
208
209/* OMAP4 specific voltage init functions */
210static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
211{
212 struct omap_vc_instance_data *vc = voltdm->vdd->vc_data;
213 struct omap_vdd_info *vdd = voltdm->vdd;
214 static bool is_initialized;
215 u32 vc_val;
216
217 if (is_initialized)
218 return;
219
220 /* TODO: Configure setup times and CMD_VAL values*/
221
222 /*
223 * Generic VC parameters init
224 * XXX This data should be abstracted out
225 */
226 vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
227 OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
228 OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
229 vdd->write_reg(vc_val, vc->vc_common->prm_mod, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
230
231 /* XXX These are magic numbers and do not belong! */
232 vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
233 vdd->write_reg(vc_val, vc->vc_common->prm_mod, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
234
235 is_initialized = true;
236}
237
238void __init omap_vc_init_channel(struct voltagedomain *voltdm)
239{
240 struct omap_vc_instance_data *vc = voltdm->vdd->vc_data;
241 struct omap_vdd_info *vdd = voltdm->vdd;
242 u32 vc_val;
243
244 if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
245 pr_err("%s: PMIC info requried to configure vc for"
246 "vdd_%s not populated.Hence cannot initialize vc\n",
247 __func__, voltdm->name);
248 return;
249 }
250
251 if (!vdd->read_reg || !vdd->write_reg) {
252 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
253 __func__, voltdm->name);
254 return;
255 }
256
257 /* Set up the SMPS_SA(i2c slave address in VC */
258 vc_val = vdd->read_reg(vc->vc_common->prm_mod,
259 vc->vc_common->smps_sa_reg);
260 vc_val &= ~vc->smps_sa_mask;
261 vc_val |= vdd->pmic_info->i2c_slave_addr << vc->smps_sa_shift;
262 vdd->write_reg(vc_val, vc->vc_common->prm_mod,
263 vc->vc_common->smps_sa_reg);
264
265 /* Setup the VOLRA(pmic reg addr) in VC */
266 vc_val = vdd->read_reg(vc->vc_common->prm_mod,
267 vc->vc_common->smps_volra_reg);
268 vc_val &= ~vc->smps_volra_mask;
269 vc_val |= vdd->pmic_info->pmic_reg << vc->smps_volra_shift;
270 vdd->write_reg(vc_val, vc->vc_common->prm_mod,
271 vc->vc_common->smps_volra_reg);
272
273 /* Configure the setup times */
274 vc_val = vdd->read_reg(vc->vc_common->prm_mod, vdd->vfsm->voltsetup_reg);
275 vc_val &= ~vdd->vfsm->voltsetup_mask;
276 vc_val |= vdd->pmic_info->volt_setup_time <<
277 vdd->vfsm->voltsetup_shift;
278 vdd->write_reg(vc_val, vc->vc_common->prm_mod, vdd->vfsm->voltsetup_reg);
279
280 if (cpu_is_omap34xx())
281 omap3_vc_init_channel(voltdm);
282 else if (cpu_is_omap44xx())
283 omap4_vc_init_channel(voltdm);
284}
285