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