]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - arch/arm/mach-tegra/clock.c
ARM: tegra: clock: Make TWD optional
[linux-3.10.git] / arch / arm / mach-tegra / clock.c
1 /*
2  *
3  * Copyright (C) 2010 Google, Inc.
4  * Copyright (c) 2012 NVIDIA CORPORATION.  All rights reserved.
5  *
6  * Author:
7  *      Colin Cross <ccross@google.com>
8  *
9  * Copyright (C) 2010-2012 NVIDIA Corporation
10  *
11  * This software is licensed under the terms of the GNU General Public
12  * License version 2, as published by the Free Software Foundation, and
13  * may be copied, distributed, and modified under those terms.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/clk.h>
24 #include <linux/clkdev.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/clk/tegra.h>
33 #include <linux/uaccess.h>
34 #include <trace/events/power.h>
35
36 #include "board.h"
37 #include "clock.h"
38 #include "dvfs.h"
39 #include "timer.h"
40
41 /* Global data of Tegra CPU CAR ops */
42 struct tegra_cpu_car_ops *tegra_cpu_car_ops;
43
44 #define DISABLE_BOOT_CLOCKS 1
45
46 /*
47  * Locking:
48  *
49  * Each struct clk has a lock.  Depending on the cansleep flag, that lock
50  * may be a spinlock or a mutex.  For most clocks, the spinlock is sufficient,
51  * and using the spinlock allows the clock to be manipulated from an interrupt
52  * or while holding a spinlock.  Some clocks may need to adjust a regulator
53  * in order to maintain the required voltage for a new frequency.  Those
54  * clocks set the cansleep flag, and take a mutex so that the regulator api
55  * can be used while holding the lock.
56  *
57  * To avoid AB-BA locking problems, locks must always be traversed from child
58  * clock to parent clock.  For example, when enabling a clock, the clock's lock
59  * is taken, and then clk_enable is called on the parent, which take's the
60  * parent clock's lock.  There are two exceptions to this ordering:
61  *  1. When setting a clock as cansleep, in which case the entire list of clocks
62  *     is traversed to set the children as cansleep as well.  This must occur
63  *     during init, before any calls to clk_get, so no other clock locks can
64  *     get taken.
65  *  2. When dumping the clock tree through debugfs.  In this case, clk_lock_all
66  *     is called, which attemps to iterate through the entire list of clocks
67  *     and take every clock lock.  If any call to clk_trylock fails, a locked
68  *     clocks are unlocked, and the process is retried.  When all the locks
69  *     are held, the only clock operation that can be called is
70  *     clk_get_rate_all_locked.
71  *
72  * Within a single clock, no clock operation can call another clock operation
73  * on itself, except for clk_xxx_locked.  Any clock operation can call any other
74  * clock operation on any of it's possible parents.
75  *
76  * clk_set_cansleep is used to mark a clock as sleeping.  It is called during
77  * dvfs (Dynamic Voltage and Frequency Scaling) init on any clock that has a
78  * dvfs requirement, and propagated to all possible children of sleeping clock.
79  *
80  * An additional mutex, clock_list_lock, is used to protect the list of all
81  * clocks.
82  *
83  * The clock operations must lock internally to protect against
84  * read-modify-write on registers that are shared by multiple clocks
85  */
86
87 /* FIXME: remove and never ignore overclock */
88 #define IGNORE_PARENT_OVERCLOCK 0
89
90 static DEFINE_MUTEX(clock_list_lock);
91 static LIST_HEAD(clocks);
92
93 #ifndef CONFIG_COMMON_CLK
94 struct clk *tegra_get_clock_by_name(const char *name)
95 {
96         struct clk *c;
97         struct clk *ret = NULL;
98         mutex_lock(&clock_list_lock);
99         list_for_each_entry(c, &clocks, node) {
100                 if (strcmp(c->name, name) == 0) {
101                         ret = c;
102                         break;
103                 }
104         }
105         mutex_unlock(&clock_list_lock);
106         return ret;
107 }
108 EXPORT_SYMBOL(tegra_get_clock_by_name);
109
110 static void clk_stats_update(struct clk *c)
111 {
112         u64 cur_jiffies = get_jiffies_64();
113
114         if (c->refcnt) {
115                 c->stats.time_on = c->stats.time_on +
116                         (jiffies64_to_cputime64(cur_jiffies) -
117                          (c->stats.last_update));
118         }
119
120         c->stats.last_update = cur_jiffies;
121 }
122
123 /* Must be called with clk_lock(c) held */
124 static unsigned long clk_predict_rate_from_parent(struct clk *c, struct clk *p)
125 {
126         u64 rate;
127
128         rate = clk_get_rate(p);
129
130         if (c->mul != 0 && c->div != 0) {
131                 rate *= c->mul;
132                 rate += c->div - 1; /* round up */
133                 do_div(rate, c->div);
134         }
135
136         return rate;
137 }
138
139 unsigned long clk_get_max_rate(struct clk *c)
140 {
141                 return c->max_rate;
142 }
143
144 unsigned long clk_get_min_rate(struct clk *c)
145 {
146                 return c->min_rate;
147 }
148
149 /* Must be called with clk_lock(c) held */
150 unsigned long clk_get_rate_locked(struct clk *c)
151 {
152         unsigned long rate;
153
154         if (c->parent)
155                 rate = clk_predict_rate_from_parent(c, c->parent);
156         else
157                 rate = c->rate;
158
159         return rate;
160 }
161
162 unsigned long clk_get_rate(struct clk *c)
163 {
164         unsigned long flags;
165         unsigned long rate;
166
167         clk_lock_save(c, &flags);
168
169         rate = clk_get_rate_locked(c);
170
171         clk_unlock_restore(c, &flags);
172
173         return rate;
174 }
175 EXPORT_SYMBOL(clk_get_rate);
176
177 static void __clk_set_cansleep(struct clk *c)
178 {
179         struct clk *child;
180         int i;
181         BUG_ON(mutex_is_locked(&c->mutex));
182         BUG_ON(spin_is_locked(&c->spinlock));
183
184         /* Make sure that all possible descendants of sleeping clock are
185            marked as sleeping (to eliminate "sleeping parent - non-sleeping
186            child" relationship */
187         list_for_each_entry(child, &clocks, node) {
188                 bool possible_parent = (child->parent == c);
189
190                 if (!possible_parent && child->inputs) {
191                         for (i = 0; child->inputs[i].input; i++) {
192                                 if ((child->inputs[i].input == c) &&
193                                     tegra_clk_is_parent_allowed(child, c)) {
194                                         possible_parent = true;
195                                         break;
196                                 }
197                         }
198                 }
199
200                 if (possible_parent)
201                         __clk_set_cansleep(child);
202         }
203
204         c->cansleep = true;
205 }
206
207 /* Must be called before any clk_get calls */
208 void clk_set_cansleep(struct clk *c)
209 {
210
211         mutex_lock(&clock_list_lock);
212         __clk_set_cansleep(c);
213         mutex_unlock(&clock_list_lock);
214 }
215
216 int clk_reparent(struct clk *c, struct clk *parent)
217 {
218         c->parent = parent;
219         return 0;
220 }
221
222 void clk_init(struct clk *c)
223 {
224         clk_lock_init(c);
225
226         if (c->ops && c->ops->init)
227                 c->ops->init(c);
228
229         if (!c->ops || !c->ops->enable) {
230                 c->refcnt++;
231                 c->set = true;
232                 if (c->parent)
233                         c->state = c->parent->state;
234                 else
235                         c->state = ON;
236         }
237         c->stats.last_update = get_jiffies_64();
238
239         mutex_lock(&clock_list_lock);
240         list_add(&c->node, &clocks);
241         mutex_unlock(&clock_list_lock);
242 }
243
244 static int clk_enable_locked(struct clk *c)
245 {
246         int ret = 0;
247
248         if (clk_is_auto_dvfs(c)) {
249                 ret = tegra_dvfs_set_rate(c, clk_get_rate_locked(c));
250                 if (ret)
251                         return ret;
252         }
253
254         if (c->refcnt == 0) {
255                 if (c->parent) {
256                         ret = clk_enable(c->parent);
257                         if (ret)
258                                 return ret;
259                 }
260
261                 if (c->ops && c->ops->enable) {
262                         ret = c->ops->enable(c);
263                         trace_clock_enable(c->name, 1, 0);
264                         if (ret) {
265                                 if (c->parent)
266                                         clk_disable(c->parent);
267                                 return ret;
268                         }
269                         c->state = ON;
270                         c->set = true;
271                 }
272                 clk_stats_update(c);
273         }
274         c->refcnt++;
275
276         return ret;
277 }
278
279
280 int clk_enable(struct clk *c)
281 {
282         int ret = 0;
283         unsigned long flags;
284
285         clk_lock_save(c, &flags);
286         ret = clk_enable_locked(c);
287         clk_unlock_restore(c, &flags);
288         return ret;
289 }
290 EXPORT_SYMBOL(clk_enable);
291
292 static void clk_disable_locked(struct clk *c)
293 {
294         if (c->refcnt == 0) {
295                 WARN(1, "Attempting to disable clock %s with refcnt 0", c->name);
296                 return;
297         }
298         if (c->refcnt == 1) {
299                 if (c->ops && c->ops->disable) {
300                         trace_clock_disable(c->name, 0, 0);
301                         c->ops->disable(c);
302                 }
303                 if (c->parent)
304                         clk_disable(c->parent);
305
306                 c->state = OFF;
307                 clk_stats_update(c);
308         }
309         c->refcnt--;
310
311         if (clk_is_auto_dvfs(c) && c->refcnt == 0)
312                 tegra_dvfs_set_rate(c, 0);
313 }
314
315 void clk_disable(struct clk *c)
316 {
317         unsigned long flags;
318
319         clk_lock_save(c, &flags);
320         clk_disable_locked(c);
321         clk_unlock_restore(c, &flags);
322 }
323 EXPORT_SYMBOL(clk_disable);
324
325 static int clk_rate_change_notify(struct clk *c, unsigned long rate)
326 {
327         if (!c->rate_change_nh)
328                 return -ENOSYS;
329         return raw_notifier_call_chain(c->rate_change_nh, rate, NULL);
330 }
331
332 int clk_set_parent_locked(struct clk *c, struct clk *parent)
333 {
334         int ret = 0;
335         unsigned long new_rate;
336         unsigned long old_rate;
337         bool disable = false;
338
339         if (!c->ops || !c->ops->set_parent) {
340                 ret = -ENOSYS;
341                 goto out;
342         }
343
344         if (!tegra_clk_is_parent_allowed(c, parent)) {
345                 ret = -EINVAL;
346                 goto out;
347         }
348
349         new_rate = clk_predict_rate_from_parent(c, parent);
350         old_rate = clk_get_rate_locked(c);
351
352         if (new_rate > clk_get_max_rate(c)) {
353
354                 pr_err("Failed to set parent %s for %s (violates clock limit"
355                        " %lu)\n", parent->name, c->name, clk_get_max_rate(c));
356 #if !IGNORE_PARENT_OVERCLOCK
357                 ret = -EINVAL;
358                 goto out;
359 #endif
360         }
361
362         /* The new clock control register setting does not take effect if
363          * clock is disabled. Later, when the clock is enabled it would run
364          * for several cycles on the old parent, which may hang h/w if the
365          * parent is already disabled. To guarantee h/w switch to the new
366          * setting enable clock while setting parent.
367          */
368         if ((c->refcnt == 0) && (c->flags & MUX)) {
369                 pr_debug("Setting parent of clock %s with refcnt 0\n", c->name);
370                 ret = clk_enable_locked(c);
371                 if (ret)
372                         goto out;
373                 disable = true;
374         }
375
376         if (clk_is_auto_dvfs(c) && c->refcnt > 0 &&
377                         (!c->parent || new_rate > old_rate)) {
378                 ret = tegra_dvfs_set_rate(c, new_rate);
379                 if (ret)
380                         goto out;
381         }
382
383         ret = c->ops->set_parent(c, parent);
384         if (ret)
385                 goto out;
386
387         if (clk_is_auto_dvfs(c) && c->refcnt > 0 &&
388                         new_rate < old_rate)
389                 ret = tegra_dvfs_set_rate(c, new_rate);
390
391         if (new_rate != old_rate)
392                 clk_rate_change_notify(c, new_rate);
393
394 out:
395         if (disable)
396                 clk_disable_locked(c);
397         return ret;
398 }
399
400
401 int clk_set_parent(struct clk *c, struct clk *parent)
402 {
403         int ret = 0;
404         unsigned long flags;
405
406         clk_lock_save(c, &flags);
407         ret = clk_set_parent_locked(c, parent);
408         clk_unlock_restore(c, &flags);
409
410         return ret;
411 }
412 EXPORT_SYMBOL(clk_set_parent);
413
414 struct clk *clk_get_parent(struct clk *c)
415 {
416         return c->parent;
417 }
418 EXPORT_SYMBOL(clk_get_parent);
419
420 int clk_set_rate_locked(struct clk *c, unsigned long rate)
421 {
422         int ret = 0;
423         unsigned long old_rate, max_rate;
424         long new_rate;
425         bool disable = false;
426
427         old_rate = clk_get_rate_locked(c);
428
429         max_rate = clk_get_max_rate(c);
430         if (rate > max_rate)
431                 rate = max_rate;
432
433         if (c->ops && c->ops->round_rate) {
434                 new_rate = c->ops->round_rate(c, rate);
435
436                 if (new_rate < 0) {
437                         ret = new_rate;
438                         return ret;
439                 }
440
441                 rate = new_rate;
442         }
443
444         /* The new clock control register setting does not take effect if
445          * clock is disabled. Later, when the clock is enabled it would run
446          * for several cycles on the old rate, which may over-clock module
447          * at given voltage. To guarantee h/w switch to the new setting
448          * enable clock while setting rate.
449          */
450         if ((c->refcnt == 0) && (c->flags & (DIV_U71 | DIV_U16)) &&
451                 clk_is_auto_dvfs(c)) {
452                 pr_debug("Setting rate of clock %s with refcnt 0\n", c->name);
453                 ret = clk_enable_locked(c);
454                 if (ret)
455                         goto out;
456                 disable = true;
457         }
458
459         if (clk_is_auto_dvfs(c) && rate > old_rate && c->refcnt > 0) {
460                 ret = tegra_dvfs_set_rate(c, rate);
461                 if (ret)
462                         goto out;
463         }
464
465         trace_clock_set_rate(c->name, rate, 0);
466         ret = c->ops->set_rate(c, rate);
467         if (ret)
468                 goto out;
469
470         if (clk_is_auto_dvfs(c) && rate < old_rate && c->refcnt > 0)
471                 ret = tegra_dvfs_set_rate(c, rate);
472
473         if (rate != old_rate)
474                 clk_rate_change_notify(c, rate);
475
476 out:
477         if (disable)
478                 clk_disable_locked(c);
479         return ret;
480 }
481
482 int clk_set_rate(struct clk *c, unsigned long rate)
483 {
484         unsigned long flags;
485         int ret;
486
487         if (!c->ops || !c->ops->set_rate)
488                 return -ENOSYS;
489
490         clk_lock_save(c, &flags);
491
492         ret = clk_set_rate_locked(c, rate);
493
494         clk_unlock_restore(c, &flags);
495
496         return ret;
497 }
498 EXPORT_SYMBOL(clk_set_rate);
499
500 /* Must be called with clocks lock and all indvidual clock locks held */
501 unsigned long clk_get_rate_all_locked(struct clk *c)
502 {
503         u64 rate;
504         int mul = 1;
505         int div = 1;
506         struct clk *p = c;
507
508         while (p) {
509                 c = p;
510                 if (c->mul != 0 && c->div != 0) {
511                         mul *= c->mul;
512                         div *= c->div;
513                 }
514                 p = c->parent;
515         }
516
517         rate = c->rate;
518         rate *= mul;
519         do_div(rate, div);
520
521         return rate;
522 }
523
524 long clk_round_rate_locked(struct clk *c, unsigned long rate)
525 {
526         unsigned long max_rate;
527         long ret;
528
529         if (!c->ops || !c->ops->round_rate) {
530                 ret = -ENOSYS;
531                 goto out;
532         }
533
534         max_rate = clk_get_max_rate(c);
535         if (rate > max_rate)
536                 rate = max_rate;
537
538         ret = c->ops->round_rate(c, rate);
539
540 out:
541         return ret;
542 }
543
544 long clk_round_rate(struct clk *c, unsigned long rate)
545 {
546         unsigned long flags;
547         long ret;
548
549         clk_lock_save(c, &flags);
550         ret = clk_round_rate_locked(c, rate);
551         clk_unlock_restore(c, &flags);
552         return ret;
553 }
554 EXPORT_SYMBOL(clk_round_rate);
555
556 static int tegra_clk_clip_rate_for_parent(struct clk *c, struct clk *p)
557 {
558         unsigned long flags, max_rate, old_rate, new_rate;
559
560         clk_lock_save(c, &flags);
561
562         max_rate = clk_get_max_rate(c);
563         new_rate = clk_predict_rate_from_parent(c, p);
564         old_rate = clk_get_rate_locked(c);
565
566         clk_unlock_restore(c, &flags);
567
568         if (new_rate > max_rate) {
569                 u64 rate = max_rate;
570                 rate *= old_rate;
571                 do_div(rate, new_rate);
572
573                 return clk_set_rate(c, (unsigned long)rate);
574         }
575         return 0;
576 }
577
578 static int tegra_clk_init_one_from_table(struct tegra_clk_init_table *table)
579 {
580         struct clk *c;
581         struct clk *p;
582
583         int ret = 0;
584
585         c = tegra_get_clock_by_name(table->name);
586
587         if (!c) {
588                 pr_warning("Unable to initialize clock %s\n",
589                         table->name);
590                 return -ENODEV;
591         }
592
593         if (table->parent) {
594                 p = tegra_get_clock_by_name(table->parent);
595                 if (!p) {
596                         pr_warning("Unable to find parent %s of clock %s\n",
597                                 table->parent, table->name);
598                         return -ENODEV;
599                 }
600
601                 if (c->parent != p) {
602                         ret = tegra_clk_clip_rate_for_parent(c, p);
603                         if (ret) {
604                                 pr_warning("Unable to clip rate for parent %s"
605                                            " of clock %s: %d\n",
606                                            table->parent, table->name, ret);
607                                 return -EINVAL;
608                         }
609
610                         ret = clk_set_parent(c, p);
611                         if (ret) {
612                                 pr_warning("Unable to set parent %s of clock %s: %d\n",
613                                         table->parent, table->name, ret);
614                                 return -EINVAL;
615                         }
616                 }
617         }
618
619         if (table->rate && table->rate != clk_get_rate(c)) {
620                 ret = clk_set_rate(c, table->rate);
621                 if (ret) {
622                         pr_warning("Unable to set clock %s to rate %lu: %d\n",
623                                 table->name, table->rate, ret);
624                         return -EINVAL;
625                 }
626         }
627
628         if (table->enabled) {
629                 ret = clk_enable(c);
630                 if (ret) {
631                         pr_warning("Unable to enable clock %s: %d\n",
632                                 table->name, ret);
633                         return -EINVAL;
634                 }
635         }
636
637         return 0;
638 }
639
640 void tegra_clk_init_from_table(struct tegra_clk_init_table *table)
641 {
642         for (; table->name; table++)
643                 tegra_clk_init_one_from_table(table);
644 }
645 EXPORT_SYMBOL(tegra_clk_init_from_table);
646
647 void tegra_periph_reset_deassert(struct clk *c)
648 {
649         BUG_ON(!c->ops->reset);
650         c->ops->reset(c, false);
651 }
652 EXPORT_SYMBOL(tegra_periph_reset_deassert);
653
654 void tegra_periph_reset_assert(struct clk *c)
655 {
656         BUG_ON(!c->ops->reset);
657         c->ops->reset(c, true);
658 }
659 EXPORT_SYMBOL(tegra_periph_reset_assert);
660
661 int tegra_is_clk_enabled(struct clk *c)
662 {
663         return c->refcnt;
664 }
665 EXPORT_SYMBOL(tegra_is_clk_enabled);
666
667 int tegra_clk_shared_bus_update(struct clk *c)
668 {
669         int ret = 0;
670         unsigned long flags;
671
672         clk_lock_save(c, &flags);
673
674         if (c->ops && c->ops->shared_bus_update)
675                 ret = c->ops->shared_bus_update(c);
676
677         clk_unlock_restore(c, &flags);
678         return ret;
679 }
680
681 /* dvfs initialization may lower default maximum rate */
682 void __init tegra_init_max_rate(struct clk *c, unsigned long max_rate)
683 {
684         struct clk *shared_bus_user;
685
686         if (c->max_rate <= max_rate)
687                 return;
688
689         pr_warning("Lowering %s maximum rate from %lu to %lu\n",
690                 c->name, c->max_rate, max_rate);
691
692         c->max_rate = max_rate;
693         list_for_each_entry(shared_bus_user,
694                             &c->shared_bus_list, u.shared_bus_user.node) {
695                 shared_bus_user->u.shared_bus_user.rate = max_rate;
696                 shared_bus_user->max_rate = max_rate;
697         }
698 }
699
700 void __init tegra_common_init_clock(void)
701 {
702         tegra_init_early_timer();
703 }
704
705 static bool tegra_keep_boot_clocks = false;
706 static int __init tegra_keep_boot_clocks_setup(char *__unused)
707 {
708         tegra_keep_boot_clocks = true;
709         return 1;
710 }
711 __setup("tegra_keep_boot_clocks", tegra_keep_boot_clocks_setup);
712
713 /*
714  * Bootloader may not match kernel restrictions on CPU clock sources.
715  * Make sure CPU clock is sourced from either main or backup parent.
716  */
717 static int tegra_sync_cpu_clock(void)
718 {
719         int ret;
720         unsigned long rate;
721         struct clk *c = tegra_get_clock_by_name("cpu");
722
723         BUG_ON(!c);
724         rate = clk_get_rate(c);
725         ret = clk_set_rate(c, rate);
726         if (ret)
727                 pr_err("%s: Failed to sync CPU at rate %lu\n", __func__, rate);
728         else
729                 pr_info("CPU rate: %lu MHz\n", clk_get_rate(c) / 1000000);
730         return ret;
731 }
732 late_initcall(tegra_sync_cpu_clock);
733
734 /*
735  * Iterate through all clocks, disabling any for which the refcount is 0
736  * but the clock init detected the bootloader left the clock on.
737  */
738 static int __init tegra_init_disable_boot_clocks(void)
739 {
740 #if DISABLE_BOOT_CLOCKS
741         unsigned long flags;
742         struct clk *c;
743
744         mutex_lock(&clock_list_lock);
745
746         list_for_each_entry(c, &clocks, node) {
747                 clk_lock_save(c, &flags);
748                 if (c->refcnt == 0 && c->state == ON &&
749                                 c->ops && c->ops->disable) {
750                         pr_warn_once("%s clocks left on by bootloader:\n",
751                                 tegra_keep_boot_clocks ?
752                                         "Prevented disabling" :
753                                         "Disabling");
754
755                         pr_warn("   %s\n", c->name);
756
757                         if (!tegra_keep_boot_clocks) {
758                                 c->ops->disable(c);
759                                 c->state = OFF;
760                         }
761                 }
762                 clk_unlock_restore(c, &flags);
763         }
764
765         mutex_unlock(&clock_list_lock);
766 #endif
767         return 0;
768 }
769 late_initcall(tegra_init_disable_boot_clocks);
770
771 /* Several extended clock configuration bits (e.g., clock routing, clock
772  * phase control) are included in PLL and peripheral clock source
773  * registers. */
774 int tegra_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
775 {
776         int ret = 0;
777         unsigned long flags;
778
779         spin_lock_irqsave(&c->spinlock, flags);
780
781         if (!c->ops || !c->ops->clk_cfg_ex) {
782                 ret = -ENOSYS;
783                 goto out;
784         }
785         ret = c->ops->clk_cfg_ex(c, p, setting);
786
787 out:
788         spin_unlock_irqrestore(&c->spinlock, flags);
789         return ret;
790 }
791
792 int tegra_register_clk_rate_notifier(struct clk *c, struct notifier_block *nb)
793 {
794         int ret;
795         unsigned long flags;
796
797         if (!c->rate_change_nh)
798                 return -ENOSYS;
799
800         clk_lock_save(c, &flags);
801         ret = raw_notifier_chain_register(c->rate_change_nh, nb);
802         clk_unlock_restore(c, &flags);
803         return ret;
804 }
805
806 void tegra_unregister_clk_rate_notifier(
807         struct clk *c, struct notifier_block *nb)
808 {
809         unsigned long flags;
810
811         if (!c->rate_change_nh)
812                 return;
813
814         clk_lock_save(c, &flags);
815         raw_notifier_chain_unregister(c->rate_change_nh, nb);
816         clk_unlock_restore(c, &flags);
817 }
818
819 #ifdef CONFIG_DEBUG_FS
820
821 /*
822  * Attempt to lock all the clocks that are marked cansleep
823  * Must be called with irqs enabled
824  */
825 static int __clk_lock_all_mutexes(void)
826 {
827         struct clk *c;
828
829         might_sleep();
830
831         list_for_each_entry(c, &clocks, node)
832                 if (clk_cansleep(c))
833                         if (!mutex_trylock(&c->mutex))
834                                 goto unlock_mutexes;
835
836         return 0;
837
838 unlock_mutexes:
839         list_for_each_entry_continue_reverse(c, &clocks, node)
840                 if (clk_cansleep(c))
841                         mutex_unlock(&c->mutex);
842
843         return -EAGAIN;
844 }
845
846 /*
847  * Attempt to lock all the clocks that are not marked cansleep
848  * Must be called with irqs disabled
849  */
850 static int __clk_lock_all_spinlocks(void)
851 {
852         struct clk *c;
853
854         list_for_each_entry(c, &clocks, node)
855                 if (!clk_cansleep(c))
856                         if (!spin_trylock(&c->spinlock))
857                                 goto unlock_spinlocks;
858
859         return 0;
860
861 unlock_spinlocks:
862         list_for_each_entry_continue_reverse(c, &clocks, node)
863                 if (!clk_cansleep(c))
864                         spin_unlock(&c->spinlock);
865
866         return -EAGAIN;
867 }
868
869 static void __clk_unlock_all_mutexes(void)
870 {
871         struct clk *c;
872
873         list_for_each_entry_reverse(c, &clocks, node)
874                 if (clk_cansleep(c))
875                         mutex_unlock(&c->mutex);
876 }
877
878 static void __clk_unlock_all_spinlocks(void)
879 {
880         struct clk *c;
881
882         list_for_each_entry_reverse(c, &clocks, node)
883                 if (!clk_cansleep(c))
884                         spin_unlock(&c->spinlock);
885 }
886
887 /*
888  * This function retries until it can take all locks, and may take
889  * an arbitrarily long time to complete.
890  * Must be called with irqs enabled, returns with irqs disabled
891  * Must be called with clock_list_lock held
892  */
893 static void clk_lock_all(void)
894 {
895         int ret;
896 retry:
897         ret = __clk_lock_all_mutexes();
898         if (ret)
899                 goto failed_mutexes;
900
901         local_irq_disable();
902
903         ret = __clk_lock_all_spinlocks();
904         if (ret)
905                 goto failed_spinlocks;
906
907         /* All locks taken successfully, return */
908         return;
909
910 failed_spinlocks:
911         local_irq_enable();
912         __clk_unlock_all_mutexes();
913 failed_mutexes:
914         msleep(1);
915         goto retry;
916 }
917
918 /*
919  * Unlocks all clocks after a clk_lock_all
920  * Must be called with irqs disabled, returns with irqs enabled
921  * Must be called with clock_list_lock held
922  */
923 static void clk_unlock_all(void)
924 {
925         __clk_unlock_all_spinlocks();
926
927         local_irq_enable();
928
929         __clk_unlock_all_mutexes();
930 }
931
932 static struct dentry *clk_debugfs_root;
933
934 static void dvfs_show_one(struct seq_file *s, struct dvfs *d, int level)
935 {
936         seq_printf(s, "%*s  %-*s%21s%d mV\n",
937                         level * 3 + 1, "",
938                         30 - level * 3, d->dvfs_rail->reg_id,
939                         "",
940                         d->cur_millivolts);
941 }
942
943 static void clock_tree_show_one(struct seq_file *s, struct clk *c, int level)
944 {
945         struct clk *child;
946         const char *state = "uninit";
947         char div[8] = {0};
948         unsigned long rate = clk_get_rate_all_locked(c);
949         unsigned long max_rate = clk_get_max_rate(c);;
950
951         if (c->state == ON)
952                 state = "on";
953         else if (c->state == OFF)
954                 state = "off";
955
956         if (c->mul != 0 && c->div != 0) {
957                 if (c->mul > c->div) {
958                         int mul = c->mul / c->div;
959                         int mul2 = (c->mul * 10 / c->div) % 10;
960                         int mul3 = (c->mul * 10) % c->div;
961                         if (mul2 == 0 && mul3 == 0)
962                                 snprintf(div, sizeof(div), "x%d", mul);
963                         else if (mul3 == 0)
964                                 snprintf(div, sizeof(div), "x%d.%d", mul, mul2);
965                         else
966                                 snprintf(div, sizeof(div), "x%d.%d..", mul, mul2);
967                 } else {
968                         snprintf(div, sizeof(div), "%d%s", c->div / c->mul,
969                                 (c->div % c->mul) ? ".5" : "");
970                 }
971         }
972
973         seq_printf(s, "%*s%c%c%-*s%c %-6s %-3d %-8s %-10lu",
974                 level * 3 + 1, "",
975                 rate > max_rate ? '!' : ' ',
976                 !c->set ? '*' : ' ',
977                 30 - level * 3, c->name,
978                 c->cansleep ? '$' : ' ',
979                 state, c->refcnt, div, rate);
980         if (c->parent && !list_empty(&c->parent->shared_bus_list))
981                 seq_printf(s, " (%lu)", c->u.shared_bus_user.rate);
982         seq_printf(s, "\n");
983
984         if (c->dvfs)
985                 dvfs_show_one(s, c->dvfs, level + 1);
986
987         list_for_each_entry(child, &clocks, node) {
988                 if (child->parent != c)
989                         continue;
990
991                 clock_tree_show_one(s, child, level + 1);
992         }
993 }
994
995 static int clock_tree_show(struct seq_file *s, void *data)
996 {
997         struct clk *c;
998         seq_printf(s, "   clock                          state  ref div      rate       (shared rate)\n");
999         seq_printf(s, "------------------------------------------------------------------------------\n");
1000
1001         mutex_lock(&clock_list_lock);
1002
1003         clk_lock_all();
1004
1005         list_for_each_entry(c, &clocks, node)
1006                 if (c->parent == NULL)
1007                         clock_tree_show_one(s, c, 0);
1008
1009         clk_unlock_all();
1010
1011         mutex_unlock(&clock_list_lock);
1012         return 0;
1013 }
1014
1015 static int clock_tree_open(struct inode *inode, struct file *file)
1016 {
1017         return single_open(file, clock_tree_show, inode->i_private);
1018 }
1019
1020 static const struct file_operations clock_tree_fops = {
1021         .open           = clock_tree_open,
1022         .read           = seq_read,
1023         .llseek         = seq_lseek,
1024         .release        = single_release,
1025 };
1026
1027 static void syncevent_one(struct clk *c)
1028 {
1029         struct clk *child;
1030
1031         if (c->state == ON)
1032                 trace_clock_enable(c->name, 1, smp_processor_id());
1033         else
1034                 trace_clock_disable(c->name, 0, smp_processor_id());
1035
1036         trace_clock_set_rate(c->name, clk_get_rate_all_locked(c),
1037                                 smp_processor_id());
1038
1039         list_for_each_entry(child, &clocks, node) {
1040                 if (child->parent != c)
1041                         continue;
1042
1043                 syncevent_one(child);
1044         }
1045 }
1046
1047 static int syncevent_write(struct file *file, const char __user *user_buf,
1048                                 size_t count, loff_t *ppos)
1049 {
1050         struct clk *c;
1051         char buffer[40];
1052         int buf_size;
1053
1054         memset(buffer, 0, sizeof(buffer));
1055         buf_size = min(count, (sizeof(buffer)-1));
1056
1057         if (copy_from_user(buffer, user_buf, buf_size))
1058                 return -EFAULT;
1059
1060         if (!strnicmp("all", buffer, 3)) {
1061                 mutex_lock(&clock_list_lock);
1062
1063                 clk_lock_all();
1064
1065                 list_for_each_entry(c, &clocks, node) {
1066                         if (c->parent == NULL)
1067                                 syncevent_one(c);
1068                 }
1069
1070                 clk_unlock_all();
1071
1072                 mutex_unlock(&clock_list_lock);
1073         }
1074
1075         return count;
1076 }
1077
1078 static const struct file_operations syncevent_fops = {
1079         .write          = syncevent_write,
1080 };
1081
1082 static int possible_parents_show(struct seq_file *s, void *data)
1083 {
1084         struct clk *c = s->private;
1085         int i;
1086
1087         for (i = 0; c->inputs[i].input; i++) {
1088                 char *first = (i == 0) ? "" : " ";
1089                 seq_printf(s, "%s%s", first, c->inputs[i].input->name);
1090         }
1091         seq_printf(s, "\n");
1092         return 0;
1093 }
1094
1095 static int possible_parents_open(struct inode *inode, struct file *file)
1096 {
1097         return single_open(file, possible_parents_show, inode->i_private);
1098 }
1099
1100 static const struct file_operations possible_parents_fops = {
1101         .open           = possible_parents_open,
1102         .read           = seq_read,
1103         .llseek         = seq_lseek,
1104         .release        = single_release,
1105 };
1106
1107 static int parent_show(struct seq_file *s, void *data)
1108 {
1109         struct clk *c = s->private;
1110         struct clk *p = clk_get_parent(c);
1111
1112         seq_printf(s, "%s\n", p ? p->name : "clk_root");
1113         return 0;
1114 }
1115
1116 static int parent_open(struct inode *inode, struct file *file)
1117 {
1118         return single_open(file, parent_show, inode->i_private);
1119 }
1120
1121 static int rate_get(void *data, u64 *val)
1122 {
1123         struct clk *c = (struct clk *)data;
1124         *val = (u64)clk_get_rate(c);
1125         return 0;
1126 }
1127
1128 static int state_get(void *data, u64 *val)
1129 {
1130         struct clk *c = (struct clk *)data;
1131         *val = (u64)((c->state == ON) ? 1 : 0);
1132         return 0;
1133 }
1134
1135 #ifdef CONFIG_TEGRA_CLOCK_DEBUG_WRITE
1136
1137 static const mode_t parent_rate_mode =  S_IRUGO | S_IWUSR;
1138
1139 static ssize_t parent_write(struct file *file,
1140         const char __user *userbuf, size_t count, loff_t *ppos)
1141 {
1142         struct seq_file *s = file->private_data;
1143         struct clk *c = s->private;
1144         struct clk *p = NULL;
1145         char buf[32];
1146
1147         if (sizeof(buf) <= count)
1148                 return -EINVAL;
1149
1150         if (copy_from_user(buf, userbuf, count))
1151                 return -EFAULT;
1152
1153         /* terminate buffer and trim - white spaces may be appended
1154          *  at the end when invoked from shell command line */
1155         buf[count]='\0';
1156         strim(buf);
1157
1158         p = tegra_get_clock_by_name(buf);
1159         if (!p)
1160                 return -EINVAL;
1161
1162         if (clk_set_parent(c, p))
1163                 return -EINVAL;
1164
1165         return count;
1166 }
1167
1168 static const struct file_operations parent_fops = {
1169         .open           = parent_open,
1170         .read           = seq_read,
1171         .write          = parent_write,
1172         .llseek         = seq_lseek,
1173         .release        = single_release,
1174 };
1175
1176 static int rate_set(void *data, u64 val)
1177 {
1178         struct clk *c = (struct clk *)data;
1179         return clk_set_rate(c, (unsigned long)val);
1180 }
1181 DEFINE_SIMPLE_ATTRIBUTE(rate_fops, rate_get, rate_set, "%llu\n");
1182
1183 static int state_set(void *data, u64 val)
1184 {
1185         struct clk *c = (struct clk *)data;
1186
1187         if (val)
1188                 return clk_enable(c);
1189         else {
1190                 clk_disable(c);
1191                 return 0;
1192         }
1193 }
1194 DEFINE_SIMPLE_ATTRIBUTE(state_fops, state_get, state_set, "%llu\n");
1195
1196 #else
1197
1198 static const mode_t parent_rate_mode =  S_IRUGO;
1199
1200 static const struct file_operations parent_fops = {
1201         .open           = parent_open,
1202         .read           = seq_read,
1203         .llseek         = seq_lseek,
1204         .release        = single_release,
1205 };
1206
1207 DEFINE_SIMPLE_ATTRIBUTE(rate_fops, rate_get, NULL, "%llu\n");
1208 DEFINE_SIMPLE_ATTRIBUTE(state_fops, state_get, NULL, "%llu\n");
1209 #endif
1210
1211 static int time_on_get(void *data, u64 *val)
1212 {
1213         unsigned long flags;
1214         struct clk *c = (struct clk *)data;
1215
1216         clk_lock_save(c, &flags);
1217         clk_stats_update(c);
1218         *val = cputime64_to_clock_t(c->stats.time_on);
1219         clk_unlock_restore(c, &flags);
1220
1221         return 0;
1222 }
1223 DEFINE_SIMPLE_ATTRIBUTE(time_on_fops, time_on_get, NULL, "%llu\n");
1224
1225 static int possible_rates_show(struct seq_file *s, void *data)
1226 {
1227         struct clk *c = s->private;
1228         long rate = 0;
1229
1230         /* shared bus clock must round up, unless top of range reached */
1231         while (rate <= c->max_rate) {
1232                 long rounded_rate = c->ops->round_rate(c, rate);
1233                 if (IS_ERR_VALUE(rounded_rate) || (rounded_rate <= rate))
1234                         break;
1235
1236                 rate = rounded_rate + 2000;     /* 2kHz resolution */
1237                 seq_printf(s, "%ld ", rounded_rate / 1000);
1238         }
1239         seq_printf(s, "(kHz)\n");
1240         return 0;
1241 }
1242
1243 static int possible_rates_open(struct inode *inode, struct file *file)
1244 {
1245         return single_open(file, possible_rates_show, inode->i_private);
1246 }
1247
1248 static const struct file_operations possible_rates_fops = {
1249         .open           = possible_rates_open,
1250         .read           = seq_read,
1251         .llseek         = seq_lseek,
1252         .release        = single_release,
1253 };
1254
1255 static int clk_debugfs_register_one(struct clk *c)
1256 {
1257         struct dentry *d;
1258
1259         d = debugfs_create_dir(c->name, clk_debugfs_root);
1260         if (!d)
1261                 return -ENOMEM;
1262         c->dent = d;
1263
1264         d = debugfs_create_u8("refcnt", S_IRUGO, c->dent, (u8 *)&c->refcnt);
1265         if (!d)
1266                 goto err_out;
1267
1268         d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
1269         if (!d)
1270                 goto err_out;
1271
1272         d = debugfs_create_u32("max", S_IRUGO, c->dent, (u32 *)&c->max_rate);
1273         if (!d)
1274                 goto err_out;
1275
1276         d = debugfs_create_u32("min", S_IRUGO, c->dent, (u32 *)&c->min_rate);
1277         if (!d)
1278                 goto err_out;
1279
1280         d = debugfs_create_file(
1281                 "parent", parent_rate_mode, c->dent, c, &parent_fops);
1282         if (!d)
1283                 goto err_out;
1284
1285         d = debugfs_create_file(
1286                 "rate", parent_rate_mode, c->dent, c, &rate_fops);
1287         if (!d)
1288                 goto err_out;
1289
1290         d = debugfs_create_file(
1291                 "state", parent_rate_mode, c->dent, c, &state_fops);
1292         if (!d)
1293                 goto err_out;
1294
1295         d = debugfs_create_file(
1296                 "time_on", S_IRUGO, c->dent, c, &time_on_fops);
1297         if (!d)
1298                 goto err_out;
1299
1300         if (c->inputs) {
1301                 d = debugfs_create_file("possible_parents", S_IRUGO, c->dent,
1302                         c, &possible_parents_fops);
1303                 if (!d)
1304                         goto err_out;
1305         }
1306
1307         if (c->ops && c->ops->round_rate && c->ops->shared_bus_update) {
1308                 d = debugfs_create_file("possible_rates", S_IRUGO, c->dent,
1309                         c, &possible_rates_fops);
1310                 if (!d)
1311                         goto err_out;
1312         }
1313
1314         return 0;
1315
1316 err_out:
1317         debugfs_remove_recursive(c->dent);
1318         return -ENOMEM;
1319 }
1320
1321 static int clk_debugfs_register(struct clk *c)
1322 {
1323         int err;
1324         struct clk *pa = c->parent;
1325
1326         if (pa && !pa->dent) {
1327                 err = clk_debugfs_register(pa);
1328                 if (err)
1329                         return err;
1330         }
1331
1332         if (!c->dent) {
1333                 err = clk_debugfs_register_one(c);
1334                 if (err)
1335                         return err;
1336         }
1337         return 0;
1338 }
1339
1340 int __init tegra_clk_debugfs_init(void)
1341 {
1342         struct clk *c;
1343         struct dentry *d;
1344         int err = -ENOMEM;
1345
1346         d = debugfs_create_dir("clock", NULL);
1347         if (!d)
1348                 return -ENOMEM;
1349         clk_debugfs_root = d;
1350
1351         d = debugfs_create_file("clock_tree", S_IRUGO, clk_debugfs_root, NULL,
1352                 &clock_tree_fops);
1353         if (!d)
1354                 goto err_out;
1355
1356         d = debugfs_create_file("syncevents", S_IRUGO|S_IWUSR, clk_debugfs_root, NULL,
1357                 &syncevent_fops);
1358
1359         if (dvfs_debugfs_init(clk_debugfs_root))
1360                 goto err_out;
1361
1362         list_for_each_entry(c, &clocks, node) {
1363                 err = clk_debugfs_register(c);
1364                 if (err)
1365                         goto err_out;
1366         }
1367         return 0;
1368 err_out:
1369         debugfs_remove_recursive(clk_debugfs_root);
1370         return err;
1371 }
1372 #endif
1373 #else
1374
1375 void tegra_clk_add(struct clk *clk)
1376 {
1377         struct clk_tegra *c = to_clk_tegra(__clk_get_hw(clk));
1378
1379         mutex_lock(&clock_list_lock);
1380         list_add(&c->node, &clocks);
1381         mutex_unlock(&clock_list_lock);
1382 }
1383
1384 struct clk *tegra_get_clock_by_name(const char *name)
1385 {
1386         struct clk_tegra *c;
1387         struct clk *ret = NULL;
1388         mutex_lock(&clock_list_lock);
1389         list_for_each_entry(c, &clocks, node) {
1390                 if (strcmp(__clk_get_name(c->hw.clk), name) == 0) {
1391                         ret = c->hw.clk;
1392                         break;
1393                 }
1394         }
1395         mutex_unlock(&clock_list_lock);
1396         return ret;
1397 }
1398
1399 static int tegra_clk_init_one_from_table(struct tegra_clk_init_table *table)
1400 {
1401         struct clk *c;
1402         struct clk *p;
1403         struct clk *parent;
1404
1405         int ret = 0;
1406
1407         c = tegra_get_clock_by_name(table->name);
1408
1409         if (!c) {
1410                 pr_warn("Unable to initialize clock %s\n",
1411                         table->name);
1412                 return -ENODEV;
1413         }
1414
1415         parent = clk_get_parent(c);
1416
1417         if (table->parent) {
1418                 p = tegra_get_clock_by_name(table->parent);
1419                 if (!p) {
1420                         pr_warn("Unable to find parent %s of clock %s\n",
1421                                 table->parent, table->name);
1422                         return -ENODEV;
1423                 }
1424
1425                 if (parent != p) {
1426                         ret = clk_set_parent(c, p);
1427                         if (ret) {
1428                                 pr_warn("Unable to set parent %s of clock %s: %d\n",
1429                                         table->parent, table->name, ret);
1430                                 return -EINVAL;
1431                         }
1432                 }
1433         }
1434
1435         if (table->rate && table->rate != clk_get_rate(c)) {
1436                 ret = clk_set_rate(c, table->rate);
1437                 if (ret) {
1438                         pr_warn("Unable to set clock %s to rate %lu: %d\n",
1439                                 table->name, table->rate, ret);
1440                         return -EINVAL;
1441                 }
1442         }
1443
1444         if (table->enabled) {
1445                 ret = clk_prepare_enable(c);
1446                 if (ret) {
1447                         pr_warn("Unable to enable clock %s: %d\n",
1448                                 table->name, ret);
1449                         return -EINVAL;
1450                 }
1451         }
1452
1453         return 0;
1454 }
1455
1456 void tegra_clk_init_from_table(struct tegra_clk_init_table *table)
1457 {
1458         for (; table->name; table++)
1459                 tegra_clk_init_one_from_table(table);
1460 }
1461
1462 void tegra_periph_reset_deassert(struct clk *c)
1463 {
1464         struct clk_tegra *clk = to_clk_tegra(__clk_get_hw(c));
1465         BUG_ON(!clk->reset);
1466         clk->reset(__clk_get_hw(c), false);
1467 }
1468 EXPORT_SYMBOL(tegra_periph_reset_deassert);
1469
1470 void tegra_periph_reset_assert(struct clk *c)
1471 {
1472         struct clk_tegra *clk = to_clk_tegra(__clk_get_hw(c));
1473         BUG_ON(!clk->reset);
1474         clk->reset(__clk_get_hw(c), true);
1475 }
1476 EXPORT_SYMBOL(tegra_periph_reset_assert);
1477
1478 /* Several extended clock configuration bits (e.g., clock routing, clock
1479  * phase control) are included in PLL and peripheral clock source
1480  * registers. */
1481 int tegra_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
1482 {
1483         int ret = 0;
1484         struct clk_tegra *clk = to_clk_tegra(__clk_get_hw(c));
1485
1486         if (!clk->clk_cfg_ex) {
1487                 ret = -ENOSYS;
1488                 goto out;
1489         }
1490         ret = clk->clk_cfg_ex(__clk_get_hw(c), p, setting);
1491
1492 out:
1493         return ret;
1494 }
1495 #endif /* !CONFIG_COMMON_CLK */