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