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