bbf43ae9bc400d29f9be9b4a92f43ad7f91919a8
[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         int v;
211
212         if (!rail->reg) {
213                 reg = regulator_get(NULL, rail->reg_id);
214                 if (IS_ERR(reg))
215                         return -EINVAL;
216                 rail->reg = reg;
217         }
218
219         v = regulator_get_voltage(rail->reg);
220         if (v < 0) {
221                 pr_err("tegra_dvfs: failed initial get %s voltage\n",
222                        rail->reg_id);
223                 return v;
224         }
225         rail->millivolts = v / 1000;
226         rail->new_millivolts = rail->millivolts;
227         return 0;
228 }
229
230 static int
231 __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate)
232 {
233         int i = 0;
234         int ret;
235
236         if (d->freqs == NULL || d->millivolts == NULL)
237                 return -ENODEV;
238
239         if (rate > d->freqs[d->num_freqs - 1]) {
240                 pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate,
241                         d->clk_name);
242                 return -EINVAL;
243         }
244
245         if (rate == 0) {
246                 d->cur_millivolts = 0;
247         } else {
248                 while (i < d->num_freqs && rate > d->freqs[i])
249                         i++;
250
251                 d->cur_millivolts = d->millivolts[i];
252                 if ((d->max_millivolts) &&
253                     (d->cur_millivolts > d->max_millivolts)) {
254                         pr_warn("tegra_dvfs: voltage %d too high for dvfs on"
255                                 " %s\n", d->cur_millivolts, d->clk_name);
256                         return -EINVAL;
257                 }
258         }
259
260         d->cur_rate = rate;
261
262         ret = dvfs_rail_update(d->dvfs_rail);
263         if (ret)
264                 pr_err("Failed to set regulator %s for clock %s to %d mV\n",
265                         d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts);
266
267         return ret;
268 }
269
270 int tegra_dvfs_set_rate(struct clk *c, unsigned long rate)
271 {
272         int ret;
273
274         if (!c->dvfs)
275                 return -EINVAL;
276
277         mutex_lock(&dvfs_lock);
278         ret = __tegra_dvfs_set_rate(c->dvfs, rate);
279         mutex_unlock(&dvfs_lock);
280
281         return ret;
282 }
283 EXPORT_SYMBOL(tegra_dvfs_set_rate);
284
285 /* May only be called during clock init, does not take any locks on clock c. */
286 int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d)
287 {
288         int i;
289
290         if (c->dvfs) {
291                 pr_err("Error when enabling dvfs on %s for clock %s:\n",
292                         d->dvfs_rail->reg_id, c->name);
293                 pr_err("DVFS already enabled for %s\n",
294                         c->dvfs->dvfs_rail->reg_id);
295                 return -EINVAL;
296         }
297
298         for (i = 0; i < MAX_DVFS_FREQS; i++) {
299                 if (d->millivolts[i] == 0)
300                         break;
301
302                 d->freqs[i] *= d->freqs_mult;
303
304                 /* If final frequencies are 0, pad with previous frequency */
305                 if (d->freqs[i] == 0 && i > 1)
306                         d->freqs[i] = d->freqs[i - 1];
307         }
308         d->num_freqs = i;
309
310         if (d->auto_dvfs) {
311                 c->auto_dvfs = true;
312                 clk_set_cansleep(c);
313         }
314
315         c->dvfs = d;
316
317         mutex_lock(&dvfs_lock);
318         list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs);
319         mutex_unlock(&dvfs_lock);
320
321         return 0;
322 }
323
324 static bool tegra_dvfs_all_rails_suspended(void)
325 {
326         struct dvfs_rail *rail;
327         bool all_suspended = true;
328
329         list_for_each_entry(rail, &dvfs_rail_list, node)
330                 if (!rail->suspended && !rail->disabled)
331                         all_suspended = false;
332
333         return all_suspended;
334 }
335
336 static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to)
337 {
338         struct dvfs_relationship *rel;
339         bool all_suspended = true;
340
341         list_for_each_entry(rel, &to->relationships_from, from_node)
342                 if (!rel->from->suspended && !rel->from->disabled &&
343                         !rel->solved_at_nominal)
344                         all_suspended = false;
345
346         return all_suspended;
347 }
348
349 static int tegra_dvfs_suspend_one(void)
350 {
351         struct dvfs_rail *rail;
352         int ret;
353
354         list_for_each_entry(rail, &dvfs_rail_list, node) {
355                 if (!rail->suspended && !rail->disabled &&
356                     tegra_dvfs_from_rails_suspended_or_solved(rail)) {
357                         ret = dvfs_rail_set_voltage(rail,
358                                 rail->nominal_millivolts);
359                         if (ret)
360                                 return ret;
361                         rail->suspended = true;
362                         return 0;
363                 }
364         }
365
366         return -EINVAL;
367 }
368
369 static void tegra_dvfs_resume(void)
370 {
371         struct dvfs_rail *rail;
372
373         mutex_lock(&dvfs_lock);
374
375         list_for_each_entry(rail, &dvfs_rail_list, node)
376                 rail->suspended = false;
377
378         list_for_each_entry(rail, &dvfs_rail_list, node)
379                 dvfs_rail_update(rail);
380
381         mutex_unlock(&dvfs_lock);
382 }
383
384 static int tegra_dvfs_suspend(void)
385 {
386         int ret = 0;
387
388         mutex_lock(&dvfs_lock);
389
390         while (!tegra_dvfs_all_rails_suspended()) {
391                 ret = tegra_dvfs_suspend_one();
392                 if (ret)
393                         break;
394         }
395
396         mutex_unlock(&dvfs_lock);
397
398         if (ret)
399                 tegra_dvfs_resume();
400
401         return ret;
402 }
403
404 static int tegra_dvfs_pm_notify(struct notifier_block *nb,
405                                 unsigned long event, void *data)
406 {
407         switch (event) {
408         case PM_SUSPEND_PREPARE:
409                 if (tegra_dvfs_suspend())
410                         return NOTIFY_STOP;
411                 break;
412         case PM_POST_SUSPEND:
413                 tegra_dvfs_resume();
414                 break;
415         }
416
417         return NOTIFY_OK;
418 };
419
420 static struct notifier_block tegra_dvfs_nb = {
421         .notifier_call = tegra_dvfs_pm_notify,
422 };
423
424 static int tegra_dvfs_reboot_notify(struct notifier_block *nb,
425                                 unsigned long event, void *data)
426 {
427         switch (event) {
428         case SYS_RESTART:
429         case SYS_HALT:
430         case SYS_POWER_OFF:
431                 tegra_dvfs_suspend();
432                 return NOTIFY_OK;
433         }
434         return NOTIFY_DONE;
435 }
436
437 static struct notifier_block tegra_dvfs_reboot_nb = {
438         .notifier_call = tegra_dvfs_reboot_notify,
439 };
440
441 /* must be called with dvfs lock held */
442 static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail)
443 {
444         int ret;
445
446         if (!rail->disabled) {
447                 ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts);
448                 if (ret)
449                         pr_info("dvfs: failed to set regulator %s to disable "
450                                 "voltage %d\n", rail->reg_id,
451                                 rail->nominal_millivolts);
452                 rail->disabled = true;
453         }
454 }
455
456 /* must be called with dvfs lock held */
457 static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail)
458 {
459         if (rail->disabled) {
460                 rail->disabled = false;
461                 dvfs_rail_update(rail);
462         }
463 }
464
465 void tegra_dvfs_rail_enable(struct dvfs_rail *rail)
466 {
467         mutex_lock(&dvfs_lock);
468         __tegra_dvfs_rail_enable(rail);
469         mutex_unlock(&dvfs_lock);
470 }
471
472 void tegra_dvfs_rail_disable(struct dvfs_rail *rail)
473 {
474         mutex_lock(&dvfs_lock);
475         __tegra_dvfs_rail_disable(rail);
476         mutex_unlock(&dvfs_lock);
477 }
478
479 int tegra_dvfs_rail_disable_by_name(const char *reg_id)
480 {
481         struct dvfs_rail *rail;
482         int ret = 0;
483
484         mutex_lock(&dvfs_lock);
485         list_for_each_entry(rail, &dvfs_rail_list, node) {
486                 if (!strcmp(reg_id, rail->reg_id)) {
487                         __tegra_dvfs_rail_disable(rail);
488                         goto out;
489                 }
490         }
491
492         ret = -EINVAL;
493
494 out:
495         mutex_unlock(&dvfs_lock);
496         return ret;
497 }
498
499 bool tegra_dvfs_rail_updating(struct clk *clk)
500 {
501         return (!clk ? false :
502                 (!clk->dvfs ? false :
503                  (!clk->dvfs->dvfs_rail ? false :
504                   (clk->dvfs->dvfs_rail->updating))));
505 }
506
507 /*
508  * Iterate through all the dvfs regulators, finding the regulator exported
509  * by the regulator api for each one.  Must be called in late init, after
510  * all the regulator api's regulators are initialized.
511  */
512 int __init tegra_dvfs_late_init(void)
513 {
514         struct dvfs_rail *rail;
515
516         mutex_lock(&dvfs_lock);
517
518         list_for_each_entry(rail, &dvfs_rail_list, node)
519                 dvfs_rail_connect_to_regulator(rail);
520
521         list_for_each_entry(rail, &dvfs_rail_list, node)
522                 dvfs_rail_update(rail);
523
524         mutex_unlock(&dvfs_lock);
525
526         register_pm_notifier(&tegra_dvfs_nb);
527         register_reboot_notifier(&tegra_dvfs_reboot_nb);
528
529         return 0;
530 }
531 late_initcall(tegra_dvfs_late_init);
532
533 #ifdef CONFIG_DEBUG_FS
534 static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b)
535 {
536         struct dvfs *da = list_entry(a, struct dvfs, reg_node);
537         struct dvfs *db = list_entry(b, struct dvfs, reg_node);
538         int ret;
539
540         ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id);
541         if (ret != 0)
542                 return ret;
543
544         if (da->cur_millivolts < db->cur_millivolts)
545                 return 1;
546         if (da->cur_millivolts > db->cur_millivolts)
547                 return -1;
548
549         return strcmp(da->clk_name, db->clk_name);
550 }
551
552 static int dvfs_tree_show(struct seq_file *s, void *data)
553 {
554         struct dvfs *d;
555         struct dvfs_rail *rail;
556         struct dvfs_relationship *rel;
557
558         seq_printf(s, "   clock      rate       mV\n");
559         seq_printf(s, "--------------------------------\n");
560
561         mutex_lock(&dvfs_lock);
562
563         list_for_each_entry(rail, &dvfs_rail_list, node) {
564                 seq_printf(s, "%s %d mV%s:\n", rail->reg_id,
565                         rail->millivolts, rail->disabled ? " disabled" : "");
566                 list_for_each_entry(rel, &rail->relationships_from, from_node) {
567                         seq_printf(s, "   %-10s %-7d mV %-4d mV\n",
568                                 rel->from->reg_id,
569                                 rel->from->millivolts,
570                                 dvfs_solve_relationship(rel));
571                 }
572
573                 list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp);
574
575                 list_for_each_entry(d, &rail->dvfs, reg_node) {
576                         seq_printf(s, "   %-10s %-10lu %-4d mV\n", d->clk_name,
577                                 d->cur_rate, d->cur_millivolts);
578                 }
579         }
580
581         mutex_unlock(&dvfs_lock);
582
583         return 0;
584 }
585
586 static int dvfs_tree_open(struct inode *inode, struct file *file)
587 {
588         return single_open(file, dvfs_tree_show, inode->i_private);
589 }
590
591 static const struct file_operations dvfs_tree_fops = {
592         .open           = dvfs_tree_open,
593         .read           = seq_read,
594         .llseek         = seq_lseek,
595         .release        = single_release,
596 };
597
598 int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root)
599 {
600         struct dentry *d;
601
602         d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL,
603                 &dvfs_tree_fops);
604         if (!d)
605                 return -ENOMEM;
606
607         return 0;
608 }
609
610 #endif