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