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