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