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