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