blob: 705eb35d7b351f7cf628e70d524bfe6e17e1afa4 [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>
Tony Lindgren4647ca52012-03-08 10:20:14 -080013#include <linux/bug.h>
Tero Kristod68ff972012-09-25 19:33:41 +030014#include <linux/io.h>
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070015
Tero Kristod68ff972012-09-25 19:33:41 +030016#include <asm/div64.h>
17
18#include "iomap.h"
Tony Lindgrendbc04162012-08-31 10:59:07 -070019#include "soc.h"
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070020#include "voltage.h"
21#include "vc.h"
22#include "prm-regbits-34xx.h"
23#include "prm-regbits-44xx.h"
24#include "prm44xx.h"
Tero Kristod68ff972012-09-25 19:33:41 +030025#include "pm.h"
26#include "scrm44xx.h"
Tero Kristo00bd2282012-09-25 19:33:48 +030027#include "control.h"
Kevin Hilmanccd5ca72011-03-21 14:08:55 -070028
Kevin Hilman8abc0b52011-06-02 17:28:13 -070029/**
30 * struct omap_vc_channel_cfg - describe the cfg_channel bitfield
31 * @sa: bit for slave address
32 * @rav: bit for voltage configuration register
33 * @rac: bit for command configuration register
34 * @racen: enable bit for RAC
35 * @cmd: bit for command value set selection
36 *
37 * Channel configuration bits, common for OMAP3+
Kevin Hilman24d31942011-03-29 15:57:16 -070038 * OMAP3 register: PRM_VC_CH_CONF
39 * OMAP4 register: PRM_VC_CFG_CHANNEL
Kevin Hilman8abc0b52011-06-02 17:28:13 -070040 * OMAP5 register: PRM_VC_SMPS_<voltdm>_CONFIG
Kevin Hilman24d31942011-03-29 15:57:16 -070041 */
Kevin Hilman8abc0b52011-06-02 17:28:13 -070042struct omap_vc_channel_cfg {
43 u8 sa;
44 u8 rav;
45 u8 rac;
46 u8 racen;
47 u8 cmd;
48};
49
50static struct omap_vc_channel_cfg vc_default_channel_cfg = {
51 .sa = BIT(0),
52 .rav = BIT(1),
53 .rac = BIT(2),
54 .racen = BIT(3),
55 .cmd = BIT(4),
56};
57
58/*
59 * On OMAP3+, all VC channels have the above default bitfield
60 * configuration, except the OMAP4 MPU channel. This appears
61 * to be a freak accident as every other VC channel has the
62 * default configuration, thus creating a mutant channel config.
63 */
64static struct omap_vc_channel_cfg vc_mutant_channel_cfg = {
65 .sa = BIT(0),
66 .rav = BIT(2),
67 .rac = BIT(3),
68 .racen = BIT(4),
69 .cmd = BIT(1),
70};
71
72static struct omap_vc_channel_cfg *vc_cfg_bits;
Tero Kristo00bd2282012-09-25 19:33:48 +030073
74/* Default I2C trace length on pcb, 6.3cm. Used for capacitance calculations. */
75static u32 sr_i2c_pcb_length = 63;
Kevin Hilman8abc0b52011-06-02 17:28:13 -070076#define CFG_CHANNEL_MASK 0x1f
Kevin Hilman24d31942011-03-29 15:57:16 -070077
78/**
79 * omap_vc_config_channel - configure VC channel to PMIC mappings
80 * @voltdm: pointer to voltagdomain defining the desired VC channel
81 *
82 * Configures the VC channel to PMIC mappings for the following
83 * PMIC settings
84 * - i2c slave address (SA)
85 * - voltage configuration address (RAV)
86 * - command configuration address (RAC) and enable bit (RACEN)
87 * - command values for ON, ONLP, RET and OFF (CMD)
88 *
89 * This function currently only allows flexible configuration of the
90 * non-default channel. Starting with OMAP4, there are more than 2
91 * channels, with one defined as the default (on OMAP4, it's MPU.)
92 * Only the non-default channel can be configured.
93 */
94static int omap_vc_config_channel(struct voltagedomain *voltdm)
95{
96 struct omap_vc_channel *vc = voltdm->vc;
97
98 /*
99 * For default channel, the only configurable bit is RACEN.
100 * All others must stay at zero (see function comment above.)
101 */
102 if (vc->flags & OMAP_VC_CHANNEL_DEFAULT)
Kevin Hilman8abc0b52011-06-02 17:28:13 -0700103 vc->cfg_channel &= vc_cfg_bits->racen;
Kevin Hilman24d31942011-03-29 15:57:16 -0700104
105 voltdm->rmw(CFG_CHANNEL_MASK << vc->cfg_channel_sa_shift,
106 vc->cfg_channel << vc->cfg_channel_sa_shift,
Kevin Hilman5876c942011-07-20 16:35:46 -0700107 vc->cfg_channel_reg);
Kevin Hilman24d31942011-03-29 15:57:16 -0700108
109 return 0;
110}
111
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700112/* Voltage scale and accessory APIs */
113int omap_vc_pre_scale(struct voltagedomain *voltdm,
114 unsigned long target_volt,
115 u8 *target_vsel, u8 *current_vsel)
116{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700117 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilman76ea7422011-04-05 15:15:31 -0700118 u32 vc_cmdval;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700119
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700120 /* Check if sufficient pmic info is available for this vdd */
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700121 if (!voltdm->pmic) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700122 pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
123 __func__, voltdm->name);
124 return -EINVAL;
125 }
126
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700127 if (!voltdm->pmic->uv_to_vsel) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600128 pr_err("%s: PMIC function to convert voltage in uV to vsel not registered. Hence unable to scale voltage for vdd_%s\n",
129 __func__, voltdm->name);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700130 return -ENODATA;
131 }
132
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700133 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700134 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
135 __func__, voltdm->name);
136 return -EINVAL;
137 }
138
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700139 *target_vsel = voltdm->pmic->uv_to_vsel(target_volt);
Kevin Hilman7590f602011-04-05 16:55:22 -0700140 *current_vsel = voltdm->pmic->uv_to_vsel(voltdm->nominal_volt);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700141
142 /* Setting the ON voltage to the new target voltage */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700143 vc_cmdval = voltdm->read(vc->cmdval_reg);
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700144 vc_cmdval &= ~vc->common->cmd_on_mask;
145 vc_cmdval |= (*target_vsel << vc->common->cmd_on_shift);
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700146 voltdm->write(vc_cmdval, vc->cmdval_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700147
Tero Kristo8b5d8c02012-09-25 19:33:35 +0300148 voltdm->vc_param->on = target_volt;
149
Kevin Hilman76ea7422011-04-05 15:15:31 -0700150 omap_vp_update_errorgain(voltdm, target_volt);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700151
152 return 0;
153}
154
155void omap_vc_post_scale(struct voltagedomain *voltdm,
156 unsigned long target_volt,
157 u8 target_vsel, u8 current_vsel)
158{
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700159 u32 smps_steps = 0, smps_delay = 0;
160
161 smps_steps = abs(target_vsel - current_vsel);
162 /* SMPS slew rate / step size. 2us added as buffer. */
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700163 smps_delay = ((smps_steps * voltdm->pmic->step_size) /
164 voltdm->pmic->slew_rate) + 2;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700165 udelay(smps_delay);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700166}
167
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700168/* vc_bypass_scale - VC bypass method of voltage scaling */
169int omap_vc_bypass_scale(struct voltagedomain *voltdm,
170 unsigned long target_volt)
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700171{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700172 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700173 u32 loop_cnt = 0, retries_cnt = 0;
174 u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
175 u8 target_vsel, current_vsel;
176 int ret;
177
178 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
179 if (ret)
180 return ret;
181
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700182 vc_valid = vc->common->valid;
183 vc_bypass_val_reg = vc->common->bypass_val_reg;
184 vc_bypass_value = (target_vsel << vc->common->data_shift) |
Kevin Hilman78614e02011-03-29 14:24:47 -0700185 (vc->volt_reg_addr << vc->common->regaddr_shift) |
186 (vc->i2c_slave_addr << vc->common->slaveaddr_shift);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700187
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700188 voltdm->write(vc_bypass_value, vc_bypass_val_reg);
189 voltdm->write(vc_bypass_value | vc_valid, vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700190
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700191 vc_bypass_value = voltdm->read(vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700192 /*
193 * Loop till the bypass command is acknowledged from the SMPS.
194 * NOTE: This is legacy code. The loop count and retry count needs
195 * to be revisited.
196 */
197 while (!(vc_bypass_value & vc_valid)) {
198 loop_cnt++;
199
200 if (retries_cnt > 10) {
201 pr_warning("%s: Retry count exceeded\n", __func__);
202 return -ETIMEDOUT;
203 }
204
205 if (loop_cnt > 50) {
206 retries_cnt++;
207 loop_cnt = 0;
208 udelay(10);
209 }
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700210 vc_bypass_value = voltdm->read(vc_bypass_val_reg);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700211 }
212
213 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
214 return 0;
215}
216
Tero Kristod68ff972012-09-25 19:33:41 +0300217/* Convert microsecond value to number of 32kHz clock cycles */
218static inline u32 omap_usec_to_32k(u32 usec)
219{
220 return DIV_ROUND_UP_ULL(32768ULL * (u64)usec, 1000000ULL);
221}
222
Tony Lindgren3b8c4eb2014-05-05 17:27:35 -0700223struct omap3_vc {
224 struct voltagedomain *vd;
225 u32 voltctrl;
226};
227static struct omap3_vc vc;
228
229void omap3_vc_set_pmic_signaling(int core_next_state)
230{
231 struct voltagedomain *vd = vc.vd;
232 u32 voltctrl;
233
234 voltctrl = vc.voltctrl;
235 switch (core_next_state) {
236 case PWRDM_POWER_OFF:
237 voltctrl &= ~(OMAP3430_PRM_VOLTCTRL_AUTO_RET |
238 OMAP3430_PRM_VOLTCTRL_AUTO_SLEEP);
239 voltctrl |= OMAP3430_PRM_VOLTCTRL_AUTO_OFF;
240 break;
241 case PWRDM_POWER_RET:
242 default:
243 voltctrl &= ~(OMAP3430_PRM_VOLTCTRL_AUTO_OFF |
244 OMAP3430_PRM_VOLTCTRL_AUTO_SLEEP);
245 voltctrl |= OMAP3430_PRM_VOLTCTRL_AUTO_RET;
246 break;
247 }
248 if (voltctrl != vc.voltctrl) {
249 vd->write(voltctrl, OMAP3_PRM_VOLTCTRL_OFFSET);
250 vc.voltctrl = voltctrl;
251 }
252}
253
254#define PRM_POLCTRL_TWL_MASK (OMAP3430_PRM_POLCTRL_CLKREQ_POL | \
255 OMAP3430_PRM_POLCTRL_CLKREQ_POL)
256#define PRM_POLCTRL_TWL_VAL OMAP3430_PRM_POLCTRL_CLKREQ_POL
257
258/*
259 * Configure signal polarity for sys_clkreq and sys_off_mode pins
260 * as the default values are wrong and can cause the system to hang
261 * if any twl4030 scripts are loaded.
262 */
263static void __init omap3_vc_init_pmic_signaling(struct voltagedomain *voltdm)
264{
265 u32 val;
266
267 if (vc.vd)
268 return;
269
270 vc.vd = voltdm;
271
272 val = voltdm->read(OMAP3_PRM_POLCTRL_OFFSET);
273 if (!(val & OMAP3430_PRM_POLCTRL_CLKREQ_POL) ||
274 (val & OMAP3430_PRM_POLCTRL_CLKREQ_POL)) {
275 val |= OMAP3430_PRM_POLCTRL_CLKREQ_POL;
276 val &= ~OMAP3430_PRM_POLCTRL_OFFMODE_POL;
277 pr_debug("PM: fixing sys_clkreq and sys_off_mode polarity to 0x%x\n",
278 val);
279 voltdm->write(val, OMAP3_PRM_POLCTRL_OFFSET);
280 }
281
282 /*
283 * By default let's use I2C4 signaling for retention idle
284 * and sys_off_mode pin signaling for off idle. This way we
285 * have sys_clk_req pin go down for retention and both
286 * sys_clk_req and sys_off_mode pins will go down for off
287 * idle. And we can also scale voltages to zero for off-idle.
288 * Note that no actual voltage scaling during off-idle will
289 * happen unless the board specific twl4030 PMIC scripts are
290 * loaded.
291 */
292 val = voltdm->read(OMAP3_PRM_VOLTCTRL_OFFSET);
293 if (!(val & OMAP3430_PRM_VOLTCTRL_SEL_OFF)) {
294 val |= OMAP3430_PRM_VOLTCTRL_SEL_OFF;
295 pr_debug("PM: setting voltctrl sys_off_mode signaling to 0x%x\n",
296 val);
297 voltdm->write(val, OMAP3_PRM_VOLTCTRL_OFFSET);
298 }
299 vc.voltctrl = val;
300
301 omap3_vc_set_pmic_signaling(PWRDM_POWER_ON);
302}
303
Tero Kristod68ff972012-09-25 19:33:41 +0300304/* Set oscillator setup time for omap3 */
305static void omap3_set_clksetup(u32 usec, struct voltagedomain *voltdm)
306{
307 voltdm->write(omap_usec_to_32k(usec), OMAP3_PRM_CLKSETUP_OFFSET);
308}
309
Tero Kristoc589eb32012-09-25 19:33:36 +0300310/**
311 * omap3_set_i2c_timings - sets i2c sleep timings for a channel
312 * @voltdm: channel to configure
313 * @off_mode: select whether retention or off mode values used
314 *
315 * Calculates and sets up voltage controller to use I2C based
316 * voltage scaling for sleep modes. This can be used for either off mode
317 * or retention. Off mode has additionally an option to use sys_off_mode
318 * pad, which uses a global signal to program the whole power IC to
319 * off-mode.
320 */
321static void omap3_set_i2c_timings(struct voltagedomain *voltdm, bool off_mode)
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700322{
Tero Kristoc589eb32012-09-25 19:33:36 +0300323 unsigned long voltsetup1;
324 u32 tgt_volt;
325
Tero Kristod68ff972012-09-25 19:33:41 +0300326 /*
327 * Oscillator is shut down only if we are using sys_off_mode pad,
328 * thus we set a minimal setup time here
329 */
330 omap3_set_clksetup(1, voltdm);
331
Tero Kristoc589eb32012-09-25 19:33:36 +0300332 if (off_mode)
333 tgt_volt = voltdm->vc_param->off;
334 else
335 tgt_volt = voltdm->vc_param->ret;
336
337 voltsetup1 = (voltdm->vc_param->on - tgt_volt) /
338 voltdm->pmic->slew_rate;
339
340 voltsetup1 = voltsetup1 * voltdm->sys_clk.rate / 8 / 1000000 + 1;
341
342 voltdm->rmw(voltdm->vfsm->voltsetup_mask,
343 voltsetup1 << __ffs(voltdm->vfsm->voltsetup_mask),
344 voltdm->vfsm->voltsetup_reg);
345
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700346 /*
Tero Kristoc589eb32012-09-25 19:33:36 +0300347 * pmic is not controlling the voltage scaling during retention,
348 * thus set voltsetup2 to 0
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700349 */
Tero Kristoc589eb32012-09-25 19:33:36 +0300350 voltdm->write(0, OMAP3_PRM_VOLTSETUP2_OFFSET);
351}
352
353/**
354 * omap3_set_off_timings - sets off-mode timings for a channel
355 * @voltdm: channel to configure
356 *
357 * Calculates and sets up off-mode timings for a channel. Off-mode
358 * can use either I2C based voltage scaling, or alternatively
359 * sys_off_mode pad can be used to send a global command to power IC.
360 * This function first checks which mode is being used, and calls
361 * omap3_set_i2c_timings() if the system is using I2C control mode.
362 * sys_off_mode has the additional benefit that voltages can be
363 * scaled to zero volt level with TWL4030 / TWL5030, I2C can only
364 * scale to 600mV.
365 */
366static void omap3_set_off_timings(struct voltagedomain *voltdm)
367{
368 unsigned long clksetup;
369 unsigned long voltsetup2;
370 unsigned long voltsetup2_old;
371 u32 val;
Tero Kristod68ff972012-09-25 19:33:41 +0300372 u32 tstart, tshut;
Tero Kristoc589eb32012-09-25 19:33:36 +0300373
374 /* check if sys_off_mode is used to control off-mode voltages */
375 val = voltdm->read(OMAP3_PRM_VOLTCTRL_OFFSET);
Tony Lindgren3b8c4eb2014-05-05 17:27:35 -0700376 if (!(val & OMAP3430_PRM_VOLTCTRL_SEL_OFF)) {
Tero Kristoc589eb32012-09-25 19:33:36 +0300377 /* No, omap is controlling them over I2C */
378 omap3_set_i2c_timings(voltdm, true);
379 return;
380 }
381
Tero Kristod68ff972012-09-25 19:33:41 +0300382 omap_pm_get_oscillator(&tstart, &tshut);
383 omap3_set_clksetup(tstart, voltdm);
384
Tero Kristoc589eb32012-09-25 19:33:36 +0300385 clksetup = voltdm->read(OMAP3_PRM_CLKSETUP_OFFSET);
386
387 /* voltsetup 2 in us */
388 voltsetup2 = voltdm->vc_param->on / voltdm->pmic->slew_rate;
389
390 /* convert to 32k clk cycles */
391 voltsetup2 = DIV_ROUND_UP(voltsetup2 * 32768, 1000000);
392
393 voltsetup2_old = voltdm->read(OMAP3_PRM_VOLTSETUP2_OFFSET);
394
395 /*
396 * Update voltsetup2 if higher than current value (needed because
397 * we have multiple channels with different ramp times), also
398 * update voltoffset always to value recommended by TRM
399 */
400 if (voltsetup2 > voltsetup2_old) {
401 voltdm->write(voltsetup2, OMAP3_PRM_VOLTSETUP2_OFFSET);
402 voltdm->write(clksetup - voltsetup2,
403 OMAP3_PRM_VOLTOFFSET_OFFSET);
404 } else
405 voltdm->write(clksetup - voltsetup2_old,
406 OMAP3_PRM_VOLTOFFSET_OFFSET);
407
408 /*
409 * omap is not controlling voltage scaling during off-mode,
410 * thus set voltsetup1 to 0
411 */
412 voltdm->rmw(voltdm->vfsm->voltsetup_mask, 0,
413 voltdm->vfsm->voltsetup_reg);
414
415 /* voltoffset must be clksetup minus voltsetup2 according to TRM */
416 voltdm->write(clksetup - voltsetup2, OMAP3_PRM_VOLTOFFSET_OFFSET);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700417}
418
419static void __init omap3_vc_init_channel(struct voltagedomain *voltdm)
420{
Tony Lindgren3b8c4eb2014-05-05 17:27:35 -0700421 omap3_vc_init_pmic_signaling(voltdm);
Tero Kristoc589eb32012-09-25 19:33:36 +0300422 omap3_set_off_timings(voltdm);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700423}
424
Tero Kristo9a1729c2012-09-25 19:33:38 +0300425/**
426 * omap4_calc_volt_ramp - calculates voltage ramping delays on omap4
427 * @voltdm: channel to calculate values for
428 * @voltage_diff: voltage difference in microvolts
429 *
430 * Calculates voltage ramp prescaler + counter values for a voltage
431 * difference on omap4. Returns a field value suitable for writing to
432 * VOLTSETUP register for a channel in following format:
433 * bits[8:9] prescaler ... bits[0:5] counter. See OMAP4 TRM for reference.
434 */
435static u32 omap4_calc_volt_ramp(struct voltagedomain *voltdm, u32 voltage_diff)
436{
437 u32 prescaler;
438 u32 cycles;
439 u32 time;
440
441 time = voltage_diff / voltdm->pmic->slew_rate;
442
443 cycles = voltdm->sys_clk.rate / 1000 * time / 1000;
444
445 cycles /= 64;
446 prescaler = 0;
447
448 /* shift to next prescaler until no overflow */
449
450 /* scale for div 256 = 64 * 4 */
451 if (cycles > 63) {
452 cycles /= 4;
453 prescaler++;
454 }
455
456 /* scale for div 512 = 256 * 2 */
457 if (cycles > 63) {
458 cycles /= 2;
459 prescaler++;
460 }
461
462 /* scale for div 2048 = 512 * 4 */
463 if (cycles > 63) {
464 cycles /= 4;
465 prescaler++;
466 }
467
468 /* check for overflow => invalid ramp time */
469 if (cycles > 63) {
470 pr_warn("%s: invalid setuptime for vdd_%s\n", __func__,
471 voltdm->name);
472 return 0;
473 }
474
475 cycles++;
476
477 return (prescaler << OMAP4430_RAMP_UP_PRESCAL_SHIFT) |
478 (cycles << OMAP4430_RAMP_UP_COUNT_SHIFT);
479}
480
481/**
Tero Kristod68ff972012-09-25 19:33:41 +0300482 * omap4_usec_to_val_scrm - convert microsecond value to SCRM module bitfield
483 * @usec: microseconds
484 * @shift: number of bits to shift left
485 * @mask: bitfield mask
486 *
487 * Converts microsecond value to OMAP4 SCRM bitfield. Bitfield is
488 * shifted to requested position, and checked agains the mask value.
489 * If larger, forced to the max value of the field (i.e. the mask itself.)
490 * Returns the SCRM bitfield value.
491 */
492static u32 omap4_usec_to_val_scrm(u32 usec, int shift, u32 mask)
493{
494 u32 val;
495
496 val = omap_usec_to_32k(usec) << shift;
497
498 /* Check for overflow, if yes, force to max value */
499 if (val > mask)
500 val = mask;
501
502 return val;
503}
504
505/**
Tero Kristo9a1729c2012-09-25 19:33:38 +0300506 * omap4_set_timings - set voltage ramp timings for a channel
507 * @voltdm: channel to configure
508 * @off_mode: whether off-mode values are used
509 *
510 * Calculates and sets the voltage ramp up / down values for a channel.
511 */
512static void omap4_set_timings(struct voltagedomain *voltdm, bool off_mode)
513{
514 u32 val;
515 u32 ramp;
516 int offset;
Tero Kristod68ff972012-09-25 19:33:41 +0300517 u32 tstart, tshut;
Tero Kristo9a1729c2012-09-25 19:33:38 +0300518
519 if (off_mode) {
520 ramp = omap4_calc_volt_ramp(voltdm,
521 voltdm->vc_param->on - voltdm->vc_param->off);
522 offset = voltdm->vfsm->voltsetup_off_reg;
523 } else {
524 ramp = omap4_calc_volt_ramp(voltdm,
525 voltdm->vc_param->on - voltdm->vc_param->ret);
526 offset = voltdm->vfsm->voltsetup_reg;
527 }
528
529 if (!ramp)
530 return;
531
532 val = voltdm->read(offset);
533
534 val |= ramp << OMAP4430_RAMP_DOWN_COUNT_SHIFT;
535
536 val |= ramp << OMAP4430_RAMP_UP_COUNT_SHIFT;
537
538 voltdm->write(val, offset);
Tero Kristod68ff972012-09-25 19:33:41 +0300539
540 omap_pm_get_oscillator(&tstart, &tshut);
541
542 val = omap4_usec_to_val_scrm(tstart, OMAP4_SETUPTIME_SHIFT,
543 OMAP4_SETUPTIME_MASK);
544 val |= omap4_usec_to_val_scrm(tshut, OMAP4_DOWNTIME_SHIFT,
545 OMAP4_DOWNTIME_MASK);
546
547 __raw_writel(val, OMAP4_SCRM_CLKSETUPTIME);
Tero Kristo9a1729c2012-09-25 19:33:38 +0300548}
549
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700550/* OMAP4 specific voltage init functions */
551static void __init omap4_vc_init_channel(struct voltagedomain *voltdm)
552{
Tero Kristo9a1729c2012-09-25 19:33:38 +0300553 omap4_set_timings(voltdm, true);
554 omap4_set_timings(voltdm, false);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700555}
556
Tero Kristo00bd2282012-09-25 19:33:48 +0300557struct i2c_init_data {
558 u8 loadbits;
559 u8 load;
560 u8 hsscll_38_4;
561 u8 hsscll_26;
562 u8 hsscll_19_2;
563 u8 hsscll_16_8;
564 u8 hsscll_12;
565};
566
567static const __initdata struct i2c_init_data omap4_i2c_timing_data[] = {
568 {
569 .load = 50,
570 .loadbits = 0x3,
571 .hsscll_38_4 = 13,
572 .hsscll_26 = 11,
573 .hsscll_19_2 = 9,
574 .hsscll_16_8 = 9,
575 .hsscll_12 = 8,
576 },
577 {
578 .load = 25,
579 .loadbits = 0x2,
580 .hsscll_38_4 = 13,
581 .hsscll_26 = 11,
582 .hsscll_19_2 = 9,
583 .hsscll_16_8 = 9,
584 .hsscll_12 = 8,
585 },
586 {
587 .load = 12,
588 .loadbits = 0x1,
589 .hsscll_38_4 = 11,
590 .hsscll_26 = 10,
591 .hsscll_19_2 = 9,
592 .hsscll_16_8 = 9,
593 .hsscll_12 = 8,
594 },
595 {
596 .load = 0,
597 .loadbits = 0x0,
598 .hsscll_38_4 = 12,
599 .hsscll_26 = 10,
600 .hsscll_19_2 = 9,
601 .hsscll_16_8 = 8,
602 .hsscll_12 = 8,
603 },
604};
605
606/**
607 * omap4_vc_i2c_timing_init - sets up board I2C timing parameters
608 * @voltdm: voltagedomain pointer to get data from
609 *
610 * Use PMIC + board supplied settings for calculating the total I2C
611 * channel capacitance and set the timing parameters based on this.
612 * Pre-calculated values are provided in data tables, as it is not
613 * too straightforward to calculate these runtime.
614 */
615static void __init omap4_vc_i2c_timing_init(struct voltagedomain *voltdm)
616{
617 u32 capacitance;
618 u32 val;
619 u16 hsscll;
620 const struct i2c_init_data *i2c_data;
621
622 if (!voltdm->pmic->i2c_high_speed) {
623 pr_warn("%s: only high speed supported!\n", __func__);
624 return;
625 }
626
627 /* PCB trace capacitance, 0.125pF / mm => mm / 8 */
628 capacitance = DIV_ROUND_UP(sr_i2c_pcb_length, 8);
629
630 /* OMAP pad capacitance */
631 capacitance += 4;
632
633 /* PMIC pad capacitance */
634 capacitance += voltdm->pmic->i2c_pad_load;
635
636 /* Search for capacitance match in the table */
637 i2c_data = omap4_i2c_timing_data;
638
639 while (i2c_data->load > capacitance)
640 i2c_data++;
641
642 /* Select proper values based on sysclk frequency */
643 switch (voltdm->sys_clk.rate) {
644 case 38400000:
645 hsscll = i2c_data->hsscll_38_4;
646 break;
647 case 26000000:
648 hsscll = i2c_data->hsscll_26;
649 break;
650 case 19200000:
651 hsscll = i2c_data->hsscll_19_2;
652 break;
653 case 16800000:
654 hsscll = i2c_data->hsscll_16_8;
655 break;
656 case 12000000:
657 hsscll = i2c_data->hsscll_12;
658 break;
659 default:
660 pr_warn("%s: unsupported sysclk rate: %d!\n", __func__,
661 voltdm->sys_clk.rate);
662 return;
663 }
664
665 /* Loadbits define pull setup for the I2C channels */
666 val = i2c_data->loadbits << 25 | i2c_data->loadbits << 29;
667
668 /* Write to SYSCTRL_PADCONF_WKUP_CTRL_I2C_2 to setup I2C pull */
669 __raw_writel(val, OMAP2_L4_IO_ADDRESS(OMAP4_CTRL_MODULE_PAD_WKUP +
670 OMAP4_CTRL_MODULE_PAD_WKUP_CONTROL_I2C_2));
671
672 /* HSSCLH can always be zero */
673 val = hsscll << OMAP4430_HSSCLL_SHIFT;
674 val |= (0x28 << OMAP4430_SCLL_SHIFT | 0x2c << OMAP4430_SCLH_SHIFT);
675
676 /* Write setup times to I2C config register */
677 voltdm->write(val, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
678}
679
680
681
Kevin Hilmanf5395482011-03-30 16:36:30 -0700682/**
683 * omap_vc_i2c_init - initialize I2C interface to PMIC
684 * @voltdm: voltage domain containing VC data
685 *
Russell King2d5b4792012-02-07 10:13:02 +0000686 * Use PMIC supplied settings for I2C high-speed mode and
Kevin Hilmanf5395482011-03-30 16:36:30 -0700687 * master code (if set) and program the VC I2C configuration
688 * register.
689 *
690 * The VC I2C configuration is common to all VC channels,
691 * so this function only configures I2C for the first VC
692 * channel registers. All other VC channels will use the
693 * same configuration.
694 */
695static void __init omap_vc_i2c_init(struct voltagedomain *voltdm)
696{
697 struct omap_vc_channel *vc = voltdm->vc;
698 static bool initialized;
699 static bool i2c_high_speed;
700 u8 mcode;
701
702 if (initialized) {
703 if (voltdm->pmic->i2c_high_speed != i2c_high_speed)
Kevin Hilman43993e42012-10-25 10:30:34 -0700704 pr_warn("%s: I2C config for vdd_%s does not match other channels (%u).\n",
Russell King0bf68f52012-02-07 10:23:43 +0000705 __func__, voltdm->name, i2c_high_speed);
Kevin Hilmanf5395482011-03-30 16:36:30 -0700706 return;
707 }
708
709 i2c_high_speed = voltdm->pmic->i2c_high_speed;
710 if (i2c_high_speed)
711 voltdm->rmw(vc->common->i2c_cfg_hsen_mask,
712 vc->common->i2c_cfg_hsen_mask,
713 vc->common->i2c_cfg_reg);
714
715 mcode = voltdm->pmic->i2c_mcode;
716 if (mcode)
717 voltdm->rmw(vc->common->i2c_mcode_mask,
718 mcode << __ffs(vc->common->i2c_mcode_mask),
719 vc->common->i2c_cfg_reg);
720
Tero Kristo00bd2282012-09-25 19:33:48 +0300721 if (cpu_is_omap44xx())
722 omap4_vc_i2c_timing_init(voltdm);
723
Kevin Hilmanf5395482011-03-30 16:36:30 -0700724 initialized = true;
725}
726
Tero Kristo8b5d8c02012-09-25 19:33:35 +0300727/**
728 * omap_vc_calc_vsel - calculate vsel value for a channel
729 * @voltdm: channel to calculate value for
730 * @uvolt: microvolt value to convert to vsel
731 *
732 * Converts a microvolt value to vsel value for the used PMIC.
733 * This checks whether the microvolt value is out of bounds, and
734 * adjusts the value accordingly. If unsupported value detected,
735 * warning is thrown.
736 */
737static u8 omap_vc_calc_vsel(struct voltagedomain *voltdm, u32 uvolt)
738{
739 if (voltdm->pmic->vddmin > uvolt)
740 uvolt = voltdm->pmic->vddmin;
741 if (voltdm->pmic->vddmax < uvolt) {
742 WARN(1, "%s: voltage not supported by pmic: %u vs max %u\n",
743 __func__, uvolt, voltdm->pmic->vddmax);
744 /* Lets try maximum value anyway */
745 uvolt = voltdm->pmic->vddmax;
746 }
747
748 return voltdm->pmic->uv_to_vsel(uvolt);
749}
750
Kevin Hilman74d29162012-11-14 17:13:04 -0800751#ifdef CONFIG_PM
Tero Kristo00bd2282012-09-25 19:33:48 +0300752/**
753 * omap_pm_setup_sr_i2c_pcb_length - set length of SR I2C traces on PCB
754 * @mm: length of the PCB trace in millimetres
755 *
756 * Sets the PCB trace length for the I2C channel. By default uses 63mm.
757 * This is needed for properly calculating the capacitance value for
758 * the PCB trace, and for setting the SR I2C channel timing parameters.
759 */
760void __init omap_pm_setup_sr_i2c_pcb_length(u32 mm)
761{
762 sr_i2c_pcb_length = mm;
763}
Kevin Hilman74d29162012-11-14 17:13:04 -0800764#endif
Tero Kristo00bd2282012-09-25 19:33:48 +0300765
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700766void __init omap_vc_init_channel(struct voltagedomain *voltdm)
767{
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700768 struct omap_vc_channel *vc = voltdm->vc;
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700769 u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
770 u32 val;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700771
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700772 if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
Russell King2d5b4792012-02-07 10:13:02 +0000773 pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700774 return;
775 }
776
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700777 if (!voltdm->read || !voltdm->write) {
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700778 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
779 __func__, voltdm->name);
780 return;
781 }
782
Kevin Hilman24d31942011-03-29 15:57:16 -0700783 vc->cfg_channel = 0;
Kevin Hilman8abc0b52011-06-02 17:28:13 -0700784 if (vc->flags & OMAP_VC_CHANNEL_CFG_MUTANT)
785 vc_cfg_bits = &vc_mutant_channel_cfg;
786 else
787 vc_cfg_bits = &vc_default_channel_cfg;
Kevin Hilman24d31942011-03-29 15:57:16 -0700788
Kevin Hilmanba112a42011-03-29 14:02:36 -0700789 /* get PMIC/board specific settings */
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700790 vc->i2c_slave_addr = voltdm->pmic->i2c_slave_addr;
791 vc->volt_reg_addr = voltdm->pmic->volt_reg_addr;
792 vc->cmd_reg_addr = voltdm->pmic->cmd_reg_addr;
Kevin Hilmanba112a42011-03-29 14:02:36 -0700793
794 /* Configure the i2c slave address for this VC */
795 voltdm->rmw(vc->smps_sa_mask,
796 vc->i2c_slave_addr << __ffs(vc->smps_sa_mask),
Kevin Hilman5876c942011-07-20 16:35:46 -0700797 vc->smps_sa_reg);
Kevin Hilman8abc0b52011-06-02 17:28:13 -0700798 vc->cfg_channel |= vc_cfg_bits->sa;
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700799
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700800 /*
801 * Configure the PMIC register addresses.
802 */
803 voltdm->rmw(vc->smps_volra_mask,
804 vc->volt_reg_addr << __ffs(vc->smps_volra_mask),
Kevin Hilman5876c942011-07-20 16:35:46 -0700805 vc->smps_volra_reg);
Kevin Hilman8abc0b52011-06-02 17:28:13 -0700806 vc->cfg_channel |= vc_cfg_bits->rav;
Kevin Hilman24d31942011-03-29 15:57:16 -0700807
808 if (vc->cmd_reg_addr) {
Kevin Hilmane4e021c2011-06-09 11:01:55 -0700809 voltdm->rmw(vc->smps_cmdra_mask,
810 vc->cmd_reg_addr << __ffs(vc->smps_cmdra_mask),
Kevin Hilman5876c942011-07-20 16:35:46 -0700811 vc->smps_cmdra_reg);
Tero Kristo2ceec7b2012-09-25 19:33:47 +0300812 vc->cfg_channel |= vc_cfg_bits->rac;
Kevin Hilman24d31942011-03-29 15:57:16 -0700813 }
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700814
Tero Kristo2ceec7b2012-09-25 19:33:47 +0300815 if (vc->cmd_reg_addr == vc->volt_reg_addr)
816 vc->cfg_channel |= vc_cfg_bits->racen;
817
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700818 /* Set up the on, inactive, retention and off voltage */
Tero Kristo8b5d8c02012-09-25 19:33:35 +0300819 on_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->on);
820 onlp_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->onlp);
821 ret_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->ret);
822 off_vsel = omap_vc_calc_vsel(voltdm, voltdm->vc_param->off);
823
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700824 val = ((on_vsel << vc->common->cmd_on_shift) |
825 (onlp_vsel << vc->common->cmd_onlp_shift) |
826 (ret_vsel << vc->common->cmd_ret_shift) |
827 (off_vsel << vc->common->cmd_off_shift));
828 voltdm->write(val, vc->cmdval_reg);
Kevin Hilman8abc0b52011-06-02 17:28:13 -0700829 vc->cfg_channel |= vc_cfg_bits->cmd;
Kevin Hilman24d31942011-03-29 15:57:16 -0700830
831 /* Channel configuration */
832 omap_vc_config_channel(voltdm);
Kevin Hilman08d1c9a2011-03-29 15:14:38 -0700833
Kevin Hilmanf5395482011-03-30 16:36:30 -0700834 omap_vc_i2c_init(voltdm);
835
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700836 if (cpu_is_omap34xx())
837 omap3_vc_init_channel(voltdm);
838 else if (cpu_is_omap44xx())
839 omap4_vc_init_channel(voltdm);
840}
841