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