Merge branch 'exec_rm_compat' of git://git.kernel.org/pub/scm/linux/kernel/git/oleg...
[pandora-kernel.git] / arch / arm / mach-omap2 / voltage.c
1 /*
2  * OMAP3/OMAP4 Voltage Management Routines
3  *
4  * Author: Thara Gopinath       <thara@ti.com>
5  *
6  * Copyright (C) 2007 Texas Instruments, Inc.
7  * Rajendra Nayak <rnayak@ti.com>
8  * Lesly A M <x0080970@ti.com>
9  *
10  * Copyright (C) 2008, 2011 Nokia Corporation
11  * Kalle Jokiniemi
12  * Paul Walmsley
13  *
14  * Copyright (C) 2010 Texas Instruments, Inc.
15  * Thara Gopinath <thara@ti.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License version 2 as
19  * published by the Free Software Foundation.
20  */
21
22 #include <linux/delay.h>
23 #include <linux/io.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/debugfs.h>
27 #include <linux/slab.h>
28
29 #include <plat/common.h>
30
31 #include "prm-regbits-34xx.h"
32 #include "prm-regbits-44xx.h"
33 #include "prm44xx.h"
34 #include "prcm44xx.h"
35 #include "prminst44xx.h"
36 #include "control.h"
37
38 #include "voltage.h"
39
40 #include "vc.h"
41 #include "vp.h"
42
43 #define VOLTAGE_DIR_SIZE        16
44
45
46 static struct omap_vdd_info **vdd_info;
47
48 /*
49  * Number of scalable voltage domains.
50  */
51 static int nr_scalable_vdd;
52
53 /* XXX document */
54 static s16 prm_mod_offs;
55 static s16 prm_irqst_ocp_mod_offs;
56
57 static struct dentry *voltage_dir;
58
59 /* Init function pointers */
60 static int vp_forceupdate_scale_voltage(struct omap_vdd_info *vdd,
61                                         unsigned long target_volt);
62
63 static u32 omap3_voltage_read_reg(u16 mod, u8 offset)
64 {
65         return omap2_prm_read_mod_reg(mod, offset);
66 }
67
68 static void omap3_voltage_write_reg(u32 val, u16 mod, u8 offset)
69 {
70         omap2_prm_write_mod_reg(val, mod, offset);
71 }
72
73 static u32 omap4_voltage_read_reg(u16 mod, u8 offset)
74 {
75         return omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
76                                         mod, offset);
77 }
78
79 static void omap4_voltage_write_reg(u32 val, u16 mod, u8 offset)
80 {
81         omap4_prminst_write_inst_reg(val, OMAP4430_PRM_PARTITION, mod, offset);
82 }
83
84 static int __init _config_common_vdd_data(struct omap_vdd_info *vdd)
85 {
86         char *sys_ck_name;
87         struct clk *sys_ck;
88         u32 sys_clk_speed, timeout_val, waittime;
89
90         /*
91          * XXX Clockfw should handle this, or this should be in a
92          * struct record
93          */
94         if (cpu_is_omap24xx() || cpu_is_omap34xx())
95                 sys_ck_name = "sys_ck";
96         else if (cpu_is_omap44xx())
97                 sys_ck_name = "sys_clkin_ck";
98         else
99                 return -EINVAL;
100
101         /*
102          * Sys clk rate is require to calculate vp timeout value and
103          * smpswaittimemin and smpswaittimemax.
104          */
105         sys_ck = clk_get(NULL, sys_ck_name);
106         if (IS_ERR(sys_ck)) {
107                 pr_warning("%s: Could not get the sys clk to calculate"
108                         "various vdd_%s params\n", __func__, vdd->voltdm.name);
109                 return -EINVAL;
110         }
111         sys_clk_speed = clk_get_rate(sys_ck);
112         clk_put(sys_ck);
113         /* Divide to avoid overflow */
114         sys_clk_speed /= 1000;
115
116         /* Generic voltage parameters */
117         vdd->volt_scale = vp_forceupdate_scale_voltage;
118         vdd->vp_enabled = false;
119
120         vdd->vp_rt_data.vpconfig_erroroffset =
121                 (vdd->pmic_info->vp_erroroffset <<
122                  vdd->vp_data->vp_common->vpconfig_erroroffset_shift);
123
124         timeout_val = (sys_clk_speed * vdd->pmic_info->vp_timeout_us) / 1000;
125         vdd->vp_rt_data.vlimitto_timeout = timeout_val;
126         vdd->vp_rt_data.vlimitto_vddmin = vdd->pmic_info->vp_vddmin;
127         vdd->vp_rt_data.vlimitto_vddmax = vdd->pmic_info->vp_vddmax;
128
129         waittime = ((vdd->pmic_info->step_size / vdd->pmic_info->slew_rate) *
130                                 sys_clk_speed) / 1000;
131         vdd->vp_rt_data.vstepmin_smpswaittimemin = waittime;
132         vdd->vp_rt_data.vstepmax_smpswaittimemax = waittime;
133         vdd->vp_rt_data.vstepmin_stepmin = vdd->pmic_info->vp_vstepmin;
134         vdd->vp_rt_data.vstepmax_stepmax = vdd->pmic_info->vp_vstepmax;
135
136         return 0;
137 }
138
139 /* Voltage debugfs support */
140 static int vp_volt_debug_get(void *data, u64 *val)
141 {
142         struct omap_vdd_info *vdd = (struct omap_vdd_info *) data;
143         u8 vsel;
144
145         if (!vdd) {
146                 pr_warning("Wrong paramater passed\n");
147                 return -EINVAL;
148         }
149
150         vsel = vdd->read_reg(prm_mod_offs, vdd->vp_data->voltage);
151         pr_notice("curr_vsel = %x\n", vsel);
152
153         if (!vdd->pmic_info->vsel_to_uv) {
154                 pr_warning("PMIC function to convert vsel to voltage"
155                         "in uV not registerd\n");
156                 return -EINVAL;
157         }
158
159         *val = vdd->pmic_info->vsel_to_uv(vsel);
160         return 0;
161 }
162
163 static int nom_volt_debug_get(void *data, u64 *val)
164 {
165         struct omap_vdd_info *vdd = (struct omap_vdd_info *) data;
166
167         if (!vdd) {
168                 pr_warning("Wrong paramater passed\n");
169                 return -EINVAL;
170         }
171
172         *val = omap_voltage_get_nom_volt(&vdd->voltdm);
173
174         return 0;
175 }
176
177 DEFINE_SIMPLE_ATTRIBUTE(vp_volt_debug_fops, vp_volt_debug_get, NULL, "%llu\n");
178 DEFINE_SIMPLE_ATTRIBUTE(nom_volt_debug_fops, nom_volt_debug_get, NULL,
179                                                                 "%llu\n");
180 static void vp_latch_vsel(struct omap_vdd_info *vdd)
181 {
182         u32 vpconfig;
183         unsigned long uvdc;
184         char vsel;
185
186         uvdc = omap_voltage_get_nom_volt(&vdd->voltdm);
187         if (!uvdc) {
188                 pr_warning("%s: unable to find current voltage for vdd_%s\n",
189                         __func__, vdd->voltdm.name);
190                 return;
191         }
192
193         if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
194                 pr_warning("%s: PMIC function to convert voltage in uV to"
195                         " vsel not registered\n", __func__);
196                 return;
197         }
198
199         vsel = vdd->pmic_info->uv_to_vsel(uvdc);
200
201         vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
202         vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvoltage_mask |
203                         vdd->vp_data->vp_common->vpconfig_initvdd);
204         vpconfig |= vsel << vdd->vp_data->vp_common->vpconfig_initvoltage_shift;
205
206         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
207
208         /* Trigger initVDD value copy to voltage processor */
209         vdd->write_reg((vpconfig | vdd->vp_data->vp_common->vpconfig_initvdd),
210                        prm_mod_offs, vdd->vp_data->vpconfig);
211
212         /* Clear initVDD copy trigger bit */
213         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
214 }
215
216 /* Generic voltage init functions */
217 static void __init vp_init(struct omap_vdd_info *vdd)
218 {
219         u32 vp_val;
220
221         if (!vdd->read_reg || !vdd->write_reg) {
222                 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
223                         __func__, vdd->voltdm.name);
224                 return;
225         }
226
227         vp_val = vdd->vp_rt_data.vpconfig_erroroffset |
228                 (vdd->vp_rt_data.vpconfig_errorgain <<
229                 vdd->vp_data->vp_common->vpconfig_errorgain_shift) |
230                 vdd->vp_data->vp_common->vpconfig_timeouten;
231         vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vpconfig);
232
233         vp_val = ((vdd->vp_rt_data.vstepmin_smpswaittimemin <<
234                 vdd->vp_data->vp_common->vstepmin_smpswaittimemin_shift) |
235                 (vdd->vp_rt_data.vstepmin_stepmin <<
236                 vdd->vp_data->vp_common->vstepmin_stepmin_shift));
237         vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vstepmin);
238
239         vp_val = ((vdd->vp_rt_data.vstepmax_smpswaittimemax <<
240                 vdd->vp_data->vp_common->vstepmax_smpswaittimemax_shift) |
241                 (vdd->vp_rt_data.vstepmax_stepmax <<
242                 vdd->vp_data->vp_common->vstepmax_stepmax_shift));
243         vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vstepmax);
244
245         vp_val = ((vdd->vp_rt_data.vlimitto_vddmax <<
246                 vdd->vp_data->vp_common->vlimitto_vddmax_shift) |
247                 (vdd->vp_rt_data.vlimitto_vddmin <<
248                 vdd->vp_data->vp_common->vlimitto_vddmin_shift) |
249                 (vdd->vp_rt_data.vlimitto_timeout <<
250                 vdd->vp_data->vp_common->vlimitto_timeout_shift));
251         vdd->write_reg(vp_val, prm_mod_offs, vdd->vp_data->vlimitto);
252 }
253
254 static void __init vdd_debugfs_init(struct omap_vdd_info *vdd)
255 {
256         char *name;
257
258         name = kzalloc(VOLTAGE_DIR_SIZE, GFP_KERNEL);
259         if (!name) {
260                 pr_warning("%s: Unable to allocate memory for debugfs"
261                         " directory name for vdd_%s",
262                         __func__, vdd->voltdm.name);
263                 return;
264         }
265         strcpy(name, "vdd_");
266         strcat(name, vdd->voltdm.name);
267
268         vdd->debug_dir = debugfs_create_dir(name, voltage_dir);
269         kfree(name);
270         if (IS_ERR(vdd->debug_dir)) {
271                 pr_warning("%s: Unable to create debugfs directory for"
272                         " vdd_%s\n", __func__, vdd->voltdm.name);
273                 vdd->debug_dir = NULL;
274                 return;
275         }
276
277         (void) debugfs_create_x16("vp_errorgain", S_IRUGO, vdd->debug_dir,
278                                 &(vdd->vp_rt_data.vpconfig_errorgain));
279         (void) debugfs_create_x16("vp_smpswaittimemin", S_IRUGO,
280                                 vdd->debug_dir,
281                                 &(vdd->vp_rt_data.vstepmin_smpswaittimemin));
282         (void) debugfs_create_x8("vp_stepmin", S_IRUGO, vdd->debug_dir,
283                                 &(vdd->vp_rt_data.vstepmin_stepmin));
284         (void) debugfs_create_x16("vp_smpswaittimemax", S_IRUGO,
285                                 vdd->debug_dir,
286                                 &(vdd->vp_rt_data.vstepmax_smpswaittimemax));
287         (void) debugfs_create_x8("vp_stepmax", S_IRUGO, vdd->debug_dir,
288                                 &(vdd->vp_rt_data.vstepmax_stepmax));
289         (void) debugfs_create_x8("vp_vddmax", S_IRUGO, vdd->debug_dir,
290                                 &(vdd->vp_rt_data.vlimitto_vddmax));
291         (void) debugfs_create_x8("vp_vddmin", S_IRUGO, vdd->debug_dir,
292                                 &(vdd->vp_rt_data.vlimitto_vddmin));
293         (void) debugfs_create_x16("vp_timeout", S_IRUGO, vdd->debug_dir,
294                                 &(vdd->vp_rt_data.vlimitto_timeout));
295         (void) debugfs_create_file("curr_vp_volt", S_IRUGO, vdd->debug_dir,
296                                 (void *) vdd, &vp_volt_debug_fops);
297         (void) debugfs_create_file("curr_nominal_volt", S_IRUGO,
298                                 vdd->debug_dir, (void *) vdd,
299                                 &nom_volt_debug_fops);
300 }
301
302 /* Voltage scale and accessory APIs */
303 static int _pre_volt_scale(struct omap_vdd_info *vdd,
304                 unsigned long target_volt, u8 *target_vsel, u8 *current_vsel)
305 {
306         struct omap_volt_data *volt_data;
307         const struct omap_vc_common_data *vc_common;
308         const struct omap_vp_common_data *vp_common;
309         u32 vc_cmdval, vp_errgain_val;
310
311         vc_common = vdd->vc_data->vc_common;
312         vp_common = vdd->vp_data->vp_common;
313
314         /* Check if suffiecient pmic info is available for this vdd */
315         if (!vdd->pmic_info) {
316                 pr_err("%s: Insufficient pmic info to scale the vdd_%s\n",
317                         __func__, vdd->voltdm.name);
318                 return -EINVAL;
319         }
320
321         if (!vdd->pmic_info->uv_to_vsel) {
322                 pr_err("%s: PMIC function to convert voltage in uV to"
323                         "vsel not registered. Hence unable to scale voltage"
324                         "for vdd_%s\n", __func__, vdd->voltdm.name);
325                 return -ENODATA;
326         }
327
328         if (!vdd->read_reg || !vdd->write_reg) {
329                 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
330                         __func__, vdd->voltdm.name);
331                 return -EINVAL;
332         }
333
334         /* Get volt_data corresponding to target_volt */
335         volt_data = omap_voltage_get_voltdata(&vdd->voltdm, target_volt);
336         if (IS_ERR(volt_data))
337                 volt_data = NULL;
338
339         *target_vsel = vdd->pmic_info->uv_to_vsel(target_volt);
340         *current_vsel = vdd->read_reg(prm_mod_offs, vdd->vp_data->voltage);
341
342         /* Setting the ON voltage to the new target voltage */
343         vc_cmdval = vdd->read_reg(prm_mod_offs, vdd->vc_data->cmdval_reg);
344         vc_cmdval &= ~vc_common->cmd_on_mask;
345         vc_cmdval |= (*target_vsel << vc_common->cmd_on_shift);
346         vdd->write_reg(vc_cmdval, prm_mod_offs, vdd->vc_data->cmdval_reg);
347
348         /* Setting vp errorgain based on the voltage */
349         if (volt_data) {
350                 vp_errgain_val = vdd->read_reg(prm_mod_offs,
351                                                vdd->vp_data->vpconfig);
352                 vdd->vp_rt_data.vpconfig_errorgain = volt_data->vp_errgain;
353                 vp_errgain_val &= ~vp_common->vpconfig_errorgain_mask;
354                 vp_errgain_val |= vdd->vp_rt_data.vpconfig_errorgain <<
355                         vp_common->vpconfig_errorgain_shift;
356                 vdd->write_reg(vp_errgain_val, prm_mod_offs,
357                                vdd->vp_data->vpconfig);
358         }
359
360         return 0;
361 }
362
363 static void _post_volt_scale(struct omap_vdd_info *vdd,
364                 unsigned long target_volt, u8 target_vsel, u8 current_vsel)
365 {
366         u32 smps_steps = 0, smps_delay = 0;
367
368         smps_steps = abs(target_vsel - current_vsel);
369         /* SMPS slew rate / step size. 2us added as buffer. */
370         smps_delay = ((smps_steps * vdd->pmic_info->step_size) /
371                         vdd->pmic_info->slew_rate) + 2;
372         udelay(smps_delay);
373
374         vdd->curr_volt = target_volt;
375 }
376
377 /* vc_bypass_scale_voltage - VC bypass method of voltage scaling */
378 static int vc_bypass_scale_voltage(struct omap_vdd_info *vdd,
379                 unsigned long target_volt)
380 {
381         u32 loop_cnt = 0, retries_cnt = 0;
382         u32 vc_valid, vc_bypass_val_reg, vc_bypass_value;
383         u8 target_vsel, current_vsel;
384         int ret;
385
386         ret = _pre_volt_scale(vdd, target_volt, &target_vsel, &current_vsel);
387         if (ret)
388                 return ret;
389
390         vc_valid = vdd->vc_data->vc_common->valid;
391         vc_bypass_val_reg = vdd->vc_data->vc_common->bypass_val_reg;
392         vc_bypass_value = (target_vsel << vdd->vc_data->vc_common->data_shift) |
393                         (vdd->pmic_info->pmic_reg <<
394                         vdd->vc_data->vc_common->regaddr_shift) |
395                         (vdd->pmic_info->i2c_slave_addr <<
396                         vdd->vc_data->vc_common->slaveaddr_shift);
397
398         vdd->write_reg(vc_bypass_value, prm_mod_offs, vc_bypass_val_reg);
399         vdd->write_reg(vc_bypass_value | vc_valid, prm_mod_offs,
400                        vc_bypass_val_reg);
401
402         vc_bypass_value = vdd->read_reg(prm_mod_offs, vc_bypass_val_reg);
403         /*
404          * Loop till the bypass command is acknowledged from the SMPS.
405          * NOTE: This is legacy code. The loop count and retry count needs
406          * to be revisited.
407          */
408         while (!(vc_bypass_value & vc_valid)) {
409                 loop_cnt++;
410
411                 if (retries_cnt > 10) {
412                         pr_warning("%s: Retry count exceeded\n", __func__);
413                         return -ETIMEDOUT;
414                 }
415
416                 if (loop_cnt > 50) {
417                         retries_cnt++;
418                         loop_cnt = 0;
419                         udelay(10);
420                 }
421                 vc_bypass_value = vdd->read_reg(prm_mod_offs,
422                                                 vc_bypass_val_reg);
423         }
424
425         _post_volt_scale(vdd, target_volt, target_vsel, current_vsel);
426         return 0;
427 }
428
429 /* VP force update method of voltage scaling */
430 static int vp_forceupdate_scale_voltage(struct omap_vdd_info *vdd,
431                 unsigned long target_volt)
432 {
433         u32 vpconfig;
434         u8 target_vsel, current_vsel, prm_irqst_reg;
435         int ret, timeout = 0;
436
437         ret = _pre_volt_scale(vdd, target_volt, &target_vsel, &current_vsel);
438         if (ret)
439                 return ret;
440
441         prm_irqst_reg = vdd->vp_data->prm_irqst_data->prm_irqst_reg;
442
443         /*
444          * Clear all pending TransactionDone interrupt/status. Typical latency
445          * is <3us
446          */
447         while (timeout++ < VP_TRANXDONE_TIMEOUT) {
448                 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
449                                prm_irqst_ocp_mod_offs, prm_irqst_reg);
450                 if (!(vdd->read_reg(prm_irqst_ocp_mod_offs, prm_irqst_reg) &
451                       vdd->vp_data->prm_irqst_data->tranxdone_status))
452                         break;
453                 udelay(1);
454         }
455         if (timeout >= VP_TRANXDONE_TIMEOUT) {
456                 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
457                         "Voltage change aborted", __func__, vdd->voltdm.name);
458                 return -ETIMEDOUT;
459         }
460
461         /* Configure for VP-Force Update */
462         vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
463         vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvdd |
464                         vdd->vp_data->vp_common->vpconfig_forceupdate |
465                         vdd->vp_data->vp_common->vpconfig_initvoltage_mask);
466         vpconfig |= ((target_vsel <<
467                         vdd->vp_data->vp_common->vpconfig_initvoltage_shift));
468         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
469
470         /* Trigger initVDD value copy to voltage processor */
471         vpconfig |= vdd->vp_data->vp_common->vpconfig_initvdd;
472         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
473
474         /* Force update of voltage */
475         vpconfig |= vdd->vp_data->vp_common->vpconfig_forceupdate;
476         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
477
478         /*
479          * Wait for TransactionDone. Typical latency is <200us.
480          * Depends on SMPSWAITTIMEMIN/MAX and voltage change
481          */
482         timeout = 0;
483         omap_test_timeout((vdd->read_reg(prm_irqst_ocp_mod_offs, prm_irqst_reg) &
484                            vdd->vp_data->prm_irqst_data->tranxdone_status),
485                           VP_TRANXDONE_TIMEOUT, timeout);
486         if (timeout >= VP_TRANXDONE_TIMEOUT)
487                 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
488                         "TRANXDONE never got set after the voltage update\n",
489                         __func__, vdd->voltdm.name);
490
491         _post_volt_scale(vdd, target_volt, target_vsel, current_vsel);
492
493         /*
494          * Disable TransactionDone interrupt , clear all status, clear
495          * control registers
496          */
497         timeout = 0;
498         while (timeout++ < VP_TRANXDONE_TIMEOUT) {
499                 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
500                                prm_irqst_ocp_mod_offs, prm_irqst_reg);
501                 if (!(vdd->read_reg(prm_irqst_ocp_mod_offs, prm_irqst_reg) &
502                       vdd->vp_data->prm_irqst_data->tranxdone_status))
503                         break;
504                 udelay(1);
505         }
506
507         if (timeout >= VP_TRANXDONE_TIMEOUT)
508                 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
509                         "to clear the TRANXDONE status\n",
510                         __func__, vdd->voltdm.name);
511
512         vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
513         /* Clear initVDD copy trigger bit */
514         vpconfig &= ~vdd->vp_data->vp_common->vpconfig_initvdd;
515         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
516         /* Clear force bit */
517         vpconfig &= ~vdd->vp_data->vp_common->vpconfig_forceupdate;
518         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
519
520         return 0;
521 }
522
523 static void __init omap3_vfsm_init(struct omap_vdd_info *vdd)
524 {
525         /*
526          * Voltage Manager FSM parameters init
527          * XXX This data should be passed in from the board file
528          */
529         vdd->write_reg(OMAP3_CLKSETUP, prm_mod_offs, OMAP3_PRM_CLKSETUP_OFFSET);
530         vdd->write_reg(OMAP3_VOLTOFFSET, prm_mod_offs,
531                        OMAP3_PRM_VOLTOFFSET_OFFSET);
532         vdd->write_reg(OMAP3_VOLTSETUP2, prm_mod_offs,
533                        OMAP3_PRM_VOLTSETUP2_OFFSET);
534 }
535
536 static void __init omap3_vc_init(struct omap_vdd_info *vdd)
537 {
538         static bool is_initialized;
539         u8 on_vsel, onlp_vsel, ret_vsel, off_vsel;
540         u32 vc_val;
541
542         if (is_initialized)
543                 return;
544
545         /* Set up the on, inactive, retention and off voltage */
546         on_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->on_volt);
547         onlp_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->onlp_volt);
548         ret_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->ret_volt);
549         off_vsel = vdd->pmic_info->uv_to_vsel(vdd->pmic_info->off_volt);
550         vc_val  = ((on_vsel << vdd->vc_data->vc_common->cmd_on_shift) |
551                 (onlp_vsel << vdd->vc_data->vc_common->cmd_onlp_shift) |
552                 (ret_vsel << vdd->vc_data->vc_common->cmd_ret_shift) |
553                 (off_vsel << vdd->vc_data->vc_common->cmd_off_shift));
554         vdd->write_reg(vc_val, prm_mod_offs, vdd->vc_data->cmdval_reg);
555
556         /*
557          * Generic VC parameters init
558          * XXX This data should be abstracted out
559          */
560         vdd->write_reg(OMAP3430_CMD1_MASK | OMAP3430_RAV1_MASK, prm_mod_offs,
561                         OMAP3_PRM_VC_CH_CONF_OFFSET);
562         vdd->write_reg(OMAP3430_MCODE_SHIFT | OMAP3430_HSEN_MASK, prm_mod_offs,
563                         OMAP3_PRM_VC_I2C_CFG_OFFSET);
564
565         omap3_vfsm_init(vdd);
566
567         is_initialized = true;
568 }
569
570
571 /* OMAP4 specific voltage init functions */
572 static void __init omap4_vc_init(struct omap_vdd_info *vdd)
573 {
574         static bool is_initialized;
575         u32 vc_val;
576
577         if (is_initialized)
578                 return;
579
580         /* TODO: Configure setup times and CMD_VAL values*/
581
582         /*
583          * Generic VC parameters init
584          * XXX This data should be abstracted out
585          */
586         vc_val = (OMAP4430_RAV_VDD_MPU_L_MASK | OMAP4430_CMD_VDD_MPU_L_MASK |
587                   OMAP4430_RAV_VDD_IVA_L_MASK | OMAP4430_CMD_VDD_IVA_L_MASK |
588                   OMAP4430_RAV_VDD_CORE_L_MASK | OMAP4430_CMD_VDD_CORE_L_MASK);
589         vdd->write_reg(vc_val, prm_mod_offs, OMAP4_PRM_VC_CFG_CHANNEL_OFFSET);
590
591         /* XXX These are magic numbers and do not belong! */
592         vc_val = (0x60 << OMAP4430_SCLL_SHIFT | 0x26 << OMAP4430_SCLH_SHIFT);
593         vdd->write_reg(vc_val, prm_mod_offs, OMAP4_PRM_VC_CFG_I2C_CLK_OFFSET);
594
595         is_initialized = true;
596 }
597
598 static void __init omap_vc_init(struct omap_vdd_info *vdd)
599 {
600         u32 vc_val;
601
602         if (!vdd->pmic_info || !vdd->pmic_info->uv_to_vsel) {
603                 pr_err("%s: PMIC info requried to configure vc for"
604                         "vdd_%s not populated.Hence cannot initialize vc\n",
605                         __func__, vdd->voltdm.name);
606                 return;
607         }
608
609         if (!vdd->read_reg || !vdd->write_reg) {
610                 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
611                         __func__, vdd->voltdm.name);
612                 return;
613         }
614
615         /* Set up the SMPS_SA(i2c slave address in VC */
616         vc_val = vdd->read_reg(prm_mod_offs,
617                                vdd->vc_data->vc_common->smps_sa_reg);
618         vc_val &= ~vdd->vc_data->smps_sa_mask;
619         vc_val |= vdd->pmic_info->i2c_slave_addr << vdd->vc_data->smps_sa_shift;
620         vdd->write_reg(vc_val, prm_mod_offs,
621                        vdd->vc_data->vc_common->smps_sa_reg);
622
623         /* Setup the VOLRA(pmic reg addr) in VC */
624         vc_val = vdd->read_reg(prm_mod_offs,
625                                vdd->vc_data->vc_common->smps_volra_reg);
626         vc_val &= ~vdd->vc_data->smps_volra_mask;
627         vc_val |= vdd->pmic_info->pmic_reg << vdd->vc_data->smps_volra_shift;
628         vdd->write_reg(vc_val, prm_mod_offs,
629                        vdd->vc_data->vc_common->smps_volra_reg);
630
631         /* Configure the setup times */
632         vc_val = vdd->read_reg(prm_mod_offs, vdd->vfsm->voltsetup_reg);
633         vc_val &= ~vdd->vfsm->voltsetup_mask;
634         vc_val |= vdd->pmic_info->volt_setup_time <<
635                         vdd->vfsm->voltsetup_shift;
636         vdd->write_reg(vc_val, prm_mod_offs, vdd->vfsm->voltsetup_reg);
637
638         if (cpu_is_omap34xx())
639                 omap3_vc_init(vdd);
640         else if (cpu_is_omap44xx())
641                 omap4_vc_init(vdd);
642 }
643
644 static int __init omap_vdd_data_configure(struct omap_vdd_info *vdd)
645 {
646         int ret = -EINVAL;
647
648         if (!vdd->pmic_info) {
649                 pr_err("%s: PMIC info requried to configure vdd_%s not"
650                         "populated.Hence cannot initialize vdd_%s\n",
651                         __func__, vdd->voltdm.name, vdd->voltdm.name);
652                 goto ovdc_out;
653         }
654
655         if (IS_ERR_VALUE(_config_common_vdd_data(vdd)))
656                 goto ovdc_out;
657
658         if (cpu_is_omap34xx()) {
659                 vdd->read_reg = omap3_voltage_read_reg;
660                 vdd->write_reg = omap3_voltage_write_reg;
661                 ret = 0;
662         } else if (cpu_is_omap44xx()) {
663                 vdd->read_reg = omap4_voltage_read_reg;
664                 vdd->write_reg = omap4_voltage_write_reg;
665                 ret = 0;
666         }
667
668 ovdc_out:
669         return ret;
670 }
671
672 /* Public functions */
673 /**
674  * omap_voltage_get_nom_volt() - Gets the current non-auto-compensated voltage
675  * @voltdm:     pointer to the VDD for which current voltage info is needed
676  *
677  * API to get the current non-auto-compensated voltage for a VDD.
678  * Returns 0 in case of error else returns the current voltage for the VDD.
679  */
680 unsigned long omap_voltage_get_nom_volt(struct voltagedomain *voltdm)
681 {
682         struct omap_vdd_info *vdd;
683
684         if (!voltdm || IS_ERR(voltdm)) {
685                 pr_warning("%s: VDD specified does not exist!\n", __func__);
686                 return 0;
687         }
688
689         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
690
691         return vdd->curr_volt;
692 }
693
694 /**
695  * omap_vp_get_curr_volt() - API to get the current vp voltage.
696  * @voltdm:     pointer to the VDD.
697  *
698  * This API returns the current voltage for the specified voltage processor
699  */
700 unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
701 {
702         struct omap_vdd_info *vdd;
703         u8 curr_vsel;
704
705         if (!voltdm || IS_ERR(voltdm)) {
706                 pr_warning("%s: VDD specified does not exist!\n", __func__);
707                 return 0;
708         }
709
710         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
711         if (!vdd->read_reg) {
712                 pr_err("%s: No read API for reading vdd_%s regs\n",
713                         __func__, voltdm->name);
714                 return 0;
715         }
716
717         curr_vsel = vdd->read_reg(prm_mod_offs, vdd->vp_data->voltage);
718
719         if (!vdd->pmic_info || !vdd->pmic_info->vsel_to_uv) {
720                 pr_warning("%s: PMIC function to convert vsel to voltage"
721                         "in uV not registerd\n", __func__);
722                 return 0;
723         }
724
725         return vdd->pmic_info->vsel_to_uv(curr_vsel);
726 }
727
728 /**
729  * omap_vp_enable() - API to enable a particular VP
730  * @voltdm:     pointer to the VDD whose VP is to be enabled.
731  *
732  * This API enables a particular voltage processor. Needed by the smartreflex
733  * class drivers.
734  */
735 void omap_vp_enable(struct voltagedomain *voltdm)
736 {
737         struct omap_vdd_info *vdd;
738         u32 vpconfig;
739
740         if (!voltdm || IS_ERR(voltdm)) {
741                 pr_warning("%s: VDD specified does not exist!\n", __func__);
742                 return;
743         }
744
745         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
746         if (!vdd->read_reg || !vdd->write_reg) {
747                 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
748                         __func__, voltdm->name);
749                 return;
750         }
751
752         /* If VP is already enabled, do nothing. Return */
753         if (vdd->vp_enabled)
754                 return;
755
756         vp_latch_vsel(vdd);
757
758         /* Enable VP */
759         vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
760         vpconfig |= vdd->vp_data->vp_common->vpconfig_vpenable;
761         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
762         vdd->vp_enabled = true;
763 }
764
765 /**
766  * omap_vp_disable() - API to disable a particular VP
767  * @voltdm:     pointer to the VDD whose VP is to be disabled.
768  *
769  * This API disables a particular voltage processor. Needed by the smartreflex
770  * class drivers.
771  */
772 void omap_vp_disable(struct voltagedomain *voltdm)
773 {
774         struct omap_vdd_info *vdd;
775         u32 vpconfig;
776         int timeout;
777
778         if (!voltdm || IS_ERR(voltdm)) {
779                 pr_warning("%s: VDD specified does not exist!\n", __func__);
780                 return;
781         }
782
783         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
784         if (!vdd->read_reg || !vdd->write_reg) {
785                 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
786                         __func__, voltdm->name);
787                 return;
788         }
789
790         /* If VP is already disabled, do nothing. Return */
791         if (!vdd->vp_enabled) {
792                 pr_warning("%s: Trying to disable VP for vdd_%s when"
793                         "it is already disabled\n", __func__, voltdm->name);
794                 return;
795         }
796
797         /* Disable VP */
798         vpconfig = vdd->read_reg(prm_mod_offs, vdd->vp_data->vpconfig);
799         vpconfig &= ~vdd->vp_data->vp_common->vpconfig_vpenable;
800         vdd->write_reg(vpconfig, prm_mod_offs, vdd->vp_data->vpconfig);
801
802         /*
803          * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
804          */
805         omap_test_timeout((vdd->read_reg(prm_mod_offs, vdd->vp_data->vstatus)),
806                                 VP_IDLE_TIMEOUT, timeout);
807
808         if (timeout >= VP_IDLE_TIMEOUT)
809                 pr_warning("%s: vdd_%s idle timedout\n",
810                         __func__, voltdm->name);
811
812         vdd->vp_enabled = false;
813
814         return;
815 }
816
817 /**
818  * omap_voltage_scale_vdd() - API to scale voltage of a particular
819  *                              voltage domain.
820  * @voltdm:     pointer to the VDD which is to be scaled.
821  * @target_volt:        The target voltage of the voltage domain
822  *
823  * This API should be called by the kernel to do the voltage scaling
824  * for a particular voltage domain during dvfs or any other situation.
825  */
826 int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
827                 unsigned long target_volt)
828 {
829         struct omap_vdd_info *vdd;
830
831         if (!voltdm || IS_ERR(voltdm)) {
832                 pr_warning("%s: VDD specified does not exist!\n", __func__);
833                 return -EINVAL;
834         }
835
836         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
837
838         if (!vdd->volt_scale) {
839                 pr_err("%s: No voltage scale API registered for vdd_%s\n",
840                         __func__, voltdm->name);
841                 return -ENODATA;
842         }
843
844         return vdd->volt_scale(vdd, target_volt);
845 }
846
847 /**
848  * omap_voltage_reset() - Resets the voltage of a particular voltage domain
849  *                      to that of the current OPP.
850  * @voltdm:     pointer to the VDD whose voltage is to be reset.
851  *
852  * This API finds out the correct voltage the voltage domain is supposed
853  * to be at and resets the voltage to that level. Should be used especially
854  * while disabling any voltage compensation modules.
855  */
856 void omap_voltage_reset(struct voltagedomain *voltdm)
857 {
858         unsigned long target_uvdc;
859
860         if (!voltdm || IS_ERR(voltdm)) {
861                 pr_warning("%s: VDD specified does not exist!\n", __func__);
862                 return;
863         }
864
865         target_uvdc = omap_voltage_get_nom_volt(voltdm);
866         if (!target_uvdc) {
867                 pr_err("%s: unable to find current voltage for vdd_%s\n",
868                         __func__, voltdm->name);
869                 return;
870         }
871
872         omap_voltage_scale_vdd(voltdm, target_uvdc);
873 }
874
875 /**
876  * omap_voltage_get_volttable() - API to get the voltage table associated with a
877  *                              particular voltage domain.
878  * @voltdm:     pointer to the VDD for which the voltage table is required
879  * @volt_data:  the voltage table for the particular vdd which is to be
880  *              populated by this API
881  *
882  * This API populates the voltage table associated with a VDD into the
883  * passed parameter pointer. Returns the count of distinct voltages
884  * supported by this vdd.
885  *
886  */
887 void omap_voltage_get_volttable(struct voltagedomain *voltdm,
888                 struct omap_volt_data **volt_data)
889 {
890         struct omap_vdd_info *vdd;
891
892         if (!voltdm || IS_ERR(voltdm)) {
893                 pr_warning("%s: VDD specified does not exist!\n", __func__);
894                 return;
895         }
896
897         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
898
899         *volt_data = vdd->volt_data;
900 }
901
902 /**
903  * omap_voltage_get_voltdata() - API to get the voltage table entry for a
904  *                              particular voltage
905  * @voltdm:     pointer to the VDD whose voltage table has to be searched
906  * @volt:       the voltage to be searched in the voltage table
907  *
908  * This API searches through the voltage table for the required voltage
909  * domain and tries to find a matching entry for the passed voltage volt.
910  * If a matching entry is found volt_data is populated with that entry.
911  * This API searches only through the non-compensated voltages int the
912  * voltage table.
913  * Returns pointer to the voltage table entry corresponding to volt on
914  * success. Returns -ENODATA if no voltage table exisits for the passed voltage
915  * domain or if there is no matching entry.
916  */
917 struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
918                 unsigned long volt)
919 {
920         struct omap_vdd_info *vdd;
921         int i;
922
923         if (!voltdm || IS_ERR(voltdm)) {
924                 pr_warning("%s: VDD specified does not exist!\n", __func__);
925                 return ERR_PTR(-EINVAL);
926         }
927
928         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
929
930         if (!vdd->volt_data) {
931                 pr_warning("%s: voltage table does not exist for vdd_%s\n",
932                         __func__, voltdm->name);
933                 return ERR_PTR(-ENODATA);
934         }
935
936         for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
937                 if (vdd->volt_data[i].volt_nominal == volt)
938                         return &vdd->volt_data[i];
939         }
940
941         pr_notice("%s: Unable to match the current voltage with the voltage"
942                 "table for vdd_%s\n", __func__, voltdm->name);
943
944         return ERR_PTR(-ENODATA);
945 }
946
947 /**
948  * omap_voltage_register_pmic() - API to register PMIC specific data
949  * @voltdm:     pointer to the VDD for which the PMIC specific data is
950  *              to be registered
951  * @pmic_info:  the structure containing pmic info
952  *
953  * This API is to be called by the SOC/PMIC file to specify the
954  * pmic specific info as present in omap_volt_pmic_info structure.
955  */
956 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
957                 struct omap_volt_pmic_info *pmic_info)
958 {
959         struct omap_vdd_info *vdd;
960
961         if (!voltdm || IS_ERR(voltdm)) {
962                 pr_warning("%s: VDD specified does not exist!\n", __func__);
963                 return -EINVAL;
964         }
965
966         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
967
968         vdd->pmic_info = pmic_info;
969
970         return 0;
971 }
972
973 /**
974  * omap_voltage_get_dbgdir() - API to get pointer to the debugfs directory
975  *                              corresponding to a voltage domain.
976  *
977  * @voltdm:     pointer to the VDD whose debug directory is required.
978  *
979  * This API returns pointer to the debugfs directory corresponding
980  * to the voltage domain. Should be used by drivers requiring to
981  * add any debug entry for a particular voltage domain. Returns NULL
982  * in case of error.
983  */
984 struct dentry *omap_voltage_get_dbgdir(struct voltagedomain *voltdm)
985 {
986         struct omap_vdd_info *vdd;
987
988         if (!voltdm || IS_ERR(voltdm)) {
989                 pr_warning("%s: VDD specified does not exist!\n", __func__);
990                 return NULL;
991         }
992
993         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
994
995         return vdd->debug_dir;
996 }
997
998 /**
999  * omap_change_voltscale_method() - API to change the voltage scaling method.
1000  * @voltdm:     pointer to the VDD whose voltage scaling method
1001  *              has to be changed.
1002  * @voltscale_method:   the method to be used for voltage scaling.
1003  *
1004  * This API can be used by the board files to change the method of voltage
1005  * scaling between vpforceupdate and vcbypass. The parameter values are
1006  * defined in voltage.h
1007  */
1008 void omap_change_voltscale_method(struct voltagedomain *voltdm,
1009                 int voltscale_method)
1010 {
1011         struct omap_vdd_info *vdd;
1012
1013         if (!voltdm || IS_ERR(voltdm)) {
1014                 pr_warning("%s: VDD specified does not exist!\n", __func__);
1015                 return;
1016         }
1017
1018         vdd = container_of(voltdm, struct omap_vdd_info, voltdm);
1019
1020         switch (voltscale_method) {
1021         case VOLTSCALE_VPFORCEUPDATE:
1022                 vdd->volt_scale = vp_forceupdate_scale_voltage;
1023                 return;
1024         case VOLTSCALE_VCBYPASS:
1025                 vdd->volt_scale = vc_bypass_scale_voltage;
1026                 return;
1027         default:
1028                 pr_warning("%s: Trying to change the method of voltage scaling"
1029                         "to an unsupported one!\n", __func__);
1030         }
1031 }
1032
1033 /**
1034  * omap_voltage_domain_lookup() - API to get the voltage domain pointer
1035  * @name:       Name of the voltage domain
1036  *
1037  * This API looks up in the global vdd_info struct for the
1038  * existence of voltage domain <name>. If it exists, the API returns
1039  * a pointer to the voltage domain structure corresponding to the
1040  * VDD<name>. Else retuns error pointer.
1041  */
1042 struct voltagedomain *omap_voltage_domain_lookup(char *name)
1043 {
1044         int i;
1045
1046         if (!vdd_info) {
1047                 pr_err("%s: Voltage driver init not yet happened.Faulting!\n",
1048                         __func__);
1049                 return ERR_PTR(-EINVAL);
1050         }
1051
1052         if (!name) {
1053                 pr_err("%s: No name to get the votage domain!\n", __func__);
1054                 return ERR_PTR(-EINVAL);
1055         }
1056
1057         for (i = 0; i < nr_scalable_vdd; i++) {
1058                 if (!(strcmp(name, vdd_info[i]->voltdm.name)))
1059                         return &vdd_info[i]->voltdm;
1060         }
1061
1062         return ERR_PTR(-EINVAL);
1063 }
1064
1065 /**
1066  * omap_voltage_late_init() - Init the various voltage parameters
1067  *
1068  * This API is to be called in the later stages of the
1069  * system boot to init the voltage controller and
1070  * voltage processors.
1071  */
1072 int __init omap_voltage_late_init(void)
1073 {
1074         int i;
1075
1076         if (!vdd_info) {
1077                 pr_err("%s: Voltage driver support not added\n",
1078                         __func__);
1079                 return -EINVAL;
1080         }
1081
1082         voltage_dir = debugfs_create_dir("voltage", NULL);
1083         if (IS_ERR(voltage_dir))
1084                 pr_err("%s: Unable to create voltage debugfs main dir\n",
1085                         __func__);
1086         for (i = 0; i < nr_scalable_vdd; i++) {
1087                 if (omap_vdd_data_configure(vdd_info[i]))
1088                         continue;
1089                 omap_vc_init(vdd_info[i]);
1090                 vp_init(vdd_info[i]);
1091                 vdd_debugfs_init(vdd_info[i]);
1092         }
1093
1094         return 0;
1095 }
1096
1097 /* XXX document */
1098 int __init omap_voltage_early_init(s16 prm_mod, s16 prm_irqst_ocp_mod,
1099                                    struct omap_vdd_info *omap_vdd_array[],
1100                                    u8 omap_vdd_count)
1101 {
1102         prm_mod_offs = prm_mod;
1103         prm_irqst_ocp_mod_offs = prm_irqst_ocp_mod;
1104         vdd_info = omap_vdd_array;
1105         nr_scalable_vdd = omap_vdd_count;
1106         return 0;
1107 }