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