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