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