ARM: tegra: dvfs: Re-arrange dvfs late initialization
[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 inline unsigned long *dvfs_get_freqs(struct dvfs *d)
323 {
324         return d->alt_freqs ? : &d->freqs[0];
325 }
326
327 static int
328 __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
329 {
330         int i = 0;
331         int ret;
332         unsigned long *freqs = dvfs_get_freqs(d);
333
334         if (freqs == NULL || d->millivolts == NULL)
335                 return -ENODEV;
336
337         if (rate > freqs[d->num_freqs - 1]) {
338                 pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate,
339                         d->clk_name);
340                 return -EINVAL;
341         }
342
343         if (rate == 0) {
344                 d->cur_millivolts = 0;
345         } else {
346                 while (i < d->num_freqs && rate > freqs[i])
347                         i++;
348
349                 if ((d->max_millivolts) &&
350                     (d->millivolts[i] > d->max_millivolts)) {
351                         pr_warn("tegra_dvfs: voltage %d too high for dvfs on"
352                                 " %s\n", d->millivolts[i], d->clk_name);
353                         return -EINVAL;
354                 }
355                 d->cur_millivolts = d->millivolts[i];
356         }
357
358         d->cur_rate = rate;
359
360         ret = dvfs_rail_update(d->dvfs_rail);
361         if (ret)
362                 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
363                         d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
364
365         return ret;
366 }
367
368 int tegra_dvfs_alt_freqs_set(struct dvfs *d, unsigned long *alt_freqs)
369 {
370         int ret = 0;
371
372         mutex_lock(&dvfs_lock);
373
374         if (d->alt_freqs != alt_freqs) {
375                 d->alt_freqs = alt_freqs;
376                 ret = __tegra_dvfs_set_rate(d, d->cur_rate);
377         }
378
379         mutex_unlock(&dvfs_lock);
380         return ret;
381 }
382
383 int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate)
384 {
385         int i;
386
387         if (!rate || !c->dvfs)
388                 return 0;
389
390         if (!c->dvfs->millivolts)
391                 return -ENODEV;
392
393         /*
394          * Predicted voltage can not be used across the switch to alternative
395          * frequency limits. For now, just fail the call for clock that has
396          * alternative limits initialized.
397          */
398         if (c->dvfs->alt_freqs)
399                 return -ENOSYS;
400
401         for (i = 0; i < c->dvfs->num_freqs; i++) {
402                 if (rate <= c->dvfs->freqs[i])
403                         break;
404         }
405
406         if (i == c->dvfs->num_freqs)
407                 return -EINVAL;
408
409         return c->dvfs->millivolts[i];
410 }
411
412 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
413 {
414         int ret;
415
416         if (!c->dvfs)
417                 return -EINVAL;
418
419         mutex_lock(&dvfs_lock);
420         ret = __tegra_dvfs_set_rate(c->dvfs, rate);
421         mutex_unlock(&dvfs_lock);
422
423         return ret;
424 }
425 EXPORT_SYMBOL(tegra_dvfs_set_rate);
426
427 /* May only be called during clock init, does not take any locks on clock c. */
428 int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d)
429 {
430         int i;
431
432         if (c->dvfs) {
433                 pr_err("Error when enabling dvfs on %s for clock %s:\n",
434                         d->dvfs_rail->reg_id, c->name);
435                 pr_err("DVFS already enabled for %s\n",
436                         c->dvfs->dvfs_rail->reg_id);
437                 return -EINVAL;
438         }
439
440         for (i = 0; i < MAX_DVFS_FREQS; i++) {
441                 if (d->millivolts[i] == 0)
442                         break;
443
444                 d->freqs[i] *= d->freqs_mult;
445
446                 /* If final frequencies are 0, pad with previous frequency */
447                 if (d->freqs[i] == 0 && i > 1)
448                         d->freqs[i] = d->freqs[i - 1];
449         }
450         d->num_freqs = i;
451
452         if (d->auto_dvfs) {
453                 c->auto_dvfs = true;
454                 clk_set_cansleep(c);
455         }
456
457         c->dvfs = d;
458
459         mutex_lock(&dvfs_lock);
460         list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
461         mutex_unlock(&dvfs_lock);
462
463         return 0;
464 }
465
466 static bool tegra_dvfs_all_rails_suspended(void)
467 {
468         struct dvfs_rail *rail;
469         bool all_suspended = true;
470
471         list_for_each_entry(rail, &dvfs_rail_list, node)
472                 if (!rail->suspended && !rail->disabled)
473                         all_suspended = false;
474
475         return all_suspended;
476 }
477
478 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
479 {
480         struct dvfs_relationship *rel;
481         bool all_suspended = true;
482
483         list_for_each_entry(rel, &to->relationships_from, from_node)
484                 if (!rel->from->suspended && !rel->from->disabled &&
485                         !rel->solved_at_nominal)
486                         all_suspended = false;
487
488         return all_suspended;
489 }
490
491 static int tegra_dvfs_suspend_one(void)
492 {
493         struct dvfs_rail *rail;
494         int ret;
495
496         list_for_each_entry(rail, &dvfs_rail_list, node) {
497                 if (!rail->suspended && !rail->disabled &&
498                     tegra_dvfs_from_rails_suspended_or_solved(rail)) {
499                         ret = dvfs_rail_set_voltage(rail,
500                                 rail->nominal_millivolts);
501                         if (ret)
502                                 return ret;
503                         rail->suspended = true;
504                         return 0;
505                 }
506         }
507
508         return -EINVAL;
509 }
510
511 static void tegra_dvfs_resume(void)
512 {
513         struct dvfs_rail *rail;
514
515         mutex_lock(&dvfs_lock);
516
517         list_for_each_entry(rail, &dvfs_rail_list, node)
518                 rail->suspended = false;
519
520         list_for_each_entry(rail, &dvfs_rail_list, node)
521                 dvfs_rail_update(rail);
522
523         mutex_unlock(&dvfs_lock);
524 }
525
526 static int tegra_dvfs_suspend(void)
527 {
528         int ret = 0;
529
530         mutex_lock(&dvfs_lock);
531
532         while (!tegra_dvfs_all_rails_suspended()) {
533                 ret = tegra_dvfs_suspend_one();
534                 if (ret)
535                         break;
536         }
537
538         mutex_unlock(&dvfs_lock);
539
540         if (ret)
541                 tegra_dvfs_resume();
542
543         return ret;
544 }
545
546 static int tegra_dvfs_pm_notify(struct notifier_block *nb,
547                                 unsigned long event, void *data)
548 {
549         switch (event) {
550         case PM_SUSPEND_PREPARE:
551                 if (tegra_dvfs_suspend())
552                         return NOTIFY_STOP;
553                 break;
554         case PM_POST_SUSPEND:
555                 tegra_dvfs_resume();
556                 break;
557         }
558
559         return NOTIFY_OK;
560 };
561
562 static struct notifier_block tegra_dvfs_nb = {
563         .notifier_call = tegra_dvfs_pm_notify,
564 };
565
566 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
567                                 unsigned long event, void *data)
568 {
569         switch (event) {
570         case SYS_RESTART:
571         case SYS_HALT:
572         case SYS_POWER_OFF:
573                 tegra_dvfs_suspend();
574                 return NOTIFY_OK;
575         }
576         return NOTIFY_DONE;
577 }
578
579 static struct notifier_block tegra_dvfs_reboot_nb = {
580         .notifier_call = tegra_dvfs_reboot_notify,
581 };
582
583 /* must be called with dvfs lock held */
584 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
585 {
586         int ret;
587
588         ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts);
589         if (ret)
590                 pr_info("dvfs: failed to set regulator %s to disable "
591                         "voltage %d\n", rail->reg_id,
592                         rail->nominal_millivolts);
593         rail->disabled = true;
594 }
595
596 /* must be called with dvfs lock held */
597 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
598 {
599         rail->disabled = false;
600         dvfs_rail_update(rail);
601 }
602
603 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
604 {
605         mutex_lock(&rail_disable_lock);
606
607         if (rail->disabled) {
608                 mutex_lock(&dvfs_lock);
609                 __tegra_dvfs_rail_enable(rail);
610                 mutex_unlock(&dvfs_lock);
611
612                 tegra_dvfs_rail_post_enable(rail);
613         }
614         mutex_unlock(&rail_disable_lock);
615
616 }
617
618 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
619 {
620         mutex_lock(&rail_disable_lock);
621         if (rail->disabled)
622                 goto out;
623
624         /* rail disable will set it to nominal voltage underneath clock
625            framework - need to re-configure clock rates that are not safe
626            at nominal (yes, unsafe at nominal is ugly, but possible). Rate
627            change must be done outside of dvfs lock. */
628         if (tegra_dvfs_rail_disable_prepare(rail)) {
629                 pr_info("dvfs: failed to prepare regulator %s to disable\n",
630                         rail->reg_id);
631                 goto out;
632         }
633
634         mutex_lock(&dvfs_lock);
635         __tegra_dvfs_rail_disable(rail);
636         mutex_unlock(&dvfs_lock);
637 out:
638         mutex_unlock(&rail_disable_lock);
639 }
640
641 int tegra_dvfs_rail_disable_by_name(const char *reg_id)
642 {
643         struct dvfs_rail *rail = tegra_dvfs_get_rail_by_name(reg_id);
644         if (!rail)
645                 return -EINVAL;
646
647         tegra_dvfs_rail_disable(rail);
648         return 0;
649 }
650
651 struct dvfs_rail *tegra_dvfs_get_rail_by_name(const char *reg_id)
652 {
653         struct dvfs_rail *rail;
654
655         mutex_lock(&dvfs_lock);
656         list_for_each_entry(rail, &dvfs_rail_list, node) {
657                 if (!strcmp(reg_id, rail->reg_id)) {
658                         mutex_unlock(&dvfs_lock);
659                         return rail;
660                 }
661         }
662         mutex_unlock(&dvfs_lock);
663         return NULL;
664 }
665
666 bool tegra_dvfs_rail_updating(struct clk *clk)
667 {
668         return (!clk ? false :
669                 (!clk->dvfs ? false :
670                  (!clk->dvfs->dvfs_rail ? false :
671                   (clk->dvfs->dvfs_rail->updating))));
672 }
673
674 /*
675  * Iterate through all the dvfs regulators, finding the regulator exported
676  * by the regulator api for each one.  Must be called in late init, after
677  * all the regulator api's regulators are initialized.
678  */
679 int __init tegra_dvfs_late_init(void)
680 {
681         bool connected = true;
682         struct dvfs_rail *rail;
683
684         mutex_lock(&dvfs_lock);
685
686         list_for_each_entry(rail, &dvfs_rail_list, node)
687                 if (dvfs_rail_connect_to_regulator(rail))
688                         connected = false;
689
690         list_for_each_entry(rail, &dvfs_rail_list, node)
691                 if (connected)
692                         dvfs_rail_update(rail);
693                 else
694                         __tegra_dvfs_rail_disable(rail);
695
696         mutex_unlock(&dvfs_lock);
697
698 #ifdef CONFIG_TEGRA_SILICON_PLATFORM
699         if (!connected)
700                 return -ENODEV;
701 #endif
702         register_pm_notifier(&tegra_dvfs_nb);
703         register_reboot_notifier(&tegra_dvfs_reboot_nb);
704
705         return 0;
706 }
707
708 #ifdef CONFIG_DEBUG_FS
709 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
710 {
711         struct dvfs *da = list_entry(a, struct dvfs, reg_node);
712         struct dvfs *db = list_entry(b, struct dvfs, reg_node);
713         int ret;
714
715         ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
716         if (ret != 0)
717                 return ret;
718
719         if (da->cur_millivolts < db->cur_millivolts)
720                 return 1;
721         if (da->cur_millivolts > db->cur_millivolts)
722                 return -1;
723
724         return strcmp(da->clk_name, db->clk_name);
725 }
726
727 static int dvfs_tree_show(struct seq_file *s, void *data)
728 {
729         struct dvfs *d;
730         struct dvfs_rail *rail;
731         struct dvfs_relationship *rel;
732
733         seq_printf(s, "   clock      rate       mV\n");
734         seq_printf(s, "--------------------------------\n");
735
736         mutex_lock(&dvfs_lock);
737
738         list_for_each_entry(rail, &dvfs_rail_list, node) {
739                 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
740                         rail->millivolts, rail->disabled ? " disabled" : "");
741                 list_for_each_entry(rel, &rail->relationships_from, from_node) {
742                         seq_printf(s, "   %-10s %-7d mV %-4d mV\n",
743                                 rel->from->reg_id,
744                                 rel->from->millivolts,
745                                 dvfs_solve_relationship(rel));
746                 }
747
748                 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
749
750                 list_for_each_entry(d, &rail->dvfs, reg_node) {
751                         seq_printf(s, "   %-10s %-10lu %-4d mV\n", d->clk_name,
752                                 d->cur_rate, d->cur_millivolts);
753                 }
754         }
755
756         mutex_unlock(&dvfs_lock);
757
758         return 0;
759 }
760
761 static int dvfs_tree_open(struct inode *inode, struct file *file)
762 {
763         return single_open(file, dvfs_tree_show, inode->i_private);
764 }
765
766 static const struct file_operations dvfs_tree_fops = {
767         .open           = dvfs_tree_open,
768         .read           = seq_read,
769         .llseek         = seq_lseek,
770         .release        = single_release,
771 };
772
773 static int rail_stats_show(struct seq_file *s, void *data)
774 {
775         int i;
776         struct dvfs_rail *rail;
777
778         seq_printf(s, "%-12s %-10s (bin: %d.%dmV)\n", "millivolts", "time",
779                    DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE,
780                    ((DVFS_RAIL_STATS_BIN * 100) / DVFS_RAIL_STATS_SCALE) % 100);
781
782         mutex_lock(&dvfs_lock);
783
784         list_for_each_entry(rail, &dvfs_rail_list, node) {
785                 seq_printf(s, "%s\n", rail->reg_id);
786                 dvfs_rail_stats_update(rail, -1, ktime_get());
787
788                 seq_printf(s, "%-12d %-10llu\n", 0,
789                         cputime64_to_clock_t(msecs_to_jiffies(
790                                 ktime_to_ms(rail->stats.time_at_mv[0]))));
791
792                 for (i = 1; i <= DVFS_RAIL_STATS_TOP_BIN; i++) {
793                         ktime_t ktime_zero = ktime_set(0, 0);
794                         if (ktime_equal(rail->stats.time_at_mv[i], ktime_zero))
795                                 continue;
796                         seq_printf(s, "%-12d %-10llu\n",
797                                    rail->min_millivolts + (i - 1) *
798                                    DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE,
799                                 cputime64_to_clock_t(msecs_to_jiffies(
800                                         ktime_to_ms(rail->stats.time_at_mv[i])))
801                         );
802                 }
803         }
804         mutex_unlock(&dvfs_lock);
805         return 0;
806 }
807
808 static int rail_stats_open(struct inode *inode, struct file *file)
809 {
810         return single_open(file, rail_stats_show, inode->i_private);
811 }
812
813 static const struct file_operations rail_stats_fops = {
814         .open           = rail_stats_open,
815         .read           = seq_read,
816         .llseek         = seq_lseek,
817         .release        = single_release,
818 };
819
820 int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root)
821 {
822         struct dentry *d;
823
824         d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL,
825                 &dvfs_tree_fops);
826         if (!d)
827                 return -ENOMEM;
828
829         d = debugfs_create_file("rails", S_IRUGO, clk_debugfs_root, NULL,
830                 &rail_stats_fops);
831         if (!d)
832                 return -ENOMEM;
833
834         return 0;
835 }
836
837 #endif