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