OMAP3+: VP: combine setting init voltage into common function
[deliverable/linux.git] / arch / arm / mach-omap2 / voltage.c
CommitLineData
2f34ce81
TG
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 *
c0718df4 10 * Copyright (C) 2008, 2011 Nokia Corporation
2f34ce81 11 * Kalle Jokiniemi
c0718df4 12 * Paul Walmsley
2f34ce81
TG
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>
2f34ce81
TG
24#include <linux/err.h>
25#include <linux/debugfs.h>
26#include <linux/slab.h>
0e2f3d9c 27#include <linux/clk.h>
2f34ce81
TG
28
29#include <plat/common.h>
2f34ce81
TG
30
31#include "prm-regbits-34xx.h"
bd38107b
TG
32#include "prm-regbits-44xx.h"
33#include "prm44xx.h"
34#include "prcm44xx.h"
35#include "prminst44xx.h"
2f34ce81
TG
36#include "control.h"
37
e1d6f472 38#include "voltage.h"
e69c22b1 39#include "powerdomain.h"
e1d6f472 40
c0718df4
PW
41#include "vc.h"
42#include "vp.h"
43
81a60482 44static LIST_HEAD(voltdm_list);
2f34ce81 45
81a60482 46static int __init _config_common_vdd_data(struct voltagedomain *voltdm)
c0718df4 47{
c0718df4 48 /* Generic voltage parameters */
0f01565a 49 voltdm->scale = omap_vp_forceupdate_scale;
c0718df4
PW
50
51 return 0;
52}
53
81a60482 54static int __init omap_vdd_data_configure(struct voltagedomain *voltdm)
bd38107b 55{
c0718df4 56 int ret = -EINVAL;
bd38107b 57
ce8ebe0d 58 if (!voltdm->pmic) {
bd38107b
TG
59 pr_err("%s: PMIC info requried to configure vdd_%s not"
60 "populated.Hence cannot initialize vdd_%s\n",
81a60482 61 __func__, voltdm->name, voltdm->name);
c0718df4 62 goto ovdc_out;
bd38107b
TG
63 }
64
81a60482 65 if (IS_ERR_VALUE(_config_common_vdd_data(voltdm)))
c0718df4 66 goto ovdc_out;
bd38107b 67
4bcc475e 68 ret = 0;
bd38107b 69
c0718df4
PW
70ovdc_out:
71 return ret;
bd38107b
TG
72}
73
2f34ce81
TG
74/* Public functions */
75/**
76 * omap_voltage_get_nom_volt() - Gets the current non-auto-compensated voltage
77 * @voltdm: pointer to the VDD for which current voltage info is needed
78 *
79 * API to get the current non-auto-compensated voltage for a VDD.
80 * Returns 0 in case of error else returns the current voltage for the VDD.
81 */
82unsigned long omap_voltage_get_nom_volt(struct voltagedomain *voltdm)
83{
84 struct omap_vdd_info *vdd;
85
86 if (!voltdm || IS_ERR(voltdm)) {
87 pr_warning("%s: VDD specified does not exist!\n", __func__);
88 return 0;
89 }
90
81a60482 91 vdd = voltdm->vdd;
2f34ce81
TG
92
93 return vdd->curr_volt;
94}
95
2f34ce81
TG
96/**
97 * omap_voltage_scale_vdd() - API to scale voltage of a particular
98 * voltage domain.
99 * @voltdm: pointer to the VDD which is to be scaled.
100 * @target_volt: The target voltage of the voltage domain
101 *
102 * This API should be called by the kernel to do the voltage scaling
103 * for a particular voltage domain during dvfs or any other situation.
104 */
105int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
106 unsigned long target_volt)
107{
2f34ce81
TG
108 if (!voltdm || IS_ERR(voltdm)) {
109 pr_warning("%s: VDD specified does not exist!\n", __func__);
110 return -EINVAL;
111 }
112
0f01565a 113 if (!voltdm->scale) {
2f34ce81
TG
114 pr_err("%s: No voltage scale API registered for vdd_%s\n",
115 __func__, voltdm->name);
116 return -ENODATA;
117 }
118
0f01565a 119 return voltdm->scale(voltdm, target_volt);
2f34ce81
TG
120}
121
122/**
123 * omap_voltage_reset() - Resets the voltage of a particular voltage domain
124 * to that of the current OPP.
125 * @voltdm: pointer to the VDD whose voltage is to be reset.
126 *
127 * This API finds out the correct voltage the voltage domain is supposed
25985edc 128 * to be at and resets the voltage to that level. Should be used especially
2f34ce81
TG
129 * while disabling any voltage compensation modules.
130 */
131void omap_voltage_reset(struct voltagedomain *voltdm)
132{
133 unsigned long target_uvdc;
134
135 if (!voltdm || IS_ERR(voltdm)) {
136 pr_warning("%s: VDD specified does not exist!\n", __func__);
137 return;
138 }
139
140 target_uvdc = omap_voltage_get_nom_volt(voltdm);
141 if (!target_uvdc) {
142 pr_err("%s: unable to find current voltage for vdd_%s\n",
143 __func__, voltdm->name);
144 return;
145 }
146
147 omap_voltage_scale_vdd(voltdm, target_uvdc);
148}
149
150/**
151 * omap_voltage_get_volttable() - API to get the voltage table associated with a
152 * particular voltage domain.
153 * @voltdm: pointer to the VDD for which the voltage table is required
154 * @volt_data: the voltage table for the particular vdd which is to be
155 * populated by this API
156 *
157 * This API populates the voltage table associated with a VDD into the
158 * passed parameter pointer. Returns the count of distinct voltages
159 * supported by this vdd.
160 *
161 */
162void omap_voltage_get_volttable(struct voltagedomain *voltdm,
163 struct omap_volt_data **volt_data)
164{
165 struct omap_vdd_info *vdd;
166
167 if (!voltdm || IS_ERR(voltdm)) {
168 pr_warning("%s: VDD specified does not exist!\n", __func__);
169 return;
170 }
171
81a60482 172 vdd = voltdm->vdd;
2f34ce81
TG
173
174 *volt_data = vdd->volt_data;
175}
176
177/**
178 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
179 * particular voltage
180 * @voltdm: pointer to the VDD whose voltage table has to be searched
181 * @volt: the voltage to be searched in the voltage table
182 *
183 * This API searches through the voltage table for the required voltage
184 * domain and tries to find a matching entry for the passed voltage volt.
185 * If a matching entry is found volt_data is populated with that entry.
186 * This API searches only through the non-compensated voltages int the
187 * voltage table.
188 * Returns pointer to the voltage table entry corresponding to volt on
25985edc 189 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
2f34ce81
TG
190 * domain or if there is no matching entry.
191 */
192struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
193 unsigned long volt)
194{
195 struct omap_vdd_info *vdd;
196 int i;
197
198 if (!voltdm || IS_ERR(voltdm)) {
199 pr_warning("%s: VDD specified does not exist!\n", __func__);
200 return ERR_PTR(-EINVAL);
201 }
202
81a60482 203 vdd = voltdm->vdd;
2f34ce81
TG
204
205 if (!vdd->volt_data) {
206 pr_warning("%s: voltage table does not exist for vdd_%s\n",
207 __func__, voltdm->name);
208 return ERR_PTR(-ENODATA);
209 }
210
211 for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
212 if (vdd->volt_data[i].volt_nominal == volt)
213 return &vdd->volt_data[i];
214 }
215
216 pr_notice("%s: Unable to match the current voltage with the voltage"
217 "table for vdd_%s\n", __func__, voltdm->name);
218
219 return ERR_PTR(-ENODATA);
220}
221
222/**
223 * omap_voltage_register_pmic() - API to register PMIC specific data
224 * @voltdm: pointer to the VDD for which the PMIC specific data is
225 * to be registered
ce8ebe0d 226 * @pmic: the structure containing pmic info
2f34ce81
TG
227 *
228 * This API is to be called by the SOC/PMIC file to specify the
ce8ebe0d 229 * pmic specific info as present in omap_voltdm_pmic structure.
2f34ce81
TG
230 */
231int omap_voltage_register_pmic(struct voltagedomain *voltdm,
ce8ebe0d 232 struct omap_voltdm_pmic *pmic)
2f34ce81 233{
2f34ce81
TG
234 if (!voltdm || IS_ERR(voltdm)) {
235 pr_warning("%s: VDD specified does not exist!\n", __func__);
236 return -EINVAL;
237 }
238
ce8ebe0d 239 voltdm->pmic = pmic;
2f34ce81
TG
240
241 return 0;
242}
243
2f34ce81
TG
244/**
245 * omap_change_voltscale_method() - API to change the voltage scaling method.
246 * @voltdm: pointer to the VDD whose voltage scaling method
247 * has to be changed.
248 * @voltscale_method: the method to be used for voltage scaling.
249 *
250 * This API can be used by the board files to change the method of voltage
251 * scaling between vpforceupdate and vcbypass. The parameter values are
252 * defined in voltage.h
253 */
254void omap_change_voltscale_method(struct voltagedomain *voltdm,
0f01565a 255 int voltscale_method)
2f34ce81 256{
2f34ce81
TG
257 if (!voltdm || IS_ERR(voltdm)) {
258 pr_warning("%s: VDD specified does not exist!\n", __func__);
259 return;
260 }
261
2f34ce81
TG
262 switch (voltscale_method) {
263 case VOLTSCALE_VPFORCEUPDATE:
0f01565a 264 voltdm->scale = omap_vp_forceupdate_scale;
2f34ce81
TG
265 return;
266 case VOLTSCALE_VCBYPASS:
0f01565a 267 voltdm->scale = omap_vc_bypass_scale;
2f34ce81
TG
268 return;
269 default:
270 pr_warning("%s: Trying to change the method of voltage scaling"
271 "to an unsupported one!\n", __func__);
272 }
273}
274
2f34ce81
TG
275/**
276 * omap_voltage_late_init() - Init the various voltage parameters
277 *
278 * This API is to be called in the later stages of the
279 * system boot to init the voltage controller and
280 * voltage processors.
281 */
282int __init omap_voltage_late_init(void)
283{
81a60482 284 struct voltagedomain *voltdm;
2f34ce81 285
81a60482 286 if (list_empty(&voltdm_list)) {
2f34ce81
TG
287 pr_err("%s: Voltage driver support not added\n",
288 __func__);
289 return -EINVAL;
290 }
291
81a60482 292 list_for_each_entry(voltdm, &voltdm_list, node) {
0e2f3d9c
KH
293 struct clk *sys_ck;
294
37efca7e
KH
295 if (!voltdm->scalable)
296 continue;
297
0e2f3d9c
KH
298 sys_ck = clk_get(NULL, voltdm->sys_clk.name);
299 if (IS_ERR(sys_ck)) {
300 pr_warning("%s: Could not get sys clk.\n", __func__);
301 return -EINVAL;
302 }
303 voltdm->sys_clk.rate = clk_get_rate(sys_ck);
304 WARN_ON(!voltdm->sys_clk.rate);
305 clk_put(sys_ck);
306
4d47506a 307 if (voltdm->vc) {
0f01565a 308 voltdm->scale = omap_vc_bypass_scale;
d84adcf4 309 omap_vc_init_channel(voltdm);
4d47506a 310 }
d84adcf4 311
81a60482
KH
312 if (voltdm->vdd) {
313 if (omap_vdd_data_configure(voltdm))
314 continue;
01f48d30 315 omap_vp_init(voltdm);
81a60482 316 }
2f34ce81
TG
317 }
318
319 return 0;
320}
321
81a60482 322static struct voltagedomain *_voltdm_lookup(const char *name)
2f34ce81 323{
81a60482
KH
324 struct voltagedomain *voltdm, *temp_voltdm;
325
326 voltdm = NULL;
327
328 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
329 if (!strcmp(name, temp_voltdm->name)) {
330 voltdm = temp_voltdm;
331 break;
332 }
333 }
334
335 return voltdm;
336}
337
e69c22b1
KH
338/**
339 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
340 * @voltdm: struct voltagedomain * to add the powerdomain to
341 * @pwrdm: struct powerdomain * to associate with a voltagedomain
342 *
343 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
344 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
345 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
346 * or 0 upon success.
347 */
348int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
349{
350 if (!voltdm || !pwrdm)
351 return -EINVAL;
352
353 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
354 "%s\n", pwrdm->name, voltdm->name);
355
356 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
357
358 return 0;
359}
360
361/**
362 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
363 * @voltdm: struct voltagedomain * to iterate over
364 * @fn: callback function *
365 *
366 * Call the supplied function @fn for each powerdomain in the
367 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
368 * pointers; or passes along the last return value of the callback
369 * function, which should be 0 for success or anything else to
370 * indicate failure.
371 */
372int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
373 int (*fn)(struct voltagedomain *voltdm,
374 struct powerdomain *pwrdm))
375{
376 struct powerdomain *pwrdm;
377 int ret = 0;
378
379 if (!fn)
380 return -EINVAL;
381
382 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
383 ret = (*fn)(voltdm, pwrdm);
384
385 return ret;
386}
387
388/**
389 * voltdm_for_each - call function on each registered voltagedomain
390 * @fn: callback function *
391 *
392 * Call the supplied function @fn for each registered voltagedomain.
393 * The callback function @fn can return anything but 0 to bail out
394 * early from the iterator. Returns the last return value of the
395 * callback function, which should be 0 for success or anything else
396 * to indicate failure; or -EINVAL if the function pointer is null.
397 */
398int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
399 void *user)
400{
401 struct voltagedomain *temp_voltdm;
402 int ret = 0;
403
404 if (!fn)
405 return -EINVAL;
406
407 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
408 ret = (*fn)(temp_voltdm, user);
409 if (ret)
410 break;
411 }
412
413 return ret;
414}
415
81a60482
KH
416static int _voltdm_register(struct voltagedomain *voltdm)
417{
418 if (!voltdm || !voltdm->name)
419 return -EINVAL;
420
e69c22b1 421 INIT_LIST_HEAD(&voltdm->pwrdm_list);
81a60482
KH
422 list_add(&voltdm->node, &voltdm_list);
423
424 pr_debug("voltagedomain: registered %s\n", voltdm->name);
425
2f34ce81
TG
426 return 0;
427}
81a60482
KH
428
429/**
430 * voltdm_lookup - look up a voltagedomain by name, return a pointer
431 * @name: name of voltagedomain
432 *
433 * Find a registered voltagedomain by its name @name. Returns a pointer
434 * to the struct voltagedomain if found, or NULL otherwise.
435 */
436struct voltagedomain *voltdm_lookup(const char *name)
437{
438 struct voltagedomain *voltdm ;
439
440 if (!name)
441 return NULL;
442
443 voltdm = _voltdm_lookup(name);
444
445 return voltdm;
446}
447
448/**
449 * voltdm_init - set up the voltagedomain layer
450 * @voltdm_list: array of struct voltagedomain pointers to register
451 *
452 * Loop through the array of voltagedomains @voltdm_list, registering all
453 * that are available on the current CPU. If voltdm_list is supplied
454 * and not null, all of the referenced voltagedomains will be
455 * registered. No return value.
456 */
457void voltdm_init(struct voltagedomain **voltdms)
458{
459 struct voltagedomain **v;
460
461 if (voltdms) {
462 for (v = voltdms; *v; v++)
463 _voltdm_register(*v);
464 }
465}
This page took 0.135833 seconds and 5 git commands to generate.