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