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