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