OMAP3+: VP: move voltage scale function pointer 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         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  * 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  */
105 int omap_voltage_scale_vdd(struct voltagedomain *voltdm,
106                 unsigned long target_volt)
107 {
108         if (!voltdm || IS_ERR(voltdm)) {
109                 pr_warning("%s: VDD specified does not exist!\n", __func__);
110                 return -EINVAL;
111         }
112
113         if (!voltdm->scale) {
114                 pr_err("%s: No voltage scale API registered for vdd_%s\n",
115                         __func__, voltdm->name);
116                 return -ENODATA;
117         }
118
119         return voltdm->scale(voltdm, target_volt);
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
128  * to be at and resets the voltage to that level. Should be used especially
129  * while disabling any voltage compensation modules.
130  */
131 void 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  */
162 void 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
172         vdd = voltdm->vdd;
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
189  * success. Returns -ENODATA if no voltage table exisits for the passed voltage
190  * domain or if there is no matching entry.
191  */
192 struct 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
203         vdd = voltdm->vdd;
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
226  * @pmic:       the structure containing pmic info
227  *
228  * This API is to be called by the SOC/PMIC file to specify the
229  * pmic specific info as present in omap_voltdm_pmic structure.
230  */
231 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
232                                struct omap_voltdm_pmic *pmic)
233 {
234         if (!voltdm || IS_ERR(voltdm)) {
235                 pr_warning("%s: VDD specified does not exist!\n", __func__);
236                 return -EINVAL;
237         }
238
239         voltdm->pmic = pmic;
240
241         return 0;
242 }
243
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  */
254 void omap_change_voltscale_method(struct voltagedomain *voltdm,
255                                   int voltscale_method)
256 {
257         if (!voltdm || IS_ERR(voltdm)) {
258                 pr_warning("%s: VDD specified does not exist!\n", __func__);
259                 return;
260         }
261
262         switch (voltscale_method) {
263         case VOLTSCALE_VPFORCEUPDATE:
264                 voltdm->scale = omap_vp_forceupdate_scale;
265                 return;
266         case VOLTSCALE_VCBYPASS:
267                 voltdm->scale = omap_vc_bypass_scale;
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
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  */
282 int __init omap_voltage_late_init(void)
283 {
284         struct voltagedomain *voltdm;
285
286         if (list_empty(&voltdm_list)) {
287                 pr_err("%s: Voltage driver support not added\n",
288                         __func__);
289                 return -EINVAL;
290         }
291
292         list_for_each_entry(voltdm, &voltdm_list, node) {
293                 struct clk *sys_ck;
294
295                 if (!voltdm->scalable)
296                         continue;
297
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
307                 if (voltdm->vc) {
308                         voltdm->scale = omap_vc_bypass_scale;
309                         omap_vc_init_channel(voltdm);
310                 }
311
312                 if (voltdm->vdd) {
313                         if (omap_vdd_data_configure(voltdm))
314                                 continue;
315                         omap_vp_init(voltdm);
316                 }
317         }
318
319         return 0;
320 }
321
322 static struct voltagedomain *_voltdm_lookup(const char *name)
323 {
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
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  */
348 int 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  */
372 int 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  */
398 int 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
416 static int _voltdm_register(struct voltagedomain *voltdm)
417 {
418         if (!voltdm || !voltdm->name)
419                 return -EINVAL;
420
421         INIT_LIST_HEAD(&voltdm->pwrdm_list);
422         list_add(&voltdm->node, &voltdm_list);
423
424         pr_debug("voltagedomain: registered %s\n", voltdm->name);
425
426         return 0;
427 }
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  */
436 struct 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  */
457 void 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 }