]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - arch/arm/mach-tegra/tegra12_clocks.c
b96c2a91216adf21bc8847fab752c8178c3b879e
[sojka/nv-tegra/linux-3.10.git] / arch / arm / mach-tegra / tegra12_clocks.c
1 /*
2  * arch/arm/mach-tegra/tegra12_clocks.c
3  *
4  * Copyright (C) 2011-2014 NVIDIA Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/clk.h>
29 #include <linux/cpufreq.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/platform_device.h>
32 #include <linux/tegra-soc.h>
33 #include <linux/tegra-fuse.h>
34
35 #include <asm/clkdev.h>
36
37 #include <mach/edp.h>
38 #include <mach/mc.h>
39
40 #include "clock.h"
41 #include "dvfs.h"
42 #include "iomap.h"
43 #include "pm.h"
44 #include "sleep.h"
45 #include "devices.h"
46 #include "tegra12_emc.h"
47 #include "tegra_cl_dvfs.h"
48 #include "cpu-tegra.h"
49 #include "tegra11_soctherm.h"
50
51 #define RST_DEVICES_L                   0x004
52 #define RST_DEVICES_H                   0x008
53 #define RST_DEVICES_U                   0x00C
54 #define RST_DEVICES_V                   0x358
55 #define RST_DEVICES_W                   0x35C
56 #define RST_DEVICES_X                   0x28C
57 #define RST_DEVICES_SET_L               0x300
58 #define RST_DEVICES_CLR_L               0x304
59 #define RST_DEVICES_SET_V               0x430
60 #define RST_DEVICES_CLR_V               0x434
61 #define RST_DEVICES_SET_X               0x290
62 #define RST_DEVICES_CLR_X               0x294
63 #define RST_DEVICES_NUM                 6
64
65 #define CLK_OUT_ENB_L                   0x010
66 #define CLK_OUT_ENB_H                   0x014
67 #define CLK_OUT_ENB_U                   0x018
68 #define CLK_OUT_ENB_V                   0x360
69 #define CLK_OUT_ENB_W                   0x364
70 #define CLK_OUT_ENB_X                   0x280
71 #define CLK_OUT_ENB_SET_L               0x320
72 #define CLK_OUT_ENB_CLR_L               0x324
73 #define CLK_OUT_ENB_SET_V               0x440
74 #define CLK_OUT_ENB_CLR_V               0x444
75 #define CLK_OUT_ENB_SET_X               0x284
76 #define CLK_OUT_ENB_CLR_X               0x288
77 #define CLK_OUT_ENB_NUM                 6
78
79 #define CLK_OUT_ENB_L_RESET_MASK        0xfcd7dff1
80 #define CLK_OUT_ENB_H_RESET_MASK        0xefddfff7
81 #define CLK_OUT_ENB_U_RESET_MASK        0xfbfefbfa
82 #define CLK_OUT_ENB_V_RESET_MASK        0xffc1fffb
83 #define CLK_OUT_ENB_W_RESET_MASK        0x3f7fbfff
84 #define CLK_OUT_ENB_X_RESET_MASK        0x00170979
85
86 #define RST_DEVICES_V_SWR_CPULP_RST_DIS (0x1 << 1) /* Reserved on Tegra11 */
87 #define CLK_OUT_ENB_V_CLK_ENB_CPULP_EN  (0x1 << 1)
88
89 #define PERIPH_CLK_TO_BIT(c)            (1 << (c->u.periph.clk_num % 32))
90 #define PERIPH_CLK_TO_RST_REG(c)        \
91         periph_clk_to_reg((c), RST_DEVICES_L, RST_DEVICES_V, RST_DEVICES_X, 4)
92 #define PERIPH_CLK_TO_RST_SET_REG(c)    \
93         periph_clk_to_reg((c), RST_DEVICES_SET_L, RST_DEVICES_SET_V, \
94                 RST_DEVICES_SET_X, 8)
95 #define PERIPH_CLK_TO_RST_CLR_REG(c)    \
96         periph_clk_to_reg((c), RST_DEVICES_CLR_L, RST_DEVICES_CLR_V, \
97                 RST_DEVICES_CLR_X, 8)
98
99 #define PERIPH_CLK_TO_ENB_REG(c)        \
100         periph_clk_to_reg((c), CLK_OUT_ENB_L, CLK_OUT_ENB_V, CLK_OUT_ENB_X, 4)
101 #define PERIPH_CLK_TO_ENB_SET_REG(c)    \
102         periph_clk_to_reg((c), CLK_OUT_ENB_SET_L, CLK_OUT_ENB_SET_V, \
103                 CLK_OUT_ENB_SET_X, 8)
104 #define PERIPH_CLK_TO_ENB_CLR_REG(c)    \
105         periph_clk_to_reg((c), CLK_OUT_ENB_CLR_L, CLK_OUT_ENB_CLR_V, \
106                 CLK_OUT_ENB_CLR_X, 8)
107
108 #define IS_PERIPH_IN_RESET(c)           \
109         (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
110
111 #define CLK_MASK_ARM                    0x44
112 #define MISC_CLK_ENB                    0x48
113
114 #define OSC_CTRL                        0x50
115 #define OSC_CTRL_OSC_FREQ_MASK          (0xF<<28)
116 #define OSC_CTRL_OSC_FREQ_13MHZ         (0x0<<28)
117 #define OSC_CTRL_OSC_FREQ_19_2MHZ       (0x4<<28)
118 #define OSC_CTRL_OSC_FREQ_12MHZ         (0x8<<28)
119 #define OSC_CTRL_OSC_FREQ_26MHZ         (0xC<<28)
120 #define OSC_CTRL_OSC_FREQ_16_8MHZ       (0x1<<28)
121 #define OSC_CTRL_OSC_FREQ_38_4MHZ       (0x5<<28)
122 #define OSC_CTRL_OSC_FREQ_48MHZ         (0x9<<28)
123 #define OSC_CTRL_MASK                   (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
124
125 #define OSC_CTRL_PLL_REF_DIV_MASK       (3<<26)
126 #define OSC_CTRL_PLL_REF_DIV_1          (0<<26)
127 #define OSC_CTRL_PLL_REF_DIV_2          (1<<26)
128 #define OSC_CTRL_PLL_REF_DIV_4          (2<<26)
129
130 #define PERIPH_CLK_SOURCE_I2S1          0x100
131 #define PERIPH_CLK_SOURCE_EMC           0x19c
132 #define PERIPH_CLK_SOURCE_EMC_MC_SAME   (1<<16)
133
134 #define PERIPH_CLK_SOURCE_LA            0x1f8
135 #define PERIPH_CLK_SOURCE_NUM1 \
136         ((PERIPH_CLK_SOURCE_LA - PERIPH_CLK_SOURCE_I2S1) / 4)
137
138 #define PERIPH_CLK_SOURCE_MSELECT       0x3b4
139 #define PERIPH_CLK_SOURCE_SE            0x42c
140 #define PERIPH_CLK_SOURCE_NUM2 \
141         ((PERIPH_CLK_SOURCE_SE - PERIPH_CLK_SOURCE_MSELECT) / 4 + 1)
142
143 #define AUDIO_DLY_CLK                   0x49c
144 #define AUDIO_SYNC_CLK_SPDIF            0x4b4
145 #define PERIPH_CLK_SOURCE_NUM3 \
146         ((AUDIO_SYNC_CLK_SPDIF - AUDIO_DLY_CLK) / 4 + 1)
147
148 #define SPARE_REG                       0x55c
149 #define SPARE_REG_CLK_M_DIVISOR_SHIFT   2
150 #define SPARE_REG_CLK_M_DIVISOR_MASK    (3 << SPARE_REG_CLK_M_DIVISOR_SHIFT)
151
152 #define PERIPH_CLK_SOURCE_XUSB_HOST     0x600
153 #define PERIPH_CLK_SOURCE_VIC           0x678
154 #define PERIPH_CLK_SOURCE_NUM4 \
155         ((PERIPH_CLK_SOURCE_VIC - PERIPH_CLK_SOURCE_XUSB_HOST) / 4 + 1)
156
157 #define PERIPH_CLK_SOURCE_NUM           (PERIPH_CLK_SOURCE_NUM1 + \
158                                          PERIPH_CLK_SOURCE_NUM2 + \
159                                          PERIPH_CLK_SOURCE_NUM3 + \
160                                          PERIPH_CLK_SOURCE_NUM4)
161
162 #define CPU_SOFTRST_CTRL                0x380
163 #define CPU_SOFTRST_CTRL1               0x384
164 #define CPU_SOFTRST_CTRL2               0x388
165
166 #define PERIPH_CLK_SOURCE_DIVU71_MASK   0xFF
167 #define PERIPH_CLK_SOURCE_DIVU16_MASK   0xFFFF
168 #define PERIPH_CLK_SOURCE_DIV_SHIFT     0
169 #define PERIPH_CLK_SOURCE_DIVIDLE_SHIFT 8
170 #define PERIPH_CLK_SOURCE_DIVIDLE_VAL   50
171 #define PERIPH_CLK_UART_DIV_ENB         (1<<24)
172 #define PERIPH_CLK_VI_SEL_EX_SHIFT      24
173 #define PERIPH_CLK_VI_SEL_EX_MASK       (0x3<<PERIPH_CLK_VI_SEL_EX_SHIFT)
174 #define PERIPH_CLK_NAND_DIV_EX_ENB      (1<<8)
175 #define PERIPH_CLK_DTV_POLARITY_INV     (1<<25)
176
177 #define AUDIO_SYNC_SOURCE_MASK          0x0F
178 #define AUDIO_SYNC_DISABLE_BIT          0x10
179 #define AUDIO_SYNC_TAP_NIBBLE_SHIFT(c)  ((c->reg_shift - 24) * 4)
180
181 #define PERIPH_CLK_SOR_CLK_SEL_SHIFT    14
182 #define PERIPH_CLK_SOR_CLK_SEL_MASK     (0x3<<PERIPH_CLK_SOR_CLK_SEL_SHIFT)
183
184 /* PLL common */
185 #define PLL_BASE                        0x0
186 #define PLL_BASE_BYPASS                 (1<<31)
187 #define PLL_BASE_ENABLE                 (1<<30)
188 #define PLL_BASE_REF_ENABLE             (1<<29)
189 #define PLL_BASE_OVERRIDE               (1<<28)
190 #define PLL_BASE_LOCK                   (1<<27)
191 #define PLL_BASE_DIVP_MASK              (0x7<<20)
192 #define PLL_BASE_DIVP_SHIFT             20
193 #define PLL_BASE_DIVN_MASK              (0x3FF<<8)
194 #define PLL_BASE_DIVN_SHIFT             8
195 #define PLL_BASE_DIVM_MASK              (0x1F)
196 #define PLL_BASE_DIVM_SHIFT             0
197
198 #define PLL_BASE_PARSE(pll, cfg, b)                                            \
199         do {                                                                   \
200                 (cfg).m = ((b) & pll##_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT; \
201                 (cfg).n = ((b) & pll##_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT; \
202                 (cfg).p = ((b) & pll##_BASE_DIVP_MASK) >> PLL_BASE_DIVP_SHIFT; \
203         } while (0)
204
205 #define PLL_OUT_RATIO_MASK              (0xFF<<8)
206 #define PLL_OUT_RATIO_SHIFT             8
207 #define PLL_OUT_OVERRIDE                (1<<2)
208 #define PLL_OUT_CLKEN                   (1<<1)
209 #define PLL_OUT_RESET_DISABLE           (1<<0)
210
211 #define PLL_MISC(c)                     \
212         (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
213 #define PLL_MISCN(c, n)         \
214         ((c)->u.pll.misc1 + ((n) - 1) * PLL_MISC(c))
215 #define PLL_MISC_LOCK_ENABLE(c) \
216         (((c)->flags & (PLLU | PLLD)) ? (1<<22) : (1<<18))
217
218 #define PLL_MISC_DCCON_SHIFT            20
219 #define PLL_MISC_CPCON_SHIFT            8
220 #define PLL_MISC_CPCON_MASK             (0xF<<PLL_MISC_CPCON_SHIFT)
221 #define PLL_MISC_LFCON_SHIFT            4
222 #define PLL_MISC_LFCON_MASK             (0xF<<PLL_MISC_LFCON_SHIFT)
223 #define PLL_MISC_VCOCON_SHIFT           0
224 #define PLL_MISC_VCOCON_MASK            (0xF<<PLL_MISC_VCOCON_SHIFT)
225
226 #define PLL_FIXED_MDIV(c, ref)          ((ref) > (c)->u.pll.cf_max ? 2 : 1)
227
228 /* PLLU */
229 #define PLLU_BASE_OVERRIDE              (1<<24)
230 #define PLLU_BASE_POST_DIV              (1<<20)
231
232 /* PLLD */
233 #define PLLD_BASE_DIVN_MASK             (0x7FF<<8)
234 #define PLLD_BASE_CSI_CLKENABLE         (1<<26)
235 #define PLLD_BASE_DSI_MUX_SHIFT         25
236 #define PLLD_BASE_DSI_MUX_MASK          (1<<PLLD_BASE_DSI_MUX_SHIFT)
237 #define PLLD_BASE_CSI_CLKSOURCE         (1<<23)
238
239 #define PLLD_MISC_DSI_CLKENABLE         (1<<30)
240 #define PLLD_MISC_DIV_RST               (1<<23)
241 #define PLLD_MISC_DCCON_SHIFT           12
242
243 #define PLLDU_LFCON                     2
244
245 /* PLLC2 and PLLC3 (PLLCX) */
246 #define PLLCX_USE_DYN_RAMP              0
247 #define PLLCX_BASE_PHASE_LOCK           (1<<26)
248 #define PLLCX_BASE_DIVP_MASK            (0x7<<PLL_BASE_DIVP_SHIFT)
249 #define PLLCX_BASE_DIVN_MASK            (0xFF<<PLL_BASE_DIVN_SHIFT)
250 #define PLLCX_BASE_DIVM_MASK            (0x3<<PLL_BASE_DIVM_SHIFT)
251 #define PLLCX_PDIV_MAX  ((PLLCX_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT))
252 #define PLLCX_IS_DYN(new_p, old_p)      (((new_p) <= 8) && ((old_p) <= 8))
253
254 #define PLLCX_MISC_STROBE               (1<<31)
255 #define PLLCX_MISC_RESET                (1<<30)
256 #define PLLCX_MISC_SDM_DIV_SHIFT        28
257 #define PLLCX_MISC_SDM_DIV_MASK         (0x3 << PLLCX_MISC_SDM_DIV_SHIFT)
258 #define PLLCX_MISC_FILT_DIV_SHIFT       26
259 #define PLLCX_MISC_FILT_DIV_MASK        (0x3 << PLLCX_MISC_FILT_DIV_SHIFT)
260 #define PLLCX_MISC_ALPHA_SHIFT          18
261 #define PLLCX_MISC_ALPHA_MASK           (0xFF << PLLCX_MISC_ALPHA_SHIFT)
262 #define PLLCX_MISC_KB_SHIFT             9
263 #define PLLCX_MISC_KB_MASK              (0x1FF << PLLCX_MISC_KB_SHIFT)
264 #define PLLCX_MISC_KA_SHIFT             2
265 #define PLLCX_MISC_KA_MASK              (0x7F << PLLCX_MISC_KA_SHIFT)
266 #define PLLCX_MISC_VCO_GAIN_SHIFT       0
267 #define PLLCX_MISC_VCO_GAIN_MASK        (0x3 << PLLCX_MISC_VCO_GAIN_SHIFT)
268
269 #define PLLCX_MISC_KOEF_LOW_RANGE       \
270         ((0x14 << PLLCX_MISC_KA_SHIFT) | (0x23 << PLLCX_MISC_KB_SHIFT))
271
272 #define PLLCX_MISC_DIV_LOW_RANGE        \
273         ((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | (0x1 << PLLCX_MISC_FILT_DIV_SHIFT))
274 #define PLLCX_MISC_DIV_HIGH_RANGE       \
275         ((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | (0x2 << PLLCX_MISC_FILT_DIV_SHIFT))
276 #define PLLCX_FD_ULCK_FRM_SHIFT         12
277 #define PLLCX_FD_ULCK_FRM_MASK          (0x3 << PLLCX_FD_ULCK_FRM_SHIFT)
278 #define PLLCX_FD_LCK_FRM_SHIFT          8
279 #define PLLCX_FD_LCK_FRM_MASK           (0x3 << PLLCX_FD_LCK_FRM_SHIFT)
280 #define PLLCX_PD_ULCK_FRM_SHIFT         28
281 #define PLLCX_PD_ULCK_FRM_MASK          (0x3 << PLLCX_PD_ULCK_FRM_SHIFT)
282 #define PLLCX_PD_LCK_FRM_SHIFT          24
283 #define PLLCX_PD_LCK_FRM_MASK           (0x3 << PLLCX_PD_LCK_FRM_SHIFT)
284 #define PLLCX_PD_OUT_HYST_SHIFT         20
285 #define PLLCX_PD_OUT_HYST_MASK          (0x3 << PLLCX_PD_OUT_HYST_SHIFT)
286 #define PLLCX_PD_IN_HYST_SHIFT          16
287 #define PLLCX_PD_IN_HYST_MASK           (0x3 << PLLCX_PD_IN_HYST_SHIFT)
288
289 #define PLLCX_MISC_DEFAULT_VALUE        ((0x0 << PLLCX_MISC_VCO_GAIN_SHIFT) | \
290                                         PLLCX_MISC_KOEF_LOW_RANGE | \
291                                         (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \
292                                         PLLCX_MISC_DIV_LOW_RANGE | \
293                                         PLLCX_MISC_RESET)
294 #define PLLCX_MISC1_DEFAULT_VALUE       0x000d2308
295 #define PLLCX_MISC2_DEFAULT_VALUE       ((0x2 << PLLCX_PD_ULCK_FRM_SHIFT) | \
296                                         (0x1 << PLLCX_PD_LCK_FRM_SHIFT)   | \
297                                         (0x3 << PLLCX_PD_OUT_HYST_SHIFT)  | \
298                                         (0x1 << PLLCX_PD_IN_HYST_SHIFT)   | \
299                                         (0x2 << PLLCX_FD_ULCK_FRM_SHIFT)  | \
300                                         (0x2 << PLLCX_FD_LCK_FRM_SHIFT))
301 #define PLLCX_MISC3_DEFAULT_VALUE       0x200
302
303 #define PLLCX_MISC1_IDDQ                (0x1 << 27)
304
305 /* PLLX and PLLC (PLLXC)*/
306 #define PLLXC_USE_DYN_RAMP              0
307 #define PLLXC_BASE_DIVP_MASK            (0xF<<PLL_BASE_DIVP_SHIFT)
308 #define PLLXC_BASE_DIVN_MASK            (0xFF<<PLL_BASE_DIVN_SHIFT)
309 #define PLLXC_BASE_DIVM_MASK            (0xFF<<PLL_BASE_DIVM_SHIFT)
310
311 /* PLLXC has 4-bit PDIV, but entry 15 is not allowed in h/w,
312    and s/w usage is limited to 5 */
313 #define PLLXC_PDIV_MAX                  14
314 #define PLLXC_SW_PDIV_MAX               5
315
316 /* PLLX */
317 #define PLLX_MISC2_DYNRAMP_STEPB_SHIFT  24
318 #define PLLX_MISC2_DYNRAMP_STEPB_MASK   (0xFF << PLLX_MISC2_DYNRAMP_STEPB_SHIFT)
319 #define PLLX_MISC2_DYNRAMP_STEPA_SHIFT  16
320 #define PLLX_MISC2_DYNRAMP_STEPA_MASK   (0xFF << PLLX_MISC2_DYNRAMP_STEPA_SHIFT)
321 #define PLLX_MISC2_NDIV_NEW_SHIFT       8
322 #define PLLX_MISC2_NDIV_NEW_MASK        (0xFF << PLLX_MISC2_NDIV_NEW_SHIFT)
323 #define PLLX_MISC2_LOCK_OVERRIDE        (0x1 << 4)
324 #define PLLX_MISC2_DYNRAMP_DONE         (0x1 << 2)
325 #define PLLX_MISC2_CLAMP_NDIV           (0x1 << 1)
326 #define PLLX_MISC2_EN_DYNRAMP           (0x1 << 0)
327
328 #define PLLX_MISC3_IDDQ                 (0x1 << 3)
329
330 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
331   #define PLLX_HW_CTRL_CFG              0x548
332 #else
333   #define PLLX_HW_CTRL_CFG              0x14
334 #endif
335
336 #define PLLX_HW_CTRL_CFG_SWCTRL         (0x1 << 0)
337
338 /* PLLC */
339 #define PLLC_BASE_LOCK_OVERRIDE         (1<<28)
340
341 #define PLLC_MISC_IDDQ                  (0x1 << 26)
342 #define PLLC_MISC_LOCK_ENABLE           (0x1 << 24)
343
344 #define PLLC_MISC1_CLAMP_NDIV           (0x1 << 26)
345 #define PLLC_MISC1_EN_DYNRAMP           (0x1 << 25)
346 #define PLLC_MISC1_DYNRAMP_STEPA_SHIFT  17
347 #define PLLC_MISC1_DYNRAMP_STEPA_MASK   (0xFF << PLLC_MISC1_DYNRAMP_STEPA_SHIFT)
348 #define PLLC_MISC1_DYNRAMP_STEPB_SHIFT  9
349 #define PLLC_MISC1_DYNRAMP_STEPB_MASK   (0xFF << PLLC_MISC1_DYNRAMP_STEPB_SHIFT)
350 #define PLLC_MISC1_NDIV_NEW_SHIFT       1
351 #define PLLC_MISC1_NDIV_NEW_MASK        (0xFF << PLLC_MISC1_NDIV_NEW_SHIFT)
352 #define PLLC_MISC1_DYNRAMP_DONE         (0x1 << 0)
353
354 /* PLLM */
355 #define PLLM_BASE_DIVP_MASK             (0xF << PLL_BASE_DIVP_SHIFT)
356 #define PLLM_BASE_DIVN_MASK             (0xFF << PLL_BASE_DIVN_SHIFT)
357 #define PLLM_BASE_DIVM_MASK             (0xFF << PLL_BASE_DIVM_SHIFT)
358
359 /* PLLM has 4-bit PDIV, but entry 15 is not allowed in h/w,
360    and s/w usage is limited to 5 */
361 #define PLLM_PDIV_MAX                   14
362 #define PLLM_SW_PDIV_MAX                5
363
364 #define PLLM_MISC_FSM_SW_OVERRIDE       (0x1 << 10)
365 #define PLLM_MISC_IDDQ                  (0x1 << 5)
366 #define PLLM_MISC_LOCK_DISABLE          (0x1 << 4)
367 #define PLLM_MISC_LOCK_OVERRIDE         (0x1 << 3)
368
369 #define PMC_PLLP_WB0_OVERRIDE                   0xf8
370 #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE       (1 << 12)
371 #define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE     (1 << 11)
372
373 /* M, N layout for PLLM override and base registers are the same */
374 #define PMC_PLLM_WB0_OVERRIDE                   0x1dc
375
376 #define PMC_PLLM_WB0_OVERRIDE_2                 0x2b0
377 #define PMC_PLLM_WB0_OVERRIDE_2_DIVP_SHIFT      27
378 #define PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK       (0xF << 27)
379
380 /* PLLSS */
381 #define PLLSS_CFG(c)    ((c)->u.pll.misc1 + 0)
382 #define PLLSS_CTRL1(c)  ((c)->u.pll.misc1 + 4)
383 #define PLLSS_CTRL2(c)  ((c)->u.pll.misc1 + 8)
384
385 #define PLLSS_BASE_DIVP_MASK            (0xF << PLL_BASE_DIVP_SHIFT)
386 #define PLLSS_BASE_DIVN_MASK            (0xFF << PLL_BASE_DIVN_SHIFT)
387 #define PLLSS_BASE_DIVM_MASK            (0xFF << PLL_BASE_DIVM_SHIFT)
388 #define PLLSS_BASE_SOURCE_SHIFT         25
389 #define PLLSS_BASE_SOURCE_MASK          (3 << PLLSS_BASE_SOURCE_SHIFT)
390
391 /* PLLSS has 4-bit PDIV, but entry 15 is not allowed in h/w,
392    and s/w usage is limited to 5 */
393 #define PLLSS_PDIV_MAX                  14
394 #define PLLSS_SW_PDIV_MAX               5
395
396 #define PLLSS_MISC_LOCK_ENABLE          (0x1 << 30)
397 #define PLLSS_MISC_KCP_SHIFT            25
398 #define PLLSS_MISC_KCP_MASK                     (0x3 << PLLSS_MISC_KCP_SHIFT)
399 #define PLLSS_MISC_KVCO_SHIFT           24
400 #define PLLSS_MISC_KVCO_MASK            (0x1 << PLLSS_MISC_KVCO_SHIFT)
401 #define PLLSS_MISC_SETUP_SHIFT          0
402 #define PLLSS_MISC_SETUP_MASK           (0xFFFFFF << PLLSS_MISC_SETUP_SHIFT)
403 #define PLLSS_BASE_LOCK_OVERRIDE        (0x1 << 24)
404 #define PLLSS_BASE_LOCK                 (0x1 << 27)
405 #define PLLSS_BASE_IDDQ                 (0x1 << 19)
406
407 #define PLLSS_MISC_DEFAULT_VALUE ( \
408         (PLLSS_MISC_KVCO << PLLSS_MISC_KVCO_SHIFT) | \
409         (PLLSS_MISC_SETUP << PLLSS_MISC_SETUP_SHIFT))
410 #define PLLSS_CFG_DEFAULT_VALUE ( \
411         (PLLSS_EN_SDM << 31) | \
412         (PLLSS_EN_SSC << 30) | \
413         (PLLSS_EN_DITHER2 << 29) | \
414         (PLLSS_EN_DITHER << 28) | \
415         (PLLSS_SDM_RESET << 27) | \
416         (PLLSS_CLAMP << 22))
417 #define PLLSS_CTRL1_DEFAULT_VALUE \
418         ((PLLSS_SDM_SSC_MAX << 16) | (PLLSS_SDM_SSC_MIN << 0))
419 #define PLLSS_CTRL2_DEFAULT_VALUE \
420         ((PLLSS_SDM_SSC_STEP << 16) | (PLLSS_SDM_DIN << 0))
421
422 /* PLLSS configuration */
423 #define PLLSS_MISC_KVCO                 0
424 #define PLLSS_MISC_SETUP                0
425 #define PLLSS_EN_SDM                    0
426 #define PLLSS_EN_SSC                    0
427 #define PLLSS_EN_DITHER2                0
428 #define PLLSS_EN_DITHER                 1
429 #define PLLSS_SDM_RESET                 0
430 #define PLLSS_CLAMP                     0
431 #define PLLSS_SDM_SSC_MAX               0
432 #define PLLSS_SDM_SSC_MIN               0
433 #define PLLSS_SDM_SSC_STEP              0
434 #define PLLSS_SDM_DIN                   0
435
436 /* PLLDP SS parameters */
437 #define PLLDP_SS_CTRL1_0_DEFAULT_VALUE  0xF000E5EC
438 #define PLLDP_SS_CTRL2_0_DEFAULT_VALUE  0x101BF000
439 #define PLLDP_SS_CFG_0_DEFAULT_VALUE    0xC0000000
440
441
442 /* PLLRE */
443 #define PLLRE_BASE_DIVP_SHIFT           16
444 #define PLLRE_BASE_DIVP_MASK            (0xF << PLLRE_BASE_DIVP_SHIFT)
445 #define PLLRE_BASE_DIVN_MASK            (0xFF << PLL_BASE_DIVN_SHIFT)
446 #define PLLRE_BASE_DIVM_MASK            (0xFF << PLL_BASE_DIVM_SHIFT)
447
448 /* PLLRE has 4-bit PDIV, but entry 15 is not allowed in h/w,
449    and s/w usage is limited to 5 */
450 #define PLLRE_PDIV_MAX                  14
451 #define PLLRE_SW_PDIV_MAX               5
452
453 #define PLLRE_MISC_LOCK_ENABLE          (0x1 << 30)
454 #define PLLRE_MISC_LOCK_OVERRIDE        (0x1 << 29)
455 #define PLLRE_MISC_LOCK                 (0x1 << 24)
456 #define PLLRE_MISC_IDDQ                 (0x1 << 16)
457
458 #define OUT_OF_TABLE_CPCON              0x8
459
460 #define SUPER_CLK_MUX                   0x00
461 #define SUPER_STATE_SHIFT               28
462 #define SUPER_STATE_MASK                (0xF << SUPER_STATE_SHIFT)
463 #define SUPER_STATE_STANDBY             (0x0 << SUPER_STATE_SHIFT)
464 #define SUPER_STATE_IDLE                (0x1 << SUPER_STATE_SHIFT)
465 #define SUPER_STATE_RUN                 (0x2 << SUPER_STATE_SHIFT)
466 #define SUPER_STATE_IRQ                 (0x3 << SUPER_STATE_SHIFT)
467 #define SUPER_STATE_FIQ                 (0x4 << SUPER_STATE_SHIFT)
468 #define SUPER_LP_DIV2_BYPASS            (0x1 << 16)
469 #define SUPER_SOURCE_MASK               0xF
470 #define SUPER_FIQ_SOURCE_SHIFT          12
471 #define SUPER_IRQ_SOURCE_SHIFT          8
472 #define SUPER_RUN_SOURCE_SHIFT          4
473 #define SUPER_IDLE_SOURCE_SHIFT         0
474
475 #define SUPER_CLK_DIVIDER               0x04
476 #define SUPER_CLOCK_DIV_U71_SHIFT       16
477 #define SUPER_CLOCK_DIV_U71_MASK        (0xff << SUPER_CLOCK_DIV_U71_SHIFT)
478
479 #define CLK13_SOURCE_SHIFT              28
480 #define CLK13_SOURCE_MASK               0xF
481
482 #define BUS_CLK_DISABLE                 (1<<3)
483 #define BUS_CLK_DIV_MASK                0x3
484
485 #define PMC_CTRL                        0x0
486  #define PMC_CTRL_BLINK_ENB             (1 << 7)
487
488 #define PMC_DPD_PADS_ORIDE              0x1c
489  #define PMC_DPD_PADS_ORIDE_BLINK_ENB   (1 << 20)
490
491 #define PMC_BLINK_TIMER_DATA_ON_SHIFT   0
492 #define PMC_BLINK_TIMER_DATA_ON_MASK    0x7fff
493 #define PMC_BLINK_TIMER_ENB             (1 << 15)
494 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT  16
495 #define PMC_BLINK_TIMER_DATA_OFF_MASK   0xffff
496
497 #define UTMIP_PLL_CFG2                                  0x488
498 #define UTMIP_PLL_CFG2_STABLE_COUNT(x)                  (((x) & 0xfff) << 6)
499 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x)              (((x) & 0x3f) << 18)
500 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN        (1 << 0)
501 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERUP          (1 << 1)
502 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN        (1 << 2)
503 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERUP          (1 << 3)
504 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN        (1 << 4)
505 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERUP          (1 << 5)
506 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERDOWN        (1 << 24)
507 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERUP          (1 << 25)
508
509 #define UTMIP_PLL_CFG1                                  0x484
510 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x)              (((x) & 0x1f) << 27)
511 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x)               (((x) & 0xfff) << 0)
512 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP (1 << 15)
513 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN       (1 << 14)
514 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN       (1 << 12)
515 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERUP               (1 << 17)
516 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN             (1 << 16)
517
518 /* PLLE */
519 #define PLLE_BASE_LOCK_OVERRIDE         (0x1 << 29)
520 #define PLLE_BASE_DIVCML_SHIFT          24
521 #define PLLE_BASE_DIVCML_MASK           (0xf<<PLLE_BASE_DIVCML_SHIFT)
522
523 #define PLLE_BASE_DIVN_MASK             (0xFF<<PLL_BASE_DIVN_SHIFT)
524 #define PLLE_BASE_DIVM_MASK             (0xFF<<PLL_BASE_DIVM_SHIFT)
525
526 /* PLLE has 4-bit CMLDIV, but entry 15 is not allowed in h/w */
527 #define PLLE_CMLDIV_MAX                 14
528 #define PLLE_MISC_READY                 (1<<15)
529 #define PLLE_MISC_IDDQ_SW_CTRL          (1<<14)
530 #define PLLE_MISC_IDDQ_SW_VALUE         (1<<13)
531 #define PLLE_MISC_LOCK                  (1<<11)
532 #define PLLE_MISC_LOCK_ENABLE           (1<<9)
533 #define PLLE_MISC_PLLE_PTS              (1<<8)
534 #define PLLE_MISC_VREG_BG_CTRL_SHIFT    4
535 #define PLLE_MISC_VREG_BG_CTRL_MASK     (0x3<<PLLE_MISC_VREG_BG_CTRL_SHIFT)
536 #define PLLE_MISC_VREG_CTRL_SHIFT       2
537 #define PLLE_MISC_VREG_CTRL_MASK        (0x3<<PLLE_MISC_VREG_CTRL_SHIFT)
538
539 #define PLLE_SS_CTRL                    0x68
540 #define PLLE_SS_INCINTRV_SHIFT          24
541 #define PLLE_SS_INCINTRV_MASK           (0x3f<<PLLE_SS_INCINTRV_SHIFT)
542 #define PLLE_SS_INC_SHIFT               16
543 #define PLLE_SS_INC_MASK                (0xff<<PLLE_SS_INC_SHIFT)
544 #define PLLE_SS_CNTL_INVERT             (0x1 << 15)
545 #define PLLE_SS_CNTL_CENTER             (0x1 << 14)
546 #define PLLE_SS_CNTL_SSC_BYP            (0x1 << 12)
547 #define PLLE_SS_CNTL_INTERP_RESET       (0x1 << 11)
548 #define PLLE_SS_CNTL_BYPASS_SS          (0x1 << 10)
549 #define PLLE_SS_MAX_SHIFT               0
550 #define PLLE_SS_MAX_MASK                (0x1ff<<PLLE_SS_MAX_SHIFT)
551 #define PLLE_SS_COEFFICIENTS_MASK       \
552         (PLLE_SS_INCINTRV_MASK | PLLE_SS_INC_MASK | PLLE_SS_MAX_MASK)
553 #define PLLE_SS_COEFFICIENTS_VAL        \
554         ((0x20<<PLLE_SS_INCINTRV_SHIFT) | (0x1<<PLLE_SS_INC_SHIFT) | \
555          (0x25<<PLLE_SS_MAX_SHIFT))
556 #define PLLE_SS_DISABLE                 (PLLE_SS_CNTL_SSC_BYP |\
557         PLLE_SS_CNTL_INTERP_RESET | PLLE_SS_CNTL_BYPASS_SS)
558
559 #define PLLE_AUX                        0x48c
560 #define PLLE_AUX_PLLRE_SEL              (1<<28)
561 #define PLLE_AUX_SEQ_STATE_SHIFT        26
562 #define PLLE_AUX_SEQ_STATE_MASK         (0x3<<PLLE_AUX_SEQ_STATE_SHIFT)
563 #define PLLE_AUX_SEQ_START_STATE        (1<<25)
564 #define PLLE_AUX_SEQ_ENABLE             (1<<24)
565 #define PLLE_AUX_SS_SWCTL               (1<<6)
566 #define PLLE_AUX_ENABLE_SWCTL           (1<<4)
567 #define PLLE_AUX_USE_LOCKDET            (1<<3)
568 #define PLLE_AUX_PLLP_SEL               (1<<2)
569
570 #define PLLE_AUX_CML_SATA_ENABLE        (1<<1)
571 #define PLLE_AUX_CML_PCIE_ENABLE        (1<<0)
572
573 /* USB PLLs PD HW controls */
574 #define XUSBIO_PLL_CFG0                         0x51c
575 #define XUSBIO_PLL_CFG0_SEQ_START_STATE         (1<<25)
576 #define XUSBIO_PLL_CFG0_SEQ_ENABLE              (1<<24)
577 #define XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET      (1<<6)
578 #define XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL        (1<<2)
579 #define XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL      (1<<0)
580
581 #define UTMIPLL_HW_PWRDN_CFG0                   0x52c
582 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_START_STATE   (1<<25)
583 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE        (1<<24)
584 #define UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET       (1<<6)
585 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_RESET_INPUT_VALUE     (1<<5)
586 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_IN_SWCTL      (1<<4)
587 #define UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL  (1<<2)
588 #define UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE     (1<<1)
589 #define UTMIPLL_HW_PWRDN_CFG0_IDDQ_SWCTL        (1<<0)
590
591 #define PLLU_HW_PWRDN_CFG0                      0x530
592 #define PLLU_HW_PWRDN_CFG0_SEQ_START_STATE      (1<<25)
593 #define PLLU_HW_PWRDN_CFG0_SEQ_ENABLE           (1<<24)
594 #define PLLU_HW_PWRDN_CFG0_USE_LOCKDET          (1<<6)
595 #define PLLU_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL     (1<<2)
596 #define PLLU_HW_PWRDN_CFG0_CLK_SWITCH_SWCTL     (1<<0)
597
598 #define USB_PLLS_SEQ_START_STATE                (1<<25)
599 #define USB_PLLS_SEQ_ENABLE                     (1<<24)
600 #define USB_PLLS_USE_LOCKDET                    (1<<6)
601 #define USB_PLLS_ENABLE_SWCTL                   ((1<<2) | (1<<0))
602
603 /* XUSB PLL PAD controls */
604 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0         0x40
605 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_PWR_OVRD    (1<<3)
606 #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_IDDQ        (1<<0)
607
608
609 /* DFLL */
610 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
611 #define DFLL_BASE                               0x2f4
612 #else
613 #define DFLL_BASE                               0x80
614 #endif
615 #define DFLL_BASE_RESET                         (1<<0)
616
617 #define LVL2_CLK_GATE_OVRE                      0x554
618
619 #define ROUND_DIVIDER_UP        0
620 #define ROUND_DIVIDER_DOWN      1
621 #define DIVIDER_1_5_ALLOWED     0
622
623 /* Tegra CPU clock and reset control regs */
624 #define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX          0x4c
625 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET      0x340
626 #define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR      0x344
627 #define TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR    0x34c
628 #define TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS     0x470
629
630 #define CPU_CLOCK(cpu)  (0x1 << (8 + cpu))
631 #define CPU_RESET(cpu)  (0x111001ul << (cpu))
632
633 /* PLLP default fixed rate in h/w controlled mode */
634 #define PLLP_DEFAULT_FIXED_RATE         408000000
635
636 /* Use PLL_RE as PLLE input (default - OSC via pll reference divider) */
637 #define USE_PLLE_INPUT_PLLRE    0
638
639 static bool tegra12_is_dyn_ramp(struct clk *c,
640                                 unsigned long rate, bool from_vco_min);
641 static void tegra12_pllp_init_dependencies(unsigned long pllp_rate);
642 static unsigned long tegra12_clk_shared_bus_update(struct clk *bus,
643         struct clk **bus_top, struct clk **bus_slow, unsigned long *rate_cap);
644 static unsigned long tegra12_clk_cap_shared_bus(struct clk *bus,
645         unsigned long rate, unsigned long ceiling);
646
647 static bool tegra12_periph_is_special_reset(struct clk *c);
648 static void tegra12_dfll_cpu_late_init(struct clk *c);
649
650 static bool detach_shared_bus;
651 module_param(detach_shared_bus, bool, 0644);
652
653 /* Defines default range for dynamic frequency lock loop (DFLL)
654    to be used as CPU clock source:
655    "0" - DFLL is not used,
656    "1" - DFLL is used as a source for all CPU rates
657    "2" - DFLL is used only for high rates above crossover with PLL dvfs curve
658 */
659 static int use_dfll;
660
661 /**
662 * Structure defining the fields for USB UTMI clocks Parameters.
663 */
664 struct utmi_clk_param
665 {
666         /* Oscillator Frequency in KHz */
667         u32 osc_frequency;
668         /* UTMIP PLL Enable Delay Count  */
669         u8 enable_delay_count;
670         /* UTMIP PLL Stable count */
671         u8 stable_count;
672         /*  UTMIP PLL Active delay count */
673         u8 active_delay_count;
674         /* UTMIP PLL Xtal frequency count */
675         u8 xtal_freq_count;
676 };
677
678 static const struct utmi_clk_param utmi_parameters[] =
679 {
680 /*      OSC_FREQUENCY,  ENABLE_DLY,     STABLE_CNT,     ACTIVE_DLY,     XTAL_FREQ_CNT */
681         {13000000,      0x02,           0x33,           0x05,           0x7F},
682         {19200000,      0x03,           0x4B,           0x06,           0xBB},
683         {12000000,      0x02,           0x2F,           0x04,           0x76},
684         {26000000,      0x04,           0x66,           0x09,           0xFE},
685         {16800000,      0x03,           0x41,           0x0A,           0xA4},
686 };
687
688 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
689 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
690 static void __iomem *reg_clk13_base = IO_ADDRESS(TEGRA_CLK13_RESET_BASE);
691 #endif
692 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
693 static void __iomem *misc_gp_base = IO_ADDRESS(TEGRA_APB_MISC_BASE);
694 static void __iomem *reg_xusb_padctl_base = IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE);
695
696 #define MISC_GP_TRANSACTOR_SCRATCH_0            0x864
697 #define MISC_GP_TRANSACTOR_SCRATCH_LA_ENABLE    (0x1 << 1)
698 #define MISC_GP_TRANSACTOR_SCRATCH_DDS_ENABLE   (0x1 << 2)
699 #define MISC_GP_TRANSACTOR_SCRATCH_DP2_ENABLE   (0x1 << 3)
700
701 /*
702  * Some peripheral clocks share an enable bit, so refcount the enable bits
703  * in registers CLK_ENABLE_L, ... CLK_ENABLE_W, and protect refcount updates
704  * with lock
705  */
706 static DEFINE_SPINLOCK(periph_refcount_lock);
707 static int tegra_periph_clk_enable_refcount[CLK_OUT_ENB_NUM * 32];
708
709 #define clk_writel(value, reg) \
710         __raw_writel(value, reg_clk_base + (reg))
711 #define clk_readl(reg) \
712         __raw_readl(reg_clk_base + (reg))
713 #define pmc_writel(value, reg) \
714         __raw_writel(value, reg_pmc_base + (reg))
715 #define pmc_readl(reg) \
716         readl(reg_pmc_base + (reg))
717 #define xusb_padctl_writel(value, reg) \
718          __raw_writel(value, reg_xusb_padctl_base + (reg))
719 #define xusb_padctl_readl(reg) \
720         readl(reg_xusb_padctl_base + (reg))
721
722 static inline void clk_writel_delay(u32 value, u32 reg)
723 {
724         __raw_writel((value), reg_clk_base + (reg));
725         __raw_readl(reg_clk_base + (reg));
726         dsb();
727         udelay(2);
728 }
729
730 static inline void pll_writel_delay(u32 value, u32 reg)
731 {
732         __raw_writel((value), reg_clk_base + (reg));
733         __raw_readl(reg_clk_base + (reg));
734         dsb();
735         udelay(1);
736 }
737
738 /* Overloading clk_writelx macro based on the TEGRA_13x_SOC define */
739 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
740
741 #define clk_writelx(value, reg) \
742         __raw_writel(value, reg_clk_base + (reg))
743 #define clk_readlx(reg) \
744         __raw_readl(reg_clk_base + (reg))
745
746 #else
747
748 #define clk_writelx(value, reg) \
749         __raw_writel(value, reg_clk13_base + (reg))
750 #define clk_readlx(reg) \
751         __raw_readl(reg_clk13_base + (reg))
752
753 #endif
754
755 static inline void clk_writelx_delay(u32 value, u32 reg)
756 {
757         clk_writelx(value, reg);
758         clk_readlx(reg);
759         dsb();
760         udelay(2);
761 }
762
763 static inline void pll_writelx_delay(u32 value, u32 reg)
764 {
765         clk_writelx(value, reg);
766         clk_readlx(reg);
767         dsb();
768         udelay(1);
769 }
770
771 static inline int clk_set_div(struct clk *c, u32 n)
772 {
773         return clk_set_rate(c, (clk_get_rate(c->parent) + n-1) / n);
774 }
775
776 static inline u32 periph_clk_to_reg(
777         struct clk *c, u32 reg_L, u32 reg_V, u32 reg_X, int offs)
778 {
779         u32 reg = c->u.periph.clk_num / 32;
780         BUG_ON(reg >= RST_DEVICES_NUM);
781         if (reg < 3)
782                 reg = reg_L + (reg * offs);
783         else if (reg < 5)
784                 reg = reg_V + ((reg - 3) * offs);
785         else
786                 reg = reg_X;
787         return reg;
788 }
789
790 static int clk_div_x1_get_divider(unsigned long parent_rate, unsigned long rate,
791                         u32 max_x,
792                                  u32 flags, u32 round_mode)
793 {
794         s64 divider_ux1 = parent_rate;
795         if (!rate)
796                 return -EINVAL;
797
798         if (!(flags & DIV_U71_INT))
799                 divider_ux1 *= 2;
800         if (round_mode == ROUND_DIVIDER_UP)
801                 divider_ux1 += rate - 1;
802         do_div(divider_ux1, rate);
803         if (flags & DIV_U71_INT)
804                 divider_ux1 *= 2;
805
806         if (divider_ux1 - 2 < 0)
807                 return 0;
808
809         if (divider_ux1 - 2 > max_x)
810                 return -EINVAL;
811
812 #if !DIVIDER_1_5_ALLOWED
813         if (divider_ux1 == 3)
814                 divider_ux1 = (round_mode == ROUND_DIVIDER_UP) ? 4 : 2;
815 #endif
816         return divider_ux1 - 2;
817 }
818
819 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate,
820                                  u32 flags, u32 round_mode)
821 {
822         return clk_div_x1_get_divider(parent_rate, rate, 0xFF,
823                         flags, round_mode);
824 }
825
826 static int clk_div151_get_divider(unsigned long parent_rate, unsigned long rate,
827                                  u32 flags, u32 round_mode)
828 {
829         return clk_div_x1_get_divider(parent_rate, rate, 0xFFFF,
830                         flags, round_mode);
831 }
832
833 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
834 {
835         s64 divider_u16;
836
837         divider_u16 = parent_rate;
838         if (!rate)
839                 return -EINVAL;
840         divider_u16 += rate - 1;
841         do_div(divider_u16, rate);
842
843         if (divider_u16 - 1 < 0)
844                 return 0;
845
846         if (divider_u16 - 1 > 0xFFFF)
847                 return -EINVAL;
848
849         return divider_u16 - 1;
850 }
851
852 static long fixed_src_bus_round_updown(struct clk *c, struct clk *src,
853                                        u32 flags, unsigned long rate, bool up)
854 {
855         int divider;
856         unsigned long source_rate, round_rate;
857
858         source_rate = clk_get_rate(src);
859
860         divider = clk_div71_get_divider(source_rate, rate + (up ? -1 : 1),
861                 flags, up ? ROUND_DIVIDER_DOWN : ROUND_DIVIDER_UP);
862         if (divider < 0)
863                 return c->min_rate;
864
865         round_rate = source_rate * 2 / (divider + 2);
866
867         if (round_rate > c->max_rate) {
868                 divider += flags & DIV_U71_INT ? 2 : 1;
869 #if !DIVIDER_1_5_ALLOWED
870                 divider = max(2, divider);
871 #endif
872                 round_rate = source_rate * 2 / (divider + 2);
873         }
874         return round_rate;
875 }
876
877 static inline bool bus_user_is_slower(struct clk *a, struct clk *b)
878 {
879         return a->u.shared_bus_user.client->max_rate * a->div <
880                 b->u.shared_bus_user.client->max_rate * b->div;
881 }
882
883 static inline bool bus_user_request_is_lower(struct clk *a, struct clk *b)
884 {
885         return a->u.shared_bus_user.rate * a->div <
886                 b->u.shared_bus_user.rate * b->div;
887 }
888
889 /* clk_m functions */
890 static unsigned long tegra12_clk_m_autodetect_rate(struct clk *c)
891 {
892         u32 osc_ctrl = clk_readl(OSC_CTRL);
893         u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
894         u32 pll_ref_div = osc_ctrl & OSC_CTRL_PLL_REF_DIV_MASK;
895
896         u32 spare = clk_readl(SPARE_REG);
897         u32 divisor = (spare & SPARE_REG_CLK_M_DIVISOR_MASK)
898                 >> SPARE_REG_CLK_M_DIVISOR_SHIFT;
899         u32 spare_update = spare & ~SPARE_REG_CLK_M_DIVISOR_MASK;
900
901         c->rate = tegra_clk_measure_input_freq();
902         switch (c->rate) {
903         case 12000000:
904                 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
905                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
906                 BUG_ON(divisor != 0);
907                 break;
908         case 13000000:
909                 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
910                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
911                 BUG_ON(divisor != 0);
912                 break;
913         case 19200000:
914                 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
915                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
916                 BUG_ON(divisor != 0);
917                 break;
918         case 26000000:
919                 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
920                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
921                 BUG_ON(divisor != 0);
922                 break;
923         case 16800000:
924                 auto_clock_control |= OSC_CTRL_OSC_FREQ_16_8MHZ;
925                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
926                 BUG_ON(divisor != 0);
927                 break;
928         case 38400000:
929                 auto_clock_control |= OSC_CTRL_OSC_FREQ_38_4MHZ;
930                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_2);
931                 BUG_ON(divisor != 1);
932                 spare_update |= (1 << SPARE_REG_CLK_M_DIVISOR_SHIFT);
933                 break;
934         case 48000000:
935                 auto_clock_control |= OSC_CTRL_OSC_FREQ_48MHZ;
936                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_4);
937                 BUG_ON(divisor != 3);
938                 spare_update |= (3 << SPARE_REG_CLK_M_DIVISOR_SHIFT);
939                 break;
940         case 115200:    /* fake 13M for QT */
941         case 230400:    /* fake 13M for QT */
942                 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
943                 c->rate = 13000000;
944                 BUG_ON(pll_ref_div != OSC_CTRL_PLL_REF_DIV_1);
945                 BUG_ON(divisor != 0);
946                 break;
947         default:
948                 pr_err("%s: Unexpected clock rate %ld", __func__, c->rate);
949                 BUG();
950         }
951
952         clk_writel(auto_clock_control, OSC_CTRL);
953         clk_writel(spare_update, SPARE_REG);
954
955         return c->rate;
956 }
957
958 static void tegra12_clk_m_init(struct clk *c)
959 {
960         pr_debug("%s on clock %s\n", __func__, c->name);
961         tegra12_clk_m_autodetect_rate(c);
962 }
963
964 static int tegra12_clk_m_enable(struct clk *c)
965 {
966         pr_debug("%s on clock %s\n", __func__, c->name);
967         return 0;
968 }
969
970 static void tegra12_clk_m_disable(struct clk *c)
971 {
972         pr_debug("%s on clock %s\n", __func__, c->name);
973         WARN(1, "Attempting to disable main SoC clock\n");
974 }
975
976 static struct clk_ops tegra_clk_m_ops = {
977         .init           = tegra12_clk_m_init,
978         .enable         = tegra12_clk_m_enable,
979         .disable        = tegra12_clk_m_disable,
980 };
981
982 static struct clk_ops tegra_clk_m_div_ops = {
983         .enable         = tegra12_clk_m_enable,
984 };
985
986 /* PLL reference divider functions */
987 static void tegra12_pll_ref_init(struct clk *c)
988 {
989         u32 pll_ref_div = clk_readl(OSC_CTRL) & OSC_CTRL_PLL_REF_DIV_MASK;
990         pr_debug("%s on clock %s\n", __func__, c->name);
991
992         switch (pll_ref_div) {
993         case OSC_CTRL_PLL_REF_DIV_1:
994                 c->div = 1;
995                 break;
996         case OSC_CTRL_PLL_REF_DIV_2:
997                 c->div = 2;
998                 break;
999         case OSC_CTRL_PLL_REF_DIV_4:
1000                 c->div = 4;
1001                 break;
1002         default:
1003                 pr_err("%s: Invalid pll ref divider %d", __func__, pll_ref_div);
1004                 BUG();
1005         }
1006         c->mul = 1;
1007         c->state = ON;
1008 }
1009
1010 static struct clk_ops tegra_pll_ref_ops = {
1011         .init           = tegra12_pll_ref_init,
1012         .enable         = tegra12_clk_m_enable,
1013         .disable        = tegra12_clk_m_disable,
1014 };
1015
1016 /* super clock functions */
1017 /* "super clocks" on tegra12x have two-stage muxes, fractional 7.1 divider and
1018  * clock skipping super divider.  We will ignore the clock skipping divider,
1019  * since we can't lower the voltage when using the clock skip, but we can if
1020  * we lower the PLL frequency. Note that skipping divider can and will be used
1021  * by thermal control h/w for automatic throttling. There is also a 7.1 divider
1022  * that most CPU super-clock inputs can be routed through. We will not use it
1023  * as well (keep default 1:1 state), to avoid high jitter on PLLX and DFLL path
1024  * and possible concurrency access issues with thermal h/w (7.1 divider setting
1025  * share register with clock skipping divider)
1026  */
1027 static void tegra12_super_clk_init(struct clk *c)
1028 {
1029         u32 val;
1030         int source;
1031         int shift;
1032         const struct clk_mux_sel *sel;
1033         val = clk_readl(c->reg + SUPER_CLK_MUX);
1034         c->state = ON;
1035         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
1036                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
1037         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
1038                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
1039         source = (val >> shift) & SUPER_SOURCE_MASK;
1040
1041         /*
1042          * Enforce PLLX DIV2 bypass setting as early as possible. It is always
1043          * safe to do for both cclk_lp and cclk_g when booting on G CPU. (In
1044          * case of booting on LP CPU, cclk_lp will be updated during the cpu
1045          * rate change after boot, and cclk_g after the cluster switch.)
1046          */
1047         if ((c->flags & DIV_U71) && (!is_lp_cluster())) {
1048                 val |= SUPER_LP_DIV2_BYPASS;
1049                 clk_writel_delay(val, c->reg);
1050         }
1051
1052         for (sel = c->inputs; sel->input != NULL; sel++) {
1053                 if (sel->value == source)
1054                         break;
1055         }
1056         BUG_ON(sel->input == NULL);
1057         c->parent = sel->input;
1058
1059         /* Update parent in case when LP CPU PLLX DIV2 bypassed */
1060         if ((c->flags & DIV_2) && (c->parent->flags & PLLX) &&
1061             (val & SUPER_LP_DIV2_BYPASS))
1062                 c->parent = c->parent->parent;
1063
1064         /* Update parent in case when LP CPU PLLX DIV2 bypassed */
1065         if ((c->flags & DIV_2) && (c->parent->flags & PLLX) &&
1066             (val & SUPER_LP_DIV2_BYPASS))
1067                 c->parent = c->parent->parent;
1068
1069         if (c->flags & DIV_U71) {
1070                 c->mul = 2;
1071                 c->div = 2;
1072
1073                 /*
1074                  * Make sure 7.1 divider is 1:1; clear h/w skipper control -
1075                  * it will be enabled by soctherm later
1076                  */
1077                 val = clk_readl(c->reg + SUPER_CLK_DIVIDER);
1078                 BUG_ON(val & SUPER_CLOCK_DIV_U71_MASK);
1079                 val = 0;
1080                 clk_writel(val, c->reg + SUPER_CLK_DIVIDER);
1081         }
1082         else
1083                 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
1084 }
1085
1086 static int tegra12_super_clk_enable(struct clk *c)
1087 {
1088         return 0;
1089 }
1090
1091 static void tegra12_super_clk_disable(struct clk *c)
1092 {
1093         /* since tegra12 has 2 CPU super clocks - low power lp-mode clock and
1094            geared up g-mode super clock - mode switch may request to disable
1095            either of them; accept request with no affect on h/w */
1096 }
1097
1098 static int tegra12_super_clk_set_parent(struct clk *c, struct clk *p)
1099 {
1100         u32 val;
1101         const struct clk_mux_sel *sel;
1102         int shift;
1103
1104         val = clk_readl(c->reg + SUPER_CLK_MUX);
1105         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
1106                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
1107         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
1108                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
1109         for (sel = c->inputs; sel->input != NULL; sel++) {
1110                 if (sel->input == p) {
1111                         /* For LP mode super-clock switch between PLLX direct
1112                            and divided-by-2 outputs is allowed only when other
1113                            than PLLX clock source is current parent */
1114                         if ((c->flags & DIV_2) && (p->flags & PLLX) &&
1115                             ((sel->value ^ val) & SUPER_LP_DIV2_BYPASS)) {
1116                                 if (c->parent->flags & PLLX)
1117                                         return -EINVAL;
1118                                 val ^= SUPER_LP_DIV2_BYPASS;
1119                                 clk_writel_delay(val, c->reg);
1120                         }
1121                         val &= ~(SUPER_SOURCE_MASK << shift);
1122                         val |= (sel->value & SUPER_SOURCE_MASK) << shift;
1123
1124                         if (c->flags & DIV_U71) {
1125                                 /* Make sure 7.1 divider is 1:1 */
1126                                 u32 div = clk_readl(c->reg + SUPER_CLK_DIVIDER);
1127                                 BUG_ON(div & SUPER_CLOCK_DIV_U71_MASK);
1128                         }
1129
1130                         if (c->refcnt)
1131                                 clk_enable(p);
1132
1133                         clk_writel_delay(val, c->reg);
1134
1135                         if (c->refcnt && c->parent)
1136                                 clk_disable(c->parent);
1137
1138                         clk_reparent(c, p);
1139                         return 0;
1140                 }
1141         }
1142         return -EINVAL;
1143 }
1144
1145 /*
1146  * Do not use super clocks "skippers", since dividing using a clock skipper
1147  * does not allow the voltage to be scaled down. Instead adjust the rate of
1148  * the parent clock. This requires that the parent of a super clock have no
1149  * other children, otherwise the rate will change underneath the other
1150  * children.
1151  */
1152 static int tegra12_super_clk_set_rate(struct clk *c, unsigned long rate)
1153 {
1154         /* In tegra12_cpu_clk_set_plls() and  tegra12_sbus_cmplx_set_rate()
1155          * this call is skipped by directly setting rate of source plls. If we
1156          * ever use 7.1 divider at other than 1:1 setting, or exercise s/w
1157          * skipper control, not only this function, but cpu and sbus set_rate
1158          * APIs should be changed accordingly.
1159          */
1160         return clk_set_rate(c->parent, rate);
1161 }
1162
1163 #ifdef CONFIG_PM_SLEEP
1164 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
1165 static void tegra12_super_clk_resume(struct clk *c, struct clk *backup,
1166                                      u32 setting)
1167 {
1168         u32 val;
1169         const struct clk_mux_sel *sel;
1170         int shift;
1171
1172         /* For sclk and cclk_g super clock just restore saved value */
1173         if (!(c->flags & DIV_2)) {
1174                 clk_writel_delay(setting, c->reg);
1175                 return;
1176         }
1177
1178         /*
1179          * For cclk_lp supper clock: switch to backup (= not PLLX) source,
1180          * safely restore PLLX DIV2 bypass, and only then restore full
1181          * setting
1182          */
1183         val = clk_readl(c->reg);
1184         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
1185                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
1186         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
1187                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
1188         for (sel = c->inputs; sel->input != NULL; sel++) {
1189                 if (sel->input == backup) {
1190                         val &= ~(SUPER_SOURCE_MASK << shift);
1191                         val |= (sel->value & SUPER_SOURCE_MASK) << shift;
1192
1193                         BUG_ON(backup->flags & PLLX);
1194                         clk_writel_delay(val, c->reg);
1195
1196                         val &= ~SUPER_LP_DIV2_BYPASS;
1197                         val |= (setting & SUPER_LP_DIV2_BYPASS);
1198                         clk_writel_delay(val, c->reg);
1199                         clk_writel_delay(setting, c->reg);
1200                         return;
1201                 }
1202         }
1203         BUG();
1204 }
1205 #endif
1206 #endif
1207
1208 static struct clk_ops tegra_super_ops = {
1209         .init                   = tegra12_super_clk_init,
1210         .enable                 = tegra12_super_clk_enable,
1211         .disable                = tegra12_super_clk_disable,
1212         .set_parent             = tegra12_super_clk_set_parent,
1213         .set_rate               = tegra12_super_clk_set_rate,
1214 };
1215
1216 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
1217 static void tegra13_super_cclk_init(struct clk *c)
1218 {
1219         u32 val;
1220         int source;
1221         int shift;
1222         const struct clk_mux_sel *sel;
1223         val = clk_readlx(c->reg + SUPER_CLK_MUX);
1224         c->state = ON;
1225
1226         shift  = CLK13_SOURCE_SHIFT;
1227         source = (val >> shift) & CLK13_SOURCE_MASK;
1228
1229         for (sel = c->inputs; sel->input != NULL; sel++) {
1230                 if (sel->value == source)
1231                         break;
1232         }
1233         BUG_ON(sel->input == NULL);
1234         c->parent = sel->input;
1235
1236         if (c->flags & DIV_U71) {
1237                 c->mul = 2;
1238                 c->div = 2;
1239
1240                 /*
1241                  * Make sure 7.1 divider is 1:1; clear h/w skipper control -
1242                  * it will be enabled by soctherm later
1243                  */
1244                 val = clk_readlx(c->reg + SUPER_CLK_DIVIDER);
1245                 BUG_ON(val & SUPER_CLOCK_DIV_U71_MASK);
1246                 val = 0;
1247                 clk_writelx(val, c->reg + SUPER_CLK_DIVIDER);
1248         }
1249         else
1250                 clk_writelx(0, c->reg + SUPER_CLK_DIVIDER);
1251 }
1252
1253 static int tegra13_super_cclk_enable(struct clk *c)
1254 {
1255         return 0;
1256 }
1257
1258 static void tegra13_super_cclk_disable(struct clk *c)
1259 {
1260         /* since tegra13 has 1 CPU super clocks that is never disabled
1261            by clock framework accept request with no affect on h/w */
1262 }
1263
1264 static int tegra13_super_cclk_set_parent(struct clk *c, struct clk *p)
1265 {
1266         u32 val;
1267         const struct clk_mux_sel *sel;
1268         int shift;
1269
1270         val = clk_readlx(c->reg);
1271         shift = CLK13_SOURCE_SHIFT;
1272         for (sel = c->inputs; sel->input != NULL; sel++) {
1273                 if (sel->input == p) {
1274                         val &= ~(CLK13_SOURCE_MASK << shift);
1275                         val |= (sel->value & CLK13_SOURCE_MASK) << shift;
1276
1277                         if (c->flags & DIV_U71) {
1278                                 /* Make sure 7.1 divider is 1:1 */
1279                                 u32 div = clk_readlx(c->reg + SUPER_CLK_DIVIDER);
1280                                 BUG_ON(div & SUPER_CLOCK_DIV_U71_MASK);
1281                         }
1282
1283                         if (c->refcnt)
1284                                 clk_enable(p);
1285
1286                         clk_writelx_delay(val, c->reg);
1287
1288                         if (c->refcnt && c->parent)
1289                                 clk_disable(c->parent);
1290
1291                         clk_reparent(c, p);
1292                         return 0;
1293                 }
1294         }
1295         return -EINVAL;
1296 }
1297
1298 /*
1299  * Do not use super clocks "skippers", since dividing using a clock skipper
1300  * does not allow the voltage to be scaled down. Instead adjust the rate of
1301  * the parent clock. This requires that the parent of a super clock have no
1302  * other children, otherwise the rate will change underneath the other
1303  * children.
1304  */
1305 static int tegra13_super_cclk_set_rate(struct clk *c, unsigned long rate)
1306 {
1307         /* In tegra12_cpu_clk_set_plls() op (shared with tegra13 as well)
1308          * this call is skipped by directly setting rate of source plls. If we
1309          * ever use 7.1 divider at other than 1:1 setting, or exercise s/w
1310          * skipper control, not only this function, but cpu and sbus set_rate
1311          * APIs should be changed accordingly.
1312          */
1313         return clk_set_rate(c->parent, rate);
1314 }
1315
1316 static struct clk_ops tegra13_super_cclk_ops = {
1317         .init                   = tegra13_super_cclk_init,
1318         .enable                 = tegra13_super_cclk_enable,
1319         .disable                = tegra13_super_cclk_disable,
1320         .set_parent             = tegra13_super_cclk_set_parent,
1321         .set_rate               = tegra13_super_cclk_set_rate,
1322 };
1323 #endif
1324
1325 /* virtual cpu clock functions */
1326 /* some clocks can not be stopped (cpu, memory bus) while the SoC is running.
1327    To change the frequency of these clocks, the parent pll may need to be
1328    reprogrammed, so the clock must be moved off the pll, the pll reprogrammed,
1329    and then the clock moved back to the pll.  To hide this sequence, a virtual
1330    clock handles it.
1331  */
1332 static void tegra12_cpu_clk_init(struct clk *c)
1333 {
1334         c->state = (!is_lp_cluster() == (c->u.cpu.mode == MODE_G))? ON : OFF;
1335 }
1336
1337 static int tegra12_cpu_clk_enable(struct clk *c)
1338 {
1339         return 0;
1340 }
1341
1342 static void tegra12_cpu_clk_disable(struct clk *c)
1343 {
1344         /*
1345          * tegra12 has 2 virtual CPU clocks - low power lp-mode clock
1346          * and geared up g-mode clock - mode switch may request to disable
1347          * either of them; tegra13 that shares CPU ops with tegra12 has
1348          * only one virtual CPU that is never disabled; in any case accept
1349          * request with no affect on h/w/
1350          */
1351 }
1352
1353 static int tegra12_cpu_clk_set_plls(struct clk *c, unsigned long rate,
1354                                     unsigned long old_rate)
1355 {
1356         int ret = 0;
1357         bool on_main = false;
1358         unsigned long backup_rate, main_rate;
1359         unsigned long vco_min = c->u.cpu.main->u.pll.vco_min;
1360
1361         /*
1362          * Take an extra reference to the main pll so it doesn't turn off when
1363          * we move the cpu off of it. If possible, use main pll dynamic ramp
1364          * to reach target rate in one shot. Otherwise, use dynamic ramp to
1365          * lower current rate to pll VCO minimum level before switching to
1366          * backup source.
1367          */
1368         if (c->parent->parent == c->u.cpu.main) {
1369                 bool dramp = (rate > c->u.cpu.backup_rate) &&
1370                         tegra12_is_dyn_ramp(c->u.cpu.main, rate, false);
1371                 clk_enable(c->u.cpu.main);
1372                 on_main = true;
1373
1374                 if (dramp ||
1375                     ((old_rate > vco_min) &&
1376                      tegra12_is_dyn_ramp(c->u.cpu.main, vco_min, false))) {
1377                         main_rate = dramp ? rate : vco_min;
1378                         ret = clk_set_rate(c->u.cpu.main, main_rate);
1379                         if (ret) {
1380                                 pr_err("Failed to set cpu rate %lu on source"
1381                                        " %s\n", main_rate, c->u.cpu.main->name);
1382                                 goto out;
1383                         }
1384                         if (dramp)
1385                                 goto out;
1386                 } else if (old_rate > vco_min) {
1387 #if PLLXC_USE_DYN_RAMP
1388                         pr_warn("No dynamic ramp down: %s: %lu to %lu\n",
1389                                 c->u.cpu.main->name, old_rate, vco_min);
1390 #endif
1391                 }
1392         }
1393
1394         /* Switch to back-up source, and stay on it if target rate is below
1395            backup rate */
1396         if (c->parent->parent != c->u.cpu.backup) {
1397                 ret = clk_set_parent(c->parent, c->u.cpu.backup);
1398                 if (ret) {
1399                         pr_err("Failed to switch cpu to %s\n",
1400                                c->u.cpu.backup->name);
1401                         goto out;
1402                 }
1403         }
1404
1405         backup_rate = min(rate, c->u.cpu.backup_rate);
1406         if (backup_rate != clk_get_rate_locked(c)) {
1407                 ret = clk_set_rate(c->u.cpu.backup, backup_rate);
1408                 if (ret) {
1409                         pr_err("Failed to set cpu rate %lu on backup source\n",
1410                                backup_rate);
1411                         goto out;
1412                 }
1413         }
1414         if (rate == backup_rate)
1415                 goto out;
1416
1417         /* Switch from backup source to main at rate not exceeding pll VCO
1418            minimum. Use dynamic ramp to reach target rate if it is above VCO
1419            minimum. */
1420         main_rate = rate;
1421         if (rate > vco_min) {
1422                 if (tegra12_is_dyn_ramp(c->u.cpu.main, rate, true))
1423                         main_rate = vco_min;
1424 #if PLLXC_USE_DYN_RAMP
1425                 else
1426                         pr_warn("No dynamic ramp up: %s: %lu to %lu\n",
1427                                 c->u.cpu.main->name, vco_min, rate);
1428 #endif
1429         }
1430
1431         ret = clk_set_rate(c->u.cpu.main, main_rate);
1432         if (ret) {
1433                 pr_err("Failed to set cpu rate %lu on source"
1434                        " %s\n", main_rate, c->u.cpu.main->name);
1435                 goto out;
1436         }
1437         ret = clk_set_parent(c->parent, c->u.cpu.main);
1438         if (ret) {
1439                 pr_err("Failed to switch cpu to %s\n", c->u.cpu.main->name);
1440                 goto out;
1441         }
1442         if (rate != main_rate) {
1443                 ret = clk_set_rate(c->u.cpu.main, rate);
1444                 if (ret) {
1445                         pr_err("Failed to set cpu rate %lu on source"
1446                                " %s\n", rate, c->u.cpu.main->name);
1447                         goto out;
1448                 }
1449         }
1450
1451 out:
1452         if (on_main)
1453                 clk_disable(c->u.cpu.main);
1454
1455         return ret;
1456 }
1457
1458 static int tegra12_cpu_clk_dfll_on(struct clk *c, unsigned long rate,
1459                                    unsigned long old_rate)
1460 {
1461         int ret;
1462         struct clk *dfll = c->u.cpu.dynamic;
1463         unsigned long dfll_rate_min = c->dvfs->dfll_data.use_dfll_rate_min;
1464
1465         /* dfll rate request */
1466         ret = clk_set_rate(dfll, rate);
1467         if (ret) {
1468                 pr_err("Failed to set cpu rate %lu on source"
1469                        " %s\n", rate, dfll->name);
1470                 return ret;
1471         }
1472
1473         /* 1st time - switch to dfll */
1474         if (c->parent->parent != dfll) {
1475                 if (max(old_rate, rate) < dfll_rate_min) {
1476                         /* set interim cpu dvfs rate at dfll_rate_min to
1477                            prevent voltage drop below dfll Vmin */
1478                         ret = tegra_dvfs_set_rate(c, dfll_rate_min);
1479                         if (ret) {
1480                                 pr_err("Failed to set cpu dvfs rate %lu\n",
1481                                        dfll_rate_min);
1482                                 return ret;
1483                         }
1484                 }
1485
1486                 tegra_dvfs_rail_mode_updating(tegra_cpu_rail, true);
1487                 ret = clk_set_parent(c->parent, dfll);
1488                 if (ret) {
1489                         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1490                         pr_err("Failed to switch cpu to %s\n", dfll->name);
1491                         return ret;
1492                 }
1493                 ret = tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1494                 WARN(ret, "Failed to lock %s at rate %lu\n", dfll->name, rate);
1495
1496                 /* prevent legacy dvfs voltage scaling */
1497                 tegra_dvfs_dfll_mode_set(c->dvfs, rate);
1498                 tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1499         }
1500         return 0;
1501 }
1502
1503 static int tegra12_cpu_clk_dfll_off(struct clk *c, unsigned long rate,
1504                                     unsigned long old_rate)
1505 {
1506         int ret;
1507         struct clk *pll;
1508         struct clk *dfll = c->u.cpu.dynamic;
1509         unsigned long dfll_rate_min = c->dvfs->dfll_data.use_dfll_rate_min;
1510
1511         rate = min(rate, c->max_rate - c->dvfs->dfll_data.max_rate_boost);
1512         pll = (rate <= c->u.cpu.backup_rate) ? c->u.cpu.backup : c->u.cpu.main;
1513         dfll_rate_min = max(rate, dfll_rate_min);
1514
1515         /* set target rate last time in dfll mode */
1516         if (old_rate != dfll_rate_min) {
1517                 ret = tegra_dvfs_set_rate(c, dfll_rate_min);
1518                 if (!ret)
1519                         ret = clk_set_rate(dfll, dfll_rate_min);
1520
1521                 if (ret) {
1522                         pr_err("Failed to set cpu rate %lu on source %s\n",
1523                                dfll_rate_min, dfll->name);
1524                         return ret;
1525                 }
1526         }
1527
1528         /* unlock dfll - release volatge rail control */
1529         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, true);
1530         ret = tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 0);
1531         if (ret) {
1532                 pr_err("Failed to unlock %s\n", dfll->name);
1533                 goto back_to_dfll;
1534         }
1535
1536         /* restore legacy dvfs operations and set appropriate voltage */
1537         ret = tegra_dvfs_dfll_mode_clear(c->dvfs, dfll_rate_min);
1538         if (ret) {
1539                 pr_err("Failed to set cpu rail for rate %lu\n", rate);
1540                 goto back_to_dfll;
1541         }
1542
1543         /* set pll to target rate and return to pll source */
1544         ret = clk_set_rate(pll, rate);
1545         if (ret) {
1546                 pr_err("Failed to set cpu rate %lu on source"
1547                        " %s\n", rate, pll->name);
1548                 goto back_to_dfll;
1549         }
1550         ret = clk_set_parent(c->parent, pll);
1551         if (ret) {
1552                 pr_err("Failed to switch cpu to %s\n", pll->name);
1553                 goto back_to_dfll;
1554         }
1555
1556         /* If going up, adjust voltage here (down path is taken care of by the
1557            framework after set rate exit) */
1558         if (old_rate <= rate)
1559                 tegra_dvfs_set_rate(c, rate);
1560
1561         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1562         return 0;
1563
1564 back_to_dfll:
1565         tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1566         tegra_dvfs_dfll_mode_set(c->dvfs, old_rate);
1567         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1568         return ret;
1569 }
1570
1571 static int tegra12_cpu_clk_set_rate(struct clk *c, unsigned long rate)
1572 {
1573         unsigned long old_rate = clk_get_rate_locked(c);
1574         bool has_dfll = c->u.cpu.dynamic &&
1575                 (c->u.cpu.dynamic->state != UNINITIALIZED);
1576         bool is_dfll = c->parent->parent == c->u.cpu.dynamic;
1577
1578         /* On SILICON allow CPU rate change only if cpu regulator is connected.
1579            Ignore regulator connection on FPGA and SIMULATION platforms. */
1580         if (c->dvfs && tegra_platform_is_silicon()) {
1581                 if (!c->dvfs->dvfs_rail)
1582                         return -ENOSYS;
1583                 else if ((!c->dvfs->dvfs_rail->reg) && (old_rate < rate) &&
1584                          (c->boot_rate < rate)) {
1585                         WARN(1, "Increasing CPU rate while regulator is not"
1586                                 " ready is not allowed\n");
1587                         return -ENOSYS;
1588                 }
1589         }
1590         if (has_dfll && c->dvfs && c->dvfs->dvfs_rail) {
1591                 if (tegra_dvfs_is_dfll_range(c->dvfs, rate))
1592                         return tegra12_cpu_clk_dfll_on(c, rate, old_rate);
1593                 else if (is_dfll)
1594                         return tegra12_cpu_clk_dfll_off(c, rate, old_rate);
1595         }
1596         return tegra12_cpu_clk_set_plls(c, rate, old_rate);
1597 }
1598
1599 static long tegra12_cpu_clk_round_rate(struct clk *c, unsigned long rate)
1600 {
1601         unsigned long max_rate = c->max_rate;
1602
1603         /* Remove dfll boost to maximum rate when running on PLL */
1604         if (c->dvfs && !tegra_dvfs_is_dfll_scale(c->dvfs, rate))
1605                 max_rate -= c->dvfs->dfll_data.max_rate_boost;
1606
1607         if (rate > max_rate)
1608                 rate = max_rate;
1609         else if (rate < c->min_rate)
1610                 rate = c->min_rate;
1611         return rate;
1612 }
1613
1614 static struct clk_ops tegra_cpu_ops = {
1615         .init     = tegra12_cpu_clk_init,
1616         .enable   = tegra12_cpu_clk_enable,
1617         .disable  = tegra12_cpu_clk_disable,
1618         .set_rate = tegra12_cpu_clk_set_rate,
1619         .round_rate = tegra12_cpu_clk_round_rate,
1620 };
1621
1622
1623 static void tegra12_cpu_cmplx_clk_init(struct clk *c)
1624 {
1625         int i = !!is_lp_cluster();
1626
1627         BUG_ON(c->inputs[0].input->u.cpu.mode != MODE_G);
1628 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
1629         BUG_ON(c->inputs[1].input->u.cpu.mode != MODE_LP);
1630 #endif
1631         c->parent = c->inputs[i].input;
1632 }
1633
1634 /* cpu complex clock provides second level vitualization (on top of
1635    cpu virtual cpu rate control) in order to hide the CPU mode switch
1636    sequence */
1637 #if PARAMETERIZE_CLUSTER_SWITCH
1638 static unsigned int switch_delay;
1639 static unsigned int switch_flags;
1640 static DEFINE_SPINLOCK(parameters_lock);
1641
1642 void tegra_cluster_switch_set_parameters(unsigned int us, unsigned int flags)
1643 {
1644         spin_lock(&parameters_lock);
1645         switch_delay = us;
1646         switch_flags = flags;
1647         spin_unlock(&parameters_lock);
1648 }
1649 #endif
1650
1651 static int tegra12_cpu_cmplx_clk_enable(struct clk *c)
1652 {
1653         return 0;
1654 }
1655
1656 static void tegra12_cpu_cmplx_clk_disable(struct clk *c)
1657 {
1658         pr_debug("%s on clock %s\n", __func__, c->name);
1659
1660         /* oops - don't disable the CPU complex clock! */
1661         BUG();
1662 }
1663
1664 static int tegra12_cpu_cmplx_clk_set_rate(struct clk *c, unsigned long rate)
1665 {
1666         unsigned long flags;
1667         int ret;
1668         struct clk *parent = c->parent;
1669
1670         if (!parent->ops || !parent->ops->set_rate)
1671                 return -ENOSYS;
1672
1673         clk_lock_save(parent, &flags);
1674
1675         ret = clk_set_rate_locked(parent, rate);
1676
1677         clk_unlock_restore(parent, &flags);
1678
1679         return ret;
1680 }
1681
1682 static int tegra12_cpu_cmplx_clk_set_parent(struct clk *c, struct clk *p)
1683 {
1684         int ret;
1685         unsigned int flags, delay;
1686         const struct clk_mux_sel *sel;
1687         unsigned long rate = clk_get_rate(c->parent);
1688         struct clk *dfll = c->parent->u.cpu.dynamic ? : p->u.cpu.dynamic;
1689         struct clk *p_source_old = NULL;
1690         struct clk *p_source;
1691
1692         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
1693         BUG_ON(c->parent->u.cpu.mode != (is_lp_cluster() ? MODE_LP : MODE_G));
1694
1695         for (sel = c->inputs; sel->input != NULL; sel++) {
1696                 if (sel->input == p)
1697                         break;
1698         }
1699         if (!sel->input)
1700                 return -EINVAL;
1701
1702 #if PARAMETERIZE_CLUSTER_SWITCH
1703         spin_lock(&parameters_lock);
1704         flags = switch_flags;
1705         delay = switch_delay;
1706         switch_flags = 0;
1707         spin_unlock(&parameters_lock);
1708
1709         if (flags) {
1710                 /* over/under-clocking after switch - allow, but update rate */
1711                 if ((rate > p->max_rate) || (rate < p->min_rate)) {
1712                         rate = rate > p->max_rate ? p->max_rate : p->min_rate;
1713                         ret = clk_set_rate(c->parent, rate);
1714                         if (ret) {
1715                                 pr_err("%s: Failed to set rate %lu for %s\n",
1716                                         __func__, rate, p->name);
1717                                 return ret;
1718                         }
1719                 }
1720         } else
1721 #endif
1722         {
1723                 if (rate > p->max_rate) {       /* over-clocking - no switch */
1724                         pr_warn("%s: No %s mode switch to %s at rate %lu\n",
1725                                  __func__, c->name, p->name, rate);
1726                         return -ECANCELED;
1727                 }
1728                 flags = TEGRA_POWER_CLUSTER_IMMEDIATE;
1729                 flags |= TEGRA_POWER_CLUSTER_PART_DEFAULT;
1730                 delay = 0;
1731         }
1732         flags |= (p->u.cpu.mode == MODE_LP) ? TEGRA_POWER_CLUSTER_LP :
1733                 TEGRA_POWER_CLUSTER_G;
1734
1735         if (p == c->parent) {
1736                 if (flags & TEGRA_POWER_CLUSTER_FORCE) {
1737                         /* Allow parameterized switch to the same mode */
1738                         ret = tegra_cluster_control(delay, flags);
1739                         if (ret)
1740                                 pr_err("%s: Failed to force %s mode to %s\n",
1741                                        __func__, c->name, p->name);
1742                         return ret;
1743                 }
1744                 return 0;       /* already switched - exit */
1745         }
1746
1747         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, true);
1748         if (c->parent->parent->parent == dfll) {
1749                 /* G (DFLL selected as clock source) => LP switch:
1750                  * turn DFLL into open loop mode ("release" VDD_CPU rail)
1751                  * select target p_source for LP, and get its rate ready
1752                  */
1753                 ret = tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 0);
1754                 if (ret)
1755                         goto abort;
1756
1757                 p_source = rate <= p->u.cpu.backup_rate ?
1758                         p->u.cpu.backup : p->u.cpu.main;
1759                 ret = clk_set_rate(p_source, rate);
1760                 if (ret)
1761                         goto abort;
1762         } else if ((p->parent->parent == dfll) ||
1763                    (p->dvfs && tegra_dvfs_is_dfll_range(p->dvfs, rate))) {
1764                 /* LP => G (DFLL selected as clock source) switch:
1765                  * set DFLL rate ready (DFLL is still disabled)
1766                  * (set target p_source as dfll, G source is already selected)
1767                  */
1768                 p_source = dfll;
1769                 ret = clk_set_rate(dfll,
1770                         tegra_dvfs_rail_is_dfll_mode(tegra_cpu_rail) ? rate :
1771                         max(rate, p->dvfs->dfll_data.use_dfll_rate_min));
1772                 if (ret)
1773                         goto abort;
1774
1775                 ret = tegra_dvfs_rail_dfll_mode_set_cold(tegra_cpu_rail, dfll);
1776                 if (ret)
1777                         goto abort;
1778
1779         } else
1780                 /* DFLL is not selected on either side of the switch:
1781                  * set target p_source equal to current clock source
1782                  */
1783                 p_source = c->parent->parent->parent;
1784
1785         /* Switch new parent to target clock source if necessary */
1786         if (p->parent->parent != p_source) {
1787                 clk_enable(p->parent->parent);
1788                 clk_enable(p->parent);
1789                 p_source_old = p->parent->parent;
1790                 ret = clk_set_parent(p->parent, p_source);
1791                 if (ret) {
1792                         pr_err("%s: Failed to set parent %s for %s\n",
1793                                __func__, p_source->name, p->name);
1794                         goto abort;
1795                 }
1796         }
1797
1798         /* Enabling new parent scales new mode voltage rail in advanvce
1799            before the switch happens (if p_source is DFLL: open loop mode) */
1800         if (c->refcnt)
1801                 clk_enable(p);
1802
1803         /* switch CPU mode */
1804         ret = tegra_cluster_control(delay, flags);
1805         if (ret) {
1806                 if (c->refcnt)
1807                         clk_disable(p);
1808                 pr_err("%s: Failed to switch %s mode to %s\n",
1809                        __func__, c->name, p->name);
1810                 goto abort;
1811         }
1812
1813         /*
1814          * Lock DFLL now (resume closed loop VDD_CPU control).
1815          * G CPU operations are resumed on DFLL if it was the last G CPU
1816          * clock source, or if resume rate is in DFLL usage range in case
1817          * when auto-switch between PLL and DFLL is enabled.
1818          */
1819         if (p_source == dfll) {
1820                 if (tegra_dvfs_rail_is_dfll_mode(tegra_cpu_rail)) {
1821                         tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1822                 } else {
1823                         clk_set_rate(dfll, rate);
1824                         tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1825                         tegra_dvfs_dfll_mode_set(p->dvfs, rate);
1826                 }
1827         }
1828
1829         /* Disabling old parent scales old mode voltage rail */
1830         if (c->refcnt)
1831                 clk_disable(c->parent);
1832         if (p_source_old) {
1833                 clk_disable(p->parent);
1834                 clk_disable(p_source_old);
1835         }
1836
1837         clk_reparent(c, p);
1838
1839         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1840         return 0;
1841
1842 abort:
1843         /* Re-lock DFLL if necessary after aborted switch */
1844         if (c->parent->parent->parent == dfll) {
1845                 clk_set_rate(dfll, rate);
1846                 tegra_clk_cfg_ex(dfll, TEGRA_CLK_DFLL_LOCK, 1);
1847         }
1848         if (p_source_old) {
1849                 clk_disable(p->parent);
1850                 clk_disable(p_source_old);
1851         }
1852         tegra_dvfs_rail_mode_updating(tegra_cpu_rail, false);
1853
1854         pr_err("%s: aborted switch from %s to %s\n",
1855                __func__, c->parent->name, p->name);
1856         return ret;
1857 }
1858
1859 static long tegra12_cpu_cmplx_round_rate(struct clk *c,
1860         unsigned long rate)
1861 {
1862         return clk_round_rate(c->parent, rate);
1863 }
1864
1865 static struct clk_ops tegra_cpu_cmplx_ops = {
1866         .init     = tegra12_cpu_cmplx_clk_init,
1867         .enable   = tegra12_cpu_cmplx_clk_enable,
1868         .disable  = tegra12_cpu_cmplx_clk_disable,
1869         .set_rate = tegra12_cpu_cmplx_clk_set_rate,
1870         .set_parent = tegra12_cpu_cmplx_clk_set_parent,
1871         .round_rate = tegra12_cpu_cmplx_round_rate,
1872 };
1873
1874 /* virtual cop clock functions. Used to acquire the fake 'cop' clock to
1875  * reset the COP block (i.e. AVP) */
1876 static void tegra12_cop_clk_reset(struct clk *c, bool assert)
1877 {
1878         unsigned long reg = assert ? RST_DEVICES_SET_L : RST_DEVICES_CLR_L;
1879
1880         pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
1881         clk_writel(1 << 1, reg);
1882 }
1883
1884 static struct clk_ops tegra_cop_ops = {
1885         .reset    = tegra12_cop_clk_reset,
1886 };
1887
1888 /* bus clock functions */
1889 static DEFINE_SPINLOCK(bus_clk_lock);
1890
1891 static int bus_set_div(struct clk *c, int div)
1892 {
1893         u32 val;
1894         unsigned long flags;
1895
1896         if (!div || (div > (BUS_CLK_DIV_MASK + 1)))
1897                 return -EINVAL;
1898
1899         spin_lock_irqsave(&bus_clk_lock, flags);
1900         val = clk_readl(c->reg);
1901         val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
1902         val |= (div - 1) << c->reg_shift;
1903         clk_writel(val, c->reg);
1904         c->div = div;
1905         spin_unlock_irqrestore(&bus_clk_lock, flags);
1906
1907         return 0;
1908 }
1909
1910 static void tegra12_bus_clk_init(struct clk *c)
1911 {
1912         u32 val = clk_readl(c->reg);
1913         c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
1914         c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
1915         c->mul = 1;
1916 }
1917
1918 static int tegra12_bus_clk_enable(struct clk *c)
1919 {
1920         u32 val = clk_readl(c->reg);
1921         val &= ~(BUS_CLK_DISABLE << c->reg_shift);
1922         clk_writel(val, c->reg);
1923         return 0;
1924 }
1925
1926 static void tegra12_bus_clk_disable(struct clk *c)
1927 {
1928         u32 val = clk_readl(c->reg);
1929         val |= BUS_CLK_DISABLE << c->reg_shift;
1930         clk_writel(val, c->reg);
1931 }
1932
1933 static int tegra12_bus_clk_set_rate(struct clk *c, unsigned long rate)
1934 {
1935         unsigned long parent_rate = clk_get_rate(c->parent);
1936         int i;
1937
1938         if (tegra_platform_is_qt())
1939                 return 0;
1940         for (i = 1; i <= 4; i++) {
1941                 if (rate >= parent_rate / i)
1942                         return bus_set_div(c, i);
1943         }
1944         return -EINVAL;
1945 }
1946
1947 static struct clk_ops tegra_bus_ops = {
1948         .init                   = tegra12_bus_clk_init,
1949         .enable                 = tegra12_bus_clk_enable,
1950         .disable                = tegra12_bus_clk_disable,
1951         .set_rate               = tegra12_bus_clk_set_rate,
1952 };
1953
1954 /* Virtual system bus complex clock is used to hide the sequence of
1955    changing sclk/hclk/pclk parents and dividers to configure requested
1956    sclk target rate. */
1957 static void tegra12_sbus_cmplx_init(struct clk *c)
1958 {
1959         unsigned long rate;
1960
1961         c->max_rate = c->parent->max_rate;
1962         c->min_rate = c->parent->min_rate;
1963
1964         /* Threshold must be an exact proper factor of low range parent,
1965            and both low/high range parents have 7.1 fractional dividers */
1966         rate = clk_get_rate(c->u.system.sclk_low->parent);
1967         if (tegra_platform_is_qt())
1968                 return;
1969         if (c->u.system.threshold) {
1970                 BUG_ON(c->u.system.threshold > rate) ;
1971                 BUG_ON((rate % c->u.system.threshold) != 0);
1972         }
1973         BUG_ON(!(c->u.system.sclk_low->flags & DIV_U71));
1974         BUG_ON(!(c->u.system.sclk_high->flags & DIV_U71));
1975 }
1976
1977 /* This special sbus round function is implemented because:
1978  *
1979  * (a) sbus complex clock source is selected automatically based on rate
1980  *
1981  * (b) since sbus is a shared bus, and its frequency is set to the highest
1982  * enabled shared_bus_user clock, the target rate should be rounded up divider
1983  * ladder (if max limit allows it) - for pll_div and peripheral_div common is
1984  * rounding down - special case again.
1985  *
1986  * Note that final rate is trimmed (not rounded up) to avoid spiraling up in
1987  * recursive calls. Lost 1Hz is added in tegra12_sbus_cmplx_set_rate before
1988  * actually setting divider rate.
1989  */
1990 static long tegra12_sbus_cmplx_round_updown(struct clk *c, unsigned long rate,
1991                                             bool up)
1992 {
1993         unsigned long round_rate;
1994         struct clk *new_parent;
1995
1996         rate = max(rate, c->min_rate);
1997
1998         new_parent = (rate <= c->u.system.threshold) ?
1999                 c->u.system.sclk_low : c->u.system.sclk_high;
2000
2001         round_rate = fixed_src_bus_round_updown(
2002                 c, new_parent->parent, new_parent->flags, rate, up);
2003
2004         if (new_parent == c->u.system.sclk_high) {
2005                 /* Prevent oscillation across threshold */
2006                 if (round_rate <= c->u.system.threshold)
2007                         round_rate = c->u.system.threshold;
2008         }
2009         return round_rate;
2010 }
2011
2012 static long tegra12_sbus_cmplx_round_rate(struct clk *c, unsigned long rate)
2013 {
2014         return tegra12_sbus_cmplx_round_updown(c, rate, true);
2015 }
2016
2017 /*
2018  * FIXME: This limitation may have been relaxed on Tegra12.
2019  * This issue has to be visited again once the new limitation is clarified.
2020  *
2021  * Limitations on SCLK/HCLK/PCLK dividers:
2022  * (A) H/w limitation:
2023  *      if SCLK >= 60MHz, SCLK:PCLK >= 2
2024  * (B) S/w policy limitation, in addition to (A):
2025  *      if any APB bus shared user request is enabled, HCLK:PCLK >= 2
2026  *  Reason for (B): assuming APB bus shared user has requested X < 60MHz,
2027  *  HCLK = PCLK = X, and new AHB user is coming on-line requesting Y >= 60MHz,
2028  *  we can consider 2 paths depending on order of changing HCLK rate and
2029  *  HCLK:PCLK ratio
2030  *  (i)  HCLK:PCLK = X:X => Y:Y* => Y:Y/2,   (*) violates rule (A)
2031  *  (ii) HCLK:PCLK = X:X => X:X/2* => Y:Y/2, (*) under-clocks APB user
2032  *  In this case we can not guarantee safe transition from HCLK:PCLK = 1:1
2033  *  below 60MHz to HCLK rate above 60MHz without under-clocking APB user.
2034  *  Hence, policy (B).
2035  *
2036  *  Note: when there are no request from APB users, path (ii) can be used to
2037  *  increase HCLK above 60MHz, and HCLK:PCLK = 1:1 is allowed.
2038  */
2039
2040 #define SCLK_PCLK_UNITY_RATIO_RATE_MAX  60000000
2041 #define BUS_AHB_DIV_MAX                 (BUS_CLK_DIV_MASK + 1UL)
2042 #define BUS_APB_DIV_MAX                 (BUS_CLK_DIV_MASK + 1UL)
2043
2044 static int tegra12_sbus_cmplx_set_rate(struct clk *c, unsigned long rate)
2045 {
2046         int ret;
2047         struct clk *new_parent;
2048
2049         /*
2050          * Configure SCLK/HCLK/PCLK guranteed safe combination:
2051          * - select the appropriate sclk parent
2052          * - keep hclk at the same rate as sclk
2053          * - set pclk at 1:2 rate of hclk
2054          */
2055         bus_set_div(c->u.system.pclk, 2);
2056         bus_set_div(c->u.system.hclk, 1);
2057         c->child_bus->child_bus->div = 2;
2058         c->child_bus->div = 1;
2059
2060         if (rate == clk_get_rate_locked(c))
2061                 return 0;
2062
2063         new_parent = (rate <= c->u.system.threshold) ?
2064                 c->u.system.sclk_low : c->u.system.sclk_high;
2065
2066         ret = clk_set_rate(new_parent, rate + 1);
2067         if (ret) {
2068                 pr_err("Failed to set sclk source %s to %lu\n",
2069                        new_parent->name, rate);
2070                 return ret;
2071         }
2072
2073         if (new_parent != clk_get_parent(c->parent)) {
2074                 ret = clk_set_parent(c->parent, new_parent);
2075                 if (ret) {
2076                         pr_err("Failed to switch sclk source to %s\n",
2077                                new_parent->name);
2078                         return ret;
2079                 }
2080         }
2081
2082         return 0;
2083 }
2084
2085 static int tegra12_clk_sbus_update(struct clk *bus)
2086 {
2087         int ret, div;
2088         bool p_requested;
2089         unsigned long s_rate, h_rate, p_rate, ceiling;
2090         struct clk *ahb, *apb;
2091
2092         if (detach_shared_bus)
2093                 return 0;
2094
2095         s_rate = tegra12_clk_shared_bus_update(bus, &ahb, &apb, &ceiling);
2096         if (bus->override_rate)
2097                 return clk_set_rate_locked(bus, s_rate);
2098
2099         ahb = bus->child_bus;
2100         apb = ahb->child_bus;
2101         h_rate = ahb->u.shared_bus_user.rate;
2102         p_rate = apb->u.shared_bus_user.rate;
2103         p_requested = apb->refcnt > 1;
2104
2105         /* Propagate ratio requirements up from PCLK to SCLK */
2106         if (p_requested)
2107                 h_rate = max(h_rate, p_rate * 2);
2108         s_rate = max(s_rate, h_rate);
2109         if (s_rate >= SCLK_PCLK_UNITY_RATIO_RATE_MAX)
2110                 s_rate = max(s_rate, p_rate * 2);
2111
2112         /* Propagate cap requirements down from SCLK to PCLK */
2113         s_rate = tegra12_clk_cap_shared_bus(bus, s_rate, ceiling);
2114         if (s_rate >= SCLK_PCLK_UNITY_RATIO_RATE_MAX)
2115                 p_rate = min(p_rate, s_rate / 2);
2116         h_rate = min(h_rate, s_rate);
2117         if (p_requested)
2118                 p_rate = min(p_rate, h_rate / 2);
2119
2120
2121         /* Set new sclk rate in safe 1:1:2, rounded "up" configuration */
2122         ret = clk_set_rate_locked(bus, s_rate);
2123         if (ret)
2124                 return ret;
2125
2126         /* Finally settle new bus divider values */
2127         s_rate = clk_get_rate_locked(bus);
2128         div = min(s_rate / h_rate, BUS_AHB_DIV_MAX);
2129         if (div != 1) {
2130                 bus_set_div(bus->u.system.hclk, div);
2131                 ahb->div = div;
2132         }
2133
2134         h_rate = clk_get_rate(bus->u.system.hclk);
2135         div = min(h_rate / p_rate, BUS_APB_DIV_MAX);
2136         if (div != 2) {
2137                 bus_set_div(bus->u.system.pclk, div);
2138                 apb->div = div;
2139         }
2140
2141         return 0;
2142 }
2143
2144 static struct clk_ops tegra_sbus_cmplx_ops = {
2145         .init = tegra12_sbus_cmplx_init,
2146         .set_rate = tegra12_sbus_cmplx_set_rate,
2147         .round_rate = tegra12_sbus_cmplx_round_rate,
2148         .round_rate_updown = tegra12_sbus_cmplx_round_updown,
2149         .shared_bus_update = tegra12_clk_sbus_update,
2150 };
2151
2152 /* Blink output functions */
2153
2154 static void tegra12_blink_clk_init(struct clk *c)
2155 {
2156         u32 val;
2157
2158         val = pmc_readl(PMC_CTRL);
2159         c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
2160         c->mul = 1;
2161         val = pmc_readl(c->reg);
2162
2163         if (val & PMC_BLINK_TIMER_ENB) {
2164                 unsigned int on_off;
2165
2166                 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
2167                         PMC_BLINK_TIMER_DATA_ON_MASK;
2168                 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
2169                 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
2170                 on_off += val;
2171                 /* each tick in the blink timer is 4 32KHz clocks */
2172                 c->div = on_off * 4;
2173         } else {
2174                 c->div = 1;
2175         }
2176 }
2177
2178 static int tegra12_blink_clk_enable(struct clk *c)
2179 {
2180         u32 val;
2181
2182         val = pmc_readl(PMC_DPD_PADS_ORIDE);
2183         pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
2184
2185         val = pmc_readl(PMC_CTRL);
2186         pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
2187         pmc_readl(PMC_CTRL);
2188
2189         return 0;
2190 }
2191
2192 static void tegra12_blink_clk_disable(struct clk *c)
2193 {
2194         u32 val;
2195
2196         val = pmc_readl(PMC_CTRL);
2197         pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
2198
2199         val = pmc_readl(PMC_DPD_PADS_ORIDE);
2200         pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
2201         pmc_readl(PMC_DPD_PADS_ORIDE);
2202 }
2203
2204 static int tegra12_blink_clk_set_rate(struct clk *c, unsigned long rate)
2205 {
2206         unsigned long parent_rate = clk_get_rate(c->parent);
2207         if (rate >= parent_rate) {
2208                 c->div = 1;
2209                 pmc_writel(0, c->reg);
2210         } else {
2211                 unsigned int on_off;
2212                 u32 val;
2213
2214                 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
2215                 c->div = on_off * 8;
2216
2217                 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
2218                         PMC_BLINK_TIMER_DATA_ON_SHIFT;
2219                 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
2220                 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
2221                 val |= on_off;
2222                 val |= PMC_BLINK_TIMER_ENB;
2223                 pmc_writel(val, c->reg);
2224         }
2225         pmc_readl(c->reg);
2226
2227         return 0;
2228 }
2229
2230 static struct clk_ops tegra_blink_clk_ops = {
2231         .init                   = &tegra12_blink_clk_init,
2232         .enable                 = &tegra12_blink_clk_enable,
2233         .disable                = &tegra12_blink_clk_disable,
2234         .set_rate               = &tegra12_blink_clk_set_rate,
2235 };
2236
2237 /* PLL Functions */
2238 static int tegra12_pll_clk_wait_for_lock(
2239         struct clk *c, u32 lock_reg, u32 lock_bits)
2240 {
2241 #if USE_PLL_LOCK_BITS
2242         int i;
2243         u32 val = 0;
2244
2245         for (i = 0; i < (c->u.pll.lock_delay / PLL_PRE_LOCK_DELAY + 1); i++) {
2246                 udelay(PLL_PRE_LOCK_DELAY);
2247                 if (c->flags & PLLX)
2248                         val = clk_readlx(lock_reg);
2249                 else
2250                         val = clk_readl(lock_reg);
2251
2252                 if ((val & lock_bits) == lock_bits) {
2253                         udelay(PLL_POST_LOCK_DELAY);
2254                         return 0;
2255                 }
2256         }
2257
2258         /* PLLCX lock bits may fluctuate after the lock - do detailed reporting
2259            at debug level (phase lock bit happens to uniquely identify PLLCX) */
2260         if (lock_bits & PLLCX_BASE_PHASE_LOCK) {
2261                 pr_debug("Timed out waiting %s locks: %s %s not set\n", c->name,
2262                        val & PLL_BASE_LOCK ? "" : "frequency_lock",
2263                        val & PLLCX_BASE_PHASE_LOCK ? "" : "phase_lock");
2264                 pr_debug("base =  0x%x\n", val);
2265                 if (c->flags & PLLX) {
2266                         pr_debug("misc =  0x%x\n", clk_readlx(c->reg + PLL_MISC(c)));
2267                         pr_debug("misc1 = 0x%x\n", clk_readlx(c->reg + PLL_MISCN(c, 1)));
2268                         pr_debug("misc2 = 0x%x\n", clk_readlx(c->reg + PLL_MISCN(c, 2)));
2269                         pr_debug("misc3 = 0x%x\n", clk_readlx(c->reg + PLL_MISCN(c, 3)));
2270                 } else {
2271                         pr_debug("misc =  0x%x\n", clk_readl(c->reg + PLL_MISC(c)));
2272                         pr_debug("misc1 = 0x%x\n", clk_readl(c->reg + PLL_MISCN(c, 1)));
2273                         pr_debug("misc2 = 0x%x\n", clk_readl(c->reg + PLL_MISCN(c, 2)));
2274                         pr_debug("misc3 = 0x%x\n", clk_readl(c->reg + PLL_MISCN(c, 3)));
2275                 }
2276                 return -ETIMEDOUT;
2277         } else {
2278                 pr_err("Timed out waiting for %s lock bit ([0x%x] = 0x%x)\n",
2279                        c->name, lock_reg, val);
2280                 return -ETIMEDOUT;
2281         }
2282 #endif
2283         udelay(c->u.pll.lock_delay);
2284         return 0;
2285 }
2286
2287 static void usb_plls_hw_control_enable(u32 reg)
2288 {
2289         u32 val = clk_readl(reg);
2290         val |= USB_PLLS_USE_LOCKDET | USB_PLLS_SEQ_START_STATE;
2291         val &= ~USB_PLLS_ENABLE_SWCTL;
2292         val |= USB_PLLS_SEQ_START_STATE;
2293         pll_writel_delay(val, reg);
2294
2295         val |= USB_PLLS_SEQ_ENABLE;
2296         pll_writel_delay(val, reg);
2297 }
2298
2299 static void tegra12_utmi_param_configure(struct clk *c)
2300 {
2301         u32 reg;
2302         int i;
2303         unsigned long main_rate =
2304                 clk_get_rate(c->parent->parent);
2305
2306         for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
2307                 if (main_rate == utmi_parameters[i].osc_frequency) {
2308                         break;
2309                 }
2310         }
2311
2312         if (i >= ARRAY_SIZE(utmi_parameters)) {
2313                 pr_err("%s: Unexpected main rate %lu\n", __func__, main_rate);
2314                 return;
2315         }
2316
2317         reg = clk_readl(UTMIP_PLL_CFG2);
2318
2319         /* Program UTMIP PLL stable and active counts */
2320         /* [FIXME] arclk_rst.h says WRONG! This should be 1ms -> 0x50 Check! */
2321         reg &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
2322         reg |= UTMIP_PLL_CFG2_STABLE_COUNT(
2323                         utmi_parameters[i].stable_count);
2324
2325         reg &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
2326
2327         reg |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(
2328                         utmi_parameters[i].active_delay_count);
2329
2330         /* Remove power downs from UTMIP PLL control bits */
2331         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERUP;
2332         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERUP;
2333         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERUP;
2334         reg |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERUP;
2335         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
2336         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
2337         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
2338         reg &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERDOWN;
2339
2340         clk_writel(reg, UTMIP_PLL_CFG2);
2341
2342         /* Program UTMIP PLL delay and oscillator frequency counts */
2343         reg = clk_readl(UTMIP_PLL_CFG1);
2344         reg &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
2345
2346         reg |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(
2347                 utmi_parameters[i].enable_delay_count);
2348
2349         reg &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
2350         reg |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(
2351                 utmi_parameters[i].xtal_freq_count);
2352
2353         /* Remove power downs from UTMIP PLL control bits */
2354         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
2355         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
2356         reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERUP;
2357         reg &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
2358         clk_writel(reg, UTMIP_PLL_CFG1);
2359
2360         /* Setup HW control of UTMIPLL */
2361         reg = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
2362         reg |= UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET;
2363         reg &= ~UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL;
2364         reg |= UTMIPLL_HW_PWRDN_CFG0_SEQ_START_STATE;
2365         clk_writel(reg, UTMIPLL_HW_PWRDN_CFG0);
2366
2367         reg = clk_readl(UTMIP_PLL_CFG1);
2368         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
2369         reg &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
2370         pll_writel_delay(reg, UTMIP_PLL_CFG1);
2371
2372         /* Setup SW override of UTMIPLL assuming USB2.0
2373            ports are assigned to USB2 */
2374         reg = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
2375         reg |= UTMIPLL_HW_PWRDN_CFG0_IDDQ_SWCTL;
2376         reg |= UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE;
2377         pll_writel_delay(reg, UTMIPLL_HW_PWRDN_CFG0);
2378
2379         /* Enable HW control UTMIPLL */
2380         reg = clk_readl(UTMIPLL_HW_PWRDN_CFG0);
2381         reg |= UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE;
2382         pll_writel_delay(reg, UTMIPLL_HW_PWRDN_CFG0);
2383 }
2384
2385 static void tegra12_pll_clk_init(struct clk *c)
2386 {
2387         u32 val = clk_readl(c->reg + PLL_BASE);
2388         u32 divn_mask = c->flags & PLLD ?
2389                 PLLD_BASE_DIVN_MASK : PLL_BASE_DIVN_MASK;
2390
2391         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
2392
2393         if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
2394                 const struct clk_pll_freq_table *sel;
2395                 unsigned long input_rate = clk_get_rate(c->parent);
2396                 c->u.pll.fixed_rate = PLLP_DEFAULT_FIXED_RATE;
2397
2398                 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
2399                         if (sel->input_rate == input_rate &&
2400                                 sel->output_rate == c->u.pll.fixed_rate) {
2401                                 c->mul = sel->n;
2402                                 c->div = sel->m * sel->p;
2403                                 return;
2404                         }
2405                 }
2406                 pr_err("Clock %s has unknown fixed frequency\n", c->name);
2407                 BUG();
2408         } else if (val & PLL_BASE_BYPASS) {
2409                 c->mul = 1;
2410                 c->div = 1;
2411         } else {
2412                 c->mul = (val & divn_mask) >> PLL_BASE_DIVN_SHIFT;
2413                 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
2414                 if (c->flags & PLLU)
2415                         c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
2416                 else
2417                         c->div *= (0x1 << ((val & PLL_BASE_DIVP_MASK) >>
2418                                         PLL_BASE_DIVP_SHIFT));
2419         }
2420
2421         if (c->flags & PLL_FIXED) {
2422                 c->u.pll.fixed_rate = clk_get_rate_locked(c);
2423         }
2424
2425         if (c->flags & PLLU) {
2426                 /* Configure UTMI PLL power management */
2427                 tegra12_utmi_param_configure(c);
2428
2429                 /* Put PLLU under h/w control */
2430                 usb_plls_hw_control_enable(PLLU_HW_PWRDN_CFG0);
2431
2432                 val = clk_readl(c->reg + PLL_BASE);
2433                 val &= ~PLLU_BASE_OVERRIDE;
2434                 pll_writel_delay(val, c->reg + PLL_BASE);
2435
2436                 /* Set XUSB PLL pad pwr override and iddq */
2437                 val = xusb_padctl_readl(XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
2438                 val |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_PWR_OVRD;
2439                 val |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_IDDQ;
2440                 xusb_padctl_writel(val, XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
2441                 xusb_padctl_readl(XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
2442         }
2443 }
2444
2445 static int tegra12_pll_clk_enable(struct clk *c)
2446 {
2447         u32 val;
2448         pr_debug("%s on clock %s\n", __func__, c->name);
2449
2450 #if USE_PLL_LOCK_BITS
2451         /* toggle lock enable bit to reset lock detection circuit (couple
2452            register reads provide enough duration for reset pulse) */
2453         val = clk_readl(c->reg + PLL_MISC(c));
2454         val &= ~PLL_MISC_LOCK_ENABLE(c);
2455         clk_writel(val, c->reg + PLL_MISC(c));
2456         val = clk_readl(c->reg + PLL_MISC(c));
2457         val = clk_readl(c->reg + PLL_MISC(c));
2458         val |= PLL_MISC_LOCK_ENABLE(c);
2459         clk_writel(val, c->reg + PLL_MISC(c));
2460 #endif
2461         val = clk_readl(c->reg + PLL_BASE);
2462         val &= ~PLL_BASE_BYPASS;
2463         val |= PLL_BASE_ENABLE;
2464         clk_writel(val, c->reg + PLL_BASE);
2465
2466         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
2467
2468         return 0;
2469 }
2470
2471 static void tegra12_pll_clk_disable(struct clk *c)
2472 {
2473         u32 val;
2474         pr_debug("%s on clock %s\n", __func__, c->name);
2475
2476         val = clk_readl(c->reg);
2477         val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
2478         if (tegra_platform_is_qt())
2479                 return;
2480         clk_writel(val, c->reg);
2481 }
2482
2483 /* Special comparison frequency selection for PLLD at 12MHz refrence rate */
2484 unsigned long get_pll_cfreq_special(struct clk *c, unsigned long input_rate,
2485                                    unsigned long rate, unsigned long *vco)
2486 {
2487         if (!(c->flags & PLLD) || (input_rate != 12000000))
2488                 return 0;
2489
2490         *vco = c->u.pll.vco_min;
2491
2492         if (rate <= 250000000)
2493                 return 4000000;
2494         else if (rate <= 500000000)
2495                 return 2000000;
2496         else
2497                 return 1000000;
2498 }
2499
2500 /* Common comparison frequency selection */
2501 unsigned long get_pll_cfreq_common(struct clk *c, unsigned long input_rate,
2502                                    unsigned long rate, unsigned long *vco)
2503 {
2504         unsigned long cfreq = 0;
2505
2506         switch (input_rate) {
2507         case 12000000:
2508         case 26000000:
2509                 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
2510                 break;
2511         case 13000000:
2512                 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
2513                 break;
2514         case 16800000:
2515         case 19200000:
2516                 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
2517                 break;
2518         default:
2519                 if (c->parent->flags & DIV_U71_FIXED) {
2520                         /* PLLP_OUT1 rate is not in PLLA table */
2521                         pr_warn("%s: failed %s ref/out rates %lu/%lu\n",
2522                                 __func__, c->name, input_rate, rate);
2523                         cfreq = input_rate/(input_rate/1000000);
2524                         break;
2525                 }
2526                 pr_err("%s: Unexpected reference rate %lu\n",
2527                        __func__, input_rate);
2528                 BUG();
2529         }
2530
2531         /* Raise VCO to guarantee 0.5% accuracy, and vco min boundary */
2532         *vco = max(200 * cfreq, c->u.pll.vco_min);
2533         return cfreq;
2534 }
2535
2536 static u8 get_pll_cpcon(struct clk *c, u16 n)
2537 {
2538         if (c->flags & PLLD) {
2539                 if (n >= 1000)
2540                         return 15;
2541                 else if (n >= 600)
2542                         return 12;
2543                 else if (n >= 300)
2544                         return 8;
2545                 else if (n >= 50)
2546                         return 3;
2547                 else
2548                         return 2;
2549         }
2550         return c->u.pll.cpcon_default ? : OUT_OF_TABLE_CPCON;
2551 }
2552
2553 static int tegra12_pll_clk_set_rate(struct clk *c, unsigned long rate)
2554 {
2555         u32 val, p_div, old_base;
2556         unsigned long input_rate;
2557         const struct clk_pll_freq_table *sel;
2558         struct clk_pll_freq_table cfg;
2559         u32 divn_mask = c->flags & PLLD ?
2560                 PLLD_BASE_DIVN_MASK : PLL_BASE_DIVN_MASK;
2561
2562         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
2563
2564         if (tegra_platform_is_qt())
2565                 return 0;
2566         if (c->flags & PLL_FIXED) {
2567                 int ret = 0;
2568                 if (rate != c->u.pll.fixed_rate) {
2569                         pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
2570                                __func__, c->name, c->u.pll.fixed_rate, rate);
2571                         ret = -EINVAL;
2572                 }
2573                 return ret;
2574         }
2575
2576         p_div = 0;
2577         input_rate = clk_get_rate(c->parent);
2578
2579         /* Check if the target rate is tabulated */
2580         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
2581                 if (sel->input_rate == input_rate && sel->output_rate == rate) {
2582                         if (c->flags & PLLU) {
2583                                 BUG_ON(sel->p < 1 || sel->p > 2);
2584                                 if (sel->p == 1)
2585                                         p_div = PLLU_BASE_POST_DIV;
2586                         } else {
2587                                 BUG_ON(sel->p < 1);
2588                                 for (val = sel->p; val > 1; val >>= 1, p_div++);
2589                                 p_div <<= PLL_BASE_DIVP_SHIFT;
2590                         }
2591                         break;
2592                 }
2593         }
2594
2595         /* Configure out-of-table rate */
2596         if (sel->input_rate == 0) {
2597                 unsigned long cfreq, vco;
2598                 BUG_ON(c->flags & PLLU);
2599                 sel = &cfg;
2600
2601                 /* If available, use pll specific algorithm to select comparison
2602                    frequency, and vco target */
2603                 cfreq = get_pll_cfreq_special(c, input_rate, rate, &vco);
2604                 if (!cfreq)
2605                         cfreq = get_pll_cfreq_common(c, input_rate, rate, &vco);
2606
2607                 for (cfg.output_rate = rate; cfg.output_rate < vco; p_div++)
2608                         cfg.output_rate <<= 1;
2609
2610                 cfg.p = 0x1 << p_div;
2611                 cfg.m = input_rate / cfreq;
2612                 cfg.n = cfg.output_rate / cfreq;
2613                 cfg.cpcon = get_pll_cpcon(c, cfg.n);
2614
2615                 if ((cfg.m > (PLL_BASE_DIVM_MASK >> PLL_BASE_DIVM_SHIFT)) ||
2616                     (cfg.n > (divn_mask >> PLL_BASE_DIVN_SHIFT)) ||
2617                     (p_div > (PLL_BASE_DIVP_MASK >> PLL_BASE_DIVP_SHIFT)) ||
2618                     (cfg.output_rate > c->u.pll.vco_max)) {
2619                         pr_err("%s: Failed to set %s out-of-table rate %lu\n",
2620                                __func__, c->name, rate);
2621                         return -EINVAL;
2622                 }
2623                 p_div <<= PLL_BASE_DIVP_SHIFT;
2624         }
2625
2626         c->mul = sel->n;
2627         c->div = sel->m * sel->p;
2628
2629         old_base = val = clk_readl(c->reg + PLL_BASE);
2630         val &= ~(PLL_BASE_DIVM_MASK | divn_mask |
2631                  ((c->flags & PLLU) ? PLLU_BASE_POST_DIV : PLL_BASE_DIVP_MASK));
2632         val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
2633                 (sel->n << PLL_BASE_DIVN_SHIFT) | p_div;
2634         if (val == old_base)
2635                 return 0;
2636
2637         if (c->state == ON) {
2638                 tegra12_pll_clk_disable(c);
2639                 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
2640         }
2641         clk_writel(val, c->reg + PLL_BASE);
2642
2643         if (c->flags & PLL_HAS_CPCON) {
2644                 val = clk_readl(c->reg + PLL_MISC(c));
2645                 val &= ~PLL_MISC_CPCON_MASK;
2646                 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
2647                 if (c->flags & (PLLU | PLLD)) {
2648                         val &= ~PLL_MISC_LFCON_MASK;
2649                         val |= PLLDU_LFCON << PLL_MISC_LFCON_SHIFT;
2650                 }
2651                 clk_writel(val, c->reg + PLL_MISC(c));
2652         }
2653
2654         if (c->state == ON)
2655                 tegra12_pll_clk_enable(c);
2656
2657         return 0;
2658 }
2659
2660 static struct clk_ops tegra_pll_ops = {
2661         .init                   = tegra12_pll_clk_init,
2662         .enable                 = tegra12_pll_clk_enable,
2663         .disable                = tegra12_pll_clk_disable,
2664         .set_rate               = tegra12_pll_clk_set_rate,
2665 };
2666
2667 static void tegra12_pllp_clk_init(struct clk *c)
2668 {
2669         tegra12_pll_clk_init(c);
2670         tegra12_pllp_init_dependencies(c->u.pll.fixed_rate);
2671 }
2672
2673 #ifdef CONFIG_PM_SLEEP
2674 static void tegra12_pllp_clk_resume(struct clk *c)
2675 {
2676         unsigned long rate = c->u.pll.fixed_rate;
2677         tegra12_pll_clk_init(c);
2678         BUG_ON(rate != c->u.pll.fixed_rate);
2679 }
2680 #endif
2681
2682 static struct clk_ops tegra_pllp_ops = {
2683         .init                   = tegra12_pllp_clk_init,
2684         .enable                 = tegra12_pll_clk_enable,
2685         .disable                = tegra12_pll_clk_disable,
2686         .set_rate               = tegra12_pll_clk_set_rate,
2687 };
2688
2689 static int
2690 tegra12_plld_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
2691 {
2692         u32 val, mask, reg;
2693         u32 clear = 0;
2694
2695         switch (p) {
2696         case TEGRA_CLK_PLLD_CSI_OUT_ENB:
2697                 mask = PLLD_BASE_CSI_CLKENABLE | PLLD_BASE_CSI_CLKSOURCE;
2698                 reg = c->reg + PLL_BASE;
2699                 break;
2700         case TEGRA_CLK_MIPI_CSI_OUT_ENB:
2701                 mask = PLLD_BASE_CSI_CLKENABLE;
2702                 clear = PLLD_BASE_CSI_CLKSOURCE;
2703                 reg = c->reg + PLL_BASE;
2704                 break;
2705         case TEGRA_CLK_PLLD_DSI_OUT_ENB:
2706                 mask = PLLD_MISC_DSI_CLKENABLE;
2707                 reg = c->reg + PLL_MISC(c);
2708                 break;
2709         case TEGRA_CLK_PLLD_MIPI_MUX_SEL:
2710                 mask = PLLD_BASE_DSI_MUX_MASK;
2711                 reg = c->reg + PLL_BASE;
2712                 break;
2713         default:
2714                 return -EINVAL;
2715         }
2716
2717         val = clk_readl(reg);
2718         if (setting) {
2719                 val |= mask;
2720                 val &= ~clear;
2721         } else
2722                 val &= ~mask;
2723         clk_writel(val, reg);
2724         return 0;
2725 }
2726
2727 static struct clk_ops tegra_plld_ops = {
2728         .init                   = tegra12_pll_clk_init,
2729         .enable                 = tegra12_pll_clk_enable,
2730         .disable                = tegra12_pll_clk_disable,
2731         .set_rate               = tegra12_pll_clk_set_rate,
2732         .clk_cfg_ex             = tegra12_plld_clk_cfg_ex,
2733 };
2734
2735 /*
2736  * Dynamic ramp PLLs:
2737  *  PLLC2 and PLLC3 (PLLCX)
2738  *  PLLX and PLLC (PLLXC)
2739  *
2740  * When scaling PLLC and PLLX, dynamic ramp is allowed for any transition that
2741  * changes NDIV only. As a matter of policy we will make sure that switching
2742  * between output rates above VCO minimum is always dynamic. The pre-requisite
2743  * for the above guarantee is the following configuration convention:
2744  * - pll configured with fixed MDIV
2745  * - when output rate is above VCO minimum PDIV = 0 (p-value = 1)
2746  * Switching between output rates below VCO minimum may or may not be dynamic,
2747  * and switching across VCO minimum is never dynamic.
2748  *
2749  * PLLC2 and PLLC3 in addition to dynamic ramp mechanism have also glitchless
2750  * output dividers. However dynamic ramp without overshoot is guaranteed only
2751  * when output divisor is less or equal 8.
2752  *
2753  * Of course, dynamic ramp is applied provided PLL is already enabled.
2754  */
2755
2756 /*
2757  * Common configuration policy for dynamic ramp PLLs:
2758  * - always set fixed M-value based on the reference rate
2759  * - always set P-value value 1:1 for output rates above VCO minimum, and
2760  *   choose minimum necessary P-value for output rates below VCO minimum
2761  * - calculate N-value based on selected M and P
2762  */
2763 static int pll_dyn_ramp_cfg(struct clk *c, struct clk_pll_freq_table *cfg,
2764         unsigned long rate, unsigned long input_rate, u32 *pdiv)
2765 {
2766         u32 p;
2767
2768         if (!rate)
2769                 return -EINVAL;
2770
2771         p = DIV_ROUND_UP(c->u.pll.vco_min, rate);
2772         p = c->u.pll.round_p_to_pdiv(p, pdiv);
2773         if (IS_ERR_VALUE(p))
2774                 return -EINVAL;
2775
2776         cfg->m = PLL_FIXED_MDIV(c, input_rate);
2777         cfg->p = p;
2778         cfg->output_rate = rate * cfg->p;
2779         if (cfg->output_rate > c->u.pll.vco_max)
2780                 cfg->output_rate = c->u.pll.vco_max;
2781         cfg->n = cfg->output_rate * cfg->m / input_rate;
2782
2783         /* can use PLLCX N-divider field layout for all dynamic ramp PLLs */
2784         if (cfg->n > (PLLCX_BASE_DIVN_MASK >> PLL_BASE_DIVN_SHIFT))
2785                 return -EINVAL;
2786
2787         return 0;
2788 }
2789
2790 static int pll_dyn_ramp_find_cfg(struct clk *c, struct clk_pll_freq_table *cfg,
2791         unsigned long rate, unsigned long input_rate, u32 *pdiv)
2792 {
2793         const struct clk_pll_freq_table *sel;
2794
2795         /* Check if the target rate is tabulated */
2796         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
2797                 if (sel->input_rate == input_rate && sel->output_rate == rate) {
2798                         u32 p = c->u.pll.round_p_to_pdiv(sel->p, pdiv);
2799                         BUG_ON(IS_ERR_VALUE(p));
2800                         BUG_ON(sel->m != PLL_FIXED_MDIV(c, input_rate));
2801                         *cfg = *sel;
2802                         return 0;
2803                 }
2804         }
2805
2806         /* Configure out-of-table rate */
2807         if (pll_dyn_ramp_cfg(c, cfg, rate, input_rate, pdiv)) {
2808                 pr_err("%s: Failed to set %s out-of-table rate %lu\n",
2809                        __func__, c->name, rate);
2810                 return -EINVAL;
2811         }
2812         return 0;
2813 }
2814
2815 static inline void pll_do_iddq(struct clk *c, u32 offs, u32 iddq_bit, bool set)
2816 {
2817         u32 val;
2818         if (c->flags & PLLX)
2819                 val = clk_readlx(c->reg + offs);
2820         else
2821                 val = clk_readl(c->reg + offs);
2822
2823         if (set)
2824                 val |= iddq_bit;
2825         else
2826                 val &= ~iddq_bit;
2827
2828         if (c->flags & PLLX)
2829                 clk_writelx_delay(val, c->reg + offs);
2830         else
2831                 clk_writel_delay(val, c->reg + offs);
2832 }
2833
2834
2835 static u8 pllcx_p[PLLCX_PDIV_MAX + 1] = {
2836 /* PDIV: 0, 1, 2, 3, 4, 5,  6,  7 */
2837 /* p: */ 1, 2, 3, 4, 6, 8, 12, 16 };
2838
2839 static u32 pllcx_round_p_to_pdiv(u32 p, u32 *pdiv)
2840 {
2841         int i;
2842
2843         if (p) {
2844                 for (i = 0; i <= PLLCX_PDIV_MAX; i++) {
2845                         /* Do not use DIV3 p values - mapped to even PDIV */
2846                         if (i && ((i & 0x1) == 0))
2847                                 continue;
2848
2849                         if (p <= pllcx_p[i]) {
2850                                 if (pdiv)
2851                                         *pdiv = i;
2852                                 return pllcx_p[i];
2853                         }
2854                 }
2855         }
2856         return -EINVAL;
2857 }
2858
2859 static void pllcx_update_dynamic_koef(struct clk *c, unsigned long input_rate,
2860                                         u32 n)
2861 {
2862         u32 val, n_threshold;
2863
2864         switch (input_rate) {
2865         case 12000000:
2866                 n_threshold = 70;
2867                 break;
2868         case 13000000:
2869         case 26000000:
2870                 n_threshold = 71;
2871                 break;
2872         case 16800000:
2873                 n_threshold = 55;
2874                 break;
2875         case 19200000:
2876                 n_threshold = 48;
2877                 break;
2878         default:
2879                 pr_err("%s: Unexpected reference rate %lu\n",
2880                         __func__, input_rate);
2881                 BUG();
2882                 return;
2883         }
2884
2885         val = clk_readl(c->reg + PLL_MISC(c));
2886         val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
2887         val |= n <= n_threshold ?
2888                 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
2889         clk_writel(val, c->reg + PLL_MISC(c));
2890 }
2891
2892 static void pllcx_strobe(struct clk *c)
2893 {
2894         u32 reg = c->reg + PLL_MISC(c);
2895         u32 val = clk_readl(reg);
2896
2897         val |= PLLCX_MISC_STROBE;
2898         pll_writel_delay(val, reg);
2899
2900         val &= ~PLLCX_MISC_STROBE;
2901         clk_writel(val, reg);
2902 }
2903
2904 static void pllcx_set_defaults(struct clk *c, unsigned long input_rate, u32 n)
2905 {
2906         u32 misc1val = PLLCX_MISC1_DEFAULT_VALUE;
2907
2908         if (c->state != ON)
2909                 misc1val |= PLLCX_MISC1_IDDQ;
2910         clk_writel(PLLCX_MISC_DEFAULT_VALUE, c->reg + PLL_MISC(c));
2911         clk_writel(misc1val, c->reg + PLL_MISCN(c, 1));
2912         clk_writel(PLLCX_MISC2_DEFAULT_VALUE, c->reg + PLL_MISCN(c, 2));
2913         clk_writel(PLLCX_MISC3_DEFAULT_VALUE, c->reg + PLL_MISCN(c, 3));
2914
2915         pllcx_update_dynamic_koef(c, input_rate, n);
2916 }
2917
2918 static void tegra12_pllcx_clk_init(struct clk *c)
2919 {
2920         unsigned long input_rate = clk_get_rate(c->parent);
2921         u32 m, n, p, val;
2922
2923         /* clip vco_min to exact multiple of input rate to avoid crossover
2924            by rounding */
2925         c->u.pll.vco_min =
2926                 DIV_ROUND_UP(c->u.pll.vco_min, input_rate) * input_rate;
2927         c->min_rate = DIV_ROUND_UP(c->u.pll.vco_min, pllcx_p[PLLCX_PDIV_MAX]);
2928
2929         val = clk_readl(c->reg + PLL_BASE);
2930         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
2931
2932         /*
2933          * PLLCX is not a boot PLL, it should be left disabled by boot-loader,
2934          * and no enabled module clocks should use it as a source during clock
2935          * init.
2936          */
2937         BUG_ON(c->state == ON && !tegra_platform_is_linsim() &&
2938                         !is_tegra_hypervisor_mode());
2939         /*
2940          * Most of PLLCX register fields are shadowed, and can not be read
2941          * directly from PLL h/w. Hence, actual PLLCX boot state is unknown.
2942          * Initialize PLL to default state: disabled, reset; shadow registers
2943          * loaded with default parameters; dividers are preset for half of
2944          * minimum VCO rate (the latter assured that shadowed divider settings
2945          * are within supported range).
2946          */
2947         m = PLL_FIXED_MDIV(c, input_rate);
2948         n = m * c->u.pll.vco_min / input_rate;
2949         p = pllcx_p[1];
2950         val = (m << PLL_BASE_DIVM_SHIFT) | (n << PLL_BASE_DIVN_SHIFT) |
2951                 (1 << PLL_BASE_DIVP_SHIFT);
2952         clk_writel(val, c->reg + PLL_BASE);     /* PLL disabled */
2953
2954         pllcx_set_defaults(c, input_rate, n);
2955
2956         c->mul = n;
2957         c->div = m * p;
2958 }
2959
2960 static int tegra12_pllcx_clk_enable(struct clk *c)
2961 {
2962         u32 val;
2963         pr_debug("%s on clock %s\n", __func__, c->name);
2964
2965         pll_do_iddq(c, PLL_MISCN(c, 1), PLLCX_MISC1_IDDQ, false);
2966
2967         val = clk_readl(c->reg + PLL_BASE);
2968         val &= ~PLL_BASE_BYPASS;
2969         val |= PLL_BASE_ENABLE;
2970         pll_writel_delay(val, c->reg + PLL_BASE);
2971
2972         val = clk_readl(c->reg + PLL_MISC(c));
2973         val &= ~PLLCX_MISC_RESET;
2974         pll_writel_delay(val, c->reg + PLL_MISC(c));
2975
2976         pllcx_strobe(c);
2977         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE,
2978                         PLL_BASE_LOCK | PLLCX_BASE_PHASE_LOCK);
2979         return 0;
2980 }
2981
2982 static void tegra12_pllcx_clk_disable(struct clk *c)
2983 {
2984         u32 val;
2985         pr_debug("%s on clock %s\n", __func__, c->name);
2986
2987         val = clk_readl(c->reg);
2988         val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
2989         clk_writel(val, c->reg);
2990
2991         val = clk_readl(c->reg + PLL_MISC(c));
2992         val |= PLLCX_MISC_RESET;
2993         pll_writel_delay(val, c->reg + PLL_MISC(c));
2994
2995         pll_do_iddq(c, PLL_MISCN(c, 1), PLLCX_MISC1_IDDQ, true);
2996 }
2997
2998 static int tegra12_pllcx_clk_set_rate(struct clk *c, unsigned long rate)
2999 {
3000         u32 val, pdiv;
3001         unsigned long input_rate;
3002         struct clk_pll_freq_table cfg, old_cfg;
3003         const struct clk_pll_freq_table *sel = &cfg;
3004
3005         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
3006         if (tegra_platform_is_qt())
3007                 return 0;
3008
3009         input_rate = clk_get_rate(c->parent);
3010
3011         if (pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, &pdiv))
3012                 return -EINVAL;
3013
3014         c->mul = sel->n;
3015         c->div = sel->m * sel->p;
3016
3017         val = clk_readl(c->reg + PLL_BASE);
3018         PLL_BASE_PARSE(PLLCX, old_cfg, val);
3019         old_cfg.p = pllcx_p[old_cfg.p];
3020
3021         BUG_ON(old_cfg.m != sel->m);
3022         if ((sel->n == old_cfg.n) && (sel->p == old_cfg.p))
3023                 return 0;
3024
3025 #if PLLCX_USE_DYN_RAMP
3026         if (c->state == ON && ((sel->n == old_cfg.n) ||
3027                                PLLCX_IS_DYN(sel->p, old_cfg.p))) {
3028                 /*
3029                  * Dynamic ramp if PLL is enabled, and M divider is unchanged:
3030                  * - Change P divider 1st if intermediate rate is below either
3031                  *   old or new rate.
3032                  * - Change N divider with DFS strobe - target rate is either
3033                  *   final new rate or below old rate
3034                  * - If divider has been changed, exit without waiting for lock.
3035                  *   Otherwise, wait for lock and change divider.
3036                  */
3037                 if (sel->p > old_cfg.p) {
3038                         val &= ~PLLCX_BASE_DIVP_MASK;
3039                         val |= pdiv << PLL_BASE_DIVP_SHIFT;
3040                         clk_writel(val, c->reg + PLL_BASE);
3041                 }
3042
3043                 if (sel->n != old_cfg.n) {
3044                         pllcx_update_dynamic_koef(c, input_rate, sel->n);
3045                         val &= ~PLLCX_BASE_DIVN_MASK;
3046                         val |= sel->n << PLL_BASE_DIVN_SHIFT;
3047                         pll_writel_delay(val, c->reg + PLL_BASE);
3048
3049                         pllcx_strobe(c);
3050                         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE,
3051                                         PLL_BASE_LOCK | PLLCX_BASE_PHASE_LOCK);
3052                 }
3053
3054                 if (sel->p < old_cfg.p) {
3055                         val &= ~PLLCX_BASE_DIVP_MASK;
3056                         val |= pdiv << PLL_BASE_DIVP_SHIFT;
3057                         clk_writel(val, c->reg + PLL_BASE);
3058                 }
3059                 return 0;
3060         }
3061 #endif
3062
3063         val &= ~(PLLCX_BASE_DIVN_MASK | PLLCX_BASE_DIVP_MASK);
3064         val |= (sel->n << PLL_BASE_DIVN_SHIFT) |
3065                 (pdiv << PLL_BASE_DIVP_SHIFT);
3066
3067         if (c->state == ON) {
3068                 tegra12_pllcx_clk_disable(c);
3069                 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
3070         }
3071         pllcx_update_dynamic_koef(c, input_rate, sel->n);
3072         clk_writel(val, c->reg + PLL_BASE);
3073         if (c->state == ON)
3074                 tegra12_pllcx_clk_enable(c);
3075
3076         return 0;
3077 }
3078
3079 #ifdef CONFIG_PM_SLEEP
3080 static void tegra12_pllcx_clk_resume_enable(struct clk *c)
3081 {
3082         unsigned long rate = clk_get_rate_all_locked(c->parent);
3083         u32 val = clk_readl(c->reg + PLL_BASE);
3084         enum clk_state state = c->state;
3085
3086         if (val & PLL_BASE_ENABLE)
3087                 return;         /* already resumed */
3088
3089         /* Restore input divider */
3090         val &= ~PLLCX_BASE_DIVM_MASK;
3091         val |= PLL_FIXED_MDIV(c, rate) << PLL_BASE_DIVM_SHIFT;
3092         clk_writel(val, c->reg + PLL_BASE);
3093
3094         /* temporarily sync h/w and s/w states, final sync happens
3095            in tegra_clk_resume later */
3096         c->state = OFF;
3097         pllcx_set_defaults(c, rate, c->mul);
3098
3099         rate = clk_get_rate_all_locked(c);
3100         tegra12_pllcx_clk_set_rate(c, rate);
3101         tegra12_pllcx_clk_enable(c);
3102         c->state = state;
3103 }
3104 #endif
3105
3106 static struct clk_ops tegra_pllcx_ops = {
3107         .init                   = tegra12_pllcx_clk_init,
3108         .enable                 = tegra12_pllcx_clk_enable,
3109         .disable                = tegra12_pllcx_clk_disable,
3110         .set_rate               = tegra12_pllcx_clk_set_rate,
3111 };
3112
3113
3114 /* non-monotonic mapping below is not a typo */
3115 static u8 pllxc_p[PLLXC_PDIV_MAX + 1] = {
3116 /* PDIV: 0, 1, 2, 3, 4, 5, 6,  7,  8,  9, 10, 11, 12, 13, 14 */
3117 /* p: */ 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 12, 16, 20, 24, 32 };
3118
3119 static u32 pllxc_round_p_to_pdiv(u32 p, u32 *pdiv)
3120 {
3121         if (!p || (p > PLLXC_SW_PDIV_MAX + 1))
3122                 return -EINVAL;
3123
3124         if (pdiv)
3125                 *pdiv = p - 1;
3126         return p;
3127 }
3128
3129 static void pllxc_get_dyn_steps(struct clk *c, unsigned long input_rate,
3130                                 u32 *step_a, u32 *step_b)
3131 {
3132         switch (input_rate) {
3133         case 12000000:
3134         case 13000000:
3135         case 26000000:
3136                 *step_a = 0x2B;
3137                 *step_b = 0x0B;
3138                 return;
3139         case 16800000:
3140                 *step_a = 0x1A;
3141                 *step_b = 0x09;
3142                 return;
3143         case 19200000:
3144                 *step_a = 0x12;
3145                 *step_b = 0x08;
3146                 return;
3147         default:
3148                 pr_err("%s: Unexpected reference rate %lu\n",
3149                         __func__, input_rate);
3150                 BUG();
3151         }
3152 }
3153
3154 static void pllx_set_defaults(struct clk *c, unsigned long input_rate)
3155 {
3156         u32 val;
3157         u32 step_a, step_b;
3158
3159         /* Only s/w dyn ramp control is supported */
3160         val = clk_readlx(PLLX_HW_CTRL_CFG);
3161         BUG_ON(!(val & PLLX_HW_CTRL_CFG_SWCTRL) && !tegra_platform_is_linsim());
3162
3163         pllxc_get_dyn_steps(c, input_rate, &step_a, &step_b);
3164         val = step_a << PLLX_MISC2_DYNRAMP_STEPA_SHIFT;
3165         val |= step_b << PLLX_MISC2_DYNRAMP_STEPB_SHIFT;
3166
3167         /* Get ready dyn ramp state machine, disable lock override */
3168         clk_writelx(val, c->reg + PLL_MISCN(c, 2));
3169
3170         /* Enable outputs to CPUs and configure lock */
3171         val = 0;
3172 #if USE_PLL_LOCK_BITS
3173         val |= PLL_MISC_LOCK_ENABLE(c);
3174 #endif
3175         clk_writelx(val, c->reg + PLL_MISC(c));
3176
3177         /* Check/set IDDQ */
3178         val = clk_readlx(c->reg + PLL_MISCN(c, 3));
3179         if (c->state == ON) {
3180                 BUG_ON(val & PLLX_MISC3_IDDQ && !tegra_platform_is_linsim());
3181         } else {
3182                 val |= PLLX_MISC3_IDDQ;
3183                 clk_writelx(val, c->reg + PLL_MISCN(c, 3));
3184         }
3185 }
3186
3187 static void pllc_set_defaults(struct clk *c, unsigned long input_rate)
3188 {
3189         u32 val;
3190         u32 step_a, step_b;
3191
3192         /* Get ready dyn ramp state machine */
3193         pllxc_get_dyn_steps(c, input_rate, &step_a, &step_b);
3194         val = step_a << PLLC_MISC1_DYNRAMP_STEPA_SHIFT;
3195         val |= step_b << PLLC_MISC1_DYNRAMP_STEPB_SHIFT;
3196         clk_writel(val, c->reg + PLL_MISCN(c, 1));
3197
3198         /* Configure lock and check/set IDDQ */
3199         val = clk_readl(c->reg + PLL_BASE);
3200         val &= ~PLLC_BASE_LOCK_OVERRIDE;
3201         clk_writel(val, c->reg + PLL_BASE);
3202
3203         val = clk_readl(c->reg + PLL_MISC(c));
3204 #if USE_PLL_LOCK_BITS
3205         val |= PLLC_MISC_LOCK_ENABLE;
3206 #else
3207         val &= ~PLLC_MISC_LOCK_ENABLE;
3208 #endif
3209         clk_writel(val, c->reg + PLL_MISC(c));
3210
3211         if (c->state == ON) {
3212                 BUG_ON(val & PLLC_MISC_IDDQ && !tegra_platform_is_linsim());
3213         } else {
3214                 val |= PLLC_MISC_IDDQ;
3215                 clk_writel(val, c->reg + PLL_MISC(c));
3216         }
3217 }
3218
3219 static void tegra12_pllxc_clk_init(struct clk *c)
3220 {
3221         unsigned long input_rate = clk_get_rate(c->parent);
3222         u32 m, p, val;
3223
3224         /* clip vco_min to exact multiple of input rate to avoid crossover
3225            by rounding */
3226         c->u.pll.vco_min =
3227                 DIV_ROUND_UP(c->u.pll.vco_min, input_rate) * input_rate;
3228         c->min_rate =
3229                 DIV_ROUND_UP(c->u.pll.vco_min, pllxc_p[PLLXC_SW_PDIV_MAX]);
3230
3231         if (c->flags & PLLX)
3232                 val = clk_readlx(c->reg + PLL_BASE);
3233         else
3234                 val = clk_readl(c->reg + PLL_BASE);
3235
3236         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
3237
3238         m = (val & PLLXC_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
3239         p = (val & PLLXC_BASE_DIVP_MASK) >> PLL_BASE_DIVP_SHIFT;
3240         BUG_ON(p > PLLXC_PDIV_MAX);
3241         p = pllxc_p[p];
3242
3243         c->div = m * p;
3244         c->mul = (val & PLLXC_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
3245
3246         if (c->flags & PLLX)
3247                 pllx_set_defaults(c, input_rate);
3248         else
3249                 pllc_set_defaults(c, input_rate);
3250 }
3251
3252 static int tegra12_pllxc_clk_enable(struct clk *c)
3253 {
3254         u32 val;
3255         pr_debug("%s on clock %s\n", __func__, c->name);
3256
3257         if (c->flags & PLLX) {
3258                 pll_do_iddq(c, PLL_MISCN(c, 3), PLLX_MISC3_IDDQ, false);
3259                 val = clk_readlx(c->reg + PLL_BASE);
3260                 val |= PLL_BASE_ENABLE;
3261                 clk_writelx(val, c->reg + PLL_BASE);
3262         } else {
3263                 pll_do_iddq(c, PLL_MISC(c), PLLC_MISC_IDDQ, false);
3264                 val = clk_readl(c->reg + PLL_BASE);
3265                 val |= PLL_BASE_ENABLE;
3266                 clk_writel(val, c->reg + PLL_BASE);
3267         }
3268
3269         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
3270
3271         return 0;
3272 }
3273
3274 static void tegra12_pllxc_clk_disable(struct clk *c)
3275 {
3276         u32 val;
3277         pr_debug("%s on clock %s\n", __func__, c->name);
3278
3279         if (c->flags & PLLX) {
3280                 val = clk_readlx(c->reg + PLL_BASE);
3281                 val &= ~PLL_BASE_ENABLE;
3282                 clk_writelx(val, c->reg + PLL_BASE);
3283         } else {
3284                 val = clk_readl(c->reg + PLL_BASE);
3285                 val &= ~PLL_BASE_ENABLE;
3286                 clk_writel(val, c->reg + PLL_BASE);
3287         }
3288
3289         if (c->flags & PLLX)
3290                 pll_do_iddq(c, PLL_MISCN(c, 3), PLLX_MISC3_IDDQ, true);
3291         else
3292                 pll_do_iddq(c, PLL_MISC(c), PLLC_MISC_IDDQ, true);
3293
3294 }
3295
3296 #define PLLXC_DYN_RAMP(pll_misc, reg)                                   \
3297         do {                                                            \
3298                 u32 misc = clk_readl((reg));                            \
3299                                                                         \
3300                 misc &= ~pll_misc##_NDIV_NEW_MASK;                      \
3301                 misc |= sel->n << pll_misc##_NDIV_NEW_SHIFT;            \
3302                 pll_writel_delay(misc, (reg));                          \
3303                                                                         \
3304                 misc |= pll_misc##_EN_DYNRAMP;                          \
3305                 clk_writel(misc, (reg));                                \
3306                 tegra12_pll_clk_wait_for_lock(c, (reg),                 \
3307                                         pll_misc##_DYNRAMP_DONE);       \
3308                                                                         \
3309                 val &= ~PLLXC_BASE_DIVN_MASK;                           \
3310                 val |= sel->n << PLL_BASE_DIVN_SHIFT;                   \
3311                 pll_writel_delay(val, c->reg + PLL_BASE);               \
3312                                                                         \
3313                 misc &= ~pll_misc##_EN_DYNRAMP;                         \
3314                 pll_writel_delay(misc, (reg));                          \
3315         } while (0)
3316
3317 #define PLLX_DYN_RAMP(pll_misc, reg)                                    \
3318         do {                                                            \
3319                 u32 misc = clk_readlx((reg));                           \
3320                                                                         \
3321                 misc &= ~pll_misc##_NDIV_NEW_MASK;                      \
3322                 misc |= sel->n << pll_misc##_NDIV_NEW_SHIFT;            \
3323                 pll_writelx_delay(misc, (reg));                         \
3324                                                                         \
3325                 misc |= pll_misc##_EN_DYNRAMP;                          \
3326                 clk_writelx(misc, (reg));                               \
3327                 tegra12_pll_clk_wait_for_lock(c, (reg),                 \
3328                                         pll_misc##_DYNRAMP_DONE);       \
3329                                                                         \
3330                 val &= ~PLLXC_BASE_DIVN_MASK;                           \
3331                 val |= sel->n << PLL_BASE_DIVN_SHIFT;                   \
3332                 pll_writelx_delay(val, c->reg + PLL_BASE);              \
3333                                                                         \
3334                 misc &= ~pll_misc##_EN_DYNRAMP;                         \
3335                 pll_writelx_delay(misc, (reg));                         \
3336         } while (0)
3337
3338 static int tegra12_pllxc_clk_set_rate(struct clk *c, unsigned long rate)
3339 {
3340         u32 val, pdiv;
3341         unsigned long input_rate;
3342         struct clk_pll_freq_table cfg, old_cfg;
3343         const struct clk_pll_freq_table *sel = &cfg;
3344
3345         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
3346         if (tegra_platform_is_qt())
3347                 return 0;
3348
3349         input_rate = clk_get_rate(c->parent);
3350
3351         if (pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, &pdiv))
3352                 return -EINVAL;
3353
3354         c->mul = sel->n;
3355         c->div = sel->m * sel->p;
3356
3357         if (c->flags & PLLX)
3358                 val = clk_readlx(c->reg + PLL_BASE);
3359         else
3360                 val = clk_readl(c->reg + PLL_BASE);
3361
3362         PLL_BASE_PARSE(PLLXC, old_cfg, val);
3363         old_cfg.p = pllxc_p[old_cfg.p];
3364
3365         if ((sel->m == old_cfg.m) && (sel->n == old_cfg.n) &&
3366             (sel->p == old_cfg.p))
3367                 return 0;
3368
3369 #if PLLXC_USE_DYN_RAMP
3370         /*
3371          * Dynamic ramp can be used if M, P dividers are unchanged
3372          * (coveres superset of conventional dynamic ramps)
3373          */
3374         if ((c->state == ON) && (sel->m == old_cfg.m) &&
3375             (sel->p == old_cfg.p)) {
3376
3377                 if (c->flags & PLLX) {
3378                         u32 reg = c->reg + PLL_MISCN(c, 2);
3379                         PLLX_DYN_RAMP(PLLX_MISC2, reg);
3380                 } else {
3381                         u32 reg = c->reg + PLL_MISCN(c, 1);
3382                         PLLXC_DYN_RAMP(PLLC_MISC1, reg);
3383                 }
3384
3385                 return 0;
3386         }
3387 #endif
3388         if (c->state == ON) {
3389                 /* Use "ENABLE" pulse without placing PLL into IDDQ */
3390                 val &= ~PLL_BASE_ENABLE;
3391                 if (c->flags & PLLX)
3392                         pll_writelx_delay(val, c->reg + PLL_BASE);
3393                 else
3394                         pll_writel_delay(val, c->reg + PLL_BASE);
3395         }
3396
3397         val &= ~(PLLXC_BASE_DIVM_MASK |
3398                  PLLXC_BASE_DIVN_MASK | PLLXC_BASE_DIVP_MASK);
3399         val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
3400                 (sel->n << PLL_BASE_DIVN_SHIFT) | (pdiv << PLL_BASE_DIVP_SHIFT);
3401         if (c->flags & PLLX)
3402                 clk_writelx(val, c->reg + PLL_BASE);
3403         else
3404                 clk_writel(val, c->reg + PLL_BASE);
3405
3406         if (c->state == ON) {
3407                 val |= PLL_BASE_ENABLE;
3408                 if (c->flags & PLLX)
3409                         clk_writelx(val, c->reg + PLL_BASE);
3410                 else
3411                         clk_writel(val, c->reg + PLL_BASE);
3412                 tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE,
3413                                               PLL_BASE_LOCK);
3414         }
3415         return 0;
3416 }
3417
3418 #ifdef CONFIG_PM_SLEEP
3419 static void tegra12_pllxc_clk_resume_enable(struct clk *c)
3420 {
3421         unsigned long rate = clk_get_rate_all_locked(c->parent);
3422         enum clk_state state = c->state;
3423
3424         if (c->flags & PLLX) {
3425                 if (clk_readlx(c->reg + PLL_BASE) & PLL_BASE_ENABLE)
3426                         return;         /* already resumed */
3427         } else {
3428                 if (clk_readl(c->reg + PLL_BASE) & PLL_BASE_ENABLE)
3429                         return;         /* already resumed */
3430         }
3431
3432         /* temporarily sync h/w and s/w states, final sync happens
3433            in tegra_clk_resume later */
3434         c->state = OFF;
3435         if (c->flags & PLLX)
3436                 pllx_set_defaults(c, rate);
3437         else
3438                 pllc_set_defaults(c, rate);
3439
3440         rate = clk_get_rate_all_locked(c);
3441         tegra12_pllxc_clk_set_rate(c, rate);
3442         tegra12_pllxc_clk_enable(c);
3443         c->state = state;
3444 }
3445 #endif
3446
3447 static struct clk_ops tegra_pllxc_ops = {
3448         .init                   = tegra12_pllxc_clk_init,
3449         .enable                 = tegra12_pllxc_clk_enable,
3450         .disable                = tegra12_pllxc_clk_disable,
3451         .set_rate               = tegra12_pllxc_clk_set_rate,
3452 };
3453
3454
3455 /* FIXME: pllm suspend/resume */
3456
3457 /* non-monotonic mapping below is not a typo */
3458 static u8 pllm_p[PLLM_PDIV_MAX + 1] = {
3459 /* PDIV: 0, 1, 2, 3, 4, 5, 6,  7,  8,  9, 10, 11, 12, 13, 14 */
3460 /* p: */ 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 12, 16, 20, 24, 32 };
3461
3462 static u32 pllm_round_p_to_pdiv(u32 p, u32 *pdiv)
3463 {
3464         if (!p || (p > PLLM_SW_PDIV_MAX + 1))
3465                 return -EINVAL;
3466
3467         if (pdiv)
3468                 *pdiv = p - 1;
3469         return p;
3470 }
3471
3472 static void pllm_set_defaults(struct clk *c, unsigned long input_rate)
3473 {
3474         u32 val = clk_readl(c->reg + PLL_MISC(c));
3475
3476         val &= ~PLLM_MISC_LOCK_OVERRIDE;
3477 #if USE_PLL_LOCK_BITS
3478         val &= ~PLLM_MISC_LOCK_DISABLE;
3479 #else
3480         val |= PLLM_MISC_LOCK_DISABLE;
3481 #endif
3482
3483         if (c->state != ON)
3484                 val |= PLLM_MISC_IDDQ;
3485         else
3486                 BUG_ON(val & PLLM_MISC_IDDQ && !tegra_platform_is_linsim());
3487
3488         clk_writel(val, c->reg + PLL_MISC(c));
3489 }
3490
3491 static void tegra12_pllm_clk_init(struct clk *c)
3492 {
3493         unsigned long input_rate = clk_get_rate(c->parent);
3494         u32 m, p, val;
3495
3496         /* clip vco_min to exact multiple of input rate to avoid crossover
3497            by rounding */
3498         c->u.pll.vco_min =
3499                 DIV_ROUND_UP(c->u.pll.vco_min, input_rate) * input_rate;
3500         c->min_rate =
3501                 DIV_ROUND_UP(c->u.pll.vco_min, pllm_p[PLLM_SW_PDIV_MAX]);
3502
3503         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3504         if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) {
3505                 c->state = (val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE) ? ON : OFF;
3506
3507                 /* Tegra12 has bad default value of PMC_PLLM_WB0_OVERRIDE.
3508                  * If bootloader does not initialize PLLM, kernel has to
3509                  * initialize the register with sane value. */
3510                 if (c->state == OFF) {
3511                         val = pmc_readl(PMC_PLLM_WB0_OVERRIDE);
3512                         m = (val & PLLM_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
3513                         if (m != PLL_FIXED_MDIV(c, input_rate)) {
3514                                 /* Copy DIVM and DIVN from PLLM_BASE */
3515                                 pr_info("%s: Fixing DIVM and DIVN\n", __func__);
3516                                 val = clk_readl(c->reg + PLL_BASE);
3517                                 val &= (PLLM_BASE_DIVM_MASK
3518                                         | PLLM_BASE_DIVN_MASK);
3519                                 pmc_writel(val, PMC_PLLM_WB0_OVERRIDE);
3520                         }
3521                 }
3522
3523                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE_2);
3524                 p = (val & PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK) >>
3525                         PMC_PLLM_WB0_OVERRIDE_2_DIVP_SHIFT;
3526
3527                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE);
3528         } else {
3529                 val = clk_readl(c->reg + PLL_BASE);
3530                 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
3531                 p = (val & PLLM_BASE_DIVP_MASK) >> PLL_BASE_DIVP_SHIFT;
3532         }
3533
3534         m = (val & PLLM_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
3535         BUG_ON(m != PLL_FIXED_MDIV(c, input_rate)
3536                          && tegra_platform_is_silicon());
3537         c->div = m * pllm_p[p];
3538         c->mul = (val & PLLM_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
3539
3540         pllm_set_defaults(c, input_rate);
3541 }
3542
3543 static int tegra12_pllm_clk_enable(struct clk *c)
3544 {
3545         u32 val;
3546         pr_debug("%s on clock %s\n", __func__, c->name);
3547
3548         pll_do_iddq(c, PLL_MISC(c), PLLM_MISC_IDDQ, false);
3549
3550         /* Just enable both base and override - one would work */
3551         val = clk_readl(c->reg + PLL_BASE);
3552         val |= PLL_BASE_ENABLE;
3553         clk_writel(val, c->reg + PLL_BASE);
3554
3555         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3556         val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
3557         pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
3558         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3559
3560         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLL_BASE_LOCK);
3561         return 0;
3562 }
3563
3564 static void tegra12_pllm_clk_disable(struct clk *c)
3565 {
3566         u32 val;
3567         pr_debug("%s on clock %s\n", __func__, c->name);
3568
3569         /* Just disable both base and override - one would work */
3570         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3571         val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
3572         pmc_writel(val, PMC_PLLP_WB0_OVERRIDE);
3573         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3574
3575         val = clk_readl(c->reg + PLL_BASE);
3576         val &= ~PLL_BASE_ENABLE;
3577         clk_writel(val, c->reg + PLL_BASE);
3578
3579         pll_do_iddq(c, PLL_MISC(c), PLLM_MISC_IDDQ, true);
3580 }
3581
3582 static int tegra12_pllm_clk_set_rate(struct clk *c, unsigned long rate)
3583 {
3584         u32 val, pdiv;
3585         unsigned long input_rate;
3586         struct clk_pll_freq_table cfg;
3587         const struct clk_pll_freq_table *sel = &cfg;
3588
3589         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
3590
3591         if (c->state == ON) {
3592                 if (rate != clk_get_rate_locked(c)) {
3593                         pr_err("%s: Can not change memory %s rate in flight\n",
3594                                __func__, c->name);
3595                         return -EINVAL;
3596                 }
3597                 return 0;
3598         }
3599
3600         input_rate = clk_get_rate(c->parent);
3601
3602         if (pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, &pdiv))
3603                 return -EINVAL;
3604
3605         c->mul = sel->n;
3606         c->div = sel->m * sel->p;
3607
3608         val = pmc_readl(PMC_PLLP_WB0_OVERRIDE);
3609         if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) {
3610                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE_2);
3611                 val &= ~PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK;
3612                 val |= pdiv << PMC_PLLM_WB0_OVERRIDE_2_DIVP_SHIFT;
3613                 pmc_writel(val, PMC_PLLM_WB0_OVERRIDE_2);
3614
3615                 val = pmc_readl(PMC_PLLM_WB0_OVERRIDE);
3616                 val &= ~(PLLM_BASE_DIVM_MASK | PLLM_BASE_DIVN_MASK);
3617                 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
3618                         (sel->n << PLL_BASE_DIVN_SHIFT);
3619                 pmc_writel(val, PMC_PLLM_WB0_OVERRIDE);
3620         } else {
3621                 val = clk_readl(c->reg + PLL_BASE);
3622                 val &= ~(PLLM_BASE_DIVM_MASK | PLLM_BASE_DIVN_MASK |
3623                          PLLM_BASE_DIVP_MASK);
3624                 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
3625                         (sel->n << PLL_BASE_DIVN_SHIFT) |
3626                         (pdiv << PLL_BASE_DIVP_SHIFT);
3627                 clk_writel(val, c->reg + PLL_BASE);
3628         }
3629
3630         return 0;
3631 }
3632
3633 static struct clk_ops tegra_pllm_ops = {
3634         .init                   = tegra12_pllm_clk_init,
3635         .enable                 = tegra12_pllm_clk_enable,
3636         .disable                = tegra12_pllm_clk_disable,
3637         .set_rate               = tegra12_pllm_clk_set_rate,
3638 };
3639
3640
3641 static u8 pllss_p[PLLSS_PDIV_MAX + 1] = {
3642 /* PDIV: 0, 1, 2, 3, 4, 5, 6,  7,  8,  9, 10, 11, 12, 13, 14 */
3643 /* p: */ 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 12, 16, 20, 24, 32 };
3644
3645 static u32 pllss_round_p_to_pdiv(u32 p, u32 *pdiv)
3646 {
3647         if (!p || (p > PLLSS_SW_PDIV_MAX + 1))
3648                 return -EINVAL;
3649
3650         if (pdiv)
3651                 *pdiv = p - 1;
3652         return p;
3653 }
3654
3655 static void pllss_set_defaults(struct clk *c, unsigned long input_rate)
3656 {
3657         u32 val;
3658
3659         clk_writel(PLLSS_MISC_DEFAULT_VALUE, c->reg + PLL_MISC(c));
3660
3661         if (strcmp(c->name, "pll_dp") == 0) {
3662                 clk_writel(PLLDP_SS_CFG_0_DEFAULT_VALUE, c->reg + PLLSS_CFG(c));
3663                 clk_writel(PLLDP_SS_CTRL1_0_DEFAULT_VALUE, c->reg + PLLSS_CTRL1(c));
3664                 clk_writel(PLLDP_SS_CTRL2_0_DEFAULT_VALUE, c->reg + PLLSS_CTRL2(c));
3665         } else {
3666                 clk_writel(PLLSS_CFG_DEFAULT_VALUE, c->reg + PLLSS_CFG(c));
3667                 clk_writel(PLLSS_CTRL1_DEFAULT_VALUE, c->reg + PLLSS_CTRL1(c));
3668                 clk_writel(PLLSS_CTRL2_DEFAULT_VALUE, c->reg + PLLSS_CTRL2(c));
3669         }
3670         val = clk_readl(c->reg + PLL_MISC(c));
3671         val &= ~(PLLSS_MISC_KCP_MASK);
3672         val |= (c->u.pll.cpcon_default << PLLSS_MISC_KCP_SHIFT);
3673 #if USE_PLL_LOCK_BITS
3674         val |= PLLSS_MISC_LOCK_ENABLE;
3675 #else
3676         val &= ~PLLSS_MISC_LOCK_ENABLE;
3677 #endif
3678         clk_writel(val, c->reg + PLL_MISC(c));
3679
3680         val = clk_readl(c->reg + PLL_BASE);
3681         if (c->state != ON)
3682                 val |= PLLSS_BASE_IDDQ;
3683         else
3684                 BUG_ON(val & PLLSS_BASE_IDDQ && !tegra_platform_is_linsim());
3685         val &= ~PLLSS_BASE_LOCK_OVERRIDE;
3686         clk_writel(val, c->reg + PLL_BASE);
3687 }
3688
3689 static void tegra12_pllss_clk_init(struct clk *c)
3690 {
3691         unsigned long input_rate;
3692         u32 m, n, p_div, val;
3693
3694         val = clk_readl(c->reg + PLL_BASE);
3695         BUG_ON(((val & PLLSS_BASE_SOURCE_MASK)
3696                         >> PLLSS_BASE_SOURCE_SHIFT) != 0);
3697
3698         input_rate = clk_get_rate(c->parent);
3699
3700         /* clip vco_min to exact multiple of input rate to avoid crossover
3701            by rounding */
3702         c->u.pll.vco_min =
3703                 DIV_ROUND_UP(c->u.pll.vco_min, input_rate) * input_rate;
3704         c->min_rate =
3705                 DIV_ROUND_UP(c->u.pll.vco_min, pllss_p[PLLSS_SW_PDIV_MAX]);
3706
3707         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
3708
3709         if (c->state == OFF){
3710                 /* Reset default value of those PLLs are not safe.
3711                    For example, they cause problem in LP0 resume.
3712                    Replace them here with the safe value. */
3713                 m = PLL_FIXED_MDIV(c, input_rate);
3714                 n = c->u.pll.vco_min / input_rate * m;
3715                 p_div = PLLSS_SW_PDIV_MAX;
3716                 val &= ~PLLSS_BASE_DIVM_MASK;
3717                 val &= ~PLLSS_BASE_DIVN_MASK;
3718                 val &= ~PLLSS_BASE_DIVP_MASK;
3719                 val |= m << PLL_BASE_DIVM_SHIFT;
3720                 val |= n << PLL_BASE_DIVN_SHIFT;
3721                 val |= p_div << PLL_BASE_DIVP_SHIFT;
3722                 clk_writel(val, c->reg + PLL_BASE);
3723
3724                 pllss_set_defaults(c, input_rate);
3725         } else
3726                 pr_info("%s was initialized by BootLoader\n", c->name);
3727
3728         m = (val & PLLSS_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
3729         n = (val & PLLSS_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
3730         p_div = (val & PLLSS_BASE_DIVP_MASK) >> PLL_BASE_DIVP_SHIFT;
3731
3732         c->div = m * pllss_p[p_div];
3733         c->mul = n;
3734
3735         pr_debug("%s: val=%08x m=%d n=%d p_div=%d input_rate=%lu\n",
3736                  c->name, val, m, n, p_div, input_rate);
3737
3738 }
3739
3740 static int tegra12_pllss_clk_enable(struct clk *c)
3741 {
3742         u32 val;
3743         pr_debug("%s on clock %s\n", __func__, c->name);
3744
3745         pll_do_iddq(c, PLL_BASE, PLLSS_BASE_IDDQ, false);
3746
3747         val = clk_readl(c->reg + PLL_BASE);
3748         val |= PLL_BASE_ENABLE;
3749         clk_writel(val, c->reg + PLL_BASE);
3750
3751         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_BASE, PLLSS_BASE_LOCK);
3752         return 0;
3753 }
3754
3755 static void tegra12_pllss_clk_disable(struct clk *c)
3756 {
3757         u32 val;
3758         pr_debug("%s on clock %s\n", __func__, c->name);
3759
3760         val = clk_readl(c->reg + PLL_BASE);
3761         val &= ~PLL_BASE_ENABLE;
3762         clk_writel(val, c->reg + PLL_BASE);
3763
3764         pll_do_iddq(c, PLL_BASE, PLLSS_BASE_IDDQ, true);
3765 }
3766
3767 static int tegra12_pllss_clk_set_rate(struct clk *c, unsigned long rate)
3768 {
3769         u32 val, pdiv, old_base;
3770         unsigned long input_rate;
3771         struct clk_pll_freq_table cfg, old_cfg;
3772         const struct clk_pll_freq_table *sel = &cfg;
3773
3774         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
3775         if (tegra_platform_is_qt())
3776                 return 0;
3777
3778         input_rate = clk_get_rate(c->parent);
3779
3780         if (pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, &pdiv))
3781                 return -EINVAL;
3782
3783         c->mul = sel->n;
3784         c->div = sel->m * sel->p;
3785
3786         val = clk_readl(c->reg + PLL_BASE);
3787         PLL_BASE_PARSE(PLLSS, old_cfg, val);
3788         old_cfg.p = pllss_p[old_cfg.p];
3789
3790         if ((sel->m == old_cfg.m) && (sel->n == old_cfg.n) &&
3791             (sel->p == old_cfg.p))
3792                 return 0;
3793
3794         val = old_base = clk_readl(c->reg + PLL_BASE);
3795         val &= ~(PLLSS_BASE_DIVM_MASK
3796                 | PLLSS_BASE_DIVN_MASK | PLLSS_BASE_DIVP_MASK);
3797         val |= (sel->m << PLL_BASE_DIVM_SHIFT)
3798                 | (sel->n << PLL_BASE_DIVN_SHIFT)
3799                 | (pdiv << PLL_BASE_DIVP_SHIFT);
3800
3801         if (val == old_base)
3802                 return 0;
3803
3804         if (c->state == ON) {
3805                 /* Use "ENABLE" pulse without placing PLL into IDDQ */
3806                 val &= ~PLL_BASE_ENABLE;
3807                 old_base &= ~PLL_BASE_ENABLE;
3808                 pll_writel_delay(old_base, c->reg + PLL_BASE);
3809         }
3810
3811         clk_writel(val, c->reg + PLL_BASE);
3812
3813         if (c->state == ON) {
3814                 val |= PLL_BASE_ENABLE;
3815                 clk_writel(val, c->reg + PLL_BASE);
3816                 tegra12_pll_clk_wait_for_lock(
3817                         c, c->reg + PLL_BASE, PLLSS_BASE_LOCK);
3818         }
3819         return 0;
3820 }
3821
3822 #ifdef CONFIG_PM_SLEEP
3823 static void tegra12_pllss_clk_resume_enable(struct clk *c)
3824 {
3825         unsigned long rate = clk_get_rate_all_locked(c->parent);
3826         u32 val = clk_readl(c->reg + PLL_BASE);
3827         enum clk_state state = c->state;
3828
3829         if (val & PLL_BASE_ENABLE)
3830                 return;         /* already resumed */
3831
3832         /* Restore input divider */
3833         val &= ~PLLSS_BASE_DIVM_MASK;
3834         val |= PLL_FIXED_MDIV(c, rate) << PLL_BASE_DIVM_SHIFT;
3835         clk_writel(val, c->reg + PLL_BASE);
3836
3837         /* temporarily sync h/w and s/w states, final sync happens
3838            in tegra_clk_resume later */
3839         c->state = OFF;
3840         pllss_set_defaults(c, rate);
3841
3842         rate = clk_get_rate_all_locked(c);
3843         tegra12_pllss_clk_set_rate(c, rate);
3844         tegra12_pllss_clk_enable(c);
3845         c->state = state;
3846 }
3847 #endif
3848
3849 static struct clk_ops tegra_pllss_ops = {
3850         .init                   = tegra12_pllss_clk_init,
3851         .enable                 = tegra12_pllss_clk_enable,
3852         .disable                = tegra12_pllss_clk_disable,
3853         .set_rate               = tegra12_pllss_clk_set_rate,
3854         /* s/w policy, no set_parent, always use tegra_pll_ref */
3855 };
3856
3857 /* non-monotonic mapping below is not a typo */
3858 static u8 pllre_p[PLLRE_PDIV_MAX + 1] = {
3859 /* PDIV: 0, 1, 2, 3, 4, 5, 6,  7,  8,  9, 10, 11, 12, 13, 14 */
3860 /* p: */ 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 12, 16, 20, 24, 32 };
3861
3862 static u32 pllre_round_p_to_pdiv(u32 p, u32 *pdiv)
3863 {
3864         if (!p || (p > PLLRE_SW_PDIV_MAX + 1))
3865                 return -EINVAL;
3866
3867         if (pdiv)
3868                 *pdiv = p - 1;
3869         return p;
3870 }
3871
3872 static void pllre_set_defaults(struct clk *c, unsigned long input_rate)
3873 {
3874         u32 val = clk_readl(c->reg + PLL_MISC(c));
3875
3876         val &= ~PLLRE_MISC_LOCK_OVERRIDE;
3877 #if USE_PLL_LOCK_BITS
3878         val |= PLLRE_MISC_LOCK_ENABLE;
3879 #else
3880         val &= ~PLLRE_MISC_LOCK_ENABLE;
3881 #endif
3882
3883         if (c->state != ON)
3884                 val |= PLLRE_MISC_IDDQ;
3885         else
3886                 BUG_ON(val & PLLRE_MISC_IDDQ && !tegra_platform_is_linsim());
3887
3888         clk_writel(val, c->reg + PLL_MISC(c));
3889 }
3890
3891 static void tegra12_pllre_clk_init(struct clk *c)
3892 {
3893         unsigned long input_rate = clk_get_rate(c->parent);
3894         u32 m, val;
3895
3896         /* clip vco_min to exact multiple of input rate to avoid crossover
3897            by rounding */
3898         c->u.pll.vco_min =
3899                 DIV_ROUND_UP(c->u.pll.vco_min, input_rate) * input_rate;
3900         c->min_rate = c->u.pll.vco_min;
3901
3902         val = clk_readl(c->reg + PLL_BASE);
3903         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
3904
3905         if (!val) {
3906                 /* overwrite h/w por state with min setting */
3907                 m = PLL_FIXED_MDIV(c, input_rate);
3908                 val = (m << PLL_BASE_DIVM_SHIFT) |
3909                         (c->min_rate / input_rate << PLL_BASE_DIVN_SHIFT);
3910                 clk_writel(val, c->reg + PLL_BASE);
3911         }
3912
3913         m = (val & PLLRE_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
3914         BUG_ON(m != PLL_FIXED_MDIV(c, input_rate));
3915
3916         c->div = m;
3917         c->mul = (val & PLLRE_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
3918
3919         pllre_set_defaults(c, input_rate);
3920 }
3921
3922 static int tegra12_pllre_clk_enable(struct clk *c)
3923 {
3924         u32 val;
3925         pr_debug("%s on clock %s\n", __func__, c->name);
3926
3927         pll_do_iddq(c, PLL_MISC(c), PLLRE_MISC_IDDQ, false);
3928
3929         val = clk_readl(c->reg + PLL_BASE);
3930         val |= PLL_BASE_ENABLE;
3931         clk_writel(val, c->reg + PLL_BASE);
3932
3933         tegra12_pll_clk_wait_for_lock(c, c->reg + PLL_MISC(c), PLLRE_MISC_LOCK);
3934         return 0;
3935 }
3936
3937 static void tegra12_pllre_clk_disable(struct clk *c)
3938 {
3939         u32 val;
3940         pr_debug("%s on clock %s\n", __func__, c->name);
3941
3942         val = clk_readl(c->reg + PLL_BASE);
3943         val &= ~PLL_BASE_ENABLE;
3944         clk_writel(val, c->reg + PLL_BASE);
3945
3946         pll_do_iddq(c, PLL_MISC(c), PLLRE_MISC_IDDQ, true);
3947 }
3948
3949 static int tegra12_pllre_clk_set_rate(struct clk *c, unsigned long rate)
3950 {
3951         u32 val, old_base;
3952         unsigned long input_rate;
3953         struct clk_pll_freq_table cfg;
3954
3955         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
3956
3957         if (rate < c->min_rate) {
3958                 pr_err("%s: Failed to set %s rate %lu\n",
3959                        __func__, c->name, rate);
3960                 return -EINVAL;
3961         }
3962
3963         input_rate = clk_get_rate(c->parent);
3964         cfg.m = PLL_FIXED_MDIV(c, input_rate);
3965         cfg.n = rate * cfg.m / input_rate;
3966
3967         c->mul = cfg.n;
3968         c->div = cfg.m;
3969
3970         val = old_base = clk_readl(c->reg + PLL_BASE);
3971         val &= ~(PLLRE_BASE_DIVM_MASK | PLLRE_BASE_DIVN_MASK);
3972         val |= (cfg.m << PLL_BASE_DIVM_SHIFT) | (cfg.n << PLL_BASE_DIVN_SHIFT);
3973         if (val == old_base)
3974                 return 0;
3975
3976         if (c->state == ON) {
3977                 /* Use "ENABLE" pulse without placing PLL into IDDQ */
3978                 val &= ~PLL_BASE_ENABLE;
3979                 old_base &= ~PLL_BASE_ENABLE;
3980                 pll_writel_delay(old_base, c->reg + PLL_BASE);
3981         }
3982
3983         clk_writel(val, c->reg + PLL_BASE);
3984
3985         if (c->state == ON) {
3986                 val |= PLL_BASE_ENABLE;
3987                 clk_writel(val, c->reg + PLL_BASE);
3988                 tegra12_pll_clk_wait_for_lock(
3989                         c, c->reg + PLL_MISC(c), PLLRE_MISC_LOCK);
3990         }
3991         return 0;
3992 }
3993
3994 static struct clk_ops tegra_pllre_ops = {
3995         .init                   = tegra12_pllre_clk_init,
3996         .enable                 = tegra12_pllre_clk_enable,
3997         .disable                = tegra12_pllre_clk_disable,
3998         .set_rate               = tegra12_pllre_clk_set_rate,
3999 };
4000
4001 static void tegra12_pllre_out_clk_init(struct clk *c)
4002 {
4003         u32 p, val;
4004
4005         val = clk_readl(c->reg);
4006         p = (val & PLLRE_BASE_DIVP_MASK) >> PLLRE_BASE_DIVP_SHIFT;
4007         BUG_ON(p > PLLRE_PDIV_MAX);
4008         p = pllre_p[p];
4009
4010         c->div = p;
4011         c->mul = 1;
4012         c->state = c->parent->state;
4013 }
4014
4015 static int tegra12_pllre_out_clk_enable(struct clk *c)
4016 {
4017         return 0;
4018 }
4019
4020 static void tegra12_pllre_out_clk_disable(struct clk *c)
4021 {
4022 }
4023
4024 static int tegra12_pllre_out_clk_set_rate(struct clk *c, unsigned long rate)
4025 {
4026         u32 val, p, pdiv;
4027         unsigned long input_rate, flags;
4028
4029         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4030
4031         clk_lock_save(c->parent, &flags);
4032         input_rate = clk_get_rate_locked(c->parent);
4033
4034         p = DIV_ROUND_UP(input_rate, rate);
4035         p = c->parent->u.pll.round_p_to_pdiv(p, &pdiv);
4036         if (IS_ERR_VALUE(p)) {
4037                 pr_err("%s: Failed to set %s rate %lu\n",
4038                        __func__, c->name, rate);
4039                 clk_unlock_restore(c->parent, &flags);
4040                 return -EINVAL;
4041         }
4042         c->div = p;
4043
4044         val = clk_readl(c->reg);
4045         val &= ~PLLRE_BASE_DIVP_MASK;
4046         val |= pdiv << PLLRE_BASE_DIVP_SHIFT;
4047         clk_writel(val, c->reg);
4048
4049         clk_unlock_restore(c->parent, &flags);
4050         return 0;
4051 }
4052
4053 static struct clk_ops tegra_pllre_out_ops = {
4054         .init                   = tegra12_pllre_out_clk_init,
4055         .enable                 = tegra12_pllre_out_clk_enable,
4056         .disable                = tegra12_pllre_out_clk_disable,
4057         .set_rate               = tegra12_pllre_out_clk_set_rate,
4058 };
4059
4060 #ifdef CONFIG_PM_SLEEP
4061 /* Resume both pllre_vco and pllre_out */
4062 static void tegra12_pllre_clk_resume_enable(struct clk *c)
4063 {
4064         u32 pdiv;
4065         u32 val = clk_readl(c->reg + PLL_BASE);
4066         unsigned long rate = clk_get_rate_all_locked(c->parent->parent);
4067         enum clk_state state = c->parent->state;
4068
4069         if (val & PLL_BASE_ENABLE)
4070                 return;         /* already resumed */
4071
4072         /* temporarily sync h/w and s/w states, final sync happens
4073            in tegra_clk_resume later */
4074         c->parent->state = OFF;
4075         pllre_set_defaults(c->parent, rate);
4076
4077         /* restore PLLRE VCO feedback loop (m, n) */
4078         rate = clk_get_rate_all_locked(c->parent);
4079         tegra12_pllre_clk_set_rate(c->parent, rate);
4080
4081         /* restore PLLRE post-divider */
4082         c->parent->u.pll.round_p_to_pdiv(c->div, &pdiv);
4083         val = clk_readl(c->reg);
4084         val &= ~PLLRE_BASE_DIVP_MASK;
4085         val |= pdiv << PLLRE_BASE_DIVP_SHIFT;
4086         clk_writel(val, c->reg);
4087
4088         tegra12_pllre_clk_enable(c->parent);
4089         c->parent->state = state;
4090 }
4091 #endif
4092
4093 /* non-monotonic mapping below is not a typo */
4094 static u8 plle_p[PLLE_CMLDIV_MAX + 1] = {
4095 /* CMLDIV: 0, 1, 2, 3, 4, 5, 6,  7,  8,  9, 10, 11, 12, 13, 14 */
4096 /* p: */   1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 12, 16, 20, 24, 32 };
4097
4098 static inline void select_pll_e_input(u32 aux_reg)
4099 {
4100 #if USE_PLLE_INPUT_PLLRE
4101         aux_reg |= PLLE_AUX_PLLRE_SEL;
4102 #else
4103         aux_reg &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
4104 #endif
4105         clk_writel(aux_reg, PLLE_AUX);
4106 }
4107
4108 static void tegra12_plle_clk_init(struct clk *c)
4109 {
4110         u32 val, p;
4111         struct clk *pll_ref = tegra_get_clock_by_name("pll_ref");
4112         struct clk *re_vco = tegra_get_clock_by_name("pll_re_vco");
4113         struct clk *pllp = tegra_get_clock_by_name("pllp");
4114 #if USE_PLLE_INPUT_PLLRE
4115         struct clk *ref = re_vco;
4116 #else
4117         struct clk *ref = pll_ref;
4118 #endif
4119
4120         val = clk_readl(c->reg + PLL_BASE);
4121         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
4122         c->mul = (val & PLLE_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
4123         c->div = (val & PLLE_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
4124         p = (val & PLLE_BASE_DIVCML_MASK) >> PLLE_BASE_DIVCML_SHIFT;
4125         c->div *= plle_p[p];
4126
4127         val = clk_readl(PLLE_AUX);
4128         c->parent = (val & PLLE_AUX_PLLRE_SEL) ? re_vco :
4129                 (val & PLLE_AUX_PLLP_SEL) ? pllp : pll_ref;
4130         if (c->parent != ref) {
4131                 if (c->state == ON) {
4132                         WARN(1, "%s: pll_e is left enabled with %s input\n",
4133                              __func__, c->parent->name);
4134                 } else {
4135                         c->parent = ref;
4136                         select_pll_e_input(val);
4137                 }
4138         }
4139 }
4140
4141 static void tegra12_plle_clk_disable(struct clk *c)
4142 {
4143         u32 val;
4144         pr_debug("%s on clock %s\n", __func__, c->name);
4145
4146         /* FIXME: do we need to restore other s/w controls ? */
4147         val = clk_readl(c->reg + PLL_BASE);
4148         val &= ~PLL_BASE_ENABLE;
4149         clk_writel(val, c->reg + PLL_BASE);
4150
4151         val = clk_readl(c->reg + PLL_MISC(c));
4152         val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
4153         pll_writel_delay(val, c->reg + PLL_MISC(c));
4154
4155         /* Set XUSB PLL pad pwr override and iddq */
4156         val = xusb_padctl_readl(XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
4157         val |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_PWR_OVRD;
4158         val |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_IDDQ;
4159         xusb_padctl_writel(val, XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
4160         xusb_padctl_readl(XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
4161 }
4162
4163 static int tegra12_plle_clk_enable(struct clk *c)
4164 {
4165         u32 val;
4166         const struct clk_pll_freq_table *sel;
4167         unsigned long rate = c->u.pll.fixed_rate;
4168         unsigned long input_rate = clk_get_rate(c->parent);
4169
4170         if (c->state == ON) {
4171                 /* BL left plle enabled - don't change configuartion */
4172                 pr_warn("%s: pll_e is already enabled\n", __func__);
4173                 return 0;
4174         }
4175
4176         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
4177                 if (sel->input_rate == input_rate && sel->output_rate == rate)
4178                         break;
4179         }
4180
4181         if (sel->input_rate == 0) {
4182                 pr_err("%s: %s input rate %lu is out-of-table\n",
4183                        __func__, c->name, input_rate);
4184                 return -EINVAL;
4185         }
4186
4187         /* setup locking configuration, s/w control of IDDQ and enable modes,
4188            take pll out of IDDQ via s/w control, setup VREG */
4189         val = clk_readl(c->reg + PLL_BASE);
4190         val &= ~PLLE_BASE_LOCK_OVERRIDE;
4191         clk_writel(val, c->reg + PLL_BASE);
4192
4193         val = clk_readl(c->reg + PLL_MISC(c));
4194         val |= PLLE_MISC_LOCK_ENABLE;
4195         val |= PLLE_MISC_IDDQ_SW_CTRL;
4196         val &= ~PLLE_MISC_IDDQ_SW_VALUE;
4197         val |= PLLE_MISC_PLLE_PTS;
4198         clk_writel(val, c->reg + PLL_MISC(c));
4199         udelay(5);
4200
4201         /* configure dividers, disable SS */
4202         val = clk_readl(PLLE_SS_CTRL);
4203         val |= PLLE_SS_DISABLE;
4204         clk_writel(val, PLLE_SS_CTRL);
4205
4206         val = clk_readl(c->reg + PLL_BASE);
4207         val &= ~(PLLE_BASE_DIVM_MASK | PLLE_BASE_DIVN_MASK |
4208                  PLLE_BASE_DIVCML_MASK);
4209         val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
4210                 (sel->n << PLL_BASE_DIVN_SHIFT) |
4211                 (sel->cpcon << PLLE_BASE_DIVCML_SHIFT);
4212         pll_writel_delay(val, c->reg + PLL_BASE);
4213         c->mul = sel->n;
4214         c->div = sel->m * sel->p;
4215
4216         /* enable and lock pll */
4217         val |= PLL_BASE_ENABLE;
4218         clk_writel(val, c->reg + PLL_BASE);
4219         tegra12_pll_clk_wait_for_lock(
4220                 c, c->reg + PLL_MISC(c), PLLE_MISC_LOCK);
4221 #if USE_PLLE_SS
4222         val = clk_readl(PLLE_SS_CTRL);
4223         val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
4224         val &= ~PLLE_SS_COEFFICIENTS_MASK;
4225         val |= PLLE_SS_COEFFICIENTS_VAL;
4226         clk_writel(val, PLLE_SS_CTRL);
4227         val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
4228         pll_writel_delay(val, PLLE_SS_CTRL);
4229         val &= ~PLLE_SS_CNTL_INTERP_RESET;
4230         pll_writel_delay(val, PLLE_SS_CTRL);
4231 #endif
4232 #if !USE_PLLE_SWCTL
4233         /* switch pll under h/w control */
4234         val = clk_readl(c->reg + PLL_MISC(c));
4235         val &= ~PLLE_MISC_IDDQ_SW_CTRL;
4236         clk_writel(val, c->reg + PLL_MISC(c));
4237
4238         val = clk_readl(PLLE_AUX);
4239         val |= PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE;
4240         val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
4241         pll_writel_delay(val, PLLE_AUX);
4242         val |= PLLE_AUX_SEQ_ENABLE;
4243         pll_writel_delay(val, PLLE_AUX);
4244 #endif
4245
4246         /* clear XUSB PLL pad pwr override and iddq */
4247         val = xusb_padctl_readl(XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
4248         val &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_PWR_OVRD;
4249         val &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0_PLL_IDDQ;
4250         xusb_padctl_writel(val, XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
4251         xusb_padctl_readl(XUSB_PADCTL_IOPHY_PLL_P0_CTL1_0);
4252
4253         /* enable hw control of xusb brick pll */
4254         usb_plls_hw_control_enable(XUSBIO_PLL_CFG0);
4255
4256         return 0;
4257 }
4258
4259 #ifdef CONFIG_PM_SLEEP
4260 static void tegra12_plle_clk_resume(struct clk *c)
4261 {
4262         u32 val = clk_readl(c->reg + PLL_BASE);
4263         if (val & PLL_BASE_ENABLE)
4264                 return;         /* already resumed */
4265
4266         /* Restore parent */
4267         val = clk_readl(PLLE_AUX);
4268         select_pll_e_input(val);
4269 }
4270 #endif
4271
4272 static struct clk_ops tegra_plle_ops = {
4273         .init                   = tegra12_plle_clk_init,
4274         .enable                 = tegra12_plle_clk_enable,
4275         .disable                = tegra12_plle_clk_disable,
4276 };
4277
4278 /*
4279  * Tegra12 includes dynamic frequency lock loop (DFLL) with automatic voltage
4280  * control as possible CPU clock source. It is included in the Tegra12 clock
4281  * tree as "complex PLL" with standard Tegra clock framework APIs. However,
4282  * DFLL locking logic h/w access APIs are separated in the tegra_cl_dvfs.c
4283  * module. Hence, DFLL operations, with the exception of initialization, are
4284  * basically cl-dvfs wrappers.
4285  */
4286
4287 /* DFLL operations */
4288 #ifdef  CONFIG_ARCH_TEGRA_HAS_CL_DVFS
4289 static void tune_cpu_trimmers(bool trim_high)
4290 {
4291         tegra_soctherm_adjust_cpu_zone(trim_high);
4292 }
4293 #endif
4294
4295 static void __init tegra12_dfll_clk_init(struct clk *c)
4296 {
4297         c->ops->init = tegra12_dfll_cpu_late_init;
4298 }
4299
4300 static int tegra12_dfll_clk_enable(struct clk *c)
4301 {
4302         return tegra_cl_dvfs_enable(c->u.dfll.cl_dvfs);
4303 }
4304
4305 static void tegra12_dfll_clk_disable(struct clk *c)
4306 {
4307         tegra_cl_dvfs_disable(c->u.dfll.cl_dvfs);
4308 }
4309
4310 static int tegra12_dfll_clk_set_rate(struct clk *c, unsigned long rate)
4311 {
4312         int ret = tegra_cl_dvfs_request_rate(c->u.dfll.cl_dvfs, rate);
4313
4314         if (!ret)
4315                 c->rate = tegra_cl_dvfs_request_get(c->u.dfll.cl_dvfs);
4316
4317         return ret;
4318 }
4319
4320 static void tegra12_dfll_clk_reset(struct clk *c, bool assert)
4321 {
4322         u32 val = assert ? DFLL_BASE_RESET : 0;
4323         clk_writelx_delay(val, c->reg);
4324 }
4325
4326 static int
4327 tegra12_dfll_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
4328 {
4329         if (p == TEGRA_CLK_DFLL_LOCK)
4330                 return setting ? tegra_cl_dvfs_lock(c->u.dfll.cl_dvfs) :
4331                                  tegra_cl_dvfs_unlock(c->u.dfll.cl_dvfs);
4332         return -EINVAL;
4333 }
4334
4335 #ifdef CONFIG_PM_SLEEP
4336 static void tegra12_dfll_clk_resume(struct clk *c)
4337 {
4338         if (!(clk_readlx(c->reg) & DFLL_BASE_RESET))
4339                 return;         /* already resumed */
4340
4341         if (c->state != UNINITIALIZED) {
4342                 tegra_periph_reset_deassert(c);
4343                 tegra_cl_dvfs_resume(c->u.dfll.cl_dvfs);
4344         }
4345 }
4346 #endif
4347
4348 static struct clk_ops tegra_dfll_ops = {
4349         .init                   = tegra12_dfll_clk_init,
4350         .enable                 = tegra12_dfll_clk_enable,
4351         .disable                = tegra12_dfll_clk_disable,
4352         .set_rate               = tegra12_dfll_clk_set_rate,
4353         .reset                  = tegra12_dfll_clk_reset,
4354         .clk_cfg_ex             = tegra12_dfll_clk_cfg_ex,
4355 };
4356
4357 /* DFLL sysfs interface */
4358 static int tegra12_use_dfll_cb(const char *arg, const struct kernel_param *kp)
4359 {
4360         int ret = 0;
4361         unsigned long c_flags, p_flags;
4362         unsigned int old_use_dfll;
4363         struct clk *c = tegra_get_clock_by_name("cpu");
4364         struct clk *dfll = tegra_get_clock_by_name("dfll_cpu");
4365
4366         if (!c->parent || !c->parent->dvfs || !dfll)
4367                 return -ENOSYS;
4368
4369         ret = tegra_cpu_reg_mode_force_normal(true);
4370         if (ret) {
4371                 pr_err("%s: Failed to force regulator normal mode\n", __func__);
4372                 return ret;
4373         }
4374
4375         clk_lock_save(c, &c_flags);
4376         if (dfll->state == UNINITIALIZED) {
4377                 pr_err("%s: DFLL is not initialized\n", __func__);
4378                 clk_unlock_restore(c, &c_flags);
4379                 tegra_cpu_reg_mode_force_normal(false);
4380                 return -ENOSYS;
4381         }
4382         if (c->parent->u.cpu.mode == MODE_LP) {
4383                 pr_err("%s: DFLL is not used on LP CPU\n", __func__);
4384                 clk_unlock_restore(c, &c_flags);
4385                 tegra_cpu_reg_mode_force_normal(false);
4386                 return -ENOSYS;
4387         }
4388
4389         clk_lock_save(c->parent, &p_flags);
4390         old_use_dfll = use_dfll;
4391         param_set_int(arg, kp);
4392
4393         if (use_dfll != old_use_dfll) {
4394                 ret = tegra_dvfs_set_dfll_range(c->parent->dvfs, use_dfll);
4395                 if (ret) {
4396                         use_dfll = old_use_dfll;
4397                 } else {
4398                         ret = clk_set_rate_locked(c->parent,
4399                                 clk_get_rate_locked(c->parent));
4400                         if (ret) {
4401                                 use_dfll = old_use_dfll;
4402                                 tegra_dvfs_set_dfll_range(
4403                                         c->parent->dvfs, use_dfll);
4404                         }
4405                 }
4406         }
4407         clk_unlock_restore(c->parent, &p_flags);
4408         clk_unlock_restore(c, &c_flags);
4409         tegra_update_cpu_edp_limits();
4410         return ret;
4411 }
4412
4413 static struct kernel_param_ops tegra12_use_dfll_ops = {
4414         .set = tegra12_use_dfll_cb,
4415         .get = param_get_int,
4416 };
4417 module_param_cb(use_dfll, &tegra12_use_dfll_ops, &use_dfll, 0644);
4418
4419
4420 /* Clock divider ops (non-atomic shared register access) */
4421 static DEFINE_SPINLOCK(pll_div_lock);
4422
4423 static int tegra12_pll_div_clk_set_rate(struct clk *c, unsigned long rate);
4424 static void tegra12_pll_div_clk_init(struct clk *c)
4425 {
4426         if (c->flags & DIV_U71) {
4427                 u32 val, divu71;
4428                 if (c->parent->state == OFF)
4429                         c->ops->disable(c);
4430
4431                 val = clk_readl(c->reg);
4432                 val >>= c->reg_shift;
4433                 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
4434                 if (!(val & PLL_OUT_RESET_DISABLE))
4435                         c->state = OFF;
4436
4437                 if (c->u.pll_div.default_rate) {
4438                         int ret = tegra12_pll_div_clk_set_rate(
4439                                         c, c->u.pll_div.default_rate);
4440                         if (!ret)
4441                                 return;
4442                 }
4443                 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
4444                 c->div = (divu71 + 2);
4445                 c->mul = 2;
4446         } else if (c->flags & DIV_2) {
4447                 c->state = ON;
4448                 if (c->flags & (PLLD | PLLX)) {
4449                         c->div = 2;
4450                         c->mul = 1;
4451                 }
4452                 else
4453                         BUG();
4454         } else if (c->flags & PLLU) {
4455                 u32 val = clk_readl(c->reg);
4456                 c->state = val & (0x1 << c->reg_shift) ? ON : OFF;
4457         } else {
4458                 c->state = ON;
4459                 c->div = 1;
4460                 c->mul = 1;
4461         }
4462 }
4463
4464 static int tegra12_pll_div_clk_enable(struct clk *c)
4465 {
4466         u32 val;
4467         u32 new_val;
4468         unsigned long flags;
4469
4470         pr_debug("%s: %s\n", __func__, c->name);
4471         if (c->flags & DIV_U71) {
4472                 spin_lock_irqsave(&pll_div_lock, flags);
4473                 val = clk_readl(c->reg);
4474                 new_val = val >> c->reg_shift;
4475                 new_val &= 0xFFFF;
4476
4477                 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
4478
4479                 val &= ~(0xFFFF << c->reg_shift);
4480                 val |= new_val << c->reg_shift;
4481                 clk_writel_delay(val, c->reg);
4482                 spin_unlock_irqrestore(&pll_div_lock, flags);
4483                 return 0;
4484         } else if (c->flags & DIV_2) {
4485                 return 0;
4486         } else if (c->flags & PLLU) {
4487                 clk_lock_save(c->parent, &flags);
4488                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
4489                 clk_writel_delay(val, c->reg);
4490                 clk_unlock_restore(c->parent, &flags);
4491                 return 0;
4492         }
4493         return -EINVAL;
4494 }
4495
4496 static void tegra12_pll_div_clk_disable(struct clk *c)
4497 {
4498         u32 val;
4499         u32 new_val;
4500         unsigned long flags;
4501
4502         pr_debug("%s: %s\n", __func__, c->name);
4503         if (c->flags & DIV_U71) {
4504                 spin_lock_irqsave(&pll_div_lock, flags);
4505                 val = clk_readl(c->reg);
4506                 new_val = val >> c->reg_shift;
4507                 new_val &= 0xFFFF;
4508
4509                 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
4510
4511                 val &= ~(0xFFFF << c->reg_shift);
4512                 val |= new_val << c->reg_shift;
4513                 clk_writel_delay(val, c->reg);
4514                 spin_unlock_irqrestore(&pll_div_lock, flags);
4515         } else if (c->flags & PLLU) {
4516                 clk_lock_save(c->parent, &flags);
4517                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
4518                 clk_writel_delay(val, c->reg);
4519                 clk_unlock_restore(c->parent, &flags);
4520         }
4521 }
4522
4523 static int tegra12_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
4524 {
4525         u32 val;
4526         u32 new_val;
4527         int divider_u71;
4528         unsigned long parent_rate = clk_get_rate(c->parent);
4529         unsigned long flags;
4530
4531         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4532         if (tegra_platform_is_qt())
4533                 return 0;
4534         if (c->flags & DIV_U71) {
4535                 divider_u71 = clk_div71_get_divider(
4536                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4537                 if (divider_u71 >= 0) {
4538                         spin_lock_irqsave(&pll_div_lock, flags);
4539                         val = clk_readl(c->reg);
4540                         new_val = val >> c->reg_shift;
4541                         new_val &= 0xFFFF;
4542                         if (c->flags & DIV_U71_FIXED)
4543                                 new_val |= PLL_OUT_OVERRIDE;
4544                         new_val &= ~PLL_OUT_RATIO_MASK;
4545                         new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
4546
4547                         val &= ~(0xFFFF << c->reg_shift);
4548                         val |= new_val << c->reg_shift;
4549                         clk_writel_delay(val, c->reg);
4550                         c->div = divider_u71 + 2;
4551                         c->mul = 2;
4552                         spin_unlock_irqrestore(&pll_div_lock, flags);
4553                         return 0;
4554                 }
4555         } else if (c->flags & DIV_2)
4556                 return clk_set_rate(c->parent, rate * 2);
4557
4558         return -EINVAL;
4559 }
4560
4561 static long tegra12_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
4562 {
4563         int divider;
4564         unsigned long parent_rate = clk_get_rate(c->parent);
4565         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4566
4567         if (c->flags & DIV_U71) {
4568                 divider = clk_div71_get_divider(
4569                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4570                 if (divider < 0)
4571                         return divider;
4572                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4573         } else if (c->flags & DIV_2)
4574                 /* no rounding - fixed DIV_2 dividers pass rate to parent PLL */
4575                 return rate;
4576
4577         return -EINVAL;
4578 }
4579
4580 static struct clk_ops tegra_pll_div_ops = {
4581         .init                   = tegra12_pll_div_clk_init,
4582         .enable                 = tegra12_pll_div_clk_enable,
4583         .disable                = tegra12_pll_div_clk_disable,
4584         .set_rate               = tegra12_pll_div_clk_set_rate,
4585         .round_rate             = tegra12_pll_div_clk_round_rate,
4586 };
4587
4588 /* Periph clk ops */
4589 static inline u32 periph_clk_source_mask(struct clk *c)
4590 {
4591         if (c->u.periph.src_mask)
4592                 return c->u.periph.src_mask;
4593         else if (c->flags & MUX_PWM)
4594                 return 3 << 28;
4595         else if (c->flags & MUX_CLK_OUT)
4596                 return 3 << (c->u.periph.clk_num + 4);
4597         else if (c->flags & PLLD)
4598                 return PLLD_BASE_DSI_MUX_MASK;
4599         else
4600                 return 7 << 29;
4601 }
4602
4603 static inline u32 periph_clk_source_shift(struct clk *c)
4604 {
4605         if (c->u.periph.src_shift)
4606                 return c->u.periph.src_shift;
4607         else if (c->flags & MUX_PWM)
4608                 return 28;
4609         else if (c->flags & MUX_CLK_OUT)
4610                 return c->u.periph.clk_num + 4;
4611         else if (c->flags & PLLD)
4612                 return PLLD_BASE_DSI_MUX_SHIFT;
4613         else
4614                 return 29;
4615 }
4616
4617 static void tegra12_periph_clk_init(struct clk *c)
4618 {
4619         u32 val = clk_readl(c->reg);
4620         const struct clk_mux_sel *mux = 0;
4621         const struct clk_mux_sel *sel;
4622         if (c->flags & MUX) {
4623                 for (sel = c->inputs; sel->input != NULL; sel++) {
4624                         if (((val & periph_clk_source_mask(c)) >>
4625                             periph_clk_source_shift(c)) == sel->value)
4626                                 mux = sel;
4627                 }
4628                 BUG_ON(!mux);
4629
4630                 c->parent = mux->input;
4631         } else {
4632                 if (c->flags & PLLU) {
4633                         /* for xusb_hs clock enforce SS div2 source */
4634                         val &= ~periph_clk_source_mask(c);
4635                         clk_writel_delay(val, c->reg);
4636                 }
4637                 c->parent = c->inputs[0].input;
4638         }
4639
4640         /* if peripheral is left under reset - enforce safe rate */
4641
4642         if (c->flags & PERIPH_NO_RESET) {
4643                 if (tegra12_periph_is_special_reset(c)) {
4644                         tegra_periph_clk_safe_rate_init(c);
4645                          val = clk_readl(c->reg);
4646                 }
4647         } else if (IS_PERIPH_IN_RESET(c)) {
4648                 tegra_periph_clk_safe_rate_init(c);
4649                 val = clk_readl(c->reg);
4650         }
4651
4652         if (c->flags & DIV_U71) {
4653                 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
4654                 if (c->flags & DIV_U71_IDLE) {
4655                         val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
4656                                 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
4657                         val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
4658                                 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
4659                         clk_writel(val, c->reg);
4660                 }
4661                 c->div = divu71 + 2;
4662                 c->mul = 2;
4663         } else if (c->flags & DIV_U151) {
4664                 u32 divu151 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
4665                 if ((c->flags & DIV_U151_UART) &&
4666                     (!(val & PERIPH_CLK_UART_DIV_ENB))) {
4667                         divu151 = 0;
4668                 }
4669                 c->div = divu151 + 2;
4670                 c->mul = 2;
4671         } else if (c->flags & DIV_U16) {
4672                 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
4673                 c->div = divu16 + 1;
4674                 c->mul = 1;
4675         } else {
4676                 c->div = 1;
4677                 c->mul = 1;
4678         }
4679
4680         if (c->flags & PERIPH_NO_ENB) {
4681                 c->state = c->parent->state;
4682                 return;
4683         }
4684
4685         c->state = ON;
4686
4687         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
4688                 c->state = OFF;
4689         if (!(c->flags & PERIPH_NO_RESET))
4690                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
4691                         c->state = OFF;
4692 }
4693
4694 static int tegra12_periph_clk_enable(struct clk *c)
4695 {
4696         unsigned long flags;
4697         pr_debug("%s on clock %s\n", __func__, c->name);
4698
4699         if (c->flags & PERIPH_NO_ENB)
4700                 return 0;
4701
4702         spin_lock_irqsave(&periph_refcount_lock, flags);
4703
4704         tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
4705         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1) {
4706                 spin_unlock_irqrestore(&periph_refcount_lock, flags);
4707                 return 0;
4708         }
4709
4710         clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
4711         if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET)) {
4712                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c)) {
4713                         udelay(RESET_PROPAGATION_DELAY);
4714                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4715                                          PERIPH_CLK_TO_RST_CLR_REG(c));
4716                 }
4717         }
4718         spin_unlock_irqrestore(&periph_refcount_lock, flags);
4719         return 0;
4720 }
4721
4722 static void tegra12_periph_clk_disable(struct clk *c)
4723 {
4724         unsigned long val, flags;
4725         pr_debug("%s on clock %s\n", __func__, c->name);
4726
4727         if (c->flags & PERIPH_NO_ENB)
4728                 return;
4729
4730         spin_lock_irqsave(&periph_refcount_lock, flags);
4731
4732         if (c->refcnt)
4733                 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
4734
4735         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0) {
4736                 /* If peripheral is in the APB bus then read the APB bus to
4737                  * flush the write operation in apb bus. This will avoid the
4738                  * peripheral access after disabling clock*/
4739                 if (c->flags & PERIPH_ON_APB)
4740                         val = tegra_read_chipid();
4741
4742                 clk_writel_delay(
4743                         PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
4744         }
4745         spin_unlock_irqrestore(&periph_refcount_lock, flags);
4746 }
4747
4748 static void tegra12_periph_clk_reset(struct clk *c, bool assert)
4749 {
4750         unsigned long val;
4751         pr_debug("%s %s on clock %s\n", __func__,
4752                  assert ? "assert" : "deassert", c->name);
4753
4754         if (c->flags & PERIPH_NO_ENB)
4755                 return;
4756
4757         if (!(c->flags & PERIPH_NO_RESET)) {
4758                 if (assert) {
4759                         /* If peripheral is in the APB bus then read the APB
4760                          * bus to flush the write operation in apb bus. This
4761                          * will avoid the peripheral access after disabling
4762                          * clock */
4763                         if (c->flags & PERIPH_ON_APB)
4764                                 val = tegra_read_chipid();
4765
4766                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4767                                          PERIPH_CLK_TO_RST_SET_REG(c));
4768                 } else
4769                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4770                                          PERIPH_CLK_TO_RST_CLR_REG(c));
4771         }
4772 }
4773
4774 static int tegra12_periph_clk_set_parent(struct clk *c, struct clk *p)
4775 {
4776         u32 val;
4777         const struct clk_mux_sel *sel;
4778         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
4779
4780         if (!(c->flags & MUX))
4781                 return (p == c->parent) ? 0 : (-EINVAL);
4782
4783         for (sel = c->inputs; sel->input != NULL; sel++) {
4784                 if (sel->input == p) {
4785                         val = clk_readl(c->reg);
4786                         val &= ~periph_clk_source_mask(c);
4787                         val |= (sel->value << periph_clk_source_shift(c));
4788
4789                         if (c->refcnt)
4790                                 clk_enable(p);
4791
4792                         clk_writel_delay(val, c->reg);
4793
4794                         if (c->refcnt && c->parent)
4795                                 clk_disable(c->parent);
4796
4797                         clk_reparent(c, p);
4798                         return 0;
4799                 }
4800         }
4801
4802         return -EINVAL;
4803 }
4804
4805 static int tegra12_periph_clk_set_rate(struct clk *c, unsigned long rate)
4806 {
4807         u32 val;
4808         int divider;
4809         unsigned long parent_rate = clk_get_rate(c->parent);
4810
4811         if (tegra_platform_is_qt())
4812                 return 0;
4813         if (c->flags & DIV_U71) {
4814                 divider = clk_div71_get_divider(
4815                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4816                 if (divider >= 0) {
4817                         val = clk_readl(c->reg);
4818                         val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
4819                         val |= divider;
4820                         clk_writel_delay(val, c->reg);
4821                         c->div = divider + 2;
4822                         c->mul = 2;
4823                         return 0;
4824                 }
4825         } else if (c->flags & DIV_U151) {
4826                 divider = clk_div151_get_divider(
4827                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4828                 if (divider >= 0) {
4829                         val = clk_readl(c->reg);
4830                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
4831                         val |= divider;
4832                         if (c->flags & DIV_U151_UART) {
4833                                 if (divider)
4834                                         val |= PERIPH_CLK_UART_DIV_ENB;
4835                                 else
4836                                         val &= ~PERIPH_CLK_UART_DIV_ENB;
4837                         }
4838                         clk_writel_delay(val, c->reg);
4839                         c->div = divider + 2;
4840                         c->mul = 2;
4841                         return 0;
4842                 }
4843         } else if (c->flags & DIV_U16) {
4844                 divider = clk_div16_get_divider(parent_rate, rate);
4845                 if (divider >= 0) {
4846                         val = clk_readl(c->reg);
4847                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
4848                         val |= divider;
4849                         clk_writel_delay(val, c->reg);
4850                         c->div = divider + 1;
4851                         c->mul = 1;
4852                         return 0;
4853                 }
4854         } else if (parent_rate <= rate) {
4855                 c->div = 1;
4856                 c->mul = 1;
4857                 return 0;
4858         }
4859         return -EINVAL;
4860 }
4861
4862 static long tegra12_periph_clk_round_rate(struct clk *c,
4863         unsigned long rate)
4864 {
4865         int divider;
4866         unsigned long parent_rate = clk_get_rate(c->parent);
4867         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4868
4869         if (c->flags & DIV_U71) {
4870                 divider = clk_div71_get_divider(
4871                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4872                 if (divider < 0)
4873                         return divider;
4874
4875                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4876         } else if (c->flags & DIV_U151) {
4877                 divider = clk_div151_get_divider(
4878                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4879                 if (divider < 0)
4880                         return divider;
4881
4882                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4883         } else if (c->flags & DIV_U16) {
4884                 divider = clk_div16_get_divider(parent_rate, rate);
4885                 if (divider < 0)
4886                         return divider;
4887                 return DIV_ROUND_UP(parent_rate, divider + 1);
4888         }
4889         return -EINVAL;
4890 }
4891
4892 static struct clk_ops tegra_periph_clk_ops = {
4893         .init                   = &tegra12_periph_clk_init,
4894         .enable                 = &tegra12_periph_clk_enable,
4895         .disable                = &tegra12_periph_clk_disable,
4896         .set_parent             = &tegra12_periph_clk_set_parent,
4897         .set_rate               = &tegra12_periph_clk_set_rate,
4898         .round_rate             = &tegra12_periph_clk_round_rate,
4899         .reset                  = &tegra12_periph_clk_reset,
4900 };
4901
4902 /* 1x shared bus ops */
4903 static long _1x_round_updown(struct clk *c, struct clk *src,
4904                              unsigned long rate, bool up)
4905 {
4906         return fixed_src_bus_round_updown(c, src, c->flags, rate, up);
4907 }
4908
4909 static long tegra12_1xbus_round_updown(struct clk *c, unsigned long rate,
4910                                             bool up)
4911 {
4912         unsigned long pll_low_rate, pll_high_rate;
4913
4914         rate = max(rate, c->min_rate);
4915
4916         pll_low_rate = _1x_round_updown(c, c->u.periph.pll_low, rate, up);
4917         if (rate <= c->u.periph.threshold) {
4918                 c->u.periph.pll_selected = c->u.periph.pll_low;
4919                 return pll_low_rate;
4920         }
4921
4922         pll_high_rate = _1x_round_updown(c, c->u.periph.pll_high, rate, up);
4923         if (pll_high_rate <= c->u.periph.threshold) {
4924                 c->u.periph.pll_selected = c->u.periph.pll_low;
4925                 return pll_low_rate;  /* prevent oscillation across threshold */
4926         }
4927
4928         if (up) {
4929                 /* rounding up: both plls may hit max, and round down */
4930                 if (pll_high_rate < rate) {
4931                         if (pll_low_rate < pll_high_rate) {
4932                                 c->u.periph.pll_selected = c->u.periph.pll_high;
4933                                 return pll_high_rate;
4934                         }
4935                 } else {
4936                         if ((pll_low_rate < rate) ||
4937                             (pll_low_rate > pll_high_rate)) {
4938                                 c->u.periph.pll_selected = c->u.periph.pll_high;
4939                                 return pll_high_rate;
4940                         }
4941                 }
4942         } else if (pll_low_rate < pll_high_rate) {
4943                 /* rounding down: to get here both plls able to round down */
4944                 c->u.periph.pll_selected = c->u.periph.pll_high;
4945                 return pll_high_rate;
4946         }
4947         c->u.periph.pll_selected = c->u.periph.pll_low;
4948         return pll_low_rate;
4949 }
4950
4951 static long tegra12_1xbus_round_rate(struct clk *c, unsigned long rate)
4952 {
4953         return tegra12_1xbus_round_updown(c, rate, true);
4954 }
4955
4956 static int tegra12_1xbus_set_rate(struct clk *c, unsigned long rate)
4957 {
4958         /* Compensate rate truncating during rounding */
4959         return tegra12_periph_clk_set_rate(c, rate + 1);
4960 }
4961
4962 static int tegra12_clk_1xbus_update(struct clk *c)
4963 {
4964         int ret;
4965         struct clk *new_parent;
4966         unsigned long rate, old_rate;
4967
4968         if (detach_shared_bus)
4969                 return 0;
4970
4971         rate = tegra12_clk_shared_bus_update(c, NULL, NULL, NULL);
4972
4973         old_rate = clk_get_rate_locked(c);
4974         pr_debug("\n1xbus %s: rate %lu on parent %s: new request %lu\n",
4975                  c->name, old_rate, c->parent->name, rate);
4976         if (rate == old_rate)
4977                 return 0;
4978
4979         if (!c->u.periph.min_div_low || !c->u.periph.min_div_high) {
4980                 unsigned long r, m = c->max_rate;
4981                 r = clk_get_rate(c->u.periph.pll_low);
4982                 c->u.periph.min_div_low = DIV_ROUND_UP(r, m) * c->mul;
4983                 r = clk_get_rate(c->u.periph.pll_high);
4984                 c->u.periph.min_div_high = DIV_ROUND_UP(r, m) * c->mul;
4985         }
4986
4987         new_parent = c->u.periph.pll_selected;
4988
4989         /*
4990          * The transition procedure below is guaranteed to switch to the target
4991          * parent/rate without violation of max clock limits. It would attempt
4992          * to switch without dip in bus rate if it is possible, but this cannot
4993          * be guaranteed (example: switch from 408 MHz : 1 to 624 MHz : 2 with
4994          * maximum bus limit 408 MHz will be executed as 408 => 204 => 312 MHz,
4995          * and there is no way to avoid rate dip in this case).
4996          */
4997         if (new_parent != c->parent) {
4998                 int interim_div = 0;
4999                 /* Switching to pll_high may over-clock bus if current divider
5000                    is too small - increase divider to safe value */
5001                 if ((new_parent == c->u.periph.pll_high) &&
5002                     (c->div < c->u.periph.min_div_high))
5003                         interim_div = c->u.periph.min_div_high;
5004
5005                 /* Switching to pll_low may dip down rate if current divider
5006                    is too big - decrease divider as much as we can */
5007                 if ((new_parent == c->u.periph.pll_low) &&
5008                     (c->div > c->u.periph.min_div_low) &&
5009                     (c->div > c->u.periph.min_div_high))
5010                         interim_div = c->u.periph.min_div_low;
5011
5012                 if (interim_div) {
5013                         u64 interim_rate = old_rate * c->div;
5014                         do_div(interim_rate, interim_div);
5015                         ret = clk_set_rate_locked(c, interim_rate);
5016                         if (ret) {
5017                                 pr_err("Failed to set %s rate to %lu\n",
5018                                        c->name, (unsigned long)interim_rate);
5019                                 return ret;
5020                         }
5021                         pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
5022                                  clk_get_rate_locked(c), c->parent->name);
5023                 }
5024
5025                 ret = clk_set_parent_locked(c, new_parent);
5026                 if (ret) {
5027                         pr_err("Failed to set %s parent %s\n",
5028                                c->name, new_parent->name);
5029                         return ret;
5030                 }
5031
5032                 old_rate = clk_get_rate_locked(c);
5033                 pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
5034                          old_rate, c->parent->name);
5035                 if (rate == old_rate)
5036                         return 0;
5037         }
5038
5039         ret = clk_set_rate_locked(c, rate);
5040         if (ret) {
5041                 pr_err("Failed to set %s rate to %lu\n", c->name, rate);
5042                 return ret;
5043         }
5044         pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
5045                  clk_get_rate_locked(c), c->parent->name);
5046         return 0;
5047
5048 }
5049
5050 static struct clk_ops tegra_1xbus_clk_ops = {
5051         .init                   = &tegra12_periph_clk_init,
5052         .enable                 = &tegra12_periph_clk_enable,
5053         .disable                = &tegra12_periph_clk_disable,
5054         .set_parent             = &tegra12_periph_clk_set_parent,
5055         .set_rate               = &tegra12_1xbus_set_rate,
5056         .round_rate             = &tegra12_1xbus_round_rate,
5057         .round_rate_updown      = &tegra12_1xbus_round_updown,
5058         .reset                  = &tegra12_periph_clk_reset,
5059         .shared_bus_update      = &tegra12_clk_1xbus_update,
5060 };
5061
5062 /* msenc clock propagation WAR for bug 1005168 */
5063 static int tegra12_msenc_clk_enable(struct clk *c)
5064 {
5065         int ret = tegra12_periph_clk_enable(c);
5066         if (ret)
5067                 return ret;
5068
5069         clk_writel(0, LVL2_CLK_GATE_OVRE);
5070         clk_writel(0x00400000, LVL2_CLK_GATE_OVRE);
5071         udelay(1);
5072         clk_writel(0, LVL2_CLK_GATE_OVRE);
5073         return 0;
5074 }
5075
5076 static struct clk_ops tegra_msenc_clk_ops = {
5077         .init                   = &tegra12_periph_clk_init,
5078         .enable                 = &tegra12_msenc_clk_enable,
5079         .disable                = &tegra12_periph_clk_disable,
5080         .set_parent             = &tegra12_periph_clk_set_parent,
5081         .set_rate               = &tegra12_periph_clk_set_rate,
5082         .round_rate             = &tegra12_periph_clk_round_rate,
5083         .reset                  = &tegra12_periph_clk_reset,
5084 };
5085 /* Periph extended clock configuration ops */
5086 static int
5087 tegra12_vi_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
5088 {
5089         if (p == TEGRA_CLK_VI_INP_SEL) {
5090                 u32 val = clk_readl(c->reg);
5091                 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
5092                 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
5093                         PERIPH_CLK_VI_SEL_EX_MASK;
5094                 clk_writel(val, c->reg);
5095                 return 0;
5096         }
5097         return -EINVAL;
5098 }
5099
5100 static struct clk_ops tegra_vi_clk_ops = {
5101         .init                   = &tegra12_periph_clk_init,
5102         .enable                 = &tegra12_periph_clk_enable,
5103         .disable                = &tegra12_periph_clk_disable,
5104         .set_parent             = &tegra12_periph_clk_set_parent,
5105         .set_rate               = &tegra12_periph_clk_set_rate,
5106         .round_rate             = &tegra12_periph_clk_round_rate,
5107         .clk_cfg_ex             = &tegra12_vi_clk_cfg_ex,
5108         .reset                  = &tegra12_periph_clk_reset,
5109 };
5110
5111 static int
5112 tegra12_sor_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
5113 {
5114         if (p == TEGRA_CLK_SOR_CLK_SEL) {
5115                 u32 val = clk_readl(c->reg);
5116                 val &= ~PERIPH_CLK_SOR_CLK_SEL_MASK;
5117                 val |= (setting << PERIPH_CLK_SOR_CLK_SEL_SHIFT) &
5118                         PERIPH_CLK_SOR_CLK_SEL_MASK;
5119                 clk_writel(val, c->reg);
5120                 return 0;
5121         }
5122         return -EINVAL;
5123 }
5124
5125 static struct clk_ops tegra_sor_clk_ops = {
5126         .init                   = &tegra12_periph_clk_init,
5127         .enable                 = &tegra12_periph_clk_enable,
5128         .disable                = &tegra12_periph_clk_disable,
5129         .set_parent             = &tegra12_periph_clk_set_parent,
5130         .set_rate               = &tegra12_periph_clk_set_rate,
5131         .round_rate             = &tegra12_periph_clk_round_rate,
5132         .clk_cfg_ex             = &tegra12_sor_clk_cfg_ex,
5133         .reset                  = &tegra12_periph_clk_reset,
5134 };
5135
5136 static int
5137 tegra12_dtv_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
5138 {
5139         if (p == TEGRA_CLK_DTV_INVERT) {
5140                 u32 val = clk_readl(c->reg);
5141                 if (setting)
5142                         val |= PERIPH_CLK_DTV_POLARITY_INV;
5143                 else
5144                         val &= ~PERIPH_CLK_DTV_POLARITY_INV;
5145                 clk_writel(val, c->reg);
5146                 return 0;
5147         }
5148         return -EINVAL;
5149 }
5150
5151 static struct clk_ops tegra_dtv_clk_ops = {
5152         .init                   = &tegra12_periph_clk_init,
5153         .enable                 = &tegra12_periph_clk_enable,
5154         .disable                = &tegra12_periph_clk_disable,
5155         .set_parent             = &tegra12_periph_clk_set_parent,
5156         .set_rate               = &tegra12_periph_clk_set_rate,
5157         .round_rate             = &tegra12_periph_clk_round_rate,
5158         .clk_cfg_ex             = &tegra12_dtv_clk_cfg_ex,
5159         .reset                  = &tegra12_periph_clk_reset,
5160 };
5161
5162 static struct clk_ops tegra_dsi_clk_ops = {
5163         .init                   = &tegra12_periph_clk_init,
5164         .enable                 = &tegra12_periph_clk_enable,
5165         .disable                = &tegra12_periph_clk_disable,
5166         .set_rate               = &tegra12_periph_clk_set_rate,
5167         .round_rate             = &tegra12_periph_clk_round_rate,
5168         .reset                  = &tegra12_periph_clk_reset,
5169 };
5170
5171 /* pciex clock support only reset function */
5172 static void tegra12_pciex_clk_init(struct clk *c)
5173 {
5174         c->state = c->parent->state;
5175 }
5176
5177 static int tegra12_pciex_clk_enable(struct clk *c)
5178 {
5179         return 0;
5180 }
5181
5182 static void tegra12_pciex_clk_disable(struct clk *c)
5183 {
5184 }
5185
5186 static int tegra12_pciex_clk_set_rate(struct clk *c, unsigned long rate)
5187 {
5188         unsigned long parent_rate = clk_get_rate(c->parent);
5189
5190         /*
5191          * the only supported pcie configurations:
5192          * Gen1: plle = 100MHz, link at 250MHz
5193          * Gen2: plle = 100MHz, link at 500MHz
5194          */
5195         if (parent_rate == 100000000) {
5196                 if (rate == 500000000) {
5197                         c->mul = 5;
5198                         c->div = 1;
5199                         return 0;
5200                 } else if (rate == 250000000) {
5201                         c->mul = 5;
5202                         c->div = 2;
5203                         return 0;
5204                 }
5205         }
5206         return -EINVAL;
5207 }
5208
5209 static struct clk_ops tegra_pciex_clk_ops = {
5210         .init     = tegra12_pciex_clk_init,
5211         .enable   = tegra12_pciex_clk_enable,
5212         .disable  = tegra12_pciex_clk_disable,
5213         .set_rate = tegra12_pciex_clk_set_rate,
5214         .reset    = tegra12_periph_clk_reset,
5215 };
5216
5217 /* Output clock ops */
5218
5219 static DEFINE_SPINLOCK(clk_out_lock);
5220
5221 static void tegra12_clk_out_init(struct clk *c)
5222 {
5223         const struct clk_mux_sel *mux = 0;
5224         const struct clk_mux_sel *sel;
5225         u32 val = pmc_readl(c->reg);
5226
5227         c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
5228         c->mul = 1;
5229         c->div = 1;
5230
5231         for (sel = c->inputs; sel->input != NULL; sel++) {
5232                 if (((val & periph_clk_source_mask(c)) >>
5233                      periph_clk_source_shift(c)) == sel->value)
5234                         mux = sel;
5235         }
5236         BUG_ON(!mux);
5237         c->parent = mux->input;
5238 }
5239
5240 static int tegra12_clk_out_enable(struct clk *c)
5241 {
5242         u32 val;
5243         unsigned long flags;
5244
5245         pr_debug("%s on clock %s\n", __func__, c->name);
5246
5247         spin_lock_irqsave(&clk_out_lock, flags);
5248         val = pmc_readl(c->reg);
5249         val |= (0x1 << c->u.periph.clk_num);
5250         pmc_writel(val, c->reg);
5251         pmc_readl(c->reg);
5252         spin_unlock_irqrestore(&clk_out_lock, flags);
5253
5254         return 0;
5255 }
5256
5257 static void tegra12_clk_out_disable(struct clk *c)
5258 {
5259         u32 val;
5260         unsigned long flags;
5261
5262         pr_debug("%s on clock %s\n", __func__, c->name);
5263
5264         spin_lock_irqsave(&clk_out_lock, flags);
5265         val = pmc_readl(c->reg);
5266         val &= ~(0x1 << c->u.periph.clk_num);
5267         pmc_writel(val, c->reg);
5268         pmc_readl(c->reg);
5269         spin_unlock_irqrestore(&clk_out_lock, flags);
5270 }
5271
5272 static int tegra12_clk_out_set_parent(struct clk *c, struct clk *p)
5273 {
5274         u32 val;
5275         unsigned long flags;
5276         const struct clk_mux_sel *sel;
5277
5278         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
5279
5280         for (sel = c->inputs; sel->input != NULL; sel++) {
5281                 if (sel->input == p) {
5282                         if (c->refcnt)
5283                                 clk_enable(p);
5284
5285                         spin_lock_irqsave(&clk_out_lock, flags);
5286                         val = pmc_readl(c->reg);
5287                         val &= ~periph_clk_source_mask(c);
5288                         val |= (sel->value << periph_clk_source_shift(c));
5289                         pmc_writel(val, c->reg);
5290                         pmc_readl(c->reg);
5291                         spin_unlock_irqrestore(&clk_out_lock, flags);
5292
5293                         if (c->refcnt && c->parent)
5294                                 clk_disable(c->parent);
5295
5296                         clk_reparent(c, p);
5297                         return 0;
5298                 }
5299         }
5300         return -EINVAL;
5301 }
5302
5303 static struct clk_ops tegra_clk_out_ops = {
5304         .init                   = &tegra12_clk_out_init,
5305         .enable                 = &tegra12_clk_out_enable,
5306         .disable                = &tegra12_clk_out_disable,
5307         .set_parent             = &tegra12_clk_out_set_parent,
5308 };
5309
5310
5311 /* External memory controller clock ops */
5312 static void tegra12_emc_clk_init(struct clk *c)
5313 {
5314         tegra12_periph_clk_init(c);
5315         tegra_emc_dram_type_init(c);
5316 }
5317
5318 static long tegra12_emc_clk_round_updown(struct clk *c, unsigned long rate,
5319                                          bool up)
5320 {
5321         unsigned long new_rate = max(rate, c->min_rate);
5322
5323         new_rate = tegra_emc_round_rate_updown(new_rate, up);
5324         if (IS_ERR_VALUE(new_rate))
5325                 new_rate = c->max_rate;
5326
5327         return new_rate;
5328 }
5329
5330 static long tegra12_emc_clk_round_rate(struct clk *c, unsigned long rate)
5331 {
5332         return tegra12_emc_clk_round_updown(c, rate, true);
5333 }
5334
5335 static int tegra12_emc_clk_set_rate(struct clk *c, unsigned long rate)
5336 {
5337         int ret;
5338         u32 div_value;
5339         struct clk *p;
5340
5341         if (tegra_platform_is_qt())
5342                 return 0;
5343         /* The tegra12x memory controller has an interlock with the clock
5344          * block that allows memory shadowed registers to be updated,
5345          * and then transfer them to the main registers at the same
5346          * time as the clock update without glitches. During clock change
5347          * operation both clock parent and divider may change simultaneously
5348          * to achieve requested rate. */
5349         p = tegra_emc_predict_parent(rate, &div_value);
5350         div_value += 2;         /* emc has fractional DIV_U71 divider */
5351         if (IS_ERR_OR_NULL(p)) {
5352                 pr_err("%s: Failed to predict emc parent for rate %lu\n",
5353                        __func__, rate);
5354                 return -EINVAL;
5355         }
5356
5357         if (p == c->parent) {
5358                 if (div_value == c->div)
5359                         return 0;
5360         } else if (c->refcnt)
5361                 clk_enable(p);
5362
5363         ret = tegra_emc_set_rate(rate);
5364         if (ret < 0)
5365                 return ret;
5366
5367         if (p != c->parent) {
5368                 if(c->refcnt && c->parent)
5369                         clk_disable(c->parent);
5370                 clk_reparent(c, p);
5371         }
5372         c->div = div_value;
5373         c->mul = 2;
5374         return 0;
5375 }
5376
5377 static int tegra12_clk_emc_bus_update(struct clk *bus)
5378 {
5379         struct clk *p = NULL;
5380         unsigned long rate, old_rate, parent_rate, backup_rate;
5381
5382         if (detach_shared_bus)
5383                 return 0;
5384
5385         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
5386
5387         old_rate = clk_get_rate_locked(bus);
5388         if (rate == old_rate)
5389                 return 0;
5390
5391         if (!tegra_emc_is_parent_ready(rate, &p, &parent_rate, &backup_rate)) {
5392                 if (bus->parent == p) {
5393                         /* need backup to re-lock current parent */
5394                         int ret;
5395                         if (IS_ERR_VALUE(backup_rate)) {
5396                                 pr_err("%s: No backup for %s rate %lu\n",
5397                                        __func__, bus->name, rate);
5398                                 return -EINVAL;
5399                         }
5400
5401                         /* set volatge for backup rate if going up */
5402                         if (backup_rate > old_rate) {
5403                                 ret = tegra_dvfs_set_rate(bus, backup_rate);
5404                                 if (ret) {
5405                                         pr_err("%s: dvfs failed on %s rate %lu\n",
5406                                               __func__, bus->name, backup_rate);
5407                                         return -EINVAL;
5408                                 }
5409                         }
5410
5411                         trace_clock_set_rate(bus->name, backup_rate, 0);
5412                         ret = bus->ops->set_rate(bus, backup_rate);
5413                         if (ret) {
5414                                 pr_err("%s: Failed to backup %s for rate %lu\n",
5415                                        __func__, bus->name, rate);
5416                                 return -EINVAL;
5417                         }
5418                         clk_rate_change_notify(bus, backup_rate);
5419                 }
5420                 if (p->refcnt) {
5421                         pr_err("%s: %s has other than emc child\n",
5422                                __func__, p->name);
5423                         return -EINVAL;
5424                 }
5425
5426                 if (clk_set_rate(p, parent_rate)) {
5427                         pr_err("%s: Failed to set %s rate %lu\n",
5428                                __func__, p->name, parent_rate);
5429                         return -EINVAL;
5430                 }
5431         }
5432
5433         return clk_set_rate_locked(bus, rate);
5434 }
5435
5436 static struct clk_ops tegra_emc_clk_ops = {
5437         .init                   = &tegra12_emc_clk_init,
5438         .enable                 = &tegra12_periph_clk_enable,
5439         .disable                = &tegra12_periph_clk_disable,
5440         .set_rate               = &tegra12_emc_clk_set_rate,
5441         .round_rate             = &tegra12_emc_clk_round_rate,
5442         .round_rate_updown      = &tegra12_emc_clk_round_updown,
5443         .reset                  = &tegra12_periph_clk_reset,
5444         .shared_bus_update      = &tegra12_clk_emc_bus_update,
5445 };
5446
5447 void tegra_mc_divider_update(struct clk *emc)
5448 {
5449         emc->child_bus->div = (clk_readl(emc->reg) &
5450                                PERIPH_CLK_SOURCE_EMC_MC_SAME) ? 1 : 2;
5451 }
5452
5453 static void tegra12_mc_clk_init(struct clk *c)
5454 {
5455         c->state = ON;
5456         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
5457                 c->state = OFF;
5458
5459         c->parent->child_bus = c;
5460         tegra_mc_divider_update(c->parent);
5461         c->mul = 1;
5462 }
5463
5464 static struct clk_ops tegra_mc_clk_ops = {
5465         .init                   = &tegra12_mc_clk_init,
5466         .enable                 = &tegra12_periph_clk_enable,
5467         .disable                = &tegra12_periph_clk_disable,
5468 };
5469
5470 /* Clock doubler ops (non-atomic shared register access) */
5471 static DEFINE_SPINLOCK(doubler_lock);
5472
5473 static void tegra12_clk_double_init(struct clk *c)
5474 {
5475         u32 val = clk_readl(c->reg);
5476         c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
5477         c->div = 1;
5478         c->state = ON;
5479         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
5480                 c->state = OFF;
5481 };
5482
5483 static int tegra12_clk_double_set_rate(struct clk *c, unsigned long rate)
5484 {
5485         u32 val;
5486         unsigned long parent_rate = clk_get_rate(c->parent);
5487         unsigned long flags;
5488
5489         if (rate == parent_rate) {
5490                 spin_lock_irqsave(&doubler_lock, flags);
5491                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
5492                 clk_writel(val, c->reg);
5493                 c->mul = 1;
5494                 c->div = 1;
5495                 spin_unlock_irqrestore(&doubler_lock, flags);
5496                 return 0;
5497         } else if (rate == 2 * parent_rate) {
5498                 spin_lock_irqsave(&doubler_lock, flags);
5499                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
5500                 clk_writel(val, c->reg);
5501                 c->mul = 2;
5502                 c->div = 1;
5503                 spin_unlock_irqrestore(&doubler_lock, flags);
5504                 return 0;
5505         }
5506         return -EINVAL;
5507 }
5508
5509 static struct clk_ops tegra_clk_double_ops = {
5510         .init                   = &tegra12_clk_double_init,
5511         .enable                 = &tegra12_periph_clk_enable,
5512         .disable                = &tegra12_periph_clk_disable,
5513         .set_rate               = &tegra12_clk_double_set_rate,
5514 };
5515
5516 /* Audio sync clock ops */
5517 static int tegra12_sync_source_set_rate(struct clk *c, unsigned long rate)
5518 {
5519         c->rate = rate;
5520         return 0;
5521 }
5522
5523 static struct clk_ops tegra_sync_source_ops = {
5524         .set_rate               = &tegra12_sync_source_set_rate,
5525 };
5526
5527 static void tegra12_audio_sync_clk_init(struct clk *c)
5528 {
5529         int source;
5530         const struct clk_mux_sel *sel;
5531         u32 val = clk_readl(c->reg);
5532         c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
5533         source = val & AUDIO_SYNC_SOURCE_MASK;
5534         for (sel = c->inputs; sel->input != NULL; sel++)
5535                 if (sel->value == source)
5536                         break;
5537         BUG_ON(sel->input == NULL);
5538         c->parent = sel->input;
5539 }
5540
5541 static int tegra12_audio_sync_clk_enable(struct clk *c)
5542 {
5543         u32 val = clk_readl(c->reg);
5544         clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
5545         return 0;
5546 }
5547
5548 static void tegra12_audio_sync_clk_disable(struct clk *c)
5549 {
5550         u32 val = clk_readl(c->reg);
5551         clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
5552 }
5553
5554 static int tegra12_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
5555 {
5556         u32 val;
5557         const struct clk_mux_sel *sel;
5558         for (sel = c->inputs; sel->input != NULL; sel++) {
5559                 if (sel->input == p) {
5560                         val = clk_readl(c->reg);
5561                         val &= ~AUDIO_SYNC_SOURCE_MASK;
5562                         val |= sel->value;
5563
5564                         if (c->refcnt)
5565                                 clk_enable(p);
5566
5567                         clk_writel(val, c->reg);
5568
5569                         if (c->refcnt && c->parent)
5570                                 clk_disable(c->parent);
5571
5572                         clk_reparent(c, p);
5573                         return 0;
5574                 }
5575         }
5576
5577         return -EINVAL;
5578 }
5579
5580 static struct clk_ops tegra_audio_sync_clk_ops = {
5581         .init       = tegra12_audio_sync_clk_init,
5582         .enable     = tegra12_audio_sync_clk_enable,
5583         .disable    = tegra12_audio_sync_clk_disable,
5584         .set_parent = tegra12_audio_sync_clk_set_parent,
5585 };
5586
5587 /* cml0 (pcie), and cml1 (sata) clock ops */
5588 static void tegra12_cml_clk_init(struct clk *c)
5589 {
5590         u32 val = clk_readl(c->reg);
5591         c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
5592 }
5593
5594 static int tegra12_cml_clk_enable(struct clk *c)
5595 {
5596         u32 val = clk_readl(c->reg);
5597         val |= (0x1 << c->u.periph.clk_num);
5598         clk_writel(val, c->reg);
5599         return 0;
5600 }
5601
5602 static void tegra12_cml_clk_disable(struct clk *c)
5603 {
5604         u32 val = clk_readl(c->reg);
5605         val &= ~(0x1 << c->u.periph.clk_num);
5606         clk_writel(val, c->reg);
5607 }
5608
5609 static struct clk_ops tegra_cml_clk_ops = {
5610         .init                   = &tegra12_cml_clk_init,
5611         .enable                 = &tegra12_cml_clk_enable,
5612         .disable                = &tegra12_cml_clk_disable,
5613 };
5614
5615
5616 /* cbus ops */
5617 /*
5618  * Some clocks require dynamic re-locking of source PLL in order to
5619  * achieve frequency scaling granularity that matches characterized
5620  * core voltage steps. The cbus clock creates a shared bus that
5621  * provides a virtual root for such clocks to hide and synchronize
5622  * parent PLL re-locking as well as backup operations.
5623 */
5624
5625 static void tegra12_clk_cbus_init(struct clk *c)
5626 {
5627         c->state = OFF;
5628         c->set = true;
5629 }
5630
5631 static int tegra12_clk_cbus_enable(struct clk *c)
5632 {
5633         return 0;
5634 }
5635
5636 static long tegra12_clk_cbus_round_updown(struct clk *c, unsigned long rate,
5637                                           bool up)
5638 {
5639         int i;
5640         const int *millivolts;
5641
5642         if (!c->dvfs) {
5643                 if (!c->min_rate)
5644                         c->min_rate = c->parent->min_rate;
5645                 rate = max(rate, c->min_rate);
5646                 return rate;
5647         }
5648
5649         /* update min now, since no dvfs table was available during init
5650            (skip placeholder entries set to 1 kHz) */
5651         if (!c->min_rate) {
5652                 for (i = 0; i < c->dvfs->num_freqs; i++) {
5653                         if (c->dvfs->freqs[i] > 1 * c->dvfs->freqs_mult) {
5654                                 c->min_rate = c->dvfs->freqs[i];
5655                                 break;
5656                         }
5657                 }
5658                 BUG_ON(!c->min_rate);
5659         }
5660         rate = max(rate, c->min_rate);
5661
5662         millivolts = tegra_dvfs_get_millivolts_pll(c->dvfs);
5663         for (i = 0; ; i++) {
5664                 unsigned long f = c->dvfs->freqs[i];
5665                 int mv = millivolts[i];
5666                 if ((f >= rate) || (mv >= c->dvfs->max_millivolts) ||
5667                     ((i + 1) >=  c->dvfs->num_freqs)) {
5668                         if (!up && i && (f > rate))
5669                                 i--;
5670                         break;
5671                 }
5672         }
5673         return c->dvfs->freqs[i];
5674 }
5675
5676 static long tegra12_clk_cbus_round_rate(struct clk *c, unsigned long rate)
5677 {
5678         return tegra12_clk_cbus_round_updown(c, rate, true);
5679 }
5680
5681 static int cbus_switch_one(struct clk *c, struct clk *p, u32 div, bool abort)
5682 {
5683         int ret = 0;
5684
5685         /* set new divider if it is bigger than the current one */
5686         if (c->div < c->mul * div) {
5687                 ret = clk_set_div(c, div);
5688                 if (ret) {
5689                         pr_err("%s: failed to set %s clock divider %u: %d\n",
5690                                __func__, c->name, div, ret);
5691                         if (abort)
5692                                 return ret;
5693                 }
5694         }
5695
5696         if (c->parent != p) {
5697                 ret = clk_set_parent(c, p);
5698                 if (ret) {
5699                         pr_err("%s: failed to set %s clock parent %s: %d\n",
5700                                __func__, c->name, p->name, ret);
5701                         if (abort)
5702                                 return ret;
5703                 }
5704         }
5705
5706         /* set new divider if it is smaller than the current one */
5707         if (c->div > c->mul * div) {
5708                 ret = clk_set_div(c, div);
5709                 if (ret)
5710                         pr_err("%s: failed to set %s clock divider %u: %d\n",
5711                                __func__, c->name, div, ret);
5712         }
5713
5714         return ret;
5715 }
5716
5717 static int cbus_backup(struct clk *c)
5718 {
5719         int ret;
5720         struct clk *user;
5721
5722         list_for_each_entry(user, &c->shared_bus_list,
5723                         u.shared_bus_user.node) {
5724                 struct clk *client = user->u.shared_bus_user.client;
5725                 if (client && (client->state == ON) &&
5726                     (client->parent == c->parent)) {
5727                         ret = cbus_switch_one(client,
5728                                               c->shared_bus_backup.input,
5729                                               c->shared_bus_backup.value *
5730                                               user->div, true);
5731                         if (ret)
5732                                 return ret;
5733                 }
5734         }
5735         return 0;
5736 }
5737
5738 static int cbus_dvfs_set_rate(struct clk *c, unsigned long rate)
5739 {
5740         int ret;
5741         struct clk *user;
5742
5743         list_for_each_entry(user, &c->shared_bus_list,
5744                         u.shared_bus_user.node) {
5745                 struct clk *client =  user->u.shared_bus_user.client;
5746                 if (client && client->refcnt && (client->parent == c->parent)) {
5747                         ret = tegra_dvfs_set_rate(c, rate);
5748                         if (ret)
5749                                 return ret;
5750                 }
5751         }
5752         return 0;
5753 }
5754
5755 static void cbus_restore(struct clk *c)
5756 {
5757         struct clk *user;
5758
5759         list_for_each_entry(user, &c->shared_bus_list,
5760                         u.shared_bus_user.node) {
5761                 if (user->u.shared_bus_user.client)
5762                         cbus_switch_one(user->u.shared_bus_user.client,
5763                                         c->parent, c->div * user->div, false);
5764         }
5765 }
5766
5767 static int get_next_backup_div(struct clk *c, unsigned long rate)
5768 {
5769         u32 div = c->div;
5770         unsigned long backup_rate = clk_get_rate(c->shared_bus_backup.input);
5771
5772         rate = max(rate, clk_get_rate_locked(c));
5773         rate = rate - (rate >> 2);      /* 25% margin for backup rate */
5774         if ((u64)rate * div < backup_rate)
5775                 div = DIV_ROUND_UP(backup_rate, rate);
5776
5777         BUG_ON(!div);
5778         return div;
5779 }
5780
5781 static int tegra12_clk_cbus_set_rate(struct clk *c, unsigned long rate)
5782 {
5783         int ret;
5784         bool dramp;
5785
5786         if (rate == 0)
5787                 return 0;
5788         if (tegra_platform_is_qt())
5789                 return 0;
5790         ret = clk_enable(c->parent);
5791         if (ret) {
5792                 pr_err("%s: failed to enable %s clock: %d\n",
5793                        __func__, c->name, ret);
5794                 return ret;
5795         }
5796
5797         dramp = tegra12_is_dyn_ramp(c->parent, rate * c->div, false);
5798         if (!dramp) {
5799                 c->shared_bus_backup.value = get_next_backup_div(c, rate);
5800                 ret = cbus_backup(c);
5801                 if (ret)
5802                         goto out;
5803         }
5804
5805         ret = clk_set_rate(c->parent, rate * c->div);
5806         if (ret) {
5807                 pr_err("%s: failed to set %s clock rate %lu: %d\n",
5808                        __func__, c->name, rate, ret);
5809                 goto out;
5810         }
5811
5812         /* Safe voltage setting is taken care of by cbus clock dvfs; the call
5813          * below only records requirements for each enabled client.
5814          */
5815         if (dramp)
5816                 ret = cbus_dvfs_set_rate(c, rate);
5817
5818         cbus_restore(c);
5819
5820 out:
5821         clk_disable(c->parent);
5822         return ret;
5823 }
5824
5825 static inline void cbus_move_enabled_user(
5826         struct clk *user, struct clk *dst, struct clk *src)
5827 {
5828         clk_enable(dst);
5829         list_move_tail(&user->u.shared_bus_user.node, &dst->shared_bus_list);
5830         clk_disable(src);
5831         clk_reparent(user, dst);
5832 }
5833
5834 #ifdef CONFIG_TEGRA_DYNAMIC_CBUS
5835 static int tegra12_clk_cbus_update(struct clk *bus)
5836 {
5837         int ret, mv;
5838         struct clk *slow = NULL;
5839         struct clk *top = NULL;
5840         unsigned long rate;
5841         unsigned long old_rate;
5842         unsigned long ceiling;
5843
5844         if (detach_shared_bus)
5845                 return 0;
5846
5847         rate = tegra12_clk_shared_bus_update(bus, &top, &slow, &ceiling);
5848
5849         /* use dvfs table of the slowest enabled client as cbus dvfs table */
5850         if (bus->dvfs && slow && (slow != bus->u.cbus.slow_user)) {
5851                 int i;
5852                 unsigned long *dest = &bus->dvfs->freqs[0];
5853                 unsigned long *src =
5854                         &slow->u.shared_bus_user.client->dvfs->freqs[0];
5855                 if (slow->div > 1)
5856                         for (i = 0; i < bus->dvfs->num_freqs; i++)
5857                                 dest[i] = src[i] * slow->div;
5858                 else
5859                         memcpy(dest, src, sizeof(*dest) * bus->dvfs->num_freqs);
5860         }
5861
5862         /* update bus state variables and rate */
5863         bus->u.cbus.slow_user = slow;
5864         bus->u.cbus.top_user = top;
5865
5866         rate = tegra12_clk_cap_shared_bus(bus, rate, ceiling);
5867         mv = tegra_dvfs_predict_millivolts(bus, rate);
5868         if (IS_ERR_VALUE(mv))
5869                 return -EINVAL;
5870
5871         if (bus->dvfs) {
5872                 mv -= bus->dvfs->cur_millivolts;
5873                 if (bus->refcnt && (mv > 0)) {
5874                         ret = tegra_dvfs_set_rate(bus, rate);
5875                         if (ret)
5876                                 return ret;
5877                 }
5878         }
5879
5880         old_rate = clk_get_rate_locked(bus);
5881         if (IS_ENABLED(CONFIG_TEGRA_MIGRATE_CBUS_USERS) || (old_rate != rate)) {
5882                 ret = bus->ops->set_rate(bus, rate);
5883                 if (ret)
5884                         return ret;
5885         }
5886
5887         if (bus->dvfs) {
5888                 if (bus->refcnt && (mv <= 0)) {
5889                         ret = tegra_dvfs_set_rate(bus, rate);
5890                         if (ret)
5891                                 return ret;
5892                 }
5893         }
5894
5895         clk_rate_change_notify(bus, rate);
5896         return 0;
5897 };
5898 #else
5899 static int tegra12_clk_cbus_update(struct clk *bus)
5900 {
5901         unsigned long rate, old_rate;
5902
5903         if (detach_shared_bus)
5904                 return 0;
5905
5906         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
5907
5908         old_rate = clk_get_rate_locked(bus);
5909         if (rate == old_rate)
5910                 return 0;
5911
5912         return clk_set_rate_locked(bus, rate);
5913 }
5914 #endif
5915
5916 static int tegra12_clk_cbus_migrate_users(struct clk *user)
5917 {
5918 #ifdef CONFIG_TEGRA_MIGRATE_CBUS_USERS
5919         struct clk *src_bus, *dst_bus, *top_user, *c;
5920         struct list_head *pos, *n;
5921
5922         if (!user->u.shared_bus_user.client || !user->inputs)
5923                 return 0;
5924
5925         /* Dual cbus on Tegra12 */
5926         src_bus = user->inputs[0].input;
5927         dst_bus = user->inputs[1].input;
5928
5929         if (!src_bus->u.cbus.top_user && !dst_bus->u.cbus.top_user)
5930                 return 0;
5931
5932         /* Make sure top user on the source bus is requesting highest rate */
5933         if (!src_bus->u.cbus.top_user || (dst_bus->u.cbus.top_user &&
5934                 bus_user_request_is_lower(src_bus->u.cbus.top_user,
5935                                            dst_bus->u.cbus.top_user)))
5936                 swap(src_bus, dst_bus);
5937
5938         /* If top user is the slow one on its own (source) bus, do nothing */
5939         top_user = src_bus->u.cbus.top_user;
5940         BUG_ON(!top_user->u.shared_bus_user.client);
5941         if (!bus_user_is_slower(src_bus->u.cbus.slow_user, top_user))
5942                 return 0;
5943
5944         /* If source bus top user is slower than all users on destination bus,
5945            move top user; otherwise move all users slower than the top one */
5946         if (!dst_bus->u.cbus.slow_user ||
5947             !bus_user_is_slower(dst_bus->u.cbus.slow_user, top_user)) {
5948                 cbus_move_enabled_user(top_user, dst_bus, src_bus);
5949         } else {
5950                 list_for_each_safe(pos, n, &src_bus->shared_bus_list) {
5951                         c = list_entry(pos, struct clk, u.shared_bus_user.node);
5952                         if (c->u.shared_bus_user.enabled &&
5953                             c->u.shared_bus_user.client &&
5954                             bus_user_is_slower(c, top_user))
5955                                 cbus_move_enabled_user(c, dst_bus, src_bus);
5956                 }
5957         }
5958
5959         /* Update destination bus 1st (move clients), then source */
5960         tegra_clk_shared_bus_update(dst_bus);
5961         tegra_clk_shared_bus_update(src_bus);
5962 #endif
5963         return 0;
5964 }
5965
5966 static struct clk_ops tegra_clk_cbus_ops = {
5967         .init = tegra12_clk_cbus_init,
5968         .enable = tegra12_clk_cbus_enable,
5969         .set_rate = tegra12_clk_cbus_set_rate,
5970         .round_rate = tegra12_clk_cbus_round_rate,
5971         .round_rate_updown = tegra12_clk_cbus_round_updown,
5972         .shared_bus_update = tegra12_clk_cbus_update,
5973 };
5974
5975 /* shared bus ops */
5976 /*
5977  * Some clocks may have multiple downstream users that need to request a
5978  * higher clock rate.  Shared bus clocks provide a unique shared_bus_user
5979  * clock to each user.  The frequency of the bus is set to the highest
5980  * enabled shared_bus_user clock, with a minimum value set by the
5981  * shared bus.
5982  *
5983  * Optionally shared bus may support users migration. Since shared bus and
5984  * its * children (users) have reversed rate relations: user rates determine
5985  * bus rate, * switching user from one parent/bus to another may change rates
5986  * of both parents. Therefore we need a cross-bus lock on top of individual
5987  * user and bus locks. For now, limit bus switch support to cbus only if
5988  * CONFIG_TEGRA_MIGRATE_CBUS_USERS is set.
5989  */
5990
5991 static unsigned long tegra12_clk_shared_bus_update(struct clk *bus,
5992         struct clk **bus_top, struct clk **bus_slow, unsigned long *rate_cap)
5993 {
5994         struct clk *c;
5995         struct clk *slow = NULL;
5996         struct clk *top = NULL;
5997
5998         unsigned long override_rate = 0;
5999         unsigned long top_rate = 0;
6000         unsigned long rate = bus->min_rate;
6001         unsigned long bw = 0;
6002         unsigned long iso_bw = 0;
6003         unsigned long ceiling = bus->max_rate;
6004         unsigned long ceiling_but_iso = bus->max_rate;
6005         u32 usage_flags = 0;
6006         bool rate_set = false;
6007
6008         list_for_each_entry(c, &bus->shared_bus_list,
6009                         u.shared_bus_user.node) {
6010                 bool cap_user = (c->u.shared_bus_user.mode == SHARED_CEILING) ||
6011                         (c->u.shared_bus_user.mode == SHARED_CEILING_BUT_ISO);
6012                 /*
6013                  * Ignore requests from disabled floor and bw users, and from
6014                  * auto-users riding the bus. Always honor ceiling users, even
6015                  * if they are disabled - we do not want to keep enabled parent
6016                  * bus just because ceiling is set. Ignore SCLK/AHB/APB dividers
6017                  * to propagate flat max request.
6018                  */
6019                 if (c->u.shared_bus_user.enabled || cap_user) {
6020                         unsigned long request_rate = c->u.shared_bus_user.rate;
6021                         if (!(c->flags & DIV_BUS))
6022                                 request_rate *= c->div ? : 1;
6023                         usage_flags |= c->u.shared_bus_user.usage_flag;
6024
6025                         if (!(c->flags & BUS_RATE_LIMIT))
6026                                 rate_set = true;
6027
6028                         switch (c->u.shared_bus_user.mode) {
6029                         case SHARED_ISO_BW:
6030                                 iso_bw += request_rate;
6031                                 if (iso_bw > bus->max_rate)
6032                                         iso_bw = bus->max_rate;
6033                                 /* fall thru */
6034                         case SHARED_BW:
6035                                 bw += request_rate;
6036                                 if (bw > bus->max_rate)
6037                                         bw = bus->max_rate;
6038                                 break;
6039                         case SHARED_CEILING_BUT_ISO:
6040                                 ceiling_but_iso =
6041                                         min(request_rate, ceiling_but_iso);
6042                                 break;
6043                         case SHARED_CEILING:
6044                                 ceiling = min(request_rate, ceiling);
6045                                 break;
6046                         case SHARED_OVERRIDE:
6047                                 if (override_rate == 0)
6048                                         override_rate = request_rate;
6049                                 break;
6050                         case SHARED_AUTO:
6051                                 break;
6052                         case SHARED_FLOOR:
6053                         default:
6054                                 rate = max(request_rate, rate);
6055                                 if (c->u.shared_bus_user.client
6056                                                         && request_rate) {
6057                                         if (top_rate < request_rate) {
6058                                                 top_rate = request_rate;
6059                                                 top = c;
6060                                         } else if ((top_rate == request_rate) &&
6061                                                 bus_user_is_slower(c, top)) {
6062                                                 top = c;
6063                                         }
6064                                 }
6065                         }
6066                         if (c->u.shared_bus_user.client &&
6067                                 (!slow || bus_user_is_slower(c, slow)))
6068                                 slow = c;
6069                 }
6070         }
6071
6072         if (bus->flags & PERIPH_EMC_ENB) {
6073                 unsigned long iso_bw_min;
6074                 bw = tegra_emc_apply_efficiency(
6075                         bw, iso_bw, bus->max_rate, usage_flags, &iso_bw_min);
6076                 if (bus->ops && bus->ops->round_rate)
6077                         iso_bw_min = bus->ops->round_rate(bus, iso_bw_min);
6078                 ceiling_but_iso = max(ceiling_but_iso, iso_bw_min);
6079         }
6080
6081         rate = override_rate ? : max(rate, bw);
6082         ceiling = min(ceiling, ceiling_but_iso);
6083         ceiling = override_rate ? bus->max_rate : ceiling;
6084         bus->override_rate = override_rate;
6085
6086         if (bus_top && bus_slow && rate_cap) {
6087                 /* If dynamic bus dvfs table, let the caller to complete
6088                    rounding and aggregation */
6089                 *bus_top = top;
6090                 *bus_slow = slow;
6091                 *rate_cap = ceiling;
6092         } else {
6093                 /*
6094                  * If satic bus dvfs table, complete rounding and aggregation.
6095                  * In case when no user requested bus rate, and bus retention
6096                  * is enabled, don't scale down - keep current rate.
6097                  */
6098                 if (!rate_set && (bus->shared_bus_flags & SHARED_BUS_RETENTION))
6099                         rate = clk_get_rate_locked(bus);
6100                 rate = tegra12_clk_cap_shared_bus(bus, rate, ceiling);
6101         }
6102
6103         return rate;
6104 };
6105
6106 static unsigned long tegra12_clk_cap_shared_bus(struct clk *bus,
6107         unsigned long rate, unsigned long ceiling)
6108 {
6109         if (bus->ops && bus->ops->round_rate_updown)
6110                 ceiling = bus->ops->round_rate_updown(bus, ceiling, false);
6111
6112         rate = min(rate, ceiling);
6113
6114         if (bus->ops && bus->ops->round_rate)
6115                 rate = bus->ops->round_rate(bus, rate);
6116
6117         return rate;
6118 }
6119
6120 static int tegra_clk_shared_bus_migrate_users(struct clk *user)
6121 {
6122         if (detach_shared_bus)
6123                 return 0;
6124
6125         /* Only cbus migration is supported */
6126         if (user->flags & PERIPH_ON_CBUS)
6127                 return tegra12_clk_cbus_migrate_users(user);
6128         return -ENOSYS;
6129 }
6130
6131 static void tegra_clk_shared_bus_user_init(struct clk *c)
6132 {
6133         c->max_rate = c->parent->max_rate;
6134         c->u.shared_bus_user.rate = c->parent->max_rate;
6135         c->state = OFF;
6136         c->set = true;
6137
6138         if ((c->u.shared_bus_user.mode == SHARED_CEILING) ||
6139             (c->u.shared_bus_user.mode == SHARED_CEILING_BUT_ISO)) {
6140                 c->state = ON;
6141                 c->refcnt++;
6142         }
6143
6144         if (c->u.shared_bus_user.client_id) {
6145                 struct clk *client =
6146                         tegra_get_clock_by_name(c->u.shared_bus_user.client_id);
6147                 if (!client) {
6148                         pr_err("%s: could not find clk %s\n", __func__,
6149                                c->u.shared_bus_user.client_id);
6150                         return;
6151                 }
6152
6153                 if ((client->state == ON) && !(client->flags & PERIPH_NO_ENB))
6154                         pr_info("%s: %s client %s left ON\n", __func__,
6155                                 c->parent->name, client->name);
6156
6157                 c->u.shared_bus_user.client = client;
6158                 c->u.shared_bus_user.client->flags |=
6159                         c->parent->flags & PERIPH_ON_CBUS;
6160                 c->flags |= c->parent->flags & PERIPH_ON_CBUS;
6161                 c->div = c->u.shared_bus_user.client_div ? : 1;
6162                 c->mul = 1;
6163         }
6164
6165         list_add_tail(&c->u.shared_bus_user.node,
6166                 &c->parent->shared_bus_list);
6167 }
6168
6169 static int tegra_clk_shared_bus_user_set_parent(struct clk *c, struct clk *p)
6170 {
6171         int ret;
6172         const struct clk_mux_sel *sel;
6173
6174         if (detach_shared_bus)
6175                 return 0;
6176
6177         if (c->parent == p)
6178                 return 0;
6179
6180         if (!(c->inputs && c->cross_clk_mutex && clk_cansleep(c)))
6181                 return -ENOSYS;
6182
6183         for (sel = c->inputs; sel->input != NULL; sel++) {
6184                 if (sel->input == p)
6185                         break;
6186         }
6187         if (!sel->input)
6188                 return -EINVAL;
6189
6190         if (c->refcnt)
6191                 clk_enable(p);
6192
6193         list_move_tail(&c->u.shared_bus_user.node, &p->shared_bus_list);
6194         ret = tegra_clk_shared_bus_update(p);
6195         if (ret) {
6196                 list_move_tail(&c->u.shared_bus_user.node,
6197                                &c->parent->shared_bus_list);
6198                 tegra_clk_shared_bus_update(c->parent);
6199                 clk_disable(p);
6200                 return ret;
6201         }
6202
6203         tegra_clk_shared_bus_update(c->parent);
6204
6205         if (c->refcnt)
6206                 clk_disable(c->parent);
6207
6208         clk_reparent(c, p);
6209
6210         return 0;
6211 }
6212
6213 static int tegra_clk_shared_bus_user_set_rate(struct clk *c, unsigned long rate)
6214 {
6215         int ret;
6216
6217         c->u.shared_bus_user.rate = rate;
6218         ret = tegra_clk_shared_bus_update(c->parent);
6219
6220         if (!ret && c->cross_clk_mutex && clk_cansleep(c))
6221                 tegra_clk_shared_bus_migrate_users(c);
6222
6223         return ret;
6224 }
6225
6226 static long tegra_clk_shared_bus_user_round_rate(
6227         struct clk *c, unsigned long rate)
6228 {
6229         /*
6230          * Defer rounding requests until aggregated. BW users must not be
6231          * rounded at all, others just clipped to bus range (some clients
6232          * may use round api to find limits). Ignore SCLK/AHB and AHB/APB
6233          * dividers to keep flat bus requests propagation.
6234          */
6235         if ((c->u.shared_bus_user.mode != SHARED_BW) &&
6236             (c->u.shared_bus_user.mode != SHARED_ISO_BW)) {
6237                 if (!(c->flags & DIV_BUS) && (c->div > 1))
6238                         rate *= c->div;
6239
6240                 if (rate > c->parent->max_rate)
6241                         rate = c->parent->max_rate;
6242                 else if (rate < c->parent->min_rate)
6243                         rate = c->parent->min_rate;
6244
6245                 if (!(c->flags & DIV_BUS) && (c->div > 1))
6246                         rate /= c->div;
6247         }
6248         return rate;
6249 }
6250
6251 static int tegra_clk_shared_bus_user_enable(struct clk *c)
6252 {
6253         int ret;
6254
6255         c->u.shared_bus_user.enabled = true;
6256         ret = tegra_clk_shared_bus_update(c->parent);
6257         if (!ret && c->u.shared_bus_user.client)
6258                 ret = clk_enable(c->u.shared_bus_user.client);
6259
6260         if (!ret && c->cross_clk_mutex && clk_cansleep(c))
6261                 tegra_clk_shared_bus_migrate_users(c);
6262
6263         return ret;
6264 }
6265
6266 static void tegra_clk_shared_bus_user_disable(struct clk *c)
6267 {
6268         if (c->u.shared_bus_user.client)
6269                 clk_disable(c->u.shared_bus_user.client);
6270         c->u.shared_bus_user.enabled = false;
6271         tegra_clk_shared_bus_update(c->parent);
6272
6273         if (c->cross_clk_mutex && clk_cansleep(c))
6274                 tegra_clk_shared_bus_migrate_users(c);
6275 }
6276
6277 static void tegra_clk_shared_bus_user_reset(struct clk *c, bool assert)
6278 {
6279         if (c->u.shared_bus_user.client) {
6280                 if (c->u.shared_bus_user.client->ops &&
6281                     c->u.shared_bus_user.client->ops->reset)
6282                         c->u.shared_bus_user.client->ops->reset(
6283                                 c->u.shared_bus_user.client, assert);
6284         }
6285 }
6286
6287 static struct clk_ops tegra_clk_shared_bus_user_ops = {
6288         .init = tegra_clk_shared_bus_user_init,
6289         .enable = tegra_clk_shared_bus_user_enable,
6290         .disable = tegra_clk_shared_bus_user_disable,
6291         .set_parent = tegra_clk_shared_bus_user_set_parent,
6292         .set_rate = tegra_clk_shared_bus_user_set_rate,
6293         .round_rate = tegra_clk_shared_bus_user_round_rate,
6294         .reset = tegra_clk_shared_bus_user_reset,
6295 };
6296
6297 /* shared bus connector ops (user/bus connector to cascade shared buses) */
6298 static int tegra12_clk_shared_connector_update(struct clk *bus)
6299 {
6300         unsigned long rate, old_rate;
6301
6302         if (detach_shared_bus)
6303                 return 0;
6304
6305         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
6306
6307         old_rate = clk_get_rate_locked(bus);
6308         if (rate == old_rate)
6309                 return 0;
6310
6311         return clk_set_rate_locked(bus, rate);
6312 }
6313
6314 static struct clk_ops tegra_clk_shared_connector_ops = {
6315         .init = tegra_clk_shared_bus_user_init,
6316         .enable = tegra_clk_shared_bus_user_enable,
6317         .disable = tegra_clk_shared_bus_user_disable,
6318         .set_parent = tegra_clk_shared_bus_user_set_parent,
6319         .set_rate = tegra_clk_shared_bus_user_set_rate,
6320         .round_rate = tegra_clk_shared_bus_user_round_rate,
6321         .reset = tegra_clk_shared_bus_user_reset,
6322         .shared_bus_update = tegra12_clk_shared_connector_update,
6323 };
6324
6325 /* coupled gate ops */
6326 /*
6327  * Some clocks may have common enable/disable control, but run at different
6328  * rates, and have different dvfs tables. Coupled gate clock synchronize
6329  * enable/disable operations for such clocks.
6330  */
6331
6332 static int tegra12_clk_coupled_gate_enable(struct clk *c)
6333 {
6334         int ret;
6335         const struct clk_mux_sel *sel;
6336
6337         BUG_ON(!c->inputs);
6338         pr_debug("%s on clock %s\n", __func__, c->name);
6339
6340         for (sel = c->inputs; sel->input != NULL; sel++) {
6341                 if (sel->input == c->parent)
6342                         continue;
6343
6344                 ret = clk_enable(sel->input);
6345                 if (ret) {
6346                         while (sel != c->inputs) {
6347                                 sel--;
6348                                 if (sel->input == c->parent)
6349                                         continue;
6350                                 clk_disable(sel->input);
6351                         }
6352                         return ret;
6353                 }
6354         }
6355
6356         return tegra12_periph_clk_enable(c);
6357 }
6358
6359 static void tegra12_clk_coupled_gate_disable(struct clk *c)
6360 {
6361         const struct clk_mux_sel *sel;
6362
6363         BUG_ON(!c->inputs);
6364         pr_debug("%s on clock %s\n", __func__, c->name);
6365
6366         tegra12_periph_clk_disable(c);
6367
6368         if (!c->refcnt) /* happens only on boot clean-up: don't propagate */
6369                 return;
6370
6371         for (sel = c->inputs; sel->input != NULL; sel++) {
6372                 if (sel->input == c->parent)
6373                         continue;
6374
6375                 if (sel->input->set)    /* enforce coupling after boot only */
6376                         clk_disable(sel->input);
6377         }
6378 }
6379
6380 static struct clk_ops tegra_clk_coupled_gate_ops = {
6381         .init                   = tegra12_periph_clk_init,
6382         .enable                 = tegra12_clk_coupled_gate_enable,
6383         .disable                = tegra12_clk_coupled_gate_disable,
6384         .reset                  = &tegra12_periph_clk_reset,
6385 };
6386
6387
6388 /*
6389  * AHB and APB shared bus operations
6390  * APB shared bus is a user of AHB shared bus
6391  * AHB shared bus is a user of SCLK complex shared bus
6392  * SCLK/AHB and AHB/APB dividers can be dynamically changed. When AHB and APB
6393  * users requests are propagated to SBUS target rate, current values of the
6394  * dividers are ignored, and flat maximum request is selected as SCLK bus final
6395  * target. Then the dividers will be re-evaluated, based on AHB and APB targets.
6396  * Both AHB and APB buses are always enabled.
6397  */
6398 static void tegra12_clk_ahb_apb_init(struct clk *c, struct clk *bus_clk)
6399 {
6400         tegra_clk_shared_bus_user_init(c);
6401         c->max_rate = bus_clk->max_rate;
6402         c->min_rate = bus_clk->min_rate;
6403         c->mul = bus_clk->mul;
6404         c->div = bus_clk->div;
6405
6406         c->u.shared_bus_user.rate = clk_get_rate(bus_clk);
6407         c->u.shared_bus_user.enabled = true;
6408         c->parent->child_bus = c;
6409 }
6410
6411 static void tegra12_clk_ahb_init(struct clk *c)
6412 {
6413         struct clk *bus_clk = c->parent->u.system.hclk;
6414         tegra12_clk_ahb_apb_init(c, bus_clk);
6415 }
6416
6417 static void tegra12_clk_apb_init(struct clk *c)
6418 {
6419         struct clk *bus_clk = c->parent->parent->u.system.pclk;
6420         tegra12_clk_ahb_apb_init(c, bus_clk);
6421 }
6422
6423 static int tegra12_clk_ahb_apb_update(struct clk *bus)
6424 {
6425         unsigned long rate;
6426
6427         if (detach_shared_bus)
6428                 return 0;
6429
6430         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
6431         return clk_set_rate_locked(bus, rate);
6432 }
6433
6434 static struct clk_ops tegra_clk_ahb_ops = {
6435         .init = tegra12_clk_ahb_init,
6436         .set_rate = tegra_clk_shared_bus_user_set_rate,
6437         .round_rate = tegra_clk_shared_bus_user_round_rate,
6438         .shared_bus_update = tegra12_clk_ahb_apb_update,
6439 };
6440
6441 static struct clk_ops tegra_clk_apb_ops = {
6442         .init = tegra12_clk_apb_init,
6443         .set_rate = tegra_clk_shared_bus_user_set_rate,
6444         .round_rate = tegra_clk_shared_bus_user_round_rate,
6445         .shared_bus_update = tegra12_clk_ahb_apb_update,
6446 };
6447
6448 /* Clock definitions */
6449 static struct clk tegra_clk_32k = {
6450         .name = "clk_32k",
6451         .rate = 32768,
6452         .ops  = NULL,
6453         .max_rate = 32768,
6454 };
6455
6456 static struct clk tegra_clk_m = {
6457         .name      = "clk_m",
6458         .flags     = ENABLE_ON_INIT,
6459         .ops       = &tegra_clk_m_ops,
6460         .max_rate  = 48000000,
6461 };
6462
6463 static struct clk tegra_clk_m_div2 = {
6464         .name      = "clk_m_div2",
6465         .ops       = &tegra_clk_m_div_ops,
6466         .parent    = &tegra_clk_m,
6467         .mul       = 1,
6468         .div       = 2,
6469         .state     = ON,
6470         .max_rate  = 24000000,
6471 };
6472
6473 static struct clk tegra_clk_m_div4 = {
6474         .name      = "clk_m_div4",
6475         .ops       = &tegra_clk_m_div_ops,
6476         .parent    = &tegra_clk_m,
6477         .mul       = 1,
6478         .div       = 4,
6479         .state     = ON,
6480         .max_rate  = 12000000,
6481 };
6482
6483 static struct clk tegra_pll_ref = {
6484         .name      = "pll_ref",
6485         .flags     = ENABLE_ON_INIT,
6486         .ops       = &tegra_pll_ref_ops,
6487         .parent    = &tegra_clk_m,
6488         .max_rate  = 26000000,
6489 };
6490
6491 static struct clk_pll_freq_table tegra_pll_c_freq_table[] = {
6492         { 12000000, 600000000, 100, 1, 2},
6493         { 13000000, 600000000,  92, 1, 2},      /* actual: 598.0 MHz */
6494         { 16800000, 600000000,  71, 1, 2},      /* actual: 596.4 MHz */
6495         { 19200000, 600000000,  62, 1, 2},      /* actual: 595.2 MHz */
6496         { 26000000, 600000000,  92, 2, 2},      /* actual: 598.0 MHz */
6497         { 0, 0, 0, 0, 0, 0 },
6498 };
6499
6500 static struct clk tegra_pll_c = {
6501         .name      = "pll_c",
6502         .ops       = &tegra_pllxc_ops,
6503         .reg       = 0x80,
6504         .parent    = &tegra_pll_ref,
6505         .max_rate  = 1400000000,
6506         .u.pll = {
6507                 .input_min = 12000000,
6508                 .input_max = 800000000,
6509                 .cf_min    = 12000000,
6510                 .cf_max    = 19200000,  /* s/w policy, h/w capability 50 MHz */
6511                 .vco_min   = 600000000,
6512                 .vco_max   = 1400000000,
6513                 .freq_table = tegra_pll_c_freq_table,
6514                 .lock_delay = 300,
6515                 .misc1 = 0x88 - 0x80,
6516                 .round_p_to_pdiv = pllxc_round_p_to_pdiv,
6517         },
6518 };
6519
6520 static struct clk tegra_pll_c_out1 = {
6521         .name      = "pll_c_out1",
6522         .ops       = &tegra_pll_div_ops,
6523 #ifdef CONFIG_TEGRA_DUAL_CBUS
6524         .flags     = DIV_U71,
6525 #else
6526         .flags     = DIV_U71 | PERIPH_ON_CBUS,
6527 #endif
6528         .parent    = &tegra_pll_c,
6529         .reg       = 0x84,
6530         .reg_shift = 0,
6531         .max_rate  = 700000000,
6532 };
6533
6534 static struct clk_pll_freq_table tegra_pll_cx_freq_table[] = {
6535         { 12000000, 600000000, 100, 1, 2},
6536         { 13000000, 600000000,  92, 1, 2},      /* actual: 598.0 MHz */
6537         { 16800000, 600000000,  71, 1, 2},      /* actual: 596.4 MHz */
6538         { 19200000, 600000000,  62, 1, 2},      /* actual: 595.2 MHz */
6539         { 26000000, 600000000,  92, 2, 2},      /* actual: 598.0 MHz */
6540         { 0, 0, 0, 0, 0, 0 },
6541 };
6542
6543 static struct clk tegra_pll_c2 = {
6544         .name      = "pll_c2",
6545         .ops       = &tegra_pllcx_ops,
6546         .flags     = PLL_ALT_MISC_REG,
6547         .reg       = 0x4e8,
6548         .parent    = &tegra_pll_ref,
6549         .max_rate  = 1200000000,
6550         .u.pll = {
6551                 .input_min = 12000000,
6552                 .input_max = 48000000,
6553                 .cf_min    = 12000000,
6554                 .cf_max    = 19200000,
6555                 .vco_min   = 650000000,
6556                 .vco_max   = 1300000000,
6557                 .freq_table = tegra_pll_cx_freq_table,
6558                 .lock_delay = 360,
6559                 .misc1 = 0x4f0 - 0x4e8,
6560                 .round_p_to_pdiv = pllcx_round_p_to_pdiv,
6561         },
6562 };
6563
6564 static struct clk tegra_pll_c3 = {
6565         .name      = "pll_c3",
6566         .ops       = &tegra_pllcx_ops,
6567         .flags     = PLL_ALT_MISC_REG,
6568         .reg       = 0x4fc,
6569         .parent    = &tegra_pll_ref,
6570         .max_rate  = 1200000000,
6571         .u.pll = {
6572                 .input_min = 12000000,
6573                 .input_max = 48000000,
6574                 .cf_min    = 12000000,
6575                 .cf_max    = 19200000,
6576                 .vco_min   = 650000000,
6577                 .vco_max   = 1300000000,
6578                 .freq_table = tegra_pll_cx_freq_table,
6579                 .lock_delay = 360,
6580                 .misc1 = 0x504 - 0x4fc,
6581                 .round_p_to_pdiv = pllcx_round_p_to_pdiv,
6582         },
6583 };
6584
6585 static struct clk_pll_freq_table tegra_pll_m_freq_table[] = {
6586         { 12000000, 800000000, 66, 1, 1},       /* actual: 792.0 MHz */
6587         { 13000000, 800000000, 61, 1, 1},       /* actual: 793.0 MHz */
6588         { 16800000, 800000000, 47, 1, 1},       /* actual: 789.6 MHz */
6589         { 19200000, 800000000, 41, 1, 1},       /* actual: 787.2 MHz */
6590         { 26000000, 800000000, 61, 2, 1},       /* actual: 793.0 MHz */
6591         { 0, 0, 0, 0, 0, 0 },
6592 };
6593
6594 static struct clk tegra_pll_m = {
6595         .name      = "pll_m",
6596         .flags     = PLLM,
6597         .ops       = &tegra_pllm_ops,
6598         .reg       = 0x90,
6599         .parent    = &tegra_pll_ref,
6600         .max_rate  = 1200000000,
6601         .u.pll = {
6602                 .input_min = 12000000,
6603                 .input_max = 500000000,
6604                 .cf_min    = 12000000,
6605                 .cf_max    = 19200000,  /* s/w policy, h/w capability 50 MHz */
6606                 .vco_min   = 500000000,
6607                 .vco_max   = 1200000000,
6608                 .freq_table = tegra_pll_m_freq_table,
6609                 .lock_delay = 300,
6610                 .misc1 = 0x98 - 0x90,
6611                 .round_p_to_pdiv = pllm_round_p_to_pdiv,
6612         },
6613 };
6614
6615 static struct clk tegra_pll_m_out1 = {
6616         .name      = "pll_m_out1",
6617         .ops       = &tegra_pll_div_ops,
6618         .flags     = DIV_U71 | DIV_U71_INT,
6619         .parent    = &tegra_pll_m,
6620         .reg       = 0x94,
6621         .reg_shift = 0,
6622         .max_rate  = 1200000000,
6623 };
6624
6625 static struct clk_pll_freq_table tegra_pll_p_freq_table[] = {
6626         { 12000000, 408000000, 816, 12, 2, 8},
6627         { 13000000, 408000000, 816, 13, 2, 8},
6628         { 16800000, 408000000, 680, 14, 2, 8},
6629         { 19200000, 408000000, 680, 16, 2, 8},
6630         { 26000000, 408000000, 816, 26, 2, 8},
6631         { 0, 0, 0, 0, 0, 0 },
6632 };
6633
6634 static struct clk tegra_pll_p = {
6635         .name      = "pll_p",
6636         .flags     = ENABLE_ON_INIT | PLL_FIXED | PLL_HAS_CPCON,
6637         .ops       = &tegra_pllp_ops,
6638         .reg       = 0xa0,
6639         .parent    = &tegra_pll_ref,
6640         .max_rate  = 432000000,
6641         .u.pll = {
6642                 .input_min = 2000000,
6643                 .input_max = 31000000,
6644                 .cf_min    = 1000000,
6645                 .cf_max    = 6000000,
6646                 .vco_min   = 200000000,
6647                 .vco_max   = 700000000,
6648                 .freq_table = tegra_pll_p_freq_table,
6649                 .lock_delay = 300,
6650         },
6651 };
6652
6653 static struct clk tegra_pll_p_out1 = {
6654         .name      = "pll_p_out1",
6655         .ops       = &tegra_pll_div_ops,
6656         .flags     = DIV_U71 | DIV_U71_FIXED,
6657         .parent    = &tegra_pll_p,
6658         .reg       = 0xa4,
6659         .reg_shift = 0,
6660         .max_rate  = 432000000,
6661 };
6662
6663 static struct clk tegra_pll_p_out2 = {
6664         .name      = "pll_p_out2",
6665         .ops       = &tegra_pll_div_ops,
6666         .flags     = DIV_U71 | DIV_U71_FIXED | DIV_U71_INT,
6667         .parent    = &tegra_pll_p,
6668         .reg       = 0xa4,
6669         .reg_shift = 16,
6670         .max_rate  = 432000000,
6671 };
6672
6673 static struct clk tegra_pll_p_out3 = {
6674         .name      = "pll_p_out3",
6675         .ops       = &tegra_pll_div_ops,
6676         .flags     = DIV_U71 | DIV_U71_FIXED,
6677         .parent    = &tegra_pll_p,
6678         .reg       = 0xa8,
6679         .reg_shift = 0,
6680         .max_rate  = 432000000,
6681 };
6682
6683 static struct clk tegra_pll_p_out4 = {
6684         .name      = "pll_p_out4",
6685         .ops       = &tegra_pll_div_ops,
6686         .flags     = DIV_U71 | DIV_U71_FIXED,
6687         .parent    = &tegra_pll_p,
6688         .reg       = 0xa8,
6689         .reg_shift = 16,
6690         .max_rate  = 432000000,
6691 };
6692
6693 static struct clk tegra_pll_p_out5 = {
6694         .name      = "pll_p_out5",
6695         .ops       = &tegra_pll_div_ops,
6696         .flags     = DIV_U71 | DIV_U71_FIXED,
6697         .parent    = &tegra_pll_p,
6698         .reg       = 0x67c,
6699         .reg_shift = 16,
6700         .max_rate  = 432000000,
6701 };
6702
6703 static struct clk_pll_freq_table tegra_pll_a_freq_table[] = {
6704         {  9600000, 282240000, 147,  5, 1, 4},
6705         {  9600000, 368640000, 192,  5, 1, 4},
6706         {  9600000, 240000000, 200,  8, 1, 8},
6707
6708         { 28800000, 282240000, 245, 25, 1, 8},
6709         { 28800000, 368640000, 320, 25, 1, 8},
6710         { 28800000, 240000000, 200, 24, 1, 8},
6711         { 0, 0, 0, 0, 0, 0 },
6712 };
6713
6714 static struct clk tegra_pll_a = {
6715         .name      = "pll_a",
6716         .flags     = PLL_HAS_CPCON,
6717         .ops       = &tegra_pll_ops,
6718         .reg       = 0xb0,
6719         .parent    = &tegra_pll_p_out1,
6720         .max_rate  = 700000000,
6721         .u.pll = {
6722                 .input_min = 2000000,
6723                 .input_max = 31000000,
6724                 .cf_min    = 1000000,
6725                 .cf_max    = 6000000,
6726                 .vco_min   = 200000000,
6727                 .vco_max   = 700000000,
6728                 .freq_table = tegra_pll_a_freq_table,
6729                 .lock_delay = 300,
6730         },
6731 };
6732
6733 static struct clk tegra_pll_a_out0 = {
6734         .name      = "pll_a_out0",
6735         .ops       = &tegra_pll_div_ops,
6736         .flags     = DIV_U71,
6737         .parent    = &tegra_pll_a,
6738         .reg       = 0xb4,
6739         .reg_shift = 0,
6740         .max_rate  = 100000000,
6741 };
6742
6743 static struct clk_pll_freq_table tegra_pll_d_freq_table[] = {
6744         { 12000000, 216000000, 864, 12, 4, 12},
6745         { 13000000, 216000000, 864, 13, 4, 12},
6746         { 16800000, 216000000, 720, 14, 4, 12},
6747         { 19200000, 216000000, 720, 16, 4, 12},
6748         { 26000000, 216000000, 864, 26, 4, 12},
6749
6750         { 12000000, 594000000,  99,  2, 1,  8},
6751         { 13000000, 594000000, 594, 13, 1, 12},
6752         { 16800000, 594000000, 495, 14, 1, 12},
6753         { 19200000, 594000000, 495, 16, 1, 12},
6754         { 26000000, 594000000, 594, 26, 1, 12},
6755
6756         { 12000000, 1000000000, 1000, 12, 1, 12},
6757         { 13000000, 1000000000, 1000, 13, 1, 12},
6758         { 19200000, 1000000000, 625,  12, 1, 12},
6759         { 26000000, 1000000000, 1000, 26, 1, 12},
6760
6761         { 0, 0, 0, 0, 0, 0 },
6762 };
6763
6764 static struct clk tegra_pll_d = {
6765         .name      = "pll_d",
6766         .flags     = PLL_HAS_CPCON | PLLD,
6767         .ops       = &tegra_plld_ops,
6768         .reg       = 0xd0,
6769         .parent    = &tegra_pll_ref,
6770         .max_rate  = 1500000000,
6771         .u.pll = {
6772                 .input_min = 2000000,
6773                 .input_max = 40000000,
6774                 .cf_min    = 1000000,
6775                 .cf_max    = 6000000,
6776                 .vco_min   = 500000000,
6777                 .vco_max   = 1500000000,
6778                 .freq_table = tegra_pll_d_freq_table,
6779                 .lock_delay = 1000,
6780         },
6781 };
6782
6783 static struct clk tegra_pll_d_out0 = {
6784         .name      = "pll_d_out0",
6785         .ops       = &tegra_pll_div_ops,
6786         .flags     = DIV_2 | PLLD,
6787         .parent    = &tegra_pll_d,
6788         .max_rate  = 750000000,
6789 };
6790
6791 static struct clk_pll_freq_table tegra_pll_u_freq_table[] = {
6792         { 12000000, 480000000, 960, 12, 2, 12},
6793         { 13000000, 480000000, 960, 13, 2, 12},
6794         { 16800000, 480000000, 400, 7,  2, 5},
6795         { 19200000, 480000000, 200, 4,  2, 3},
6796         { 26000000, 480000000, 960, 26, 2, 12},
6797         { 0, 0, 0, 0, 0, 0 },
6798 };
6799
6800 static struct clk tegra_pll_u = {
6801         .name      = "pll_u",
6802         .flags     = PLL_HAS_CPCON | PLLU,
6803         .ops       = &tegra_pll_ops,
6804         .reg       = 0xc0,
6805         .parent    = &tegra_pll_ref,
6806         .max_rate  = 480000000,
6807         .u.pll = {
6808                 .input_min = 2000000,
6809                 .input_max = 40000000,
6810                 .cf_min    = 1000000,
6811                 .cf_max    = 6000000,
6812                 .vco_min   = 480000000,
6813                 .vco_max   = 960000000,
6814                 .freq_table = tegra_pll_u_freq_table,
6815                 .lock_delay = 1000,
6816         },
6817 };
6818
6819 static struct clk tegra_pll_u_480M = {
6820         .name      = "pll_u_480M",
6821         .flags     = PLLU,
6822         .ops       = &tegra_pll_div_ops,
6823         .reg       = 0xc0,
6824         .reg_shift = 22,
6825         .parent    = &tegra_pll_u,
6826         .mul       = 1,
6827         .div       = 1,
6828         .max_rate  = 480000000,
6829 };
6830
6831 static struct clk tegra_pll_u_60M = {
6832         .name      = "pll_u_60M",
6833         .flags     = PLLU,
6834         .ops       = &tegra_pll_div_ops,
6835         .reg       = 0xc0,
6836         .reg_shift = 23,
6837         .parent    = &tegra_pll_u,
6838         .mul       = 1,
6839         .div       = 8,
6840         .max_rate  = 60000000,
6841 };
6842
6843 static struct clk tegra_pll_u_48M = {
6844         .name      = "pll_u_48M",
6845         .flags     = PLLU,
6846         .ops       = &tegra_pll_div_ops,
6847         .reg       = 0xc0,
6848         .reg_shift = 25,
6849         .parent    = &tegra_pll_u,
6850         .mul       = 1,
6851         .div       = 10,
6852         .max_rate  = 48000000,
6853 };
6854
6855 static struct clk tegra_pll_u_12M = {
6856         .name      = "pll_u_12M",
6857         .flags     = PLLU,
6858         .ops       = &tegra_pll_div_ops,
6859         .reg       = 0xc0,
6860         .reg_shift = 21,
6861         .parent    = &tegra_pll_u,
6862         .mul       = 1,
6863         .div       = 40,
6864         .max_rate  = 12000000,
6865 };
6866
6867 static struct clk_pll_freq_table tegra_pll_x_freq_table[] = {
6868         /* 1 GHz */
6869         { 12000000, 1000000000, 83, 1, 1},      /* actual: 996.0 MHz */
6870         { 13000000, 1000000000, 76, 1, 1},      /* actual: 988.0 MHz */
6871         { 16800000, 1000000000, 59, 1, 1},      /* actual: 991.2 MHz */
6872         { 19200000, 1000000000, 52, 1, 1},      /* actual: 998.4 MHz */
6873         { 26000000, 1000000000, 76, 2, 1},      /* actual: 988.0 MHz */
6874
6875         { 0, 0, 0, 0, 0, 0 },
6876 };
6877
6878 static struct clk tegra_pll_x = {
6879         .name      = "pll_x",
6880         .flags     = PLL_ALT_MISC_REG | PLLX,
6881         .ops       = &tegra_pllxc_ops,
6882 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
6883         .reg       = 0xe0,
6884 #else
6885         .reg       = 0x0,
6886 #endif
6887         .parent    = &tegra_pll_ref,
6888         .max_rate  = 3000000000UL,
6889         .u.pll = {
6890                 .input_min = 12000000,
6891                 .input_max = 800000000,
6892                 .cf_min    = 12000000,
6893                 .cf_max    = 19200000,  /* s/w policy, h/w capability 50 MHz */
6894                 .vco_min   = 1200000000,
6895                 .vco_max   = 3000000000UL,
6896                 .freq_table = tegra_pll_x_freq_table,
6897                 .lock_delay = 300,
6898 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
6899                 .misc1 = 0x510 - 0xe0,
6900 #else
6901                 .misc1 = 0x8,
6902 #endif
6903                 .round_p_to_pdiv = pllxc_round_p_to_pdiv,
6904         },
6905 };
6906
6907 static struct clk tegra_pll_x_out0 = {
6908         .name      = "pll_x_out0",
6909         .ops       = &tegra_pll_div_ops,
6910         .flags     = DIV_2 | PLLX,
6911         .parent    = &tegra_pll_x,
6912         .max_rate  = 1500000000UL,
6913 };
6914
6915 static struct clk tegra_dfll_cpu = {
6916         .name      = "dfll_cpu",
6917         .flags     = DFLL,
6918         .ops       = &tegra_dfll_ops,
6919 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
6920         .reg       = 0x2f4,
6921 #else
6922         .reg       = 0x80,
6923 #endif
6924         .max_rate  = 3000000000UL,
6925 };
6926
6927 static struct clk_pll_freq_table tegra_pllc4_freq_table[] = {
6928         { 12000000, 600000000, 100, 1, 2},
6929         { 13000000, 600000000,  92, 1, 2},      /* actual: 598.0 MHz */
6930         { 16800000, 600000000,  71, 1, 2},      /* actual: 596.4 MHz */
6931         { 19200000, 600000000,  62, 1, 2},      /* actual: 595.2 MHz */
6932         { 26000000, 600000000,  92, 2, 2},      /* actual: 598.0 MHz */
6933         { 0, 0, 0, 0, 0, 0 },
6934 };
6935
6936 static struct clk tegra_pll_c4 = {
6937         .name      = "pll_c4",
6938         .flags     = PLL_ALT_MISC_REG,
6939         .ops       = &tegra_pllss_ops,
6940         .reg       = 0x5a4,
6941         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
6942         .max_rate  = 600000000,
6943         .u.pll = {
6944                 .input_min = 12000000,
6945                 .input_max = 1000000000,
6946                 .cf_min    = 12000000,
6947                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
6948                 .vco_min   = 600000000,
6949                 .vco_max   = 1200000000,
6950                 .freq_table = tegra_pllc4_freq_table,
6951                 .lock_delay = 300,
6952                 .misc1 = 0x8,
6953                 .round_p_to_pdiv = pllss_round_p_to_pdiv,
6954                 .cpcon_default = 0,
6955         },
6956 };
6957
6958 static struct clk_pll_freq_table tegra_plldp_freq_table[] = {
6959         { 12000000, 270000000,  90, 1, 4},
6960         { 13000000, 270000000,  83, 1, 4},      /* actual: 269.8 MHz */
6961         { 16800000, 270000000,  96, 1, 6},      /* actual: 268.8 MHz */
6962         { 19200000, 270000000,  84, 1, 6},      /* actual: 268.8 MHz */
6963         { 26000000, 270000000,  83, 2, 4},      /* actual: 269.8 MHz */
6964         { 0, 0, 0, 0, 0, 0 },
6965 };
6966
6967 static struct clk tegra_pll_dp = {
6968         .name      = "pll_dp",
6969         .flags     = PLL_ALT_MISC_REG,
6970         .ops       = &tegra_pllss_ops,
6971         .reg       = 0x590,
6972         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
6973         .max_rate  = 600000000,
6974         .u.pll = {
6975                 .input_min = 12000000,
6976                 .input_max = 1000000000,
6977                 .cf_min    = 12000000,
6978                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
6979                 .vco_min   = 600000000,
6980                 .vco_max   = 1200000000,
6981                 .freq_table = tegra_plldp_freq_table,
6982                 .lock_delay = 300,
6983                 .misc1 = 0x8,
6984                 .round_p_to_pdiv = pllss_round_p_to_pdiv,
6985                 .cpcon_default = 0,
6986         },
6987 };
6988
6989 static struct clk_pll_freq_table tegra_plld2_freq_table[] = {
6990         { 12000000, 594000000,  99, 1, 2,  3}, /*4kx2k @ 30Hz*/
6991         { 12000000, 297000000,  99, 1, 4,  1}, /*1080p @ 60Hz*/
6992         { 12000000, 148500000,  99, 1, 8,  1}, /* 720p @ 60Hz*/
6993         { 12000000,  54000000,  54, 1, 6,  1}, /* 480p @ 60Hz*/
6994         { 13000000, 594000000,  91, 1, 2},      /* actual: 591.5 MHz */
6995         { 16800000, 594000000,  71, 1, 2},      /* actual: 596.4 MHz */
6996         { 19200000, 594000000,  62, 1, 2},      /* actual: 595.2 MHz */
6997         { 26000000, 594000000,  91, 2, 2},      /* actual: 591.5 MHz */
6998         { 0, 0, 0, 0, 0, 0 },
6999 };
7000
7001 static struct clk tegra_pll_d2 = {
7002         .name      = "pll_d2",
7003         .flags     = PLL_ALT_MISC_REG,
7004         .ops       = &tegra_pllss_ops,
7005         .reg       = 0x4b8,
7006         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
7007         .max_rate  = 600000000,
7008         .u.pll = {
7009                 .input_min = 12000000,
7010                 .input_max = 1000000000,
7011                 .cf_min    = 12000000,
7012                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
7013                 .vco_min   = 600000000,
7014                 .vco_max   = 1200000000,
7015                 .freq_table = tegra_plld2_freq_table,
7016                 .lock_delay = 300,
7017                 .misc1 = 0x570 - 0x4b8,
7018                 .round_p_to_pdiv = pllss_round_p_to_pdiv,
7019                 .cpcon_default = 0,
7020         },
7021 };
7022
7023 static struct clk tegra_pll_re_vco = {
7024         .name      = "pll_re_vco",
7025         .flags     = PLL_ALT_MISC_REG,
7026         .ops       = &tegra_pllre_ops,
7027         .reg       = 0x4c4,
7028         .parent    = &tegra_pll_ref,
7029         .max_rate  = 672000000,
7030         .u.pll = {
7031                 .input_min = 12000000,
7032                 .input_max = 1000000000,
7033                 .cf_min    = 12000000,
7034                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
7035                 .vco_min   = 300000000,
7036                 .vco_max   = 672000000,
7037                 .lock_delay = 300,
7038                 .round_p_to_pdiv = pllre_round_p_to_pdiv,
7039         },
7040 };
7041
7042 static struct clk tegra_pll_re_out = {
7043         .name      = "pll_re_out",
7044         .ops       = &tegra_pllre_out_ops,
7045         .parent    = &tegra_pll_re_vco,
7046         .reg       = 0x4c4,
7047         .max_rate  = 672000000,
7048 };
7049
7050 static struct clk_pll_freq_table tegra_pll_e_freq_table[] = {
7051         /* PLLE special case: use cpcon field to store cml divider value */
7052         { 336000000, 100000000, 100, 21,  16, 11},
7053         { 312000000, 100000000, 200, 26,  24, 13},
7054         { 13000000,  100000000, 200, 1,  26, 13},
7055         { 12000000,  100000000, 200, 1,  24, 13},
7056         { 0, 0, 0, 0, 0, 0 },
7057 };
7058
7059 static struct clk tegra_pll_e = {
7060         .name      = "pll_e",
7061         .flags     = PLL_ALT_MISC_REG,
7062         .ops       = &tegra_plle_ops,
7063         .reg       = 0xe8,
7064         .max_rate  = 100000000,
7065         .u.pll = {
7066                 .input_min = 12000000,
7067                 .input_max = 1000000000,
7068                 .cf_min    = 12000000,
7069                 .cf_max    = 75000000,
7070                 .vco_min   = 1600000000,
7071                 .vco_max   = 2400000000U,
7072                 .freq_table = tegra_pll_e_freq_table,
7073                 .lock_delay = 300,
7074                 .fixed_rate = 100000000,
7075         },
7076 };
7077
7078
7079
7080 static struct clk tegra_cml0_clk = {
7081         .name      = "cml0",
7082         .parent    = &tegra_pll_e,
7083         .ops       = &tegra_cml_clk_ops,
7084         .reg       = PLLE_AUX,
7085         .max_rate  = 100000000,
7086         .u.periph  = {
7087                 .clk_num = 0,
7088         },
7089 };
7090
7091 static struct clk tegra_cml1_clk = {
7092         .name      = "cml1",
7093         .parent    = &tegra_pll_e,
7094         .ops       = &tegra_cml_clk_ops,
7095         .reg       = PLLE_AUX,
7096         .max_rate  = 100000000,
7097         .u.periph  = {
7098                 .clk_num   = 1,
7099         },
7100 };
7101
7102 static struct clk tegra_pciex_clk = {
7103         .name      = "pciex",
7104         .parent    = &tegra_pll_e,
7105         .ops       = &tegra_pciex_clk_ops,
7106         .max_rate  = 500000000,
7107         .u.periph  = {
7108                 .clk_num   = 74,
7109         },
7110 };
7111
7112 /* Audio sync clocks */
7113 #define SYNC_SOURCE(_id, _dev)                          \
7114         {                                               \
7115                 .name      = #_id "_sync",              \
7116                 .lookup    = {                          \
7117                         .dev_id    = #_dev ,            \
7118                         .con_id    = "ext_audio_sync",  \
7119                 },                                      \
7120                 .rate      = 24000000,                  \
7121                 .max_rate  = 24000000,                  \
7122                 .ops       = &tegra_sync_source_ops     \
7123         }
7124 static struct clk tegra_sync_source_list[] = {
7125         SYNC_SOURCE(spdif_in, tegra30-spdif),
7126         SYNC_SOURCE(i2s0, tegra30-i2s.0),
7127         SYNC_SOURCE(i2s1, tegra30-i2s.1),
7128         SYNC_SOURCE(i2s2, tegra30-i2s.2),
7129         SYNC_SOURCE(i2s3, tegra30-i2s.3),
7130         SYNC_SOURCE(i2s4, tegra30-i2s.4),
7131         SYNC_SOURCE(vimclk, vimclk),
7132 };
7133
7134 static struct clk_mux_sel mux_d_audio_clk[] = {
7135         { .input = &tegra_pll_a_out0,           .value = 0},
7136         { .input = &tegra_pll_p,                .value = 0x8000},
7137         { .input = &tegra_clk_m,                .value = 0xc000},
7138         { .input = &tegra_sync_source_list[0],  .value = 0xE000},
7139         { .input = &tegra_sync_source_list[1],  .value = 0xE001},
7140         { .input = &tegra_sync_source_list[2],  .value = 0xE002},
7141         { .input = &tegra_sync_source_list[3],  .value = 0xE003},
7142         { .input = &tegra_sync_source_list[4],  .value = 0xE004},
7143         { .input = &tegra_sync_source_list[5],  .value = 0xE005},
7144         { .input = &tegra_pll_a_out0,           .value = 0xE006},
7145         { .input = &tegra_sync_source_list[6],  .value = 0xE007},
7146         { 0, 0 }
7147 };
7148
7149 static struct clk_mux_sel mux_audio_sync_clk[] =
7150 {
7151         { .input = &tegra_sync_source_list[0],  .value = 0},
7152         { .input = &tegra_sync_source_list[1],  .value = 1},
7153         { .input = &tegra_sync_source_list[2],  .value = 2},
7154         { .input = &tegra_sync_source_list[3],  .value = 3},
7155         { .input = &tegra_sync_source_list[4],  .value = 4},
7156         { .input = &tegra_sync_source_list[5],  .value = 5},
7157         { .input = &tegra_pll_a_out0,           .value = 6},
7158         { .input = &tegra_sync_source_list[6],  .value = 7},
7159         { 0, 0 }
7160 };
7161
7162 #define AUDIO_SYNC_CLK(_id, _dev, _index)                       \
7163         {                                               \
7164                 .name      = #_id,                      \
7165                 .lookup    = {                          \
7166                         .dev_id    = #_dev,             \
7167                         .con_id    = "audio_sync",      \
7168                 },                                      \
7169                 .inputs    = mux_audio_sync_clk,        \
7170                 .reg       = 0x4A0 + (_index) * 4,      \
7171                 .max_rate  = 24000000,                  \
7172                 .ops       = &tegra_audio_sync_clk_ops  \
7173         }
7174 static struct clk tegra_clk_audio_list[] = {
7175         AUDIO_SYNC_CLK(audio0, tegra30-i2s.0, 0),
7176         AUDIO_SYNC_CLK(audio1, tegra30-i2s.1, 1),
7177         AUDIO_SYNC_CLK(audio2, tegra30-i2s.2, 2),
7178         AUDIO_SYNC_CLK(audio3, tegra30-i2s.3, 3),
7179         AUDIO_SYNC_CLK(audio4, tegra30-i2s.4, 4),
7180         AUDIO_SYNC_CLK(audio, tegra30-spdif, 5),        /* SPDIF */
7181 };
7182
7183 #define AUDIO_SYNC_2X_CLK(_id, _dev, _index)                            \
7184         {                                                       \
7185                 .name      = #_id "_2x",                        \
7186                 .lookup    = {                                  \
7187                         .dev_id    = #_dev,                     \
7188                         .con_id    = "audio_sync_2x"            \
7189                 },                                              \
7190                 .flags     = PERIPH_NO_RESET,                   \
7191                 .max_rate  = 48000000,                          \
7192                 .ops       = &tegra_clk_double_ops,             \
7193                 .reg       = 0x49C,                             \
7194                 .reg_shift = 24 + (_index),                     \
7195                 .parent    = &tegra_clk_audio_list[(_index)],   \
7196                 .u.periph = {                                   \
7197                         .clk_num = 113 + (_index),              \
7198                 },                                              \
7199         }
7200 static struct clk tegra_clk_audio_2x_list[] = {
7201         AUDIO_SYNC_2X_CLK(audio0, tegra30-i2s.0, 0),
7202         AUDIO_SYNC_2X_CLK(audio1, tegra30-i2s.1, 1),
7203         AUDIO_SYNC_2X_CLK(audio2, tegra30-i2s.2, 2),
7204         AUDIO_SYNC_2X_CLK(audio3, tegra30-i2s.3, 3),
7205         AUDIO_SYNC_2X_CLK(audio4, tegra30-i2s.4, 4),
7206         AUDIO_SYNC_2X_CLK(audio, tegra30-spdif, 5),     /* SPDIF */
7207 };
7208
7209 #define MUX_I2S_SPDIF(_id, _index)                                      \
7210 static struct clk_mux_sel mux_pllaout0_##_id##_2x_pllp_clkm[] = {       \
7211         {.input = &tegra_pll_a_out0, .value = 0},                       \
7212         {.input = &tegra_clk_audio_2x_list[(_index)], .value = 2},      \
7213         {.input = &tegra_pll_p, .value = 4},                            \
7214         {.input = &tegra_clk_m, .value = 6},                            \
7215         { 0, 0},                                                        \
7216 }
7217 MUX_I2S_SPDIF(audio0, 0);
7218 MUX_I2S_SPDIF(audio1, 1);
7219 MUX_I2S_SPDIF(audio2, 2);
7220 MUX_I2S_SPDIF(audio3, 3);
7221 MUX_I2S_SPDIF(audio4, 4);
7222 MUX_I2S_SPDIF(audio, 5);                /* SPDIF */
7223
7224 /* External clock outputs (through PMC) */
7225 #define MUX_EXTERN_OUT(_id)                                             \
7226 static struct clk_mux_sel mux_clkm_clkm2_clkm4_extern##_id[] = {        \
7227         {.input = &tegra_clk_m,         .value = 0},                    \
7228         {.input = &tegra_clk_m_div2,    .value = 1},                    \
7229         {.input = &tegra_clk_m_div4,    .value = 2},                    \
7230         {.input = NULL,                 .value = 3}, /* placeholder */  \
7231         { 0, 0},                                                        \
7232 }
7233 MUX_EXTERN_OUT(1);
7234 MUX_EXTERN_OUT(2);
7235 MUX_EXTERN_OUT(3);
7236
7237 static struct clk_mux_sel *mux_extern_out_list[] = {
7238         mux_clkm_clkm2_clkm4_extern1,
7239         mux_clkm_clkm2_clkm4_extern2,
7240         mux_clkm_clkm2_clkm4_extern3,
7241 };
7242
7243 #define CLK_OUT_CLK(_id, _max_rate)                                     \
7244         {                                                       \
7245                 .name      = "clk_out_" #_id,                   \
7246                 .lookup    = {                                  \
7247                         .dev_id    = "clk_out_" #_id,           \
7248                         .con_id    = "extern" #_id,             \
7249                 },                                              \
7250                 .ops       = &tegra_clk_out_ops,                \
7251                 .reg       = 0x1a8,                             \
7252                 .inputs    = mux_clkm_clkm2_clkm4_extern##_id,  \
7253                 .flags     = MUX_CLK_OUT,                       \
7254                 .max_rate  = _max_rate,                         \
7255                 .u.periph = {                                   \
7256                         .clk_num   = (_id - 1) * 8 + 2,         \
7257                 },                                              \
7258         }
7259 static struct clk tegra_clk_out_list[] = {
7260         CLK_OUT_CLK(1, 26000000),
7261         CLK_OUT_CLK(2, 40800000),
7262         CLK_OUT_CLK(3, 26000000),
7263 };
7264
7265 /* called after peripheral external clocks are initialized */
7266 static void init_clk_out_mux(void)
7267 {
7268         int i;
7269         struct clk *c;
7270
7271         /* output clock con_id is the name of peripheral
7272            external clock connected to input 3 of the output mux */
7273         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++) {
7274                 c = tegra_get_clock_by_name(
7275                         tegra_clk_out_list[i].lookup.con_id);
7276                 if (!c)
7277                         pr_err("%s: could not find clk %s\n", __func__,
7278                                tegra_clk_out_list[i].lookup.con_id);
7279                 mux_extern_out_list[i][3].input = c;
7280         }
7281 }
7282
7283 static struct clk_mux_sel mux_sclk[] = {
7284         { .input = &tegra_clk_m,        .value = 0},
7285         { .input = &tegra_pll_c_out1,   .value = 1},
7286         { .input = &tegra_pll_p_out4,   .value = 2},
7287         { .input = &tegra_pll_p,        .value = 3},
7288         { .input = &tegra_pll_p_out2,   .value = 4},
7289         { .input = &tegra_pll_c,        .value = 5},
7290         { .input = &tegra_clk_32k,      .value = 6},
7291         { .input = &tegra_pll_m_out1,   .value = 7},
7292         { 0, 0},
7293 };
7294
7295 static struct clk tegra_clk_sclk = {
7296         .name   = "sclk",
7297         .inputs = mux_sclk,
7298         .reg    = 0x28,
7299         .ops    = &tegra_super_ops,
7300         .max_rate = 420000000,
7301         .min_rate = 12000000,
7302 };
7303
7304 /* Peripheral muxes */
7305 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7306 static struct clk_mux_sel mux_cclk_g[] = {
7307         { .input = &tegra_clk_m,        .value = 0},
7308         { .input = &tegra_pll_c,        .value = 1},
7309         { .input = &tegra_clk_32k,      .value = 2},
7310         { .input = &tegra_pll_m,        .value = 3},
7311         { .input = &tegra_pll_p,        .value = 4},
7312         { .input = &tegra_pll_p_out4,   .value = 5},
7313         /* { .input = &tegra_pll_c2,    .value = 6}, - no use on tegra12x */
7314         /* { .input = &tegra_clk_c3,    .value = 7}, - no use on tegra12x */
7315         { .input = &tegra_pll_x,        .value = 8},
7316         { .input = &tegra_dfll_cpu,     .value = 15},
7317         { 0, 0},
7318 };
7319
7320 static struct clk_mux_sel mux_cclk_lp[] = {
7321         { .input = &tegra_clk_m,        .value = 0},
7322         { .input = &tegra_pll_c,        .value = 1},
7323         { .input = &tegra_clk_32k,      .value = 2},
7324         { .input = &tegra_pll_m,        .value = 3},
7325         { .input = &tegra_pll_p,        .value = 4},
7326         { .input = &tegra_pll_p_out4,   .value = 5},
7327         /* { .input = &tegra_pll_c2,    .value = 6}, - no use on tegra12x */
7328         /* { .input = &tegra_clk_c3,    .value = 7}, - no use on tegra12x */
7329         { .input = &tegra_pll_x_out0,   .value = 8},
7330         { .input = &tegra_pll_x,        .value = 8 | SUPER_LP_DIV2_BYPASS},
7331         { 0, 0},
7332 };
7333 #else
7334
7335 static struct clk_mux_sel mux_cclk_g[] = {
7336         { .input = &tegra_clk_m,        .value = 0},
7337         /* { .input = ,                 .value = 1}, - testclk */
7338         { .input = &tegra_clk_m,        .value = 2},
7339         { .input = &tegra_pll_ref,      .value = 3},
7340         { .input = &tegra_pll_m,        .value = 4},
7341         { .input = &tegra_pll_p,        .value = 5},
7342         { .input = &tegra_clk_sclk,     .value = 6},
7343         { .input = &tegra_clk_m,        .value = 7},
7344         { .input = &tegra_pll_x,        .value = 8},
7345         /* { .input = ,                 .value = 9},  - High jitter DFLL */
7346         /* { .input = ,                 .value = 14}, - High jitter PLLX */
7347         { .input = &tegra_dfll_cpu,     .value = 15},
7348         { 0, 0},
7349 };
7350
7351 #endif
7352
7353 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7354 static struct clk tegra_clk_cclk_g = {
7355         .name   = "cclk_g",
7356         .flags  = DIV_U71 | DIV_U71_INT | MUX,
7357         .inputs = mux_cclk_g,
7358         .reg    = 0x368,
7359         .ops    = &tegra_super_ops,
7360         .max_rate = 3000000000UL,
7361 };
7362
7363 static struct clk tegra_clk_cclk_lp = {
7364         .name   = "cclk_lp",
7365         .flags  = DIV_2 | DIV_U71 | DIV_U71_INT | MUX,
7366         .inputs = mux_cclk_lp,
7367         .reg    = 0x370,
7368         .ops    = &tegra_super_ops,
7369         .max_rate = 1350000000,
7370 };
7371 #else
7372
7373 static struct clk tegra_clk_cclk_g = {
7374         .name   = "cclk_g",
7375         .flags  = DIV_U71 | DIV_U71_INT | MUX,
7376         .inputs = mux_cclk_g,
7377         .reg    = 0x20,
7378         .ops    = &tegra13_super_cclk_ops,
7379         .max_rate = 3000000000UL,
7380 };
7381
7382 #endif
7383
7384 static struct raw_notifier_head cpu_g_rate_change_nh;
7385
7386 static struct clk tegra_clk_virtual_cpu_g = {
7387         .name      = "cpu_g",
7388         .parent    = &tegra_clk_cclk_g,
7389         .ops       = &tegra_cpu_ops,
7390         .max_rate  = 3000000000UL,
7391         .min_rate  = 3187500,
7392         .u.cpu = {
7393                 .main      = &tegra_pll_x,
7394 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7395                 .backup    = &tegra_pll_p_out4,
7396 #else
7397                 .backup    = &tegra_pll_p,
7398 #endif
7399                 .dynamic   = &tegra_dfll_cpu,
7400                 .mode      = MODE_G,
7401         },
7402         .rate_change_nh = &cpu_g_rate_change_nh,
7403 };
7404
7405 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7406 static struct clk tegra_clk_virtual_cpu_lp = {
7407         .name      = "cpu_lp",
7408         .parent    = &tegra_clk_cclk_lp,
7409         .ops       = &tegra_cpu_ops,
7410         .max_rate  = 1350000000,
7411         .min_rate  = 3187500,
7412         .u.cpu = {
7413                 .main      = &tegra_pll_x,
7414                 .backup    = &tegra_pll_p_out4,
7415                 .mode      = MODE_LP,
7416         },
7417 };
7418 #endif
7419
7420 static struct clk_mux_sel mux_cpu_cmplx[] = {
7421         { .input = &tegra_clk_virtual_cpu_g,    .value = 0},
7422 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7423         { .input = &tegra_clk_virtual_cpu_lp,   .value = 1},
7424 #endif
7425         { 0, 0},
7426 };
7427
7428 static struct clk tegra_clk_cpu_cmplx = {
7429         .name      = "cpu",
7430         .inputs    = mux_cpu_cmplx,
7431         .ops       = &tegra_cpu_cmplx_ops,
7432         .max_rate  = 3000000000UL,
7433 };
7434
7435 static struct clk tegra_clk_cop = {
7436         .name      = "cop",
7437         .parent    = &tegra_clk_sclk,
7438         .ops       = &tegra_cop_ops,
7439         .max_rate  = 408000000,
7440 };
7441
7442 static struct clk tegra_clk_hclk = {
7443         .name           = "hclk",
7444         .flags          = DIV_BUS,
7445         .parent         = &tegra_clk_sclk,
7446         .reg            = 0x30,
7447         .reg_shift      = 4,
7448         .ops            = &tegra_bus_ops,
7449         .max_rate       = 408000000,
7450         .min_rate       = 12000000,
7451 };
7452
7453 static struct clk tegra_clk_pclk = {
7454         .name           = "pclk",
7455         .flags          = DIV_BUS,
7456         .parent         = &tegra_clk_hclk,
7457         .reg            = 0x30,
7458         .reg_shift      = 0,
7459         .ops            = &tegra_bus_ops,
7460         .max_rate       = 204000000,
7461         .min_rate       = 12000000,
7462 };
7463
7464 static struct raw_notifier_head sbus_rate_change_nh;
7465
7466 static struct clk tegra_clk_sbus_cmplx = {
7467         .name      = "sbus",
7468         .parent    = &tegra_clk_sclk,
7469         .ops       = &tegra_sbus_cmplx_ops,
7470         .u.system  = {
7471                 .pclk = &tegra_clk_pclk,
7472                 .hclk = &tegra_clk_hclk,
7473                 .sclk_low = &tegra_pll_p_out2,
7474 #ifdef CONFIG_TEGRA_PLLM_SCALED
7475                 .sclk_high = &tegra_pll_c_out1,
7476 #else
7477                 .sclk_high = &tegra_pll_m_out1,
7478 #endif
7479         },
7480         .rate_change_nh = &sbus_rate_change_nh,
7481 };
7482
7483 static struct clk tegra_clk_ahb = {
7484         .name      = "ahb.sclk",
7485         .flags     = DIV_BUS,
7486         .parent    = &tegra_clk_sbus_cmplx,
7487         .ops       = &tegra_clk_ahb_ops,
7488 };
7489
7490 static struct clk tegra_clk_apb = {
7491         .name      = "apb.sclk",
7492         .flags     = DIV_BUS,
7493         .parent    = &tegra_clk_ahb,
7494         .ops       = &tegra_clk_apb_ops,
7495 };
7496
7497 static struct clk tegra_clk_blink = {
7498         .name           = "blink",
7499         .parent         = &tegra_clk_32k,
7500         .reg            = 0x40,
7501         .ops            = &tegra_blink_clk_ops,
7502         .max_rate       = 32768,
7503 };
7504
7505
7506 /* Multimedia modules muxes */
7507 static struct clk_mux_sel mux_pllm_pllc2_c_c3_pllp_plla[] = {
7508         { .input = &tegra_pll_m,  .value = 0},
7509         { .input = &tegra_pll_c2, .value = 1},
7510         { .input = &tegra_pll_c,  .value = 2},
7511         { .input = &tegra_pll_c3, .value = 3},
7512         { .input = &tegra_pll_p,  .value = 4},
7513         { .input = &tegra_pll_a_out0, .value = 6},
7514         { 0, 0},
7515 };
7516
7517 static struct clk_mux_sel mux_pllm_pllc_pllp_plla[] = {
7518         { .input = &tegra_pll_m, .value = 0},
7519         { .input = &tegra_pll_c, .value = 2},
7520         { .input = &tegra_pll_p, .value = 4},
7521         { .input = &tegra_pll_a_out0, .value = 6},
7522         { 0, 0},
7523 };
7524
7525 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_v2[] = {
7526         { .input = &tegra_pll_m, .value = 0},
7527         { .input = &tegra_pll_c, .value = 1},
7528         { .input = &tegra_pll_p, .value = 2},
7529         { .input = &tegra_pll_a_out0, .value = 3},
7530         /* Skip C2(4) */
7531         /* Skip C2(5) */
7532         { 0, 0},
7533 };
7534
7535 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_pllc2_c3_clkm[] = {
7536         { .input = &tegra_pll_m, .value = 0},
7537         { .input = &tegra_pll_c, .value = 1},
7538         { .input = &tegra_pll_p, .value = 2},
7539         { .input = &tegra_pll_a_out0, .value = 3},
7540         { .input = &tegra_pll_c2, .value = 4},
7541         { .input = &tegra_pll_c3, .value = 5},
7542         { .input = &tegra_clk_m, .value = 6},
7543         { 0, 0},
7544 };
7545
7546 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_pllc4[] = {
7547         { .input = &tegra_pll_m, .value = 0},
7548         /* Skip C2(1) */
7549         { .input = &tegra_pll_c, .value = 2},
7550         /* Skip C2(3) */
7551         { .input = &tegra_pll_p, .value = 4},
7552         { .input = &tegra_pll_a_out0, .value = 6},
7553         { .input = &tegra_pll_c4, .value = 7},
7554         { 0, 0},
7555 };
7556
7557 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_clkm_pllc4[] = {
7558         { .input = &tegra_pll_m, .value = 0},
7559         { .input = &tegra_pll_c, .value = 1},
7560         { .input = &tegra_pll_p, .value = 2},
7561         { .input = &tegra_pll_a_out0, .value = 3},
7562         /* Skip C2(4) & C3(5) */
7563         { .input = &tegra_clk_m, .value = 6},
7564         { .input = &tegra_pll_c4, .value = 7},
7565         { 0, 0},
7566 };
7567
7568 static struct clk_mux_sel mux_plla_pllc_pllp_clkm[] = {
7569         { .input = &tegra_pll_a_out0, .value = 0},
7570         { .input = &tegra_pll_c, .value = 2},
7571         { .input = &tegra_pll_p, .value = 4},
7572         { .input = &tegra_clk_m, .value = 6},
7573         { 0, 0},
7574 };
7575
7576 /* EMC muxes */
7577 /* FIXME: add EMC latency mux */
7578 static struct clk_mux_sel mux_pllm_pllc_pllp_clkm[] = {
7579         { .input = &tegra_pll_m, .value = 0},
7580         { .input = &tegra_pll_c, .value = 1},
7581         { .input = &tegra_pll_p, .value = 2},
7582         { .input = &tegra_clk_m, .value = 3},
7583         { .input = &tegra_pll_m, .value = 4}, /* low jitter PLLM output */
7584         /* { .input = &tegra_pll_c2, .value = 5}, - no use on tegra12x */
7585         /* { .input = &tegra_pll_c3, .value = 6}, - no use on tegra12x */
7586         { .input = &tegra_pll_c, .value = 7}, /* low jitter PLLC output */
7587         { 0, 0},
7588 };
7589
7590
7591 /* Display subsystem muxes */
7592 static struct clk_mux_sel mux_pllp_pllm_plld_plla_pllc_plld2_clkm[] = {
7593         {.input = &tegra_pll_p, .value = 0},
7594         {.input = &tegra_pll_m, .value = 1},
7595         {.input = &tegra_pll_d_out0, .value = 2},
7596         {.input = &tegra_pll_a_out0, .value = 3},
7597         {.input = &tegra_pll_c, .value = 4},
7598         {.input = &tegra_pll_d2, .value = 5},
7599         {.input = &tegra_clk_m, .value = 6},
7600         { 0, 0},
7601 };
7602
7603 static struct clk_mux_sel mux_plld_out0[] = {
7604         { .input = &tegra_pll_d_out0,  .value = 0},
7605         { 0, 0},
7606 };
7607
7608 /* Peripheral muxes */
7609 static struct clk_mux_sel mux_pllp_pllc_clkm[] = {
7610         {.input = &tegra_pll_p,     .value = 0},
7611         {.input = &tegra_pll_c,     .value = 2},
7612         {.input = &tegra_clk_m,     .value = 6},
7613         { 0, 0},
7614 };
7615
7616 static struct clk_mux_sel mux_pllp_pllc_clkm1[] = {
7617         {.input = &tegra_pll_p,     .value = 0},
7618         {.input = &tegra_pll_c,     .value = 1},
7619         {.input = &tegra_clk_m,     .value = 3},
7620         { 0, 0},
7621 };
7622
7623 static struct clk_mux_sel mux_pllp_pllc2_c_c3_pllm_clkm[] = {
7624         { .input = &tegra_pll_p,  .value = 0},
7625         { .input = &tegra_pll_c2, .value = 1},
7626         { .input = &tegra_pll_c,  .value = 2},
7627         { .input = &tegra_pll_c3, .value = 3},
7628         { .input = &tegra_pll_m,  .value = 4},
7629         { .input = &tegra_clk_m,  .value = 6},
7630         { 0, 0},
7631 };
7632
7633 static struct clk_mux_sel mux_pllp_pllc_pllm_clkm[] = {
7634         { .input = &tegra_pll_p, .value = 0},
7635         { .input = &tegra_pll_c, .value = 2},
7636         { .input = &tegra_pll_m, .value = 4},
7637         { .input = &tegra_clk_m, .value = 6},
7638         { 0, 0},
7639 };
7640
7641 static struct clk_mux_sel mux_pllp_pllc_pllm[] = {
7642         {.input = &tegra_pll_p,     .value = 0},
7643         {.input = &tegra_pll_c,     .value = 2},
7644         {.input = &tegra_pll_m,     .value = 4},
7645         { 0, 0},
7646 };
7647
7648 static struct clk_mux_sel mux_pllp_clkm_clk32_plle[] = {
7649         { .input = &tegra_pll_p,    .value = 0},
7650         { .input = &tegra_clk_m,    .value = 1},
7651         { .input = &tegra_clk_32k,  .value = 2},
7652         { .input = &tegra_pll_e,    .value = 3},
7653         { 0, 0},
7654 };
7655
7656 static struct clk_mux_sel mux_pllp_clkm[] = {
7657         { .input = &tegra_pll_p, .value = 0},
7658         { .input = &tegra_clk_m, .value = 6},
7659         { 0, 0},
7660 };
7661
7662 static struct clk_mux_sel mux_pllp_pllc_clk32_clkm[] = {
7663         {.input = &tegra_pll_p,     .value = 0},
7664         {.input = &tegra_pll_c,     .value = 2},
7665         {.input = &tegra_clk_32k,   .value = 4},
7666         {.input = &tegra_clk_m,     .value = 6},
7667         { 0, 0},
7668 };
7669
7670 static struct clk_mux_sel mux_pllp_pllc_clkm_clk32[] = {
7671         {.input = &tegra_pll_p,     .value = 0},
7672         {.input = &tegra_pll_c,     .value = 2},
7673         {.input = &tegra_clk_m,     .value = 4},
7674         {.input = &tegra_clk_32k,   .value = 6},
7675         { 0, 0},
7676 };
7677
7678 static struct clk_mux_sel mux_plla_clk32_pllp_clkm_plle[] = {
7679         { .input = &tegra_pll_a_out0, .value = 0},
7680         { .input = &tegra_clk_32k,    .value = 1},
7681         { .input = &tegra_pll_p,      .value = 2},
7682         { .input = &tegra_clk_m,      .value = 3},
7683         { .input = &tegra_pll_e,      .value = 4},
7684         { 0, 0},
7685 };
7686
7687 static struct clk_mux_sel mux_clkm_pllp_pllc_pllre[] = {
7688         { .input = &tegra_clk_m,  .value = 0},
7689         { .input = &tegra_pll_p,  .value = 1},
7690         { .input = &tegra_pll_c,  .value = 3},
7691         { .input = &tegra_pll_re_out,  .value = 5},
7692         { 0, 0},
7693 };
7694
7695 static struct clk_mux_sel mux_clkm_48M_pllp_480M[] = {
7696         { .input = &tegra_clk_m,      .value = 0},
7697         { .input = &tegra_pll_u_48M,  .value = 2},
7698         { .input = &tegra_pll_p,      .value = 4},
7699         { .input = &tegra_pll_u_480M, .value = 6},
7700         { 0, 0},
7701 };
7702
7703 static struct clk_mux_sel mux_clkm_pllre_clk32_480M_pllc_ref[] = {
7704         { .input = &tegra_clk_m,      .value = 0},
7705         { .input = &tegra_pll_re_out, .value = 1},
7706         { .input = &tegra_clk_32k,    .value = 2},
7707         { .input = &tegra_pll_u_480M, .value = 3},
7708         { .input = &tegra_pll_c,      .value = 4},
7709         { .input = &tegra_pll_ref,    .value = 7},
7710         { 0, 0},
7711 };
7712
7713 static struct clk_mux_sel mux_pllp3_pllc_clkm[] = {
7714         { .input = &tegra_pll_p_out3, .value = 0},
7715         { .input = &tegra_pll_c,  .value = 1},
7716         { .input = &tegra_clk_m,  .value = 3},
7717         { 0, 0},
7718 };
7719
7720 /* Single clock source ("fake") muxes */
7721 static struct clk_mux_sel mux_clk_m[] = {
7722         { .input = &tegra_clk_m, .value = 0},
7723         { 0, 0},
7724 };
7725
7726 static struct clk_mux_sel mux_pllp_out3[] = {
7727         { .input = &tegra_pll_p_out3, .value = 0},
7728         { 0, 0},
7729 };
7730
7731 static struct clk_mux_sel mux_clk_32k[] = {
7732         { .input = &tegra_clk_32k, .value = 0},
7733         { 0, 0},
7734 };
7735
7736 static struct clk_mux_sel mux_plld[] = {
7737         { .input = &tegra_pll_d_out0, .value = 1},
7738         { 0, 0},
7739 };
7740
7741
7742 static struct raw_notifier_head emc_rate_change_nh;
7743
7744 static struct clk tegra_clk_emc = {
7745         .name = "emc",
7746         .ops = &tegra_emc_clk_ops,
7747         .reg = 0x19c,
7748         .max_rate = 1200000000,
7749         .min_rate = 12750000,
7750         .inputs = mux_pllm_pllc_pllp_clkm,
7751         .flags = MUX | DIV_U71 | PERIPH_EMC_ENB,
7752         .u.periph = {
7753                 .clk_num = 57,
7754         },
7755         .rate_change_nh = &emc_rate_change_nh,
7756 };
7757
7758 static struct clk tegra_clk_mc = {
7759         .name = "mc",
7760         .ops = &tegra_mc_clk_ops,
7761         .max_rate =  533000000,
7762         .parent = &tegra_clk_emc,
7763         .flags = PERIPH_NO_RESET,
7764         .u.periph = {
7765                 .clk_num = 32,
7766         },
7767 };
7768
7769 static struct raw_notifier_head host1x_rate_change_nh;
7770
7771 static struct clk tegra_clk_host1x = {
7772         .name      = "host1x",
7773         .lookup    = {
7774                 .dev_id = "host1x",
7775         },
7776         .ops       = &tegra_1xbus_clk_ops,
7777         .reg       = 0x180,
7778         .inputs    = mux_pllm_pllc_pllp_plla,
7779         .flags     = MUX | DIV_U71 | DIV_U71_INT,
7780         .max_rate  = 500000000,
7781         .min_rate  = 12000000,
7782         .u.periph = {
7783                 .clk_num   = 28,
7784                 .pll_low = &tegra_pll_p,
7785 #ifdef CONFIG_TEGRA_PLLM_SCALED
7786                 .pll_high = &tegra_pll_c,
7787 #else
7788                 .pll_high = &tegra_pll_m,
7789 #endif
7790         },
7791         .rate_change_nh = &host1x_rate_change_nh,
7792 };
7793
7794 static struct raw_notifier_head mselect_rate_change_nh;
7795
7796 static struct clk tegra_clk_mselect = {
7797         .name      = "mselect",
7798         .lookup    = {
7799                 .dev_id = "mselect",
7800         },
7801         .ops       = &tegra_1xbus_clk_ops,
7802         .reg       = 0x3b4,
7803         .inputs    = mux_pllp_clkm,
7804         .flags     = MUX | DIV_U71 | DIV_U71_INT,
7805         .max_rate  = 408000000,
7806         .min_rate  = 12000000,
7807         .u.periph = {
7808                 .clk_num   = 99,
7809                 .pll_low = &tegra_pll_p,
7810                 .pll_high = &tegra_pll_p,
7811                 .threshold = 408000000,
7812         },
7813         .rate_change_nh = &mselect_rate_change_nh,
7814 };
7815
7816 #ifdef CONFIG_TEGRA_DUAL_CBUS
7817
7818 static struct raw_notifier_head c2bus_rate_change_nh;
7819 static struct raw_notifier_head c3bus_rate_change_nh;
7820
7821 static struct clk tegra_clk_c2bus = {
7822         .name      = "c2bus",
7823         .parent    = &tegra_pll_c2,
7824         .ops       = &tegra_clk_cbus_ops,
7825         .max_rate  = 700000000,
7826         .mul       = 1,
7827         .div       = 1,
7828         .flags     = PERIPH_ON_CBUS,
7829         .shared_bus_backup = {
7830                 .input = &tegra_pll_p,
7831         },
7832         .rate_change_nh = &c2bus_rate_change_nh,
7833 };
7834 static struct clk tegra_clk_c3bus = {
7835         .name      = "c3bus",
7836         .parent    = &tegra_pll_c3,
7837         .ops       = &tegra_clk_cbus_ops,
7838         .max_rate  = 900000000,
7839         .mul       = 1,
7840         .div       = 1,
7841         .flags     = PERIPH_ON_CBUS,
7842         .shared_bus_backup = {
7843                 .input = &tegra_pll_p,
7844         },
7845         .rate_change_nh = &c3bus_rate_change_nh,
7846 };
7847
7848 #ifdef CONFIG_TEGRA_MIGRATE_CBUS_USERS
7849 static DEFINE_MUTEX(cbus_mutex);
7850 #define CROSS_CBUS_MUTEX (&cbus_mutex)
7851 #else
7852 #define CROSS_CBUS_MUTEX NULL
7853 #endif
7854
7855
7856 static struct clk_mux_sel mux_clk_cbus[] = {
7857         { .input = &tegra_clk_c2bus, .value = 0},
7858         { .input = &tegra_clk_c3bus, .value = 1},
7859         { 0, 0},
7860 };
7861
7862 #define DUAL_CBUS_CLK(_name, _dev, _con, _parent, _id, _div, _mode)\
7863         {                                               \
7864                 .name      = _name,                     \
7865                 .lookup    = {                          \
7866                         .dev_id    = _dev,              \
7867                         .con_id    = _con,              \
7868                 },                                      \
7869                 .ops = &tegra_clk_shared_bus_user_ops,  \
7870                 .parent = _parent,                      \
7871                 .inputs = mux_clk_cbus,                 \
7872                 .flags = MUX,                           \
7873                 .u.shared_bus_user = {                  \
7874                         .client_id = _id,               \
7875                         .client_div = _div,             \
7876                         .mode = _mode,                  \
7877                 },                                      \
7878                 .cross_clk_mutex = CROSS_CBUS_MUTEX,    \
7879         }
7880
7881 #else
7882
7883 static struct raw_notifier_head cbus_rate_change_nh;
7884
7885 static struct clk tegra_clk_cbus = {
7886         .name      = "cbus",
7887         .parent    = &tegra_pll_c,
7888         .ops       = &tegra_clk_cbus_ops,
7889         .max_rate  = 700000000,
7890         .mul       = 1,
7891         .div       = 2,
7892         .flags     = PERIPH_ON_CBUS,
7893         .shared_bus_backup = {
7894                 .input = &tegra_pll_p,
7895         },
7896         .rate_change_nh = &cbus_rate_change_nh,
7897 };
7898 #endif
7899
7900 static struct clk_ops tegra_clk_gpu_ops = {
7901         .enable         = &tegra12_periph_clk_enable,
7902         .disable        = &tegra12_periph_clk_disable,
7903         .reset          = &tegra12_periph_clk_reset,
7904 };
7905
7906 /* This is a dummy clock for gpu. The enable/disable/reset routine controls
7907    input clock of the actual gpu clock. The input clock itself has a fixed
7908    frequency. The actual gpu clock's frequency is controlled by gpu driver,
7909    not here in clock framework. However, we assoicate this dummy clock with
7910    dvfs to control voltage of gpu rail along with frequency change of actual
7911    gpu clock. So frequency here and in dvfs are based on the acutal gpu clock. */
7912 static struct clk tegra_clk_gpu = {
7913         .name      = "gpu_ref",
7914         .ops       = &tegra_clk_gpu_ops,
7915         .parent    = &tegra_pll_ref,
7916         .u.periph  = {
7917                 .clk_num = 184,
7918         },
7919         .max_rate  = 48000000,
7920         .min_rate  = 12000000,
7921 };
7922
7923 #define RATE_GRANULARITY        100000 /* 0.1 MHz */
7924 #if defined(CONFIG_TEGRA_CLOCK_DEBUG_FUNC)
7925 static int gbus_round_pass_thru;
7926 void tegra_gbus_round_pass_thru_enable(bool enable)
7927 {
7928         if (enable)
7929                 gbus_round_pass_thru = 1;
7930         else
7931                 gbus_round_pass_thru = 0;
7932 }
7933 EXPORT_SYMBOL(tegra_gbus_round_pass_thru_enable);
7934 #else
7935 #define gbus_round_pass_thru    0
7936 #endif
7937
7938 static void tegra12_clk_gbus_init(struct clk *c)
7939 {
7940         unsigned long rate;
7941         bool enabled;
7942
7943         pr_debug("%s on clock %s (export ops %s)\n", __func__,
7944                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
7945
7946         if (!c->u.export_clk.ops || !c->u.export_clk.ops->init)
7947                 return;
7948
7949         c->u.export_clk.ops->init(c->u.export_clk.ops->data, &rate, &enabled);
7950         c->div = clk_get_rate(c->parent) / RATE_GRANULARITY;
7951         c->mul = rate / RATE_GRANULARITY;
7952         c->state = enabled ? ON : OFF;
7953 }
7954
7955 static int tegra12_clk_gbus_enable(struct clk *c)
7956 {
7957         pr_debug("%s on clock %s (export ops %s)\n", __func__,
7958                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
7959
7960         if (!c->u.export_clk.ops || !c->u.export_clk.ops->enable)
7961                 return -ENOENT;
7962
7963         return c->u.export_clk.ops->enable(c->u.export_clk.ops->data);
7964 }
7965
7966 static void tegra12_clk_gbus_disable(struct clk *c)
7967 {
7968         pr_debug("%s on clock %s (export ops %s)\n", __func__,
7969                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
7970
7971         if (!c->u.export_clk.ops || !c->u.export_clk.ops->disable)
7972                 return;
7973
7974         c->u.export_clk.ops->disable(c->u.export_clk.ops->data);
7975 }
7976
7977 static int tegra12_clk_gbus_set_rate(struct clk *c, unsigned long rate)
7978 {
7979         int ret;
7980
7981         pr_debug("%s %lu on clock %s (export ops %s)\n", __func__,
7982                  rate, c->name, c->u.export_clk.ops ? "ready" : "not ready");
7983
7984         if (!c->u.export_clk.ops || !c->u.export_clk.ops->set_rate)
7985                 return -ENOENT;
7986
7987         ret = c->u.export_clk.ops->set_rate(c->u.export_clk.ops->data, &rate);
7988         if (!ret)
7989                 c->mul = rate / RATE_GRANULARITY;
7990         return ret;
7991 }
7992
7993 static long tegra12_clk_gbus_round_updown(struct clk *c, unsigned long rate,
7994                                           bool up)
7995 {
7996         return gbus_round_pass_thru ? rate :
7997                 tegra12_clk_cbus_round_updown(c, rate, up);
7998 }
7999
8000 static long tegra12_clk_gbus_round_rate(struct clk *c, unsigned long rate)
8001 {
8002         return tegra12_clk_gbus_round_updown(c, rate, true);
8003 }
8004
8005 static struct clk_ops tegra_clk_gbus_ops = {
8006         .init           = tegra12_clk_gbus_init,
8007         .enable         = tegra12_clk_gbus_enable,
8008         .disable        = tegra12_clk_gbus_disable,
8009         .set_rate       = tegra12_clk_gbus_set_rate,
8010         .round_rate     = tegra12_clk_gbus_round_rate,
8011         .round_rate_updown = tegra12_clk_gbus_round_updown,
8012         .shared_bus_update = tegra12_clk_shared_connector_update, /* re-use */
8013 };
8014
8015 static struct raw_notifier_head gbus_rate_change_nh;
8016
8017 static struct clk tegra_clk_gbus = {
8018         .name      = "gbus",
8019         .ops       = &tegra_clk_gbus_ops,
8020         .parent    = &tegra_clk_gpu,
8021         .max_rate  = 1032000000,
8022         .shared_bus_flags = SHARED_BUS_RETENTION,
8023         .rate_change_nh = &gbus_rate_change_nh,
8024 };
8025
8026 static void tegra12_camera_mclk_init(struct clk *c)
8027 {
8028         c->state = OFF;
8029         c->set = true;
8030
8031         if (!strcmp(c->name, "mclk")) {
8032                 c->parent = tegra_get_clock_by_name("vi_sensor");
8033                 c->max_rate = c->parent->max_rate;
8034         } else if (!strcmp(c->name, "mclk2")) {
8035                 c->parent = tegra_get_clock_by_name("vi_sensor2");
8036                 c->max_rate = c->parent->max_rate;
8037         }
8038 }
8039
8040 static int tegra12_camera_mclk_set_rate(struct clk *c, unsigned long rate)
8041 {
8042         return clk_set_rate(c->parent, rate);
8043 }
8044
8045 static struct clk_ops tegra_camera_mclk_ops = {
8046         .init     = tegra12_camera_mclk_init,
8047         .enable   = tegra12_periph_clk_enable,
8048         .disable  = tegra12_periph_clk_disable,
8049         .set_rate = tegra12_camera_mclk_set_rate,
8050 };
8051
8052 static struct clk tegra_camera_mclk = {
8053         .name = "mclk",
8054         .ops = &tegra_camera_mclk_ops,
8055         .u.periph = {
8056                 .clk_num = 92, /* csus */
8057         },
8058         .flags = PERIPH_NO_RESET,
8059 };
8060
8061 static struct clk tegra_camera_mclk2 = {
8062         .name = "mclk2",
8063         .ops = &tegra_camera_mclk_ops,
8064         .u.periph = {
8065                 .clk_num = 171, /* vim2_clk */
8066         },
8067         .flags = PERIPH_NO_RESET,
8068 };
8069
8070 static struct clk tegra_clk_isp = {
8071         .name = "isp",
8072         .ops = &tegra_periph_clk_ops,
8073         .reg = 0x144,
8074         .max_rate = 700000000,
8075         .inputs = mux_pllm_pllc_pllp_plla_clkm_pllc4,
8076         .flags = MUX | DIV_U71 | PERIPH_NO_ENB | PERIPH_NO_RESET,
8077 };
8078
8079 static struct clk_mux_sel mux_isp[] = {
8080         { .input = &tegra_clk_isp, .value = 0},
8081         { 0, 0},
8082 };
8083
8084 static struct raw_notifier_head c4bus_rate_change_nh;
8085
8086 static struct clk tegra_clk_c4bus = {
8087         .name      = "c4bus",
8088         .parent    = &tegra_pll_c4,
8089         .ops       = &tegra_clk_cbus_ops,
8090         .max_rate  = 700000000,
8091         .mul       = 1,
8092         .div       = 1,
8093         .shared_bus_backup = {
8094                 .input = &tegra_pll_p,
8095         },
8096         .rate_change_nh = &c4bus_rate_change_nh,
8097 };
8098
8099 #define PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
8100         {                                               \
8101                 .name      = _name,                     \
8102                 .lookup    = {                          \
8103                         .dev_id    = _dev,              \
8104                         .con_id    = _con,              \
8105                 },                                      \
8106                 .ops       = &tegra_periph_clk_ops,     \
8107                 .reg       = _reg,                      \
8108                 .inputs    = _inputs,                   \
8109                 .flags     = _flags,                    \
8110                 .max_rate  = _max,                      \
8111                 .u.periph = {                           \
8112                         .clk_num   = _clk_num,          \
8113                 },                                      \
8114         }
8115
8116 #define PERIPH_CLK_EX(_name, _dev, _con, _clk_num, _reg, _max, _inputs, \
8117                         _flags, _ops)                                   \
8118         {                                               \
8119                 .name      = _name,                     \
8120                 .lookup    = {                          \
8121                         .dev_id    = _dev,              \
8122                         .con_id    = _con,              \
8123                 },                                      \
8124                 .ops       = _ops,                      \
8125                 .reg       = _reg,                      \
8126                 .inputs    = _inputs,                   \
8127                 .flags     = _flags,                    \
8128                 .max_rate  = _max,                      \
8129                 .u.periph = {                           \
8130                         .clk_num   = _clk_num,          \
8131                 },                                      \
8132         }
8133
8134 #define D_AUDIO_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
8135         {                                               \
8136                 .name      = _name,                     \
8137                 .lookup    = {                          \
8138                         .dev_id    = _dev,              \
8139                         .con_id    = _con,              \
8140                 },                                      \
8141                 .ops       = &tegra_periph_clk_ops,     \
8142                 .reg       = _reg,                      \
8143                 .inputs    = _inputs,                   \
8144                 .flags     = _flags,                    \
8145                 .max_rate  = _max,                      \
8146                 .u.periph = {                           \
8147                         .clk_num   = _clk_num,          \
8148                         .src_mask  = 0xE01F << 16,      \
8149                         .src_shift = 16,                \
8150                 },                                      \
8151         }
8152
8153 #define SHARED_CLK(_name, _dev, _con, _parent, _id, _div, _mode)\
8154         {                                               \
8155                 .name      = _name,                     \
8156                 .lookup    = {                          \
8157                         .dev_id    = _dev,              \
8158                         .con_id    = _con,              \
8159                 },                                      \
8160                 .ops = &tegra_clk_shared_bus_user_ops,  \
8161                 .parent = _parent,                      \
8162                 .u.shared_bus_user = {                  \
8163                         .client_id = _id,               \
8164                         .client_div = _div,             \
8165                         .mode = _mode,                  \
8166                 },                                      \
8167         }
8168 #define SHARED_LIMIT(_name, _dev, _con, _parent, _id, _div, _mode)\
8169         {                                               \
8170                 .name      = _name,                     \
8171                 .lookup    = {                          \
8172                         .dev_id    = _dev,              \
8173                         .con_id    = _con,              \
8174                 },                                      \
8175                 .ops = &tegra_clk_shared_bus_user_ops,  \
8176                 .parent = _parent,                      \
8177                 .flags     = BUS_RATE_LIMIT,            \
8178                 .u.shared_bus_user = {                  \
8179                         .client_id = _id,               \
8180                         .client_div = _div,             \
8181                         .mode = _mode,                  \
8182                 },                                      \
8183         }
8184 #define SHARED_CONNECT(_name, _dev, _con, _parent, _id, _div, _mode)\
8185         {                                               \
8186                 .name      = _name,                     \
8187                 .lookup    = {                          \
8188                         .dev_id    = _dev,              \
8189                         .con_id    = _con,              \
8190                 },                                      \
8191                 .ops = &tegra_clk_shared_connector_ops, \
8192                 .parent = _parent,                      \
8193                 .u.shared_bus_user = {                  \
8194                         .client_id = _id,               \
8195                         .client_div = _div,             \
8196                         .mode = _mode,                  \
8197                 },                                      \
8198         }
8199
8200 #define SHARED_EMC_CLK(_name, _dev, _con, _parent, _id, _div, _mode, _flag)\
8201         {                                               \
8202                 .name      = _name,                     \
8203                 .lookup    = {                          \
8204                         .dev_id    = _dev,              \
8205                         .con_id    = _con,              \
8206                 },                                      \
8207                 .ops = &tegra_clk_shared_bus_user_ops,  \
8208                 .parent = _parent,                      \
8209                 .u.shared_bus_user = {                  \
8210                         .client_id = _id,               \
8211                         .client_div = _div,             \
8212                         .mode = _mode,                  \
8213                         .usage_flag = _flag,            \
8214                 },                                      \
8215         }
8216
8217 static DEFINE_MUTEX(sbus_cross_mutex);
8218 #define SHARED_SCLK(_name, _dev, _con, _parent, _id, _div, _mode)\
8219         {                                               \
8220                 .name = _name,                          \
8221                 .lookup = {                             \
8222                         .dev_id = _dev,                 \
8223                         .con_id = _con,                 \
8224                 },                                      \
8225                 .ops = &tegra_clk_shared_bus_user_ops,  \
8226                 .parent = _parent,                      \
8227                 .u.shared_bus_user = {                  \
8228                         .client_id = _id,               \
8229                         .client_div = _div,             \
8230                         .mode = _mode,                  \
8231                 },                                      \
8232                 .cross_clk_mutex = &sbus_cross_mutex,   \
8233 }
8234
8235 struct clk tegra_list_clks[] = {
8236         PERIPH_CLK("apbdma",    "tegra-apbdma",         NULL,   34,     0,      26000000,  mux_clk_m,                   0),
8237         PERIPH_CLK("rtc",       "rtc-tegra",            NULL,   4,      0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB),
8238         PERIPH_CLK("kbc",       "tegra-kbc",            NULL,   36,     0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB),
8239         PERIPH_CLK("timer",     "timer",                NULL,   5,      0,      26000000,  mux_clk_m,                   0),
8240         PERIPH_CLK("kfuse",     "kfuse-tegra",          NULL,   40,     0,      26000000,  mux_clk_m,                   PERIPH_ON_APB),
8241         PERIPH_CLK("fuse",      "fuse-tegra",           "fuse", 39,     0,      26000000,  mux_clk_m,                   PERIPH_ON_APB),
8242         PERIPH_CLK("fuse_burn", "fuse-tegra",           "fuse_burn",    39,     0,      26000000,  mux_clk_m,           PERIPH_ON_APB),
8243         PERIPH_CLK("apbif",     "tegra30-ahub",         "apbif", 107,   0,      26000000,  mux_clk_m,                   PERIPH_ON_APB),
8244         PERIPH_CLK("i2s0",      "tegra30-i2s.0",        NULL,   30,     0x1d8,  24576000,  mux_pllaout0_audio0_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8245         PERIPH_CLK("i2s1",      "tegra30-i2s.1",        NULL,   11,     0x100,  24576000,  mux_pllaout0_audio1_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8246         PERIPH_CLK("i2s2",      "tegra30-i2s.2",        NULL,   18,     0x104,  24576000,  mux_pllaout0_audio2_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8247         PERIPH_CLK("i2s3",      "tegra30-i2s.3",        NULL,   101,    0x3bc,  24576000,  mux_pllaout0_audio3_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8248         PERIPH_CLK("i2s4",      "tegra30-i2s.4",        NULL,   102,    0x3c0,  24576000,  mux_pllaout0_audio4_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8249         PERIPH_CLK("spdif_out", "tegra30-spdif",        "spdif_out",    10,     0x108,   24576000, mux_pllaout0_audio_2x_pllp_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8250         PERIPH_CLK("spdif_in",  "tegra30-spdif",        "spdif_in",     10,     0x10c,  100000000, mux_pllp_pllc_pllm,          MUX | DIV_U71 | PERIPH_ON_APB),
8251         PERIPH_CLK("pwm",       "tegra-pwm",            NULL,   17,     0x110,  48000000, mux_pllp_pllc_clk32_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8252         D_AUDIO_CLK("d_audio",  "tegra30-ahub",         "d_audio",      106,    0x3d0,  48000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8253         D_AUDIO_CLK("dam0",     "tegra30-dam.0",        NULL,   108,    0x3d8,  40000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8254         D_AUDIO_CLK("dam1",     "tegra30-dam.1",        NULL,   109,    0x3dc,  40000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8255         D_AUDIO_CLK("dam2",     "tegra30-dam.2",        NULL,   110,    0x3e0,  40000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8256         PERIPH_CLK("adx",       "adx",                  NULL,   154,    0x638,  24580000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8257         PERIPH_CLK("adx1",      "adx1",                 NULL,   180,    0x670,  24580000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8258         PERIPH_CLK("amx",       "amx",                  NULL,   153,    0x63c,  24600000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8259         PERIPH_CLK("amx1",      "amx1",                 NULL,   185,    0x674,  24600000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8260         PERIPH_CLK("afc0",      "tegra124-afc.0",       NULL,   186,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8261         PERIPH_CLK("afc1",      "tegra124-afc.1",       NULL,   187,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8262         PERIPH_CLK("afc2",      "tegra124-afc.2",       NULL,   188,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8263         PERIPH_CLK("afc3",      "tegra124-afc.3",       NULL,   189,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8264         PERIPH_CLK("afc4",      "tegra124-afc.4",       NULL,   190,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8265         PERIPH_CLK("afc5",      "tegra124-afc.5",       NULL,   191,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8266         PERIPH_CLK("hda",       "tegra30-hda",          "hda",   125,   0x428,  108000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8267         PERIPH_CLK("hda2codec_2x",      "tegra30-hda",  "hda2codec",   111,     0x3e4,  48000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8268         PERIPH_CLK("hda2hdmi",  "tegra30-hda",          "hda2hdmi",     128,    0,      48000000,  mux_clk_m,                   PERIPH_ON_APB),
8269         PERIPH_CLK("sbc1",      "spi-tegra114.0",       NULL,   41,     0x134, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8270         PERIPH_CLK("sbc2",      "spi-tegra114.1",       NULL,   44,     0x118, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8271         PERIPH_CLK("sbc3",      "spi-tegra114.2",       NULL,   46,     0x11c, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8272         PERIPH_CLK("sbc4",      "spi-tegra114.3",       NULL,   68,     0x1b4, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8273         PERIPH_CLK("sbc5",      "spi-tegra114.4",       NULL,   104,    0x3c8, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8274         PERIPH_CLK("sbc6",      "spi-tegra114.5",       NULL,   105,    0x3cc, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8275         PERIPH_CLK("sata_oob",  "tegra_sata_oob",       NULL,   123,    0x420,  216000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8276         PERIPH_CLK("sata",      "tegra_sata",           NULL,   124,    0x424,  216000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8277         PERIPH_CLK("sata_cold", "tegra_sata_cold",      NULL,   129,    0,      48000000,  mux_clk_m,                   PERIPH_ON_APB),
8278         PERIPH_CLK("vfir",      "vfir",                 NULL,   7,      0x168,  72000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8279         PERIPH_CLK("sdmmc1",    "sdhci-tegra.0",        NULL,   14,     0x150,  208000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8280         PERIPH_CLK("sdmmc2",    "sdhci-tegra.1",        NULL,   9,      0x154,  200000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8281         PERIPH_CLK("sdmmc3",    "sdhci-tegra.2",        NULL,   69,     0x1bc,  208000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8282         PERIPH_CLK("sdmmc4",    "sdhci-tegra.3",        NULL,   15,     0x164,  200000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8283         PERIPH_CLK("sdmmc1_ddr", "sdhci-tegra.0",       "ddr",  14,     0x150,  100000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8284         PERIPH_CLK("sdmmc3_ddr", "sdhci-tegra.2",       "ddr",  69,     0x1bc,  100000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8285         PERIPH_CLK("sdmmc4_ddr", "sdhci-tegra.3",       "ddr",  15,     0x164,  102000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8286         PERIPH_CLK("vcp",       "tegra-avp",            "vcp",  29,     0,      250000000, mux_clk_m,                   0),
8287         PERIPH_CLK("bsea",      "tegra-avp",            "bsea", 62,     0,      250000000, mux_clk_m,                   0),
8288         PERIPH_CLK("bsev",      "tegra-aes",            "bsev", 63,     0,      250000000, mux_clk_m,                   0),
8289         PERIPH_CLK("cec",       "tegra_cec",            NULL,   136,    0,      250000000, mux_clk_m,                   PERIPH_ON_APB),
8290         PERIPH_CLK("vde",       "vde",                  NULL,   61,     0x1c8,  600000000, mux_pllp_pllc2_c_c3_pllm_clkm,       MUX | DIV_U71 | DIV_U71_INT),
8291         PERIPH_CLK("csite",     "csite",                NULL,   73,     0x1d4,  144000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8292         PERIPH_CLK("la",        "la",                   NULL,   76,     0x1f8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8293         PERIPH_CLK("trace",     "trace",                NULL,   77,     0x634,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8294         PERIPH_CLK("owr",       "tegra_w1",             NULL,   71,     0x1cc,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8295         PERIPH_CLK("nor",       "tegra-nor",            NULL,   42,     0x1d0,  127000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
8296         PERIPH_CLK("mipi",      "mipi",                 NULL,   50,     0x174,  60000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8297         PERIPH_CLK("i2c1",      "tegra12-i2c.0",        "div-clk",      12,     0x124,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8298         PERIPH_CLK("i2c2",      "tegra12-i2c.1",        "div-clk",      54,     0x198,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8299         PERIPH_CLK("i2c3",      "tegra12-i2c.2",        "div-clk",      67,     0x1b8,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8300         PERIPH_CLK("i2c4",      "tegra12-i2c.3",        "div-clk",      103,    0x3c4,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8301         PERIPH_CLK("i2c5",      "tegra12-i2c.4",        "div-clk",      47,     0x128,  136000000,  mux_pllp_clkm,      MUX | DIV_U16 | PERIPH_ON_APB),
8302         PERIPH_CLK("i2c6",      "tegra12-i2c.5",        "div-clk",      166,    0x65c,  58300000,  mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8303         PERIPH_CLK("mipi-cal",  "mipi-cal",             NULL,   56,     0,      60000000,  mux_clk_m,                   PERIPH_ON_APB),
8304         PERIPH_CLK("mipi-cal-fixed", "mipi-cal-fixed",  NULL,   0,      0,      108000000, mux_pllp_out3,       PERIPH_NO_ENB),
8305         PERIPH_CLK("uarta",     "serial-tegra.0",               NULL,   6,      0x178,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8306         PERIPH_CLK("uartb",     "serial-tegra.1",               NULL,   7,      0x17c,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8307         PERIPH_CLK("uartc",     "serial-tegra.2",               NULL,   55,     0x1a0,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8308         PERIPH_CLK("uartd",     "serial-tegra.3",               NULL,   65,     0x1c0,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8309         PERIPH_CLK("vic03",     "vic03",                NULL,   178,    0x678,  900000000, mux_pllm_pllc_pllp_plla_pllc2_c3_clkm,       MUX | DIV_U71),
8310         PERIPH_CLK_EX("vi",     "vi",                   "vi",   20,     0x148,  700000000, mux_pllm_pllc_pllp_plla_pllc4,       MUX | DIV_U71 | DIV_U71_INT, &tegra_vi_clk_ops),
8311         PERIPH_CLK("vi_sensor",  NULL,                  "vi_sensor",    164,    0x1a8,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET),
8312         PERIPH_CLK("vi_sensor2", NULL,                  "vi_sensor2",   165,    0x658,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET),
8313         PERIPH_CLK_EX("msenc",  "msenc",                NULL,   91,     0x1f0,  600000000, mux_pllm_pllc2_c_c3_pllp_plla,       MUX | DIV_U71 | DIV_U71_INT, &tegra_msenc_clk_ops),
8314         PERIPH_CLK("tsec",      "tsec",                 NULL,   83,     0x1f4,  900000000, mux_pllp_pllc2_c_c3_pllm_clkm,       MUX | DIV_U71 | DIV_U71_INT),
8315         PERIPH_CLK_EX("dtv",    "dtv",                  NULL,   79,     0x1dc,  250000000, mux_clk_m,                   PERIPH_ON_APB,  &tegra_dtv_clk_ops),
8316         PERIPH_CLK("hdmi",      "hdmi",                 NULL,   51,     0x18c,  594000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | DIV_U71),
8317         PERIPH_CLK("disp1",     "tegradc.0",            NULL,   27,     0x138,  600000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX),
8318         PERIPH_CLK("disp2",     "tegradc.1",            NULL,   26,     0x13c,  600000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX),
8319         PERIPH_CLK_EX("sor0",   "sor0",                 NULL,   182,    0x414,  540000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | DIV_U71, &tegra_sor_clk_ops),
8320         PERIPH_CLK("dpaux",     "dpaux",                NULL,   181,    0,      24000000, mux_clk_m,                    0),
8321         PERIPH_CLK("usbd",      "tegra-udc.0",          NULL,   22,     0,      480000000, mux_clk_m,                   0),
8322         PERIPH_CLK("usb2",      "tegra-ehci.1",         NULL,   58,     0,      480000000, mux_clk_m,                   0),
8323         PERIPH_CLK("usb3",      "tegra-ehci.2",         NULL,   59,     0,      480000000, mux_clk_m,                   0),
8324         PERIPH_CLK_EX("dsia",   "tegradc.0",            "dsia", 48,     0xd0,   750000000, mux_plld_out0,               PLLD,   &tegra_dsi_clk_ops),
8325         PERIPH_CLK_EX("dsib",   "tegradc.1",            "dsib", 82,     0x4b8,  750000000, mux_plld_out0,               PLLD,   &tegra_dsi_clk_ops),
8326         PERIPH_CLK("dsi1-fixed", "tegradc.0",           "dsi-fixed",    0,      0,      108000000, mux_pllp_out3,       PERIPH_NO_ENB),
8327         PERIPH_CLK("dsi2-fixed", "tegradc.1",           "dsi-fixed",    0,      0,      108000000, mux_pllp_out3,       PERIPH_NO_ENB),
8328         PERIPH_CLK("csi",       "vi",                   "csi",  52,     0,      750000000, mux_plld,                    PLLD),
8329         PERIPH_CLK("ispa",      "isp",                  "ispa", 23,     0,      700000000, mux_isp,                     PERIPH_ON_APB),
8330         PERIPH_CLK("ispb",      "isp",                  "ispb", 3,      0,      700000000, mux_isp,                     PERIPH_ON_APB),
8331         PERIPH_CLK("csus",      "vi",                   "csus", 92,     0,      150000000, mux_clk_m,                   PERIPH_NO_RESET),
8332         PERIPH_CLK("vim2_clk",  "vi",                   "vim2_clk",     171,    0,      150000000, mux_clk_m,           PERIPH_NO_RESET),
8333         PERIPH_CLK("cilab",     "vi",                   "cilab", 144,   0x614,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8334         PERIPH_CLK("cilcd",     "vi",                   "cilcd", 145,   0x618,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8335         PERIPH_CLK("cile",      "vi",                   "cile",  146,   0x61c,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8336         PERIPH_CLK("dsialp",    "tegradc.0",            "dsialp", 147,  0x620,  156000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8337         PERIPH_CLK("dsiblp",    "tegradc.1",            "dsiblp", 148,  0x624,  156000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8338         PERIPH_CLK("entropy",   "entropy",              NULL, 149,      0x628,  102000000, mux_pllp_clkm_clk32_plle,    MUX | DIV_U71),
8339         PERIPH_CLK("hdmi_audio", "hdmi_audio",          NULL, 176,      0x668,  48000000,  mux_pllp_pllc_clkm1,         MUX | DIV_U71 | PERIPH_NO_RESET),
8340         PERIPH_CLK("clk72mhz",  "clk72mhz",             NULL, 177,      0x66c,  102000000, mux_pllp3_pllc_clkm,         MUX | DIV_U71 | PERIPH_NO_RESET),
8341
8342         PERIPH_CLK("tsensor",   "tegra-tsensor",        NULL,   100,    0x3b8,   12000000, mux_pllp_pllc_clkm_clk32,    MUX | DIV_U71 | PERIPH_ON_APB),
8343         PERIPH_CLK("actmon",    "actmon",               NULL,   119,    0x3e8,  216000000, mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71),
8344         PERIPH_CLK("extern1",   "extern1",              NULL,   120,    0x3ec,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71),
8345         PERIPH_CLK("extern2",   "extern2",              NULL,   121,    0x3f0,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71),
8346         PERIPH_CLK("extern3",   "extern3",              NULL,   122,    0x3f4,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71),
8347         PERIPH_CLK("i2cslow",   "i2cslow",              NULL,   81,     0x3fc,  26000000,  mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8348         PERIPH_CLK("pcie",      "tegra-pcie",           "pcie", 70,     0,      250000000, mux_clk_m,                   0),
8349         PERIPH_CLK("afi",       "tegra-pcie",           "afi",  72,     0,      250000000, mux_clk_m,                   0),
8350         PERIPH_CLK("se",        "se",                   NULL,   127,    0x42c,  600000000, mux_pllp_pllc2_c_c3_pllm_clkm,       MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB),
8351         PERIPH_CLK("cl_dvfs_ref", "tegra_cl_dvfs",      "ref",  155,    0x62c,  54000000,  mux_pllp_clkm,               MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB),
8352         PERIPH_CLK("cl_dvfs_soc", "tegra_cl_dvfs",      "soc",  155,    0x630,  54000000,  mux_pllp_clkm,               MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB),
8353         PERIPH_CLK("soc_therm", "soc_therm",            NULL,   78,     0x644,  136000000, mux_pllm_pllc_pllp_plla_v2,  MUX | DIV_U71 | PERIPH_ON_APB),
8354
8355         PERIPH_CLK("dds",       "dds",                  NULL,   150,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8356         PERIPH_CLK("dp2",       "dp2",                  NULL,   152,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8357
8358         SHARED_SCLK("automotive.hclk", "automotive",    "hclk", &tegra_clk_ahb,        NULL, 0, 0),
8359         SHARED_SCLK("automotive.pclk", "automotive",    "pclk", &tegra_clk_apb,        NULL, 0, 0),
8360
8361         SHARED_SCLK("avp.sclk",  "tegra-avp",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8362         SHARED_SCLK("bsea.sclk", "tegra-aes",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8363         SHARED_SCLK("usbd.sclk", "tegra-udc.0",         "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8364         SHARED_SCLK("usb1.sclk", "tegra-ehci.0",        "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8365         SHARED_SCLK("usb2.sclk", "tegra-ehci.1",        "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8366         SHARED_SCLK("usb3.sclk", "tegra-ehci.2",        "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8367         SHARED_SCLK("wake.sclk", "wake_sclk",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8368         SHARED_SCLK("automotive.sclk", "automotive",    "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8369         SHARED_SCLK("via.sclk", "tegra_vi.0",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8370         SHARED_SCLK("vib.sclk", "tegra_vi.1",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8371         SHARED_SCLK("ispa.sclk",        "tegra_isp.0",          "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8372         SHARED_SCLK("ispb.sclk",        "tegra_isp.1",          "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8373         SHARED_SCLK("mon.avp",  "tegra_actmon",         "avp",  &tegra_clk_sbus_cmplx, NULL, 0, 0),
8374         SHARED_SCLK("cap.sclk", "cap_sclk",             NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING),
8375         SHARED_SCLK("cap.vcore.sclk",   "cap.vcore.sclk", NULL, &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING),
8376         SHARED_SCLK("cap.throttle.sclk", "cap_throttle", NULL,  &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING),
8377         SHARED_SCLK("floor.sclk", "floor_sclk",         NULL,   &tegra_clk_sbus_cmplx, NULL, 0, 0),
8378         SHARED_SCLK("override.sclk", "override_sclk",   NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_OVERRIDE),
8379         SHARED_SCLK("sbc1.sclk", "tegra12-spi.0",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8380         SHARED_SCLK("sbc2.sclk", "tegra12-spi.1",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8381         SHARED_SCLK("sbc3.sclk", "tegra12-spi.2",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8382         SHARED_SCLK("sbc4.sclk", "tegra12-spi.3",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8383         SHARED_SCLK("sbc5.sclk", "tegra12-spi.4",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8384         SHARED_SCLK("sbc6.sclk", "tegra12-spi.5",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8385
8386         SHARED_EMC_CLK("avp.emc",       "tegra-avp",    "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8387         SHARED_EMC_CLK("mon_cpu.emc",   "tegra_mon", "cpu_emc", &tegra_clk_emc, NULL, 0, 0, 0),
8388 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
8389         SHARED_EMC_CLK("cpu.emc",       "tegra-cpu", "cpu_emc", &tegra_clk_emc, NULL, 0, 0, 0),
8390 #endif
8391         SHARED_EMC_CLK("disp1.emc",     "tegradc.0",    "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_DC1)),
8392         SHARED_EMC_CLK("disp2.emc",     "tegradc.1",    "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_DC2)),
8393         SHARED_EMC_CLK("disp1.la.emc",  "tegradc.0",    "emc.la",       &tegra_clk_emc, NULL, 0, 0, 0),
8394         SHARED_EMC_CLK("disp2.la.emc",  "tegradc.1",    "emc.la",       &tegra_clk_emc, NULL, 0, 0, 0),
8395         SHARED_EMC_CLK("hdmi.emc",      "hdmi",         "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8396         SHARED_EMC_CLK("usbd.emc",      "tegra-udc.0",  "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8397         SHARED_EMC_CLK("usb1.emc",      "tegra-ehci.0", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8398         SHARED_EMC_CLK("usb2.emc",      "tegra-ehci.1", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8399         SHARED_EMC_CLK("usb3.emc",      "tegra-ehci.2", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8400         SHARED_EMC_CLK("sdmmc3.emc",    "sdhci-tegra.2","emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8401         SHARED_EMC_CLK("sdmmc4.emc",    "sdhci-tegra.3","emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8402         SHARED_EMC_CLK("mon.emc",       "tegra_actmon", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8403         SHARED_EMC_CLK("cap.emc",       "cap.emc",      NULL,   &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0),
8404         SHARED_EMC_CLK("cap.vcore.emc", "cap.vcore.emc", NULL,  &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0),
8405         SHARED_EMC_CLK("cap.throttle.emc", "cap_throttle", NULL, &tegra_clk_emc, NULL, 0, SHARED_CEILING_BUT_ISO, 0),
8406         SHARED_EMC_CLK("3d.emc",        "tegra_gk20a.0", "emc", &tegra_clk_emc, NULL, 0, 0, BIT(EMC_USER_3D)),
8407         SHARED_EMC_CLK("msenc.emc",     "tegra_msenc",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_BW,     BIT(EMC_USER_MSENC)),
8408         SHARED_EMC_CLK("tsec.emc",      "tegra_tsec",   "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8409         SHARED_EMC_CLK("via.emc",       "tegra_vi.0",   "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI)),
8410         SHARED_EMC_CLK("vib.emc",       "tegra_vi.1",   "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI2)),
8411         SHARED_EMC_CLK("ispa.emc",      "tegra_isp.0",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_ISP1)),
8412         SHARED_EMC_CLK("ispb.emc",      "tegra_isp.1",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_ISP2)),
8413         SHARED_EMC_CLK("iso.emc",       "iso",          "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8414         SHARED_EMC_CLK("override.emc", "override.emc",  NULL,   &tegra_clk_emc, NULL, 0, SHARED_OVERRIDE, 0),
8415         SHARED_EMC_CLK("vic.emc",       "tegra_vic03.0",        "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8416         SHARED_EMC_CLK("battery.emc",   "battery_edp",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0),
8417         SHARED_LIMIT("floor.emc",       "floor.emc",    NULL,   &tegra_clk_emc, NULL,  0, 0),
8418         SHARED_LIMIT("floor.profile.emc", "profile.emc", "floor", &tegra_clk_emc, NULL,  0, 0),
8419
8420 #ifdef CONFIG_TEGRA_DUAL_CBUS
8421         DUAL_CBUS_CLK("msenc.cbus",     "tegra_msenc",          "msenc", &tegra_clk_c2bus, "msenc", 0, 0),
8422         DUAL_CBUS_CLK("vde.cbus",       "tegra-avp",            "vde",   &tegra_clk_c2bus, "vde",   0, 0),
8423         DUAL_CBUS_CLK("se.cbus",        "tegra12-se",           NULL,    &tegra_clk_c2bus, "se",    0, 0),
8424         SHARED_LIMIT("cap.c2bus",       "cap.c2bus",            NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING),
8425         SHARED_LIMIT("cap.vcore.c2bus", "cap.vcore.c2bus",      NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING),
8426         SHARED_LIMIT("cap.throttle.c2bus", "cap_throttle",      NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING),
8427         SHARED_LIMIT("floor.c2bus",     "floor.c2bus",          NULL,    &tegra_clk_c2bus, NULL,    0, 0),
8428         SHARED_CLK("override.c2bus",    "override.c2bus",       NULL,    &tegra_clk_c2bus, NULL,  0, SHARED_OVERRIDE),
8429
8430         DUAL_CBUS_CLK("vic03.cbus",     "tegra_vic03.0",                "vic03", &tegra_clk_c3bus, "vic03", 0, 0),
8431         DUAL_CBUS_CLK("tsec.cbus",      "tegra_tsec",           "tsec",  &tegra_clk_c3bus,  "tsec", 0, 0),
8432         SHARED_LIMIT("cap.c3bus",       "cap.c3bus",            NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING),
8433         SHARED_LIMIT("cap.vcore.c3bus", "cap.vcore.c3bus",      NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING),
8434         SHARED_LIMIT("cap.throttle.c3bus", "cap_throttle",      NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING),
8435         SHARED_LIMIT("floor.c3bus",     "floor.c3bus",          NULL,    &tegra_clk_c3bus, NULL,    0, 0),
8436         SHARED_CLK("override.c3bus",    "override.c3bus",       NULL,    &tegra_clk_c3bus, NULL,  0, SHARED_OVERRIDE),
8437 #else
8438         SHARED_CLK("vic03.cbus",  "tegra_vic03.0",      "vic03", &tegra_clk_cbus, "vic03", 0, 0),
8439         SHARED_CLK("msenc.cbus","tegra_msenc",          "msenc",&tegra_clk_cbus, "msenc", 0, 0),
8440         SHARED_CLK("tsec.cbus", "tegra_tsec",           "tsec", &tegra_clk_cbus, "tsec", 0, 0),
8441         SHARED_CLK("vde.cbus",  "tegra-avp",            "vde",  &tegra_clk_cbus, "vde", 0, 0),
8442         SHARED_CLK("se.cbus",   "tegra12-se",           NULL,   &tegra_clk_cbus, "se",  0, 0),
8443         SHARED_LIMIT("cap.cbus", "cap.cbus",            NULL,   &tegra_clk_cbus, NULL,  0, SHARED_CEILING),
8444         SHARED_LIMIT("cap.vcore.cbus", "cap.vcore.cbus", NULL,  &tegra_clk_cbus, NULL,  0, SHARED_CEILING),
8445         SHARED_LIMIT("cap.throttle.cbus", "cap_throttle", NULL, &tegra_clk_cbus, NULL,  0, SHARED_CEILING),
8446         SHARED_LIMIT("floor.cbus", "floor.cbus",        NULL,   &tegra_clk_cbus, NULL,  0, 0),
8447         SHARED_CLK("override.cbus", "override.cbus",    NULL,   &tegra_clk_cbus, NULL,  0, SHARED_OVERRIDE),
8448 #endif
8449         SHARED_CLK("gk20a.gbus",        "tegra_gk20a",  "gpu",  &tegra_clk_gbus, NULL,  0, 0),
8450         SHARED_LIMIT("cap.gbus",        "cap.gbus",     NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8451         SHARED_LIMIT("edp.gbus",        "edp.gbus",     NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8452         SHARED_LIMIT("battery.gbus",    "battery_edp",  "gpu",  &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8453         SHARED_LIMIT("cap.throttle.gbus", "cap_throttle", NULL, &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8454         SHARED_LIMIT("cap.profile.gbus", "profile.gbus", "cap", &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8455         SHARED_CLK("override.gbus",     "override.gbus", NULL,  &tegra_clk_gbus, NULL,  0, SHARED_OVERRIDE),
8456         SHARED_LIMIT("floor.gbus",      "floor.gbus",   NULL,   &tegra_clk_gbus, NULL,  0, 0),
8457         SHARED_LIMIT("floor.profile.gbus", "profile.gbus", "floor", &tegra_clk_gbus, NULL,  0, 0),
8458
8459         SHARED_CLK("automotive.host1x", "automotive",   "host1x", &tegra_clk_host1x, NULL,  0, 0),
8460         SHARED_CLK("nv.host1x", "tegra_host1x",         "host1x", &tegra_clk_host1x, NULL,  0, 0),
8461         SHARED_CLK("vi.host1x", "tegra_vi",             "host1x", &tegra_clk_host1x, NULL,  0, 0),
8462         SHARED_LIMIT("cap.host1x", "cap.host1x",        NULL,     &tegra_clk_host1x, NULL,  0, SHARED_CEILING),
8463         SHARED_LIMIT("cap.vcore.host1x", "cap.vcore.host1x", NULL, &tegra_clk_host1x, NULL,  0, SHARED_CEILING),
8464         SHARED_LIMIT("floor.host1x", "floor.host1x",    NULL,     &tegra_clk_host1x, NULL,  0, 0),
8465         SHARED_CLK("override.host1x", "override.host1x", NULL,    &tegra_clk_host1x, NULL,  0, SHARED_OVERRIDE),
8466
8467         SHARED_CLK("cpu.mselect",         "cpu",        "mselect",   &tegra_clk_mselect, NULL,  0, 0),
8468         SHARED_CLK("pcie.mselect",        "tegra_pcie", "mselect",   &tegra_clk_mselect, NULL,  0, 0),
8469         SHARED_CLK("automotive.mselect",  "automotive", "mselect",   &tegra_clk_mselect, NULL,  0, 0),
8470         SHARED_LIMIT("cap.vcore.mselect", "cap.vcore.mselect", NULL, &tegra_clk_mselect, NULL,  0, SHARED_CEILING),
8471         SHARED_CLK("override.mselect",    "override.mselect",  NULL, &tegra_clk_mselect, NULL,  0, SHARED_OVERRIDE),
8472 };
8473
8474 /* VI, ISP buses */
8475 static struct clk tegra_visp_clks[] = {
8476         SHARED_CONNECT("vi.c4bus",      "vi.c4bus",     NULL,   &tegra_clk_c4bus,   "vi",    0, 0),
8477         SHARED_CONNECT("isp.c4bus",     "isp.c4bus",    NULL,   &tegra_clk_c4bus,   "isp",   0, 0),
8478         SHARED_CLK("override.c4bus",    "override.c4bus", NULL, &tegra_clk_c4bus,    NULL,   0, SHARED_OVERRIDE),
8479
8480         SHARED_CLK("via.vi.c4bus",      "via.vi",       NULL,   &tegra_visp_clks[0], NULL,   0, 0),
8481         SHARED_CLK("vib.vi.c4bus",      "vib.vi",       NULL,   &tegra_visp_clks[0], NULL,   0, 0),
8482
8483         SHARED_CLK("ispa.isp.c4bus",    "ispa.isp",     NULL,   &tegra_visp_clks[1], "ispa", 0, 0),
8484         SHARED_CLK("ispb.isp.c4bus",    "ispb.isp",     NULL,   &tegra_visp_clks[1], "ispb", 0, 0),
8485 };
8486
8487 /* XUSB clocks */
8488 #define XUSB_ID "tegra-xhci"
8489 /* xusb common clock gate - enabled on init and never disabled */
8490 static void tegra12_xusb_gate_clk_init(struct clk *c)
8491 {
8492         tegra12_periph_clk_enable(c);
8493 }
8494
8495 static struct clk_ops tegra_xusb_gate_clk_ops = {
8496         .init    = tegra12_xusb_gate_clk_init,
8497 };
8498
8499 static struct clk tegra_clk_xusb_gate = {
8500         .name      = "xusb_gate",
8501         .flags     = ENABLE_ON_INIT | PERIPH_NO_RESET,
8502         .ops       = &tegra_xusb_gate_clk_ops,
8503         .rate      = 12000000,
8504         .max_rate  = 48000000,
8505         .u.periph = {
8506                 .clk_num   = 143,
8507         },
8508 };
8509
8510 static struct clk tegra_xusb_source_clks[] = {
8511         PERIPH_CLK("xusb_host_src",     XUSB_ID, "host_src",    143,    0x600,  112000000, mux_clkm_pllp_pllc_pllre,    MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB),
8512         PERIPH_CLK("xusb_falcon_src",   XUSB_ID, "falcon_src",  143,    0x604,  336000000, mux_clkm_pllp_pllc_pllre,    MUX | DIV_U71 | PERIPH_NO_RESET),
8513         PERIPH_CLK("xusb_fs_src",       XUSB_ID, "fs_src",      143,    0x608,   48000000, mux_clkm_48M_pllp_480M,      MUX | DIV_U71 | PERIPH_NO_RESET),
8514         PERIPH_CLK("xusb_ss_src",       XUSB_ID, "ss_src",      143,    0x610,  120000000, mux_clkm_pllre_clk32_480M_pllc_ref,  MUX | DIV_U71 | PERIPH_NO_RESET),
8515         PERIPH_CLK("xusb_dev_src",      XUSB_ID, "dev_src",     95,     0x60c,  112000000, mux_clkm_pllp_pllc_pllre,    MUX | DIV_U71 | PERIPH_NO_RESET | PERIPH_ON_APB),
8516         SHARED_EMC_CLK("xusb.emc",      XUSB_ID, "emc", &tegra_clk_emc, NULL,   0,      SHARED_BW, 0),
8517 };
8518
8519 static struct clk tegra_xusb_ss_div2 = {
8520         .name      = "xusb_ss_div2",
8521         .ops       = &tegra_clk_m_div_ops,
8522         .parent    = &tegra_xusb_source_clks[3],
8523         .mul       = 1,
8524         .div       = 2,
8525         .state     = OFF,
8526         .max_rate  = 61200000,
8527 };
8528
8529 static struct clk_mux_sel mux_ss_div2_pllu_60M[] = {
8530         { .input = &tegra_xusb_ss_div2, .value = 0},
8531         { .input = &tegra_pll_u_60M,    .value = 1},
8532         { 0, 0},
8533 };
8534
8535 static struct clk tegra_xusb_hs_src = {
8536         .name      = "xusb_hs_src",
8537         .lookup    = {
8538                 .dev_id    = XUSB_ID,
8539                 .con_id    = "hs_src",
8540         },
8541         .ops       = &tegra_periph_clk_ops,
8542         .reg       = 0x610,
8543         .inputs    = mux_ss_div2_pllu_60M,
8544         .flags     = MUX | PLLU | PERIPH_NO_ENB,
8545         .max_rate  = 60000000,
8546         .u.periph = {
8547                 .src_mask  = 0x1 << 25,
8548                 .src_shift = 25,
8549         },
8550 };
8551
8552 static struct clk_mux_sel mux_xusb_host[] = {
8553         { .input = &tegra_xusb_source_clks[0], .value = 0},
8554         { .input = &tegra_xusb_source_clks[1], .value = 1},
8555         { .input = &tegra_xusb_source_clks[2], .value = 2},
8556         { .input = &tegra_xusb_hs_src,         .value = 5},
8557         { 0, 0},
8558 };
8559
8560 static struct clk_mux_sel mux_xusb_ss[] = {
8561         { .input = &tegra_xusb_source_clks[3], .value = 3},
8562         { .input = &tegra_xusb_source_clks[0], .value = 0},
8563         { .input = &tegra_xusb_source_clks[1], .value = 1},
8564         { 0, 0},
8565 };
8566
8567 static struct clk_mux_sel mux_xusb_dev[] = {
8568         { .input = &tegra_xusb_source_clks[4], .value = 4},
8569         { .input = &tegra_xusb_source_clks[2], .value = 2},
8570         { .input = &tegra_xusb_source_clks[3], .value = 3},
8571         { 0, 0},
8572 };
8573
8574 static struct clk tegra_xusb_coupled_clks[] = {
8575         PERIPH_CLK_EX("xusb_host", XUSB_ID, "host", 89, 0, 350000000, mux_xusb_host, 0, &tegra_clk_coupled_gate_ops),
8576         PERIPH_CLK_EX("xusb_ss",   XUSB_ID, "ss",  156, 0, 350000000, mux_xusb_ss,   0, &tegra_clk_coupled_gate_ops),
8577         PERIPH_CLK_EX("xusb_dev",  XUSB_ID, "dev",  95, 0, 120000000, mux_xusb_dev,  0, &tegra_clk_coupled_gate_ops),
8578 };
8579
8580 #define CLK_DUPLICATE(_name, _dev, _con)                \
8581         {                                               \
8582                 .name   = _name,                        \
8583                 .lookup = {                             \
8584                         .dev_id = _dev,                 \
8585                         .con_id         = _con,         \
8586                 },                                      \
8587         }
8588
8589 /* Some clocks may be used by different drivers depending on the board
8590  * configuration.  List those here to register them twice in the clock lookup
8591  * table under two names.
8592  */
8593 struct clk_duplicate tegra_clk_duplicates[] = {
8594         CLK_DUPLICATE("uarta",  "serial8250.0", NULL),
8595         CLK_DUPLICATE("uartb",  "serial8250.1", NULL),
8596         CLK_DUPLICATE("uartc",  "serial8250.2", NULL),
8597         CLK_DUPLICATE("uartd",  "serial8250.3", NULL),
8598         CLK_DUPLICATE("usbd", XUSB_ID, "utmip-pad"),
8599         CLK_DUPLICATE("usbd", "utmip-pad", NULL),
8600         CLK_DUPLICATE("usbd", "tegra-ehci.0", NULL),
8601         CLK_DUPLICATE("usbd", "tegra-otg", NULL),
8602         CLK_DUPLICATE("disp1", "tegra_dc_dsi_vs1.0", NULL),
8603         CLK_DUPLICATE("disp1.emc", "tegra_dc_dsi_vs1.0", "emc"),
8604         CLK_DUPLICATE("disp1", "tegra_dc_dsi_vs1.1", NULL),
8605         CLK_DUPLICATE("disp1.emc", "tegra_dc_dsi_vs1.1", "emc"),
8606         CLK_DUPLICATE("hdmi", "tegradc.0", "hdmi"),
8607         CLK_DUPLICATE("hdmi", "tegradc.1", "hdmi"),
8608         CLK_DUPLICATE("dsib", "tegradc.0", "dsib"),
8609         CLK_DUPLICATE("dsia", "tegradc.1", "dsia"),
8610         CLK_DUPLICATE("dsiblp", "tegradc.0", "dsiblp"),
8611         CLK_DUPLICATE("dsialp", "tegradc.1", "dsialp"),
8612         CLK_DUPLICATE("dsia", "tegra_dc_dsi_vs1.0", "dsia"),
8613         CLK_DUPLICATE("dsia", "tegra_dc_dsi_vs1.1", "dsia"),
8614         CLK_DUPLICATE("dsialp", "tegra_dc_dsi_vs1.0", "dsialp"),
8615         CLK_DUPLICATE("dsialp", "tegra_dc_dsi_vs1.1", "dsialp"),
8616         CLK_DUPLICATE("dsi1-fixed", "tegra_dc_dsi_vs1.0", "dsi-fixed"),
8617         CLK_DUPLICATE("dsi1-fixed", "tegra_dc_dsi_vs1.1", "dsi-fixed"),
8618         CLK_DUPLICATE("cop", "tegra-avp", "cop"),
8619         CLK_DUPLICATE("bsev", "tegra-avp", "bsev"),
8620         CLK_DUPLICATE("cop", "nvavp", "cop"),
8621         CLK_DUPLICATE("bsev", "nvavp", "bsev"),
8622         CLK_DUPLICATE("vde", "tegra-aes", "vde"),
8623         CLK_DUPLICATE("bsea", "tegra-aes", "bsea"),
8624         CLK_DUPLICATE("bsea", "nvavp", "bsea"),
8625         CLK_DUPLICATE("cml1", "tegra_sata_cml", NULL),
8626         CLK_DUPLICATE("cml0", "tegra_pcie", "cml"),
8627         CLK_DUPLICATE("pciex", "tegra_pcie", "pciex"),
8628         CLK_DUPLICATE("clk_m", NULL, "apb_pclk"),
8629         CLK_DUPLICATE("i2c1", "tegra-i2c-slave.0", NULL),
8630         CLK_DUPLICATE("i2c2", "tegra-i2c-slave.1", NULL),
8631         CLK_DUPLICATE("i2c3", "tegra-i2c-slave.2", NULL),
8632         CLK_DUPLICATE("i2c4", "tegra-i2c-slave.3", NULL),
8633         CLK_DUPLICATE("i2c5", "tegra-i2c-slave.4", NULL),
8634         CLK_DUPLICATE("cl_dvfs_ref", "tegra12-i2c.4", NULL),
8635         CLK_DUPLICATE("cl_dvfs_soc", "tegra12-i2c.4", NULL),
8636         CLK_DUPLICATE("sbc1", "tegra11-spi-slave.0", NULL),
8637         CLK_DUPLICATE("sbc2", "tegra11-spi-slave.1", NULL),
8638         CLK_DUPLICATE("sbc3", "tegra11-spi-slave.2", NULL),
8639         CLK_DUPLICATE("sbc4", "tegra11-spi-slave.3", NULL),
8640         CLK_DUPLICATE("sbc5", "tegra11-spi-slave.4", NULL),
8641         CLK_DUPLICATE("sbc6", "tegra11-spi-slave.5", NULL),
8642         CLK_DUPLICATE("vcp", "nvavp", "vcp"),
8643         CLK_DUPLICATE("avp.sclk", "nvavp", "sclk"),
8644         CLK_DUPLICATE("avp.emc", "nvavp", "emc"),
8645         CLK_DUPLICATE("vde.cbus", "nvavp", "vde"),
8646         CLK_DUPLICATE("i2c5", "tegra_cl_dvfs", "i2c"),
8647         CLK_DUPLICATE("cpu_g", "tegra_cl_dvfs", "safe_dvfs"),
8648         CLK_DUPLICATE("actmon", "tegra_host1x", "actmon"),
8649         CLK_DUPLICATE("gpu_ref", "tegra_gk20a.0", "PLLG_ref"),
8650         CLK_DUPLICATE("gbus", "tegra_gk20a.0", "PLLG_out"),
8651         CLK_DUPLICATE("pll_p_out5", "tegra_gk20a.0", "pwr"),
8652         CLK_DUPLICATE("ispa.isp.c4bus", "tegra_isp.0", "isp"),
8653         CLK_DUPLICATE("ispb.isp.c4bus", "tegra_isp.1", "isp"),
8654         CLK_DUPLICATE("via.vi.c4bus", "tegra_vi.0", "vi"),
8655         CLK_DUPLICATE("vib.vi.c4bus", "tegra_vi.1", "vi"),
8656         CLK_DUPLICATE("csi", "tegra_vi.0", "csi"),
8657         CLK_DUPLICATE("csi", "tegra_vi.1", "csi"),
8658         CLK_DUPLICATE("csus", "tegra_vi.0", "csus"),
8659         CLK_DUPLICATE("vim2_clk", "tegra_vi.1", "vim2_clk"),
8660         CLK_DUPLICATE("cilab", "tegra_vi.0", "cilab"),
8661         CLK_DUPLICATE("cilcd", "tegra_vi.1", "cilcd"),
8662         CLK_DUPLICATE("cile", "tegra_vi.1", "cile"),
8663         CLK_DUPLICATE("i2s0", NULL, "i2s0"),
8664         CLK_DUPLICATE("i2s1", NULL, "i2s1"),
8665         CLK_DUPLICATE("i2s2", NULL, "i2s2"),
8666         CLK_DUPLICATE("i2s3", NULL, "i2s3"),
8667         CLK_DUPLICATE("i2s4", NULL, "i2s4"),
8668         CLK_DUPLICATE("dam0", NULL, "dam0"),
8669         CLK_DUPLICATE("dam1", NULL, "dam1"),
8670         CLK_DUPLICATE("dam2", NULL, "dam2"),
8671         CLK_DUPLICATE("spdif_in", NULL, "spdif_in"),
8672         CLK_DUPLICATE("mclk", NULL, "default_mclk"),
8673         CLK_DUPLICATE("amx", NULL, "amx"),
8674         CLK_DUPLICATE("amx1", NULL, "amx1"),
8675         CLK_DUPLICATE("adx", NULL, "adx"),
8676         CLK_DUPLICATE("adx1", NULL, "adx1"),
8677         CLK_DUPLICATE("afc0", NULL, "afc0"),
8678         CLK_DUPLICATE("afc1", NULL, "afc1"),
8679         CLK_DUPLICATE("afc2", NULL, "afc2"),
8680         CLK_DUPLICATE("afc3", NULL, "afc3"),
8681         CLK_DUPLICATE("afc4", NULL, "afc4"),
8682         CLK_DUPLICATE("afc5", NULL, "afc5"),
8683         CLK_DUPLICATE("amx", "tegra124-amx.0", NULL),
8684         CLK_DUPLICATE("amx1", "tegra124-amx.1", NULL),
8685         CLK_DUPLICATE("adx", "tegra124-adx.0", NULL),
8686         CLK_DUPLICATE("adx1", "tegra124-adx.1", NULL),
8687         CLK_DUPLICATE("amx", "tegra30-ahub-apbif", "amx"),
8688         CLK_DUPLICATE("amx1", "tegra30-ahub-apbif", "amx1"),
8689         CLK_DUPLICATE("adx", "tegra30-ahub-apbif", "adx"),
8690         CLK_DUPLICATE("adx1", "tegra30-ahub-apbif", "adx1"),
8691         CLK_DUPLICATE("d_audio", "tegra30-ahub-xbar", "d_audio"),
8692         CLK_DUPLICATE("apbif", "tegra30-ahub-apbif", "apbif"),
8693         CLK_DUPLICATE("afc0", "tegra30-ahub-apbif", "afc0"),
8694         CLK_DUPLICATE("afc1", "tegra30-ahub-apbif", "afc1"),
8695         CLK_DUPLICATE("afc2", "tegra30-ahub-apbif", "afc2"),
8696         CLK_DUPLICATE("afc3", "tegra30-ahub-apbif", "afc3"),
8697         CLK_DUPLICATE("afc4", "tegra30-ahub-apbif", "afc4"),
8698         CLK_DUPLICATE("afc5", "tegra30-ahub-apbif", "afc5"),
8699         CLK_DUPLICATE("cpu_g", "tegra_simon", "cpu"),
8700 };
8701
8702 struct clk *tegra_ptr_clks[] = {
8703         &tegra_clk_32k,
8704         &tegra_clk_m,
8705         &tegra_clk_m_div2,
8706         &tegra_clk_m_div4,
8707         &tegra_pll_ref,
8708         &tegra_pll_m,
8709         &tegra_pll_m_out1,
8710         &tegra_pll_c,
8711         &tegra_pll_c_out1,
8712         &tegra_pll_c2,
8713         &tegra_pll_c3,
8714         &tegra_pll_p,
8715         &tegra_pll_p_out1,
8716         &tegra_pll_p_out2,
8717         &tegra_pll_p_out3,
8718         &tegra_pll_p_out4,
8719         &tegra_pll_p_out5,
8720         &tegra_pll_a,
8721         &tegra_pll_a_out0,
8722         &tegra_pll_d,
8723         &tegra_pll_d_out0,
8724         &tegra_clk_xusb_gate,
8725         &tegra_pll_u,
8726         &tegra_pll_u_480M,
8727         &tegra_pll_u_60M,
8728         &tegra_pll_u_48M,
8729         &tegra_pll_u_12M,
8730         &tegra_pll_x,
8731         &tegra_pll_x_out0,
8732         &tegra_dfll_cpu,
8733         &tegra_pll_d2,
8734         &tegra_pll_c4,
8735         &tegra_pll_dp,
8736         &tegra_pll_re_vco,
8737         &tegra_pll_re_out,
8738         &tegra_pll_e,
8739         &tegra_cml0_clk,
8740         &tegra_cml1_clk,
8741         &tegra_pciex_clk,
8742         &tegra_clk_cclk_g,
8743 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8744         &tegra_clk_cclk_lp,
8745 #endif
8746         &tegra_clk_sclk,
8747         &tegra_clk_hclk,
8748         &tegra_clk_pclk,
8749         &tegra_clk_virtual_cpu_g,
8750 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8751         &tegra_clk_virtual_cpu_lp,
8752 #endif
8753         &tegra_clk_cpu_cmplx,
8754         &tegra_clk_blink,
8755         &tegra_clk_cop,
8756         &tegra_clk_sbus_cmplx,
8757         &tegra_clk_ahb,
8758         &tegra_clk_apb,
8759         &tegra_clk_emc,
8760         &tegra_clk_mc,
8761         &tegra_clk_host1x,
8762         &tegra_clk_mselect,
8763 #ifdef CONFIG_TEGRA_DUAL_CBUS
8764         &tegra_clk_c2bus,
8765         &tegra_clk_c3bus,
8766 #else
8767         &tegra_clk_cbus,
8768 #endif
8769         &tegra_clk_gpu,
8770         &tegra_clk_gbus,
8771         &tegra_clk_isp,
8772         &tegra_clk_c4bus,
8773 };
8774
8775 struct clk *tegra_ptr_camera_mclks[] = {
8776         &tegra_camera_mclk,
8777         &tegra_camera_mclk2,
8778 };
8779
8780 /*
8781  * Use this API only when all the clocks are not registered to the clock
8782  * subsystem.
8783  */
8784 static struct clk *query_clk_from_list(char *clk_name)
8785 {
8786         int i;
8787
8788         if (!clk_name)
8789                 return NULL;
8790
8791         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
8792                 if (!strcmp(tegra_list_clks[i].name, clk_name))
8793                         return &tegra_list_clks[i];
8794
8795         return NULL;
8796 }
8797
8798 /*
8799  * Handle special clocks to check if they can be set to safe rate
8800  */
8801 static bool tegra12_periph_is_special_reset(struct clk *c)
8802 {
8803         struct clk *temp;
8804
8805         if (!strcmp(c->name, "isp")) {
8806                 /* Make sure that ispa and ispb are in reset */
8807
8808                 /*
8809                  * Since clocks may not have been registered by this time,
8810                  * so query clock structure directly from the list
8811                  */
8812                 temp = query_clk_from_list("ispa");
8813                 if (!temp)
8814                         return false;
8815
8816                 /* If ispa is not in reset, return false */
8817                 if (!IS_PERIPH_IN_RESET(temp))
8818                         return false;
8819
8820                 temp = query_clk_from_list("ispb");
8821                 if (!temp)
8822                         return false;
8823
8824                 /* If ispb is not in reset, return false */
8825                 if (!IS_PERIPH_IN_RESET(temp))
8826                         return false;
8827
8828                 return true;
8829         }
8830
8831         if (!strcmp(c->name, "vi_sensor") || !strcmp(c->name, "vi_sensor2")) {
8832                 temp = query_clk_from_list("vi");
8833                 if (!temp)
8834                         return false;
8835
8836                 /* If vi is not in reset, return false */
8837                 if (!IS_PERIPH_IN_RESET(temp))
8838                         return false;
8839
8840                 return true;
8841         }
8842
8843
8844         if (!strcmp(c->name, "hdmi_audio")) {
8845                 temp = query_clk_from_list("hdmi");
8846                 if (!temp)
8847                         return false;
8848
8849                 /* If hdmi is not in reset, return false */
8850                 if (!IS_PERIPH_IN_RESET(temp))
8851                         return false;
8852
8853                 return true;
8854         }
8855
8856         return false;
8857 }
8858
8859 /* Return true from this function if the target rate can be locked without
8860    switching pll clients to back-up source */
8861 static bool tegra12_is_dyn_ramp(
8862         struct clk *c, unsigned long rate, bool from_vco_min)
8863 {
8864 #if PLLCX_USE_DYN_RAMP
8865         /* PLLC2, PLLC3 support dynamic ramp only when output divider <= 8 */
8866         if ((c == &tegra_pll_c2) || (c == &tegra_pll_c3)) {
8867                 struct clk_pll_freq_table cfg, old_cfg;
8868                 unsigned long input_rate = clk_get_rate(c->parent);
8869
8870                 u32 val = clk_readl(c->reg + PLL_BASE);
8871                 PLL_BASE_PARSE(PLLCX, old_cfg, val);
8872                 old_cfg.p = pllcx_p[old_cfg.p];
8873
8874                 if (!pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, NULL)) {
8875                         if ((cfg.n == old_cfg.n) ||
8876                             PLLCX_IS_DYN(cfg.p, old_cfg.p))
8877                                 return true;
8878                 }
8879         }
8880 #endif
8881
8882 #if PLLXC_USE_DYN_RAMP
8883         /* PPLX, PLLC support dynamic ramp when changing NDIV only */
8884         if ((c == &tegra_pll_x) || (c == &tegra_pll_c)) {
8885                 struct clk_pll_freq_table cfg, old_cfg;
8886                 unsigned long input_rate = clk_get_rate(c->parent);
8887
8888                 if (from_vco_min) {
8889                         old_cfg.m = PLL_FIXED_MDIV(c, input_rate);
8890                         old_cfg.p = 1;
8891                 } else {
8892                         if (c->flags & PLLX)
8893                                 u32 val = clk_readlx(c->reg + PLL_BASE);
8894                         else
8895                                 u32 val = clk_readl(c->reg + PLL_BASE);
8896                         PLL_BASE_PARSE(PLLXC, old_cfg, val);
8897                         old_cfg.p = pllxc_p[old_cfg.p];
8898                 }
8899
8900                 if (!pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, NULL)) {
8901                         if ((cfg.m == old_cfg.m) && (cfg.p == old_cfg.p))
8902                                 return true;
8903                 }
8904         }
8905 #endif
8906         return false;
8907 }
8908
8909 /* DFLL late init called with CPU clock lock taken */
8910 static void __init tegra12_dfll_cpu_late_init(struct clk *c)
8911 {
8912 #ifdef CONFIG_ARCH_TEGRA_HAS_CL_DVFS
8913         int ret;
8914         struct clk *cpu = &tegra_clk_virtual_cpu_g;
8915
8916         if (!cpu || !cpu->dvfs) {
8917                 pr_err("%s: CPU dvfs is not present\n", __func__);
8918                 return;
8919         }
8920         tegra_dvfs_set_dfll_tune_trimmers(cpu->dvfs, tune_cpu_trimmers);
8921
8922         /* release dfll clock source reset, init cl_dvfs control logic, and
8923            move dfll to initialized state, so it can be used as CPU source */
8924         tegra_periph_reset_deassert(c);
8925         ret = tegra_init_cl_dvfs();
8926         if (!ret) {
8927                 c->state = OFF;
8928                 if (tegra_platform_is_silicon()) {
8929                         use_dfll = CONFIG_TEGRA_USE_DFLL_RANGE;
8930 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
8931                         if (tegra_cpu_speedo_id() == 0)
8932                                 use_dfll = 0;
8933 #endif
8934                 }
8935                 tegra_dvfs_set_dfll_range(cpu->dvfs, use_dfll);
8936                 tegra_cl_dvfs_debug_init(c);
8937                 pr_info("Tegra CPU DFLL is initialized with use_dfll = %d\n",
8938                         use_dfll);
8939         }
8940 #endif
8941 }
8942
8943 /*
8944  * Backup pll is used as transitional CPU clock source while main pll is
8945  * relocking; in addition all CPU rates below backup level are sourced from
8946  * backup pll only. Target backup levels for each CPU mode are selected high
8947  * enough to avoid voltage droop when CPU clock is switched between backup and
8948  * main plls. Actual backup rates will be rounded to match backup source fixed
8949  * frequency. Backup rates are also used as stay-on-backup thresholds, and must
8950  * be kept the same in G and LP mode (will need to add a separate stay-on-backup
8951  * parameter to allow different backup rates if necessary).
8952  *
8953  * Sbus threshold must be exact factor of pll_p rate.
8954  */
8955 #define CPU_G_BACKUP_RATE_TARGET        200000000
8956 #define CPU_LP_BACKUP_RATE_TARGET       200000000
8957
8958 static void tegra12_pllp_init_dependencies(unsigned long pllp_rate)
8959 {
8960 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8961         u32 div;
8962         unsigned long backup_rate;
8963 #endif
8964
8965         switch (pllp_rate) {
8966         case 216000000:
8967                 tegra_pll_p_out1.u.pll_div.default_rate = 28800000;
8968                 tegra_pll_p_out3.u.pll_div.default_rate = 72000000;
8969                 tegra_clk_sbus_cmplx.u.system.threshold = 108000000;
8970                 tegra_clk_host1x.u.periph.threshold = 108000000;
8971                 break;
8972         case 408000000:
8973                 tegra_pll_p_out1.u.pll_div.default_rate = 9600000;
8974                 tegra_pll_p_out3.u.pll_div.default_rate = 102000000;
8975                 tegra_clk_sbus_cmplx.u.system.threshold = 204000000;
8976                 tegra_clk_host1x.u.periph.threshold = 204000000;
8977                 break;
8978         case 204000000:
8979                 tegra_pll_p_out1.u.pll_div.default_rate = 4800000;
8980                 tegra_pll_p_out3.u.pll_div.default_rate = 102000000;
8981                 tegra_clk_sbus_cmplx.u.system.threshold = 204000000;
8982                 tegra_clk_host1x.u.periph.threshold = 204000000;
8983                 break;
8984         default:
8985                 pr_err("tegra: PLLP rate: %lu is not supported\n", pllp_rate);
8986                 BUG();
8987         }
8988         pr_info("tegra: PLLP fixed rate: %lu\n", pllp_rate);
8989
8990 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8991         div = pllp_rate / CPU_G_BACKUP_RATE_TARGET;
8992         backup_rate = pllp_rate / div;
8993         tegra_clk_virtual_cpu_g.u.cpu.backup_rate = backup_rate;
8994
8995         div = pllp_rate / CPU_LP_BACKUP_RATE_TARGET;
8996         backup_rate = pllp_rate / div;
8997         tegra_clk_virtual_cpu_lp.u.cpu.backup_rate = backup_rate;
8998 #else
8999         tegra_clk_virtual_cpu_g.u.cpu.backup_rate = pllp_rate;
9000 #endif
9001 }
9002
9003 static void tegra12_init_one_clock(struct clk *c)
9004 {
9005         clk_init(c);
9006         INIT_LIST_HEAD(&c->shared_bus_list);
9007         if (!c->lookup.dev_id && !c->lookup.con_id)
9008                 c->lookup.con_id = c->name;
9009         c->lookup.clk = c;
9010         clkdev_add(&c->lookup);
9011 }
9012
9013 /* Direct access to CPU clock sources fot CPU idle driver */
9014 int tegra12_cpu_g_idle_rate_exchange(unsigned long *rate)
9015 {
9016         int ret = 0;
9017         struct clk *dfll = tegra_clk_cpu_cmplx.parent->u.cpu.dynamic;
9018         unsigned long old_rate, new_rate, flags;
9019
9020         if (!dfll || !tegra_dvfs_rail_is_dfll_mode(tegra_cpu_rail))
9021                 return -EPERM;
9022
9023         /* Clipping min to oscillator rate is pretty much arbitrary */
9024         new_rate = max(*rate, tegra_clk_m.rate);
9025
9026         clk_lock_save(dfll, &flags);
9027
9028         old_rate = clk_get_rate_locked(dfll);
9029         *rate = old_rate;
9030         if (new_rate != old_rate)
9031                 ret = clk_set_rate_locked(dfll, new_rate);
9032
9033         clk_unlock_restore(dfll, &flags);
9034         return ret;
9035 }
9036
9037 int tegra12_cpu_lp_idle_rate_exchange(unsigned long *rate)
9038 {
9039         int ret = 0;
9040         struct clk *backup = tegra_clk_cpu_cmplx.parent->u.cpu.backup;
9041         unsigned long old_rate, flags;
9042         unsigned long new_rate = min(
9043                 *rate, tegra_clk_cpu_cmplx.parent->u.cpu.backup_rate);
9044
9045         clk_lock_save(backup, &flags);
9046
9047         old_rate = clk_get_rate_locked(backup);
9048         *rate = old_rate;
9049         if (new_rate != old_rate)
9050                 ret = clk_set_rate_locked(backup, new_rate);
9051
9052         clk_unlock_restore(backup, &flags);
9053         return ret;
9054 }
9055
9056 void tegra_edp_throttle_cpu_now(u8 factor)
9057 {
9058         /* empty definition for tegra12 */
9059         return;
9060 }
9061
9062 bool tegra_clk_is_parent_allowed(struct clk *c, struct clk *p)
9063 {
9064         /*
9065          * Most of the Tegra12 multimedia and peripheral muxes include pll_c2
9066          * and pll_c3 as possible inputs. However, per clock policy these plls
9067          * are allowed to be used only by handful devices aggregated on cbus.
9068          * For all others, instead of enforcing policy at run-time in this
9069          * function, we simply stripped out pll_c2 and pll_c3 options from the
9070          * respective muxes statically.
9071          */
9072
9073         /*
9074          * In configuration with dual cbus pll_c can be used as a scaled clock
9075          * source for EMC only when pll_m is fixed, or as a general fixed rate
9076          * clock source for EMC and other peripherals if pll_m is scaled. In
9077          * configuration with single cbus pll_c can be used as a scaled cbus
9078          * clock source only. No direct use for pll_c by super clocks.
9079          */
9080         if ((p == &tegra_pll_c) && (c != &tegra_pll_c_out1)) {
9081                 if (c->ops == &tegra_super_ops)
9082                         return false;
9083 #ifdef CONFIG_TEGRA_DUAL_CBUS
9084 #ifndef CONFIG_TEGRA_PLLM_SCALED
9085                 return c->flags & PERIPH_EMC_ENB;
9086 #endif
9087 #else
9088                 return c->flags & PERIPH_ON_CBUS;
9089 #endif
9090         }
9091
9092         /*
9093          * In any configuration pll_m must not be used as a clock source for
9094          * cbus modules. If pll_m is scaled it can be used as EMC source only.
9095          * Otherwise fixed rate pll_m can be used as clock source for EMC and
9096          * other peripherals. No direct use for pll_m by super clocks.
9097          */
9098         if ((p == &tegra_pll_m) && (c != &tegra_pll_m_out1)) {
9099                 if (c->ops == &tegra_super_ops)
9100                         return false;
9101
9102                 if (c->flags & PERIPH_ON_CBUS)
9103                         return false;
9104 #ifdef CONFIG_TEGRA_PLLM_SCALED
9105                 return c->flags & PERIPH_EMC_ENB;
9106 #endif
9107         }
9108         return true;
9109 }
9110
9111 /* Internal LA may request some clocks to be enabled on init via TRANSACTION
9112    SCRATCH register settings */
9113 void __init tegra12x_clk_init_la(void)
9114 {
9115         struct clk *c;
9116         u32 reg = readl((void *)
9117                 ((uintptr_t)misc_gp_base + MISC_GP_TRANSACTOR_SCRATCH_0));
9118
9119         if (!(reg & MISC_GP_TRANSACTOR_SCRATCH_LA_ENABLE))
9120                 return;
9121
9122         c = tegra_get_clock_by_name("la");
9123         if (WARN(!c, "%s: could not find la clk\n", __func__))
9124                 return;
9125         clk_enable(c);
9126
9127         if (reg & MISC_GP_TRANSACTOR_SCRATCH_DDS_ENABLE) {
9128                 c = tegra_get_clock_by_name("dds");
9129                 if (WARN(!c, "%s: could not find la clk\n", __func__))
9130                         return;
9131                 clk_enable(c);
9132         }
9133         if (reg & MISC_GP_TRANSACTOR_SCRATCH_DP2_ENABLE) {
9134                 c = tegra_get_clock_by_name("dp2");
9135                 if (WARN(!c, "%s: could not find la clk\n", __func__))
9136                         return;
9137                 clk_enable(c);
9138
9139                 c = tegra_get_clock_by_name("hdmi");
9140                 if (WARN(!c, "%s: could not find la clk\n", __func__))
9141                         return;
9142                 clk_enable(c);
9143         }
9144 }
9145
9146 #ifdef CONFIG_CPU_FREQ
9147
9148 /*
9149  * Frequency table index must be sequential starting at 0 and frequencies
9150  * must be ascending.
9151  */
9152 #define CPU_FREQ_STEP 102000 /* 102MHz cpu_g table step */
9153 #define CPU_FREQ_TABLE_MAX_SIZE (2 * MAX_DVFS_FREQS + 1)
9154
9155 static struct cpufreq_frequency_table freq_table[CPU_FREQ_TABLE_MAX_SIZE];
9156 static struct tegra_cpufreq_table_data freq_table_data;
9157
9158 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
9159 struct tegra_cpufreq_table_data *tegra_cpufreq_table_get(void)
9160 {
9161         int i, j;
9162         bool g_vmin_done = false;
9163         unsigned int freq, lp_backup_freq, g_vmin_freq, g_start_freq, max_freq;
9164         struct clk *cpu_clk_g = tegra_get_clock_by_name("cpu_g");
9165         struct clk *cpu_clk_lp = tegra_get_clock_by_name("cpu_lp");
9166
9167         /* Initialize once */
9168         if (freq_table_data.freq_table)
9169                 return &freq_table_data;
9170
9171         /* Clean table */
9172         for (i = 0; i < CPU_FREQ_TABLE_MAX_SIZE; i++) {
9173                 freq_table[i].index = i;
9174                 freq_table[i].frequency = CPUFREQ_TABLE_END;
9175         }
9176
9177         lp_backup_freq = cpu_clk_lp->u.cpu.backup_rate / 1000;
9178         if (!lp_backup_freq) {
9179                 WARN(1, "%s: cannot make cpufreq table: no LP CPU backup rate\n",
9180                      __func__);
9181                 return NULL;
9182         }
9183         if (!cpu_clk_lp->dvfs) {
9184                 WARN(1, "%s: cannot make cpufreq table: no LP CPU dvfs\n",
9185                      __func__);
9186                 return NULL;
9187         }
9188         if (!cpu_clk_g->dvfs) {
9189                 WARN(1, "%s: cannot make cpufreq table: no G CPU dvfs\n",
9190                      __func__);
9191                 return NULL;
9192         }
9193         g_vmin_freq = cpu_clk_g->dvfs->freqs[0] / 1000;
9194         if (g_vmin_freq < lp_backup_freq) {
9195                 WARN(1, "%s: cannot make cpufreq table: LP CPU backup rate"
9196                         " exceeds G CPU rate at Vmin\n", __func__);
9197                 return NULL;
9198         }
9199         /* Avoid duplicate frequency if g_vim_freq is already part of table */
9200         if (g_vmin_freq == lp_backup_freq)
9201                 g_vmin_done = true;
9202
9203         /* Start with backup frequencies */
9204         i = 0;
9205         freq = lp_backup_freq;
9206         freq_table[i++].frequency = freq/4;
9207         freq_table[i++].frequency = freq/2;
9208         freq_table[i++].frequency = freq;
9209
9210         /* Throttle low index at backup level*/
9211         freq_table_data.throttle_lowest_index = i - 1;
9212
9213         /*
9214          * Next, set table steps along LP CPU dvfs ladder, but make sure G CPU
9215          * dvfs rate at minimum voltage is not missed (if it happens to be below
9216          * LP maximum rate)
9217          */
9218         max_freq = cpu_clk_lp->max_rate / 1000;
9219         for (j = 0; j < cpu_clk_lp->dvfs->num_freqs; j++) {
9220                 freq = cpu_clk_lp->dvfs->freqs[j] / 1000;
9221                 if (freq <= lp_backup_freq)
9222                         continue;
9223
9224                 if (!g_vmin_done && (freq >= g_vmin_freq)) {
9225                         g_vmin_done = true;
9226                         if (freq > g_vmin_freq)
9227                                 freq_table[i++].frequency = g_vmin_freq;
9228                 }
9229                 freq_table[i++].frequency = freq;
9230
9231                 if (freq == max_freq)
9232                         break;
9233         }
9234
9235         /* Set G CPU min rate at least one table step below LP maximum */
9236         cpu_clk_g->min_rate = min(freq_table[i-2].frequency, g_vmin_freq)*1000;
9237
9238         /* Suspend index at max LP CPU */
9239         freq_table_data.suspend_index = i - 1;
9240
9241         /* Fill in "hole" (if any) between LP CPU maximum rate and G CPU dvfs
9242            ladder rate at minimum voltage */
9243         if (freq < g_vmin_freq) {
9244                 int n = (g_vmin_freq - freq) / CPU_FREQ_STEP;
9245                 for (j = 0; j <= n; j++) {
9246                         freq = g_vmin_freq - CPU_FREQ_STEP * (n - j);
9247                         freq_table[i++].frequency = freq;
9248                 }
9249         }
9250
9251         /* Now, step along the rest of G CPU dvfs ladder */
9252         g_start_freq = freq;
9253         max_freq = cpu_clk_g->max_rate / 1000;
9254         for (j = 0; j < cpu_clk_g->dvfs->num_freqs; j++) {
9255                 freq = cpu_clk_g->dvfs->freqs[j] / 1000;
9256                 if (freq > g_start_freq)
9257                         freq_table[i++].frequency = freq;
9258                 if (freq == max_freq)
9259                         break;
9260         }
9261
9262         /* Throttle high index one step below maximum */
9263         BUG_ON(i >= CPU_FREQ_TABLE_MAX_SIZE);
9264         freq_table_data.throttle_highest_index = i - 2;
9265         freq_table_data.freq_table = freq_table;
9266         return &freq_table_data;
9267 }
9268
9269 #else
9270
9271 #define GRANULARITY_KHZ   25500
9272 #define GRANULARITY_END 1020000
9273 #define CPU_THROTTLE_FREQ 408000
9274 #define CPU_SUSPEND_FREQ  408000
9275
9276 struct tegra_cpufreq_table_data *tegra_cpufreq_table_get(void)
9277 {
9278         int i, j;
9279         unsigned int freq, max_freq, cpu_min_freq;
9280         struct clk *cpu_clk_g = tegra_get_clock_by_name("cpu_g");
9281
9282         /* Initialize once */
9283         if (freq_table_data.freq_table)
9284                 return &freq_table_data;
9285
9286         /* Clean table */
9287         for (i = 0; i < CPU_FREQ_TABLE_MAX_SIZE; i++) {
9288                 freq_table[i].index = i;
9289                 freq_table[i].frequency = CPUFREQ_TABLE_END;
9290         }
9291
9292         if (!cpu_clk_g->dvfs) {
9293                 WARN(1, "%s: cannot make cpufreq table: no CPU dvfs\n",
9294                      __func__);
9295                 return NULL;
9296         }
9297
9298         cpu_min_freq = 204000;
9299
9300         cpu_clk_g->min_rate = cpu_min_freq*1000;
9301
9302         i = 0;
9303         freq_table[i++].frequency = cpu_min_freq;
9304         for (j=1; j <= (GRANULARITY_END - cpu_min_freq)/GRANULARITY_KHZ; j++)
9305                 freq_table[i++].frequency = cpu_min_freq + j*GRANULARITY_KHZ;
9306
9307         /* Now, step along the rest of G CPU dvfs ladder */
9308         max_freq = cpu_clk_g->max_rate / 1000;
9309         for (j = 0; j < cpu_clk_g->dvfs->num_freqs; j++) {
9310                 freq = cpu_clk_g->dvfs->freqs[j] / 1000;
9311                 if (freq > GRANULARITY_END)
9312                         freq_table[i++].frequency = freq;
9313                 if (freq == max_freq)
9314                         break;
9315         }
9316
9317         freq_table_data.throttle_lowest_index = 0;
9318         freq_table_data.suspend_index = 0;
9319
9320         for (j = 1; j < i; j++) {
9321                 if ((freq_table[j].frequency > CPU_THROTTLE_FREQ) &&
9322                         (freq_table[j-1].frequency <= CPU_THROTTLE_FREQ))
9323                         freq_table_data.throttle_lowest_index = j - 1;
9324                 if ((freq_table[j].frequency > CPU_SUSPEND_FREQ) &&
9325                         (freq_table[j-1].frequency <= CPU_SUSPEND_FREQ))
9326                         freq_table_data.suspend_index = j - 1;
9327         }
9328
9329         /* Throttle high index one step below maximum */
9330         BUG_ON(i >= CPU_FREQ_TABLE_MAX_SIZE);
9331         freq_table_data.throttle_highest_index = i - 2;
9332         freq_table_data.freq_table = freq_table;
9333         return &freq_table_data;
9334 }
9335
9336 #endif
9337
9338 /* EMC/CPU frequency ratio for power/performance optimization */
9339 unsigned long tegra_emc_to_cpu_ratio(unsigned long cpu_rate)
9340 {
9341         static unsigned long emc_max_rate;
9342
9343         if (emc_max_rate == 0)
9344                 emc_max_rate = clk_round_rate(
9345                         tegra_get_clock_by_name("emc"), ULONG_MAX);
9346
9347         /* Vote on memory bus frequency based on cpu frequency;
9348            cpu rate is in kHz, emc rate is in Hz */
9349         if (cpu_rate >= 1300000)
9350                 return emc_max_rate;    /* cpu >= 1.3GHz, emc max */
9351         else if (cpu_rate >= 975000)
9352                 return 550000000;       /* cpu >= 975 MHz, emc 550 MHz */
9353         else if (cpu_rate >= 725000)
9354                 return  350000000;      /* cpu >= 725 MHz, emc 350 MHz */
9355         else if (cpu_rate >= 500000)
9356                 return  150000000;      /* cpu >= 500 MHz, emc 150 MHz */
9357         else if (cpu_rate >= 275000)
9358                 return  50000000;       /* cpu >= 275 MHz, emc 50 MHz */
9359         else
9360                 return 0;               /* emc min */
9361 }
9362
9363 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
9364 /* EMC/CPU frequency operational requirement limit */
9365 unsigned long tegra_emc_cpu_limit(unsigned long cpu_rate)
9366 {
9367         static unsigned long last_emc_rate;
9368         unsigned long emc_rate;
9369
9370         /* Vote on memory bus frequency based on cpu frequency;
9371            cpu rate is in kHz, emc rate is in Hz */
9372
9373         if ((tegra_revision != TEGRA_REVISION_A01) &&
9374             (tegra_revision != TEGRA_REVISION_A02))
9375                 return 0; /* no frequency dependency for A03+ revisions */
9376
9377         if (cpu_rate > 1020000)
9378                 emc_rate = 600000000;   /* cpu > 1.02GHz, emc 600MHz */
9379         else
9380                 emc_rate = 300000000;   /* 300MHz floor always */
9381
9382         /* When going down, allow some time for CPU DFLL to settle */
9383         if (emc_rate < last_emc_rate)
9384                 udelay(200);            /* FIXME: to be characterized */
9385
9386         last_emc_rate = emc_rate;
9387         return emc_rate;
9388 }
9389 #endif
9390
9391 int tegra_update_mselect_rate(unsigned long cpu_rate)
9392 {
9393         static struct clk *mselect; /* statics init to 0 */
9394
9395         unsigned long mselect_rate;
9396
9397         if (!mselect) {
9398                 mselect = tegra_get_clock_by_name("cpu.mselect");
9399                 if (!mselect)
9400                         return -ENODEV;
9401         }
9402
9403         /* Vote on mselect frequency based on cpu frequency:
9404            keep mselect at half of cpu rate up to 102 MHz;
9405            cpu rate is in kHz, mselect rate is in Hz */
9406         mselect_rate = DIV_ROUND_UP(cpu_rate, 2) * 1000;
9407         mselect_rate = min(mselect_rate, 102000000UL);
9408         return clk_set_rate(mselect, mselect_rate);
9409 }
9410 #endif
9411
9412 #ifdef CONFIG_PM_SLEEP
9413 static u32 clk_rst_suspend[RST_DEVICES_NUM + CLK_OUT_ENB_NUM +
9414                            PERIPH_CLK_SOURCE_NUM + 25];
9415
9416 static int tegra12_clk_suspend(void)
9417 {
9418         unsigned long off;
9419         u32 *ctx = clk_rst_suspend;
9420
9421         *ctx++ = clk_readl(OSC_CTRL) & OSC_CTRL_MASK;
9422         *ctx++ = clk_readl(CPU_SOFTRST_CTRL);
9423         *ctx++ = clk_readl(CPU_SOFTRST_CTRL1);
9424         *ctx++ = clk_readl(CPU_SOFTRST_CTRL2);
9425
9426         *ctx++ = clk_readl(tegra_pll_p_out1.reg);
9427         *ctx++ = clk_readl(tegra_pll_p_out3.reg);
9428         *ctx++ = clk_readl(tegra_pll_p_out5.reg);
9429
9430         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_BASE);
9431         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
9432         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_BASE);
9433         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
9434
9435         *ctx++ = clk_readl(tegra_pll_m_out1.reg);
9436         *ctx++ = clk_readl(tegra_pll_a_out0.reg);
9437         *ctx++ = clk_readl(tegra_pll_c_out1.reg);
9438
9439 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
9440         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg);
9441         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
9442 #endif
9443
9444         *ctx++ = clk_readl(tegra_clk_sclk.reg);
9445         *ctx++ = clk_readl(tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
9446         *ctx++ = clk_readl(tegra_clk_pclk.reg);
9447
9448         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_LA;
9449                         off += 4) {
9450                 if (off == PERIPH_CLK_SOURCE_EMC)
9451                         continue;
9452                 *ctx++ = clk_readl(off);
9453         }
9454         for (off = PERIPH_CLK_SOURCE_MSELECT; off <= PERIPH_CLK_SOURCE_SE;
9455                         off+=4) {
9456                 *ctx++ = clk_readl(off);
9457         }
9458         for (off = AUDIO_DLY_CLK; off <= AUDIO_SYNC_CLK_SPDIF; off+=4) {
9459                 *ctx++ = clk_readl(off);
9460         }
9461         for (off = PERIPH_CLK_SOURCE_XUSB_HOST;
9462                 off <= PERIPH_CLK_SOURCE_VIC; off += 4)
9463                 *ctx++ = clk_readl(off);
9464
9465         *ctx++ = clk_readl(RST_DEVICES_L);
9466         *ctx++ = clk_readl(RST_DEVICES_H);
9467         *ctx++ = clk_readl(RST_DEVICES_U);
9468         *ctx++ = clk_readl(RST_DEVICES_V);
9469         *ctx++ = clk_readl(RST_DEVICES_W);
9470         *ctx++ = clk_readl(RST_DEVICES_X);
9471
9472         *ctx++ = clk_readl(CLK_OUT_ENB_L);
9473         *ctx++ = clk_readl(CLK_OUT_ENB_H);
9474         *ctx++ = clk_readl(CLK_OUT_ENB_U);
9475         *ctx++ = clk_readl(CLK_OUT_ENB_V);
9476         *ctx++ = clk_readl(CLK_OUT_ENB_W);
9477         *ctx++ = clk_readl(CLK_OUT_ENB_X);
9478
9479         *ctx++ = clk_readlx(tegra_clk_cclk_g.reg);
9480         *ctx++ = clk_readlx(tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
9481
9482         *ctx++ = clk_readl(SPARE_REG);
9483         *ctx++ = clk_readl(MISC_CLK_ENB);
9484         *ctx++ = clk_readl(CLK_MASK_ARM);
9485
9486         *ctx++ = clk_get_rate_all_locked(&tegra_clk_emc);
9487
9488         pr_debug("%s: suspend entries: %d, suspend array: %u\n", __func__,
9489                 (s32)(ctx - clk_rst_suspend), (u32)ARRAY_SIZE(clk_rst_suspend));
9490         BUG_ON((ctx - clk_rst_suspend) > ARRAY_SIZE(clk_rst_suspend));
9491         return 0;
9492 }
9493
9494 static void tegra12_clk_resume(void)
9495 {
9496         unsigned long off, rate;
9497         const u32 *ctx = clk_rst_suspend;
9498         u32 val;
9499         u32 plla_base;
9500         u32 plld_base;
9501         u32 pll_p_out12, pll_p_out34;
9502         u32 pll_a_out0, pll_m_out1, pll_c_out1;
9503         struct clk *p;
9504
9505         /* FIXME: OSC_CTRL already restored by warm boot code? */
9506         val = clk_readl(OSC_CTRL) & ~OSC_CTRL_MASK;
9507         val |= *ctx++;
9508         clk_writel(val, OSC_CTRL);
9509         clk_writel(*ctx++, CPU_SOFTRST_CTRL);
9510         clk_writel(*ctx++, CPU_SOFTRST_CTRL1);
9511         clk_writel(*ctx++, CPU_SOFTRST_CTRL2);
9512
9513         /* Since we are going to reset devices and switch clock sources in this
9514          * function, plls and secondary dividers is required to be enabled. The
9515          * actual value will be restored back later. Note that boot plls: pllm,
9516          * pllp, and pllu are already configured and enabled
9517          */
9518         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
9519         val |= val << 16;
9520         pll_p_out12 = *ctx++;
9521         clk_writel(pll_p_out12 | val, tegra_pll_p_out1.reg);
9522         pll_p_out34 = *ctx++;
9523         clk_writel(pll_p_out34 | val, tegra_pll_p_out3.reg);
9524
9525         /* Restore as is, GPU is rail-gated, anyway */
9526         clk_writel(*ctx++, tegra_pll_p_out5.reg);
9527
9528         tegra12_pllss_clk_resume_enable(&tegra_pll_c4);
9529         tegra12_pllss_clk_resume_enable(&tegra_pll_d2);
9530         tegra12_pllss_clk_resume_enable(&tegra_pll_dp);
9531         tegra12_pllcx_clk_resume_enable(&tegra_pll_c2);
9532         tegra12_pllcx_clk_resume_enable(&tegra_pll_c3);
9533         tegra12_pllxc_clk_resume_enable(&tegra_pll_c);
9534         tegra12_pllxc_clk_resume_enable(&tegra_pll_x);
9535         tegra12_pllre_clk_resume_enable(&tegra_pll_re_out);
9536
9537         plla_base = *ctx++;
9538         clk_writel(*ctx++, tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
9539         clk_writel(plla_base | PLL_BASE_ENABLE, tegra_pll_a.reg + PLL_BASE);
9540
9541         plld_base = *ctx++;
9542         clk_writel(*ctx++, tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
9543         clk_writel(plld_base | PLL_BASE_ENABLE, tegra_pll_d.reg + PLL_BASE);
9544
9545         udelay(1000);
9546
9547         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
9548         pll_m_out1 = *ctx++;
9549         clk_writel(pll_m_out1 | val, tegra_pll_m_out1.reg);
9550         pll_a_out0 = *ctx++;
9551         clk_writel(pll_a_out0 | val, tegra_pll_a_out0.reg);
9552         pll_c_out1 = *ctx++;
9553         clk_writel(pll_c_out1 | val, tegra_pll_c_out1.reg);
9554
9555 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
9556         val = *ctx++;
9557         tegra12_super_clk_resume(&tegra_clk_cclk_lp,
9558                 tegra_clk_virtual_cpu_lp.u.cpu.backup, val);
9559         clk_writel(*ctx++, tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
9560 #endif
9561
9562         clk_writel(*ctx++, tegra_clk_sclk.reg);
9563         clk_writel(*ctx++, tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
9564         clk_writel(*ctx++, tegra_clk_pclk.reg);
9565
9566         /* enable all clocks before configuring clock sources */
9567         clk_writel(CLK_OUT_ENB_L_RESET_MASK, CLK_OUT_ENB_L);
9568         clk_writel(CLK_OUT_ENB_H_RESET_MASK, CLK_OUT_ENB_H);
9569         clk_writel(CLK_OUT_ENB_U_RESET_MASK, CLK_OUT_ENB_U);
9570         clk_writel(CLK_OUT_ENB_V_RESET_MASK, CLK_OUT_ENB_V);
9571         clk_writel(CLK_OUT_ENB_W_RESET_MASK, CLK_OUT_ENB_W);
9572         clk_writel(CLK_OUT_ENB_X_RESET_MASK, CLK_OUT_ENB_X);
9573         wmb();
9574
9575         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_LA;
9576                         off += 4) {
9577                 if (off == PERIPH_CLK_SOURCE_EMC)
9578                         continue;
9579                 clk_writel(*ctx++, off);
9580         }
9581         for (off = PERIPH_CLK_SOURCE_MSELECT; off <= PERIPH_CLK_SOURCE_SE;
9582                         off += 4) {
9583                 clk_writel(*ctx++, off);
9584         }
9585         for (off = AUDIO_DLY_CLK; off <= AUDIO_SYNC_CLK_SPDIF; off+=4) {
9586                 clk_writel(*ctx++, off);
9587         }
9588         for (off = PERIPH_CLK_SOURCE_XUSB_HOST;
9589                 off <= PERIPH_CLK_SOURCE_VIC; off += 4)
9590                 clk_writel(*ctx++, off);
9591
9592         udelay(RESET_PROPAGATION_DELAY);
9593
9594         clk_writel(*ctx++, RST_DEVICES_L);
9595         clk_writel(*ctx++, RST_DEVICES_H);
9596         clk_writel(*ctx++, RST_DEVICES_U);
9597         clk_writel(*ctx++, RST_DEVICES_V);
9598         clk_writel(*ctx++, RST_DEVICES_W);
9599         clk_writel(*ctx++, RST_DEVICES_X);
9600         wmb();
9601
9602         clk_writel(*ctx++, CLK_OUT_ENB_L);
9603         clk_writel(*ctx++, CLK_OUT_ENB_H);
9604         clk_writel(*ctx++, CLK_OUT_ENB_U);
9605
9606         /* For LP0 resume, clk to lpcpu is required to be on */
9607         val = *ctx++;
9608         val |= CLK_OUT_ENB_V_CLK_ENB_CPULP_EN;
9609         clk_writel(val, CLK_OUT_ENB_V);
9610
9611         clk_writel(*ctx++, CLK_OUT_ENB_W);
9612         clk_writel(*ctx++, CLK_OUT_ENB_X);
9613         wmb();
9614
9615         /* DFLL resume after cl_dvfs and i2c5 clocks are resumed */
9616         tegra12_dfll_clk_resume(&tegra_dfll_cpu);
9617
9618         /* CPU G clock restored after DFLL and PLLs */
9619         clk_writelx(*ctx++, tegra_clk_cclk_g.reg);
9620         clk_writelx(*ctx++, tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
9621
9622         clk_writel(*ctx++, SPARE_REG);
9623         clk_writel(*ctx++, MISC_CLK_ENB);
9624         clk_writel(*ctx++, CLK_MASK_ARM);
9625
9626         /* Restore back the actual pll and secondary divider values */
9627         clk_writel(pll_p_out12, tegra_pll_p_out1.reg);
9628         clk_writel(pll_p_out34, tegra_pll_p_out3.reg);
9629
9630         p = &tegra_pll_c4;
9631         if (p->state == OFF)
9632                 tegra12_pllss_clk_disable(p);
9633         p = &tegra_pll_d2;
9634         if (p->state == OFF)
9635                 tegra12_pllss_clk_disable(p);
9636         p = &tegra_pll_dp;
9637         if (p->state == OFF)
9638                 tegra12_pllss_clk_disable(p);
9639         p = &tegra_pll_c2;
9640         if (p->state == OFF)
9641                 tegra12_pllcx_clk_disable(p);
9642         p = &tegra_pll_c3;
9643         if (p->state == OFF)
9644                 tegra12_pllcx_clk_disable(p);
9645         p = &tegra_pll_c;
9646         if (p->state == OFF)
9647                 tegra12_pllxc_clk_disable(p);
9648         p = &tegra_pll_x;
9649         if (p->state == OFF)
9650                 tegra12_pllxc_clk_disable(p);
9651         p = &tegra_pll_re_vco;
9652         if (p->state == OFF)
9653                 tegra12_pllre_clk_disable(p);
9654
9655         clk_writel(plla_base, tegra_pll_a.reg + PLL_BASE);
9656         clk_writel(plld_base, tegra_pll_d.reg + PLL_BASE);
9657
9658         clk_writel(pll_m_out1, tegra_pll_m_out1.reg);
9659         clk_writel(pll_a_out0, tegra_pll_a_out0.reg);
9660         clk_writel(pll_c_out1, tegra_pll_c_out1.reg);
9661
9662         /* Since EMC clock is not restored, and may not preserve parent across
9663            suspend, update current state, and mark EMC DFS as out of sync */
9664         p = tegra_clk_emc.parent;
9665         tegra12_periph_clk_init(&tegra_clk_emc);
9666
9667         /* Turn Off pll_m if it was OFF before suspend, and emc was not switched
9668            to pll_m across suspend; re-init pll_m to sync s/w and h/w states */
9669         if ((tegra_pll_m.state == OFF) &&
9670             (&tegra_pll_m != tegra_clk_emc.parent))
9671                 tegra12_pllm_clk_disable(&tegra_pll_m);
9672         tegra12_pllm_clk_init(&tegra_pll_m);
9673
9674         if (p != tegra_clk_emc.parent) {
9675                 pr_debug("EMC parent(refcount) across suspend: %s(%d) : %s(%d)",
9676                         p->name, p->refcnt, tegra_clk_emc.parent->name,
9677                         tegra_clk_emc.parent->refcnt);
9678
9679                 /* emc switched to the new parent by low level code, but ref
9680                    count and s/w state need to be updated */
9681                 clk_disable(p);
9682                 clk_enable(tegra_clk_emc.parent);
9683         }
9684
9685         rate = clk_get_rate_all_locked(&tegra_clk_emc);
9686         if (*ctx != rate) {
9687                 tegra_dvfs_set_rate(&tegra_clk_emc, rate);
9688                 if (p == tegra_clk_emc.parent) {
9689                         rate = clk_get_rate_all_locked(p);
9690                         tegra_dvfs_set_rate(p, rate);
9691                 }
9692         }
9693         tegra_emc_timing_invalidate();
9694
9695         tegra12_pll_clk_init(&tegra_pll_u); /* Re-init utmi parameters */
9696         tegra12_plle_clk_resume(&tegra_pll_e); /* Restore plle parent as pll_re_vco */
9697         tegra12_pllp_clk_resume(&tegra_pll_p); /* Fire a bug if not restored */
9698         tegra12_mc_holdoff_enable();
9699 }
9700
9701 static struct syscore_ops tegra_clk_syscore_ops = {
9702         .suspend = tegra12_clk_suspend,
9703         .resume = tegra12_clk_resume,
9704         .save = tegra12_clk_suspend,
9705         .restore = tegra12_clk_resume,
9706 };
9707 #endif
9708
9709 /* Tegra12 CPU clock and reset control functions */
9710 static void tegra12_wait_cpu_in_reset(u32 cpu)
9711 {
9712         unsigned int reg;
9713
9714         do {
9715                 reg = readl(reg_clk_base +
9716                             TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS);
9717                 cpu_relax();
9718         } while (!(reg & (1 << cpu)));  /* check CPU been reset or not */
9719
9720         return;
9721 }
9722
9723 static void tegra12_put_cpu_in_reset(u32 cpu)
9724 {
9725         writel(CPU_RESET(cpu),
9726                reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
9727         dmb();
9728 }
9729
9730 static void tegra12_cpu_out_of_reset(u32 cpu)
9731 {
9732         writel(CPU_RESET(cpu),
9733                reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
9734         wmb();
9735 }
9736
9737 static void tegra12_enable_cpu_clock(u32 cpu)
9738 {
9739         unsigned int reg;
9740
9741         writel(CPU_CLOCK(cpu),
9742                reg_clk_base + TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
9743         reg = readl(reg_clk_base +
9744                     TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
9745 }
9746 static void tegra12_disable_cpu_clock(u32 cpu)
9747 {
9748 }
9749
9750 static struct tegra_cpu_car_ops tegra12_cpu_car_ops = {
9751         .wait_for_reset = tegra12_wait_cpu_in_reset,
9752         .put_in_reset   = tegra12_put_cpu_in_reset,
9753         .out_of_reset   = tegra12_cpu_out_of_reset,
9754         .enable_clock   = tegra12_enable_cpu_clock,
9755         .disable_clock  = tegra12_disable_cpu_clock,
9756 };
9757
9758 void __init tegra12_cpu_car_ops_init(void)
9759 {
9760         tegra_cpu_car_ops = &tegra12_cpu_car_ops;
9761 }
9762
9763 static void tegra12_init_xusb_clocks(void)
9764 {
9765         int i;
9766
9767         for (i = 0; i < ARRAY_SIZE(tegra_xusb_source_clks); i++)
9768                 tegra12_init_one_clock(&tegra_xusb_source_clks[i]);
9769
9770         tegra12_init_one_clock(&tegra_xusb_ss_div2);
9771         tegra12_init_one_clock(&tegra_xusb_hs_src);
9772
9773         for (i = 0; i < ARRAY_SIZE(tegra_xusb_coupled_clks); i++)
9774                 tegra12_init_one_clock(&tegra_xusb_coupled_clks[i]);
9775 }
9776
9777 void __init tegra12x_init_clocks(void)
9778 {
9779         int i;
9780         struct clk *c;
9781
9782         for (i = 0; i < ARRAY_SIZE(tegra_ptr_clks); i++)
9783                 tegra12_init_one_clock(tegra_ptr_clks[i]);
9784
9785         /* Fix bug in simulator clock routing */
9786         if (tegra_platform_is_linsim()) {
9787                 for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++) {
9788                         if (!strcmp("msenc", tegra_list_clks[i].name)) {
9789                                 tegra_list_clks[i].u.periph.clk_num = 60;
9790                                 tegra_list_clks[i].reg = 0x170;
9791                                 tegra_list_clks[i].flags &= ~MUX8;
9792                         }
9793                 }
9794         }
9795
9796         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
9797                 tegra12_init_one_clock(&tegra_list_clks[i]);
9798
9799         for (i = 0; i < ARRAY_SIZE(tegra_visp_clks); i++)
9800                 tegra12_init_one_clock(&tegra_visp_clks[i]);
9801
9802         for (i = 0; i < ARRAY_SIZE(tegra_ptr_camera_mclks); i++)
9803                 tegra12_init_one_clock(tegra_ptr_camera_mclks[i]);
9804
9805         for (i = 0; i < ARRAY_SIZE(tegra_sync_source_list); i++)
9806                 tegra12_init_one_clock(&tegra_sync_source_list[i]);
9807         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_list); i++)
9808                 tegra12_init_one_clock(&tegra_clk_audio_list[i]);
9809         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_2x_list); i++)
9810                 tegra12_init_one_clock(&tegra_clk_audio_2x_list[i]);
9811
9812         init_clk_out_mux();
9813         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++)
9814                 tegra12_init_one_clock(&tegra_clk_out_list[i]);
9815
9816         tegra12_init_xusb_clocks();
9817
9818         for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
9819                 c = tegra_get_clock_by_name(tegra_clk_duplicates[i].name);
9820                 if (!c) {
9821                         pr_err("%s: Unknown duplicate clock %s\n", __func__,
9822                                 tegra_clk_duplicates[i].name);
9823                         continue;
9824                 }
9825
9826                 tegra_clk_duplicates[i].lookup.clk = c;
9827                 clkdev_add(&tegra_clk_duplicates[i].lookup);
9828         }
9829
9830         /* Initialize to default */
9831         tegra_init_cpu_edp_limits(0);
9832
9833         tegra12_cpu_car_ops_init();
9834
9835         /* Tegra12 allows to change dividers of disabled clocks */
9836         tegra_clk_set_disabled_div_all();
9837
9838 #ifdef CONFIG_PM_SLEEP
9839         register_syscore_ops(&tegra_clk_syscore_ops);
9840 #endif
9841
9842 }
9843
9844 static int __init tegra12x_clk_late_init(void)
9845 {
9846         clk_disable(&tegra_pll_d);
9847         clk_disable(&tegra_pll_re_vco);
9848         return 0;
9849 }
9850 late_initcall(tegra12x_clk_late_init);