ARM: tegra: dvfs: Update dfll mode rate/voltage control
[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     25
42 #define DVFS_RAIL_STATS_SCALE   2
43 #define DVFS_RAIL_STATS_RANGE   ((DVFS_RAIL_STATS_TOP_BIN - 1) * \
44                                  DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE)
45
46 struct dvfs_rail *tegra_cpu_rail;
47 struct dvfs_rail *tegra_core_rail;
48
49 static LIST_HEAD(dvfs_rail_list);
50 static DEFINE_MUTEX(dvfs_lock);
51 static DEFINE_MUTEX(rail_disable_lock);
52
53 static int dvfs_rail_update(struct dvfs_rail *rail);
54
55 void tegra_dvfs_add_relationships(struct dvfs_relationship *rels, int n)
56 {
57         int i;
58         struct dvfs_relationship *rel;
59
60         mutex_lock(&dvfs_lock);
61
62         for (i = 0; i < n; i++) {
63                 rel = &rels[i];
64                 list_add_tail(&rel->from_node, &rel->to->relationships_from);
65                 list_add_tail(&rel->to_node, &rel->from->relationships_to);
66         }
67
68         mutex_unlock(&dvfs_lock);
69 }
70
71 int tegra_dvfs_init_rails(struct dvfs_rail *rails[], int n)
72 {
73         int i;
74
75         mutex_lock(&dvfs_lock);
76
77         for (i = 0; i < n; i++) {
78                 INIT_LIST_HEAD(&rails[i]->dvfs);
79                 INIT_LIST_HEAD(&rails[i]->relationships_from);
80                 INIT_LIST_HEAD(&rails[i]->relationships_to);
81                 rails[i]->millivolts = rails[i]->nominal_millivolts;
82                 rails[i]->new_millivolts = rails[i]->nominal_millivolts;
83                 if (!rails[i]->step)
84                         rails[i]->step = rails[i]->max_millivolts;
85
86                 list_add_tail(&rails[i]->node, &dvfs_rail_list);
87
88                 if (!strcmp("vdd_cpu", rails[i]->reg_id))
89                         tegra_cpu_rail = rails[i];
90                 else if (!strcmp("vdd_core", rails[i]->reg_id))
91                         tegra_core_rail = rails[i];
92         }
93
94         mutex_unlock(&dvfs_lock);
95
96         return 0;
97 };
98
99 static int dvfs_solve_relationship(struct dvfs_relationship *rel)
100 {
101         return rel->solve(rel->from, rel->to);
102 }
103
104 /* rail statistic - called during rail init, or under dfs_lock, or with
105    CPU0 only on-line, and interrupts disabled */
106 static void dvfs_rail_stats_init(struct dvfs_rail *rail, int millivolts)
107 {
108         rail->stats.last_update = ktime_get();
109         if (millivolts >= rail->min_millivolts) {
110                 int i = 1 + (2 * (millivolts - rail->min_millivolts) *
111                         DVFS_RAIL_STATS_SCALE + DVFS_RAIL_STATS_BIN) /
112                         (2 * DVFS_RAIL_STATS_BIN);
113                 rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN);
114         }
115
116         if (rail->max_millivolts >
117             rail->min_millivolts + DVFS_RAIL_STATS_RANGE)
118                 pr_warn("tegra_dvfs: %s: stats above %d mV will be squashed\n",
119                         rail->reg_id,
120                         rail->min_millivolts + DVFS_RAIL_STATS_RANGE);
121 }
122
123 static void dvfs_rail_stats_update(
124         struct dvfs_rail *rail, int millivolts, ktime_t now)
125 {
126         rail->stats.time_at_mv[rail->stats.last_index] = ktime_add(
127                 rail->stats.time_at_mv[rail->stats.last_index], ktime_sub(
128                         now, rail->stats.last_update));
129         rail->stats.last_update = now;
130
131         if (rail->stats.off)
132                 return;
133
134         if (millivolts >= rail->min_millivolts) {
135                 int i = 1 + (2 * (millivolts - rail->min_millivolts) *
136                         DVFS_RAIL_STATS_SCALE + DVFS_RAIL_STATS_BIN) /
137                         (2 * DVFS_RAIL_STATS_BIN);
138                 rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN);
139         } else if (millivolts == 0)
140                         rail->stats.last_index = 0;
141 }
142
143 static void dvfs_rail_stats_pause(struct dvfs_rail *rail,
144                                   ktime_t delta, bool on)
145 {
146         int i = on ? rail->stats.last_index : 0;
147         rail->stats.time_at_mv[i] = ktime_add(rail->stats.time_at_mv[i], delta);
148 }
149
150 void tegra_dvfs_rail_off(struct dvfs_rail *rail, ktime_t now)
151 {
152         if (rail) {
153                 dvfs_rail_stats_update(rail, 0, now);
154                 rail->stats.off = true;
155         }
156 }
157
158 void tegra_dvfs_rail_on(struct dvfs_rail *rail, ktime_t now)
159 {
160         if (rail) {
161                 rail->stats.off = false;
162                 dvfs_rail_stats_update(rail, rail->millivolts, now);
163         }
164 }
165
166 void tegra_dvfs_rail_pause(struct dvfs_rail *rail, ktime_t delta, bool on)
167 {
168         if (rail)
169                 dvfs_rail_stats_pause(rail, delta, on);
170 }
171
172 /* Sets the voltage on a dvfs rail to a specific value, and updates any
173  * rails that depend on this rail. */
174 static int dvfs_rail_set_voltage(struct dvfs_rail *rail, int millivolts)
175 {
176         int ret = 0;
177         struct dvfs_relationship *rel;
178         int step = (millivolts > rail->millivolts) ? rail->step : -rail->step;
179         int i;
180         int steps;
181         bool jmp_to_zero;
182
183         if (!rail->reg) {
184                 if (millivolts == rail->millivolts)
185                         return 0;
186                 else
187                         return -EINVAL;
188         }
189
190         /* DFLL adjusts rail voltage automatically - only update stats */
191         if (rail->dfll_mode) {
192                 rail->millivolts = rail->new_millivolts;
193                 dvfs_rail_stats_update(rail, rail->millivolts, ktime_get());
194                 return 0;
195         }
196
197         if (rail->disabled)
198                 return 0;
199
200         rail->resolving_to = true;
201         jmp_to_zero = rail->jmp_to_zero &&
202                         ((millivolts == 0) || (rail->millivolts == 0));
203         steps = jmp_to_zero ? 1 :
204                 DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
205
206         for (i = 0; i < steps; i++) {
207                 if (!jmp_to_zero &&
208                     (abs(millivolts - rail->millivolts) > rail->step))
209                         rail->new_millivolts = rail->millivolts + step;
210                 else
211                         rail->new_millivolts = millivolts;
212
213                 /* Before changing the voltage, tell each rail that depends
214                  * on this rail that the voltage will change.
215                  * This rail will be the "from" rail in the relationship,
216                  * the rail that depends on this rail will be the "to" rail.
217                  * from->millivolts will be the old voltage
218                  * from->new_millivolts will be the new voltage */
219                 list_for_each_entry(rel, &rail->relationships_to, to_node) {
220                         ret = dvfs_rail_update(rel->to);
221                         if (ret)
222                                 goto out;
223                 }
224
225                 if (!rail->disabled) {
226                         rail->updating = true;
227                         ret = regulator_set_voltage(rail->reg,
228                                 rail->new_millivolts * 1000,
229                                 rail->max_millivolts * 1000);
230                         rail->updating = false;
231                 }
232                 if (ret) {
233                         pr_err("Failed to set dvfs regulator %s\n", rail->reg_id);
234                         goto out;
235                 }
236
237                 rail->millivolts = rail->new_millivolts;
238                 dvfs_rail_stats_update(rail, rail->millivolts, ktime_get());
239
240                 /* After changing the voltage, tell each rail that depends
241                  * on this rail that the voltage has changed.
242                  * from->millivolts and from->new_millivolts will be the
243                  * new voltage */
244                 list_for_each_entry(rel, &rail->relationships_to, to_node) {
245                         ret = dvfs_rail_update(rel->to);
246                         if (ret)
247                                 goto out;
248                 }
249         }
250
251         if (unlikely(rail->millivolts != millivolts)) {
252                 pr_err("%s: rail didn't reach target %d in %d steps (%d)\n",
253                         __func__, millivolts, steps, rail->millivolts);
254                 ret = -EINVAL;
255         }
256
257 out:
258         rail->resolving_to = false;
259         return ret;
260 }
261
262 /* Determine the minimum valid voltage for a rail, taking into account
263  * the dvfs clocks and any rails that this rail depends on.  Calls
264  * dvfs_rail_set_voltage with the new voltage, which will call
265  * dvfs_rail_update on any rails that depend on this rail. */
266 static int dvfs_rail_update(struct dvfs_rail *rail)
267 {
268         int millivolts = 0;
269         struct dvfs *d;
270         struct dvfs_relationship *rel;
271         int ret = 0;
272         int steps;
273
274         /* if dvfs is suspended, return and handle it during resume */
275         if (rail->suspended)
276                 return 0;
277
278         /* if regulators are not connected yet, return and handle it later */
279         if (!rail->reg)
280                 return 0;
281
282         /* if rail update is entered while resolving circular dependencies,
283            abort recursion */
284         if (rail->resolving_to)
285                 return 0;
286
287         /* Find the maximum voltage requested by any clock */
288         list_for_each_entry(d, &rail->dvfs, reg_node)
289                 millivolts = max(d->cur_millivolts, millivolts);
290
291         /* Apply offset if any clock is requesting voltage */
292         if (millivolts) {
293                 millivolts += rail->offs_millivolts;
294                 if (millivolts > rail->max_millivolts)
295                         millivolts = rail->max_millivolts;
296                 else if (millivolts < rail->min_millivolts)
297                         millivolts = rail->min_millivolts;
298         }
299
300         /* retry update if limited by from-relationship to account for
301            circular dependencies */
302         steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
303         for (; steps >= 0; steps--) {
304                 rail->new_millivolts = millivolts;
305
306                 /* Check any rails that this rail depends on */
307                 list_for_each_entry(rel, &rail->relationships_from, from_node)
308                         rail->new_millivolts = dvfs_solve_relationship(rel);
309
310                 if (rail->new_millivolts == rail->millivolts)
311                         break;
312
313                 ret = dvfs_rail_set_voltage(rail, rail->new_millivolts);
314         }
315
316         return ret;
317 }
318
319 static int dvfs_rail_connect_to_regulator(struct dvfs_rail *rail)
320 {
321         struct regulator *reg;
322         int v;
323
324         if (!rail->reg) {
325                 reg = regulator_get(NULL, rail->reg_id);
326                 if (IS_ERR(reg)) {
327                         pr_err("tegra_dvfs: failed to connect %s rail\n",
328                                rail->reg_id);
329                         return -EINVAL;
330                 }
331                 rail->reg = reg;
332         }
333
334         v = regulator_enable(rail->reg);
335         if (v < 0) {
336                 pr_err("tegra_dvfs: failed on enabling regulator %s\n, err %d",
337                         rail->reg_id, v);
338                 return v;
339         }
340
341         v = regulator_get_voltage(rail->reg);
342         if (v < 0) {
343                 pr_err("tegra_dvfs: failed initial get %s voltage\n",
344                        rail->reg_id);
345                 return v;
346         }
347         rail->millivolts = v / 1000;
348         rail->new_millivolts = rail->millivolts;
349         dvfs_rail_stats_init(rail, rail->millivolts);
350         return 0;
351 }
352
353 static inline unsigned long *dvfs_get_freqs(struct dvfs *d)
354 {
355         return d->alt_freqs ? : &d->freqs[0];
356 }
357
358 static inline const int *dvfs_get_millivolts(struct dvfs *d)
359 {
360         if (d->dvfs_rail && d->dvfs_rail->dfll_mode)
361                 return d->dfll_millivolts;
362         return d->millivolts;
363 }
364
365 static int
366 __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
367 {
368         int i = 0;
369         int ret;
370         unsigned long *freqs = dvfs_get_freqs(d);
371         const int *millivolts = dvfs_get_millivolts(d);
372
373         if (freqs == NULL || millivolts == NULL)
374                 return -ENODEV;
375
376         if (rate > freqs[d->num_freqs - 1]) {
377                 pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate,
378                         d->clk_name);
379                 return -EINVAL;
380         }
381
382         if (rate == 0) {
383                 d->cur_millivolts = 0;
384         } else {
385                 while (i < d->num_freqs && rate > freqs[i])
386                         i++;
387
388                 if ((d->max_millivolts) &&
389                     (millivolts[i] > d->max_millivolts)) {
390                         pr_warn("tegra_dvfs: voltage %d too high for dvfs on"
391                                 " %s\n", millivolts[i], d->clk_name);
392                         return -EINVAL;
393                 }
394                 d->cur_millivolts = millivolts[i];
395         }
396
397         d->cur_rate = rate;
398
399         ret = dvfs_rail_update(d->dvfs_rail);
400         if (ret)
401                 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
402                         d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
403
404         return ret;
405 }
406
407 int tegra_dvfs_alt_freqs_set(struct dvfs *d, unsigned long *alt_freqs)
408 {
409         int ret = 0;
410
411         mutex_lock(&dvfs_lock);
412
413         if (d->alt_freqs != alt_freqs) {
414                 d->alt_freqs = alt_freqs;
415                 ret = __tegra_dvfs_set_rate(d, d->cur_rate);
416         }
417
418         mutex_unlock(&dvfs_lock);
419         return ret;
420 }
421
422 int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate)
423 {
424         int i;
425         const int *millivolts;
426
427         if (!rate || !c->dvfs)
428                 return 0;
429
430         millivolts = dvfs_get_millivolts(c->dvfs);
431         if (!millivolts)
432                 return -ENODEV;
433
434         /*
435          * Predicted voltage can not be used across the switch to alternative
436          * frequency limits. For now, just fail the call for clock that has
437          * alternative limits initialized.
438          */
439         if (c->dvfs->alt_freqs)
440                 return -ENOSYS;
441
442         for (i = 0; i < c->dvfs->num_freqs; i++) {
443                 if (rate <= c->dvfs->freqs[i])
444                         break;
445         }
446
447         if (i == c->dvfs->num_freqs)
448                 return -EINVAL;
449
450         return millivolts[i];
451 }
452
453 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
454 {
455         int ret;
456
457         if (!c->dvfs)
458                 return -EINVAL;
459
460         mutex_lock(&dvfs_lock);
461         ret = __tegra_dvfs_set_rate(c->dvfs, rate);
462         mutex_unlock(&dvfs_lock);
463
464         return ret;
465 }
466 EXPORT_SYMBOL(tegra_dvfs_set_rate);
467
468 /* May only be called during clock init, does not take any locks on clock c. */
469 int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d)
470 {
471         int i;
472
473         if (c->dvfs) {
474                 pr_err("Error when enabling dvfs on %s for clock %s:\n",
475                         d->dvfs_rail->reg_id, c->name);
476                 pr_err("DVFS already enabled for %s\n",
477                         c->dvfs->dvfs_rail->reg_id);
478                 return -EINVAL;
479         }
480
481         for (i = 0; i < MAX_DVFS_FREQS; i++) {
482                 if (d->millivolts[i] == 0)
483                         break;
484
485                 d->freqs[i] *= d->freqs_mult;
486
487                 /* If final frequencies are 0, pad with previous frequency */
488                 if (d->freqs[i] == 0 && i > 1)
489                         d->freqs[i] = d->freqs[i - 1];
490         }
491         d->num_freqs = i;
492
493         if (d->auto_dvfs) {
494                 c->auto_dvfs = true;
495                 clk_set_cansleep(c);
496         }
497
498         c->dvfs = d;
499
500         mutex_lock(&dvfs_lock);
501         list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
502         mutex_unlock(&dvfs_lock);
503
504         return 0;
505 }
506
507 static bool tegra_dvfs_all_rails_suspended(void)
508 {
509         struct dvfs_rail *rail;
510         bool all_suspended = true;
511
512         list_for_each_entry(rail, &dvfs_rail_list, node)
513                 if (!rail->suspended && !rail->disabled)
514                         all_suspended = false;
515
516         return all_suspended;
517 }
518
519 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
520 {
521         struct dvfs_relationship *rel;
522         bool all_suspended = true;
523
524         list_for_each_entry(rel, &to->relationships_from, from_node)
525                 if (!rel->from->suspended && !rel->from->disabled &&
526                         !rel->solved_at_nominal)
527                         all_suspended = false;
528
529         return all_suspended;
530 }
531
532 static int tegra_dvfs_suspend_one(void)
533 {
534         struct dvfs_rail *rail;
535         int ret;
536
537         list_for_each_entry(rail, &dvfs_rail_list, node) {
538                 if (!rail->suspended && !rail->disabled &&
539                     tegra_dvfs_from_rails_suspended_or_solved(rail)) {
540                         ret = dvfs_rail_set_voltage(rail,
541                                 rail->nominal_millivolts);
542                         if (ret)
543                                 return ret;
544                         rail->suspended = true;
545                         return 0;
546                 }
547         }
548
549         return -EINVAL;
550 }
551
552 static void tegra_dvfs_resume(void)
553 {
554         struct dvfs_rail *rail;
555
556         mutex_lock(&dvfs_lock);
557
558         list_for_each_entry(rail, &dvfs_rail_list, node)
559                 rail->suspended = false;
560
561         list_for_each_entry(rail, &dvfs_rail_list, node)
562                 dvfs_rail_update(rail);
563
564         mutex_unlock(&dvfs_lock);
565 }
566
567 static int tegra_dvfs_suspend(void)
568 {
569         int ret = 0;
570
571         mutex_lock(&dvfs_lock);
572
573         while (!tegra_dvfs_all_rails_suspended()) {
574                 ret = tegra_dvfs_suspend_one();
575                 if (ret)
576                         break;
577         }
578
579         mutex_unlock(&dvfs_lock);
580
581         if (ret)
582                 tegra_dvfs_resume();
583
584         return ret;
585 }
586
587 static int tegra_dvfs_pm_notify(struct notifier_block *nb,
588                                 unsigned long event, void *data)
589 {
590         switch (event) {
591         case PM_SUSPEND_PREPARE:
592                 if (tegra_dvfs_suspend())
593                         return NOTIFY_STOP;
594                 break;
595         case PM_POST_SUSPEND:
596                 tegra_dvfs_resume();
597                 break;
598         }
599
600         return NOTIFY_OK;
601 };
602
603 static struct notifier_block tegra_dvfs_nb = {
604         .notifier_call = tegra_dvfs_pm_notify,
605 };
606
607 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
608                                 unsigned long event, void *data)
609 {
610         switch (event) {
611         case SYS_RESTART:
612         case SYS_HALT:
613         case SYS_POWER_OFF:
614                 tegra_dvfs_suspend();
615                 return NOTIFY_OK;
616         }
617         return NOTIFY_DONE;
618 }
619
620 static struct notifier_block tegra_dvfs_reboot_nb = {
621         .notifier_call = tegra_dvfs_reboot_notify,
622 };
623
624 /* must be called with dvfs lock held */
625 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
626 {
627         int ret;
628
629         /* don't set voltage in DFLL mode - won't work, but break stats */
630         if (rail->dfll_mode)
631                 return;
632
633         ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts);
634         if (ret)
635                 pr_info("dvfs: failed to set regulator %s to disable "
636                         "voltage %d\n", rail->reg_id,
637                         rail->nominal_millivolts);
638         rail->disabled = true;
639 }
640
641 /* must be called with dvfs lock held */
642 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
643 {
644         rail->disabled = false;
645         dvfs_rail_update(rail);
646 }
647
648 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
649 {
650         mutex_lock(&rail_disable_lock);
651
652         if (rail->disabled) {
653                 mutex_lock(&dvfs_lock);
654                 __tegra_dvfs_rail_enable(rail);
655                 mutex_unlock(&dvfs_lock);
656
657                 tegra_dvfs_rail_post_enable(rail);
658         }
659         mutex_unlock(&rail_disable_lock);
660
661 }
662
663 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
664 {
665         mutex_lock(&rail_disable_lock);
666         if (rail->disabled)
667                 goto out;
668
669         /* rail disable will set it to nominal voltage underneath clock
670            framework - need to re-configure clock rates that are not safe
671            at nominal (yes, unsafe at nominal is ugly, but possible). Rate
672            change must be done outside of dvfs lock. */
673         if (tegra_dvfs_rail_disable_prepare(rail)) {
674                 pr_info("dvfs: failed to prepare regulator %s to disable\n",
675                         rail->reg_id);
676                 goto out;
677         }
678
679         mutex_lock(&dvfs_lock);
680         __tegra_dvfs_rail_disable(rail);
681         mutex_unlock(&dvfs_lock);
682 out:
683         mutex_unlock(&rail_disable_lock);
684 }
685
686 int tegra_dvfs_rail_disable_by_name(const char *reg_id)
687 {
688         struct dvfs_rail *rail = tegra_dvfs_get_rail_by_name(reg_id);
689         if (!rail)
690                 return -EINVAL;
691
692         tegra_dvfs_rail_disable(rail);
693         return 0;
694 }
695
696 struct dvfs_rail *tegra_dvfs_get_rail_by_name(const char *reg_id)
697 {
698         struct dvfs_rail *rail;
699
700         mutex_lock(&dvfs_lock);
701         list_for_each_entry(rail, &dvfs_rail_list, node) {
702                 if (!strcmp(reg_id, rail->reg_id)) {
703                         mutex_unlock(&dvfs_lock);
704                         return rail;
705                 }
706         }
707         mutex_unlock(&dvfs_lock);
708         return NULL;
709 }
710
711 bool tegra_dvfs_rail_updating(struct clk *clk)
712 {
713         return (!clk ? false :
714                 (!clk->dvfs ? false :
715                  (!clk->dvfs->dvfs_rail ? false :
716                   (clk->dvfs->dvfs_rail->updating))));
717 }
718
719 /*
720  * Iterate through all the dvfs regulators, finding the regulator exported
721  * by the regulator api for each one.  Must be called in late init, after
722  * all the regulator api's regulators are initialized.
723  */
724 int __init tegra_dvfs_late_init(void)
725 {
726         bool connected = true;
727         struct dvfs_rail *rail;
728
729         mutex_lock(&dvfs_lock);
730
731         list_for_each_entry(rail, &dvfs_rail_list, node)
732                 if (dvfs_rail_connect_to_regulator(rail))
733                         connected = false;
734
735         list_for_each_entry(rail, &dvfs_rail_list, node)
736                 if (connected)
737                         dvfs_rail_update(rail);
738                 else
739                         __tegra_dvfs_rail_disable(rail);
740
741         mutex_unlock(&dvfs_lock);
742
743 #ifdef CONFIG_TEGRA_SILICON_PLATFORM
744         if (!connected)
745                 return -ENODEV;
746 #endif
747         register_pm_notifier(&tegra_dvfs_nb);
748         register_reboot_notifier(&tegra_dvfs_reboot_nb);
749
750         return 0;
751 }
752
753 #ifdef CONFIG_DEBUG_FS
754 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
755 {
756         struct dvfs *da = list_entry(a, struct dvfs, reg_node);
757         struct dvfs *db = list_entry(b, struct dvfs, reg_node);
758         int ret;
759
760         ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
761         if (ret != 0)
762                 return ret;
763
764         if (da->cur_millivolts < db->cur_millivolts)
765                 return 1;
766         if (da->cur_millivolts > db->cur_millivolts)
767                 return -1;
768
769         return strcmp(da->clk_name, db->clk_name);
770 }
771
772 static int dvfs_tree_show(struct seq_file *s, void *data)
773 {
774         struct dvfs *d;
775         struct dvfs_rail *rail;
776         struct dvfs_relationship *rel;
777
778         seq_printf(s, "   clock      rate       mV\n");
779         seq_printf(s, "--------------------------------\n");
780
781         mutex_lock(&dvfs_lock);
782
783         list_for_each_entry(rail, &dvfs_rail_list, node) {
784                 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
785                         rail->millivolts, rail->disabled ? " disabled" : "");
786                 list_for_each_entry(rel, &rail->relationships_from, from_node) {
787                         seq_printf(s, "   %-10s %-7d mV %-4d mV\n",
788                                 rel->from->reg_id,
789                                 rel->from->millivolts,
790                                 dvfs_solve_relationship(rel));
791                 }
792                 seq_printf(s, "   offset     %-7d mV\n", rail->offs_millivolts);
793
794                 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
795
796                 list_for_each_entry(d, &rail->dvfs, reg_node) {
797                         seq_printf(s, "   %-10s %-10lu %-4d mV\n", d->clk_name,
798                                 d->cur_rate, d->cur_millivolts);
799                 }
800         }
801
802         mutex_unlock(&dvfs_lock);
803
804         return 0;
805 }
806
807 static int dvfs_tree_open(struct inode *inode, struct file *file)
808 {
809         return single_open(file, dvfs_tree_show, inode->i_private);
810 }
811
812 static const struct file_operations dvfs_tree_fops = {
813         .open           = dvfs_tree_open,
814         .read           = seq_read,
815         .llseek         = seq_lseek,
816         .release        = single_release,
817 };
818
819 static int rail_stats_show(struct seq_file *s, void *data)
820 {
821         int i;
822         struct dvfs_rail *rail;
823
824         seq_printf(s, "%-12s %-10s (bin: %d.%dmV)\n", "millivolts", "time",
825                    DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE,
826                    ((DVFS_RAIL_STATS_BIN * 100) / DVFS_RAIL_STATS_SCALE) % 100);
827
828         mutex_lock(&dvfs_lock);
829
830         list_for_each_entry(rail, &dvfs_rail_list, node) {
831                 seq_printf(s, "%s\n", rail->reg_id);
832                 dvfs_rail_stats_update(rail, -1, ktime_get());
833
834                 seq_printf(s, "%-12d %-10llu\n", 0,
835                         cputime64_to_clock_t(msecs_to_jiffies(
836                                 ktime_to_ms(rail->stats.time_at_mv[0]))));
837
838                 for (i = 1; i <= DVFS_RAIL_STATS_TOP_BIN; i++) {
839                         ktime_t ktime_zero = ktime_set(0, 0);
840                         if (ktime_equal(rail->stats.time_at_mv[i], ktime_zero))
841                                 continue;
842                         seq_printf(s, "%-12d %-10llu\n",
843                                    rail->min_millivolts + (i - 1) *
844                                    DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE,
845                                 cputime64_to_clock_t(msecs_to_jiffies(
846                                         ktime_to_ms(rail->stats.time_at_mv[i])))
847                         );
848                 }
849         }
850         mutex_unlock(&dvfs_lock);
851         return 0;
852 }
853
854 static int rail_stats_open(struct inode *inode, struct file *file)
855 {
856         return single_open(file, rail_stats_show, inode->i_private);
857 }
858
859 static const struct file_operations rail_stats_fops = {
860         .open           = rail_stats_open,
861         .read           = seq_read,
862         .llseek         = seq_lseek,
863         .release        = single_release,
864 };
865
866 static int cpu_offs_get(void *data, u64 *val)
867 {
868         if (tegra_cpu_rail) {
869                 *val = (u64)tegra_cpu_rail->offs_millivolts;
870                 return 0;
871         }
872         *val = 0;
873         return -ENOENT;
874 }
875 static int cpu_offs_set(void *data, u64 val)
876 {
877         if (tegra_cpu_rail) {
878                 mutex_lock(&dvfs_lock);
879                 tegra_cpu_rail->offs_millivolts = (int)val;
880                 dvfs_rail_update(tegra_cpu_rail);
881                 mutex_unlock(&dvfs_lock);
882                 return 0;
883         }
884         return -ENOENT;
885 }
886 DEFINE_SIMPLE_ATTRIBUTE(cpu_offs_fops, cpu_offs_get, cpu_offs_set, "%lld\n");
887
888 static int core_offs_get(void *data, u64 *val)
889 {
890         if (tegra_core_rail) {
891                 *val = (u64)tegra_core_rail->offs_millivolts;
892                 return 0;
893         }
894         *val = 0;
895         return -ENOENT;
896 }
897 static int core_offs_set(void *data, u64 val)
898 {
899         if (tegra_core_rail) {
900                 mutex_lock(&dvfs_lock);
901                 tegra_core_rail->offs_millivolts = (int)val;
902                 dvfs_rail_update(tegra_core_rail);
903                 mutex_unlock(&dvfs_lock);
904                 return 0;
905         }
906         return -ENOENT;
907 }
908 DEFINE_SIMPLE_ATTRIBUTE(core_offs_fops, core_offs_get, core_offs_set, "%lld\n");
909
910 int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root)
911 {
912         struct dentry *d;
913
914         d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL,
915                 &dvfs_tree_fops);
916         if (!d)
917                 return -ENOMEM;
918
919         d = debugfs_create_file("rails", S_IRUGO, clk_debugfs_root, NULL,
920                 &rail_stats_fops);
921         if (!d)
922                 return -ENOMEM;
923
924         d = debugfs_create_file("vdd_cpu_offs", S_IRUGO | S_IWUSR,
925                 clk_debugfs_root, NULL, &cpu_offs_fops);
926         if (!d)
927                 return -ENOMEM;
928
929         d = debugfs_create_file("vdd_core_offs", S_IRUGO | S_IWUSR,
930                 clk_debugfs_root, NULL, &core_offs_fops);
931         if (!d)
932                 return -ENOMEM;
933
934         return 0;
935 }
936
937 #endif