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