ARM: tegra: dvfs: Enable EMC bridge if rail is disabled
[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 static LIST_HEAD(dvfs_rail_list);
42 static DEFINE_MUTEX(dvfs_lock);
43 static DEFINE_MUTEX(rail_disable_lock);
44
45 static int dvfs_rail_update(struct dvfs_rail *rail);
46
47 void tegra_dvfs_add_relationships(struct dvfs_relationship *rels, int n)
48 {
49         int i;
50         struct dvfs_relationship *rel;
51
52         mutex_lock(&dvfs_lock);
53
54         for (i = 0; i < n; i++) {
55                 rel = &rels[i];
56                 list_add_tail(&rel->from_node, &rel->to->relationships_from);
57                 list_add_tail(&rel->to_node, &rel->from->relationships_to);
58         }
59
60         mutex_unlock(&dvfs_lock);
61 }
62
63 int tegra_dvfs_init_rails(struct dvfs_rail *rails[], int n)
64 {
65         int i;
66
67         mutex_lock(&dvfs_lock);
68
69         for (i = 0; i < n; i++) {
70                 INIT_LIST_HEAD(&rails[i]->dvfs);
71                 INIT_LIST_HEAD(&rails[i]->relationships_from);
72                 INIT_LIST_HEAD(&rails[i]->relationships_to);
73                 rails[i]->millivolts = rails[i]->nominal_millivolts;
74                 rails[i]->new_millivolts = rails[i]->nominal_millivolts;
75                 if (!rails[i]->step)
76                         rails[i]->step = rails[i]->max_millivolts;
77
78                 list_add_tail(&rails[i]->node, &dvfs_rail_list);
79         }
80
81         mutex_unlock(&dvfs_lock);
82
83         return 0;
84 };
85
86 static int dvfs_solve_relationship(struct dvfs_relationship *rel)
87 {
88         return rel->solve(rel->from, rel->to);
89 }
90
91 /* Sets the voltage on a dvfs rail to a specific value, and updates any
92  * rails that depend on this rail. */
93 static int dvfs_rail_set_voltage(struct dvfs_rail *rail, int millivolts)
94 {
95         int ret = 0;
96         struct dvfs_relationship *rel;
97         int step = (millivolts > rail->millivolts) ? rail->step : -rail->step;
98         int i;
99         int steps;
100
101         if (!rail->reg) {
102                 if (millivolts == rail->millivolts)
103                         return 0;
104                 else
105                         return -EINVAL;
106         }
107
108         if (rail->disabled)
109                 return 0;
110
111         rail->resolving_to = true;
112         steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
113
114         for (i = 0; i < steps; i++) {
115                 if (abs(millivolts - rail->millivolts) > rail->step)
116                         rail->new_millivolts = rail->millivolts + step;
117                 else
118                         rail->new_millivolts = millivolts;
119
120                 /* Before changing the voltage, tell each rail that depends
121                  * on this rail that the voltage will change.
122                  * This rail will be the "from" rail in the relationship,
123                  * the rail that depends on this rail will be the "to" rail.
124                  * from->millivolts will be the old voltage
125                  * from->new_millivolts will be the new voltage */
126                 list_for_each_entry(rel, &rail->relationships_to, to_node) {
127                         ret = dvfs_rail_update(rel->to);
128                         if (ret)
129                                 goto out;
130                 }
131
132                 if (!rail->disabled) {
133                         rail->updating = true;
134                         ret = regulator_set_voltage(rail->reg,
135                                 rail->new_millivolts * 1000,
136                                 rail->max_millivolts * 1000);
137                         rail->updating = false;
138                 }
139                 if (ret) {
140                         pr_err("Failed to set dvfs regulator %s\n", rail->reg_id);
141                         goto out;
142                 }
143
144                 rail->millivolts = rail->new_millivolts;
145
146                 /* After changing the voltage, tell each rail that depends
147                  * on this rail that the voltage has changed.
148                  * from->millivolts and from->new_millivolts will be the
149                  * new voltage */
150                 list_for_each_entry(rel, &rail->relationships_to, to_node) {
151                         ret = dvfs_rail_update(rel->to);
152                         if (ret)
153                                 goto out;
154                 }
155         }
156
157         if (unlikely(rail->millivolts != millivolts)) {
158                 pr_err("%s: rail didn't reach target %d in %d steps (%d)\n",
159                         __func__, millivolts, steps, rail->millivolts);
160                 ret = -EINVAL;
161         }
162
163 out:
164         rail->resolving_to = false;
165         return ret;
166 }
167
168 /* Determine the minimum valid voltage for a rail, taking into account
169  * the dvfs clocks and any rails that this rail depends on.  Calls
170  * dvfs_rail_set_voltage with the new voltage, which will call
171  * dvfs_rail_update on any rails that depend on this rail. */
172 static int dvfs_rail_update(struct dvfs_rail *rail)
173 {
174         int millivolts = 0;
175         struct dvfs *d;
176         struct dvfs_relationship *rel;
177         int ret = 0;
178         int steps;
179
180         /* if dvfs is suspended, return and handle it during resume */
181         if (rail->suspended)
182                 return 0;
183
184         /* if regulators are not connected yet, return and handle it later */
185         if (!rail->reg)
186                 return 0;
187
188         /* if rail update is entered while resolving circular dependencies,
189            abort recursion */
190         if (rail->resolving_to)
191                 return 0;
192
193         /* Find the maximum voltage requested by any clock */
194         list_for_each_entry(d, &rail->dvfs, reg_node)
195                 millivolts = max(d->cur_millivolts, millivolts);
196
197         /* retry update if limited by from-relationship to account for
198            circular dependencies */
199         steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step);
200         for (; steps >= 0; steps--) {
201                 rail->new_millivolts = millivolts;
202
203                 /* Check any rails that this rail depends on */
204                 list_for_each_entry(rel, &rail->relationships_from, from_node)
205                         rail->new_millivolts = dvfs_solve_relationship(rel);
206
207                 if (rail->new_millivolts == rail->millivolts)
208                         break;
209
210                 ret = dvfs_rail_set_voltage(rail, rail->new_millivolts);
211         }
212
213         return ret;
214 }
215
216 static int dvfs_rail_connect_to_regulator(struct dvfs_rail *rail)
217 {
218         struct regulator *reg;
219         int v;
220
221         if (!rail->reg) {
222                 reg = regulator_get(NULL, rail->reg_id);
223                 if (IS_ERR(reg)) {
224                         pr_err("tegra_dvfs: failed to connect %s rail\n",
225                                rail->reg_id);
226                         return -EINVAL;
227                 }
228                 rail->reg = reg;
229         }
230
231         v = regulator_get_voltage(rail->reg);
232         if (v < 0) {
233                 pr_err("tegra_dvfs: failed initial get %s voltage\n",
234                        rail->reg_id);
235                 return v;
236         }
237         rail->millivolts = v / 1000;
238         rail->new_millivolts = rail->millivolts;
239         return 0;
240 }
241
242 static int
243 __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
244 {
245         int i = 0;
246         int ret;
247
248         if (d->freqs == NULL || d->millivolts == NULL)
249                 return -ENODEV;
250
251         if (rate > d->freqs[d->num_freqs - 1]) {
252                 pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate,
253                         d->clk_name);
254                 return -EINVAL;
255         }
256
257         if (rate == 0) {
258                 d->cur_millivolts = 0;
259         } else {
260                 while (i < d->num_freqs && rate > d->freqs[i])
261                         i++;
262
263                 d->cur_millivolts = d->millivolts[i];
264                 if ((d->max_millivolts) &&
265                     (d->cur_millivolts > d->max_millivolts)) {
266                         pr_warn("tegra_dvfs: voltage %d too high for dvfs on"
267                                 " %s\n", d->cur_millivolts, d->clk_name);
268                         return -EINVAL;
269                 }
270         }
271
272         d->cur_rate = rate;
273
274         ret = dvfs_rail_update(d->dvfs_rail);
275         if (ret)
276                 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
277                         d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
278
279         return ret;
280 }
281
282 int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate)
283 {
284         int i;
285
286         if (!rate || !c->dvfs)
287                 return 0;
288
289         if (!c->dvfs->millivolts)
290                 return -ENODEV;
291
292         for (i = 0; i < c->dvfs->num_freqs; i++) {
293                 if (rate <= c->dvfs->freqs[i])
294                         break;
295         }
296
297         if (i == c->dvfs->num_freqs)
298                 return -EINVAL;
299
300         return c->dvfs->millivolts[i];
301 }
302
303 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
304 {
305         int ret;
306
307         if (!c->dvfs)
308                 return -EINVAL;
309
310         mutex_lock(&dvfs_lock);
311         ret = __tegra_dvfs_set_rate(c->dvfs, rate);
312         mutex_unlock(&dvfs_lock);
313
314         return ret;
315 }
316 EXPORT_SYMBOL(tegra_dvfs_set_rate);
317
318 /* May only be called during clock init, does not take any locks on clock c. */
319 int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d)
320 {
321         int i;
322
323         if (c->dvfs) {
324                 pr_err("Error when enabling dvfs on %s for clock %s:\n",
325                         d->dvfs_rail->reg_id, c->name);
326                 pr_err("DVFS already enabled for %s\n",
327                         c->dvfs->dvfs_rail->reg_id);
328                 return -EINVAL;
329         }
330
331         for (i = 0; i < MAX_DVFS_FREQS; i++) {
332                 if (d->millivolts[i] == 0)
333                         break;
334
335                 d->freqs[i] *= d->freqs_mult;
336
337                 /* If final frequencies are 0, pad with previous frequency */
338                 if (d->freqs[i] == 0 && i > 1)
339                         d->freqs[i] = d->freqs[i - 1];
340         }
341         d->num_freqs = i;
342
343         if (d->auto_dvfs) {
344                 c->auto_dvfs = true;
345                 clk_set_cansleep(c);
346         }
347
348         c->dvfs = d;
349
350         mutex_lock(&dvfs_lock);
351         list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
352         mutex_unlock(&dvfs_lock);
353
354         return 0;
355 }
356
357 static bool tegra_dvfs_all_rails_suspended(void)
358 {
359         struct dvfs_rail *rail;
360         bool all_suspended = true;
361
362         list_for_each_entry(rail, &dvfs_rail_list, node)
363                 if (!rail->suspended && !rail->disabled)
364                         all_suspended = false;
365
366         return all_suspended;
367 }
368
369 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
370 {
371         struct dvfs_relationship *rel;
372         bool all_suspended = true;
373
374         list_for_each_entry(rel, &to->relationships_from, from_node)
375                 if (!rel->from->suspended && !rel->from->disabled &&
376                         !rel->solved_at_nominal)
377                         all_suspended = false;
378
379         return all_suspended;
380 }
381
382 static int tegra_dvfs_suspend_one(void)
383 {
384         struct dvfs_rail *rail;
385         int ret;
386
387         list_for_each_entry(rail, &dvfs_rail_list, node) {
388                 if (!rail->suspended && !rail->disabled &&
389                     tegra_dvfs_from_rails_suspended_or_solved(rail)) {
390                         ret = dvfs_rail_set_voltage(rail,
391                                 rail->nominal_millivolts);
392                         if (ret)
393                                 return ret;
394                         rail->suspended = true;
395                         return 0;
396                 }
397         }
398
399         return -EINVAL;
400 }
401
402 static void tegra_dvfs_resume(void)
403 {
404         struct dvfs_rail *rail;
405
406         mutex_lock(&dvfs_lock);
407
408         list_for_each_entry(rail, &dvfs_rail_list, node)
409                 rail->suspended = false;
410
411         list_for_each_entry(rail, &dvfs_rail_list, node)
412                 dvfs_rail_update(rail);
413
414         mutex_unlock(&dvfs_lock);
415 }
416
417 static int tegra_dvfs_suspend(void)
418 {
419         int ret = 0;
420
421         mutex_lock(&dvfs_lock);
422
423         while (!tegra_dvfs_all_rails_suspended()) {
424                 ret = tegra_dvfs_suspend_one();
425                 if (ret)
426                         break;
427         }
428
429         mutex_unlock(&dvfs_lock);
430
431         if (ret)
432                 tegra_dvfs_resume();
433
434         return ret;
435 }
436
437 static int tegra_dvfs_pm_notify(struct notifier_block *nb,
438                                 unsigned long event, void *data)
439 {
440         switch (event) {
441         case PM_SUSPEND_PREPARE:
442                 if (tegra_dvfs_suspend())
443                         return NOTIFY_STOP;
444                 break;
445         case PM_POST_SUSPEND:
446                 tegra_dvfs_resume();
447                 break;
448         }
449
450         return NOTIFY_OK;
451 };
452
453 static struct notifier_block tegra_dvfs_nb = {
454         .notifier_call = tegra_dvfs_pm_notify,
455 };
456
457 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
458                                 unsigned long event, void *data)
459 {
460         switch (event) {
461         case SYS_RESTART:
462         case SYS_HALT:
463         case SYS_POWER_OFF:
464                 tegra_dvfs_suspend();
465                 return NOTIFY_OK;
466         }
467         return NOTIFY_DONE;
468 }
469
470 static struct notifier_block tegra_dvfs_reboot_nb = {
471         .notifier_call = tegra_dvfs_reboot_notify,
472 };
473
474 /* must be called with dvfs lock held */
475 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
476 {
477         int ret;
478
479         ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts);
480         if (ret)
481                 pr_info("dvfs: failed to set regulator %s to disable "
482                         "voltage %d\n", rail->reg_id,
483                         rail->nominal_millivolts);
484         rail->disabled = true;
485 }
486
487 /* must be called with dvfs lock held */
488 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
489 {
490         rail->disabled = false;
491         dvfs_rail_update(rail);
492 }
493
494 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
495 {
496         mutex_lock(&rail_disable_lock);
497
498         if (rail->disabled) {
499                 mutex_lock(&dvfs_lock);
500                 __tegra_dvfs_rail_enable(rail);
501                 mutex_unlock(&dvfs_lock);
502
503                 tegra_dvfs_rail_post_enable(rail);
504         }
505         mutex_unlock(&rail_disable_lock);
506
507 }
508
509 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
510 {
511         mutex_lock(&rail_disable_lock);
512         if (rail->disabled)
513                 goto out;
514
515         /* rail disable will set it to nominal voltage underneath clock
516            framework - need to re-configure clock rates that are not safe
517            at nominal (yes, unsafe at nominal is ugly, but possible). Rate
518            change must be done outside of dvfs lock. */
519         if (tegra_dvfs_rail_disable_prepare(rail)) {
520                 pr_info("dvfs: failed to prepare regulator %s to disable\n",
521                         rail->reg_id);
522                 goto out;
523         }
524
525         mutex_lock(&dvfs_lock);
526         __tegra_dvfs_rail_disable(rail);
527         mutex_unlock(&dvfs_lock);
528 out:
529         mutex_unlock(&rail_disable_lock);
530 }
531
532 int tegra_dvfs_rail_disable_by_name(const char *reg_id)
533 {
534         struct dvfs_rail *rail = tegra_dvfs_get_rail_by_name(reg_id);
535         if (!rail)
536                 return -EINVAL;
537
538         tegra_dvfs_rail_disable(rail);
539         return 0;
540 }
541
542 struct dvfs_rail *tegra_dvfs_get_rail_by_name(const char *reg_id)
543 {
544         struct dvfs_rail *rail;
545
546         mutex_lock(&dvfs_lock);
547         list_for_each_entry(rail, &dvfs_rail_list, node) {
548                 if (!strcmp(reg_id, rail->reg_id)) {
549                         mutex_unlock(&dvfs_lock);
550                         return rail;
551                 }
552         }
553         mutex_unlock(&dvfs_lock);
554         return NULL;
555 }
556
557 bool tegra_dvfs_rail_updating(struct clk *clk)
558 {
559         return (!clk ? false :
560                 (!clk->dvfs ? false :
561                  (!clk->dvfs->dvfs_rail ? false :
562                   (clk->dvfs->dvfs_rail->updating))));
563 }
564
565 /*
566  * Iterate through all the dvfs regulators, finding the regulator exported
567  * by the regulator api for each one.  Must be called in late init, after
568  * all the regulator api's regulators are initialized.
569  */
570 int __init tegra_dvfs_late_init(void)
571 {
572         bool connected = true;
573         struct dvfs_rail *rail;
574
575         mutex_lock(&dvfs_lock);
576
577         list_for_each_entry(rail, &dvfs_rail_list, node)
578                 if (dvfs_rail_connect_to_regulator(rail))
579                         connected = false;
580
581         list_for_each_entry(rail, &dvfs_rail_list, node)
582                 if (connected)
583                         dvfs_rail_update(rail);
584                 else
585                         __tegra_dvfs_rail_disable(rail);
586
587         mutex_unlock(&dvfs_lock);
588
589         register_pm_notifier(&tegra_dvfs_nb);
590         register_reboot_notifier(&tegra_dvfs_reboot_nb);
591
592         return 0;
593 }
594 late_initcall(tegra_dvfs_late_init);
595
596 #ifdef CONFIG_DEBUG_FS
597 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
598 {
599         struct dvfs *da = list_entry(a, struct dvfs, reg_node);
600         struct dvfs *db = list_entry(b, struct dvfs, reg_node);
601         int ret;
602
603         ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
604         if (ret != 0)
605                 return ret;
606
607         if (da->cur_millivolts < db->cur_millivolts)
608                 return 1;
609         if (da->cur_millivolts > db->cur_millivolts)
610                 return -1;
611
612         return strcmp(da->clk_name, db->clk_name);
613 }
614
615 static int dvfs_tree_show(struct seq_file *s, void *data)
616 {
617         struct dvfs *d;
618         struct dvfs_rail *rail;
619         struct dvfs_relationship *rel;
620
621         seq_printf(s, "   clock      rate       mV\n");
622         seq_printf(s, "--------------------------------\n");
623
624         mutex_lock(&dvfs_lock);
625
626         list_for_each_entry(rail, &dvfs_rail_list, node) {
627                 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
628                         rail->millivolts, rail->disabled ? " disabled" : "");
629                 list_for_each_entry(rel, &rail->relationships_from, from_node) {
630                         seq_printf(s, "   %-10s %-7d mV %-4d mV\n",
631                                 rel->from->reg_id,
632                                 rel->from->millivolts,
633                                 dvfs_solve_relationship(rel));
634                 }
635
636                 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
637
638                 list_for_each_entry(d, &rail->dvfs, reg_node) {
639                         seq_printf(s, "   %-10s %-10lu %-4d mV\n", d->clk_name,
640                                 d->cur_rate, d->cur_millivolts);
641                 }
642         }
643
644         mutex_unlock(&dvfs_lock);
645
646         return 0;
647 }
648
649 static int dvfs_tree_open(struct inode *inode, struct file *file)
650 {
651         return single_open(file, dvfs_tree_show, inode->i_private);
652 }
653
654 static const struct file_operations dvfs_tree_fops = {
655         .open           = dvfs_tree_open,
656         .read           = seq_read,
657         .llseek         = seq_lseek,
658         .release        = single_release,
659 };
660
661 int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root)
662 {
663         struct dentry *d;
664
665         d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL,
666                 &dvfs_tree_fops);
667         if (!d)
668                 return -ENOMEM;
669
670         return 0;
671 }
672
673 #endif