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