ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / arch / arm / mach-omap2 / voltage.c
blob995c38fe7caba95e885627b775b23ff1240d5837
1 /*
2 * OMAP3/OMAP4 Voltage Management Routines
4 * Author: Thara Gopinath <thara@ti.com>
6 * Copyright (C) 2007 Texas Instruments, Inc.
7 * Rajendra Nayak <rnayak@ti.com>
8 * Lesly A M <x0080970@ti.com>
10 * Copyright (C) 2008, 2011 Nokia Corporation
11 * Kalle Jokiniemi
12 * Paul Walmsley
14 * Copyright (C) 2010 Texas Instruments, Inc.
15 * Thara Gopinath <thara@ti.com>
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.
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>
29 #include <plat/common.h>
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"
38 #include "voltage.h"
39 #include "powerdomain.h"
41 #include "vc.h"
42 #include "vp.h"
44 static LIST_HEAD(voltdm_list);
46 static int __init _config_common_vdd_data(struct voltagedomain *voltdm)
48 struct clk *sys_ck;
51 * Sys clk rate is require to calculate vp timeout value and
52 * smpswaittimemin and smpswaittimemax.
54 sys_ck = clk_get(NULL, voltdm->sys_clk.name);
55 if (IS_ERR(sys_ck)) {
56 pr_warning("%s: Could not get the sys clk to calculate"
57 "various vdd_%s params\n", __func__, voltdm->name);
58 return -EINVAL;
60 voltdm->sys_clk.rate = clk_get_rate(sys_ck);
61 WARN_ON(!voltdm->sys_clk.rate);
63 /* Generic voltage parameters */
64 voltdm->scale = omap_vp_forceupdate_scale;
66 return 0;
69 static int __init omap_vdd_data_configure(struct voltagedomain *voltdm)
71 int ret = -EINVAL;
73 if (!voltdm->pmic) {
74 pr_err("%s: PMIC info requried to configure vdd_%s not"
75 "populated.Hence cannot initialize vdd_%s\n",
76 __func__, voltdm->name, voltdm->name);
77 goto ovdc_out;
80 if (IS_ERR_VALUE(_config_common_vdd_data(voltdm)))
81 goto ovdc_out;
83 ret = 0;
85 ovdc_out:
86 return ret;
89 /* Public functions */
90 /**
91 * omap_voltage_get_curr_vdata() - Gets the current voltage data
92 * @voltdm: pointer to the VDD for which current voltage info is needed
94 * API to get the current voltage data pointer for a VDD, returns NULL on error
96 struct omap_volt_data *omap_voltage_get_curr_vdata(struct voltagedomain *voltdm)
98 if (!voltdm || IS_ERR(voltdm)) {
99 pr_warning("%s: VDD specified does not exist!\n", __func__);
100 return NULL;
103 return voltdm->curr_volt;
107 * voltdm_scale() - API to scale voltage of a particular voltage domain.
108 * @voltdm: pointer to the voltage domain which is to be scaled.
109 * @target_volt: The target voltage of the voltage domain
111 * This API should be called by the kernel to do the voltage scaling
112 * for a particular voltage domain during DVFS.
114 int voltdm_scale(struct voltagedomain *voltdm,
115 struct omap_volt_data *target_v)
117 int ret = 0;
118 struct omap_voltage_notifier notify;
119 unsigned long target_volt = omap_get_operation_voltage(target_v);
121 if (!voltdm || IS_ERR(voltdm)) {
122 pr_warning("%s: VDD specified does not exist!\n", __func__);
123 return -EINVAL;
126 if (!voltdm->scale) {
127 pr_err("%s: No voltage scale API registered for vdd_%s\n",
128 __func__, voltdm->name);
129 return -ENODATA;
132 notify.voltdm = voltdm;
133 notify.target_volt = target_volt;
135 srcu_notifier_call_chain(&voltdm->change_notify_list,
136 OMAP_VOLTAGE_PRECHANGE,
137 (void *)&notify);
139 ret = voltdm->scale(voltdm, target_v);
140 if (ret)
141 pr_err("%s: voltage scale failed for vdd%s: %d\n",
142 __func__, voltdm->name, ret);
144 notify.op_result = ret;
145 srcu_notifier_call_chain(&voltdm->change_notify_list,
146 OMAP_VOLTAGE_POSTCHANGE,
147 (void *)&notify);
149 return ret;
153 * voltdm_reset() - Resets the voltage of a particular voltage domain
154 * to that of the current OPP.
155 * @voltdm: pointer to the voltage domain whose voltage is to be reset.
157 * This API finds out the correct voltage the voltage domain is supposed
158 * to be at and resets the voltage to that level. Should be used especially
159 * while disabling any voltage compensation modules.
161 * NOTE: appropriate locks should be held for mutual exclusivity.
163 void voltdm_reset(struct voltagedomain *voltdm)
165 struct omap_volt_data *target_volt;
167 if (!voltdm || IS_ERR(voltdm)) {
168 pr_warning("%s: VDD specified does not exist!\n", __func__);
169 return;
172 target_volt = omap_voltage_get_curr_vdata(voltdm);
173 if (!target_volt) {
174 pr_err("%s: unable to find current voltage for vdd_%s\n",
175 __func__, voltdm->name);
176 return;
179 voltdm_scale(voltdm, target_volt);
183 * omap_voltage_get_volttable() - API to get the voltage table associated with a
184 * particular voltage domain.
185 * @voltdm: pointer to the VDD for which the voltage table is required
186 * @volt_data: the voltage table for the particular vdd which is to be
187 * populated by this API
189 * This API populates the voltage table associated with a VDD into the
190 * passed parameter pointer. Returns the count of distinct voltages
191 * supported by this vdd.
194 void omap_voltage_get_volttable(struct voltagedomain *voltdm,
195 struct omap_volt_data **volt_data)
197 struct omap_vdd_info *vdd;
199 if (!voltdm || IS_ERR(voltdm)) {
200 pr_warning("%s: VDD specified does not exist!\n", __func__);
201 return;
204 vdd = voltdm->vdd;
206 *volt_data = vdd->volt_data;
210 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
211 * particular voltage
212 * @voltdm: pointer to the VDD whose voltage table has to be searched
213 * @volt: the voltage to be searched in the voltage table
215 * This API searches through the voltage table for the required voltage
216 * domain and tries to find a matching entry for the passed voltage volt.
217 * If a matching entry is found volt_data is populated with that entry.
218 * This API searches only through the non-compensated voltages int the
219 * voltage table.
220 * Returns pointer to the voltage table entry corresponding to volt on
221 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
222 * domain or if there is no matching entry.
224 struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
225 unsigned long volt)
227 struct omap_vdd_info *vdd;
228 int i;
230 if (!voltdm || IS_ERR(voltdm)) {
231 pr_warning("%s: VDD specified does not exist!\n", __func__);
232 return ERR_PTR(-EINVAL);
235 vdd = voltdm->vdd;
237 if (!vdd->volt_data) {
238 pr_warning("%s: voltage table does not exist for vdd_%s\n",
239 __func__, voltdm->name);
240 return ERR_PTR(-ENODATA);
243 for (i = 0; vdd->volt_data[i].volt_nominal != 0; i++) {
244 if (vdd->volt_data[i].volt_nominal == volt)
245 return &vdd->volt_data[i];
248 pr_notice("%s: Unable to match the current voltage %lu with the voltage"
249 "table for vdd_%s\n", __func__, volt, voltdm->name);
251 return ERR_PTR(-ENODATA);
255 * omap_voltage_register_pmic() - API to register PMIC specific data
256 * @voltdm: pointer to the VDD for which the PMIC specific data is
257 * to be registered
258 * @pmic: the structure containing pmic info
260 * This API is to be called by the SOC/PMIC file to specify the
261 * pmic specific info as present in omap_voltdm_pmic structure.
263 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
264 struct omap_voltdm_pmic *pmic)
266 if (!voltdm || IS_ERR(voltdm)) {
267 pr_warning("%s: VDD specified does not exist!\n", __func__);
268 return -EINVAL;
271 voltdm->pmic = pmic;
273 return 0;
277 * omap_change_voltscale_method() - API to change the voltage scaling method.
278 * @voltdm: pointer to the VDD whose voltage scaling method
279 * has to be changed.
280 * @voltscale_method: the method to be used for voltage scaling.
282 * This API can be used by the board files to change the method of voltage
283 * scaling between vpforceupdate and vcbypass. The parameter values are
284 * defined in voltage.h
286 void omap_change_voltscale_method(struct voltagedomain *voltdm,
287 int voltscale_method)
289 if (!voltdm || IS_ERR(voltdm)) {
290 pr_warning("%s: VDD specified does not exist!\n", __func__);
291 return;
294 switch (voltscale_method) {
295 case VOLTSCALE_VPFORCEUPDATE:
296 voltdm->scale = omap_vp_forceupdate_scale;
297 return;
298 case VOLTSCALE_VCBYPASS:
299 voltdm->scale = omap_vc_bypass_scale_voltage;
300 return;
301 default:
302 pr_warning("%s: Trying to change the method of voltage scaling"
303 "to an unsupported one!\n", __func__);
307 /* Voltage debugfs support */
308 static int vp_volt_debug_get(void *data, u64 *val)
310 struct voltagedomain *voltdm = (struct voltagedomain *)data;
312 if (!voltdm) {
313 pr_warning("Wrong paramater passed\n");
314 return -EINVAL;
316 *val = omap_vp_get_curr_volt(voltdm);
318 return 0;
320 DEFINE_SIMPLE_ATTRIBUTE(vp_volt_debug_fops, vp_volt_debug_get, NULL, "%llu\n");
322 static int dyn_volt_debug_get(void *data, u64 *val)
324 struct voltagedomain *voltdm = (struct voltagedomain *)data;
325 struct omap_volt_data *volt_data;
327 if (!voltdm) {
328 pr_warning("%s: Wrong paramater passed\n", __func__);
329 return -EINVAL;
332 volt_data = omap_voltage_get_curr_vdata(voltdm);
333 if (IS_ERR_OR_NULL(volt_data)) {
334 pr_warning("%s: No voltage/domain?\n", __func__);
335 return -ENODEV;
338 *val = volt_data->volt_dynamic_nominal;
340 return 0;
342 DEFINE_SIMPLE_ATTRIBUTE(dyn_volt_debug_fops, dyn_volt_debug_get, NULL,
343 "%llu\n");
345 static int calib_volt_debug_get(void *data, u64 *val)
347 struct voltagedomain *voltdm = (struct voltagedomain *)data;
348 struct omap_volt_data *volt_data;
350 if (!voltdm) {
351 pr_warning("%s: Wrong paramater passed\n", __func__);
352 return -EINVAL;
355 volt_data = omap_voltage_get_curr_vdata(voltdm);
356 if (IS_ERR_OR_NULL(volt_data)) {
357 pr_warning("%s: No voltage/domain?\n", __func__);
358 return -ENODEV;
361 *val = volt_data->volt_calibrated;
363 return 0;
365 DEFINE_SIMPLE_ATTRIBUTE(calib_volt_debug_fops, calib_volt_debug_get, NULL,
366 "%llu\n");
367 static int margin_volt_debug_get(void *data, u64 *val)
369 struct voltagedomain *voltdm = (struct voltagedomain *) data;
370 struct omap_volt_data *vdata;
372 if (!voltdm) {
373 pr_warning("%s: Wrong parameter passed\n", __func__);
374 return -EINVAL;
377 vdata = omap_voltage_get_curr_vdata(voltdm);
378 if (IS_ERR_OR_NULL(vdata)) {
379 pr_warning("%s: unable to get volt for vdd_%s\n",
380 __func__, voltdm->name);
381 return -ENODEV;
383 *val = vdata->volt_margin;
385 return 0;
387 DEFINE_SIMPLE_ATTRIBUTE(margin_volt_debug_fops, margin_volt_debug_get, NULL,
388 "%llu\n");
390 static int nom_volt_debug_get(void *data, u64 *val)
392 struct voltagedomain *voltdm = (struct voltagedomain *) data;
393 struct omap_volt_data *vdata;
395 if (!voltdm) {
396 pr_warning("Wrong paramater passed\n");
397 return -EINVAL;
400 vdata = omap_voltage_get_curr_vdata(voltdm);
401 if (IS_ERR_OR_NULL(vdata)) {
402 pr_warning("%s: unable to get volt for vdd_%s\n",
403 __func__, voltdm->name);
404 return -ENODEV;
406 *val = vdata->volt_nominal;
408 return 0;
410 DEFINE_SIMPLE_ATTRIBUTE(nom_volt_debug_fops, nom_volt_debug_get, NULL,
411 "%llu\n");
413 static void __init voltdm_debugfs_init(struct dentry *voltage_dir,
414 struct voltagedomain *voltdm)
416 char *name;
418 name = kasprintf(GFP_KERNEL, "vdd_%s", voltdm->name);
419 if (!name) {
420 pr_warning("%s:vdd_%s: no mem for debugfs\n", __func__,
421 voltdm->name);
422 return;
425 voltdm->debug_dir = debugfs_create_dir(name, voltage_dir);
426 kfree(name);
427 if (IS_ERR_OR_NULL(voltdm->debug_dir)) {
428 pr_warning("%s: Unable to create debugfs directory for"
429 " vdd_%s\n", __func__, voltdm->name);
430 voltdm->debug_dir = NULL;
431 return;
434 (void) debugfs_create_file("curr_vp_volt", S_IRUGO, voltdm->debug_dir,
435 (void *) voltdm, &vp_volt_debug_fops);
436 (void) debugfs_create_file("curr_nominal_volt", S_IRUGO,
437 voltdm->debug_dir, (void *) voltdm,
438 &nom_volt_debug_fops);
439 (void) debugfs_create_file("curr_dyn_nominal_volt", S_IRUGO,
440 voltdm->debug_dir, (void *) voltdm,
441 &dyn_volt_debug_fops);
442 (void) debugfs_create_file("curr_calibrated_volt", S_IRUGO,
443 voltdm->debug_dir, (void *) voltdm,
444 &calib_volt_debug_fops);
445 (void) debugfs_create_file("curr_margin_volt", S_IRUGO,
446 voltdm->debug_dir, (void *) voltdm,
447 &margin_volt_debug_fops);
451 * omap_voltage_late_init() - Init the various voltage parameters
453 * This API is to be called in the later stages of the
454 * system boot to init the voltage controller and
455 * voltage processors.
457 int __init omap_voltage_late_init(void)
459 struct voltagedomain *voltdm;
460 struct dentry *voltage_dir;
462 if (list_empty(&voltdm_list)) {
463 pr_err("%s: Voltage driver support not added\n",
464 __func__);
465 return -EINVAL;
468 voltage_dir = debugfs_create_dir("voltage", NULL);
470 list_for_each_entry(voltdm, &voltdm_list, node) {
471 if (!voltdm->scalable)
472 continue;
474 if (voltdm->vdd) {
475 if (omap_vdd_data_configure(voltdm))
476 continue;
477 omap_vp_init(voltdm);
480 if (voltdm->vc)
481 omap_vc_init_channel(voltdm);
483 if (voltdm->abb)
484 omap_ldo_abb_init(voltdm);
486 if (voltage_dir)
487 voltdm_debugfs_init(voltage_dir, voltdm);
489 srcu_init_notifier_head(&voltdm->change_notify_list);
492 return 0;
495 static struct voltagedomain *_voltdm_lookup(const char *name)
497 struct voltagedomain *voltdm, *temp_voltdm;
499 voltdm = NULL;
501 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
502 if (!strcmp(name, temp_voltdm->name)) {
503 voltdm = temp_voltdm;
504 break;
508 return voltdm;
512 * omap_voltage_calib_reset() - reset the calibrated voltage entries
513 * @voltdm: voltage domain to reset the entries for
515 * when the calibrated entries are no longer valid, this api allows
516 * the calibrated voltages to be reset.
518 * NOTE: Appropriate locks must be held by calling path to ensure mutual
519 * exclusivity
521 int omap_voltage_calib_reset(struct voltagedomain *voltdm)
523 struct omap_volt_data *volt_data;
525 if (!voltdm) {
526 pr_warning("%s: voltdm NULL!\n", __func__);
527 return -EINVAL;
530 volt_data = voltdm->vdd->volt_data;
532 /* reset the calibrated voltages as 0 */
533 while (volt_data->volt_nominal) {
534 volt_data->volt_calibrated = 0;
535 volt_data++;
537 return 0;
541 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
542 * @voltdm: struct voltagedomain * to add the powerdomain to
543 * @pwrdm: struct powerdomain * to associate with a voltagedomain
545 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
546 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
547 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
548 * or 0 upon success.
550 int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
552 if (!voltdm || !pwrdm)
553 return -EINVAL;
555 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
556 "%s\n", pwrdm->name, voltdm->name);
558 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
560 return 0;
564 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
565 * @voltdm: struct voltagedomain * to iterate over
566 * @fn: callback function *
568 * Call the supplied function @fn for each powerdomain in the
569 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
570 * pointers; or passes along the last return value of the callback
571 * function, which should be 0 for success or anything else to
572 * indicate failure.
574 int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
575 int (*fn)(struct voltagedomain *voltdm,
576 struct powerdomain *pwrdm))
578 struct powerdomain *pwrdm;
579 int ret = 0;
581 if (!fn)
582 return -EINVAL;
584 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
585 ret = (*fn)(voltdm, pwrdm);
587 return ret;
591 * voltdm_for_each - call function on each registered voltagedomain
592 * @fn: callback function *
594 * Call the supplied function @fn for each registered voltagedomain.
595 * The callback function @fn can return anything but 0 to bail out
596 * early from the iterator. Returns the last return value of the
597 * callback function, which should be 0 for success or anything else
598 * to indicate failure; or -EINVAL if the function pointer is null.
600 int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
601 void *user)
603 struct voltagedomain *temp_voltdm;
604 int ret = 0;
606 if (!fn)
607 return -EINVAL;
609 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
610 ret = (*fn)(temp_voltdm, user);
611 if (ret)
612 break;
615 return ret;
618 static int _voltdm_register(struct voltagedomain *voltdm)
620 if (!voltdm || !voltdm->name)
621 return -EINVAL;
623 INIT_LIST_HEAD(&voltdm->pwrdm_list);
624 list_add(&voltdm->node, &voltdm_list);
626 pr_debug("voltagedomain: registered %s\n", voltdm->name);
628 return 0;
632 * voltdm_lookup - look up a voltagedomain by name, return a pointer
633 * @name: name of voltagedomain
635 * Find a registered voltagedomain by its name @name. Returns a pointer
636 * to the struct voltagedomain if found, or NULL otherwise.
638 struct voltagedomain *voltdm_lookup(const char *name)
640 struct voltagedomain *voltdm ;
642 if (!name)
643 return NULL;
645 voltdm = _voltdm_lookup(name);
647 return voltdm;
651 * voltdm_init - set up the voltagedomain layer
652 * @voltdm_list: array of struct voltagedomain pointers to register
654 * Loop through the array of voltagedomains @voltdm_list, registering all
655 * that are available on the current CPU. If voltdm_list is supplied
656 * and not null, all of the referenced voltagedomains will be
657 * registered. No return value.
659 void voltdm_init(struct voltagedomain **voltdms)
661 struct voltagedomain **v;
663 if (voltdms) {
664 for (v = voltdms; *v; v++)
665 _voltdm_register(*v);