blob: 9b9f01973d122251157f6b3f6898d2cc391a0b44 [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
Thara Gopinath2f34ce82010-05-29 22:02:21 +053049static u32 omap3_voltage_read_reg(u16 mod, u8 offset)
50{
51 return omap2_prm_read_mod_reg(mod, offset);
52}
53
54static void omap3_voltage_write_reg(u32 val, u16 mod, u8 offset)
55{
56 omap2_prm_write_mod_reg(val, mod, offset);
57}
58
Thara Gopinathbd381072010-12-10 23:15:23 +053059static u32 omap4_voltage_read_reg(u16 mod, u8 offset)
60{
61 return omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
62 mod, offset);
63}
64
65static void omap4_voltage_write_reg(u32 val, u16 mod, u8 offset)
66{
67 omap4_prminst_write_inst_reg(val, OMAP4430_PRM_PARTITION, mod, offset);
68}
69
Kevin Hilman81a60482011-03-16 14:25:45 -070070static int __init _config_common_vdd_data(struct voltagedomain *voltdm)
Paul Walmsleyc0718df2011-03-10 22:17:45 -070071{
72 char *sys_ck_name;
73 struct clk *sys_ck;
74 u32 sys_clk_speed, timeout_val, waittime;
Kevin Hilman81a60482011-03-16 14:25:45 -070075 struct omap_vdd_info *vdd = voltdm->vdd;
Paul Walmsleyc0718df2011-03-10 22:17:45 -070076
77 /*
78 * XXX Clockfw should handle this, or this should be in a
79 * struct record
80 */
81 if (cpu_is_omap24xx() || cpu_is_omap34xx())
82 sys_ck_name = "sys_ck";
83 else if (cpu_is_omap44xx())
84 sys_ck_name = "sys_clkin_ck";
85 else
86 return -EINVAL;
87
88 /*
89 * Sys clk rate is require to calculate vp timeout value and
90 * smpswaittimemin and smpswaittimemax.
91 */
92 sys_ck = clk_get(NULL, sys_ck_name);
93 if (IS_ERR(sys_ck)) {
94 pr_warning("%s: Could not get the sys clk to calculate"
Kevin Hilman81a60482011-03-16 14:25:45 -070095 "various vdd_%s params\n", __func__, voltdm->name);
Paul Walmsleyc0718df2011-03-10 22:17:45 -070096 return -EINVAL;
97 }
98 sys_clk_speed = clk_get_rate(sys_ck);
99 clk_put(sys_ck);
100 /* Divide to avoid overflow */
101 sys_clk_speed /= 1000;
102
103 /* Generic voltage parameters */
Kevin Hilman01f48d32011-03-21 14:29:13 -0700104 vdd->volt_scale = omap_vp_forceupdate_scale;
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700105 vdd->vp_enabled = false;
106
107 vdd->vp_rt_data.vpconfig_erroroffset =
108 (vdd->pmic_info->vp_erroroffset <<
109 vdd->vp_data->vp_common->vpconfig_erroroffset_shift);
110
111 timeout_val = (sys_clk_speed * vdd->pmic_info->vp_timeout_us) / 1000;
112 vdd->vp_rt_data.vlimitto_timeout = timeout_val;
113 vdd->vp_rt_data.vlimitto_vddmin = vdd->pmic_info->vp_vddmin;
114 vdd->vp_rt_data.vlimitto_vddmax = vdd->pmic_info->vp_vddmax;
115
116 waittime = ((vdd->pmic_info->step_size / vdd->pmic_info->slew_rate) *
117 sys_clk_speed) / 1000;
118 vdd->vp_rt_data.vstepmin_smpswaittimemin = waittime;
119 vdd->vp_rt_data.vstepmax_smpswaittimemax = waittime;
120 vdd->vp_rt_data.vstepmin_stepmin = vdd->pmic_info->vp_vstepmin;
121 vdd->vp_rt_data.vstepmax_stepmax = vdd->pmic_info->vp_vstepmax;
122
123 return 0;
124}
125
Thara Gopinath077fcec2010-10-27 20:29:37 +0530126static int nom_volt_debug_get(void *data, u64 *val)
127{
Kevin Hilman81a60482011-03-16 14:25:45 -0700128 struct voltagedomain *voltdm = (struct voltagedomain *)data;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530129
Kevin Hilman81a60482011-03-16 14:25:45 -0700130 if (!voltdm) {
Thara Gopinath077fcec2010-10-27 20:29:37 +0530131 pr_warning("Wrong paramater passed\n");
132 return -EINVAL;
133 }
134
Kevin Hilman81a60482011-03-16 14:25:45 -0700135 *val = omap_voltage_get_nom_volt(voltdm);
Thara Gopinath077fcec2010-10-27 20:29:37 +0530136
137 return 0;
138}
139
Thara Gopinath077fcec2010-10-27 20:29:37 +0530140DEFINE_SIMPLE_ATTRIBUTE(nom_volt_debug_fops, nom_volt_debug_get, NULL,
141 "%llu\n");
Kevin Hilman81a60482011-03-16 14:25:45 -0700142static void __init vdd_debugfs_init(struct voltagedomain *voltdm)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530143{
144 char *name;
Kevin Hilman81a60482011-03-16 14:25:45 -0700145 struct omap_vdd_info *vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530146
147 name = kzalloc(VOLTAGE_DIR_SIZE, GFP_KERNEL);
148 if (!name) {
149 pr_warning("%s: Unable to allocate memory for debugfs"
150 " directory name for vdd_%s",
Kevin Hilman81a60482011-03-16 14:25:45 -0700151 __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530152 return;
153 }
154 strcpy(name, "vdd_");
Kevin Hilman81a60482011-03-16 14:25:45 -0700155 strcat(name, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530156
157 vdd->debug_dir = debugfs_create_dir(name, voltage_dir);
Aaro Koskinen62270112011-01-28 18:24:11 +0200158 kfree(name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530159 if (IS_ERR(vdd->debug_dir)) {
160 pr_warning("%s: Unable to create debugfs directory for"
Kevin Hilman81a60482011-03-16 14:25:45 -0700161 " vdd_%s\n", __func__, voltdm->name);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530162 vdd->debug_dir = NULL;
Thara Gopinath077fcec2010-10-27 20:29:37 +0530163 return;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530164 }
Thara Gopinath077fcec2010-10-27 20:29:37 +0530165
Thara Gopinath077fcec2010-10-27 20:29:37 +0530166 (void) debugfs_create_file("curr_nominal_volt", S_IRUGO,
Kevin Hilman81a60482011-03-16 14:25:45 -0700167 vdd->debug_dir, (void *) voltdm,
Thara Gopinath077fcec2010-10-27 20:29:37 +0530168 &nom_volt_debug_fops);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530169}
170
Kevin Hilman81a60482011-03-16 14:25:45 -0700171static int __init omap_vdd_data_configure(struct voltagedomain *voltdm)
Thara Gopinathbd381072010-12-10 23:15:23 +0530172{
Kevin Hilman81a60482011-03-16 14:25:45 -0700173 struct omap_vdd_info *vdd = voltdm->vdd;
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700174 int ret = -EINVAL;
Thara Gopinathbd381072010-12-10 23:15:23 +0530175
176 if (!vdd->pmic_info) {
177 pr_err("%s: PMIC info requried to configure vdd_%s not"
178 "populated.Hence cannot initialize vdd_%s\n",
Kevin Hilman81a60482011-03-16 14:25:45 -0700179 __func__, voltdm->name, voltdm->name);
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700180 goto ovdc_out;
Thara Gopinathbd381072010-12-10 23:15:23 +0530181 }
182
Kevin Hilman81a60482011-03-16 14:25:45 -0700183 if (IS_ERR_VALUE(_config_common_vdd_data(voltdm)))
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700184 goto ovdc_out;
185
186 if (cpu_is_omap34xx()) {
187 vdd->read_reg = omap3_voltage_read_reg;
188 vdd->write_reg = omap3_voltage_write_reg;
189 ret = 0;
190 } else if (cpu_is_omap44xx()) {
191 vdd->read_reg = omap4_voltage_read_reg;
192 vdd->write_reg = omap4_voltage_write_reg;
193 ret = 0;
Thara Gopinathbd381072010-12-10 23:15:23 +0530194 }
195
Paul Walmsleyc0718df2011-03-10 22:17:45 -0700196ovdc_out:
197 return ret;
Thara Gopinathbd381072010-12-10 23:15:23 +0530198}
199
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530200/* Public functions */
201/**
202 * omap_voltage_get_nom_volt() - Gets the current non-auto-compensated voltage
203 * @voltdm: pointer to the VDD for which current voltage info is needed
204 *
205 * API to get the current non-auto-compensated voltage for a VDD.
206 * Returns 0 in case of error else returns the current voltage for the VDD.
207 */
208unsigned long omap_voltage_get_nom_volt(struct voltagedomain *voltdm)
209{
210 struct omap_vdd_info *vdd;
211
212 if (!voltdm || IS_ERR(voltdm)) {
213 pr_warning("%s: VDD specified does not exist!\n", __func__);
214 return 0;
215 }
216
Kevin Hilman81a60482011-03-16 14:25:45 -0700217 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530218
219 return vdd->curr_volt;
220}
221
222/**
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530223 * omap_voltage_scale_vdd() - API to scale voltage of a particular
224 * voltage domain.
225 * @voltdm: pointer to the VDD which is to be scaled.
226 * @target_volt: The target voltage of the voltage domain
227 *
228 * This API should be called by the kernel to do the voltage scaling
229 * for a particular voltage domain during dvfs or any other situation.
230 */
231int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
232 unsigned long target_volt)
233{
234 struct omap_vdd_info *vdd;
235
236 if (!voltdm || IS_ERR(voltdm)) {
237 pr_warning("%s: VDD specified does not exist!\n", __func__);
238 return -EINVAL;
239 }
240
Kevin Hilman81a60482011-03-16 14:25:45 -0700241 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530242
243 if (!vdd->volt_scale) {
244 pr_err("%s: No voltage scale API registered for vdd_%s\n",
245 __func__, voltdm->name);
246 return -ENODATA;
247 }
248
Kevin Hilman81a60482011-03-16 14:25:45 -0700249 return vdd->volt_scale(voltdm, target_volt);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530250}
251
252/**
253 * omap_voltage_reset() - Resets the voltage of a particular voltage domain
254 * to that of the current OPP.
255 * @voltdm: pointer to the VDD whose voltage is to be reset.
256 *
257 * This API finds out the correct voltage the voltage domain is supposed
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300258 * to be at and resets the voltage to that level. Should be used especially
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530259 * while disabling any voltage compensation modules.
260 */
261void omap_voltage_reset(struct voltagedomain *voltdm)
262{
263 unsigned long target_uvdc;
264
265 if (!voltdm || IS_ERR(voltdm)) {
266 pr_warning("%s: VDD specified does not exist!\n", __func__);
267 return;
268 }
269
270 target_uvdc = omap_voltage_get_nom_volt(voltdm);
271 if (!target_uvdc) {
272 pr_err("%s: unable to find current voltage for vdd_%s\n",
273 __func__, voltdm->name);
274 return;
275 }
276
277 omap_voltage_scale_vdd(voltdm, target_uvdc);
278}
279
280/**
281 * omap_voltage_get_volttable() - API to get the voltage table associated with a
282 * particular voltage domain.
283 * @voltdm: pointer to the VDD for which the voltage table is required
284 * @volt_data: the voltage table for the particular vdd which is to be
285 * populated by this API
286 *
287 * This API populates the voltage table associated with a VDD into the
288 * passed parameter pointer. Returns the count of distinct voltages
289 * supported by this vdd.
290 *
291 */
292void omap_voltage_get_volttable(struct voltagedomain *voltdm,
293 struct omap_volt_data **volt_data)
294{
295 struct omap_vdd_info *vdd;
296
297 if (!voltdm || IS_ERR(voltdm)) {
298 pr_warning("%s: VDD specified does not exist!\n", __func__);
299 return;
300 }
301
Kevin Hilman81a60482011-03-16 14:25:45 -0700302 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530303
304 *volt_data = vdd->volt_data;
305}
306
307/**
308 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
309 * particular voltage
310 * @voltdm: pointer to the VDD whose voltage table has to be searched
311 * @volt: the voltage to be searched in the voltage table
312 *
313 * This API searches through the voltage table for the required voltage
314 * domain and tries to find a matching entry for the passed voltage volt.
315 * If a matching entry is found volt_data is populated with that entry.
316 * This API searches only through the non-compensated voltages int the
317 * voltage table.
318 * Returns pointer to the voltage table entry corresponding to volt on
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300319 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530320 * domain or if there is no matching entry.
321 */
322struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
323 unsigned long volt)
324{
325 struct omap_vdd_info *vdd;
326 int i;
327
328 if (!voltdm || IS_ERR(voltdm)) {
329 pr_warning("%s: VDD specified does not exist!\n", __func__);
330 return ERR_PTR(-EINVAL);
331 }
332
Kevin Hilman81a60482011-03-16 14:25:45 -0700333 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530334
335 if (!vdd->volt_data) {
336 pr_warning("%s: voltage table does not exist for vdd_%s\n",
337 __func__, voltdm->name);
338 return ERR_PTR(-ENODATA);
339 }
340
341 for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
342 if (vdd->volt_data[i].volt_nominal == volt)
343 return &vdd->volt_data[i];
344 }
345
346 pr_notice("%s: Unable to match the current voltage with the voltage"
347 "table for vdd_%s\n", __func__, voltdm->name);
348
349 return ERR_PTR(-ENODATA);
350}
351
352/**
353 * omap_voltage_register_pmic() - API to register PMIC specific data
354 * @voltdm: pointer to the VDD for which the PMIC specific data is
355 * to be registered
356 * @pmic_info: the structure containing pmic info
357 *
358 * This API is to be called by the SOC/PMIC file to specify the
359 * pmic specific info as present in omap_volt_pmic_info structure.
360 */
361int omap_voltage_register_pmic(struct voltagedomain *voltdm,
362 struct omap_volt_pmic_info *pmic_info)
363{
364 struct omap_vdd_info *vdd;
365
366 if (!voltdm || IS_ERR(voltdm)) {
367 pr_warning("%s: VDD specified does not exist!\n", __func__);
368 return -EINVAL;
369 }
370
Kevin Hilman81a60482011-03-16 14:25:45 -0700371 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530372
373 vdd->pmic_info = pmic_info;
374
375 return 0;
376}
377
378/**
379 * omap_voltage_get_dbgdir() - API to get pointer to the debugfs directory
380 * corresponding to a voltage domain.
381 *
382 * @voltdm: pointer to the VDD whose debug directory is required.
383 *
384 * This API returns pointer to the debugfs directory corresponding
385 * to the voltage domain. Should be used by drivers requiring to
386 * add any debug entry for a particular voltage domain. Returns NULL
387 * in case of error.
388 */
389struct dentry *omap_voltage_get_dbgdir(struct voltagedomain *voltdm)
390{
391 struct omap_vdd_info *vdd;
392
393 if (!voltdm || IS_ERR(voltdm)) {
394 pr_warning("%s: VDD specified does not exist!\n", __func__);
395 return NULL;
396 }
397
Kevin Hilman81a60482011-03-16 14:25:45 -0700398 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530399
400 return vdd->debug_dir;
401}
402
403/**
404 * omap_change_voltscale_method() - API to change the voltage scaling method.
405 * @voltdm: pointer to the VDD whose voltage scaling method
406 * has to be changed.
407 * @voltscale_method: the method to be used for voltage scaling.
408 *
409 * This API can be used by the board files to change the method of voltage
410 * scaling between vpforceupdate and vcbypass. The parameter values are
411 * defined in voltage.h
412 */
413void omap_change_voltscale_method(struct voltagedomain *voltdm,
414 int voltscale_method)
415{
416 struct omap_vdd_info *vdd;
417
418 if (!voltdm || IS_ERR(voltdm)) {
419 pr_warning("%s: VDD specified does not exist!\n", __func__);
420 return;
421 }
422
Kevin Hilman81a60482011-03-16 14:25:45 -0700423 vdd = voltdm->vdd;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530424
425 switch (voltscale_method) {
426 case VOLTSCALE_VPFORCEUPDATE:
Kevin Hilman01f48d32011-03-21 14:29:13 -0700427 vdd->volt_scale = omap_vp_forceupdate_scale;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530428 return;
429 case VOLTSCALE_VCBYPASS:
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700430 vdd->volt_scale = omap_vc_bypass_scale;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530431 return;
432 default:
433 pr_warning("%s: Trying to change the method of voltage scaling"
434 "to an unsupported one!\n", __func__);
435 }
436}
437
438/**
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530439 * omap_voltage_late_init() - Init the various voltage parameters
440 *
441 * This API is to be called in the later stages of the
442 * system boot to init the voltage controller and
443 * voltage processors.
444 */
445int __init omap_voltage_late_init(void)
446{
Kevin Hilman81a60482011-03-16 14:25:45 -0700447 struct voltagedomain *voltdm;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530448
Kevin Hilman81a60482011-03-16 14:25:45 -0700449 if (list_empty(&voltdm_list)) {
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530450 pr_err("%s: Voltage driver support not added\n",
451 __func__);
452 return -EINVAL;
453 }
454
455 voltage_dir = debugfs_create_dir("voltage", NULL);
456 if (IS_ERR(voltage_dir))
457 pr_err("%s: Unable to create voltage debugfs main dir\n",
458 __func__);
Kevin Hilman81a60482011-03-16 14:25:45 -0700459 list_for_each_entry(voltdm, &voltdm_list, node) {
Kevin Hilman37efca72011-03-23 17:00:21 -0700460 if (!voltdm->scalable)
461 continue;
462
Kevin Hilman4d475062011-07-18 15:48:22 -0700463 if (voltdm->vc) {
464 voltdm->vdd->volt_scale = omap_vc_bypass_scale;
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700465 omap_vc_init_channel(voltdm);
Kevin Hilman4d475062011-07-18 15:48:22 -0700466 }
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700467
Kevin Hilman81a60482011-03-16 14:25:45 -0700468 if (voltdm->vdd) {
469 if (omap_vdd_data_configure(voltdm))
470 continue;
Kevin Hilman81a60482011-03-16 14:25:45 -0700471 vdd_debugfs_init(voltdm);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700472 omap_vp_init(voltdm);
Kevin Hilman81a60482011-03-16 14:25:45 -0700473 }
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530474 }
475
476 return 0;
477}
478
Kevin Hilman81a60482011-03-16 14:25:45 -0700479static struct voltagedomain *_voltdm_lookup(const char *name)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530480{
Kevin Hilman81a60482011-03-16 14:25:45 -0700481 struct voltagedomain *voltdm, *temp_voltdm;
482
483 voltdm = NULL;
484
485 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
486 if (!strcmp(name, temp_voltdm->name)) {
487 voltdm = temp_voltdm;
488 break;
489 }
490 }
491
492 return voltdm;
493}
494
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700495/**
496 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
497 * @voltdm: struct voltagedomain * to add the powerdomain to
498 * @pwrdm: struct powerdomain * to associate with a voltagedomain
499 *
500 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
501 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
502 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
503 * or 0 upon success.
504 */
505int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
506{
507 if (!voltdm || !pwrdm)
508 return -EINVAL;
509
510 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
511 "%s\n", pwrdm->name, voltdm->name);
512
513 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
514
515 return 0;
516}
517
518/**
519 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
520 * @voltdm: struct voltagedomain * to iterate over
521 * @fn: callback function *
522 *
523 * Call the supplied function @fn for each powerdomain in the
524 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
525 * pointers; or passes along the last return value of the callback
526 * function, which should be 0 for success or anything else to
527 * indicate failure.
528 */
529int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
530 int (*fn)(struct voltagedomain *voltdm,
531 struct powerdomain *pwrdm))
532{
533 struct powerdomain *pwrdm;
534 int ret = 0;
535
536 if (!fn)
537 return -EINVAL;
538
539 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
540 ret = (*fn)(voltdm, pwrdm);
541
542 return ret;
543}
544
545/**
546 * voltdm_for_each - call function on each registered voltagedomain
547 * @fn: callback function *
548 *
549 * Call the supplied function @fn for each registered voltagedomain.
550 * The callback function @fn can return anything but 0 to bail out
551 * early from the iterator. Returns the last return value of the
552 * callback function, which should be 0 for success or anything else
553 * to indicate failure; or -EINVAL if the function pointer is null.
554 */
555int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
556 void *user)
557{
558 struct voltagedomain *temp_voltdm;
559 int ret = 0;
560
561 if (!fn)
562 return -EINVAL;
563
564 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
565 ret = (*fn)(temp_voltdm, user);
566 if (ret)
567 break;
568 }
569
570 return ret;
571}
572
Kevin Hilman81a60482011-03-16 14:25:45 -0700573static int _voltdm_register(struct voltagedomain *voltdm)
574{
575 if (!voltdm || !voltdm->name)
576 return -EINVAL;
577
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700578 INIT_LIST_HEAD(&voltdm->pwrdm_list);
Kevin Hilman81a60482011-03-16 14:25:45 -0700579 list_add(&voltdm->node, &voltdm_list);
580
581 pr_debug("voltagedomain: registered %s\n", voltdm->name);
582
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530583 return 0;
584}
Kevin Hilman81a60482011-03-16 14:25:45 -0700585
586/**
587 * voltdm_lookup - look up a voltagedomain by name, return a pointer
588 * @name: name of voltagedomain
589 *
590 * Find a registered voltagedomain by its name @name. Returns a pointer
591 * to the struct voltagedomain if found, or NULL otherwise.
592 */
593struct voltagedomain *voltdm_lookup(const char *name)
594{
595 struct voltagedomain *voltdm ;
596
597 if (!name)
598 return NULL;
599
600 voltdm = _voltdm_lookup(name);
601
602 return voltdm;
603}
604
605/**
606 * voltdm_init - set up the voltagedomain layer
607 * @voltdm_list: array of struct voltagedomain pointers to register
608 *
609 * Loop through the array of voltagedomains @voltdm_list, registering all
610 * that are available on the current CPU. If voltdm_list is supplied
611 * and not null, all of the referenced voltagedomains will be
612 * registered. No return value.
613 */
614void voltdm_init(struct voltagedomain **voltdms)
615{
616 struct voltagedomain **v;
617
618 if (voltdms) {
619 for (v = voltdms; *v; v++)
620 _voltdm_register(*v);
621 }
622}