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