RS485: fix inconsistencies in the meaning of some variables
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / mach-omap2 / voltage.c
blob1f8fdf736e630976bc97fc781d58bfbf85704179
1 /*
2 * OMAP3/OMAP4 Voltage Management Routines
4 * Author: Thara Gopinath <thara@ti.com>
6 * Copyright (C) 2007 Texas Instruments, Inc.
7 * Rajendra Nayak <rnayak@ti.com>
8 * Lesly A M <x0080970@ti.com>
10 * Copyright (C) 2008, 2011 Nokia Corporation
11 * Kalle Jokiniemi
12 * Paul Walmsley
14 * Copyright (C) 2010 Texas Instruments, Inc.
15 * Thara Gopinath <thara@ti.com>
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.
22 #include <linux/delay.h>
23 #include <linux/io.h>
24 #include <linux/err.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <linux/slab.h>
28 #include <linux/clk.h>
30 #include <plat/common.h>
32 #include "prm-regbits-34xx.h"
33 #include "prm-regbits-44xx.h"
34 #include "prm44xx.h"
35 #include "prcm44xx.h"
36 #include "prminst44xx.h"
37 #include "control.h"
39 #include "voltage.h"
40 #include "powerdomain.h"
42 #include "vc.h"
43 #include "vp.h"
45 static LIST_HEAD(voltdm_list);
47 /* Public functions */
48 /**
49 * voltdm_get_voltage() - Gets the current non-auto-compensated voltage
50 * @voltdm: pointer to the voltdm for which current voltage info is needed
52 * API to get the current non-auto-compensated voltage for a voltage domain.
53 * Returns 0 in case of error else returns the current voltage.
55 unsigned long voltdm_get_voltage(struct voltagedomain *voltdm)
57 if (!voltdm || IS_ERR(voltdm)) {
58 pr_warning("%s: VDD specified does not exist!\n", __func__);
59 return 0;
62 return voltdm->nominal_volt;
65 /**
66 * voltdm_scale() - API to scale voltage of a particular voltage domain.
67 * @voltdm: pointer to the voltage domain which is to be scaled.
68 * @target_volt: The target voltage of the voltage domain
70 * This API should be called by the kernel to do the voltage scaling
71 * for a particular voltage domain during DVFS.
73 int voltdm_scale(struct voltagedomain *voltdm,
74 unsigned long target_volt)
76 int ret;
78 if (!voltdm || IS_ERR(voltdm)) {
79 pr_warning("%s: VDD specified does not exist!\n", __func__);
80 return -EINVAL;
83 if (!voltdm->scale) {
84 pr_err("%s: No voltage scale API registered for vdd_%s\n",
85 __func__, voltdm->name);
86 return -ENODATA;
89 ret = voltdm->scale(voltdm, target_volt);
90 if (!ret)
91 voltdm->nominal_volt = target_volt;
93 return ret;
96 /**
97 * voltdm_reset() - Resets the voltage of a particular voltage domain
98 * to that of the current OPP.
99 * @voltdm: pointer to the voltage domain whose voltage is to be reset.
101 * This API finds out the correct voltage the voltage domain is supposed
102 * to be at and resets the voltage to that level. Should be used especially
103 * while disabling any voltage compensation modules.
105 void voltdm_reset(struct voltagedomain *voltdm)
107 unsigned long target_volt;
109 if (!voltdm || IS_ERR(voltdm)) {
110 pr_warning("%s: VDD specified does not exist!\n", __func__);
111 return;
114 target_volt = voltdm_get_voltage(voltdm);
115 if (!target_volt) {
116 pr_err("%s: unable to find current voltage for vdd_%s\n",
117 __func__, voltdm->name);
118 return;
121 voltdm_scale(voltdm, target_volt);
125 * omap_voltage_get_volttable() - API to get the voltage table associated with a
126 * particular voltage domain.
127 * @voltdm: pointer to the VDD for which the voltage table is required
128 * @volt_data: the voltage table for the particular vdd which is to be
129 * populated by this API
131 * This API populates the voltage table associated with a VDD into the
132 * passed parameter pointer. Returns the count of distinct voltages
133 * supported by this vdd.
136 void omap_voltage_get_volttable(struct voltagedomain *voltdm,
137 struct omap_volt_data **volt_data)
139 if (!voltdm || IS_ERR(voltdm)) {
140 pr_warning("%s: VDD specified does not exist!\n", __func__);
141 return;
144 *volt_data = voltdm->volt_data;
148 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
149 * particular voltage
150 * @voltdm: pointer to the VDD whose voltage table has to be searched
151 * @volt: the voltage to be searched in the voltage table
153 * This API searches through the voltage table for the required voltage
154 * domain and tries to find a matching entry for the passed voltage volt.
155 * If a matching entry is found volt_data is populated with that entry.
156 * This API searches only through the non-compensated voltages int the
157 * voltage table.
158 * Returns pointer to the voltage table entry corresponding to volt on
159 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
160 * domain or if there is no matching entry.
162 struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
163 unsigned long volt)
165 int i;
167 if (!voltdm || IS_ERR(voltdm)) {
168 pr_warning("%s: VDD specified does not exist!\n", __func__);
169 return ERR_PTR(-EINVAL);
172 if (!voltdm->volt_data) {
173 pr_warning("%s: voltage table does not exist for vdd_%s\n",
174 __func__, voltdm->name);
175 return ERR_PTR(-ENODATA);
178 for (i = 0; voltdm->volt_data[i].volt_nominal != 0; i++) {
179 if (voltdm->volt_data[i].volt_nominal == volt)
180 return &voltdm->volt_data[i];
183 pr_notice("%s: Unable to match the current voltage with the voltage"
184 "table for vdd_%s\n", __func__, voltdm->name);
186 return ERR_PTR(-ENODATA);
190 * omap_voltage_register_pmic() - API to register PMIC specific data
191 * @voltdm: pointer to the VDD for which the PMIC specific data is
192 * to be registered
193 * @pmic: the structure containing pmic info
195 * This API is to be called by the SOC/PMIC file to specify the
196 * pmic specific info as present in omap_voltdm_pmic structure.
198 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
199 struct omap_voltdm_pmic *pmic)
201 if (!voltdm || IS_ERR(voltdm)) {
202 pr_warning("%s: VDD specified does not exist!\n", __func__);
203 return -EINVAL;
206 voltdm->pmic = pmic;
208 return 0;
212 * omap_change_voltscale_method() - API to change the voltage scaling method.
213 * @voltdm: pointer to the VDD whose voltage scaling method
214 * has to be changed.
215 * @voltscale_method: the method to be used for voltage scaling.
217 * This API can be used by the board files to change the method of voltage
218 * scaling between vpforceupdate and vcbypass. The parameter values are
219 * defined in voltage.h
221 void omap_change_voltscale_method(struct voltagedomain *voltdm,
222 int voltscale_method)
224 if (!voltdm || IS_ERR(voltdm)) {
225 pr_warning("%s: VDD specified does not exist!\n", __func__);
226 return;
229 switch (voltscale_method) {
230 case VOLTSCALE_VPFORCEUPDATE:
231 voltdm->scale = omap_vp_forceupdate_scale;
232 return;
233 case VOLTSCALE_VCBYPASS:
234 voltdm->scale = omap_vc_bypass_scale;
235 return;
236 default:
237 pr_warning("%s: Trying to change the method of voltage scaling"
238 "to an unsupported one!\n", __func__);
243 * omap_voltage_late_init() - Init the various voltage parameters
245 * This API is to be called in the later stages of the
246 * system boot to init the voltage controller and
247 * voltage processors.
249 int __init omap_voltage_late_init(void)
251 struct voltagedomain *voltdm;
253 if (list_empty(&voltdm_list)) {
254 pr_err("%s: Voltage driver support not added\n",
255 __func__);
256 return -EINVAL;
259 list_for_each_entry(voltdm, &voltdm_list, node) {
260 struct clk *sys_ck;
262 if (!voltdm->scalable)
263 continue;
265 sys_ck = clk_get(NULL, voltdm->sys_clk.name);
266 if (IS_ERR(sys_ck)) {
267 pr_warning("%s: Could not get sys clk.\n", __func__);
268 return -EINVAL;
270 voltdm->sys_clk.rate = clk_get_rate(sys_ck);
271 WARN_ON(!voltdm->sys_clk.rate);
272 clk_put(sys_ck);
274 if (voltdm->vc) {
275 voltdm->scale = omap_vc_bypass_scale;
276 omap_vc_init_channel(voltdm);
279 if (voltdm->vp) {
280 voltdm->scale = omap_vp_forceupdate_scale;
281 omap_vp_init(voltdm);
285 return 0;
288 static struct voltagedomain *_voltdm_lookup(const char *name)
290 struct voltagedomain *voltdm, *temp_voltdm;
292 voltdm = NULL;
294 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
295 if (!strcmp(name, temp_voltdm->name)) {
296 voltdm = temp_voltdm;
297 break;
301 return voltdm;
305 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
306 * @voltdm: struct voltagedomain * to add the powerdomain to
307 * @pwrdm: struct powerdomain * to associate with a voltagedomain
309 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
310 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
311 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
312 * or 0 upon success.
314 int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
316 if (!voltdm || !pwrdm)
317 return -EINVAL;
319 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
320 "%s\n", pwrdm->name, voltdm->name);
322 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
324 return 0;
328 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
329 * @voltdm: struct voltagedomain * to iterate over
330 * @fn: callback function *
332 * Call the supplied function @fn for each powerdomain in the
333 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
334 * pointers; or passes along the last return value of the callback
335 * function, which should be 0 for success or anything else to
336 * indicate failure.
338 int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
339 int (*fn)(struct voltagedomain *voltdm,
340 struct powerdomain *pwrdm))
342 struct powerdomain *pwrdm;
343 int ret = 0;
345 if (!fn)
346 return -EINVAL;
348 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
349 ret = (*fn)(voltdm, pwrdm);
351 return ret;
355 * voltdm_for_each - call function on each registered voltagedomain
356 * @fn: callback function *
358 * Call the supplied function @fn for each registered voltagedomain.
359 * The callback function @fn can return anything but 0 to bail out
360 * early from the iterator. Returns the last return value of the
361 * callback function, which should be 0 for success or anything else
362 * to indicate failure; or -EINVAL if the function pointer is null.
364 int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
365 void *user)
367 struct voltagedomain *temp_voltdm;
368 int ret = 0;
370 if (!fn)
371 return -EINVAL;
373 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
374 ret = (*fn)(temp_voltdm, user);
375 if (ret)
376 break;
379 return ret;
382 static int _voltdm_register(struct voltagedomain *voltdm)
384 if (!voltdm || !voltdm->name)
385 return -EINVAL;
387 INIT_LIST_HEAD(&voltdm->pwrdm_list);
388 list_add(&voltdm->node, &voltdm_list);
390 pr_debug("voltagedomain: registered %s\n", voltdm->name);
392 return 0;
396 * voltdm_lookup - look up a voltagedomain by name, return a pointer
397 * @name: name of voltagedomain
399 * Find a registered voltagedomain by its name @name. Returns a pointer
400 * to the struct voltagedomain if found, or NULL otherwise.
402 struct voltagedomain *voltdm_lookup(const char *name)
404 struct voltagedomain *voltdm ;
406 if (!name)
407 return NULL;
409 voltdm = _voltdm_lookup(name);
411 return voltdm;
415 * voltdm_init - set up the voltagedomain layer
416 * @voltdm_list: array of struct voltagedomain pointers to register
418 * Loop through the array of voltagedomains @voltdm_list, registering all
419 * that are available on the current CPU. If voltdm_list is supplied
420 * and not null, all of the referenced voltagedomains will be
421 * registered. No return value.
423 void voltdm_init(struct voltagedomain **voltdms)
425 struct voltagedomain **v;
427 if (voltdms) {
428 for (v = voltdms; *v; v++)
429 _voltdm_register(*v);