2 * Copyright (C) 2010 Google, Inc.
5 * Colin Cross <ccross@google.com>
7 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/kernel.h>
23 #include <linux/clk.h>
24 #include <linux/clkdev.h>
25 #include <linux/clk-provider.h>
26 #include <linux/debugfs.h>
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/list_sort.h>
30 #include <linux/module.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/suspend.h>
33 #include <linux/reboot.h>
34 #include <linux/platform_device.h>
36 #include <linux/pm_opp.h>
37 #include <linux/cpu.h>
39 #include <soc/tegra/tegra-dvfs.h>
41 struct dvfs_rail *tegra_cpu_rail;
42 struct dvfs_rail *tegra_core_rail;
43 static struct dvfs_rail *tegra_gpu_rail;
45 bool core_dvfs_started;
47 static LIST_HEAD(dvfs_rail_list);
48 static DEFINE_MUTEX(dvfs_lock);
50 static int dvfs_rail_update(struct dvfs_rail *rail);
52 static inline int tegra_dvfs_rail_get_disable_level(struct dvfs_rail *rail)
54 return rail->disable_millivolts ? : rail->nominal_millivolts;
57 static inline int tegra_dvfs_rail_get_suspend_level(struct dvfs_rail *rail)
59 return rail->suspend_millivolts ? : rail->nominal_millivolts;
62 void tegra_dvfs_add_relationships(struct dvfs_relationship *rels, int n)
65 struct dvfs_relationship *rel;
67 mutex_lock(&dvfs_lock);
69 for (i = 0; i < n; i++) {
71 list_add_tail(&rel->from_node, &rel->to->relationships_from);
72 list_add_tail(&rel->to_node, &rel->from->relationships_to);
75 mutex_unlock(&dvfs_lock);
78 int tegra_dvfs_init_rails(struct dvfs_rail *rails[], int n)
82 mutex_lock(&dvfs_lock);
84 for (i = 0; i < n; i++) {
85 INIT_LIST_HEAD(&rails[i]->dvfs);
86 INIT_LIST_HEAD(&rails[i]->relationships_from);
87 INIT_LIST_HEAD(&rails[i]->relationships_to);
89 mv = rails[i]->nominal_millivolts;
90 if (rails[i]->disable_millivolts > mv)
91 rails[i]->disable_millivolts = mv;
92 if (rails[i]->suspend_millivolts > mv)
93 rails[i]->suspend_millivolts = mv;
95 rails[i]->millivolts = mv;
96 rails[i]->new_millivolts = mv;
98 rails[i]->step = rails[i]->max_millivolts;
99 if (!rails[i]->step_up)
100 rails[i]->step_up = rails[i]->step;
102 list_add_tail(&rails[i]->node, &dvfs_rail_list);
104 if (!strcmp("vdd-cpu", rails[i]->reg_id))
105 tegra_cpu_rail = rails[i];
106 else if (!strcmp("vdd-core", rails[i]->reg_id))
107 tegra_core_rail = rails[i];
108 else if (!strcmp("vdd-gpu", rails[i]->reg_id))
109 tegra_gpu_rail = rails[i];
112 mutex_unlock(&dvfs_lock);
117 static int dvfs_solve_relationship(struct dvfs_relationship *rel)
119 return rel->solve(rel->from, rel->to);
122 static void dvfs_rail_stats_init(struct dvfs_rail *rail, int millivolts)
124 int dvfs_rail_stats_range;
126 if (!rail->stats.bin_uv)
127 rail->stats.bin_uv = DVFS_RAIL_STATS_BIN;
129 dvfs_rail_stats_range =
130 (DVFS_RAIL_STATS_TOP_BIN - 1) * rail->stats.bin_uv / 1000;
132 rail->stats.last_update = ktime_get();
133 if (millivolts >= rail->min_millivolts) {
134 int i = 1 + (2 * (millivolts - rail->min_millivolts) * 1000 +
135 rail->stats.bin_uv) / (2 * rail->stats.bin_uv);
136 rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN);
139 if (rail->max_millivolts >
140 rail->min_millivolts + dvfs_rail_stats_range)
141 pr_warn("tegra_dvfs: %s: stats above %d mV will be squashed\n",
143 rail->min_millivolts + dvfs_rail_stats_range);
146 static void dvfs_rail_stats_update(
147 struct dvfs_rail *rail, int millivolts, ktime_t now)
149 rail->stats.time_at_mv[rail->stats.last_index] = ktime_add(
150 rail->stats.time_at_mv[rail->stats.last_index], ktime_sub(
151 now, rail->stats.last_update));
152 rail->stats.last_update = now;
157 if (millivolts >= rail->min_millivolts) {
158 int i = 1 + (2 * (millivolts - rail->min_millivolts) * 1000 +
159 rail->stats.bin_uv) / (2 * rail->stats.bin_uv);
160 rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN);
161 } else if (millivolts == 0)
162 rail->stats.last_index = 0;
165 static int dvfs_rail_set_voltage_reg(struct dvfs_rail *rail, int millivolts)
169 ret = regulator_set_voltage(rail->reg,
171 rail->max_millivolts * 1000);
177 * dvfs_rail_set_voltage - set voltage in millivolts to specific rail
179 * @rail: struct dvfs_rail * power rail context
180 * @millivolts: voltage in millivolts to be set to regulator
182 * Sets the voltage on a dvfs rail to a specific value, and updates any
183 * rails that depend on this rail.
185 static int dvfs_rail_set_voltage(struct dvfs_rail *rail, int millivolts)
188 struct dvfs_relationship *rel;
195 if (millivolts == rail->millivolts)
201 if (millivolts > rail->millivolts) {
202 step = rail->step_up;
209 if (rail->dfll_mode) {
210 rail->millivolts = millivolts;
211 rail->new_millivolts = millivolts;
212 dvfs_rail_stats_update(rail, millivolts, ktime_get());
219 rail->resolving_to = true;
220 jmp_to_zero = rail->jmp_to_zero &&
221 ((millivolts == 0) || (rail->millivolts == 0));
222 if (jmp_to_zero || (rail->in_band_pm && rail->stats.off))
225 steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), step);
227 for (i = 0; i < steps; i++) {
229 (abs(millivolts - rail->millivolts) > step))
230 rail->new_millivolts = rail->millivolts + offset;
232 rail->new_millivolts = millivolts;
235 * Before changing the voltage, tell each rail that depends
236 * on this rail that the voltage will change.
237 * This rail will be the "from" rail in the relationship,
238 * the rail that depends on this rail will be the "to" rail.
239 * from->millivolts will be the old voltage
240 * from->new_millivolts will be the new voltage
242 list_for_each_entry(rel, &rail->relationships_to, to_node) {
243 ret = dvfs_rail_update(rel->to);
248 ret = dvfs_rail_set_voltage_reg(rail, rail->new_millivolts);
250 pr_err("Failed to set dvfs regulator %s\n",
255 rail->millivolts = rail->new_millivolts;
256 dvfs_rail_stats_update(rail, rail->millivolts, ktime_get());
259 * After changing the voltage, tell each rail that depends
260 * on this rail that the voltage has changed.
261 * from->millivolts and from->new_millivolts will be the
264 list_for_each_entry(rel, &rail->relationships_to, to_node) {
265 ret = dvfs_rail_update(rel->to);
271 if (unlikely(rail->millivolts != millivolts)) {
272 pr_err("%s: rail didn't reach target %d in %d steps (%d)\n",
273 __func__, millivolts, steps, rail->millivolts);
278 rail->resolving_to = false;
282 static inline int dvfs_rail_apply_limits(struct dvfs_rail *rail, int millivolts)
284 int min_mv = rail->min_millivolts;
285 int max_mv = rail->max_millivolts;
287 if (rail->therm_floors) {
288 int i = rail->therm_floor_idx;
290 if (i < rail->therm_floors_size)
291 min_mv = rail->therm_floors[i].mv;
294 if (rail->therm_caps) {
295 int i = rail->therm_cap_idx;
298 max_mv = rail->therm_caps[i - 1].mv;
301 if (rail->override_millivolts)
302 millivolts = rail->override_millivolts;
304 clamp_val(millivolts, min_mv, max_mv);
310 * dvfs_rail_update - update rail voltage
312 * @rail: struct dvfs_rail * power rail context
314 * Determine the minimum valid voltage for a rail, taking into account
315 * the dvfs clocks and any rails that this rail depends on. Calls
316 * dvfs_rail_set_voltage with the new voltage, which will call
317 * dvfs_rail_update on any rails that depend on this rail.
319 static int dvfs_rail_update(struct dvfs_rail *rail)
323 struct dvfs_relationship *rel;
330 /* if dvfs is suspended, return and handle it during resume */
334 /* if regulators are not connected yet, return and handle it later */
338 /* if rail update is entered while resolving circular dependencies,
340 if (rail->resolving_to)
343 /* Find the maximum voltage requested by any clock */
344 list_for_each_entry(d, &rail->dvfs, reg_node)
345 millivolts = max(d->cur_millivolts, millivolts);
347 /* Apply offset and min/max limits if any clock is requesting voltage */
349 millivolts = dvfs_rail_apply_limits(rail, millivolts);
350 /* Keep current voltage if regulator is to be disabled via explicitly */
351 else if (rail->in_band_pm)
353 /* Keep current voltage if regulator must not be disabled at run time */
354 else if (!rail->jmp_to_zero) {
355 WARN(1, "%s cannot be turned off by dvfs\n", rail->reg_id);
360 * retry update if limited by from-relationship to account for
361 * circular dependencies
363 steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
364 for (; steps >= 0; steps--) {
365 rail->new_millivolts = millivolts;
367 /* Check any rails that this rail depends on */
368 list_for_each_entry(rel, &rail->relationships_from, from_node)
369 rail->new_millivolts = dvfs_solve_relationship(rel);
371 if (rail->new_millivolts == rail->millivolts)
374 ret = dvfs_rail_set_voltage(rail, rail->new_millivolts);
380 static int dvfs_rail_connect_to_regulator(struct device *dev,
381 struct dvfs_rail *rail)
383 struct regulator *reg;
387 mutex_unlock(&dvfs_lock);
388 reg = regulator_get(dev, rail->reg_id);
389 mutex_lock(&dvfs_lock);
391 pr_err("tegra_dvfs: failed to connect %s rail\n",
398 if (!rail->in_band_pm) {
399 v = regulator_enable(rail->reg);
401 pr_err("tegra_dvfs: failed on enabling regulator %s\n, err %d",
407 v = regulator_get_voltage(rail->reg);
409 pr_err("tegra_dvfs: failed initial get %s voltage\n",
414 if (!rail->min_millivolts) {
417 if (!regulator_get_constraint_voltages(rail->reg, &min_uv,
419 rail->min_millivolts = min_uv / 1000;
422 rail->millivolts = v / 1000;
423 rail->new_millivolts = rail->millivolts;
424 dvfs_rail_stats_init(rail, rail->millivolts);
429 static inline const int *dvfs_get_millivolts(struct dvfs *d, unsigned long rate)
431 if (tegra_dvfs_is_dfll_scale(d, rate))
432 return d->dfll_millivolts;
434 return d->millivolts;
437 static unsigned long *dvfs_get_freqs(struct dvfs *d)
439 if (d->use_alt_freqs)
440 return &d->alt_freqs[0];
445 static int __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
449 unsigned long *freqs = dvfs_get_freqs(d);
450 const int *millivolts = dvfs_get_millivolts(d, rate);
452 if (freqs == NULL || millivolts == NULL)
456 * On entry to dfll range limit 1st step to range bottom (full ramp of
457 * voltage/rate is completed automatically in dfll mode)
459 if (tegra_dvfs_is_dfll_range_entry(d, rate))
460 rate = d->use_dfll_rate_min;
462 if (rate > freqs[d->num_freqs - 1]) {
463 pr_warn("tegra-dvfs: rate %lu too high for dvfs on %s\n", rate,
469 d->cur_millivolts = 0;
471 while (i < d->num_freqs && rate > freqs[i])
474 if ((d->max_millivolts) &&
475 (millivolts[i] > d->max_millivolts)) {
476 pr_warn("tegra-dvfs: voltage %d too high for dvfs on %s\n",
477 millivolts[i], d->clk_name);
482 d->cur_millivolts = millivolts[i];
487 ret = dvfs_rail_update(d->dvfs_rail);
489 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
490 d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
495 static struct dvfs *tegra_clk_to_dvfs(struct clk *c)
498 struct dvfs_rail *rail;
500 list_for_each_entry(rail, &dvfs_rail_list, node) {
501 list_for_each_entry(d, &rail->dvfs, reg_node) {
502 if (clk_is_match(c, d->clk))
509 static int predict_millivolts(struct dvfs *d, const int *millivolts,
513 unsigned long *freqs = dvfs_get_freqs(d);
518 for (i = 0; i < d->num_freqs; i++) {
519 if (rate <= freqs[i])
523 if (i == d->num_freqs)
526 return millivolts[i];
529 int opp_millivolts[MAX_DVFS_FREQS];
530 unsigned long opp_frequencies[MAX_DVFS_FREQS];
533 * tegra_get_cpu_fv_table - get CPU frequencies/voltages table
535 * @num_freqs: number of frequencies
536 * @freqs: the array of frequencies
537 * @mvs: the array of voltages
539 * Get the frequency and voltage table using CPU OPP which were built by the
542 int tegra_get_cpu_fv_table(int *num_freqs, unsigned long **freqs, int **mvs)
544 struct device *cpu_dev;
545 struct dev_pm_opp *opp;
549 cpu_dev = get_cpu_device(0);
553 mutex_lock(&dvfs_lock);
554 for (i = 0, rate = 0;; rate++) {
556 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);
561 opp_frequencies[i] = rate;
562 opp_millivolts[i++] = dev_pm_opp_get_voltage(opp);
571 *freqs = opp_frequencies;
572 *mvs = opp_millivolts;
574 mutex_unlock(&dvfs_lock);
577 EXPORT_SYMBOL(tegra_get_cpu_fv_table);
580 * tegra_dvfs_predict_millivolts - return the safe voltage for running
581 * the clock at one sepcific rate
583 * @c: struct clk * the clock which needs the voltage info
584 * @rate: the rate being predicted
586 * Extract the voltage table associated with the clock and return the safe
587 * voltage for ticking the clock at the specified rate
589 int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate)
592 const int *millivolts;
595 mutex_lock(&dvfs_lock);
597 d = tegra_clk_to_dvfs(c);
599 mutex_unlock(&dvfs_lock);
604 mutex_unlock(&dvfs_lock);
608 millivolts = dvfs_is_dfll_range(d, rate) ?
612 ret = predict_millivolts(d, millivolts, rate);
614 mutex_unlock(&dvfs_lock);
618 EXPORT_SYMBOL(tegra_dvfs_predict_millivolts);
620 int tegra_dvfs_predict_mv_at_hz_cur_tfloor(struct clk *c, unsigned long rate)
622 return tegra_dvfs_predict_millivolts(c, rate);
626 * tegra_dvfs_set_rate - update rail voltage due to the clock rate change
628 * @c: struct clk * the clock which has changed rate
629 * @rate: the changed rate
631 * Check if the voltage of the power rail need to be updated due to the clock
634 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
639 if (!core_dvfs_started)
642 mutex_lock(&dvfs_lock);
644 d = tegra_clk_to_dvfs(c);
646 ret = __tegra_dvfs_set_rate(d, rate);
648 mutex_unlock(&dvfs_lock);
652 EXPORT_SYMBOL(tegra_dvfs_set_rate);
655 * tegra_dvfs_get_rate - get current rate used for determining rail voltage
657 * @c: struct clk * clock we want to know the rate of used for determining
660 * Returns 0 if there is no dvfs for the clock.
662 unsigned long tegra_dvfs_get_rate(struct clk *c)
664 unsigned long rate = 0;
667 if (!core_dvfs_started)
670 mutex_lock(&dvfs_lock);
672 d = tegra_clk_to_dvfs(c);
676 mutex_unlock(&dvfs_lock);
680 EXPORT_SYMBOL(tegra_dvfs_get_rate);
683 * tegra_dvfs_get_freqs - export dvfs frequency array associated with the clock
685 * @c: struct clk * the clock which needs the frequency table
686 * @freqs: the array of the frequencies
687 * @num_freqs: number of the frequencies
689 * Check if the voltage of the power rail need to be updated due to the clock
692 int tegra_dvfs_get_freqs(struct clk *c, unsigned long **freqs, int *num_freqs)
696 if (!core_dvfs_started)
699 d = tegra_clk_to_dvfs(c);
701 pr_err("Failed to get dvfs structure\n");
705 *num_freqs = d->num_freqs;
706 *freqs = dvfs_get_freqs(d);
710 EXPORT_SYMBOL(tegra_dvfs_get_freqs);
712 unsigned long tegra_dvfs_get_maxrate(struct clk *c)
714 unsigned long rate = 0;
716 unsigned long *freqs;
718 if (!core_dvfs_started)
721 err = tegra_dvfs_get_freqs(c, &freqs, &num_freqs);
725 return freqs[num_freqs - 1];
728 unsigned long tegra_dvfs_round_rate(struct clk *c, unsigned long rate)
730 int i, err, num_freqs;
731 unsigned long *freqs;
733 if (!core_dvfs_started)
736 err = tegra_dvfs_get_freqs(c, &freqs, &num_freqs);
740 for (i = 0; i < num_freqs; i++)
741 if (freqs[i] >= rate)
747 int tegra_dvfs_use_alt_freqs_on_clk(struct clk *c, bool use_alt_freq)
752 mutex_lock(&dvfs_lock);
754 d = tegra_clk_to_dvfs(c);
755 if (!d && d->alt_freqs) {
757 if (d->use_alt_freqs != use_alt_freq) {
758 d->use_alt_freqs = use_alt_freq;
759 if (__tegra_dvfs_set_rate(d, d->cur_rate) < 0) {
760 d->use_alt_freqs = !use_alt_freq;
761 pr_err("%s: %s: %s alt dvfs failed\n", __func__,
763 use_alt_freq ? "set" : "clear");
764 __tegra_dvfs_set_rate(d, d->cur_rate);
770 mutex_unlock(&dvfs_lock);
774 EXPORT_SYMBOL(tegra_dvfs_use_alt_freqs_on_clk);
776 static int tegra_dvfs_clk_event(struct notifier_block *this,
777 unsigned long event, void *ptr)
779 struct clk_notifier_data *cnd = ptr;
782 d = tegra_clk_to_dvfs(cnd->clk);
786 if (d->dvfs_rail == tegra_core_rail && !core_dvfs_started)
789 if (!__clk_is_enabled(cnd->clk) && !__clk_is_prepared(cnd->clk))
793 case PRE_RATE_CHANGE:
794 if (cnd->old_rate < cnd->new_rate)
795 tegra_dvfs_set_rate(cnd->clk, cnd->new_rate);
797 case POST_RATE_CHANGE:
798 if (cnd->old_rate > cnd->new_rate)
799 tegra_dvfs_set_rate(cnd->clk, cnd->new_rate);
801 case ABORT_RATE_CHANGE:
808 static struct notifier_block tegra_dvfs_nb = {
809 .notifier_call = tegra_dvfs_clk_event,
813 static void cleanup_dvfs_table(struct dvfs *d)
817 for (i = 0; i < MAX_DVFS_FREQS; i++) {
818 if (d->millivolts[i] == 0)
822 d->freqs[i] *= d->freqs_mult;
824 /* If final frequencies are 0, pad with previous frequency */
825 if (d->freqs[i] == 0 && i > 1)
826 d->freqs[i] = d->freqs[i - 1];
832 int tegra_setup_dvfs(struct clk *c, struct dvfs *d)
834 cleanup_dvfs_table(d);
838 mutex_lock(&dvfs_lock);
839 list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
840 mutex_unlock(&dvfs_lock);
845 int tegra_dvfs_add_alt_freqs(struct clk *c, struct dvfs *alt_d)
850 mutex_lock(&dvfs_lock);
852 d = tegra_clk_to_dvfs(c);
858 cleanup_dvfs_table(alt_d);
860 d->alt_freqs = alt_d->freqs;
863 mutex_unlock(&dvfs_lock);
868 static bool tegra_dvfs_all_rails_suspended(void)
870 struct dvfs_rail *rail;
872 list_for_each_entry(rail, &dvfs_rail_list, node)
873 if (!rail->suspended && !rail->disabled)
879 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
881 struct dvfs_relationship *rel;
883 list_for_each_entry(rel, &to->relationships_from, from_node)
884 if ((!rel->from->suspended) &&
885 (!rel->from->disabled) &&
886 (!rel->solved_at_nominal))
892 static int tegra_dvfs_suspend_one(void)
894 struct dvfs_rail *rail;
898 list_for_each_entry(rail, &dvfs_rail_list, node) {
899 if ((rail->suspended) ||
901 (!tegra_dvfs_from_rails_suspended_or_solved(rail)))
904 mv = tegra_dvfs_rail_get_suspend_level(rail);
905 mv = dvfs_rail_apply_limits(rail, mv);
906 /* apply suspend limit only if it is above current mv */
907 if (mv >= rail->millivolts)
908 ret = dvfs_rail_set_voltage(rail, mv);
910 pr_err("tegra_dvfs: failed %s suspend at %d\n",
911 rail->reg_id, rail->millivolts);
915 rail->suspended = true;
921 static void tegra_dvfs_resume(void)
923 struct dvfs_rail *rail;
925 mutex_lock(&dvfs_lock);
927 list_for_each_entry(rail, &dvfs_rail_list, node)
928 rail->suspended = false;
930 list_for_each_entry(rail, &dvfs_rail_list, node)
931 dvfs_rail_update(rail);
933 mutex_unlock(&dvfs_lock);
936 static int tegra_dvfs_suspend(void)
940 mutex_lock(&dvfs_lock);
942 while (!tegra_dvfs_all_rails_suspended()) {
943 ret = tegra_dvfs_suspend_one();
948 mutex_unlock(&dvfs_lock);
956 int tegra_dvfs_init_thermal_dvfs_voltages(int *therm_voltages,
957 int *peak_voltages, int freqs_num, int ranges_num, struct dvfs *d)
960 int freq_idx, therm_idx;
962 for (therm_idx = 0; therm_idx < ranges_num; therm_idx++) {
963 millivolts = therm_voltages + therm_idx * MAX_DVFS_FREQS;
964 for (freq_idx = 0; freq_idx < freqs_num; freq_idx++) {
965 int mv = millivolts[freq_idx];
966 if ((mv > d->dvfs_rail->max_millivolts) ||
967 (mv < d->dvfs_rail->min_millivolts) ||
968 (freq_idx && (mv < millivolts[freq_idx - 1]))) {
969 WARN(1, "%s: invalid thermal dvfs entry %d(%d, %d)\n",
970 d->clk_name, mv, freq_idx, therm_idx);
973 if (mv > peak_voltages[freq_idx])
974 peak_voltages[freq_idx] = mv;
978 d->millivolts = therm_voltages;
979 d->peak_millivolts = peak_voltages;
980 d->therm_dvfs = ranges_num > 1;
985 static int tegra_dvfs_pm_notifier_event(struct notifier_block *nb,
986 unsigned long event, void *data)
988 if (event == PM_SUSPEND_PREPARE) {
989 if (tegra_dvfs_suspend())
991 pr_info("tegra_dvfs: suspended\n");
992 } else if (event == PM_POST_SUSPEND) {
994 pr_info("tegra_dvfs: resumed\n");
999 static struct notifier_block tegra_dvfs_pm_nb = {
1000 .notifier_call = tegra_dvfs_pm_notifier_event,
1004 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
1005 unsigned long event, void *data)
1011 tegra_dvfs_suspend();
1017 static struct notifier_block tegra_dvfs_reboot_nb = {
1018 .notifier_call = tegra_dvfs_reboot_notify,
1021 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
1026 if (rail->dfll_mode) {
1027 rail->disabled = true;
1031 mv = tegra_dvfs_rail_get_disable_level(rail);
1032 mv = dvfs_rail_apply_limits(rail, mv);
1034 if (mv >= rail->millivolts)
1035 ret = dvfs_rail_set_voltage(rail, mv);
1037 pr_err("tegra_dvfs: failed to disable %s at %d\n",
1038 rail->reg_id, rail->millivolts);
1041 rail->disabled = true;
1044 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
1046 rail->disabled = false;
1047 dvfs_rail_update(rail);
1050 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
1055 mutex_lock(&dvfs_lock);
1058 __tegra_dvfs_rail_enable(rail);
1060 mutex_unlock(&dvfs_lock);
1063 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
1068 mutex_lock(&dvfs_lock);
1072 __tegra_dvfs_rail_disable(rail);
1074 mutex_unlock(&dvfs_lock);
1077 bool tegra_dvfs_is_dfll_range(struct clk *c, unsigned long rate)
1081 d = tegra_clk_to_dvfs(c);
1083 pr_err("Failed to get dvfs structure\n");
1087 return dvfs_is_dfll_range(d, rate);
1089 EXPORT_SYMBOL(tegra_dvfs_is_dfll_range);
1091 int tegra_dvfs_set_dfll_range(struct clk *c, int range)
1096 mutex_lock(&dvfs_lock);
1097 d = tegra_clk_to_dvfs(c);
1099 pr_err("Failed to get dvfs structure\n");
1103 if (!d->dfll_millivolts)
1106 if ((range < DFLL_RANGE_NONE) || (range > DFLL_RANGE_HIGH_RATES))
1112 mutex_unlock(&dvfs_lock);
1115 EXPORT_SYMBOL(tegra_dvfs_set_dfll_range);
1117 int tegra_dvfs_dfll_mode_set(struct clk *c, unsigned long rate)
1121 mutex_lock(&dvfs_lock);
1123 d = tegra_clk_to_dvfs(c);
1125 pr_err("Failed to get dvfs structure\n");
1126 mutex_unlock(&dvfs_lock);
1130 if (!d->dvfs_rail->dfll_mode) {
1131 d->dvfs_rail->dfll_mode = true;
1132 __tegra_dvfs_set_rate(d, rate);
1135 mutex_unlock(&dvfs_lock);
1139 EXPORT_SYMBOL(tegra_dvfs_dfll_mode_set);
1141 int tegra_dvfs_dfll_mode_clear(struct clk *c, unsigned long rate)
1146 mutex_lock(&dvfs_lock);
1148 d = tegra_clk_to_dvfs(c);
1150 pr_err("Failed to get dvfs structure\n");
1151 mutex_unlock(&dvfs_lock);
1155 if (d->dvfs_rail->dfll_mode) {
1156 d->dvfs_rail->dfll_mode = false;
1157 d->dvfs_rail->millivolts = regulator_get_voltage(
1158 d->dvfs_rail->reg) / 1000;
1159 if (d->dvfs_rail->disabled) {
1160 d->dvfs_rail->disabled = false;
1161 __tegra_dvfs_rail_disable(d->dvfs_rail);
1163 ret = __tegra_dvfs_set_rate(d, rate);
1166 mutex_unlock(&dvfs_lock);
1170 EXPORT_SYMBOL(tegra_dvfs_dfll_mode_clear);
1172 int tegra_dvfs_get_dfll_threshold(struct clk *c, unsigned long *rate)
1176 d = tegra_clk_to_dvfs(c);
1178 pr_err("Failed to get dvfs structure\n");
1182 if (d->dvfs_rail && d->use_dfll_rate_min)
1183 *rate = d->use_dfll_rate_min;
1187 EXPORT_SYMBOL(tegra_dvfs_get_dfll_threshold);
1189 int tegra_dvfs_core_count_thermal_states(enum tegra_dvfs_core_thermal_type type)
1191 if (IS_ERR_OR_NULL(tegra_core_rail) || !tegra_core_rail->is_ready)
1194 if (type == TEGRA_DVFS_CORE_THERMAL_FLOOR)
1195 return tegra_core_rail->therm_floors_size;
1196 else if (type == TEGRA_DVFS_CORE_THERMAL_CAP)
1197 return tegra_core_rail->therm_caps_size;
1201 EXPORT_SYMBOL(tegra_dvfs_core_count_thermal_states);
1203 int tegra_dvfs_core_get_thermal_index(enum tegra_dvfs_core_thermal_type type)
1205 if (IS_ERR_OR_NULL(tegra_core_rail) || !tegra_core_rail->is_ready)
1208 if (type == TEGRA_DVFS_CORE_THERMAL_FLOOR)
1209 return tegra_core_rail->therm_floor_idx;
1210 else if (type == TEGRA_DVFS_CORE_THERMAL_CAP)
1211 return tegra_core_rail->therm_cap_idx;
1215 EXPORT_SYMBOL(tegra_dvfs_core_get_thermal_index);
1217 int tegra_dvfs_core_update_thermal_index(enum tegra_dvfs_core_thermal_type type,
1218 unsigned long new_idx)
1220 struct dvfs_rail *rail = tegra_core_rail;
1223 if (IS_ERR_OR_NULL(tegra_core_rail) || !tegra_core_rail->is_ready)
1226 mutex_lock(&dvfs_lock);
1227 if (type == TEGRA_DVFS_CORE_THERMAL_FLOOR) {
1228 if (rail->therm_floor_idx != new_idx) {
1229 rail->therm_floor_idx = new_idx;
1230 dvfs_rail_update(rail);
1232 } else if (type == TEGRA_DVFS_CORE_THERMAL_CAP) {
1233 if (rail->therm_cap_idx != new_idx) {
1234 rail->therm_cap_idx = new_idx;
1235 dvfs_rail_update(rail);
1240 mutex_unlock(&dvfs_lock);
1244 EXPORT_SYMBOL(tegra_dvfs_core_update_thermal_index);
1246 struct dvfs_rail *tegra_dvfs_get_rail_by_name(char *name)
1248 struct dvfs_rail *rail;
1250 list_for_each_entry(rail, &dvfs_rail_list, node) {
1251 if (!strcmp(rail->reg_id, name))
1258 bool tegra_dvfs_is_rail_up(struct dvfs_rail *rail)
1263 if (!rail->in_band_pm)
1266 return regulator_is_enabled(rail->reg);
1269 int tegra_dvfs_rail_power_up(struct dvfs_rail *rail)
1271 if (!rail || !rail->in_band_pm)
1274 return regulator_enable(rail->reg);
1277 int tegra_dvfs_rail_power_down(struct dvfs_rail *rail)
1279 if (!rail || !rail->in_band_pm)
1282 return regulator_disable(rail->reg);
1286 * Validate rail thermal floors/caps, and get its size.
1287 * Valid floors/caps:
1288 * - voltage limits are descending with temperature increasing.
1289 * - the lowest limit is above rail minimum voltage in pll and
1290 * in dfll mode (if applicable).
1291 * - the highest limit is below rail nominal voltage.
1293 static int get_thermal_limits_size(struct dvfs_rail *rail,
1294 enum tegra_dvfs_core_thermal_type type)
1296 const struct dvfs_therm_limits *limits;
1299 if (type == TEGRA_DVFS_CORE_THERMAL_FLOOR)
1300 limits = rail->therm_floors;
1301 else if (type == TEGRA_DVFS_CORE_THERMAL_CAP)
1302 limits = rail->therm_caps;
1306 if (!limits[0].mv) {
1307 pr_warn("%s: Missing thermal limits\n", rail->reg_id);
1311 for (i = 0; i < MAX_THERMAL_LIMITS - 1; i++) {
1312 if (!limits[i + 1].mv)
1315 if ((limits[i].temperature >= limits[i + 1].temperature) ||
1316 (limits[i].mv < limits[i + 1].mv)) {
1317 pr_warn("%s: Unordered thermal limits\n",
1323 if (limits[i].mv < rail->min_millivolts) {
1324 pr_warn("%s: Thermal floors below minimum voltage\n",
1332 void tegra_dvfs_init_therm_limits(struct dvfs_rail *rail)
1336 size = get_thermal_limits_size(rail, TEGRA_DVFS_CORE_THERMAL_FLOOR);
1337 if (size <= 0 || rail->therm_floors[0].mv > rail->nominal_millivolts) {
1338 rail->therm_floors = NULL;
1339 rail->therm_floors_size = 0;
1340 pr_warn("%s: invalid Vmin thermal floors\n", rail->reg_id);
1342 rail->therm_floors_size = size;
1343 rail->therm_floor_idx = 0;
1346 size = get_thermal_limits_size(rail, TEGRA_DVFS_CORE_THERMAL_CAP);
1348 rail->therm_caps = NULL;
1349 rail->therm_caps_size = 0;
1350 pr_warn("%s: invalid Vmax thermal caps\n", rail->reg_id);
1352 rail->therm_caps_size = size;
1353 rail->therm_cap_idx = size;
1357 static int tegra_config_dvfs(struct dvfs_rail *rail)
1362 list_for_each_entry(d, &rail->dvfs, reg_node) {
1363 if (__clk_is_enabled(d->clk) || __clk_is_prepared(d->clk)) {
1364 d->cur_rate = clk_get_rate(d->clk);
1365 d->cur_millivolts = d->max_millivolts;
1367 for (i = 0; i < d->num_freqs; i++)
1368 if (d->cur_rate <= d->freqs[i])
1371 if (i != d->num_freqs)
1372 d->cur_millivolts = d->millivolts[i];
1375 mutex_unlock(&dvfs_lock);
1376 clk_notifier_register(d->clk, &tegra_dvfs_nb);
1377 mutex_lock(&dvfs_lock);
1383 static int tegra_dvfs_regulator_init(struct device *dev)
1385 struct dvfs_rail *rail;
1388 mutex_lock(&dvfs_lock);
1390 list_for_each_entry(rail, &dvfs_rail_list, node) {
1391 err = dvfs_rail_connect_to_regulator(dev, rail);
1393 if (!rail->disabled)
1394 __tegra_dvfs_rail_disable(rail);
1396 mutex_unlock(&dvfs_lock);
1401 list_for_each_entry(rail, &dvfs_rail_list, node) {
1402 tegra_config_dvfs(rail);
1403 __tegra_dvfs_rail_enable(rail);
1406 core_dvfs_started = true;
1408 mutex_unlock(&dvfs_lock);
1410 register_pm_notifier(&tegra_dvfs_pm_nb);
1411 register_reboot_notifier(&tegra_dvfs_reboot_nb);
1416 #ifdef CONFIG_DEBUG_FS
1417 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
1419 struct dvfs *da = list_entry(a, struct dvfs, reg_node);
1420 struct dvfs *db = list_entry(b, struct dvfs, reg_node);
1423 ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
1427 if (da->cur_millivolts < db->cur_millivolts)
1429 if (da->cur_millivolts > db->cur_millivolts)
1432 return strcmp(da->clk_name, db->clk_name);
1435 /* To emulate and show rail relations with 0 mV on dependent rail-to */
1436 static struct dvfs_rail show_to;
1437 static struct dvfs_relationship show_rel;
1439 static int dvfs_tree_show(struct seq_file *s, void *data)
1442 struct dvfs_rail *rail;
1443 struct dvfs_relationship *rel;
1445 seq_puts(s, " clock rate mV\n");
1446 seq_puts(s, "-------------------------------------\n");
1448 mutex_lock(&dvfs_lock);
1450 list_for_each_entry(rail, &dvfs_rail_list, node) {
1453 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
1454 rail->stats.off ? 0 : rail->millivolts,
1455 rail->dfll_mode ? " dfll mode" :
1456 rail->disabled ? " disabled" : "");
1457 list_for_each_entry(rel, &rail->relationships_from, from_node) {
1459 show_rel.to = &show_to;
1461 show_to.millivolts = show_to.new_millivolts = 0;
1462 seq_printf(s, " %-10s %-7d mV %-4d mV .. %-4d mV\n",
1463 rel->from->reg_id, rel->from->millivolts,
1464 dvfs_solve_relationship(&show_rel),
1465 dvfs_solve_relationship(rel));
1467 seq_printf(s, " nominal %-7d mV\n",
1468 rail->nominal_millivolts);
1470 if ((rail->therm_floors) &&
1471 (rail->therm_floor_idx < rail->therm_floors_size)) {
1472 therm_mv = rail->therm_floors[rail->therm_floor_idx].mv;
1474 seq_printf(s, " therm_floor %-7d mV\n", therm_mv);
1476 if ((rail->therm_caps) &&
1477 (rail->therm_cap_idx > 0)) {
1478 therm_mv = rail->therm_caps[rail->therm_cap_idx - 1].mv;
1480 seq_printf(s, " therm_cap %-7d mV\n", therm_mv);
1482 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
1484 list_for_each_entry(d, &rail->dvfs, reg_node) {
1485 seq_printf(s, " %-15s %-10lu %-4d mV\n", d->clk_name,
1486 d->cur_rate, d->cur_millivolts);
1490 mutex_unlock(&dvfs_lock);
1495 static int dvfs_tree_open(struct inode *inode, struct file *file)
1497 return single_open(file, dvfs_tree_show, inode->i_private);
1500 static const struct file_operations dvfs_tree_fops = {
1501 .open = dvfs_tree_open,
1503 .llseek = seq_lseek,
1504 .release = single_release,
1507 static int dvfs_table_show(struct seq_file *s, void *data)
1511 struct dvfs_rail *rail;
1512 const int *v_pll, *last_v_pll = NULL;
1513 const int *v_dfll, *last_v_dfll = NULL;
1515 seq_puts(s, "DVFS tables: units mV/MHz\n");
1517 mutex_lock(&dvfs_lock);
1519 list_for_each_entry(rail, &dvfs_rail_list, node) {
1520 list_for_each_entry(d, &rail->dvfs, reg_node) {
1521 bool mv_done = false;
1522 v_pll = d->millivolts;
1523 v_dfll = d->dfll_millivolts;
1525 if (v_pll && (last_v_pll != v_pll)) {
1531 seq_printf(s, "%-16s", rail->reg_id);
1532 for (i = 0; i < d->num_freqs; i++)
1533 seq_printf(s, "%7d", v_pll[i]);
1537 if (v_dfll && (last_v_dfll != v_dfll)) {
1542 last_v_dfll = v_dfll;
1543 seq_printf(s, "%-8s (dfll) ", rail->reg_id);
1544 for (i = 0; i < d->num_freqs; i++)
1545 seq_printf(s, "%7d", v_dfll[i]);
1549 seq_printf(s, "%-16s", d->clk_name);
1550 for (i = 0; i < d->num_freqs; i++) {
1551 unsigned int f = d->freqs[i]/100000;
1552 seq_printf(s, " %4u.%u", f/10, f%10);
1556 seq_printf(s, "%-10s (alt)", d->clk_name);
1557 for (i = 0; i < d->num_freqs; i++) {
1558 unsigned int f = d->alt_freqs[i]/100000;
1559 seq_printf(s, " %4u.%u", f/10, f%10);
1567 mutex_unlock(&dvfs_lock);
1572 static int dvfs_table_open(struct inode *inode, struct file *file)
1574 return single_open(file, dvfs_table_show, inode->i_private);
1577 static const struct file_operations dvfs_table_fops = {
1578 .open = dvfs_table_open,
1580 .llseek = seq_lseek,
1581 .release = single_release,
1584 static int rail_stats_save_to_buf(char *buf, int len)
1587 struct dvfs_rail *rail;
1589 char *end = buf + len;
1591 str += scnprintf(str, end - str, "%-12s %-10s\n", "millivolts", "time");
1593 mutex_lock(&dvfs_lock);
1595 list_for_each_entry(rail, &dvfs_rail_list, node) {
1596 str += scnprintf(str, end - str, "%s (bin: %d.%dmV)\n",
1598 rail->stats.bin_uv / 1000,
1599 (rail->stats.bin_uv / 10) % 100);
1601 dvfs_rail_stats_update(rail, -1, ktime_get());
1603 str += scnprintf(str, end - str, "%-12d %-10llu\n", 0,
1604 cputime64_to_clock_t(msecs_to_jiffies(
1605 ktime_to_ms(rail->stats.time_at_mv[0]))));
1607 for (i = 1; i <= DVFS_RAIL_STATS_TOP_BIN; i++) {
1608 ktime_t ktime_zero = ktime_set(0, 0);
1609 if (ktime_equal(rail->stats.time_at_mv[i], ktime_zero))
1611 str += scnprintf(str, end - str, "%-12d %-10llu\n",
1612 rail->min_millivolts +
1613 (i - 1) * rail->stats.bin_uv / 1000,
1614 cputime64_to_clock_t(msecs_to_jiffies(
1615 ktime_to_ms(rail->stats.time_at_mv[i])))
1619 mutex_unlock(&dvfs_lock);
1623 static int rail_stats_show(struct seq_file *s, void *data)
1625 char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
1631 size = rail_stats_save_to_buf(buf, PAGE_SIZE);
1632 seq_write(s, buf, size);
1637 static int rail_stats_open(struct inode *inode, struct file *file)
1639 return single_open(file, rail_stats_show, inode->i_private);
1642 static const struct file_operations rail_stats_fops = {
1643 .open = rail_stats_open,
1645 .llseek = seq_lseek,
1646 .release = single_release,
1649 static int gpu_dvfs_t_show(struct seq_file *s, void *data)
1655 struct dvfs_rail *rail = tegra_gpu_rail;
1656 int max_mv[MAX_DVFS_FREQS] = {};
1658 if (!tegra_gpu_rail) {
1659 seq_printf(s, "Only supported for T124 or higher\n");
1663 mutex_lock(&dvfs_lock);
1665 d = list_first_entry(&rail->dvfs, struct dvfs, reg_node);
1666 if (rail->vts_cdev && d->therm_dvfs) {
1667 num_ranges = rail->vts_number_of_trips + 1;
1668 trips = rail->vts_trips_table;
1671 seq_printf(s, "%-11s", "T(C)\\F(kHz)");
1672 for (i = 0; i < d->num_freqs; i++) {
1673 unsigned int f = d->freqs[i]/1000;
1674 seq_printf(s, " %7u", f);
1676 seq_printf(s, "\n");
1678 for (j = 0; j < num_ranges; j++) {
1679 seq_printf(s, "%s", j == rail->therm_scale_idx ? ">" : " ");
1681 if (!trips || (num_ranges == 1))
1682 seq_printf(s, "%4s..%-4s", "", "");
1684 seq_printf(s, "%4s..%-4d", "", trips[j]);
1685 else if (j == num_ranges - 1)
1686 seq_printf(s, "%4d..%-4s", trips[j], "");
1688 seq_printf(s, "%4d..%-4d", trips[j-1], trips[j]);
1690 for (i = 0; i < d->num_freqs; i++) {
1691 int mv = *(d->millivolts + j * MAX_DVFS_FREQS + i);
1692 seq_printf(s, " %7d", mv);
1693 max_mv[i] = max(max_mv[i], mv);
1695 seq_printf(s, " mV\n");
1698 seq_printf(s, "%3s%-8s\n", "", "------");
1699 seq_printf(s, "%3s%-8s", "", "max(T)");
1700 for (i = 0; i < d->num_freqs; i++)
1701 seq_printf(s, " %7d", max_mv[i]);
1702 seq_printf(s, " mV\n");
1704 mutex_unlock(&dvfs_lock);
1709 static int gpu_dvfs_t_open(struct inode *inode, struct file *file)
1711 return single_open(file, gpu_dvfs_t_show, NULL);
1714 static const struct file_operations gpu_dvfs_t_fops = {
1715 .open = gpu_dvfs_t_open,
1717 .llseek = seq_lseek,
1718 .release = single_release,
1721 static int dvfs_debugfs_init(void)
1723 struct dentry *d_root, *d;
1725 d_root = debugfs_create_dir("tegra_dvfs", NULL);
1729 d = debugfs_create_file("dvfs", S_IRUGO, d_root, NULL,
1734 d = debugfs_create_file("dvfs_table", S_IRUGO, d_root, NULL,
1739 d = debugfs_create_file("rails", S_IRUGO, d_root, NULL,
1744 d = debugfs_create_file("gpu_dvfs_t", S_IRUGO | S_IWUSR, d_root, NULL,
1754 typedef int (*dvfs_init_cb_t)(void);
1756 static const struct of_device_id tegra_dvfs_of_match[] = {
1757 { .compatible = "nvidia,tegra124-dvfs", .data = tegra124_init_dvfs },
1758 { .compatible = "nvidia,tegra210-dvfs", .data = tegra210_init_dvfs },
1762 static int tegra_dvfs_probe(struct platform_device *pdev)
1764 const struct of_device_id *match;
1765 dvfs_init_cb_t dvfs_init_cb;
1766 struct dvfs_rail *rail;
1769 match = of_match_node(tegra_dvfs_of_match, pdev->dev.of_node);
1773 dvfs_init_cb = (dvfs_init_cb_t)match->data;
1774 ret = dvfs_init_cb();
1778 ret = tegra_dvfs_regulator_init(&pdev->dev);
1782 list_for_each_entry(rail, &dvfs_rail_list, node) {
1783 rail->is_ready = true;
1786 #ifdef CONFIG_DEBUG_FS
1787 dvfs_debugfs_init();
1794 static int tegra_dvfs_remove(struct platform_device *pdev)
1798 core_dvfs_started = false;
1800 unregister_pm_notifier(&tegra_dvfs_reboot_nb);
1801 unregister_pm_notifier(&tegra_dvfs_pm_nb);
1803 list_for_each_entry(d, &tegra_core_rail->dvfs, reg_node) {
1804 clk_notifier_unregister(d->clk, &tegra_dvfs_nb);
1811 static struct platform_driver tegra_dvfs_platdrv = {
1813 .name = "tegra-dvfs",
1814 .owner = THIS_MODULE,
1815 .of_match_table = tegra_dvfs_of_match,
1817 .probe = tegra_dvfs_probe,
1818 .remove = tegra_dvfs_remove,
1820 module_platform_driver(tegra_dvfs_platdrv);