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