blob: c6352e3230b7a942c090c7ffb69d050b1c3e48d6 [file] [log] [blame]
Thara Gopinath2f34ce82010-05-29 22:02:21 +05301/*
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 *
Paul Walmsleyc0718df2011-03-10 22:17:45 -070010 * Copyright (C) 2008, 2011 Nokia Corporation
Thara Gopinath2f34ce82010-05-29 22:02:21 +053011 * Kalle Jokiniemi
Paul Walmsleyc0718df2011-03-10 22:17:45 -070012 * Paul Walmsley
Thara Gopinath2f34ce82010-05-29 22:02:21 +053013 *
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>
Thara Gopinath2f34ce82010-05-29 22:02:21 +053030
31#include "prm-regbits-34xx.h"
Thara Gopinathbd381072010-12-10 23:15:23 +053032#include "prm-regbits-44xx.h"
33#include "prm44xx.h"
34#include "prcm44xx.h"
35#include "prminst44xx.h"
Thara Gopinath2f34ce82010-05-29 22:02:21 +053036#include "control.h"
37
Paul Walmsleye1d6f472011-02-25 15:54:33 -070038#include "voltage.h"
Kevin Hilmane69c22b2011-03-16 16:13:15 -070039#include "powerdomain.h"
Paul Walmsleye1d6f472011-02-25 15:54:33 -070040
Paul Walmsleyc0718df2011-03-10 22:17:45 -070041#include "vc.h"
42#include "vp.h"
43
Kevin Hilman81a60482011-03-16 14:25:45 -070044static LIST_HEAD(voltdm_list);
45
Thara Gopinath2f34ce82010-05-29 22:02:21 +053046#define VOLTAGE_DIR_SIZE 16
Thara Gopinath2f34ce82010-05-29 22:02:21 +053047static struct dentry *voltage_dir;
48
49/* Init function pointers */
Kevin Hilman81a60482011-03-16 14:25:45 -070050static int vp_forceupdate_scale_voltage(struct voltagedomain *voltdm,
Paul Walmsleyc0718df2011-03-10 22:17:45 -070051 unsigned long target_volt);
Thara Gopinath2f34ce82010-05-29 22:02:21 +053052
53static u32 omap3_voltage_read_reg(u16 mod, u8 offset)
54{
55 return omap2_prm_read_mod_reg(mod, offset);
56}
57
58static void omap3_voltage_write_reg(u32 val, u16 mod, u8 offset)
59{
60 omap2_prm_write_mod_reg(val, mod, offset);
61}
62
Thara Gopinathbd381072010-12-10 23:15:23 +053063static 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
69static 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
Kevin Hilman81a60482011-03-16 14:25:45 -070074static int __init _config_common_vdd_data(struct voltagedomain *voltdm)
Paul Walmsleyc0718df2011-03-10 22:17:45 -070075{
76 char *sys_ck_name;
77 struct clk *sys_ck;
78 u32 sys_clk_speed, timeout_val, waittime;
Kevin Hilman81a60482011-03-16 14:25:45 -070079 struct omap_vdd_info *vdd = voltdm->vdd;
Paul Walmsleyc0718df2011-03-10 22:17:45 -070080
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"
Kevin Hilman81a60482011-03-16 14:25:45 -070099 "various vdd_%s params\n", __func__, voltdm->name);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700100 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 */
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700108 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
Thara Gopinath077fcec2010-10-27 20:29:37 +0530130/* Voltage debugfs support */
131static int vp_volt_debug_get(void *data, u64 *val)
132{
Kevin Hilman81a60482011-03-16 14:25:45 -0700133 struct voltagedomain *voltdm = (struct voltagedomain *)data;
134 struct omap_vdd_info *vdd = voltdm->vdd;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530135 u8 vsel;
136
137 if (!vdd) {
138 pr_warning("Wrong paramater passed\n");
139 return -EINVAL;
140 }
141
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700142 vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530143
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
154static int nom_volt_debug_get(void *data, u64 *val)
155{
Kevin Hilman81a60482011-03-16 14:25:45 -0700156 struct voltagedomain *voltdm = (struct voltagedomain *)data;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530157
Kevin Hilman81a60482011-03-16 14:25:45 -0700158 if (!voltdm) {
Thara Gopinath077fcec2010-10-27 20:29:37 +0530159 pr_warning("Wrong paramater passed\n");
160 return -EINVAL;
161 }
162
Kevin Hilman81a60482011-03-16 14:25:45 -0700163 *val = omap_voltage_get_nom_volt(voltdm);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530164
165 return 0;
166}
167
168DEFINE_SIMPLE_ATTRIBUTE(vp_volt_debug_fops, vp_volt_debug_get, NULL, "%llu\n");
169DEFINE_SIMPLE_ATTRIBUTE(nom_volt_debug_fops, nom_volt_debug_get, NULL,
170 "%llu\n");
Kevin Hilman81a60482011-03-16 14:25:45 -0700171static void vp_latch_vsel(struct voltagedomain *voltdm)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530172{
173 u32 vpconfig;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530174 unsigned long uvdc;
175 char vsel;
Kevin Hilman81a60482011-03-16 14:25:45 -0700176 struct omap_vdd_info *vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530177
Kevin Hilman81a60482011-03-16 14:25:45 -0700178 uvdc = omap_voltage_get_nom_volt(voltdm);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530179 if (!uvdc) {
180 pr_warning("%s: unable to find current voltage for vdd_%s\n",
Kevin Hilman81a60482011-03-16 14:25:45 -0700181 __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530182 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
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530191 vsel = vdd->pmic_info->uv_to_vsel(uvdc);
192
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700193 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700194 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;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530197
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700198 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530199
200 /* Trigger initVDD value copy to voltage processor */
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700201 vdd->write_reg((vpconfig | vdd->vp_data->vp_common->vpconfig_initvdd),
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700202 vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530203
204 /* Clear initVDD copy trigger bit */
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700205 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530206}
207
208/* Generic voltage init functions */
Kevin Hilman81a60482011-03-16 14:25:45 -0700209static void __init vp_init(struct voltagedomain *voltdm)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530210{
Kevin Hilman81a60482011-03-16 14:25:45 -0700211 struct omap_vdd_info *vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530212 u32 vp_val;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530213
214 if (!vdd->read_reg || !vdd->write_reg) {
215 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
Kevin Hilman81a60482011-03-16 14:25:45 -0700216 __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530217 return;
218 }
219
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700220 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;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700224 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530225
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700226 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));
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700230 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vstepmin);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530231
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700232 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));
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700236 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vstepmax);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530237
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700238 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));
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700244 vdd->write_reg(vp_val, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vlimitto);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530245}
246
Kevin Hilman81a60482011-03-16 14:25:45 -0700247static void __init vdd_debugfs_init(struct voltagedomain *voltdm)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530248{
249 char *name;
Kevin Hilman81a60482011-03-16 14:25:45 -0700250 struct omap_vdd_info *vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530251
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",
Kevin Hilman81a60482011-03-16 14:25:45 -0700256 __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530257 return;
258 }
259 strcpy(name, "vdd_");
Kevin Hilman81a60482011-03-16 14:25:45 -0700260 strcat(name, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530261
262 vdd->debug_dir = debugfs_create_dir(name, voltage_dir);
Aaro Koskinen62270112011-01-28 18:24:11 +0200263 kfree(name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530264 if (IS_ERR(vdd->debug_dir)) {
265 pr_warning("%s: Unable to create debugfs directory for"
Kevin Hilman81a60482011-03-16 14:25:45 -0700266 " vdd_%s\n", __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530267 vdd->debug_dir = NULL;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530268 return;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530269 }
Thara Gopinath077fcec2010-10-27 20:29:37 +0530270
271 (void) debugfs_create_x16("vp_errorgain", S_IRUGO, vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700272 &(vdd->vp_rt_data.vpconfig_errorgain));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530273 (void) debugfs_create_x16("vp_smpswaittimemin", S_IRUGO,
274 vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700275 &(vdd->vp_rt_data.vstepmin_smpswaittimemin));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530276 (void) debugfs_create_x8("vp_stepmin", S_IRUGO, vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700277 &(vdd->vp_rt_data.vstepmin_stepmin));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530278 (void) debugfs_create_x16("vp_smpswaittimemax", S_IRUGO,
279 vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700280 &(vdd->vp_rt_data.vstepmax_smpswaittimemax));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530281 (void) debugfs_create_x8("vp_stepmax", S_IRUGO, vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700282 &(vdd->vp_rt_data.vstepmax_stepmax));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530283 (void) debugfs_create_x8("vp_vddmax", S_IRUGO, vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700284 &(vdd->vp_rt_data.vlimitto_vddmax));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530285 (void) debugfs_create_x8("vp_vddmin", S_IRUGO, vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700286 &(vdd->vp_rt_data.vlimitto_vddmin));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530287 (void) debugfs_create_x16("vp_timeout", S_IRUGO, vdd->debug_dir,
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700288 &(vdd->vp_rt_data.vlimitto_timeout));
Thara Gopinath077fcec2010-10-27 20:29:37 +0530289 (void) debugfs_create_file("curr_vp_volt", S_IRUGO, vdd->debug_dir,
Kevin Hilman81a60482011-03-16 14:25:45 -0700290 (void *) voltdm, &vp_volt_debug_fops);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530291 (void) debugfs_create_file("curr_nominal_volt", S_IRUGO,
Kevin Hilman81a60482011-03-16 14:25:45 -0700292 vdd->debug_dir, (void *) voltdm,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530293 &nom_volt_debug_fops);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530294}
295
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530296/* VP force update method of voltage scaling */
Kevin Hilman81a60482011-03-16 14:25:45 -0700297static int vp_forceupdate_scale_voltage(struct voltagedomain *voltdm,
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530298 unsigned long target_volt)
299{
Kevin Hilman81a60482011-03-16 14:25:45 -0700300 struct omap_vdd_info *vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530301 u32 vpconfig;
Kevin Hilmanc39263c2011-03-16 17:20:35 -0700302 u8 target_vsel, current_vsel;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530303 int ret, timeout = 0;
304
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700305 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530306 if (ret)
307 return ret;
308
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530309 /*
310 * Clear all pending TransactionDone interrupt/status. Typical latency
311 * is <3us
312 */
313 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700314 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
Kevin Hilmanc39263c2011-03-16 17:20:35 -0700315 vdd->prm_irqst_mod, vdd->prm_irqst_reg);
316 if (!(vdd->read_reg(vdd->prm_irqst_mod, vdd->prm_irqst_reg) &
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700317 vdd->vp_data->prm_irqst_data->tranxdone_status))
318 break;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530319 udelay(1);
320 }
321 if (timeout >= VP_TRANXDONE_TIMEOUT) {
322 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded."
Kevin Hilman81a60482011-03-16 14:25:45 -0700323 "Voltage change aborted", __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530324 return -ETIMEDOUT;
325 }
326
327 /* Configure for VP-Force Update */
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700328 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700329 vpconfig &= ~(vdd->vp_data->vp_common->vpconfig_initvdd |
330 vdd->vp_data->vp_common->vpconfig_forceupdate |
331 vdd->vp_data->vp_common->vpconfig_initvoltage_mask);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530332 vpconfig |= ((target_vsel <<
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700333 vdd->vp_data->vp_common->vpconfig_initvoltage_shift));
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700334 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530335
336 /* Trigger initVDD value copy to voltage processor */
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700337 vpconfig |= vdd->vp_data->vp_common->vpconfig_initvdd;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700338 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530339
340 /* Force update of voltage */
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700341 vpconfig |= vdd->vp_data->vp_common->vpconfig_forceupdate;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700342 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530343
344 /*
345 * Wait for TransactionDone. Typical latency is <200us.
346 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
347 */
348 timeout = 0;
Kevin Hilmanc39263c2011-03-16 17:20:35 -0700349 omap_test_timeout((vdd->read_reg(vdd->prm_irqst_mod,
350 vdd->prm_irqst_reg) &
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700351 vdd->vp_data->prm_irqst_data->tranxdone_status),
352 VP_TRANXDONE_TIMEOUT, timeout);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530353 if (timeout >= VP_TRANXDONE_TIMEOUT)
354 pr_err("%s: vdd_%s TRANXDONE timeout exceeded."
355 "TRANXDONE never got set after the voltage update\n",
Kevin Hilman81a60482011-03-16 14:25:45 -0700356 __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530357
Kevin Hilmanccd5ca72011-03-21 14:08:55 -0700358 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530359
360 /*
361 * Disable TransactionDone interrupt , clear all status, clear
362 * control registers
363 */
364 timeout = 0;
365 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700366 vdd->write_reg(vdd->vp_data->prm_irqst_data->tranxdone_status,
Kevin Hilmanc39263c2011-03-16 17:20:35 -0700367 vdd->prm_irqst_mod, vdd->prm_irqst_reg);
368 if (!(vdd->read_reg(vdd->prm_irqst_mod, vdd->prm_irqst_reg) &
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700369 vdd->vp_data->prm_irqst_data->tranxdone_status))
370 break;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530371 udelay(1);
372 }
373
374 if (timeout >= VP_TRANXDONE_TIMEOUT)
375 pr_warning("%s: vdd_%s TRANXDONE timeout exceeded while trying"
376 "to clear the TRANXDONE status\n",
Kevin Hilman81a60482011-03-16 14:25:45 -0700377 __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530378
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700379 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530380 /* Clear initVDD copy trigger bit */
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700381 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_initvdd;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700382 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530383 /* Clear force bit */
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700384 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_forceupdate;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700385 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530386
387 return 0;
388}
389
Kevin Hilman81a60482011-03-16 14:25:45 -0700390static int __init omap_vdd_data_configure(struct voltagedomain *voltdm)
Thara Gopinathbd381072010-12-10 23:15:23 +0530391{
Kevin Hilman81a60482011-03-16 14:25:45 -0700392 struct omap_vdd_info *vdd = voltdm->vdd;
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700393 int ret = -EINVAL;
Thara Gopinathbd381072010-12-10 23:15:23 +0530394
395 if (!vdd->pmic_info) {
396 pr_err("%s: PMIC info requried to configure vdd_%s not"
397 "populated.Hence cannot initialize vdd_%s\n",
Kevin Hilman81a60482011-03-16 14:25:45 -0700398 __func__, voltdm->name, voltdm->name);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700399 goto ovdc_out;
Thara Gopinathbd381072010-12-10 23:15:23 +0530400 }
401
Kevin Hilman81a60482011-03-16 14:25:45 -0700402 if (IS_ERR_VALUE(_config_common_vdd_data(voltdm)))
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700403 goto ovdc_out;
404
405 if (cpu_is_omap34xx()) {
406 vdd->read_reg = omap3_voltage_read_reg;
407 vdd->write_reg = omap3_voltage_write_reg;
408 ret = 0;
409 } else if (cpu_is_omap44xx()) {
410 vdd->read_reg = omap4_voltage_read_reg;
411 vdd->write_reg = omap4_voltage_write_reg;
412 ret = 0;
Thara Gopinathbd381072010-12-10 23:15:23 +0530413 }
414
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700415ovdc_out:
416 return ret;
Thara Gopinathbd381072010-12-10 23:15:23 +0530417}
418
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530419/* Public functions */
420/**
421 * omap_voltage_get_nom_volt() - Gets the current non-auto-compensated voltage
422 * @voltdm: pointer to the VDD for which current voltage info is needed
423 *
424 * API to get the current non-auto-compensated voltage for a VDD.
425 * Returns 0 in case of error else returns the current voltage for the VDD.
426 */
427unsigned long omap_voltage_get_nom_volt(struct voltagedomain *voltdm)
428{
429 struct omap_vdd_info *vdd;
430
431 if (!voltdm || IS_ERR(voltdm)) {
432 pr_warning("%s: VDD specified does not exist!\n", __func__);
433 return 0;
434 }
435
Kevin Hilman81a60482011-03-16 14:25:45 -0700436 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530437
438 return vdd->curr_volt;
439}
440
441/**
442 * omap_vp_get_curr_volt() - API to get the current vp voltage.
443 * @voltdm: pointer to the VDD.
444 *
445 * This API returns the current voltage for the specified voltage processor
446 */
447unsigned long omap_vp_get_curr_volt(struct voltagedomain *voltdm)
448{
449 struct omap_vdd_info *vdd;
450 u8 curr_vsel;
451
452 if (!voltdm || IS_ERR(voltdm)) {
453 pr_warning("%s: VDD specified does not exist!\n", __func__);
454 return 0;
455 }
456
Kevin Hilman81a60482011-03-16 14:25:45 -0700457 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530458 if (!vdd->read_reg) {
459 pr_err("%s: No read API for reading vdd_%s regs\n",
460 __func__, voltdm->name);
461 return 0;
462 }
463
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700464 curr_vsel = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->voltage);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530465
466 if (!vdd->pmic_info || !vdd->pmic_info->vsel_to_uv) {
467 pr_warning("%s: PMIC function to convert vsel to voltage"
468 "in uV not registerd\n", __func__);
469 return 0;
470 }
471
472 return vdd->pmic_info->vsel_to_uv(curr_vsel);
473}
474
475/**
476 * omap_vp_enable() - API to enable a particular VP
477 * @voltdm: pointer to the VDD whose VP is to be enabled.
478 *
479 * This API enables a particular voltage processor. Needed by the smartreflex
480 * class drivers.
481 */
482void omap_vp_enable(struct voltagedomain *voltdm)
483{
484 struct omap_vdd_info *vdd;
485 u32 vpconfig;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530486
487 if (!voltdm || IS_ERR(voltdm)) {
488 pr_warning("%s: VDD specified does not exist!\n", __func__);
489 return;
490 }
491
Kevin Hilman81a60482011-03-16 14:25:45 -0700492 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530493 if (!vdd->read_reg || !vdd->write_reg) {
494 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
495 __func__, voltdm->name);
496 return;
497 }
498
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530499 /* If VP is already enabled, do nothing. Return */
500 if (vdd->vp_enabled)
501 return;
502
Kevin Hilman81a60482011-03-16 14:25:45 -0700503 vp_latch_vsel(voltdm);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530504
505 /* Enable VP */
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700506 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700507 vpconfig |= vdd->vp_data->vp_common->vpconfig_vpenable;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700508 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530509 vdd->vp_enabled = true;
510}
511
512/**
513 * omap_vp_disable() - API to disable a particular VP
514 * @voltdm: pointer to the VDD whose VP is to be disabled.
515 *
516 * This API disables a particular voltage processor. Needed by the smartreflex
517 * class drivers.
518 */
519void omap_vp_disable(struct voltagedomain *voltdm)
520{
521 struct omap_vdd_info *vdd;
522 u32 vpconfig;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530523 int timeout;
524
525 if (!voltdm || IS_ERR(voltdm)) {
526 pr_warning("%s: VDD specified does not exist!\n", __func__);
527 return;
528 }
529
Kevin Hilman81a60482011-03-16 14:25:45 -0700530 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530531 if (!vdd->read_reg || !vdd->write_reg) {
532 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
533 __func__, voltdm->name);
534 return;
535 }
536
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530537 /* If VP is already disabled, do nothing. Return */
538 if (!vdd->vp_enabled) {
539 pr_warning("%s: Trying to disable VP for vdd_%s when"
540 "it is already disabled\n", __func__, voltdm->name);
541 return;
542 }
543
544 /* Disable VP */
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700545 vpconfig = vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700546 vpconfig &= ~vdd->vp_data->vp_common->vpconfig_vpenable;
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700547 vdd->write_reg(vpconfig, vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vpconfig);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530548
549 /*
550 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
551 */
Kevin Hilmana7460daf2011-03-16 13:35:22 -0700552 omap_test_timeout((vdd->read_reg(vdd->vp_data->vp_common->prm_mod, vdd->vp_data->vstatus)),
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530553 VP_IDLE_TIMEOUT, timeout);
554
555 if (timeout >= VP_IDLE_TIMEOUT)
556 pr_warning("%s: vdd_%s idle timedout\n",
557 __func__, voltdm->name);
558
559 vdd->vp_enabled = false;
560
561 return;
562}
563
564/**
565 * omap_voltage_scale_vdd() - API to scale voltage of a particular
566 * voltage domain.
567 * @voltdm: pointer to the VDD which is to be scaled.
568 * @target_volt: The target voltage of the voltage domain
569 *
570 * This API should be called by the kernel to do the voltage scaling
571 * for a particular voltage domain during dvfs or any other situation.
572 */
573int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
574 unsigned long target_volt)
575{
576 struct omap_vdd_info *vdd;
577
578 if (!voltdm || IS_ERR(voltdm)) {
579 pr_warning("%s: VDD specified does not exist!\n", __func__);
580 return -EINVAL;
581 }
582
Kevin Hilman81a60482011-03-16 14:25:45 -0700583 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530584
585 if (!vdd->volt_scale) {
586 pr_err("%s: No voltage scale API registered for vdd_%s\n",
587 __func__, voltdm->name);
588 return -ENODATA;
589 }
590
Kevin Hilman81a60482011-03-16 14:25:45 -0700591 return vdd->volt_scale(voltdm, target_volt);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530592}
593
594/**
595 * omap_voltage_reset() - Resets the voltage of a particular voltage domain
596 * to that of the current OPP.
597 * @voltdm: pointer to the VDD whose voltage is to be reset.
598 *
599 * This API finds out the correct voltage the voltage domain is supposed
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300600 * to be at and resets the voltage to that level. Should be used especially
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530601 * while disabling any voltage compensation modules.
602 */
603void omap_voltage_reset(struct voltagedomain *voltdm)
604{
605 unsigned long target_uvdc;
606
607 if (!voltdm || IS_ERR(voltdm)) {
608 pr_warning("%s: VDD specified does not exist!\n", __func__);
609 return;
610 }
611
612 target_uvdc = omap_voltage_get_nom_volt(voltdm);
613 if (!target_uvdc) {
614 pr_err("%s: unable to find current voltage for vdd_%s\n",
615 __func__, voltdm->name);
616 return;
617 }
618
619 omap_voltage_scale_vdd(voltdm, target_uvdc);
620}
621
622/**
623 * omap_voltage_get_volttable() - API to get the voltage table associated with a
624 * particular voltage domain.
625 * @voltdm: pointer to the VDD for which the voltage table is required
626 * @volt_data: the voltage table for the particular vdd which is to be
627 * populated by this API
628 *
629 * This API populates the voltage table associated with a VDD into the
630 * passed parameter pointer. Returns the count of distinct voltages
631 * supported by this vdd.
632 *
633 */
634void omap_voltage_get_volttable(struct voltagedomain *voltdm,
635 struct omap_volt_data **volt_data)
636{
637 struct omap_vdd_info *vdd;
638
639 if (!voltdm || IS_ERR(voltdm)) {
640 pr_warning("%s: VDD specified does not exist!\n", __func__);
641 return;
642 }
643
Kevin Hilman81a60482011-03-16 14:25:45 -0700644 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530645
646 *volt_data = vdd->volt_data;
647}
648
649/**
650 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
651 * particular voltage
652 * @voltdm: pointer to the VDD whose voltage table has to be searched
653 * @volt: the voltage to be searched in the voltage table
654 *
655 * This API searches through the voltage table for the required voltage
656 * domain and tries to find a matching entry for the passed voltage volt.
657 * If a matching entry is found volt_data is populated with that entry.
658 * This API searches only through the non-compensated voltages int the
659 * voltage table.
660 * Returns pointer to the voltage table entry corresponding to volt on
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300661 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530662 * domain or if there is no matching entry.
663 */
664struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
665 unsigned long volt)
666{
667 struct omap_vdd_info *vdd;
668 int i;
669
670 if (!voltdm || IS_ERR(voltdm)) {
671 pr_warning("%s: VDD specified does not exist!\n", __func__);
672 return ERR_PTR(-EINVAL);
673 }
674
Kevin Hilman81a60482011-03-16 14:25:45 -0700675 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530676
677 if (!vdd->volt_data) {
678 pr_warning("%s: voltage table does not exist for vdd_%s\n",
679 __func__, voltdm->name);
680 return ERR_PTR(-ENODATA);
681 }
682
683 for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
684 if (vdd->volt_data[i].volt_nominal == volt)
685 return &vdd->volt_data[i];
686 }
687
688 pr_notice("%s: Unable to match the current voltage with the voltage"
689 "table for vdd_%s\n", __func__, voltdm->name);
690
691 return ERR_PTR(-ENODATA);
692}
693
694/**
695 * omap_voltage_register_pmic() - API to register PMIC specific data
696 * @voltdm: pointer to the VDD for which the PMIC specific data is
697 * to be registered
698 * @pmic_info: the structure containing pmic info
699 *
700 * This API is to be called by the SOC/PMIC file to specify the
701 * pmic specific info as present in omap_volt_pmic_info structure.
702 */
703int omap_voltage_register_pmic(struct voltagedomain *voltdm,
704 struct omap_volt_pmic_info *pmic_info)
705{
706 struct omap_vdd_info *vdd;
707
708 if (!voltdm || IS_ERR(voltdm)) {
709 pr_warning("%s: VDD specified does not exist!\n", __func__);
710 return -EINVAL;
711 }
712
Kevin Hilman81a60482011-03-16 14:25:45 -0700713 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530714
715 vdd->pmic_info = pmic_info;
716
717 return 0;
718}
719
720/**
721 * omap_voltage_get_dbgdir() - API to get pointer to the debugfs directory
722 * corresponding to a voltage domain.
723 *
724 * @voltdm: pointer to the VDD whose debug directory is required.
725 *
726 * This API returns pointer to the debugfs directory corresponding
727 * to the voltage domain. Should be used by drivers requiring to
728 * add any debug entry for a particular voltage domain. Returns NULL
729 * in case of error.
730 */
731struct dentry *omap_voltage_get_dbgdir(struct voltagedomain *voltdm)
732{
733 struct omap_vdd_info *vdd;
734
735 if (!voltdm || IS_ERR(voltdm)) {
736 pr_warning("%s: VDD specified does not exist!\n", __func__);
737 return NULL;
738 }
739
Kevin Hilman81a60482011-03-16 14:25:45 -0700740 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530741
742 return vdd->debug_dir;
743}
744
745/**
746 * omap_change_voltscale_method() - API to change the voltage scaling method.
747 * @voltdm: pointer to the VDD whose voltage scaling method
748 * has to be changed.
749 * @voltscale_method: the method to be used for voltage scaling.
750 *
751 * This API can be used by the board files to change the method of voltage
752 * scaling between vpforceupdate and vcbypass. The parameter values are
753 * defined in voltage.h
754 */
755void omap_change_voltscale_method(struct voltagedomain *voltdm,
756 int voltscale_method)
757{
758 struct omap_vdd_info *vdd;
759
760 if (!voltdm || IS_ERR(voltdm)) {
761 pr_warning("%s: VDD specified does not exist!\n", __func__);
762 return;
763 }
764
Kevin Hilman81a60482011-03-16 14:25:45 -0700765 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530766
767 switch (voltscale_method) {
768 case VOLTSCALE_VPFORCEUPDATE:
769 vdd->volt_scale = vp_forceupdate_scale_voltage;
770 return;
771 case VOLTSCALE_VCBYPASS:
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700772 vdd->volt_scale = omap_vc_bypass_scale;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530773 return;
774 default:
775 pr_warning("%s: Trying to change the method of voltage scaling"
776 "to an unsupported one!\n", __func__);
777 }
778}
779
780/**
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530781 * omap_voltage_late_init() - Init the various voltage parameters
782 *
783 * This API is to be called in the later stages of the
784 * system boot to init the voltage controller and
785 * voltage processors.
786 */
787int __init omap_voltage_late_init(void)
788{
Kevin Hilman81a60482011-03-16 14:25:45 -0700789 struct voltagedomain *voltdm;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530790
Kevin Hilman81a60482011-03-16 14:25:45 -0700791 if (list_empty(&voltdm_list)) {
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530792 pr_err("%s: Voltage driver support not added\n",
793 __func__);
794 return -EINVAL;
795 }
796
797 voltage_dir = debugfs_create_dir("voltage", NULL);
798 if (IS_ERR(voltage_dir))
799 pr_err("%s: Unable to create voltage debugfs main dir\n",
800 __func__);
Kevin Hilman81a60482011-03-16 14:25:45 -0700801 list_for_each_entry(voltdm, &voltdm_list, node) {
Kevin Hilman37efca72011-03-23 17:00:21 -0700802 if (!voltdm->scalable)
803 continue;
804
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700805 if (voltdm->vc)
806 omap_vc_init_channel(voltdm);
807
Kevin Hilman81a60482011-03-16 14:25:45 -0700808 if (voltdm->vdd) {
809 if (omap_vdd_data_configure(voltdm))
810 continue;
Kevin Hilman81a60482011-03-16 14:25:45 -0700811 vp_init(voltdm);
812 vdd_debugfs_init(voltdm);
813 }
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530814 }
815
816 return 0;
817}
818
Kevin Hilman81a60482011-03-16 14:25:45 -0700819static struct voltagedomain *_voltdm_lookup(const char *name)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530820{
Kevin Hilman81a60482011-03-16 14:25:45 -0700821 struct voltagedomain *voltdm, *temp_voltdm;
822
823 voltdm = NULL;
824
825 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
826 if (!strcmp(name, temp_voltdm->name)) {
827 voltdm = temp_voltdm;
828 break;
829 }
830 }
831
832 return voltdm;
833}
834
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700835/**
836 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
837 * @voltdm: struct voltagedomain * to add the powerdomain to
838 * @pwrdm: struct powerdomain * to associate with a voltagedomain
839 *
840 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
841 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
842 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
843 * or 0 upon success.
844 */
845int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
846{
847 if (!voltdm || !pwrdm)
848 return -EINVAL;
849
850 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
851 "%s\n", pwrdm->name, voltdm->name);
852
853 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
854
855 return 0;
856}
857
858/**
859 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
860 * @voltdm: struct voltagedomain * to iterate over
861 * @fn: callback function *
862 *
863 * Call the supplied function @fn for each powerdomain in the
864 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
865 * pointers; or passes along the last return value of the callback
866 * function, which should be 0 for success or anything else to
867 * indicate failure.
868 */
869int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
870 int (*fn)(struct voltagedomain *voltdm,
871 struct powerdomain *pwrdm))
872{
873 struct powerdomain *pwrdm;
874 int ret = 0;
875
876 if (!fn)
877 return -EINVAL;
878
879 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
880 ret = (*fn)(voltdm, pwrdm);
881
882 return ret;
883}
884
885/**
886 * voltdm_for_each - call function on each registered voltagedomain
887 * @fn: callback function *
888 *
889 * Call the supplied function @fn for each registered voltagedomain.
890 * The callback function @fn can return anything but 0 to bail out
891 * early from the iterator. Returns the last return value of the
892 * callback function, which should be 0 for success or anything else
893 * to indicate failure; or -EINVAL if the function pointer is null.
894 */
895int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
896 void *user)
897{
898 struct voltagedomain *temp_voltdm;
899 int ret = 0;
900
901 if (!fn)
902 return -EINVAL;
903
904 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
905 ret = (*fn)(temp_voltdm, user);
906 if (ret)
907 break;
908 }
909
910 return ret;
911}
912
Kevin Hilman81a60482011-03-16 14:25:45 -0700913static int _voltdm_register(struct voltagedomain *voltdm)
914{
915 if (!voltdm || !voltdm->name)
916 return -EINVAL;
917
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700918 INIT_LIST_HEAD(&voltdm->pwrdm_list);
Kevin Hilman81a60482011-03-16 14:25:45 -0700919 list_add(&voltdm->node, &voltdm_list);
920
921 pr_debug("voltagedomain: registered %s\n", voltdm->name);
922
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530923 return 0;
924}
Kevin Hilman81a60482011-03-16 14:25:45 -0700925
926/**
927 * voltdm_lookup - look up a voltagedomain by name, return a pointer
928 * @name: name of voltagedomain
929 *
930 * Find a registered voltagedomain by its name @name. Returns a pointer
931 * to the struct voltagedomain if found, or NULL otherwise.
932 */
933struct voltagedomain *voltdm_lookup(const char *name)
934{
935 struct voltagedomain *voltdm ;
936
937 if (!name)
938 return NULL;
939
940 voltdm = _voltdm_lookup(name);
941
942 return voltdm;
943}
944
945/**
946 * voltdm_init - set up the voltagedomain layer
947 * @voltdm_list: array of struct voltagedomain pointers to register
948 *
949 * Loop through the array of voltagedomains @voltdm_list, registering all
950 * that are available on the current CPU. If voltdm_list is supplied
951 * and not null, all of the referenced voltagedomains will be
952 * registered. No return value.
953 */
954void voltdm_init(struct voltagedomain **voltdms)
955{
956 struct voltagedomain **v;
957
958 if (voltdms) {
959 for (v = voltdms; *v; v++)
960 _voltdm_register(*v);
961 }
962}