ARM: tegra11: dvfs: Add core rail thermal trip-point
[linux-3.10.git] / arch / arm / mach-tegra / dvfs.c
1 /*
2  *
3  * Copyright (C) 2010 Google, Inc.
4  *
5  * Author:
6  *      Colin Cross <ccross@google.com>
7  *
8  * Copyright (C) 2010-2011 NVIDIA Corporation.
9  *
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.
13  *
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.
18  *
19  */
20
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>
36
37 #include "board.h"
38 #include "clock.h"
39 #include "dvfs.h"
40
41 #define DVFS_RAIL_STATS_BIN     12500
42
43 struct dvfs_rail *tegra_cpu_rail;
44 struct dvfs_rail *tegra_core_rail;
45
46 static LIST_HEAD(dvfs_rail_list);
47 static DEFINE_MUTEX(dvfs_lock);
48 static DEFINE_MUTEX(rail_disable_lock);
49
50 static int dvfs_rail_update(struct dvfs_rail *rail);
51
52 void tegra_dvfs_add_relationships(struct dvfs_relationship *rels, int n)
53 {
54         int i;
55         struct dvfs_relationship *rel;
56
57         mutex_lock(&dvfs_lock);
58
59         for (i = 0; i < n; i++) {
60                 rel = &rels[i];
61                 list_add_tail(&rel->from_node, &rel->to->relationships_from);
62                 list_add_tail(&rel->to_node, &rel->from->relationships_to);
63         }
64
65         mutex_unlock(&dvfs_lock);
66 }
67
68 int tegra_dvfs_init_rails(struct dvfs_rail *rails[], int n)
69 {
70         int i;
71
72         mutex_lock(&dvfs_lock);
73
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;
80                 if (!rails[i]->step)
81                         rails[i]->step = rails[i]->max_millivolts;
82
83                 list_add_tail(&rails[i]->node, &dvfs_rail_list);
84
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];
89         }
90
91         mutex_unlock(&dvfs_lock);
92
93         return 0;
94 };
95
96 static int dvfs_solve_relationship(struct dvfs_relationship *rel)
97 {
98         return rel->solve(rel->from, rel->to);
99 }
100
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)
104 {
105         int dvfs_rail_stats_range;
106
107         if (!rail->stats.bin_uV)
108                 rail->stats.bin_uV = DVFS_RAIL_STATS_BIN;
109
110         dvfs_rail_stats_range =
111                 (DVFS_RAIL_STATS_TOP_BIN - 1) * rail->stats.bin_uV / 1000;
112
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);
118         }
119
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",
123                         rail->reg_id,
124                         rail->min_millivolts + dvfs_rail_stats_range);
125 }
126
127 static void dvfs_rail_stats_update(
128         struct dvfs_rail *rail, int millivolts, ktime_t now)
129 {
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;
134
135         if (rail->stats.off)
136                 return;
137
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;
144 }
145
146 static void dvfs_rail_stats_pause(struct dvfs_rail *rail,
147                                   ktime_t delta, bool on)
148 {
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);
151 }
152
153 void tegra_dvfs_rail_off(struct dvfs_rail *rail, ktime_t now)
154 {
155         if (rail) {
156                 dvfs_rail_stats_update(rail, 0, now);
157                 rail->stats.off = true;
158         }
159 }
160
161 void tegra_dvfs_rail_on(struct dvfs_rail *rail, ktime_t now)
162 {
163         if (rail) {
164                 rail->stats.off = false;
165                 dvfs_rail_stats_update(rail, rail->millivolts, now);
166         }
167 }
168
169 void tegra_dvfs_rail_pause(struct dvfs_rail *rail, ktime_t delta, bool on)
170 {
171         if (rail)
172                 dvfs_rail_stats_pause(rail, delta, on);
173 }
174
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)
178 {
179         int ret = 0;
180         struct dvfs_relationship *rel;
181         int step = (millivolts > rail->millivolts) ? rail->step : -rail->step;
182         int i;
183         int steps;
184         bool jmp_to_zero;
185
186         if (!rail->reg) {
187                 if (millivolts == rail->millivolts)
188                         return 0;
189                 else
190                         return -EINVAL;
191         }
192
193         /*
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
198          */
199         if (rail->dfll_mode) {
200                 rail->millivolts = rail->new_millivolts = millivolts - 1;
201                 dvfs_rail_stats_update(rail, millivolts, ktime_get());
202                 return 0;
203         }
204
205         if (rail->disabled)
206                 return 0;
207
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);
213
214         for (i = 0; i < steps; i++) {
215                 if (!jmp_to_zero &&
216                     (abs(millivolts - rail->millivolts) > rail->step))
217                         rail->new_millivolts = rail->millivolts + step;
218                 else
219                         rail->new_millivolts = millivolts;
220
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);
229                         if (ret)
230                                 goto out;
231                 }
232
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;
242                 }
243                 if (ret) {
244                         pr_err("Failed to set dvfs regulator %s\n", rail->reg_id);
245                         goto out;
246                 }
247
248                 rail->millivolts = rail->new_millivolts;
249                 dvfs_rail_stats_update(rail, rail->millivolts, ktime_get());
250
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
254                  * new voltage */
255                 list_for_each_entry(rel, &rail->relationships_to, to_node) {
256                         ret = dvfs_rail_update(rel->to);
257                         if (ret)
258                                 goto out;
259                 }
260         }
261
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);
265                 ret = -EINVAL;
266         }
267
268 out:
269         rail->resolving_to = false;
270         return ret;
271 }
272
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)
278 {
279         int millivolts = 0;
280         struct dvfs *d;
281         struct dvfs_relationship *rel;
282         int ret = 0;
283         int steps;
284
285         /* if dvfs is suspended, return and handle it during resume */
286         if (rail->suspended)
287                 return 0;
288
289         /* if regulators are not connected yet, return and handle it later */
290         if (!rail->reg)
291                 return 0;
292
293         /* if rail update is entered while resolving circular dependencies,
294            abort recursion */
295         if (rail->resolving_to)
296                 return 0;
297
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);
301
302         /* Apply offset if any clock is requesting voltage */
303         if (millivolts) {
304                 millivolts += rail->offs_millivolts;
305                 if (millivolts > rail->max_millivolts)
306                         millivolts = rail->max_millivolts;
307                 else if (millivolts < rail->min_millivolts)
308                         millivolts = rail->min_millivolts;
309         }
310
311         /* retry update if limited by from-relationship to account for
312            circular dependencies */
313         steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
314         for (; steps >= 0; steps--) {
315                 rail->new_millivolts = millivolts;
316
317                 /* Check any rails that this rail depends on */
318                 list_for_each_entry(rel, &rail->relationships_from, from_node)
319                         rail->new_millivolts = dvfs_solve_relationship(rel);
320
321                 if (rail->new_millivolts == rail->millivolts)
322                         break;
323
324                 ret = dvfs_rail_set_voltage(rail, rail->new_millivolts);
325         }
326
327         return ret;
328 }
329
330 static int dvfs_rail_connect_to_regulator(struct dvfs_rail *rail)
331 {
332         struct regulator *reg;
333         int v;
334
335         if (!rail->reg) {
336                 reg = regulator_get(NULL, rail->reg_id);
337                 if (IS_ERR(reg)) {
338                         pr_err("tegra_dvfs: failed to connect %s rail\n",
339                                rail->reg_id);
340                         return -EINVAL;
341                 }
342                 rail->reg = reg;
343         }
344
345         v = regulator_enable(rail->reg);
346         if (v < 0) {
347                 pr_err("tegra_dvfs: failed on enabling regulator %s\n, err %d",
348                         rail->reg_id, v);
349                 return v;
350         }
351
352         v = regulator_get_voltage(rail->reg);
353         if (v < 0) {
354                 pr_err("tegra_dvfs: failed initial get %s voltage\n",
355                        rail->reg_id);
356                 return v;
357         }
358         rail->millivolts = v / 1000;
359         rail->new_millivolts = rail->millivolts;
360         dvfs_rail_stats_init(rail, rail->millivolts);
361         return 0;
362 }
363
364 static inline unsigned long *dvfs_get_freqs(struct dvfs *d)
365 {
366         return d->alt_freqs ? : &d->freqs[0];
367 }
368
369 static inline const int *dvfs_get_millivolts(struct dvfs *d, unsigned long rate)
370 {
371         if (tegra_dvfs_is_dfll_scale(d, rate))
372                 return d->dfll_millivolts;
373
374         return d->millivolts;
375 }
376
377 static int
378 __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
379 {
380         int i = 0;
381         int ret;
382         unsigned long *freqs = dvfs_get_freqs(d);
383         const int *millivolts = dvfs_get_millivolts(d, rate);
384
385         if (freqs == NULL || millivolts == NULL)
386                 return -ENODEV;
387
388         if (rate > freqs[d->num_freqs - 1]) {
389                 pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate,
390                         d->clk_name);
391                 return -EINVAL;
392         }
393
394         if (rate == 0) {
395                 d->cur_millivolts = 0;
396         } else {
397                 while (i < d->num_freqs && rate > freqs[i])
398                         i++;
399
400                 if ((d->max_millivolts) &&
401                     (millivolts[i] > d->max_millivolts)) {
402                         pr_warn("tegra_dvfs: voltage %d too high for dvfs on"
403                                 " %s\n", millivolts[i], d->clk_name);
404                         return -EINVAL;
405                 }
406                 d->cur_millivolts = millivolts[i];
407         }
408
409         d->cur_rate = rate;
410
411         ret = dvfs_rail_update(d->dvfs_rail);
412         if (ret)
413                 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
414                         d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
415
416         return ret;
417 }
418
419 int tegra_dvfs_alt_freqs_set(struct dvfs *d, unsigned long *alt_freqs)
420 {
421         int ret = 0;
422
423         mutex_lock(&dvfs_lock);
424
425         if (d->alt_freqs != alt_freqs) {
426                 d->alt_freqs = alt_freqs;
427                 ret = __tegra_dvfs_set_rate(d, d->cur_rate);
428         }
429
430         mutex_unlock(&dvfs_lock);
431         return ret;
432 }
433
434 static int predict_millivolts(struct clk *c, const int *millivolts,
435                               unsigned long rate)
436 {
437         int i;
438
439         if (!millivolts)
440                 return -ENODEV;
441         /*
442          * Predicted voltage can not be used across the switch to alternative
443          * frequency limits. For now, just fail the call for clock that has
444          * alternative limits initialized.
445          */
446         if (c->dvfs->alt_freqs)
447                 return -ENOSYS;
448
449         for (i = 0; i < c->dvfs->num_freqs; i++) {
450                 if (rate <= c->dvfs->freqs[i])
451                         break;
452         }
453
454         if (i == c->dvfs->num_freqs)
455                 return -EINVAL;
456
457         return millivolts[i];
458 }
459
460 int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate)
461 {
462         const int *millivolts;
463
464         if (!rate || !c->dvfs)
465                 return 0;
466
467         millivolts = dvfs_get_millivolts(c->dvfs, rate);
468         return predict_millivolts(c, millivolts, rate);
469 }
470
471 int tegra_dvfs_predict_millivolts_pll(struct clk *c, unsigned long rate)
472 {
473         const int *millivolts;
474
475         if (!rate || !c->dvfs)
476                 return 0;
477
478         millivolts = c->dvfs->millivolts;
479         return predict_millivolts(c, millivolts, rate);
480 }
481
482 int tegra_dvfs_predict_millivolts_dfll(struct clk *c, unsigned long rate)
483 {
484         const int *millivolts;
485
486         if (!rate || !c->dvfs)
487                 return 0;
488
489         millivolts = c->dvfs->dfll_millivolts;
490         return predict_millivolts(c, millivolts, rate);
491 }
492
493 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
494 {
495         int ret;
496
497         if (!c->dvfs)
498                 return -EINVAL;
499
500         mutex_lock(&dvfs_lock);
501         ret = __tegra_dvfs_set_rate(c->dvfs, rate);
502         mutex_unlock(&dvfs_lock);
503
504         return ret;
505 }
506 EXPORT_SYMBOL(tegra_dvfs_set_rate);
507
508 /* May only be called during clock init, does not take any locks on clock c. */
509 int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d)
510 {
511         int i;
512
513         if (c->dvfs) {
514                 pr_err("Error when enabling dvfs on %s for clock %s:\n",
515                         d->dvfs_rail->reg_id, c->name);
516                 pr_err("DVFS already enabled for %s\n",
517                         c->dvfs->dvfs_rail->reg_id);
518                 return -EINVAL;
519         }
520
521         for (i = 0; i < MAX_DVFS_FREQS; i++) {
522                 if (d->millivolts[i] == 0)
523                         break;
524
525                 d->freqs[i] *= d->freqs_mult;
526
527                 /* If final frequencies are 0, pad with previous frequency */
528                 if (d->freqs[i] == 0 && i > 1)
529                         d->freqs[i] = d->freqs[i - 1];
530         }
531         d->num_freqs = i;
532
533         if (d->auto_dvfs) {
534                 c->auto_dvfs = true;
535                 clk_set_cansleep(c);
536         }
537
538         c->dvfs = d;
539
540         mutex_lock(&dvfs_lock);
541         list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
542         mutex_unlock(&dvfs_lock);
543
544         return 0;
545 }
546
547 static bool tegra_dvfs_all_rails_suspended(void)
548 {
549         struct dvfs_rail *rail;
550         bool all_suspended = true;
551
552         list_for_each_entry(rail, &dvfs_rail_list, node)
553                 if (!rail->suspended && !rail->disabled)
554                         all_suspended = false;
555
556         return all_suspended;
557 }
558
559 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
560 {
561         struct dvfs_relationship *rel;
562         bool all_suspended = true;
563
564         list_for_each_entry(rel, &to->relationships_from, from_node)
565                 if (!rel->from->suspended && !rel->from->disabled &&
566                         !rel->solved_at_nominal)
567                         all_suspended = false;
568
569         return all_suspended;
570 }
571
572 static int tegra_dvfs_suspend_one(void)
573 {
574         struct dvfs_rail *rail;
575         int ret;
576
577         list_for_each_entry(rail, &dvfs_rail_list, node) {
578                 if (!rail->suspended && !rail->disabled &&
579                     tegra_dvfs_from_rails_suspended_or_solved(rail)) {
580                         ret = dvfs_rail_set_voltage(rail,
581                                 rail->nominal_millivolts);
582                         if (ret)
583                                 return ret;
584                         rail->suspended = true;
585                         return 0;
586                 }
587         }
588
589         return -EINVAL;
590 }
591
592 static void tegra_dvfs_resume(void)
593 {
594         struct dvfs_rail *rail;
595
596         mutex_lock(&dvfs_lock);
597
598         list_for_each_entry(rail, &dvfs_rail_list, node)
599                 rail->suspended = false;
600
601         list_for_each_entry(rail, &dvfs_rail_list, node)
602                 dvfs_rail_update(rail);
603
604         mutex_unlock(&dvfs_lock);
605 }
606
607 static int tegra_dvfs_suspend(void)
608 {
609         int ret = 0;
610
611         mutex_lock(&dvfs_lock);
612
613         while (!tegra_dvfs_all_rails_suspended()) {
614                 ret = tegra_dvfs_suspend_one();
615                 if (ret)
616                         break;
617         }
618
619         mutex_unlock(&dvfs_lock);
620
621         if (ret)
622                 tegra_dvfs_resume();
623
624         return ret;
625 }
626
627 static int tegra_dvfs_pm_notify(struct notifier_block *nb,
628                                 unsigned long event, void *data)
629 {
630         switch (event) {
631         case PM_SUSPEND_PREPARE:
632                 if (tegra_dvfs_suspend())
633                         return NOTIFY_STOP;
634                 break;
635         case PM_POST_SUSPEND:
636                 tegra_dvfs_resume();
637                 break;
638         }
639
640         return NOTIFY_OK;
641 };
642
643 static struct notifier_block tegra_dvfs_nb = {
644         .notifier_call = tegra_dvfs_pm_notify,
645 };
646
647 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
648                                 unsigned long event, void *data)
649 {
650         switch (event) {
651         case SYS_RESTART:
652         case SYS_HALT:
653         case SYS_POWER_OFF:
654                 tegra_dvfs_suspend();
655                 return NOTIFY_OK;
656         }
657         return NOTIFY_DONE;
658 }
659
660 static struct notifier_block tegra_dvfs_reboot_nb = {
661         .notifier_call = tegra_dvfs_reboot_notify,
662 };
663
664 /* must be called with dvfs lock held */
665 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
666 {
667         int ret;
668
669         /* don't set voltage in DFLL mode - won't work, but break stats */
670         if (rail->dfll_mode) {
671                 rail->disabled = true;
672                 return;
673         }
674
675         ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts);
676         if (ret) {
677                 pr_info("dvfs: failed to set regulator %s to disable "
678                         "voltage %d\n", rail->reg_id,
679                         rail->nominal_millivolts);
680                 return;
681         }
682         rail->disabled = true;
683 }
684
685 /* must be called with dvfs lock held */
686 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
687 {
688         rail->disabled = false;
689         dvfs_rail_update(rail);
690 }
691
692 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
693 {
694         mutex_lock(&rail_disable_lock);
695
696         if (rail->disabled) {
697                 mutex_lock(&dvfs_lock);
698                 __tegra_dvfs_rail_enable(rail);
699                 mutex_unlock(&dvfs_lock);
700
701                 tegra_dvfs_rail_post_enable(rail);
702         }
703         mutex_unlock(&rail_disable_lock);
704
705 }
706
707 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
708 {
709         mutex_lock(&rail_disable_lock);
710         if (rail->disabled)
711                 goto out;
712
713         /* rail disable will set it to nominal voltage underneath clock
714            framework - need to re-configure clock rates that are not safe
715            at nominal (yes, unsafe at nominal is ugly, but possible). Rate
716            change must be done outside of dvfs lock. */
717         if (tegra_dvfs_rail_disable_prepare(rail)) {
718                 pr_info("dvfs: failed to prepare regulator %s to disable\n",
719                         rail->reg_id);
720                 goto out;
721         }
722
723         mutex_lock(&dvfs_lock);
724         __tegra_dvfs_rail_disable(rail);
725         mutex_unlock(&dvfs_lock);
726 out:
727         mutex_unlock(&rail_disable_lock);
728 }
729
730 int tegra_dvfs_rail_disable_by_name(const char *reg_id)
731 {
732         struct dvfs_rail *rail = tegra_dvfs_get_rail_by_name(reg_id);
733         if (!rail)
734                 return -EINVAL;
735
736         tegra_dvfs_rail_disable(rail);
737         return 0;
738 }
739
740 struct dvfs_rail *tegra_dvfs_get_rail_by_name(const char *reg_id)
741 {
742         struct dvfs_rail *rail;
743
744         mutex_lock(&dvfs_lock);
745         list_for_each_entry(rail, &dvfs_rail_list, node) {
746                 if (!strcmp(reg_id, rail->reg_id)) {
747                         mutex_unlock(&dvfs_lock);
748                         return rail;
749                 }
750         }
751         mutex_unlock(&dvfs_lock);
752         return NULL;
753 }
754
755 bool tegra_dvfs_rail_updating(struct clk *clk)
756 {
757         return (!clk ? false :
758                 (!clk->dvfs ? false :
759                  (!clk->dvfs->dvfs_rail ? false :
760                   (clk->dvfs->dvfs_rail->updating))));
761 }
762
763 #ifdef CONFIG_OF
764 int __init of_tegra_dvfs_init(const struct of_device_id *matches)
765 {
766         int ret;
767         struct device_node *np;
768
769         for_each_matching_node(np, matches) {
770                 const struct of_device_id *match = of_match_node(matches, np);
771                 of_tegra_dvfs_init_cb_t dvfs_init_cb = match->data;
772                 ret = dvfs_init_cb(np);
773                 if (ret) {
774                         pr_err("dt: Failed to read %s tables from DT\n",
775                                                         match->compatible);
776                         return ret;
777                 }
778         }
779         return 0;
780 }
781 #endif
782 int tegra_dvfs_dfll_mode_set(struct dvfs *d, unsigned long rate)
783 {
784         mutex_lock(&dvfs_lock);
785         if (!d->dvfs_rail->dfll_mode) {
786                 d->dvfs_rail->dfll_mode = true;
787                 __tegra_dvfs_set_rate(d, rate);
788         }
789         mutex_unlock(&dvfs_lock);
790         return 0;
791 }
792
793 int tegra_dvfs_dfll_mode_clear(struct dvfs *d, unsigned long rate)
794 {
795         int ret = 0;
796
797         mutex_lock(&dvfs_lock);
798         if (d->dvfs_rail->dfll_mode) {
799                 d->dvfs_rail->dfll_mode = false;
800                 if (d->dvfs_rail->disabled) {
801                         d->dvfs_rail->disabled = false;
802                         __tegra_dvfs_rail_disable(d->dvfs_rail);
803                 }
804                 ret = __tegra_dvfs_set_rate(d, rate);
805         }
806         mutex_unlock(&dvfs_lock);
807         return ret;
808 }
809
810 struct tegra_cooling_device *tegra_dvfs_get_cpu_dfll_cdev(void)
811 {
812         if (tegra_cpu_rail)
813                 return tegra_cpu_rail->dfll_mode_cdev;
814         return NULL;
815 }
816
817 struct tegra_cooling_device *tegra_dvfs_get_core_cdev(void)
818 {
819         if (tegra_core_rail)
820                 return tegra_core_rail->pll_mode_cdev;
821         return NULL;
822 }
823
824 /*
825  * Iterate through all the dvfs regulators, finding the regulator exported
826  * by the regulator api for each one.  Must be called in late init, after
827  * all the regulator api's regulators are initialized.
828  */
829 int __init tegra_dvfs_late_init(void)
830 {
831         bool connected = true;
832         struct dvfs_rail *rail;
833
834         mutex_lock(&dvfs_lock);
835
836         list_for_each_entry(rail, &dvfs_rail_list, node)
837                 if (dvfs_rail_connect_to_regulator(rail))
838                         connected = false;
839
840         list_for_each_entry(rail, &dvfs_rail_list, node)
841                 if (connected)
842                         dvfs_rail_update(rail);
843                 else
844                         __tegra_dvfs_rail_disable(rail);
845
846         mutex_unlock(&dvfs_lock);
847
848 #ifdef CONFIG_TEGRA_SILICON_PLATFORM
849         if (!connected)
850                 return -ENODEV;
851 #endif
852         register_pm_notifier(&tegra_dvfs_nb);
853         register_reboot_notifier(&tegra_dvfs_reboot_nb);
854
855         return 0;
856 }
857
858 #ifdef CONFIG_DEBUG_FS
859 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
860 {
861         struct dvfs *da = list_entry(a, struct dvfs, reg_node);
862         struct dvfs *db = list_entry(b, struct dvfs, reg_node);
863         int ret;
864
865         ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
866         if (ret != 0)
867                 return ret;
868
869         if (da->cur_millivolts < db->cur_millivolts)
870                 return 1;
871         if (da->cur_millivolts > db->cur_millivolts)
872                 return -1;
873
874         return strcmp(da->clk_name, db->clk_name);
875 }
876
877 static int dvfs_tree_show(struct seq_file *s, void *data)
878 {
879         struct dvfs *d;
880         struct dvfs_rail *rail;
881         struct dvfs_relationship *rel;
882
883         seq_printf(s, "   clock      rate       mV\n");
884         seq_printf(s, "--------------------------------\n");
885
886         mutex_lock(&dvfs_lock);
887
888         list_for_each_entry(rail, &dvfs_rail_list, node) {
889                 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
890                            rail->millivolts + (rail->dfll_mode ? 1 : 0),
891                            rail->dfll_mode ? " dfll mode" :
892                                 rail->disabled ? " disabled" : "");
893                 list_for_each_entry(rel, &rail->relationships_from, from_node) {
894                         seq_printf(s, "   %-10s %-7d mV %-4d mV\n",
895                                 rel->from->reg_id,
896                                 rel->from->millivolts +
897                                    (rel->from->dfll_mode ? 1 : 0),
898                                 dvfs_solve_relationship(rel));
899                 }
900                 seq_printf(s, "   offset     %-7d mV\n", rail->offs_millivolts);
901
902                 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
903
904                 list_for_each_entry(d, &rail->dvfs, reg_node) {
905                         seq_printf(s, "   %-10s %-10lu %-4d mV\n", d->clk_name,
906                                 d->cur_rate, d->cur_millivolts);
907                 }
908         }
909
910         mutex_unlock(&dvfs_lock);
911
912         return 0;
913 }
914
915 static int dvfs_tree_open(struct inode *inode, struct file *file)
916 {
917         return single_open(file, dvfs_tree_show, inode->i_private);
918 }
919
920 static const struct file_operations dvfs_tree_fops = {
921         .open           = dvfs_tree_open,
922         .read           = seq_read,
923         .llseek         = seq_lseek,
924         .release        = single_release,
925 };
926
927 static int rail_stats_show(struct seq_file *s, void *data)
928 {
929         int i;
930         struct dvfs_rail *rail;
931
932         seq_printf(s, "%-12s %-10s\n", "millivolts", "time");
933
934         mutex_lock(&dvfs_lock);
935
936         list_for_each_entry(rail, &dvfs_rail_list, node) {
937                 seq_printf(s, "%s (bin: %d.%dmV)\n", rail->reg_id,
938                            rail->stats.bin_uV / 1000,
939                            (rail->stats.bin_uV / 10) % 100);
940
941                 dvfs_rail_stats_update(rail, -1, ktime_get());
942
943                 seq_printf(s, "%-12d %-10llu\n", 0,
944                         cputime64_to_clock_t(msecs_to_jiffies(
945                                 ktime_to_ms(rail->stats.time_at_mv[0]))));
946
947                 for (i = 1; i <= DVFS_RAIL_STATS_TOP_BIN; i++) {
948                         ktime_t ktime_zero = ktime_set(0, 0);
949                         if (ktime_equal(rail->stats.time_at_mv[i], ktime_zero))
950                                 continue;
951                         seq_printf(s, "%-12d %-10llu\n", rail->min_millivolts +
952                                 (i - 1) * rail->stats.bin_uV / 1000,
953                                 cputime64_to_clock_t(msecs_to_jiffies(
954                                         ktime_to_ms(rail->stats.time_at_mv[i])))
955                         );
956                 }
957         }
958         mutex_unlock(&dvfs_lock);
959         return 0;
960 }
961
962 static int rail_stats_open(struct inode *inode, struct file *file)
963 {
964         return single_open(file, rail_stats_show, inode->i_private);
965 }
966
967 static const struct file_operations rail_stats_fops = {
968         .open           = rail_stats_open,
969         .read           = seq_read,
970         .llseek         = seq_lseek,
971         .release        = single_release,
972 };
973
974 static int cpu_offs_get(void *data, u64 *val)
975 {
976         if (tegra_cpu_rail) {
977                 *val = (u64)tegra_cpu_rail->offs_millivolts;
978                 return 0;
979         }
980         *val = 0;
981         return -ENOENT;
982 }
983 static int cpu_offs_set(void *data, u64 val)
984 {
985         if (tegra_cpu_rail) {
986                 mutex_lock(&dvfs_lock);
987                 tegra_cpu_rail->offs_millivolts = (int)val;
988                 dvfs_rail_update(tegra_cpu_rail);
989                 mutex_unlock(&dvfs_lock);
990                 return 0;
991         }
992         return -ENOENT;
993 }
994 DEFINE_SIMPLE_ATTRIBUTE(cpu_offs_fops, cpu_offs_get, cpu_offs_set, "%lld\n");
995
996 static int core_offs_get(void *data, u64 *val)
997 {
998         if (tegra_core_rail) {
999                 *val = (u64)tegra_core_rail->offs_millivolts;
1000                 return 0;
1001         }
1002         *val = 0;
1003         return -ENOENT;
1004 }
1005 static int core_offs_set(void *data, u64 val)
1006 {
1007         if (tegra_core_rail) {
1008                 mutex_lock(&dvfs_lock);
1009                 tegra_core_rail->offs_millivolts = (int)val;
1010                 dvfs_rail_update(tegra_core_rail);
1011                 mutex_unlock(&dvfs_lock);
1012                 return 0;
1013         }
1014         return -ENOENT;
1015 }
1016 DEFINE_SIMPLE_ATTRIBUTE(core_offs_fops, core_offs_get, core_offs_set, "%lld\n");
1017
1018 int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root)
1019 {
1020         struct dentry *d;
1021
1022         d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL,
1023                 &dvfs_tree_fops);
1024         if (!d)
1025                 return -ENOMEM;
1026
1027         d = debugfs_create_file("rails", S_IRUGO, clk_debugfs_root, NULL,
1028                 &rail_stats_fops);
1029         if (!d)
1030                 return -ENOMEM;
1031
1032         d = debugfs_create_file("vdd_cpu_offs", S_IRUGO | S_IWUSR,
1033                 clk_debugfs_root, NULL, &cpu_offs_fops);
1034         if (!d)
1035                 return -ENOMEM;
1036
1037         d = debugfs_create_file("vdd_core_offs", S_IRUGO | S_IWUSR,
1038                 clk_debugfs_root, NULL, &core_offs_fops);
1039         if (!d)
1040                 return -ENOMEM;
1041
1042         return 0;
1043 }
1044
1045 #endif