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