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