blob: 5d545632388d3a82118a6532b23fed979fd40894 [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
Kevin Hilman24d31942011-03-29 15:57:16 -070022/*
23 * Channel configuration bits, common for OMAP3 & 4
24 * OMAP3 register: PRM_VC_CH_CONF
25 * OMAP4 register: PRM_VC_CFG_CHANNEL
26 */
27#define CFG_CHANNEL_SA BIT(0)
28#define CFG_CHANNEL_RAV BIT(1)
29#define CFG_CHANNEL_RAC BIT(2)
30#define CFG_CHANNEL_RACEN BIT(3)
31#define CFG_CHANNEL_CMD BIT(4)
32#define CFG_CHANNEL_MASK 0x3f
33
34/**
35 * omap_vc_config_channel - configure VC channel to PMIC mappings
36 * @voltdm: pointer to voltagdomain defining the desired VC channel
37 *
38 * Configures the VC channel to PMIC mappings for the following
39 * PMIC settings
40 * - i2c slave address (SA)
41 * - voltage configuration address (RAV)
42 * - command configuration address (RAC) and enable bit (RACEN)
43 * - command values for ON, ONLP, RET and OFF (CMD)
44 *
45 * This function currently only allows flexible configuration of the
46 * non-default channel. Starting with OMAP4, there are more than 2
47 * channels, with one defined as the default (on OMAP4, it's MPU.)
48 * Only the non-default channel can be configured.
49 */
50static int omap_vc_config_channel(struct voltagedomain *voltdm)
51{
52 struct omap_vc_channel *vc = voltdm->vc;
53
54 /*
55 * For default channel, the only configurable bit is RACEN.
56 * All others must stay at zero (see function comment above.)
57 */
58 if (vc->flags & OMAP_VC_CHANNEL_DEFAULT)
59 vc->cfg_channel &= CFG_CHANNEL_RACEN;
60
61 voltdm->rmw(CFG_CHANNEL_MASK << vc->cfg_channel_sa_shift,
62 vc->cfg_channel << vc->cfg_channel_sa_shift,
63 vc->common->cfg_channel_reg);
64
65 return 0;
66}
67
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070068/* Voltage scale and accessory APIs */
69int omap_vc_pre_scale(struct voltagedomain *voltdm,
70 unsigned long target_volt,
71 u8 *target_vsel, u8 *current_vsel)
72{
Kevin Hilmand84adcf2011-03-22 16:14:57 -070073 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070074 struct omap_vdd_info *vdd = voltdm->vdd;
75 struct omap_volt_data *volt_data;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070076 const struct omap_vp_common_data *vp_common;
77 u32 vc_cmdval, vp_errgain_val;
78
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070079 vp_common = vdd->vp_data->vp_common;
80
81 /* Check if sufficient pmic info is available for this vdd */
Kevin Hilmance8ebe02011-03-30 11:01:10 -070082 if (!voltdm->pmic) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070083 pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
84 __func__, voltdm->name);
85 return -EINVAL;
86 }
87
Kevin Hilmance8ebe02011-03-30 11:01:10 -070088 if (!voltdm->pmic->uv_to_vsel) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070089 pr_err("%s: PMIC function to convert voltage in uV to"
90 "vsel not registered. Hence unable to scale voltage"
91 "for vdd_%s\n", __func__, voltdm->name);
92 return -ENODATA;
93 }
94
Kevin Hilman4bcc4752011-03-28 10:40:15 -070095 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070096 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
97 __func__, voltdm->name);
98 return -EINVAL;
99 }
100
101 /* Get volt_data corresponding to target_volt */
102 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
103 if (IS_ERR(volt_data))
104 volt_data = NULL;
105
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700106 *target_vsel = voltdm->pmic->uv_to_vsel(target_volt);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700107 *current_vsel = voltdm->read(vdd->vp_data->voltage);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700108
109 /* Setting the ON voltage to the new target voltage */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700110 vc_cmdval = voltdm->read(vc->cmdval_reg);
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700111 vc_cmdval &= ~vc->common->cmd_on_mask;
112 vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700113 voltdm->write(vc_cmdval, vc->cmdval_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700114
115 /* Setting vp errorgain based on the voltage */
116 if (volt_data) {
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700117 vp_errgain_val = voltdm->read(vdd->vp_data->vpconfig);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700118 vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
119 vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
120 vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
121 vp_common->vpconfig_errorgain_shift;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700122 voltdm->write(vp_errgain_val, vdd->vp_data->vpconfig);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700123 }
124
125 return 0;
126}
127
128void omap_vc_post_scale(struct voltagedomain *voltdm,
129 unsigned long target_volt,
130 u8 target_vsel, u8 current_vsel)
131{
132 struct omap_vdd_info *vdd = voltdm->vdd;
133 u32 smps_steps = 0, smps_delay = 0;
134
135 smps_steps = abs(target_vsel - current_vsel);
136 /* SMPS slew rate / step size. 2us added as buffer. */
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700137 smps_delay = ((smps_steps * voltdm->pmic->step_size) /
138 voltdm->pmic->slew_rate) + 2;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700139 udelay(smps_delay);
140
141 vdd->curr_volt = target_volt;
142}
143
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700144/* vc_bypass_scale - VC bypass method of voltage scaling */
145int omap_vc_bypass_scale(struct voltagedomain *voltdm,
146 unsigned long target_volt)
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700147{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700148 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700149 u32 loop_cnt = 0, retries_cnt = 0;
150 u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
151 u8 target_vsel, current_vsel;
152 int ret;
153
154 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
155 if (ret)
156 return ret;
157
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700158 vc_valid = vc->common->valid;
159 vc_bypass_val_reg = vc->common->bypass_val_reg;
160 vc_bypass_value = (target_vsel << vc->common->data_shift) |
Kevin Hilman78614e02011-03-29 14:24:47 -0700161 (vc->volt_reg_addr << vc->common->regaddr_shift) |
162 (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700163
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700164 voltdm->write(vc_bypass_value, vc_bypass_val_reg);
165 voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700166
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700167 vc_bypass_value = voltdm->read(vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700168 /*
169 * Loop till the bypass command is acknowledged from the SMPS.
170 * NOTE: This is legacy code. The loop count and retry count needs
171 * to be revisited.
172 */
173 while (!(vc_bypass_value & vc_valid)) {
174 loop_cnt++;
175
176 if (retries_cnt > 10) {
177 pr_warning("%s: Retry count exceeded\n", __func__);
178 return -ETIMEDOUT;
179 }
180
181 if (loop_cnt > 50) {
182 retries_cnt++;
183 loop_cnt = 0;
184 udelay(10);
185 }
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700186 vc_bypass_value = voltdm->read(vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700187 }
188
189 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
190 return 0;
191}
192
193static void __init omap3_vfsm_init(struct voltagedomain *voltdm)
194{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700195 /*
196 * Voltage Manager FSM parameters init
197 * XXX This data should be passed in from the board file
198 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700199 voltdm->write(OMAP3_CLKSETUP, OMAP3_PRM_CLKSETUP_OFFSET);
200 voltdm->write(OMAP3_VOLTOFFSET, OMAP3_PRM_VOLTOFFSET_OFFSET);
201 voltdm->write(OMAP3_VOLTSETUP2, OMAP3_PRM_VOLTSETUP2_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700202}
203
204static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
205{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700206 static bool is_initialized;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700207
208 if (is_initialized)
209 return;
210
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700211 omap3_vfsm_init(voltdm);
212
213 is_initialized = true;
214}
215
216
217/* OMAP4 specific voltage init functions */
218static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
219{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700220 static bool is_initialized;
221 u32 vc_val;
222
223 if (is_initialized)
224 return;
225
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700226 /* XXX These are magic numbers and do not belong! */
227 vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700228 voltdm->write(vc_val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700229
230 is_initialized = true;
231}
232
Kevin Hilmanf5395482011-03-30 16:36:30 -0700233/**
234 * omap_vc_i2c_init - initialize I2C interface to PMIC
235 * @voltdm: voltage domain containing VC data
236 *
237 * Use PMIC supplied seetings for I2C high-speed mode and
238 * master code (if set) and program the VC I2C configuration
239 * register.
240 *
241 * The VC I2C configuration is common to all VC channels,
242 * so this function only configures I2C for the first VC
243 * channel registers. All other VC channels will use the
244 * same configuration.
245 */
246static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
247{
248 struct omap_vc_channel *vc = voltdm->vc;
249 static bool initialized;
250 static bool i2c_high_speed;
251 u8 mcode;
252
253 if (initialized) {
254 if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
255 pr_warn("%s: I2C config for all channels must match.",
256 __func__);
257 return;
258 }
259
260 i2c_high_speed = voltdm->pmic->i2c_high_speed;
261 if (i2c_high_speed)
262 voltdm->rmw(vc->common->i2c_cfg_hsen_mask,
263 vc->common->i2c_cfg_hsen_mask,
264 vc->common->i2c_cfg_reg);
265
266 mcode = voltdm->pmic->i2c_mcode;
267 if (mcode)
268 voltdm->rmw(vc->common->i2c_mcode_mask,
269 mcode << __ffs(vc->common->i2c_mcode_mask),
270 vc->common->i2c_cfg_reg);
271
272 initialized = true;
273}
274
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700275void __init omap_vc_init_channel(struct voltagedomain *voltdm)
276{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700277 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700278 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
279 u32 val;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700280
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700281 if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700282 pr_err("%s: PMIC info requried to configure vc for"
283 "vdd_%s not populated.Hence cannot initialize vc\n",
284 __func__, voltdm->name);
285 return;
286 }
287
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700288 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700289 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
290 __func__, voltdm->name);
291 return;
292 }
293
Kevin Hilman24d31942011-03-29 15:57:16 -0700294 vc->cfg_channel = 0;
295
Kevin Hilmanba112a42011-03-29 14:02:36 -0700296 /* get PMIC/board specific settings */
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700297 vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
298 vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
299 vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
300 vc->setup_time = voltdm->pmic->volt_setup_time;
Kevin Hilmanba112a42011-03-29 14:02:36 -0700301
302 /* Configure the i2c slave address for this VC */
303 voltdm->rmw(vc->smps_sa_mask,
304 vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
305 vc->common->smps_sa_reg);
Kevin Hilman24d31942011-03-29 15:57:16 -0700306 vc->cfg_channel |= CFG_CHANNEL_SA;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700307
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700308 /*
309 * Configure the PMIC register addresses.
310 */
311 voltdm->rmw(vc->smps_volra_mask,
312 vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
313 vc->common->smps_volra_reg);
Kevin Hilman24d31942011-03-29 15:57:16 -0700314 vc->cfg_channel |= CFG_CHANNEL_RAV;
315
316 if (vc->cmd_reg_addr) {
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700317 voltdm->rmw(vc->smps_cmdra_mask,
318 vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
319 vc->common->smps_cmdra_reg);
Kevin Hilman24d31942011-03-29 15:57:16 -0700320 vc->cfg_channel |= CFG_CHANNEL_RAC | CFG_CHANNEL_RACEN;
321 }
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700322
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700323 /* Set up the on, inactive, retention and off voltage */
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700324 on_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->on_volt);
325 onlp_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->onlp_volt);
326 ret_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->ret_volt);
327 off_vsel = voltdm->pmic->uv_to_vsel(voltdm->pmic->off_volt);
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700328 val = ((on_vsel << vc->common->cmd_on_shift) |
329 (onlp_vsel << vc->common->cmd_onlp_shift) |
330 (ret_vsel << vc->common->cmd_ret_shift) |
331 (off_vsel << vc->common->cmd_off_shift));
332 voltdm->write(val, vc->cmdval_reg);
Kevin Hilman24d31942011-03-29 15:57:16 -0700333 vc->cfg_channel |= CFG_CHANNEL_CMD;
334
335 /* Channel configuration */
336 omap_vc_config_channel(voltdm);
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700337
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700338 /* Configure the setup times */
Kevin Hilman5892bb12011-03-29 14:36:04 -0700339 voltdm->rmw(voltdm->vfsm->voltsetup_mask,
340 vc->setup_time << __ffs(voltdm->vfsm->voltsetup_mask),
341 voltdm->vfsm->voltsetup_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700342
Kevin Hilmanf5395482011-03-30 16:36:30 -0700343 omap_vc_i2c_init(voltdm);
344
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700345 if (cpu_is_omap34xx())
346 omap3_vc_init_channel(voltdm);
347 else if (cpu_is_omap44xx())
348 omap4_vc_init_channel(voltdm);
349}
350