]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm/mach-tegra/tegra2_clocks.c
ARM: tegra: clock: Refcount periph clock enables
[karo-tx-linux.git] / arch / arm / mach-tegra / tegra2_clocks.c
1 /*
2  * arch/arm/mach-tegra/tegra2_clocks.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
6  * Author:
7  *      Colin Cross <ccross@google.com>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list.h>
23 #include <linux/spinlock.h>
24 #include <linux/delay.h>
25 #include <linux/io.h>
26 #include <linux/clkdev.h>
27 #include <linux/clk.h>
28
29 #include <mach/iomap.h>
30 #include <mach/suspend.h>
31
32 #include "clock.h"
33 #include "fuse.h"
34 #include "tegra2_emc.h"
35
36 #define RST_DEVICES                     0x004
37 #define RST_DEVICES_SET                 0x300
38 #define RST_DEVICES_CLR                 0x304
39 #define RST_DEVICES_NUM                 3
40
41 #define CLK_OUT_ENB                     0x010
42 #define CLK_OUT_ENB_SET                 0x320
43 #define CLK_OUT_ENB_CLR                 0x324
44 #define CLK_OUT_ENB_NUM                 3
45
46 #define CLK_MASK_ARM                    0x44
47 #define MISC_CLK_ENB                    0x48
48
49 #define OSC_CTRL                        0x50
50 #define OSC_CTRL_OSC_FREQ_MASK          (3<<30)
51 #define OSC_CTRL_OSC_FREQ_13MHZ         (0<<30)
52 #define OSC_CTRL_OSC_FREQ_19_2MHZ       (1<<30)
53 #define OSC_CTRL_OSC_FREQ_12MHZ         (2<<30)
54 #define OSC_CTRL_OSC_FREQ_26MHZ         (3<<30)
55 #define OSC_CTRL_MASK                   (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
56
57 #define OSC_FREQ_DET                    0x58
58 #define OSC_FREQ_DET_TRIG               (1<<31)
59
60 #define OSC_FREQ_DET_STATUS             0x5C
61 #define OSC_FREQ_DET_BUSY               (1<<31)
62 #define OSC_FREQ_DET_CNT_MASK           0xFFFF
63
64 #define PERIPH_CLK_SOURCE_I2S1          0x100
65 #define PERIPH_CLK_SOURCE_EMC           0x19c
66 #define PERIPH_CLK_SOURCE_OSC           0x1fc
67 #define PERIPH_CLK_SOURCE_NUM \
68         ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
69
70 #define PERIPH_CLK_SOURCE_MASK          (3<<30)
71 #define PERIPH_CLK_SOURCE_SHIFT         30
72 #define PERIPH_CLK_SOURCE_ENABLE        (1<<28)
73 #define PERIPH_CLK_SOURCE_DIVU71_MASK   0xFF
74 #define PERIPH_CLK_SOURCE_DIVU16_MASK   0xFFFF
75 #define PERIPH_CLK_SOURCE_DIV_SHIFT     0
76
77 #define SDMMC_CLK_INT_FB_SEL            (1 << 23)
78 #define SDMMC_CLK_INT_FB_DLY_SHIFT      16
79 #define SDMMC_CLK_INT_FB_DLY_MASK       (0xF << SDMMC_CLK_INT_FB_DLY_SHIFT)
80
81 #define PLL_BASE                        0x0
82 #define PLL_BASE_BYPASS                 (1<<31)
83 #define PLL_BASE_ENABLE                 (1<<30)
84 #define PLL_BASE_REF_ENABLE             (1<<29)
85 #define PLL_BASE_OVERRIDE               (1<<28)
86 #define PLL_BASE_DIVP_MASK              (0x7<<20)
87 #define PLL_BASE_DIVP_SHIFT             20
88 #define PLL_BASE_DIVN_MASK              (0x3FF<<8)
89 #define PLL_BASE_DIVN_SHIFT             8
90 #define PLL_BASE_DIVM_MASK              (0x1F)
91 #define PLL_BASE_DIVM_SHIFT             0
92
93 #define PLL_OUT_RATIO_MASK              (0xFF<<8)
94 #define PLL_OUT_RATIO_SHIFT             8
95 #define PLL_OUT_OVERRIDE                (1<<2)
96 #define PLL_OUT_CLKEN                   (1<<1)
97 #define PLL_OUT_RESET_DISABLE           (1<<0)
98
99 #define PLL_MISC(c)                     (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
100
101 #define PLL_MISC_DCCON_SHIFT            20
102 #define PLL_MISC_CPCON_SHIFT            8
103 #define PLL_MISC_CPCON_MASK             (0xF<<PLL_MISC_CPCON_SHIFT)
104 #define PLL_MISC_LFCON_SHIFT            4
105 #define PLL_MISC_LFCON_MASK             (0xF<<PLL_MISC_LFCON_SHIFT)
106 #define PLL_MISC_VCOCON_SHIFT           0
107 #define PLL_MISC_VCOCON_MASK            (0xF<<PLL_MISC_VCOCON_SHIFT)
108
109 #define PLLU_BASE_POST_DIV              (1<<20)
110
111 #define PLLD_MISC_CLKENABLE             (1<<30)
112 #define PLLD_MISC_DIV_RST               (1<<23)
113 #define PLLD_MISC_DCCON_SHIFT           12
114
115 #define PLLE_MISC_READY                 (1 << 15)
116
117 #define PERIPH_CLK_TO_ENB_REG(c)        ((c->u.periph.clk_num / 32) * 4)
118 #define PERIPH_CLK_TO_ENB_SET_REG(c)    ((c->u.periph.clk_num / 32) * 8)
119 #define PERIPH_CLK_TO_ENB_BIT(c)        (1 << (c->u.periph.clk_num % 32))
120
121 #define SUPER_CLK_MUX                   0x00
122 #define SUPER_STATE_SHIFT               28
123 #define SUPER_STATE_MASK                (0xF << SUPER_STATE_SHIFT)
124 #define SUPER_STATE_STANDBY             (0x0 << SUPER_STATE_SHIFT)
125 #define SUPER_STATE_IDLE                (0x1 << SUPER_STATE_SHIFT)
126 #define SUPER_STATE_RUN                 (0x2 << SUPER_STATE_SHIFT)
127 #define SUPER_STATE_IRQ                 (0x3 << SUPER_STATE_SHIFT)
128 #define SUPER_STATE_FIQ                 (0x4 << SUPER_STATE_SHIFT)
129 #define SUPER_SOURCE_MASK               0xF
130 #define SUPER_FIQ_SOURCE_SHIFT          12
131 #define SUPER_IRQ_SOURCE_SHIFT          8
132 #define SUPER_RUN_SOURCE_SHIFT          4
133 #define SUPER_IDLE_SOURCE_SHIFT         0
134
135 #define SUPER_CLK_DIVIDER               0x04
136
137 #define BUS_CLK_DISABLE                 (1<<3)
138 #define BUS_CLK_DIV_MASK                0x3
139
140 #define PMC_CTRL                        0x0
141  #define PMC_CTRL_BLINK_ENB             (1 << 7)
142
143 #define PMC_DPD_PADS_ORIDE              0x1c
144  #define PMC_DPD_PADS_ORIDE_BLINK_ENB   (1 << 20)
145
146 #define PMC_BLINK_TIMER_DATA_ON_SHIFT   0
147 #define PMC_BLINK_TIMER_DATA_ON_MASK    0x7fff
148 #define PMC_BLINK_TIMER_ENB             (1 << 15)
149 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT  16
150 #define PMC_BLINK_TIMER_DATA_OFF_MASK   0xffff
151
152 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
153 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
154
155 /*
156  * Some clocks share a register with other clocks.  Any clock op that
157  * non-atomically modifies a register used by another clock must lock
158  * clock_register_lock first.
159  */
160 static DEFINE_SPINLOCK(clock_register_lock);
161
162 /*
163  * Some peripheral clocks share an enable bit, so refcount the enable bits
164  * in registers CLK_ENABLE_L, CLK_ENABLE_H, and CLK_ENABLE_U
165  */
166 static int tegra_periph_clk_enable_refcount[3 * 32];
167
168 #define clk_writel(value, reg) \
169         __raw_writel(value, (u32)reg_clk_base + (reg))
170 #define clk_readl(reg) \
171         __raw_readl((u32)reg_clk_base + (reg))
172 #define pmc_writel(value, reg) \
173         __raw_writel(value, (u32)reg_pmc_base + (reg))
174 #define pmc_readl(reg) \
175         __raw_readl((u32)reg_pmc_base + (reg))
176
177 unsigned long clk_measure_input_freq(void)
178 {
179         u32 clock_autodetect;
180         clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
181         do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
182         clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
183         if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
184                 return 12000000;
185         } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
186                 return 13000000;
187         } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
188                 return 19200000;
189         } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
190                 return 26000000;
191         } else {
192                 pr_err("%s: Unexpected clock autodetect value %d", __func__, clock_autodetect);
193                 BUG();
194                 return 0;
195         }
196 }
197
198 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate)
199 {
200         s64 divider_u71 = parent_rate * 2;
201         divider_u71 += rate - 1;
202         do_div(divider_u71, rate);
203
204         if (divider_u71 - 2 < 0)
205                 return 0;
206
207         if (divider_u71 - 2 > 255)
208                 return -EINVAL;
209
210         return divider_u71 - 2;
211 }
212
213 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
214 {
215         s64 divider_u16;
216
217         divider_u16 = parent_rate;
218         divider_u16 += rate - 1;
219         do_div(divider_u16, rate);
220
221         if (divider_u16 - 1 < 0)
222                 return 0;
223
224         if (divider_u16 - 1 > 255)
225                 return -EINVAL;
226
227         return divider_u16 - 1;
228 }
229
230 /* clk_m functions */
231 static unsigned long tegra2_clk_m_autodetect_rate(struct clk *c)
232 {
233         u32 auto_clock_control = clk_readl(OSC_CTRL) & ~OSC_CTRL_OSC_FREQ_MASK;
234
235         c->rate = clk_measure_input_freq();
236         switch (c->rate) {
237         case 12000000:
238                 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
239                 break;
240         case 13000000:
241                 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
242                 break;
243         case 19200000:
244                 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
245                 break;
246         case 26000000:
247                 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
248                 break;
249         default:
250                 pr_err("%s: Unexpected clock rate %ld", __func__, c->rate);
251                 BUG();
252         }
253         clk_writel(auto_clock_control, OSC_CTRL);
254         return c->rate;
255 }
256
257 static void tegra2_clk_m_init(struct clk *c)
258 {
259         pr_debug("%s on clock %s\n", __func__, c->name);
260         tegra2_clk_m_autodetect_rate(c);
261 }
262
263 static int tegra2_clk_m_enable(struct clk *c)
264 {
265         pr_debug("%s on clock %s\n", __func__, c->name);
266         return 0;
267 }
268
269 static void tegra2_clk_m_disable(struct clk *c)
270 {
271         pr_debug("%s on clock %s\n", __func__, c->name);
272         BUG();
273 }
274
275 static struct clk_ops tegra_clk_m_ops = {
276         .init           = tegra2_clk_m_init,
277         .enable         = tegra2_clk_m_enable,
278         .disable        = tegra2_clk_m_disable,
279 };
280
281 void tegra2_periph_reset_assert(struct clk *c)
282 {
283         BUG_ON(!c->ops->reset);
284         c->ops->reset(c, true);
285 }
286
287 void tegra2_periph_reset_deassert(struct clk *c)
288 {
289         BUG_ON(!c->ops->reset);
290         c->ops->reset(c, false);
291 }
292
293 /* super clock functions */
294 /* "super clocks" on tegra have two-stage muxes and a clock skipping
295  * super divider.  We will ignore the clock skipping divider, since we
296  * can't lower the voltage when using the clock skip, but we can if we
297  * lower the PLL frequency.
298  */
299 static void tegra2_super_clk_init(struct clk *c)
300 {
301         u32 val;
302         int source;
303         int shift;
304         const struct clk_mux_sel *sel;
305         val = clk_readl(c->reg + SUPER_CLK_MUX);
306         c->state = ON;
307         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
308                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
309         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
310                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
311         source = (val >> shift) & SUPER_SOURCE_MASK;
312         for (sel = c->inputs; sel->input != NULL; sel++) {
313                 if (sel->value == source)
314                         break;
315         }
316         BUG_ON(sel->input == NULL);
317         c->parent = sel->input;
318 }
319
320 static int tegra2_super_clk_enable(struct clk *c)
321 {
322         clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
323         return 0;
324 }
325
326 static void tegra2_super_clk_disable(struct clk *c)
327 {
328         pr_debug("%s on clock %s\n", __func__, c->name);
329
330         /* oops - don't disable the CPU clock! */
331         BUG();
332 }
333
334 static int tegra2_super_clk_set_parent(struct clk *c, struct clk *p)
335 {
336         u32 val;
337         const struct clk_mux_sel *sel;
338         int shift;
339
340         val = clk_readl(c->reg + SUPER_CLK_MUX);;
341         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
342                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
343         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
344                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
345         for (sel = c->inputs; sel->input != NULL; sel++) {
346                 if (sel->input == p) {
347                         val &= ~(SUPER_SOURCE_MASK << shift);
348                         val |= sel->value << shift;
349
350                         if (c->refcnt)
351                                 clk_enable(p);
352
353                         clk_writel(val, c->reg);
354
355                         if (c->refcnt && c->parent)
356                                 clk_disable(c->parent);
357
358                         clk_reparent(c, p);
359                         return 0;
360                 }
361         }
362         return -EINVAL;
363 }
364
365 /*
366  * Super clocks have "clock skippers" instead of dividers.  Dividing using
367  * a clock skipper does not allow the voltage to be scaled down, so instead
368  * adjust the rate of the parent clock.  This requires that the parent of a
369  * super clock have no other children, otherwise the rate will change
370  * underneath the other children.
371  */
372 static int tegra2_super_clk_set_rate(struct clk *c, unsigned long rate)
373 {
374         return clk_set_rate(c->parent, rate);
375 }
376
377 static struct clk_ops tegra_super_ops = {
378         .init                   = tegra2_super_clk_init,
379         .enable                 = tegra2_super_clk_enable,
380         .disable                = tegra2_super_clk_disable,
381         .set_parent             = tegra2_super_clk_set_parent,
382         .set_rate               = tegra2_super_clk_set_rate,
383 };
384
385 /* virtual cpu clock functions */
386 /* some clocks can not be stopped (cpu, memory bus) while the SoC is running.
387    To change the frequency of these clocks, the parent pll may need to be
388    reprogrammed, so the clock must be moved off the pll, the pll reprogrammed,
389    and then the clock moved back to the pll.  To hide this sequence, a virtual
390    clock handles it.
391  */
392 static void tegra2_cpu_clk_init(struct clk *c)
393 {
394 }
395
396 static int tegra2_cpu_clk_enable(struct clk *c)
397 {
398         return 0;
399 }
400
401 static void tegra2_cpu_clk_disable(struct clk *c)
402 {
403         pr_debug("%s on clock %s\n", __func__, c->name);
404
405         /* oops - don't disable the CPU clock! */
406         BUG();
407 }
408
409 static int tegra2_cpu_clk_set_rate(struct clk *c, unsigned long rate)
410 {
411         int ret;
412         /*
413          * Take an extra reference to the main pll so it doesn't turn
414          * off when we move the cpu off of it
415          */
416         clk_enable(c->u.cpu.main);
417
418         ret = clk_set_parent(c->parent, c->u.cpu.backup);
419         if (ret) {
420                 pr_err("Failed to switch cpu to clock %s\n", c->u.cpu.backup->name);
421                 goto out;
422         }
423
424         if (rate == clk_get_rate(c->u.cpu.backup))
425                 goto out;
426
427         ret = clk_set_rate(c->u.cpu.main, rate);
428         if (ret) {
429                 pr_err("Failed to change cpu pll to %lu\n", rate);
430                 goto out;
431         }
432
433         ret = clk_set_parent(c->parent, c->u.cpu.main);
434         if (ret) {
435                 pr_err("Failed to switch cpu to clock %s\n", c->u.cpu.main->name);
436                 goto out;
437         }
438
439 out:
440         clk_disable(c->u.cpu.main);
441         return ret;
442 }
443
444 static struct clk_ops tegra_cpu_ops = {
445         .init     = tegra2_cpu_clk_init,
446         .enable   = tegra2_cpu_clk_enable,
447         .disable  = tegra2_cpu_clk_disable,
448         .set_rate = tegra2_cpu_clk_set_rate,
449 };
450
451 /* virtual cop clock functions. Used to acquire the fake 'cop' clock to
452  * reset the COP block (i.e. AVP) */
453 static void tegra2_cop_clk_reset(struct clk *c, bool assert)
454 {
455         unsigned long reg = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
456
457         pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
458         clk_writel(1 << 1, reg);
459 }
460
461 static struct clk_ops tegra_cop_ops = {
462         .reset    = tegra2_cop_clk_reset,
463 };
464
465 /* bus clock functions */
466 static void tegra2_bus_clk_init(struct clk *c)
467 {
468         u32 val = clk_readl(c->reg);
469         c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
470         c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
471         c->mul = 1;
472 }
473
474 static int tegra2_bus_clk_enable(struct clk *c)
475 {
476         u32 val;
477         unsigned long flags;
478
479         spin_lock_irqsave(&clock_register_lock, flags);
480
481         val = clk_readl(c->reg);
482         val &= ~(BUS_CLK_DISABLE << c->reg_shift);
483         clk_writel(val, c->reg);
484
485         spin_unlock_irqrestore(&clock_register_lock, flags);
486
487         return 0;
488 }
489
490 static void tegra2_bus_clk_disable(struct clk *c)
491 {
492         u32 val;
493         unsigned long flags;
494
495         spin_lock_irqsave(&clock_register_lock, flags);
496
497         val = clk_readl(c->reg);
498         val |= BUS_CLK_DISABLE << c->reg_shift;
499         clk_writel(val, c->reg);
500
501         spin_unlock_irqrestore(&clock_register_lock, flags);
502 }
503
504 static int tegra2_bus_clk_set_rate(struct clk *c, unsigned long rate)
505 {
506         u32 val;
507         unsigned long parent_rate = clk_get_rate(c->parent);
508         unsigned long flags;
509         int ret = -EINVAL;
510         int i;
511
512         spin_lock_irqsave(&clock_register_lock, flags);
513
514         val = clk_readl(c->reg);
515         for (i = 1; i <= 4; i++) {
516                 if (rate == parent_rate / i) {
517                         val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
518                         val |= (i - 1) << c->reg_shift;
519                         clk_writel(val, c->reg);
520                         c->div = i;
521                         c->mul = 1;
522                         ret = 0;
523                         break;
524                 }
525         }
526
527         spin_unlock_irqrestore(&clock_register_lock, flags);
528
529         return ret;
530 }
531
532 static struct clk_ops tegra_bus_ops = {
533         .init                   = tegra2_bus_clk_init,
534         .enable                 = tegra2_bus_clk_enable,
535         .disable                = tegra2_bus_clk_disable,
536         .set_rate               = tegra2_bus_clk_set_rate,
537 };
538
539 /* Blink output functions */
540
541 static void tegra2_blink_clk_init(struct clk *c)
542 {
543         u32 val;
544
545         val = pmc_readl(PMC_CTRL);
546         c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
547         c->mul = 1;
548         val = pmc_readl(c->reg);
549
550         if (val & PMC_BLINK_TIMER_ENB) {
551                 unsigned int on_off;
552
553                 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
554                         PMC_BLINK_TIMER_DATA_ON_MASK;
555                 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
556                 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
557                 on_off += val;
558                 /* each tick in the blink timer is 4 32KHz clocks */
559                 c->div = on_off * 4;
560         } else {
561                 c->div = 1;
562         }
563 }
564
565 static int tegra2_blink_clk_enable(struct clk *c)
566 {
567         u32 val;
568
569         val = pmc_readl(PMC_DPD_PADS_ORIDE);
570         pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
571
572         val = pmc_readl(PMC_CTRL);
573         pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
574
575         return 0;
576 }
577
578 static void tegra2_blink_clk_disable(struct clk *c)
579 {
580         u32 val;
581
582         val = pmc_readl(PMC_CTRL);
583         pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
584
585         val = pmc_readl(PMC_DPD_PADS_ORIDE);
586         pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
587 }
588
589 static int tegra2_blink_clk_set_rate(struct clk *c, unsigned long rate)
590 {
591         unsigned long parent_rate = clk_get_rate(c->parent);
592         if (rate >= parent_rate) {
593                 c->div = 1;
594                 pmc_writel(0, c->reg);
595         } else {
596                 unsigned int on_off;
597                 u32 val;
598
599                 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
600                 c->div = on_off * 8;
601
602                 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
603                         PMC_BLINK_TIMER_DATA_ON_SHIFT;
604                 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
605                 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
606                 val |= on_off;
607                 val |= PMC_BLINK_TIMER_ENB;
608                 pmc_writel(val, c->reg);
609         }
610
611         return 0;
612 }
613
614 static struct clk_ops tegra_blink_clk_ops = {
615         .init                   = &tegra2_blink_clk_init,
616         .enable                 = &tegra2_blink_clk_enable,
617         .disable                = &tegra2_blink_clk_disable,
618         .set_rate               = &tegra2_blink_clk_set_rate,
619 };
620
621 /* PLL Functions */
622 static int tegra2_pll_clk_wait_for_lock(struct clk *c)
623 {
624         udelay(c->u.pll.lock_delay);
625
626         return 0;
627 }
628
629 static void tegra2_pll_clk_init(struct clk *c)
630 {
631         u32 val = clk_readl(c->reg + PLL_BASE);
632
633         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
634
635         if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
636                 pr_warning("Clock %s has unknown fixed frequency\n", c->name);
637                 c->mul = 1;
638                 c->div = 1;
639         } else if (val & PLL_BASE_BYPASS) {
640                 c->mul = 1;
641                 c->div = 1;
642         } else {
643                 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
644                 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
645                 if (c->flags & PLLU)
646                         c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
647                 else
648                         c->div *= (val & PLL_BASE_DIVP_MASK) ? 2 : 1;
649         }
650 }
651
652 static int tegra2_pll_clk_enable(struct clk *c)
653 {
654         u32 val;
655         pr_debug("%s on clock %s\n", __func__, c->name);
656
657         val = clk_readl(c->reg + PLL_BASE);
658         val &= ~PLL_BASE_BYPASS;
659         val |= PLL_BASE_ENABLE;
660         clk_writel(val, c->reg + PLL_BASE);
661
662         tegra2_pll_clk_wait_for_lock(c);
663
664         return 0;
665 }
666
667 static void tegra2_pll_clk_disable(struct clk *c)
668 {
669         u32 val;
670         pr_debug("%s on clock %s\n", __func__, c->name);
671
672         val = clk_readl(c->reg);
673         val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
674         clk_writel(val, c->reg);
675 }
676
677 static int tegra2_pll_clk_set_rate(struct clk *c, unsigned long rate)
678 {
679         u32 val;
680         unsigned long input_rate;
681         const struct clk_pll_freq_table *sel;
682
683         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
684
685         input_rate = clk_get_rate(c->parent);
686         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
687                 if (sel->input_rate == input_rate && sel->output_rate == rate) {
688                         c->mul = sel->n;
689                         c->div = sel->m * sel->p;
690
691                         val = clk_readl(c->reg + PLL_BASE);
692                         if (c->flags & PLL_FIXED)
693                                 val |= PLL_BASE_OVERRIDE;
694                         val &= ~(PLL_BASE_DIVP_MASK | PLL_BASE_DIVN_MASK |
695                                  PLL_BASE_DIVM_MASK);
696                         val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
697                                 (sel->n << PLL_BASE_DIVN_SHIFT);
698                         BUG_ON(sel->p < 1 || sel->p > 2);
699                         if (c->flags & PLLU) {
700                                 if (sel->p == 1)
701                                         val |= PLLU_BASE_POST_DIV;
702                         } else {
703                                 if (sel->p == 2)
704                                         val |= 1 << PLL_BASE_DIVP_SHIFT;
705                         }
706                         clk_writel(val, c->reg + PLL_BASE);
707
708                         if (c->flags & PLL_HAS_CPCON) {
709                                 val = clk_readl(c->reg + PLL_MISC(c));
710                                 val &= ~PLL_MISC_CPCON_MASK;
711                                 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
712                                 clk_writel(val, c->reg + PLL_MISC(c));
713                         }
714
715                         if (c->state == ON)
716                                 tegra2_pll_clk_enable(c);
717
718                         return 0;
719                 }
720         }
721         return -EINVAL;
722 }
723
724 static struct clk_ops tegra_pll_ops = {
725         .init                   = tegra2_pll_clk_init,
726         .enable                 = tegra2_pll_clk_enable,
727         .disable                = tegra2_pll_clk_disable,
728         .set_rate               = tegra2_pll_clk_set_rate,
729 };
730
731 static void tegra2_pllx_clk_init(struct clk *c)
732 {
733         tegra2_pll_clk_init(c);
734
735         if (tegra_sku_id() == 7)
736                 c->max_rate = 750000000;
737 }
738
739 static struct clk_ops tegra_pllx_ops = {
740         .init     = tegra2_pllx_clk_init,
741         .enable   = tegra2_pll_clk_enable,
742         .disable  = tegra2_pll_clk_disable,
743         .set_rate = tegra2_pll_clk_set_rate,
744 };
745
746 static int tegra2_plle_clk_enable(struct clk *c)
747 {
748         u32 val;
749
750         pr_debug("%s on clock %s\n", __func__, c->name);
751
752         mdelay(1);
753
754         val = clk_readl(c->reg + PLL_BASE);
755         if (!(val & PLLE_MISC_READY))
756                 return -EBUSY;
757
758         val = clk_readl(c->reg + PLL_BASE);
759         val |= PLL_BASE_ENABLE | PLL_BASE_BYPASS;
760         clk_writel(val, c->reg + PLL_BASE);
761
762         return 0;
763 }
764
765 static struct clk_ops tegra_plle_ops = {
766         .init       = tegra2_pll_clk_init,
767         .enable     = tegra2_plle_clk_enable,
768         .set_rate   = tegra2_pll_clk_set_rate,
769 };
770
771 /* Clock divider ops */
772 static void tegra2_pll_div_clk_init(struct clk *c)
773 {
774         u32 val = clk_readl(c->reg);
775         u32 divu71;
776         val >>= c->reg_shift;
777         c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
778         if (!(val & PLL_OUT_RESET_DISABLE))
779                 c->state = OFF;
780
781         if (c->flags & DIV_U71) {
782                 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
783                 c->div = (divu71 + 2);
784                 c->mul = 2;
785         } else if (c->flags & DIV_2) {
786                 c->div = 2;
787                 c->mul = 1;
788         } else {
789                 c->div = 1;
790                 c->mul = 1;
791         }
792 }
793
794 static int tegra2_pll_div_clk_enable(struct clk *c)
795 {
796         u32 val;
797         u32 new_val;
798         unsigned long flags;
799
800         pr_debug("%s: %s\n", __func__, c->name);
801         if (c->flags & DIV_U71) {
802                 spin_lock_irqsave(&clock_register_lock, flags);
803                 val = clk_readl(c->reg);
804                 new_val = val >> c->reg_shift;
805                 new_val &= 0xFFFF;
806
807                 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
808
809                 val &= ~(0xFFFF << c->reg_shift);
810                 val |= new_val << c->reg_shift;
811                 clk_writel(val, c->reg);
812                 spin_unlock_irqrestore(&clock_register_lock, flags);
813                 return 0;
814         } else if (c->flags & DIV_2) {
815                 BUG_ON(!(c->flags & PLLD));
816                 spin_lock_irqsave(&clock_register_lock, flags);
817                 val = clk_readl(c->reg);
818                 val &= ~PLLD_MISC_DIV_RST;
819                 clk_writel(val, c->reg);
820                 spin_unlock_irqrestore(&clock_register_lock, flags);
821                 return 0;
822         }
823         return -EINVAL;
824 }
825
826 static void tegra2_pll_div_clk_disable(struct clk *c)
827 {
828         u32 val;
829         u32 new_val;
830         unsigned long flags;
831
832         pr_debug("%s: %s\n", __func__, c->name);
833         if (c->flags & DIV_U71) {
834                 spin_lock_irqsave(&clock_register_lock, flags);
835                 val = clk_readl(c->reg);
836                 new_val = val >> c->reg_shift;
837                 new_val &= 0xFFFF;
838
839                 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
840
841                 val &= ~(0xFFFF << c->reg_shift);
842                 val |= new_val << c->reg_shift;
843                 clk_writel(val, c->reg);
844                 spin_unlock_irqrestore(&clock_register_lock, flags);
845         } else if (c->flags & DIV_2) {
846                 BUG_ON(!(c->flags & PLLD));
847                 spin_lock_irqsave(&clock_register_lock, flags);
848                 val = clk_readl(c->reg);
849                 val |= PLLD_MISC_DIV_RST;
850                 clk_writel(val, c->reg);
851                 spin_unlock_irqrestore(&clock_register_lock, flags);
852         }
853 }
854
855 static int tegra2_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
856 {
857         u32 val;
858         u32 new_val;
859         int divider_u71;
860         unsigned long parent_rate = clk_get_rate(c->parent);
861         unsigned long flags;
862
863         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
864         if (c->flags & DIV_U71) {
865                 divider_u71 = clk_div71_get_divider(parent_rate, rate);
866                 if (divider_u71 >= 0) {
867                         spin_lock_irqsave(&clock_register_lock, flags);
868                         val = clk_readl(c->reg);
869                         new_val = val >> c->reg_shift;
870                         new_val &= 0xFFFF;
871                         if (c->flags & DIV_U71_FIXED)
872                                 new_val |= PLL_OUT_OVERRIDE;
873                         new_val &= ~PLL_OUT_RATIO_MASK;
874                         new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
875
876                         val &= ~(0xFFFF << c->reg_shift);
877                         val |= new_val << c->reg_shift;
878                         clk_writel(val, c->reg);
879                         c->div = divider_u71 + 2;
880                         c->mul = 2;
881                         spin_unlock_irqrestore(&clock_register_lock, flags);
882                         return 0;
883                 }
884         } else if (c->flags & DIV_2) {
885                 if (parent_rate == rate * 2)
886                         return 0;
887         }
888         return -EINVAL;
889 }
890
891 static long tegra2_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
892 {
893         int divider;
894         unsigned long parent_rate = clk_get_rate(c->parent);
895         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
896
897         if (c->flags & DIV_U71) {
898                 divider = clk_div71_get_divider(parent_rate, rate);
899                 if (divider < 0)
900                         return divider;
901                 return parent_rate * 2 / (divider + 2);
902         } else if (c->flags & DIV_2) {
903                 return parent_rate / 2;
904         }
905         return -EINVAL;
906 }
907
908 static struct clk_ops tegra_pll_div_ops = {
909         .init                   = tegra2_pll_div_clk_init,
910         .enable                 = tegra2_pll_div_clk_enable,
911         .disable                = tegra2_pll_div_clk_disable,
912         .set_rate               = tegra2_pll_div_clk_set_rate,
913         .round_rate             = tegra2_pll_div_clk_round_rate,
914 };
915
916 /* Periph clk ops */
917
918 static void tegra2_periph_clk_init(struct clk *c)
919 {
920         u32 val = clk_readl(c->reg);
921         const struct clk_mux_sel *mux = 0;
922         const struct clk_mux_sel *sel;
923         if (c->flags & MUX) {
924                 for (sel = c->inputs; sel->input != NULL; sel++) {
925                         if (val >> PERIPH_CLK_SOURCE_SHIFT == sel->value)
926                                 mux = sel;
927                 }
928                 BUG_ON(!mux);
929
930                 c->parent = mux->input;
931         } else {
932                 c->parent = c->inputs[0].input;
933         }
934
935         if (c->flags & DIV_U71) {
936                 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
937                 c->div = divu71 + 2;
938                 c->mul = 2;
939         } else if (c->flags & DIV_U16) {
940                 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
941                 c->div = divu16 + 1;
942                 c->mul = 1;
943         } else {
944                 c->div = 1;
945                 c->mul = 1;
946         }
947
948         c->state = ON;
949         if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
950                         PERIPH_CLK_TO_ENB_BIT(c)))
951                 c->state = OFF;
952         if (!(c->flags & PERIPH_NO_RESET))
953                 if (clk_readl(RST_DEVICES + PERIPH_CLK_TO_ENB_REG(c)) &
954                                 PERIPH_CLK_TO_ENB_BIT(c))
955                         c->state = OFF;
956 }
957
958 static int tegra2_periph_clk_enable(struct clk *c)
959 {
960         u32 val;
961         unsigned long flags;
962         int refcount;
963         pr_debug("%s on clock %s\n", __func__, c->name);
964
965         spin_lock_irqsave(&clock_register_lock, flags);
966
967         refcount = tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
968
969         if (refcount > 1)
970                 goto out;
971
972         clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
973                 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
974         if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET))
975                 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
976                         RST_DEVICES_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
977         if (c->flags & PERIPH_EMC_ENB) {
978                 /* The EMC peripheral clock has 2 extra enable bits */
979                 /* FIXME: Do they need to be disabled? */
980                 val = clk_readl(c->reg);
981                 val |= 0x3 << 24;
982                 clk_writel(val, c->reg);
983         }
984
985 out:
986         spin_unlock_irqrestore(&clock_register_lock, flags);
987
988         return 0;
989 }
990
991 static void tegra2_periph_clk_disable(struct clk *c)
992 {
993         unsigned long flags;
994
995         pr_debug("%s on clock %s\n", __func__, c->name);
996
997         spin_lock_irqsave(&clock_register_lock, flags);
998
999         if (c->refcnt)
1000                 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1001
1002         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0)
1003                 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1004                         CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1005
1006         spin_unlock_irqrestore(&clock_register_lock, flags);
1007 }
1008
1009 static void tegra2_periph_clk_reset(struct clk *c, bool assert)
1010 {
1011         unsigned long base = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
1012
1013         pr_debug("%s %s on clock %s\n", __func__,
1014                  assert ? "assert" : "deassert", c->name);
1015         if (!(c->flags & PERIPH_NO_RESET))
1016                 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1017                            base + PERIPH_CLK_TO_ENB_SET_REG(c));
1018 }
1019
1020 static int tegra2_periph_clk_set_parent(struct clk *c, struct clk *p)
1021 {
1022         u32 val;
1023         const struct clk_mux_sel *sel;
1024         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1025         for (sel = c->inputs; sel->input != NULL; sel++) {
1026                 if (sel->input == p) {
1027                         val = clk_readl(c->reg);
1028                         val &= ~PERIPH_CLK_SOURCE_MASK;
1029                         val |= (sel->value) << PERIPH_CLK_SOURCE_SHIFT;
1030
1031                         if (c->refcnt)
1032                                 clk_enable(p);
1033
1034                         clk_writel(val, c->reg);
1035
1036                         if (c->refcnt && c->parent)
1037                                 clk_disable(c->parent);
1038
1039                         clk_reparent(c, p);
1040                         return 0;
1041                 }
1042         }
1043
1044         return -EINVAL;
1045 }
1046
1047 static int tegra2_periph_clk_set_rate(struct clk *c, unsigned long rate)
1048 {
1049         u32 val;
1050         int divider;
1051         unsigned long parent_rate = clk_get_rate(c->parent);
1052
1053         if (c->flags & DIV_U71) {
1054                 divider = clk_div71_get_divider(parent_rate, rate);
1055                 if (divider >= 0) {
1056                         val = clk_readl(c->reg);
1057                         val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1058                         val |= divider;
1059                         clk_writel(val, c->reg);
1060                         c->div = divider + 2;
1061                         c->mul = 2;
1062                         return 0;
1063                 }
1064         } else if (c->flags & DIV_U16) {
1065                 divider = clk_div16_get_divider(parent_rate, rate);
1066                 if (divider >= 0) {
1067                         val = clk_readl(c->reg);
1068                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1069                         val |= divider;
1070                         clk_writel(val, c->reg);
1071                         c->div = divider + 1;
1072                         c->mul = 1;
1073                         return 0;
1074                 }
1075         } else if (parent_rate <= rate) {
1076                 c->div = 1;
1077                 c->mul = 1;
1078                 return 0;
1079         }
1080         return -EINVAL;
1081 }
1082
1083 static long tegra2_periph_clk_round_rate(struct clk *c,
1084         unsigned long rate)
1085 {
1086         int divider;
1087         unsigned long parent_rate = clk_get_rate(c->parent);
1088         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1089
1090         if (c->flags & DIV_U71) {
1091                 divider = clk_div71_get_divider(parent_rate, rate);
1092                 if (divider < 0)
1093                         return divider;
1094
1095                 return parent_rate * 2 / (divider + 2);
1096         } else if (c->flags & DIV_U16) {
1097                 divider = clk_div16_get_divider(parent_rate, rate);
1098                 if (divider < 0)
1099                         return divider;
1100                 return parent_rate / (divider + 1);
1101         }
1102         return -EINVAL;
1103 }
1104
1105 static struct clk_ops tegra_periph_clk_ops = {
1106         .init                   = &tegra2_periph_clk_init,
1107         .enable                 = &tegra2_periph_clk_enable,
1108         .disable                = &tegra2_periph_clk_disable,
1109         .set_parent             = &tegra2_periph_clk_set_parent,
1110         .set_rate               = &tegra2_periph_clk_set_rate,
1111         .round_rate             = &tegra2_periph_clk_round_rate,
1112         .reset                  = &tegra2_periph_clk_reset,
1113 };
1114
1115 /* The SDMMC controllers have extra bits in the clock source register that
1116  * adjust the delay between the clock and data to compenstate for delays
1117  * on the PCB. */
1118 void tegra2_sdmmc_tap_delay(struct clk *c, int delay)
1119 {
1120         u32 reg;
1121
1122         delay = clamp(delay, 0, 15);
1123         reg = clk_readl(c->reg);
1124         reg &= ~SDMMC_CLK_INT_FB_DLY_MASK;
1125         reg |= SDMMC_CLK_INT_FB_SEL;
1126         reg |= delay << SDMMC_CLK_INT_FB_DLY_SHIFT;
1127         clk_writel(reg, c->reg);
1128 }
1129
1130 /* External memory controller clock ops */
1131 static void tegra2_emc_clk_init(struct clk *c)
1132 {
1133         tegra2_periph_clk_init(c);
1134         c->max_rate = clk_get_rate_locked(c);
1135 }
1136
1137 static long tegra2_emc_clk_round_rate(struct clk *c, unsigned long rate)
1138 {
1139         long new_rate = rate;
1140
1141         new_rate = tegra_emc_round_rate(new_rate);
1142         if (new_rate < 0)
1143                 return c->max_rate;
1144
1145         BUG_ON(new_rate != tegra2_periph_clk_round_rate(c, new_rate));
1146
1147         return new_rate;
1148 }
1149
1150 static int tegra2_emc_clk_set_rate(struct clk *c, unsigned long rate)
1151 {
1152         int ret;
1153         /*
1154          * The Tegra2 memory controller has an interlock with the clock
1155          * block that allows memory shadowed registers to be updated,
1156          * and then transfer them to the main registers at the same
1157          * time as the clock update without glitches.
1158          */
1159         ret = tegra_emc_set_rate(rate);
1160         if (ret < 0)
1161                 return ret;
1162
1163         ret = tegra2_periph_clk_set_rate(c, rate);
1164         udelay(1);
1165
1166         return ret;
1167 }
1168
1169 static struct clk_ops tegra_emc_clk_ops = {
1170         .init                   = &tegra2_emc_clk_init,
1171         .enable                 = &tegra2_periph_clk_enable,
1172         .disable                = &tegra2_periph_clk_disable,
1173         .set_parent             = &tegra2_periph_clk_set_parent,
1174         .set_rate               = &tegra2_emc_clk_set_rate,
1175         .round_rate             = &tegra2_emc_clk_round_rate,
1176         .reset                  = &tegra2_periph_clk_reset,
1177 };
1178
1179 /* Clock doubler ops */
1180 static void tegra2_clk_double_init(struct clk *c)
1181 {
1182         c->mul = 2;
1183         c->div = 1;
1184         c->state = ON;
1185         if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1186                         PERIPH_CLK_TO_ENB_BIT(c)))
1187                 c->state = OFF;
1188 };
1189
1190 static int tegra2_clk_double_set_rate(struct clk *c, unsigned long rate)
1191 {
1192         if (rate != 2 * clk_get_rate(c->parent))
1193                 return -EINVAL;
1194         c->mul = 2;
1195         c->div = 1;
1196         return 0;
1197 }
1198
1199 static struct clk_ops tegra_clk_double_ops = {
1200         .init                   = &tegra2_clk_double_init,
1201         .enable                 = &tegra2_periph_clk_enable,
1202         .disable                = &tegra2_periph_clk_disable,
1203         .set_rate               = &tegra2_clk_double_set_rate,
1204 };
1205
1206 /* Audio sync clock ops */
1207 static void tegra2_audio_sync_clk_init(struct clk *c)
1208 {
1209         int source;
1210         const struct clk_mux_sel *sel;
1211         u32 val = clk_readl(c->reg);
1212         c->state = (val & (1<<4)) ? OFF : ON;
1213         source = val & 0xf;
1214         for (sel = c->inputs; sel->input != NULL; sel++)
1215                 if (sel->value == source)
1216                         break;
1217         BUG_ON(sel->input == NULL);
1218         c->parent = sel->input;
1219 }
1220
1221 static int tegra2_audio_sync_clk_enable(struct clk *c)
1222 {
1223         clk_writel(0, c->reg);
1224         return 0;
1225 }
1226
1227 static void tegra2_audio_sync_clk_disable(struct clk *c)
1228 {
1229         clk_writel(1, c->reg);
1230 }
1231
1232 static int tegra2_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
1233 {
1234         u32 val;
1235         const struct clk_mux_sel *sel;
1236         for (sel = c->inputs; sel->input != NULL; sel++) {
1237                 if (sel->input == p) {
1238                         val = clk_readl(c->reg);
1239                         val &= ~0xf;
1240                         val |= sel->value;
1241
1242                         if (c->refcnt)
1243                                 clk_enable(p);
1244
1245                         clk_writel(val, c->reg);
1246
1247                         if (c->refcnt && c->parent)
1248                                 clk_disable(c->parent);
1249
1250                         clk_reparent(c, p);
1251                         return 0;
1252                 }
1253         }
1254
1255         return -EINVAL;
1256 }
1257
1258 static struct clk_ops tegra_audio_sync_clk_ops = {
1259         .init       = tegra2_audio_sync_clk_init,
1260         .enable     = tegra2_audio_sync_clk_enable,
1261         .disable    = tegra2_audio_sync_clk_disable,
1262         .set_parent = tegra2_audio_sync_clk_set_parent,
1263 };
1264
1265 /* cdev1 and cdev2 (dap_mclk1 and dap_mclk2) ops */
1266
1267 static void tegra2_cdev_clk_init(struct clk *c)
1268 {
1269         /* We could un-tristate the cdev1 or cdev2 pingroup here; this is
1270          * currently done in the pinmux code. */
1271         c->state = ON;
1272         if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1273                         PERIPH_CLK_TO_ENB_BIT(c)))
1274                 c->state = OFF;
1275 }
1276
1277 static int tegra2_cdev_clk_enable(struct clk *c)
1278 {
1279         clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1280                 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1281         return 0;
1282 }
1283
1284 static void tegra2_cdev_clk_disable(struct clk *c)
1285 {
1286         clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1287                 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1288 }
1289
1290 static struct clk_ops tegra_cdev_clk_ops = {
1291         .init                   = &tegra2_cdev_clk_init,
1292         .enable                 = &tegra2_cdev_clk_enable,
1293         .disable                = &tegra2_cdev_clk_disable,
1294 };
1295
1296 /* shared bus ops */
1297 /*
1298  * Some clocks may have multiple downstream users that need to request a
1299  * higher clock rate.  Shared bus clocks provide a unique shared_bus_user
1300  * clock to each user.  The frequency of the bus is set to the highest
1301  * enabled shared_bus_user clock, with a minimum value set by the
1302  * shared bus.
1303  */
1304 static int tegra_clk_shared_bus_update(struct clk *bus)
1305 {
1306         struct clk *c;
1307         unsigned long rate = bus->min_rate;
1308
1309         list_for_each_entry(c, &bus->shared_bus_list, u.shared_bus_user.node)
1310                 if (c->u.shared_bus_user.enabled)
1311                         rate = max(c->u.shared_bus_user.rate, rate);
1312
1313         if (rate == clk_get_rate_locked(bus))
1314                 return 0;
1315
1316         return clk_set_rate_locked(bus, rate);
1317 };
1318
1319 static void tegra_clk_shared_bus_init(struct clk *c)
1320 {
1321         unsigned long flags;
1322
1323         c->max_rate = c->parent->max_rate;
1324         c->u.shared_bus_user.rate = c->parent->max_rate;
1325         c->state = OFF;
1326         c->set = true;
1327
1328         spin_lock_irqsave(&c->parent->spinlock, flags);
1329
1330         list_add_tail(&c->u.shared_bus_user.node,
1331                 &c->parent->shared_bus_list);
1332
1333         spin_unlock_irqrestore(&c->parent->spinlock, flags);
1334 }
1335
1336 static int tegra_clk_shared_bus_set_rate(struct clk *c, unsigned long rate)
1337 {
1338         unsigned long flags;
1339         int ret;
1340
1341         rate = clk_round_rate(c->parent, rate);
1342         if (rate < 0)
1343                 return rate;
1344
1345         spin_lock_irqsave(&c->parent->spinlock, flags);
1346
1347         c->u.shared_bus_user.rate = rate;
1348         ret = tegra_clk_shared_bus_update(c->parent);
1349
1350         spin_unlock_irqrestore(&c->parent->spinlock, flags);
1351
1352         return ret;
1353 }
1354
1355 static long tegra_clk_shared_bus_round_rate(struct clk *c, unsigned long rate)
1356 {
1357         return clk_round_rate(c->parent, rate);
1358 }
1359
1360 static int tegra_clk_shared_bus_enable(struct clk *c)
1361 {
1362         unsigned long flags;
1363         int ret;
1364
1365         spin_lock_irqsave(&c->parent->spinlock, flags);
1366
1367         c->u.shared_bus_user.enabled = true;
1368         ret = tegra_clk_shared_bus_update(c->parent);
1369
1370         spin_unlock_irqrestore(&c->parent->spinlock, flags);
1371
1372         return ret;
1373 }
1374
1375 static void tegra_clk_shared_bus_disable(struct clk *c)
1376 {
1377         unsigned long flags;
1378         int ret;
1379
1380         spin_lock_irqsave(&c->parent->spinlock, flags);
1381
1382         c->u.shared_bus_user.enabled = false;
1383         ret = tegra_clk_shared_bus_update(c->parent);
1384         WARN_ON_ONCE(ret);
1385
1386         spin_unlock_irqrestore(&c->parent->spinlock, flags);
1387 }
1388
1389 static struct clk_ops tegra_clk_shared_bus_ops = {
1390         .init = tegra_clk_shared_bus_init,
1391         .enable = tegra_clk_shared_bus_enable,
1392         .disable = tegra_clk_shared_bus_disable,
1393         .set_rate = tegra_clk_shared_bus_set_rate,
1394         .round_rate = tegra_clk_shared_bus_round_rate,
1395 };
1396
1397
1398 /* Clock definitions */
1399 static struct clk tegra_clk_32k = {
1400         .name = "clk_32k",
1401         .rate = 32768,
1402         .ops  = NULL,
1403         .max_rate = 32768,
1404 };
1405
1406 static struct clk_pll_freq_table tegra_pll_s_freq_table[] = {
1407         {32768, 12000000, 366, 1, 1, 0},
1408         {32768, 13000000, 397, 1, 1, 0},
1409         {32768, 19200000, 586, 1, 1, 0},
1410         {32768, 26000000, 793, 1, 1, 0},
1411         {0, 0, 0, 0, 0, 0},
1412 };
1413
1414 static struct clk tegra_pll_s = {
1415         .name      = "pll_s",
1416         .flags     = PLL_ALT_MISC_REG,
1417         .ops       = &tegra_pll_ops,
1418         .parent    = &tegra_clk_32k,
1419         .max_rate  = 26000000,
1420         .reg       = 0xf0,
1421         .u.pll = {
1422                 .input_min = 32768,
1423                 .input_max = 32768,
1424                 .cf_min    = 0, /* FIXME */
1425                 .cf_max    = 0, /* FIXME */
1426                 .vco_min   = 12000000,
1427                 .vco_max   = 26000000,
1428                 .freq_table = tegra_pll_s_freq_table,
1429                 .lock_delay = 300,
1430         },
1431 };
1432
1433 static struct clk_mux_sel tegra_clk_m_sel[] = {
1434         { .input = &tegra_clk_32k, .value = 0},
1435         { .input = &tegra_pll_s,  .value = 1},
1436         { 0, 0},
1437 };
1438
1439 static struct clk tegra_clk_m = {
1440         .name      = "clk_m",
1441         .flags     = ENABLE_ON_INIT,
1442         .ops       = &tegra_clk_m_ops,
1443         .inputs    = tegra_clk_m_sel,
1444         .reg       = 0x1fc,
1445         .reg_shift = 28,
1446         .max_rate  = 26000000,
1447 };
1448
1449 static struct clk_pll_freq_table tegra_pll_c_freq_table[] = {
1450         { 0, 0, 0, 0, 0, 0 },
1451 };
1452
1453 static struct clk tegra_pll_c = {
1454         .name      = "pll_c",
1455         .flags     = PLL_HAS_CPCON,
1456         .ops       = &tegra_pll_ops,
1457         .reg       = 0x80,
1458         .parent    = &tegra_clk_m,
1459         .max_rate  = 600000000,
1460         .u.pll = {
1461                 .input_min = 2000000,
1462                 .input_max = 31000000,
1463                 .cf_min    = 1000000,
1464                 .cf_max    = 6000000,
1465                 .vco_min   = 20000000,
1466                 .vco_max   = 1400000000,
1467                 .freq_table = tegra_pll_c_freq_table,
1468                 .lock_delay = 300,
1469         },
1470 };
1471
1472 static struct clk tegra_pll_c_out1 = {
1473         .name      = "pll_c_out1",
1474         .ops       = &tegra_pll_div_ops,
1475         .flags     = DIV_U71,
1476         .parent    = &tegra_pll_c,
1477         .reg       = 0x84,
1478         .reg_shift = 0,
1479         .max_rate  = 600000000,
1480 };
1481
1482 static struct clk_pll_freq_table tegra_pll_m_freq_table[] = {
1483         { 12000000, 666000000, 666, 12, 1, 8},
1484         { 13000000, 666000000, 666, 13, 1, 8},
1485         { 19200000, 666000000, 555, 16, 1, 8},
1486         { 26000000, 666000000, 666, 26, 1, 8},
1487         { 12000000, 600000000, 600, 12, 1, 8},
1488         { 13000000, 600000000, 600, 13, 1, 8},
1489         { 19200000, 600000000, 375, 12, 1, 6},
1490         { 26000000, 600000000, 600, 26, 1, 8},
1491         { 0, 0, 0, 0, 0, 0 },
1492 };
1493
1494 static struct clk tegra_pll_m = {
1495         .name      = "pll_m",
1496         .flags     = PLL_HAS_CPCON,
1497         .ops       = &tegra_pll_ops,
1498         .reg       = 0x90,
1499         .parent    = &tegra_clk_m,
1500         .max_rate  = 800000000,
1501         .u.pll = {
1502                 .input_min = 2000000,
1503                 .input_max = 31000000,
1504                 .cf_min    = 1000000,
1505                 .cf_max    = 6000000,
1506                 .vco_min   = 20000000,
1507                 .vco_max   = 1200000000,
1508                 .freq_table = tegra_pll_m_freq_table,
1509                 .lock_delay = 300,
1510         },
1511 };
1512
1513 static struct clk tegra_pll_m_out1 = {
1514         .name      = "pll_m_out1",
1515         .ops       = &tegra_pll_div_ops,
1516         .flags     = DIV_U71,
1517         .parent    = &tegra_pll_m,
1518         .reg       = 0x94,
1519         .reg_shift = 0,
1520         .max_rate  = 600000000,
1521 };
1522
1523 static struct clk_pll_freq_table tegra_pll_p_freq_table[] = {
1524         { 12000000, 216000000, 432, 12, 2, 8},
1525         { 13000000, 216000000, 432, 13, 2, 8},
1526         { 19200000, 216000000, 90,   4, 2, 1},
1527         { 26000000, 216000000, 432, 26, 2, 8},
1528         { 12000000, 432000000, 432, 12, 1, 8},
1529         { 13000000, 432000000, 432, 13, 1, 8},
1530         { 19200000, 432000000, 90,   4, 1, 1},
1531         { 26000000, 432000000, 432, 26, 1, 8},
1532         { 0, 0, 0, 0, 0, 0 },
1533 };
1534
1535 static struct clk tegra_pll_p = {
1536         .name      = "pll_p",
1537         .flags     = ENABLE_ON_INIT | PLL_FIXED | PLL_HAS_CPCON,
1538         .ops       = &tegra_pll_ops,
1539         .reg       = 0xa0,
1540         .parent    = &tegra_clk_m,
1541         .max_rate  = 432000000,
1542         .u.pll = {
1543                 .input_min = 2000000,
1544                 .input_max = 31000000,
1545                 .cf_min    = 1000000,
1546                 .cf_max    = 6000000,
1547                 .vco_min   = 20000000,
1548                 .vco_max   = 1400000000,
1549                 .freq_table = tegra_pll_p_freq_table,
1550                 .lock_delay = 300,
1551         },
1552 };
1553
1554 static struct clk tegra_pll_p_out1 = {
1555         .name      = "pll_p_out1",
1556         .ops       = &tegra_pll_div_ops,
1557         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1558         .parent    = &tegra_pll_p,
1559         .reg       = 0xa4,
1560         .reg_shift = 0,
1561         .max_rate  = 432000000,
1562 };
1563
1564 static struct clk tegra_pll_p_out2 = {
1565         .name      = "pll_p_out2",
1566         .ops       = &tegra_pll_div_ops,
1567         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1568         .parent    = &tegra_pll_p,
1569         .reg       = 0xa4,
1570         .reg_shift = 16,
1571         .max_rate  = 432000000,
1572 };
1573
1574 static struct clk tegra_pll_p_out3 = {
1575         .name      = "pll_p_out3",
1576         .ops       = &tegra_pll_div_ops,
1577         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1578         .parent    = &tegra_pll_p,
1579         .reg       = 0xa8,
1580         .reg_shift = 0,
1581         .max_rate  = 432000000,
1582 };
1583
1584 static struct clk tegra_pll_p_out4 = {
1585         .name      = "pll_p_out4",
1586         .ops       = &tegra_pll_div_ops,
1587         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1588         .parent    = &tegra_pll_p,
1589         .reg       = 0xa8,
1590         .reg_shift = 16,
1591         .max_rate  = 432000000,
1592 };
1593
1594 static struct clk_pll_freq_table tegra_pll_a_freq_table[] = {
1595         { 28800000, 56448000, 49, 25, 1, 1},
1596         { 28800000, 73728000, 64, 25, 1, 1},
1597         { 28800000, 24000000,  5,  6, 1, 1},
1598         { 0, 0, 0, 0, 0, 0 },
1599 };
1600
1601 static struct clk tegra_pll_a = {
1602         .name      = "pll_a",
1603         .flags     = PLL_HAS_CPCON,
1604         .ops       = &tegra_pll_ops,
1605         .reg       = 0xb0,
1606         .parent    = &tegra_pll_p_out1,
1607         .max_rate  = 73728000,
1608         .u.pll = {
1609                 .input_min = 2000000,
1610                 .input_max = 31000000,
1611                 .cf_min    = 1000000,
1612                 .cf_max    = 6000000,
1613                 .vco_min   = 20000000,
1614                 .vco_max   = 1400000000,
1615                 .freq_table = tegra_pll_a_freq_table,
1616                 .lock_delay = 300,
1617         },
1618 };
1619
1620 static struct clk tegra_pll_a_out0 = {
1621         .name      = "pll_a_out0",
1622         .ops       = &tegra_pll_div_ops,
1623         .flags     = DIV_U71,
1624         .parent    = &tegra_pll_a,
1625         .reg       = 0xb4,
1626         .reg_shift = 0,
1627         .max_rate  = 73728000,
1628 };
1629
1630 static struct clk_pll_freq_table tegra_pll_d_freq_table[] = {
1631         { 12000000, 216000000, 216, 12, 1, 4},
1632         { 13000000, 216000000, 216, 13, 1, 4},
1633         { 19200000, 216000000, 135, 12, 1, 3},
1634         { 26000000, 216000000, 216, 26, 1, 4},
1635
1636         { 12000000, 594000000, 594, 12, 1, 8},
1637         { 13000000, 594000000, 594, 13, 1, 8},
1638         { 19200000, 594000000, 495, 16, 1, 8},
1639         { 26000000, 594000000, 594, 26, 1, 8},
1640
1641         { 12000000, 1000000000, 1000, 12, 1, 12},
1642         { 13000000, 1000000000, 1000, 13, 1, 12},
1643         { 19200000, 1000000000, 625,  12, 1, 8},
1644         { 26000000, 1000000000, 1000, 26, 1, 12},
1645
1646         { 0, 0, 0, 0, 0, 0 },
1647 };
1648
1649 static struct clk tegra_pll_d = {
1650         .name      = "pll_d",
1651         .flags     = PLL_HAS_CPCON | PLLD,
1652         .ops       = &tegra_pll_ops,
1653         .reg       = 0xd0,
1654         .parent    = &tegra_clk_m,
1655         .max_rate  = 1000000000,
1656         .u.pll = {
1657                 .input_min = 2000000,
1658                 .input_max = 40000000,
1659                 .cf_min    = 1000000,
1660                 .cf_max    = 6000000,
1661                 .vco_min   = 40000000,
1662                 .vco_max   = 1000000000,
1663                 .freq_table = tegra_pll_d_freq_table,
1664                 .lock_delay = 1000,
1665         },
1666 };
1667
1668 static struct clk tegra_pll_d_out0 = {
1669         .name      = "pll_d_out0",
1670         .ops       = &tegra_pll_div_ops,
1671         .flags     = DIV_2 | PLLD,
1672         .parent    = &tegra_pll_d,
1673         .max_rate  = 500000000,
1674 };
1675
1676 static struct clk_pll_freq_table tegra_pll_u_freq_table[] = {
1677         { 12000000, 480000000, 960, 12, 2, 0},
1678         { 13000000, 480000000, 960, 13, 2, 0},
1679         { 19200000, 480000000, 200, 4,  2, 0},
1680         { 26000000, 480000000, 960, 26, 2, 0},
1681         { 0, 0, 0, 0, 0, 0 },
1682 };
1683
1684 static struct clk tegra_pll_u = {
1685         .name      = "pll_u",
1686         .flags     = PLLU,
1687         .ops       = &tegra_pll_ops,
1688         .reg       = 0xc0,
1689         .parent    = &tegra_clk_m,
1690         .max_rate  = 480000000,
1691         .u.pll = {
1692                 .input_min = 2000000,
1693                 .input_max = 40000000,
1694                 .cf_min    = 1000000,
1695                 .cf_max    = 6000000,
1696                 .vco_min   = 480000000,
1697                 .vco_max   = 960000000,
1698                 .freq_table = tegra_pll_u_freq_table,
1699                 .lock_delay = 1000,
1700         },
1701 };
1702
1703 static struct clk_pll_freq_table tegra_pll_x_freq_table[] = {
1704         /* 1 GHz */
1705         { 12000000, 1000000000, 1000, 12, 1, 12},
1706         { 13000000, 1000000000, 1000, 13, 1, 12},
1707         { 19200000, 1000000000, 625,  12, 1, 8},
1708         { 26000000, 1000000000, 1000, 26, 1, 12},
1709
1710         /* 912 MHz */
1711         { 12000000, 912000000,  912,  12, 1, 12},
1712         { 13000000, 912000000,  912,  13, 1, 12},
1713         { 19200000, 912000000,  760,  16, 1, 8},
1714         { 26000000, 912000000,  912,  26, 1, 12},
1715
1716         /* 816 MHz */
1717         { 12000000, 816000000,  816,  12, 1, 12},
1718         { 13000000, 816000000,  816,  13, 1, 12},
1719         { 19200000, 816000000,  680,  16, 1, 8},
1720         { 26000000, 816000000,  816,  26, 1, 12},
1721
1722         /* 760 MHz */
1723         { 12000000, 760000000,  760,  12, 1, 12},
1724         { 13000000, 760000000,  760,  13, 1, 12},
1725         { 19200000, 760000000,  950,  24, 1, 8},
1726         { 26000000, 760000000,  760,  26, 1, 12},
1727
1728         /* 608 MHz */
1729         { 12000000, 608000000,  608,  12, 1, 12},
1730         { 13000000, 608000000,  608,  13, 1, 12},
1731         { 19200000, 608000000,  380,  12, 1, 8},
1732         { 26000000, 608000000,  608,  26, 1, 12},
1733
1734         /* 456 MHz */
1735         { 12000000, 456000000,  456,  12, 1, 12},
1736         { 13000000, 456000000,  456,  13, 1, 12},
1737         { 19200000, 456000000,  380,  16, 1, 8},
1738         { 26000000, 456000000,  456,  26, 1, 12},
1739
1740         /* 312 MHz */
1741         { 12000000, 312000000,  312,  12, 1, 12},
1742         { 13000000, 312000000,  312,  13, 1, 12},
1743         { 19200000, 312000000,  260,  16, 1, 8},
1744         { 26000000, 312000000,  312,  26, 1, 12},
1745
1746         { 0, 0, 0, 0, 0, 0 },
1747 };
1748
1749 static struct clk tegra_pll_x = {
1750         .name      = "pll_x",
1751         .flags     = PLL_HAS_CPCON | PLL_ALT_MISC_REG,
1752         .ops       = &tegra_pllx_ops,
1753         .reg       = 0xe0,
1754         .parent    = &tegra_clk_m,
1755         .max_rate  = 1000000000,
1756         .u.pll = {
1757                 .input_min = 2000000,
1758                 .input_max = 31000000,
1759                 .cf_min    = 1000000,
1760                 .cf_max    = 6000000,
1761                 .vco_min   = 20000000,
1762                 .vco_max   = 1200000000,
1763                 .freq_table = tegra_pll_x_freq_table,
1764                 .lock_delay = 300,
1765         },
1766 };
1767
1768 static struct clk_pll_freq_table tegra_pll_e_freq_table[] = {
1769         { 12000000, 100000000,  200,  24, 1, 0 },
1770         { 0, 0, 0, 0, 0, 0 },
1771 };
1772
1773 static struct clk tegra_pll_e = {
1774         .name      = "pll_e",
1775         .flags     = PLL_ALT_MISC_REG,
1776         .ops       = &tegra_plle_ops,
1777         .parent    = &tegra_clk_m,
1778         .reg       = 0xe8,
1779         .max_rate  = 100000000,
1780         .u.pll = {
1781                 .input_min = 12000000,
1782                 .input_max = 12000000,
1783                 .freq_table = tegra_pll_e_freq_table,
1784         },
1785 };
1786
1787 static struct clk tegra_clk_d = {
1788         .name      = "clk_d",
1789         .flags     = PERIPH_NO_RESET,
1790         .ops       = &tegra_clk_double_ops,
1791         .reg       = 0x34,
1792         .reg_shift = 12,
1793         .parent    = &tegra_clk_m,
1794         .max_rate  = 52000000,
1795         .u.periph  = {
1796                 .clk_num = 90,
1797         },
1798 };
1799
1800 /* dap_mclk1, belongs to the cdev1 pingroup. */
1801 static struct clk tegra_dev1_clk = {
1802         .name      = "clk_dev1",
1803         .ops       = &tegra_cdev_clk_ops,
1804         .rate      = 26000000,
1805         .max_rate  = 26000000,
1806         .u.periph  = {
1807                 .clk_num = 94,
1808         },
1809 };
1810
1811 /* dap_mclk2, belongs to the cdev2 pingroup. */
1812 static struct clk tegra_dev2_clk = {
1813         .name      = "clk_dev2",
1814         .ops       = &tegra_cdev_clk_ops,
1815         .rate      = 26000000,
1816         .max_rate  = 26000000,
1817         .u.periph  = {
1818                 .clk_num   = 93,
1819         },
1820 };
1821
1822 /* initialized before peripheral clocks */
1823 static struct clk_mux_sel mux_audio_sync_clk[8+1];
1824 static const struct audio_sources {
1825         const char *name;
1826         int value;
1827 } mux_audio_sync_clk_sources[] = {
1828         { .name = "spdif_in", .value = 0 },
1829         { .name = "i2s1", .value = 1 },
1830         { .name = "i2s2", .value = 2 },
1831         { .name = "pll_a_out0", .value = 4 },
1832 #if 0 /* FIXME: not implemented */
1833         { .name = "ac97", .value = 3 },
1834         { .name = "ext_audio_clk2", .value = 5 },
1835         { .name = "ext_audio_clk1", .value = 6 },
1836         { .name = "ext_vimclk", .value = 7 },
1837 #endif
1838         { 0, 0 }
1839 };
1840
1841 static struct clk tegra_clk_audio = {
1842         .name      = "audio",
1843         .inputs    = mux_audio_sync_clk,
1844         .reg       = 0x38,
1845         .max_rate  = 73728000,
1846         .ops       = &tegra_audio_sync_clk_ops
1847 };
1848
1849 static struct clk tegra_clk_audio_2x = {
1850         .name      = "audio_2x",
1851         .flags     = PERIPH_NO_RESET,
1852         .max_rate  = 48000000,
1853         .ops       = &tegra_clk_double_ops,
1854         .reg       = 0x34,
1855         .reg_shift = 8,
1856         .parent    = &tegra_clk_audio,
1857         .u.periph = {
1858                 .clk_num = 89,
1859         },
1860 };
1861
1862 struct clk_lookup tegra_audio_clk_lookups[] = {
1863         { .con_id = "audio", .clk = &tegra_clk_audio },
1864         { .con_id = "audio_2x", .clk = &tegra_clk_audio_2x }
1865 };
1866
1867 /* This is called after peripheral clocks are initialized, as the
1868  * audio_sync clock depends on some of the peripheral clocks.
1869  */
1870
1871 static void init_audio_sync_clock_mux(void)
1872 {
1873         int i;
1874         struct clk_mux_sel *sel = mux_audio_sync_clk;
1875         const struct audio_sources *src = mux_audio_sync_clk_sources;
1876         struct clk_lookup *lookup;
1877
1878         for (i = 0; src->name; i++, sel++, src++) {
1879                 sel->input = tegra_get_clock_by_name(src->name);
1880                 if (!sel->input)
1881                         pr_err("%s: could not find clk %s\n", __func__,
1882                                 src->name);
1883                 sel->value = src->value;
1884         }
1885
1886         lookup = tegra_audio_clk_lookups;
1887         for (i = 0; i < ARRAY_SIZE(tegra_audio_clk_lookups); i++, lookup++) {
1888                 clk_init(lookup->clk);
1889                 clkdev_add(lookup);
1890         }
1891 }
1892
1893 static struct clk_mux_sel mux_cclk[] = {
1894         { .input = &tegra_clk_m,        .value = 0},
1895         { .input = &tegra_pll_c,        .value = 1},
1896         { .input = &tegra_clk_32k,      .value = 2},
1897         { .input = &tegra_pll_m,        .value = 3},
1898         { .input = &tegra_pll_p,        .value = 4},
1899         { .input = &tegra_pll_p_out4,   .value = 5},
1900         { .input = &tegra_pll_p_out3,   .value = 6},
1901         { .input = &tegra_clk_d,        .value = 7},
1902         { .input = &tegra_pll_x,        .value = 8},
1903         { 0, 0},
1904 };
1905
1906 static struct clk_mux_sel mux_sclk[] = {
1907         { .input = &tegra_clk_m,        .value = 0},
1908         { .input = &tegra_pll_c_out1,   .value = 1},
1909         { .input = &tegra_pll_p_out4,   .value = 2},
1910         { .input = &tegra_pll_p_out3,   .value = 3},
1911         { .input = &tegra_pll_p_out2,   .value = 4},
1912         { .input = &tegra_clk_d,        .value = 5},
1913         { .input = &tegra_clk_32k,      .value = 6},
1914         { .input = &tegra_pll_m_out1,   .value = 7},
1915         { 0, 0},
1916 };
1917
1918 static struct clk tegra_clk_cclk = {
1919         .name   = "cclk",
1920         .inputs = mux_cclk,
1921         .reg    = 0x20,
1922         .ops    = &tegra_super_ops,
1923         .max_rate = 1000000000,
1924 };
1925
1926 static struct clk tegra_clk_sclk = {
1927         .name   = "sclk",
1928         .inputs = mux_sclk,
1929         .reg    = 0x28,
1930         .ops    = &tegra_super_ops,
1931         .max_rate = 240000000,
1932         .min_rate = 120000000,
1933 };
1934
1935 static struct clk tegra_clk_virtual_cpu = {
1936         .name      = "cpu",
1937         .parent    = &tegra_clk_cclk,
1938         .ops       = &tegra_cpu_ops,
1939         .max_rate  = 1000000000,
1940         .u.cpu = {
1941                 .main      = &tegra_pll_x,
1942                 .backup    = &tegra_pll_p,
1943         },
1944 };
1945
1946 static struct clk tegra_clk_cop = {
1947         .name      = "cop",
1948         .parent    = &tegra_clk_sclk,
1949         .ops       = &tegra_cop_ops,
1950         .max_rate  = 240000000,
1951 };
1952
1953 static struct clk tegra_clk_hclk = {
1954         .name           = "hclk",
1955         .flags          = DIV_BUS,
1956         .parent         = &tegra_clk_sclk,
1957         .reg            = 0x30,
1958         .reg_shift      = 4,
1959         .ops            = &tegra_bus_ops,
1960         .max_rate       = 240000000,
1961 };
1962
1963 static struct clk tegra_clk_pclk = {
1964         .name           = "pclk",
1965         .flags          = DIV_BUS,
1966         .parent         = &tegra_clk_hclk,
1967         .reg            = 0x30,
1968         .reg_shift      = 0,
1969         .ops            = &tegra_bus_ops,
1970         .max_rate       = 120000000,
1971 };
1972
1973 static struct clk tegra_clk_blink = {
1974         .name           = "blink",
1975         .parent         = &tegra_clk_32k,
1976         .reg            = 0x40,
1977         .ops            = &tegra_blink_clk_ops,
1978         .max_rate       = 32768,
1979 };
1980
1981 static struct clk_mux_sel mux_pllm_pllc_pllp_plla[] = {
1982         { .input = &tegra_pll_m, .value = 0},
1983         { .input = &tegra_pll_c, .value = 1},
1984         { .input = &tegra_pll_p, .value = 2},
1985         { .input = &tegra_pll_a_out0, .value = 3},
1986         { 0, 0},
1987 };
1988
1989 static struct clk_mux_sel mux_pllm_pllc_pllp_clkm[] = {
1990         { .input = &tegra_pll_m, .value = 0},
1991         { .input = &tegra_pll_c, .value = 1},
1992         { .input = &tegra_pll_p, .value = 2},
1993         { .input = &tegra_clk_m, .value = 3},
1994         { 0, 0},
1995 };
1996
1997 static struct clk_mux_sel mux_pllp_pllc_pllm_clkm[] = {
1998         { .input = &tegra_pll_p, .value = 0},
1999         { .input = &tegra_pll_c, .value = 1},
2000         { .input = &tegra_pll_m, .value = 2},
2001         { .input = &tegra_clk_m, .value = 3},
2002         { 0, 0},
2003 };
2004
2005 static struct clk_mux_sel mux_pllaout0_audio2x_pllp_clkm[] = {
2006         {.input = &tegra_pll_a_out0, .value = 0},
2007         {.input = &tegra_clk_audio_2x, .value = 1},
2008         {.input = &tegra_pll_p, .value = 2},
2009         {.input = &tegra_clk_m, .value = 3},
2010         { 0, 0},
2011 };
2012
2013 static struct clk_mux_sel mux_pllp_plld_pllc_clkm[] = {
2014         {.input = &tegra_pll_p, .value = 0},
2015         {.input = &tegra_pll_d_out0, .value = 1},
2016         {.input = &tegra_pll_c, .value = 2},
2017         {.input = &tegra_clk_m, .value = 3},
2018         { 0, 0},
2019 };
2020
2021 static struct clk_mux_sel mux_pllp_pllc_audio_clkm_clk32[] = {
2022         {.input = &tegra_pll_p,     .value = 0},
2023         {.input = &tegra_pll_c,     .value = 1},
2024         {.input = &tegra_clk_audio,     .value = 2},
2025         {.input = &tegra_clk_m,     .value = 3},
2026         {.input = &tegra_clk_32k,   .value = 4},
2027         { 0, 0},
2028 };
2029
2030 static struct clk_mux_sel mux_pllp_pllc_pllm[] = {
2031         {.input = &tegra_pll_p,     .value = 0},
2032         {.input = &tegra_pll_c,     .value = 1},
2033         {.input = &tegra_pll_m,     .value = 2},
2034         { 0, 0},
2035 };
2036
2037 static struct clk_mux_sel mux_clk_m[] = {
2038         { .input = &tegra_clk_m, .value = 0},
2039         { 0, 0},
2040 };
2041
2042 static struct clk_mux_sel mux_pllp_out3[] = {
2043         { .input = &tegra_pll_p_out3, .value = 0},
2044         { 0, 0},
2045 };
2046
2047 static struct clk_mux_sel mux_plld[] = {
2048         { .input = &tegra_pll_d, .value = 0},
2049         { 0, 0},
2050 };
2051
2052 static struct clk_mux_sel mux_clk_32k[] = {
2053         { .input = &tegra_clk_32k, .value = 0},
2054         { 0, 0},
2055 };
2056
2057 static struct clk_mux_sel mux_pclk[] = {
2058         { .input = &tegra_clk_pclk, .value = 0},
2059         { 0, 0},
2060 };
2061
2062 static struct clk tegra_clk_emc = {
2063         .name = "emc",
2064         .ops = &tegra_emc_clk_ops,
2065         .reg = 0x19c,
2066         .max_rate = 800000000,
2067         .inputs = mux_pllm_pllc_pllp_clkm,
2068         .flags = MUX | DIV_U71 | PERIPH_EMC_ENB,
2069         .u.periph = {
2070                 .clk_num = 57,
2071         },
2072 };
2073
2074 #define PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
2075         {                                               \
2076                 .name      = _name,                     \
2077                 .lookup    = {                          \
2078                         .dev_id    = _dev,              \
2079                         .con_id    = _con,              \
2080                 },                                      \
2081                 .ops       = &tegra_periph_clk_ops,     \
2082                 .reg       = _reg,                      \
2083                 .inputs    = _inputs,                   \
2084                 .flags     = _flags,                    \
2085                 .max_rate  = _max,                      \
2086                 .u.periph = {                           \
2087                         .clk_num   = _clk_num,          \
2088                 },                                      \
2089         }
2090
2091 #define SHARED_CLK(_name, _dev, _con, _parent)          \
2092         {                                               \
2093                 .name      = _name,                     \
2094                 .lookup    = {                          \
2095                         .dev_id    = _dev,              \
2096                         .con_id    = _con,              \
2097                 },                                      \
2098                 .ops       = &tegra_clk_shared_bus_ops, \
2099                 .parent = _parent,                      \
2100         }
2101
2102 struct clk tegra_list_clks[] = {
2103         PERIPH_CLK("apbdma",    "tegra-dma",            NULL,   34,     0,      108000000, mux_pclk,                    0),
2104         PERIPH_CLK("rtc",       "rtc-tegra",            NULL,   4,      0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET),
2105         PERIPH_CLK("timer",     "timer",                NULL,   5,      0,      26000000,  mux_clk_m,                   0),
2106         PERIPH_CLK("i2s1",      "i2s.0",                NULL,   11,     0x100,  26000000,  mux_pllaout0_audio2x_pllp_clkm,      MUX | DIV_U71),
2107         PERIPH_CLK("i2s2",      "i2s.1",                NULL,   18,     0x104,  26000000,  mux_pllaout0_audio2x_pllp_clkm,      MUX | DIV_U71),
2108         PERIPH_CLK("spdif_out", "spdif_out",            NULL,   10,     0x108,  100000000, mux_pllaout0_audio2x_pllp_clkm,      MUX | DIV_U71),
2109         PERIPH_CLK("spdif_in",  "spdif_in",             NULL,   10,     0x10c,  100000000, mux_pllp_pllc_pllm,          MUX | DIV_U71),
2110         PERIPH_CLK("pwm",       "pwm",                  NULL,   17,     0x110,  432000000, mux_pllp_pllc_audio_clkm_clk32,      MUX | DIV_U71),
2111         PERIPH_CLK("spi",       "spi",                  NULL,   43,     0x114,  40000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2112         PERIPH_CLK("xio",       "xio",                  NULL,   45,     0x120,  150000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2113         PERIPH_CLK("twc",       "twc",                  NULL,   16,     0x12c,  150000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2114         PERIPH_CLK("sbc1",      "spi_tegra.0",          NULL,   41,     0x134,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2115         PERIPH_CLK("sbc2",      "spi_tegra.1",          NULL,   44,     0x118,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2116         PERIPH_CLK("sbc3",      "spi_tegra.2",          NULL,   46,     0x11c,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2117         PERIPH_CLK("sbc4",      "spi_tegra.3",          NULL,   68,     0x1b4,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2118         PERIPH_CLK("ide",       "ide",                  NULL,   25,     0x144,  100000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* requires min voltage */
2119         PERIPH_CLK("ndflash",   "tegra_nand",           NULL,   13,     0x160,  164000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
2120         PERIPH_CLK("vfir",      "vfir",                 NULL,   7,      0x168,  72000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2121         PERIPH_CLK("sdmmc1",    "sdhci-tegra.0",        NULL,   14,     0x150,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
2122         PERIPH_CLK("sdmmc2",    "sdhci-tegra.1",        NULL,   9,      0x154,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
2123         PERIPH_CLK("sdmmc3",    "sdhci-tegra.2",        NULL,   69,     0x1bc,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
2124         PERIPH_CLK("sdmmc4",    "sdhci-tegra.3",        NULL,   15,     0x164,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
2125         PERIPH_CLK("vcp",       "tegra-avp",            "vcp",  29,     0,      250000000, mux_clk_m,                   0),
2126         PERIPH_CLK("bsea",      "tegra-avp",            "bsea", 62,     0,      250000000, mux_clk_m,                   0),
2127         PERIPH_CLK("bsev",      "tegra-aes",            "bsev", 63,     0,      250000000, mux_clk_m,                   0),
2128         PERIPH_CLK("vde",       "tegra-avp",            "vde",  61,     0x1c8,  250000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage and process_id */
2129         PERIPH_CLK("csite",     "csite",                NULL,   73,     0x1d4,  144000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* max rate ??? */
2130         /* FIXME: what is la? */
2131         PERIPH_CLK("la",        "la",                   NULL,   76,     0x1f8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2132         PERIPH_CLK("owr",       "tegra_w1",             NULL,   71,     0x1cc,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
2133         PERIPH_CLK("nor",       "nor",                  NULL,   42,     0x1d0,  92000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* requires min voltage */
2134         PERIPH_CLK("mipi",      "mipi",                 NULL,   50,     0x174,  60000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
2135         PERIPH_CLK("i2c1",      "tegra-i2c.0",          NULL,   12,     0x124,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
2136         PERIPH_CLK("i2c2",      "tegra-i2c.1",          NULL,   54,     0x198,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
2137         PERIPH_CLK("i2c3",      "tegra-i2c.2",          NULL,   67,     0x1b8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
2138         PERIPH_CLK("dvc",       "tegra-i2c.3",          NULL,   47,     0x128,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
2139         PERIPH_CLK("i2c1_i2c",  "tegra-i2c.0",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
2140         PERIPH_CLK("i2c2_i2c",  "tegra-i2c.1",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
2141         PERIPH_CLK("i2c3_i2c",  "tegra-i2c.2",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
2142         PERIPH_CLK("dvc_i2c",   "tegra-i2c.3",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
2143         PERIPH_CLK("uarta",     "uart.0",               NULL,   6,      0x178,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2144         PERIPH_CLK("uartb",     "uart.1",               NULL,   7,      0x17c,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2145         PERIPH_CLK("uartc",     "uart.2",               NULL,   55,     0x1a0,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2146         PERIPH_CLK("uartd",     "uart.3",               NULL,   65,     0x1c0,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2147         PERIPH_CLK("uarte",     "uart.4",               NULL,   66,     0x1c4,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2148         PERIPH_CLK("3d",        "3d",                   NULL,   24,     0x158,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_MANUAL_RESET), /* scales with voltage and process_id */
2149         PERIPH_CLK("2d",        "2d",                   NULL,   21,     0x15c,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2150         PERIPH_CLK("vi",        "tegra_camera",         "vi",   20,     0x148,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2151         PERIPH_CLK("vi_sensor", "tegra_camera",         "vi_sensor",    20,     0x1a8,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET), /* scales with voltage and process_id */
2152         PERIPH_CLK("epp",       "epp",                  NULL,   19,     0x16c,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2153         PERIPH_CLK("mpe",       "mpe",                  NULL,   60,     0x170,  250000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2154         PERIPH_CLK("host1x",    "host1x",               NULL,   28,     0x180,  166000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2155         PERIPH_CLK("cve",       "cve",                  NULL,   49,     0x140,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2156         PERIPH_CLK("tvo",       "tvo",                  NULL,   49,     0x188,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2157         PERIPH_CLK("hdmi",      "hdmi",                 NULL,   51,     0x18c,  600000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2158         PERIPH_CLK("tvdac",     "tvdac",                NULL,   53,     0x194,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2159         PERIPH_CLK("disp1",     "tegradc.0",            NULL,   27,     0x138,  600000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* scales with voltage and process_id */
2160         PERIPH_CLK("disp2",     "tegradc.1",            NULL,   26,     0x13c,  600000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* scales with voltage and process_id */
2161         PERIPH_CLK("usbd",      "fsl-tegra-udc",        NULL,   22,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
2162         PERIPH_CLK("usb2",      "tegra-ehci.1",         NULL,   58,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
2163         PERIPH_CLK("usb3",      "tegra-ehci.2",         NULL,   59,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
2164         PERIPH_CLK("dsi",       "dsi",                  NULL,   48,     0,      500000000, mux_plld,                    0), /* scales with voltage */
2165         PERIPH_CLK("csi",       "tegra_camera",         "csi",  52,     0,      72000000,  mux_pllp_out3,               0),
2166         PERIPH_CLK("isp",       "tegra_camera",         "isp",  23,     0,      150000000, mux_clk_m,                   0), /* same frequency as VI */
2167         PERIPH_CLK("csus",      "tegra_camera",         "csus", 92,     0,      150000000, mux_clk_m,                   PERIPH_NO_RESET),
2168         PERIPH_CLK("pex",       NULL,                   "pex",  70,     0,      26000000,  mux_clk_m,                   PERIPH_MANUAL_RESET),
2169         PERIPH_CLK("afi",       NULL,                   "afi",  72,     0,      26000000,  mux_clk_m,                   PERIPH_MANUAL_RESET),
2170         PERIPH_CLK("pcie_xclk", NULL,             "pcie_xclk",  74,     0,      26000000,  mux_clk_m,                   PERIPH_MANUAL_RESET),
2171
2172         SHARED_CLK("avp.sclk",  "tegra-avp",            "sclk", &tegra_clk_sclk),
2173         SHARED_CLK("avp.emc",   "tegra-avp",            "emc",  &tegra_clk_emc),
2174         SHARED_CLK("cpu.emc",   "cpu",                  "emc",  &tegra_clk_emc),
2175         SHARED_CLK("disp1.emc", "tegradc.0",            "emc",  &tegra_clk_emc),
2176         SHARED_CLK("disp2.emc", "tegradc.1",            "emc",  &tegra_clk_emc),
2177         SHARED_CLK("hdmi.emc",  "hdmi",                 "emc",  &tegra_clk_emc),
2178         SHARED_CLK("host.emc",  "tegra_grhost",         "emc",  &tegra_clk_emc),
2179         SHARED_CLK("usbd.emc",  "fsl-tegra-udc",        "emc",  &tegra_clk_emc),
2180         SHARED_CLK("usb1.emc",  "tegra-ehci.0",         "emc",  &tegra_clk_emc),
2181         SHARED_CLK("usb2.emc",  "tegra-ehci.1",         "emc",  &tegra_clk_emc),
2182         SHARED_CLK("usb3.emc",  "tegra-ehci.2",         "emc",  &tegra_clk_emc),
2183 };
2184
2185 #define CLK_DUPLICATE(_name, _dev, _con)                \
2186         {                                               \
2187                 .name   = _name,                        \
2188                 .lookup = {                             \
2189                         .dev_id = _dev,                 \
2190                         .con_id         = _con,         \
2191                 },                                      \
2192         }
2193
2194 /* Some clocks may be used by different drivers depending on the board
2195  * configuration.  List those here to register them twice in the clock lookup
2196  * table under two names.
2197  */
2198 struct clk_duplicate tegra_clk_duplicates[] = {
2199         CLK_DUPLICATE("uarta",  "tegra_uart.0", NULL),
2200         CLK_DUPLICATE("uartb",  "tegra_uart.1", NULL),
2201         CLK_DUPLICATE("uartc",  "tegra_uart.2", NULL),
2202         CLK_DUPLICATE("uartd",  "tegra_uart.3", NULL),
2203         CLK_DUPLICATE("uarte",  "tegra_uart.4", NULL),
2204         CLK_DUPLICATE("usbd", "utmip-pad", NULL),
2205         CLK_DUPLICATE("usbd", "tegra-ehci.0", NULL),
2206         CLK_DUPLICATE("usbd", "tegra-otg", NULL),
2207         CLK_DUPLICATE("hdmi", "tegradc.0", "hdmi"),
2208         CLK_DUPLICATE("hdmi", "tegradc.1", "hdmi"),
2209         CLK_DUPLICATE("pwm", "tegra_pwm.0", NULL),
2210         CLK_DUPLICATE("pwm", "tegra_pwm.1", NULL),
2211         CLK_DUPLICATE("pwm", "tegra_pwm.2", NULL),
2212         CLK_DUPLICATE("pwm", "tegra_pwm.3", NULL),
2213         CLK_DUPLICATE("host1x", "tegra_grhost", "host1x"),
2214         CLK_DUPLICATE("2d", "tegra_grhost", "gr2d"),
2215         CLK_DUPLICATE("3d", "tegra_grhost", "gr3d"),
2216         CLK_DUPLICATE("epp", "tegra_grhost", "epp"),
2217         CLK_DUPLICATE("mpe", "tegra_grhost", "mpe"),
2218         CLK_DUPLICATE("cop", "tegra-avp", "cop"),
2219         CLK_DUPLICATE("vde", "tegra-aes", "vde"),
2220 };
2221
2222 #define CLK(dev, con, ck)       \
2223         {                       \
2224                 .dev_id = dev,  \
2225                 .con_id = con,  \
2226                 .clk = ck,      \
2227         }
2228
2229 struct clk *tegra_ptr_clks[] = {
2230         &tegra_clk_32k,
2231         &tegra_pll_s,
2232         &tegra_clk_m,
2233         &tegra_pll_m,
2234         &tegra_pll_m_out1,
2235         &tegra_pll_c,
2236         &tegra_pll_c_out1,
2237         &tegra_pll_p,
2238         &tegra_pll_p_out1,
2239         &tegra_pll_p_out2,
2240         &tegra_pll_p_out3,
2241         &tegra_pll_p_out4,
2242         &tegra_pll_a,
2243         &tegra_pll_a_out0,
2244         &tegra_pll_d,
2245         &tegra_pll_d_out0,
2246         &tegra_pll_u,
2247         &tegra_pll_x,
2248         &tegra_pll_e,
2249         &tegra_clk_cclk,
2250         &tegra_clk_sclk,
2251         &tegra_clk_hclk,
2252         &tegra_clk_pclk,
2253         &tegra_clk_d,
2254         &tegra_dev1_clk,
2255         &tegra_dev2_clk,
2256         &tegra_clk_virtual_cpu,
2257         &tegra_clk_blink,
2258         &tegra_clk_cop,
2259         &tegra_clk_emc,
2260 };
2261
2262 static void tegra2_init_one_clock(struct clk *c)
2263 {
2264         clk_init(c);
2265         INIT_LIST_HEAD(&c->shared_bus_list);
2266         if (!c->lookup.dev_id && !c->lookup.con_id)
2267                 c->lookup.con_id = c->name;
2268         c->lookup.clk = c;
2269         clkdev_add(&c->lookup);
2270 }
2271
2272 void __init tegra2_init_clocks(void)
2273 {
2274         int i;
2275         struct clk *c;
2276
2277         for (i = 0; i < ARRAY_SIZE(tegra_ptr_clks); i++)
2278                 tegra2_init_one_clock(tegra_ptr_clks[i]);
2279
2280         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
2281                 tegra2_init_one_clock(&tegra_list_clks[i]);
2282
2283         for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
2284                 c = tegra_get_clock_by_name(tegra_clk_duplicates[i].name);
2285                 if (!c) {
2286                         pr_err("%s: Unknown duplicate clock %s\n", __func__,
2287                                 tegra_clk_duplicates[i].name);
2288                         continue;
2289                 }
2290
2291                 tegra_clk_duplicates[i].lookup.clk = c;
2292                 clkdev_add(&tegra_clk_duplicates[i].lookup);
2293         }
2294
2295         init_audio_sync_clock_mux();
2296 }
2297
2298 #ifdef CONFIG_PM
2299 static u32 clk_rst_suspend[RST_DEVICES_NUM + CLK_OUT_ENB_NUM +
2300                            PERIPH_CLK_SOURCE_NUM + 22];
2301
2302 void tegra_clk_suspend(void)
2303 {
2304         unsigned long off, i;
2305         u32 *ctx = clk_rst_suspend;
2306
2307         *ctx++ = clk_readl(OSC_CTRL) & OSC_CTRL_MASK;
2308         *ctx++ = clk_readl(tegra_pll_c.reg + PLL_BASE);
2309         *ctx++ = clk_readl(tegra_pll_c.reg + PLL_MISC(&tegra_pll_c));
2310         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_BASE);
2311         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
2312         *ctx++ = clk_readl(tegra_pll_s.reg + PLL_BASE);
2313         *ctx++ = clk_readl(tegra_pll_s.reg + PLL_MISC(&tegra_pll_s));
2314         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_BASE);
2315         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
2316         *ctx++ = clk_readl(tegra_pll_u.reg + PLL_BASE);
2317         *ctx++ = clk_readl(tegra_pll_u.reg + PLL_MISC(&tegra_pll_u));
2318
2319         *ctx++ = clk_readl(tegra_pll_m_out1.reg);
2320         *ctx++ = clk_readl(tegra_pll_a_out0.reg);
2321         *ctx++ = clk_readl(tegra_pll_c_out1.reg);
2322
2323         *ctx++ = clk_readl(tegra_clk_cclk.reg);
2324         *ctx++ = clk_readl(tegra_clk_cclk.reg + SUPER_CLK_DIVIDER);
2325
2326         *ctx++ = clk_readl(tegra_clk_sclk.reg);
2327         *ctx++ = clk_readl(tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
2328         *ctx++ = clk_readl(tegra_clk_pclk.reg);
2329
2330         *ctx++ = clk_readl(tegra_clk_audio.reg);
2331
2332         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_OSC;
2333                         off += 4) {
2334                 if (off == PERIPH_CLK_SOURCE_EMC)
2335                         continue;
2336                 *ctx++ = clk_readl(off);
2337         }
2338
2339         off = RST_DEVICES;
2340         for (i = 0; i < RST_DEVICES_NUM; i++, off += 4)
2341                 *ctx++ = clk_readl(off);
2342
2343         off = CLK_OUT_ENB;
2344         for (i = 0; i < CLK_OUT_ENB_NUM; i++, off += 4)
2345                 *ctx++ = clk_readl(off);
2346
2347         *ctx++ = clk_readl(MISC_CLK_ENB);
2348         *ctx++ = clk_readl(CLK_MASK_ARM);
2349
2350         BUG_ON(ctx - clk_rst_suspend != ARRAY_SIZE(clk_rst_suspend));
2351 }
2352
2353 void tegra_clk_resume(void)
2354 {
2355         unsigned long off, i;
2356         const u32 *ctx = clk_rst_suspend;
2357         u32 val;
2358
2359         val = clk_readl(OSC_CTRL) & ~OSC_CTRL_MASK;
2360         val |= *ctx++;
2361         clk_writel(val, OSC_CTRL);
2362
2363         clk_writel(*ctx++, tegra_pll_c.reg + PLL_BASE);
2364         clk_writel(*ctx++, tegra_pll_c.reg + PLL_MISC(&tegra_pll_c));
2365         clk_writel(*ctx++, tegra_pll_a.reg + PLL_BASE);
2366         clk_writel(*ctx++, tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
2367         clk_writel(*ctx++, tegra_pll_s.reg + PLL_BASE);
2368         clk_writel(*ctx++, tegra_pll_s.reg + PLL_MISC(&tegra_pll_s));
2369         clk_writel(*ctx++, tegra_pll_d.reg + PLL_BASE);
2370         clk_writel(*ctx++, tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
2371         clk_writel(*ctx++, tegra_pll_u.reg + PLL_BASE);
2372         clk_writel(*ctx++, tegra_pll_u.reg + PLL_MISC(&tegra_pll_u));
2373         udelay(1000);
2374
2375         clk_writel(*ctx++, tegra_pll_m_out1.reg);
2376         clk_writel(*ctx++, tegra_pll_a_out0.reg);
2377         clk_writel(*ctx++, tegra_pll_c_out1.reg);
2378
2379         clk_writel(*ctx++, tegra_clk_cclk.reg);
2380         clk_writel(*ctx++, tegra_clk_cclk.reg + SUPER_CLK_DIVIDER);
2381
2382         clk_writel(*ctx++, tegra_clk_sclk.reg);
2383         clk_writel(*ctx++, tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
2384         clk_writel(*ctx++, tegra_clk_pclk.reg);
2385
2386         clk_writel(*ctx++, tegra_clk_audio.reg);
2387
2388         /* enable all clocks before configuring clock sources */
2389         clk_writel(0xbffffff9ul, CLK_OUT_ENB);
2390         clk_writel(0xfefffff7ul, CLK_OUT_ENB + 4);
2391         clk_writel(0x77f01bfful, CLK_OUT_ENB + 8);
2392         wmb();
2393
2394         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_OSC;
2395                         off += 4) {
2396                 if (off == PERIPH_CLK_SOURCE_EMC)
2397                         continue;
2398                 clk_writel(*ctx++, off);
2399         }
2400         wmb();
2401
2402         off = RST_DEVICES;
2403         for (i = 0; i < RST_DEVICES_NUM; i++, off += 4)
2404                 clk_writel(*ctx++, off);
2405         wmb();
2406
2407         off = CLK_OUT_ENB;
2408         for (i = 0; i < CLK_OUT_ENB_NUM; i++, off += 4)
2409                 clk_writel(*ctx++, off);
2410         wmb();
2411
2412         clk_writel(*ctx++, MISC_CLK_ENB);
2413         clk_writel(*ctx++, CLK_MASK_ARM);
2414 }
2415 #endif