3 * Copyright (C) 2010 Google, Inc.
6 * Colin Cross <ccross@google.com>
8 * Copyright (C) 2010-2011 NVIDIA Corporation.
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/kernel.h>
22 #include <linux/clk.h>
23 #include <linux/clkdev.h>
24 #include <linux/debugfs.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/list_sort.h>
28 #include <linux/module.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/suspend.h>
33 #include <linux/delay.h>
34 #include <linux/clk/tegra.h>
35 #include <linux/reboot.h>
41 #define DVFS_RAIL_STATS_BIN 12500
43 struct dvfs_rail *tegra_cpu_rail;
44 struct dvfs_rail *tegra_core_rail;
46 static LIST_HEAD(dvfs_rail_list);
47 static DEFINE_MUTEX(dvfs_lock);
48 static DEFINE_MUTEX(rail_disable_lock);
50 static int dvfs_rail_update(struct dvfs_rail *rail);
52 void tegra_dvfs_add_relationships(struct dvfs_relationship *rels, int n)
55 struct dvfs_relationship *rel;
57 mutex_lock(&dvfs_lock);
59 for (i = 0; i < n; i++) {
61 list_add_tail(&rel->from_node, &rel->to->relationships_from);
62 list_add_tail(&rel->to_node, &rel->from->relationships_to);
65 mutex_unlock(&dvfs_lock);
68 int tegra_dvfs_init_rails(struct dvfs_rail *rails[], int n)
72 mutex_lock(&dvfs_lock);
74 for (i = 0; i < n; i++) {
75 INIT_LIST_HEAD(&rails[i]->dvfs);
76 INIT_LIST_HEAD(&rails[i]->relationships_from);
77 INIT_LIST_HEAD(&rails[i]->relationships_to);
78 rails[i]->millivolts = rails[i]->nominal_millivolts;
79 rails[i]->new_millivolts = rails[i]->nominal_millivolts;
81 rails[i]->step = rails[i]->max_millivolts;
83 list_add_tail(&rails[i]->node, &dvfs_rail_list);
85 if (!strcmp("vdd_cpu", rails[i]->reg_id))
86 tegra_cpu_rail = rails[i];
87 else if (!strcmp("vdd_core", rails[i]->reg_id))
88 tegra_core_rail = rails[i];
91 mutex_unlock(&dvfs_lock);
96 static int dvfs_solve_relationship(struct dvfs_relationship *rel)
98 return rel->solve(rel->from, rel->to);
101 /* rail statistic - called during rail init, or under dfs_lock, or with
102 CPU0 only on-line, and interrupts disabled */
103 static void dvfs_rail_stats_init(struct dvfs_rail *rail, int millivolts)
105 int dvfs_rail_stats_range;
107 if (!rail->stats.bin_uV)
108 rail->stats.bin_uV = DVFS_RAIL_STATS_BIN;
110 dvfs_rail_stats_range =
111 (DVFS_RAIL_STATS_TOP_BIN - 1) * rail->stats.bin_uV / 1000;
113 rail->stats.last_update = ktime_get();
114 if (millivolts >= rail->min_millivolts) {
115 int i = 1 + (2 * (millivolts - rail->min_millivolts) * 1000 +
116 rail->stats.bin_uV) / (2 * rail->stats.bin_uV);
117 rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN);
120 if (rail->max_millivolts >
121 rail->min_millivolts + dvfs_rail_stats_range)
122 pr_warn("tegra_dvfs: %s: stats above %d mV will be squashed\n",
124 rail->min_millivolts + dvfs_rail_stats_range);
127 static void dvfs_rail_stats_update(
128 struct dvfs_rail *rail, int millivolts, ktime_t now)
130 rail->stats.time_at_mv[rail->stats.last_index] = ktime_add(
131 rail->stats.time_at_mv[rail->stats.last_index], ktime_sub(
132 now, rail->stats.last_update));
133 rail->stats.last_update = now;
138 if (millivolts >= rail->min_millivolts) {
139 int i = 1 + (2 * (millivolts - rail->min_millivolts) * 1000 +
140 rail->stats.bin_uV) / (2 * rail->stats.bin_uV);
141 rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN);
142 } else if (millivolts == 0)
143 rail->stats.last_index = 0;
146 static void dvfs_rail_stats_pause(struct dvfs_rail *rail,
147 ktime_t delta, bool on)
149 int i = on ? rail->stats.last_index : 0;
150 rail->stats.time_at_mv[i] = ktime_add(rail->stats.time_at_mv[i], delta);
153 void tegra_dvfs_rail_off(struct dvfs_rail *rail, ktime_t now)
156 dvfs_rail_stats_update(rail, 0, now);
157 rail->stats.off = true;
161 void tegra_dvfs_rail_on(struct dvfs_rail *rail, ktime_t now)
164 rail->stats.off = false;
165 dvfs_rail_stats_update(rail, rail->millivolts, now);
169 void tegra_dvfs_rail_pause(struct dvfs_rail *rail, ktime_t delta, bool on)
172 dvfs_rail_stats_pause(rail, delta, on);
175 /* Sets the voltage on a dvfs rail to a specific value, and updates any
176 * rails that depend on this rail. */
177 static int dvfs_rail_set_voltage(struct dvfs_rail *rail, int millivolts)
180 struct dvfs_relationship *rel;
181 int step = (millivolts > rail->millivolts) ? rail->step : -rail->step;
187 if (millivolts == rail->millivolts)
194 * DFLL adjusts rail voltage automatically, but not exactly to the
195 * expected level - update stats, anyway, and made sure that recorded
196 * level will not match any target that can be requested when/if we
197 * switch back from DFLL to s/w control
199 if (rail->dfll_mode) {
200 rail->millivolts = rail->new_millivolts = millivolts - 1;
201 dvfs_rail_stats_update(rail, millivolts, ktime_get());
208 rail->resolving_to = true;
209 jmp_to_zero = rail->jmp_to_zero &&
210 ((millivolts == 0) || (rail->millivolts == 0));
211 steps = jmp_to_zero ? 1 :
212 DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
214 for (i = 0; i < steps; i++) {
216 (abs(millivolts - rail->millivolts) > rail->step))
217 rail->new_millivolts = rail->millivolts + step;
219 rail->new_millivolts = millivolts;
221 /* Before changing the voltage, tell each rail that depends
222 * on this rail that the voltage will change.
223 * This rail will be the "from" rail in the relationship,
224 * the rail that depends on this rail will be the "to" rail.
225 * from->millivolts will be the old voltage
226 * from->new_millivolts will be the new voltage */
227 list_for_each_entry(rel, &rail->relationships_to, to_node) {
228 ret = dvfs_rail_update(rel->to);
233 if (!rail->disabled) {
234 rail->updating = true;
235 rail->reg_max_millivolts = rail->reg_max_millivolts ==
236 rail->max_millivolts ?
237 rail->max_millivolts + 1 : rail->max_millivolts;
238 ret = regulator_set_voltage(rail->reg,
239 rail->new_millivolts * 1000,
240 rail->reg_max_millivolts * 1000);
241 rail->updating = false;
244 pr_err("Failed to set dvfs regulator %s\n", rail->reg_id);
248 rail->millivolts = rail->new_millivolts;
249 dvfs_rail_stats_update(rail, rail->millivolts, ktime_get());
251 /* After changing the voltage, tell each rail that depends
252 * on this rail that the voltage has changed.
253 * from->millivolts and from->new_millivolts will be the
255 list_for_each_entry(rel, &rail->relationships_to, to_node) {
256 ret = dvfs_rail_update(rel->to);
262 if (unlikely(rail->millivolts != millivolts)) {
263 pr_err("%s: rail didn't reach target %d in %d steps (%d)\n",
264 __func__, millivolts, steps, rail->millivolts);
269 rail->resolving_to = false;
273 /* Determine the minimum valid voltage for a rail, taking into account
274 * the dvfs clocks and any rails that this rail depends on. Calls
275 * dvfs_rail_set_voltage with the new voltage, which will call
276 * dvfs_rail_update on any rails that depend on this rail. */
277 static int dvfs_rail_update(struct dvfs_rail *rail)
281 struct dvfs_relationship *rel;
285 /* if dvfs is suspended, return and handle it during resume */
289 /* if regulators are not connected yet, return and handle it later */
293 /* if rail update is entered while resolving circular dependencies,
295 if (rail->resolving_to)
298 /* Find the maximum voltage requested by any clock */
299 list_for_each_entry(d, &rail->dvfs, reg_node)
300 millivolts = max(d->cur_millivolts, millivolts);
302 /* Apply offset if any clock is requesting voltage */
304 int min_mv = rail->min_millivolts;
305 if (rail->pll_mode_cdev)
306 min_mv = max(min_mv, rail->thermal_idx ?
307 0 : rail->min_millivolts_cold);
309 millivolts += rail->offs_millivolts;
310 if (millivolts > rail->max_millivolts)
311 millivolts = rail->max_millivolts;
312 else if (millivolts < min_mv)
316 /* retry update if limited by from-relationship to account for
317 circular dependencies */
318 steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
319 for (; steps >= 0; steps--) {
320 rail->new_millivolts = millivolts;
322 /* Check any rails that this rail depends on */
323 list_for_each_entry(rel, &rail->relationships_from, from_node)
324 rail->new_millivolts = dvfs_solve_relationship(rel);
326 if (rail->new_millivolts == rail->millivolts)
329 ret = dvfs_rail_set_voltage(rail, rail->new_millivolts);
335 static int dvfs_rail_connect_to_regulator(struct dvfs_rail *rail)
337 struct regulator *reg;
341 reg = regulator_get(NULL, rail->reg_id);
343 pr_err("tegra_dvfs: failed to connect %s rail\n",
350 v = regulator_enable(rail->reg);
352 pr_err("tegra_dvfs: failed on enabling regulator %s\n, err %d",
357 v = regulator_get_voltage(rail->reg);
359 pr_err("tegra_dvfs: failed initial get %s voltage\n",
363 rail->millivolts = v / 1000;
364 rail->new_millivolts = rail->millivolts;
365 dvfs_rail_stats_init(rail, rail->millivolts);
369 static inline unsigned long *dvfs_get_freqs(struct dvfs *d)
371 return d->alt_freqs ? : &d->freqs[0];
374 static inline const int *dvfs_get_millivolts(struct dvfs *d, unsigned long rate)
376 if (tegra_dvfs_is_dfll_scale(d, rate))
377 return d->dfll_millivolts;
379 return d->millivolts;
383 __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
387 unsigned long *freqs = dvfs_get_freqs(d);
388 const int *millivolts = dvfs_get_millivolts(d, rate);
390 if (freqs == NULL || millivolts == NULL)
393 if (rate > freqs[d->num_freqs - 1]) {
394 pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate,
400 d->cur_millivolts = 0;
402 while (i < d->num_freqs && rate > freqs[i])
405 if ((d->max_millivolts) &&
406 (millivolts[i] > d->max_millivolts)) {
407 pr_warn("tegra_dvfs: voltage %d too high for dvfs on"
408 " %s\n", millivolts[i], d->clk_name);
411 d->cur_millivolts = millivolts[i];
416 ret = dvfs_rail_update(d->dvfs_rail);
418 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
419 d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
424 int tegra_dvfs_alt_freqs_set(struct dvfs *d, unsigned long *alt_freqs)
428 mutex_lock(&dvfs_lock);
430 if (d->alt_freqs != alt_freqs) {
431 d->alt_freqs = alt_freqs;
432 ret = __tegra_dvfs_set_rate(d, d->cur_rate);
435 mutex_unlock(&dvfs_lock);
439 static int predict_millivolts(struct clk *c, const int *millivolts,
447 * Predicted voltage can not be used across the switch to alternative
448 * frequency limits. For now, just fail the call for clock that has
449 * alternative limits initialized.
451 if (c->dvfs->alt_freqs)
454 for (i = 0; i < c->dvfs->num_freqs; i++) {
455 if (rate <= c->dvfs->freqs[i])
459 if (i == c->dvfs->num_freqs)
462 return millivolts[i];
465 int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate)
467 const int *millivolts;
469 if (!rate || !c->dvfs)
472 millivolts = dvfs_get_millivolts(c->dvfs, rate);
473 return predict_millivolts(c, millivolts, rate);
476 int tegra_dvfs_predict_millivolts_pll(struct clk *c, unsigned long rate)
478 const int *millivolts;
480 if (!rate || !c->dvfs)
483 millivolts = c->dvfs->millivolts;
484 return predict_millivolts(c, millivolts, rate);
487 int tegra_dvfs_predict_millivolts_dfll(struct clk *c, unsigned long rate)
489 const int *millivolts;
491 if (!rate || !c->dvfs)
494 millivolts = c->dvfs->dfll_millivolts;
495 return predict_millivolts(c, millivolts, rate);
498 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
505 mutex_lock(&dvfs_lock);
506 ret = __tegra_dvfs_set_rate(c->dvfs, rate);
507 mutex_unlock(&dvfs_lock);
511 EXPORT_SYMBOL(tegra_dvfs_set_rate);
513 /* May only be called during clock init, does not take any locks on clock c. */
514 int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d)
519 pr_err("Error when enabling dvfs on %s for clock %s:\n",
520 d->dvfs_rail->reg_id, c->name);
521 pr_err("DVFS already enabled for %s\n",
522 c->dvfs->dvfs_rail->reg_id);
526 for (i = 0; i < MAX_DVFS_FREQS; i++) {
527 if (d->millivolts[i] == 0)
530 d->freqs[i] *= d->freqs_mult;
532 /* If final frequencies are 0, pad with previous frequency */
533 if (d->freqs[i] == 0 && i > 1)
534 d->freqs[i] = d->freqs[i - 1];
545 mutex_lock(&dvfs_lock);
546 list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
547 mutex_unlock(&dvfs_lock);
552 static bool tegra_dvfs_all_rails_suspended(void)
554 struct dvfs_rail *rail;
555 bool all_suspended = true;
557 list_for_each_entry(rail, &dvfs_rail_list, node)
558 if (!rail->suspended && !rail->disabled)
559 all_suspended = false;
561 return all_suspended;
564 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
566 struct dvfs_relationship *rel;
567 bool all_suspended = true;
569 list_for_each_entry(rel, &to->relationships_from, from_node)
570 if (!rel->from->suspended && !rel->from->disabled &&
571 !rel->solved_at_nominal)
572 all_suspended = false;
574 return all_suspended;
577 static int tegra_dvfs_suspend_one(void)
579 struct dvfs_rail *rail;
582 list_for_each_entry(rail, &dvfs_rail_list, node) {
583 if (!rail->suspended && !rail->disabled &&
584 tegra_dvfs_from_rails_suspended_or_solved(rail)) {
585 ret = dvfs_rail_set_voltage(rail,
586 rail->nominal_millivolts);
589 rail->suspended = true;
597 static void tegra_dvfs_resume(void)
599 struct dvfs_rail *rail;
601 mutex_lock(&dvfs_lock);
603 list_for_each_entry(rail, &dvfs_rail_list, node)
604 rail->suspended = false;
606 list_for_each_entry(rail, &dvfs_rail_list, node)
607 dvfs_rail_update(rail);
609 mutex_unlock(&dvfs_lock);
612 static int tegra_dvfs_suspend(void)
616 mutex_lock(&dvfs_lock);
618 while (!tegra_dvfs_all_rails_suspended()) {
619 ret = tegra_dvfs_suspend_one();
624 mutex_unlock(&dvfs_lock);
632 static int tegra_dvfs_pm_notify(struct notifier_block *nb,
633 unsigned long event, void *data)
636 case PM_SUSPEND_PREPARE:
637 if (tegra_dvfs_suspend())
640 case PM_POST_SUSPEND:
648 static struct notifier_block tegra_dvfs_nb = {
649 .notifier_call = tegra_dvfs_pm_notify,
652 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
653 unsigned long event, void *data)
659 tegra_dvfs_suspend();
665 static struct notifier_block tegra_dvfs_reboot_nb = {
666 .notifier_call = tegra_dvfs_reboot_notify,
669 /* must be called with dvfs lock held */
670 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
674 /* don't set voltage in DFLL mode - won't work, but break stats */
675 if (rail->dfll_mode) {
676 rail->disabled = true;
680 ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts);
682 pr_info("dvfs: failed to set regulator %s to disable "
683 "voltage %d\n", rail->reg_id,
684 rail->nominal_millivolts);
687 rail->disabled = true;
690 /* must be called with dvfs lock held */
691 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
693 rail->disabled = false;
694 dvfs_rail_update(rail);
697 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
699 mutex_lock(&rail_disable_lock);
701 if (rail->disabled) {
702 mutex_lock(&dvfs_lock);
703 __tegra_dvfs_rail_enable(rail);
704 mutex_unlock(&dvfs_lock);
706 tegra_dvfs_rail_post_enable(rail);
708 mutex_unlock(&rail_disable_lock);
712 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
714 mutex_lock(&rail_disable_lock);
718 /* rail disable will set it to nominal voltage underneath clock
719 framework - need to re-configure clock rates that are not safe
720 at nominal (yes, unsafe at nominal is ugly, but possible). Rate
721 change must be done outside of dvfs lock. */
722 if (tegra_dvfs_rail_disable_prepare(rail)) {
723 pr_info("dvfs: failed to prepare regulator %s to disable\n",
728 mutex_lock(&dvfs_lock);
729 __tegra_dvfs_rail_disable(rail);
730 mutex_unlock(&dvfs_lock);
732 mutex_unlock(&rail_disable_lock);
735 int tegra_dvfs_rail_disable_by_name(const char *reg_id)
737 struct dvfs_rail *rail = tegra_dvfs_get_rail_by_name(reg_id);
741 tegra_dvfs_rail_disable(rail);
745 struct dvfs_rail *tegra_dvfs_get_rail_by_name(const char *reg_id)
747 struct dvfs_rail *rail;
749 mutex_lock(&dvfs_lock);
750 list_for_each_entry(rail, &dvfs_rail_list, node) {
751 if (!strcmp(reg_id, rail->reg_id)) {
752 mutex_unlock(&dvfs_lock);
756 mutex_unlock(&dvfs_lock);
760 bool tegra_dvfs_rail_updating(struct clk *clk)
762 return (!clk ? false :
763 (!clk->dvfs ? false :
764 (!clk->dvfs->dvfs_rail ? false :
765 (clk->dvfs->dvfs_rail->updating))));
769 int __init of_tegra_dvfs_init(const struct of_device_id *matches)
772 struct device_node *np;
774 for_each_matching_node(np, matches) {
775 const struct of_device_id *match = of_match_node(matches, np);
776 of_tegra_dvfs_init_cb_t dvfs_init_cb = match->data;
777 ret = dvfs_init_cb(np);
779 pr_err("dt: Failed to read %s tables from DT\n",
787 int tegra_dvfs_dfll_mode_set(struct dvfs *d, unsigned long rate)
789 mutex_lock(&dvfs_lock);
790 if (!d->dvfs_rail->dfll_mode) {
791 d->dvfs_rail->dfll_mode = true;
792 __tegra_dvfs_set_rate(d, rate);
794 mutex_unlock(&dvfs_lock);
798 int tegra_dvfs_dfll_mode_clear(struct dvfs *d, unsigned long rate)
802 mutex_lock(&dvfs_lock);
803 if (d->dvfs_rail->dfll_mode) {
804 d->dvfs_rail->dfll_mode = false;
805 if (d->dvfs_rail->disabled) {
806 d->dvfs_rail->disabled = false;
807 __tegra_dvfs_rail_disable(d->dvfs_rail);
809 ret = __tegra_dvfs_set_rate(d, rate);
811 mutex_unlock(&dvfs_lock);
815 struct tegra_cooling_device *tegra_dvfs_get_cpu_dfll_cdev(void)
818 return tegra_cpu_rail->dfll_mode_cdev;
822 struct tegra_cooling_device *tegra_dvfs_get_cpu_pll_cdev(void)
825 return tegra_cpu_rail->pll_mode_cdev;
829 struct tegra_cooling_device *tegra_dvfs_get_core_cdev(void)
832 return tegra_core_rail->pll_mode_cdev;
836 #ifdef CONFIG_THERMAL
837 /* Cooling device limits minimum rail voltage at cold temperature in pll mode */
838 static int tegra_dvfs_rail_get_cdev_max_state(
839 struct thermal_cooling_device *cdev, unsigned long *max_state)
841 struct dvfs_rail *rail = (struct dvfs_rail *)cdev->devdata;
842 *max_state = rail->pll_mode_cdev->trip_temperatures_num;
846 static int tegra_dvfs_rail_get_cdev_cur_state(
847 struct thermal_cooling_device *cdev, unsigned long *cur_state)
849 struct dvfs_rail *rail = (struct dvfs_rail *)cdev->devdata;
850 *cur_state = rail->thermal_idx;
854 static int tegra_dvfs_rail_set_cdev_state(
855 struct thermal_cooling_device *cdev, unsigned long cur_state)
857 struct dvfs_rail *rail = (struct dvfs_rail *)cdev->devdata;
859 mutex_lock(&dvfs_lock);
860 if (rail->thermal_idx != cur_state) {
861 rail->thermal_idx = cur_state;
862 dvfs_rail_update(rail);
864 mutex_unlock(&dvfs_lock);
868 static struct thermal_cooling_device_ops tegra_dvfs_rail_cooling_ops = {
869 .get_max_state = tegra_dvfs_rail_get_cdev_max_state,
870 .get_cur_state = tegra_dvfs_rail_get_cdev_cur_state,
871 .set_cur_state = tegra_dvfs_rail_set_cdev_state,
874 static void tegra_dvfs_rail_register_pll_mode_cdev(struct dvfs_rail *rail)
876 if (!rail->pll_mode_cdev)
879 /* just report error - initialized for cold temperature, anyway */
880 if (IS_ERR_OR_NULL(thermal_cooling_device_register(
881 rail->pll_mode_cdev->cdev_type, (void *)rail,
882 &tegra_dvfs_rail_cooling_ops)))
883 pr_err("tegra cooling device %s failed to register\n",
884 rail->pll_mode_cdev->cdev_type);
887 #define tegra_dvfs_rail_register_pll_mode_cdev(rail)
891 * Iterate through all the dvfs regulators, finding the regulator exported
892 * by the regulator api for each one. Must be called in late init, after
893 * all the regulator api's regulators are initialized.
895 int __init tegra_dvfs_late_init(void)
897 bool connected = true;
898 struct dvfs_rail *rail;
900 mutex_lock(&dvfs_lock);
902 list_for_each_entry(rail, &dvfs_rail_list, node)
903 if (dvfs_rail_connect_to_regulator(rail))
906 list_for_each_entry(rail, &dvfs_rail_list, node)
908 dvfs_rail_update(rail);
910 __tegra_dvfs_rail_disable(rail);
912 mutex_unlock(&dvfs_lock);
914 #ifdef CONFIG_TEGRA_SILICON_PLATFORM
918 register_pm_notifier(&tegra_dvfs_nb);
919 register_reboot_notifier(&tegra_dvfs_reboot_nb);
921 list_for_each_entry(rail, &dvfs_rail_list, node)
922 tegra_dvfs_rail_register_pll_mode_cdev(rail);
927 #ifdef CONFIG_DEBUG_FS
928 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
930 struct dvfs *da = list_entry(a, struct dvfs, reg_node);
931 struct dvfs *db = list_entry(b, struct dvfs, reg_node);
934 ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
938 if (da->cur_millivolts < db->cur_millivolts)
940 if (da->cur_millivolts > db->cur_millivolts)
943 return strcmp(da->clk_name, db->clk_name);
946 static int dvfs_tree_show(struct seq_file *s, void *data)
949 struct dvfs_rail *rail;
950 struct dvfs_relationship *rel;
952 seq_printf(s, " clock rate mV\n");
953 seq_printf(s, "--------------------------------\n");
955 mutex_lock(&dvfs_lock);
957 list_for_each_entry(rail, &dvfs_rail_list, node) {
958 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
959 rail->millivolts + (rail->dfll_mode ? 1 : 0),
960 rail->dfll_mode ? " dfll mode" :
961 rail->disabled ? " disabled" : "");
962 list_for_each_entry(rel, &rail->relationships_from, from_node) {
963 seq_printf(s, " %-10s %-7d mV %-4d mV\n",
965 rel->from->millivolts +
966 (rel->from->dfll_mode ? 1 : 0),
967 dvfs_solve_relationship(rel));
969 seq_printf(s, " offset %-7d mV\n", rail->offs_millivolts);
971 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
973 list_for_each_entry(d, &rail->dvfs, reg_node) {
974 seq_printf(s, " %-10s %-10lu %-4d mV\n", d->clk_name,
975 d->cur_rate, d->cur_millivolts);
979 mutex_unlock(&dvfs_lock);
984 static int dvfs_tree_open(struct inode *inode, struct file *file)
986 return single_open(file, dvfs_tree_show, inode->i_private);
989 static const struct file_operations dvfs_tree_fops = {
990 .open = dvfs_tree_open,
993 .release = single_release,
996 static int rail_stats_show(struct seq_file *s, void *data)
999 struct dvfs_rail *rail;
1001 seq_printf(s, "%-12s %-10s\n", "millivolts", "time");
1003 mutex_lock(&dvfs_lock);
1005 list_for_each_entry(rail, &dvfs_rail_list, node) {
1006 seq_printf(s, "%s (bin: %d.%dmV)\n", rail->reg_id,
1007 rail->stats.bin_uV / 1000,
1008 (rail->stats.bin_uV / 10) % 100);
1010 dvfs_rail_stats_update(rail, -1, ktime_get());
1012 seq_printf(s, "%-12d %-10llu\n", 0,
1013 cputime64_to_clock_t(msecs_to_jiffies(
1014 ktime_to_ms(rail->stats.time_at_mv[0]))));
1016 for (i = 1; i <= DVFS_RAIL_STATS_TOP_BIN; i++) {
1017 ktime_t ktime_zero = ktime_set(0, 0);
1018 if (ktime_equal(rail->stats.time_at_mv[i], ktime_zero))
1020 seq_printf(s, "%-12d %-10llu\n", rail->min_millivolts +
1021 (i - 1) * rail->stats.bin_uV / 1000,
1022 cputime64_to_clock_t(msecs_to_jiffies(
1023 ktime_to_ms(rail->stats.time_at_mv[i])))
1027 mutex_unlock(&dvfs_lock);
1031 static int rail_stats_open(struct inode *inode, struct file *file)
1033 return single_open(file, rail_stats_show, inode->i_private);
1036 static const struct file_operations rail_stats_fops = {
1037 .open = rail_stats_open,
1039 .llseek = seq_lseek,
1040 .release = single_release,
1043 static int cpu_offs_get(void *data, u64 *val)
1045 if (tegra_cpu_rail) {
1046 *val = (u64)tegra_cpu_rail->offs_millivolts;
1052 static int cpu_offs_set(void *data, u64 val)
1054 if (tegra_cpu_rail) {
1055 mutex_lock(&dvfs_lock);
1056 tegra_cpu_rail->offs_millivolts = (int)val;
1057 dvfs_rail_update(tegra_cpu_rail);
1058 mutex_unlock(&dvfs_lock);
1063 DEFINE_SIMPLE_ATTRIBUTE(cpu_offs_fops, cpu_offs_get, cpu_offs_set, "%lld\n");
1065 static int core_offs_get(void *data, u64 *val)
1067 if (tegra_core_rail) {
1068 *val = (u64)tegra_core_rail->offs_millivolts;
1074 static int core_offs_set(void *data, u64 val)
1076 if (tegra_core_rail) {
1077 mutex_lock(&dvfs_lock);
1078 tegra_core_rail->offs_millivolts = (int)val;
1079 dvfs_rail_update(tegra_core_rail);
1080 mutex_unlock(&dvfs_lock);
1085 DEFINE_SIMPLE_ATTRIBUTE(core_offs_fops, core_offs_get, core_offs_set, "%lld\n");
1087 int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root)
1091 d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL,
1096 d = debugfs_create_file("rails", S_IRUGO, clk_debugfs_root, NULL,
1101 d = debugfs_create_file("vdd_cpu_offs", S_IRUGO | S_IWUSR,
1102 clk_debugfs_root, NULL, &cpu_offs_fops);
1106 d = debugfs_create_file("vdd_core_offs", S_IRUGO | S_IWUSR,
1107 clk_debugfs_root, NULL, &core_offs_fops);