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