]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - arch/arm/mach-tegra/tegra12_clocks.c
arm: tegra12: add support for 0x80 embedded SKU
[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 int old_use_dfll;
4362         if (tegra_override_dfll_range != TEGRA_USE_DFLL_CDEV_CNTRL) {
4363                 old_use_dfll = use_dfll;
4364                 param_set_int(arg, kp);
4365                 ret =  tegra_clk_dfll_range_control(use_dfll);
4366                 if (ret)
4367                         use_dfll = old_use_dfll;
4368                 return ret;
4369         } else {
4370                 pr_warn("\n%s: Failed to set use_dfll\n", __func__);
4371                 pr_warn("DFLL usage is under thermal cooling device control\n");
4372                 return -EACCES;
4373         }
4374 }
4375
4376 static struct kernel_param_ops tegra12_use_dfll_ops = {
4377         .set = tegra12_use_dfll_cb,
4378         .get = param_get_int,
4379 };
4380 module_param_cb(use_dfll, &tegra12_use_dfll_ops, &use_dfll, 0644);
4381
4382
4383 /* Clock divider ops (non-atomic shared register access) */
4384 static DEFINE_SPINLOCK(pll_div_lock);
4385
4386 static int tegra12_pll_div_clk_set_rate(struct clk *c, unsigned long rate);
4387 static void tegra12_pll_div_clk_init(struct clk *c)
4388 {
4389         if (c->flags & DIV_U71) {
4390                 u32 val, divu71;
4391                 if (c->parent->state == OFF)
4392                         c->ops->disable(c);
4393
4394                 val = clk_readl(c->reg);
4395                 val >>= c->reg_shift;
4396                 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
4397                 if (!(val & PLL_OUT_RESET_DISABLE))
4398                         c->state = OFF;
4399
4400                 if (c->u.pll_div.default_rate) {
4401                         int ret = tegra12_pll_div_clk_set_rate(
4402                                         c, c->u.pll_div.default_rate);
4403                         if (!ret)
4404                                 return;
4405                 }
4406                 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
4407                 c->div = (divu71 + 2);
4408                 c->mul = 2;
4409         } else if (c->flags & DIV_2) {
4410                 c->state = ON;
4411                 if (c->flags & (PLLD | PLLX)) {
4412                         c->div = 2;
4413                         c->mul = 1;
4414                 }
4415                 else
4416                         BUG();
4417         } else if (c->flags & PLLU) {
4418                 u32 val = clk_readl(c->reg);
4419                 c->state = val & (0x1 << c->reg_shift) ? ON : OFF;
4420         } else {
4421                 c->state = ON;
4422                 c->div = 1;
4423                 c->mul = 1;
4424         }
4425 }
4426
4427 static int tegra12_pll_div_clk_enable(struct clk *c)
4428 {
4429         u32 val;
4430         u32 new_val;
4431         unsigned long flags;
4432
4433         pr_debug("%s: %s\n", __func__, c->name);
4434         if (c->flags & DIV_U71) {
4435                 spin_lock_irqsave(&pll_div_lock, flags);
4436                 val = clk_readl(c->reg);
4437                 new_val = val >> c->reg_shift;
4438                 new_val &= 0xFFFF;
4439
4440                 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
4441
4442                 val &= ~(0xFFFF << c->reg_shift);
4443                 val |= new_val << c->reg_shift;
4444                 clk_writel_delay(val, c->reg);
4445                 spin_unlock_irqrestore(&pll_div_lock, flags);
4446                 return 0;
4447         } else if (c->flags & DIV_2) {
4448                 return 0;
4449         } else if (c->flags & PLLU) {
4450                 clk_lock_save(c->parent, &flags);
4451                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
4452                 clk_writel_delay(val, c->reg);
4453                 clk_unlock_restore(c->parent, &flags);
4454                 return 0;
4455         }
4456         return -EINVAL;
4457 }
4458
4459 static void tegra12_pll_div_clk_disable(struct clk *c)
4460 {
4461         u32 val;
4462         u32 new_val;
4463         unsigned long flags;
4464
4465         pr_debug("%s: %s\n", __func__, c->name);
4466         if (c->flags & DIV_U71) {
4467                 spin_lock_irqsave(&pll_div_lock, flags);
4468                 val = clk_readl(c->reg);
4469                 new_val = val >> c->reg_shift;
4470                 new_val &= 0xFFFF;
4471
4472                 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
4473
4474                 val &= ~(0xFFFF << c->reg_shift);
4475                 val |= new_val << c->reg_shift;
4476                 clk_writel_delay(val, c->reg);
4477                 spin_unlock_irqrestore(&pll_div_lock, flags);
4478         } else if (c->flags & PLLU) {
4479                 clk_lock_save(c->parent, &flags);
4480                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
4481                 clk_writel_delay(val, c->reg);
4482                 clk_unlock_restore(c->parent, &flags);
4483         }
4484 }
4485
4486 static int tegra12_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
4487 {
4488         u32 val;
4489         u32 new_val;
4490         int divider_u71;
4491         unsigned long parent_rate = clk_get_rate(c->parent);
4492         unsigned long flags;
4493
4494         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4495         if (tegra_platform_is_qt())
4496                 return 0;
4497         if (c->flags & DIV_U71) {
4498                 divider_u71 = clk_div71_get_divider(
4499                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4500                 if (divider_u71 >= 0) {
4501                         spin_lock_irqsave(&pll_div_lock, flags);
4502                         val = clk_readl(c->reg);
4503                         new_val = val >> c->reg_shift;
4504                         new_val &= 0xFFFF;
4505                         if (c->flags & DIV_U71_FIXED)
4506                                 new_val |= PLL_OUT_OVERRIDE;
4507                         new_val &= ~PLL_OUT_RATIO_MASK;
4508                         new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
4509
4510                         val &= ~(0xFFFF << c->reg_shift);
4511                         val |= new_val << c->reg_shift;
4512                         clk_writel_delay(val, c->reg);
4513                         c->div = divider_u71 + 2;
4514                         c->mul = 2;
4515                         spin_unlock_irqrestore(&pll_div_lock, flags);
4516                         return 0;
4517                 }
4518         } else if (c->flags & DIV_2)
4519                 return clk_set_rate(c->parent, rate * 2);
4520
4521         return -EINVAL;
4522 }
4523
4524 static long tegra12_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
4525 {
4526         int divider;
4527         unsigned long parent_rate = clk_get_rate(c->parent);
4528         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4529
4530         if (c->flags & DIV_U71) {
4531                 divider = clk_div71_get_divider(
4532                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4533                 if (divider < 0)
4534                         return divider;
4535                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4536         } else if (c->flags & DIV_2)
4537                 /* no rounding - fixed DIV_2 dividers pass rate to parent PLL */
4538                 return rate;
4539
4540         return -EINVAL;
4541 }
4542
4543 static struct clk_ops tegra_pll_div_ops = {
4544         .init                   = tegra12_pll_div_clk_init,
4545         .enable                 = tegra12_pll_div_clk_enable,
4546         .disable                = tegra12_pll_div_clk_disable,
4547         .set_rate               = tegra12_pll_div_clk_set_rate,
4548         .round_rate             = tegra12_pll_div_clk_round_rate,
4549 };
4550
4551 /* Periph clk ops */
4552 static inline u32 periph_clk_source_mask(struct clk *c)
4553 {
4554         if (c->u.periph.src_mask)
4555                 return c->u.periph.src_mask;
4556         else if (c->flags & MUX_PWM)
4557                 return 3 << 28;
4558         else if (c->flags & MUX_CLK_OUT)
4559                 return 3 << (c->u.periph.clk_num + 4);
4560         else if (c->flags & PLLD)
4561                 return PLLD_BASE_DSI_MUX_MASK;
4562         else
4563                 return 7 << 29;
4564 }
4565
4566 static inline u32 periph_clk_source_shift(struct clk *c)
4567 {
4568         if (c->u.periph.src_shift)
4569                 return c->u.periph.src_shift;
4570         else if (c->flags & MUX_PWM)
4571                 return 28;
4572         else if (c->flags & MUX_CLK_OUT)
4573                 return c->u.periph.clk_num + 4;
4574         else if (c->flags & PLLD)
4575                 return PLLD_BASE_DSI_MUX_SHIFT;
4576         else
4577                 return 29;
4578 }
4579
4580 static void tegra12_periph_clk_init(struct clk *c)
4581 {
4582         u32 val = clk_readl(c->reg);
4583         const struct clk_mux_sel *mux = 0;
4584         const struct clk_mux_sel *sel;
4585         if (c->flags & MUX) {
4586                 for (sel = c->inputs; sel->input != NULL; sel++) {
4587                         if (((val & periph_clk_source_mask(c)) >>
4588                             periph_clk_source_shift(c)) == sel->value)
4589                                 mux = sel;
4590                 }
4591                 BUG_ON(!mux);
4592
4593                 c->parent = mux->input;
4594         } else {
4595                 if (c->flags & PLLU) {
4596                         /* for xusb_hs clock enforce SS div2 source */
4597                         val &= ~periph_clk_source_mask(c);
4598                         clk_writel_delay(val, c->reg);
4599                 }
4600                 c->parent = c->inputs[0].input;
4601         }
4602
4603         /* if peripheral is left under reset - enforce safe rate */
4604
4605         if (c->flags & PERIPH_NO_RESET) {
4606                 if (tegra12_periph_is_special_reset(c)) {
4607                         tegra_periph_clk_safe_rate_init(c);
4608                          val = clk_readl(c->reg);
4609                 }
4610         } else if (IS_PERIPH_IN_RESET(c)) {
4611                 tegra_periph_clk_safe_rate_init(c);
4612                 val = clk_readl(c->reg);
4613         }
4614
4615         if (c->flags & DIV_U71) {
4616                 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
4617                 if (c->flags & DIV_U71_IDLE) {
4618                         val &= ~(PERIPH_CLK_SOURCE_DIVU71_MASK <<
4619                                 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
4620                         val |= (PERIPH_CLK_SOURCE_DIVIDLE_VAL <<
4621                                 PERIPH_CLK_SOURCE_DIVIDLE_SHIFT);
4622                         clk_writel(val, c->reg);
4623                 }
4624                 c->div = divu71 + 2;
4625                 c->mul = 2;
4626         } else if (c->flags & DIV_U151) {
4627                 u32 divu151 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
4628                 if ((c->flags & DIV_U151_UART) &&
4629                     (!(val & PERIPH_CLK_UART_DIV_ENB))) {
4630                         divu151 = 0;
4631                 }
4632                 c->div = divu151 + 2;
4633                 c->mul = 2;
4634         } else if (c->flags & DIV_U16) {
4635                 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
4636                 c->div = divu16 + 1;
4637                 c->mul = 1;
4638         } else {
4639                 c->div = 1;
4640                 c->mul = 1;
4641         }
4642
4643         if (c->flags & PERIPH_NO_ENB) {
4644                 c->state = c->parent->state;
4645                 return;
4646         }
4647
4648         c->state = ON;
4649
4650         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
4651                 c->state = OFF;
4652         if (!(c->flags & PERIPH_NO_RESET))
4653                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c))
4654                         c->state = OFF;
4655 }
4656
4657 static int tegra12_periph_clk_enable(struct clk *c)
4658 {
4659         unsigned long flags;
4660         pr_debug("%s on clock %s\n", __func__, c->name);
4661
4662         if (c->flags & PERIPH_NO_ENB)
4663                 return 0;
4664
4665         spin_lock_irqsave(&periph_refcount_lock, flags);
4666
4667         tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
4668         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1) {
4669                 spin_unlock_irqrestore(&periph_refcount_lock, flags);
4670                 return 0;
4671         }
4672
4673         clk_writel_delay(PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_SET_REG(c));
4674         if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET)) {
4675                 if (clk_readl(PERIPH_CLK_TO_RST_REG(c)) & PERIPH_CLK_TO_BIT(c)) {
4676                         udelay(RESET_PROPAGATION_DELAY);
4677                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4678                                          PERIPH_CLK_TO_RST_CLR_REG(c));
4679                 }
4680         }
4681         spin_unlock_irqrestore(&periph_refcount_lock, flags);
4682         return 0;
4683 }
4684
4685 static void tegra12_periph_clk_disable(struct clk *c)
4686 {
4687         unsigned long val, flags;
4688         pr_debug("%s on clock %s\n", __func__, c->name);
4689
4690         if (c->flags & PERIPH_NO_ENB)
4691                 return;
4692
4693         spin_lock_irqsave(&periph_refcount_lock, flags);
4694
4695         if (c->refcnt)
4696                 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
4697
4698         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0) {
4699                 /* If peripheral is in the APB bus then read the APB bus to
4700                  * flush the write operation in apb bus. This will avoid the
4701                  * peripheral access after disabling clock*/
4702                 if (c->flags & PERIPH_ON_APB)
4703                         val = tegra_read_chipid();
4704
4705                 clk_writel_delay(
4706                         PERIPH_CLK_TO_BIT(c), PERIPH_CLK_TO_ENB_CLR_REG(c));
4707         }
4708         spin_unlock_irqrestore(&periph_refcount_lock, flags);
4709 }
4710
4711 static void tegra12_periph_clk_reset(struct clk *c, bool assert)
4712 {
4713         unsigned long val;
4714         pr_debug("%s %s on clock %s\n", __func__,
4715                  assert ? "assert" : "deassert", c->name);
4716
4717         if (c->flags & PERIPH_NO_ENB)
4718                 return;
4719
4720         if (!(c->flags & PERIPH_NO_RESET)) {
4721                 if (assert) {
4722                         /* If peripheral is in the APB bus then read the APB
4723                          * bus to flush the write operation in apb bus. This
4724                          * will avoid the peripheral access after disabling
4725                          * clock */
4726                         if (c->flags & PERIPH_ON_APB)
4727                                 val = tegra_read_chipid();
4728
4729                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4730                                          PERIPH_CLK_TO_RST_SET_REG(c));
4731                 } else
4732                         clk_writel_delay(PERIPH_CLK_TO_BIT(c),
4733                                          PERIPH_CLK_TO_RST_CLR_REG(c));
4734         }
4735 }
4736
4737 static int tegra12_periph_clk_set_parent(struct clk *c, struct clk *p)
4738 {
4739         u32 val;
4740         const struct clk_mux_sel *sel;
4741         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
4742
4743         if (!(c->flags & MUX))
4744                 return (p == c->parent) ? 0 : (-EINVAL);
4745
4746         for (sel = c->inputs; sel->input != NULL; sel++) {
4747                 if (sel->input == p) {
4748                         val = clk_readl(c->reg);
4749                         val &= ~periph_clk_source_mask(c);
4750                         val |= (sel->value << periph_clk_source_shift(c));
4751
4752                         if (c->refcnt)
4753                                 clk_enable(p);
4754
4755                         clk_writel_delay(val, c->reg);
4756
4757                         if (c->refcnt && c->parent)
4758                                 clk_disable(c->parent);
4759
4760                         clk_reparent(c, p);
4761                         return 0;
4762                 }
4763         }
4764
4765         return -EINVAL;
4766 }
4767
4768 static int tegra12_periph_clk_set_rate(struct clk *c, unsigned long rate)
4769 {
4770         u32 val;
4771         int divider;
4772         unsigned long parent_rate = clk_get_rate(c->parent);
4773
4774         if (tegra_platform_is_qt())
4775                 return 0;
4776         if (c->flags & DIV_U71) {
4777                 divider = clk_div71_get_divider(
4778                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4779                 if (divider >= 0) {
4780                         val = clk_readl(c->reg);
4781                         val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
4782                         val |= divider;
4783                         clk_writel_delay(val, c->reg);
4784                         c->div = divider + 2;
4785                         c->mul = 2;
4786                         return 0;
4787                 }
4788         } else if (c->flags & DIV_U151) {
4789                 divider = clk_div151_get_divider(
4790                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4791                 if (divider >= 0) {
4792                         val = clk_readl(c->reg);
4793                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
4794                         val |= divider;
4795                         if (c->flags & DIV_U151_UART) {
4796                                 if (divider)
4797                                         val |= PERIPH_CLK_UART_DIV_ENB;
4798                                 else
4799                                         val &= ~PERIPH_CLK_UART_DIV_ENB;
4800                         }
4801                         clk_writel_delay(val, c->reg);
4802                         c->div = divider + 2;
4803                         c->mul = 2;
4804                         return 0;
4805                 }
4806         } else if (c->flags & DIV_U16) {
4807                 divider = clk_div16_get_divider(parent_rate, rate);
4808                 if (divider >= 0) {
4809                         val = clk_readl(c->reg);
4810                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
4811                         val |= divider;
4812                         clk_writel_delay(val, c->reg);
4813                         c->div = divider + 1;
4814                         c->mul = 1;
4815                         return 0;
4816                 }
4817         } else if (parent_rate <= rate) {
4818                 c->div = 1;
4819                 c->mul = 1;
4820                 return 0;
4821         }
4822         return -EINVAL;
4823 }
4824
4825 static long tegra12_periph_clk_round_rate(struct clk *c,
4826         unsigned long rate)
4827 {
4828         int divider;
4829         unsigned long parent_rate = clk_get_rate(c->parent);
4830         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
4831
4832         if (c->flags & DIV_U71) {
4833                 divider = clk_div71_get_divider(
4834                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4835                 if (divider < 0)
4836                         return divider;
4837
4838                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4839         } else if (c->flags & DIV_U151) {
4840                 divider = clk_div151_get_divider(
4841                         parent_rate, rate, c->flags, ROUND_DIVIDER_UP);
4842                 if (divider < 0)
4843                         return divider;
4844
4845                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
4846         } else if (c->flags & DIV_U16) {
4847                 divider = clk_div16_get_divider(parent_rate, rate);
4848                 if (divider < 0)
4849                         return divider;
4850                 return DIV_ROUND_UP(parent_rate, divider + 1);
4851         }
4852         return -EINVAL;
4853 }
4854
4855 static struct clk_ops tegra_periph_clk_ops = {
4856         .init                   = &tegra12_periph_clk_init,
4857         .enable                 = &tegra12_periph_clk_enable,
4858         .disable                = &tegra12_periph_clk_disable,
4859         .set_parent             = &tegra12_periph_clk_set_parent,
4860         .set_rate               = &tegra12_periph_clk_set_rate,
4861         .round_rate             = &tegra12_periph_clk_round_rate,
4862         .reset                  = &tegra12_periph_clk_reset,
4863 };
4864
4865 /* 1x shared bus ops */
4866 static long _1x_round_updown(struct clk *c, struct clk *src,
4867                              unsigned long rate, bool up)
4868 {
4869         return fixed_src_bus_round_updown(c, src, c->flags, rate, up);
4870 }
4871
4872 static long tegra12_1xbus_round_updown(struct clk *c, unsigned long rate,
4873                                             bool up)
4874 {
4875         unsigned long pll_low_rate, pll_high_rate;
4876
4877         rate = max(rate, c->min_rate);
4878
4879         pll_low_rate = _1x_round_updown(c, c->u.periph.pll_low, rate, up);
4880         if (rate <= c->u.periph.threshold) {
4881                 c->u.periph.pll_selected = c->u.periph.pll_low;
4882                 return pll_low_rate;
4883         }
4884
4885         pll_high_rate = _1x_round_updown(c, c->u.periph.pll_high, rate, up);
4886         if (pll_high_rate <= c->u.periph.threshold) {
4887                 c->u.periph.pll_selected = c->u.periph.pll_low;
4888                 return pll_low_rate;  /* prevent oscillation across threshold */
4889         }
4890
4891         if (up) {
4892                 /* rounding up: both plls may hit max, and round down */
4893                 if (pll_high_rate < rate) {
4894                         if (pll_low_rate < pll_high_rate) {
4895                                 c->u.periph.pll_selected = c->u.periph.pll_high;
4896                                 return pll_high_rate;
4897                         }
4898                 } else {
4899                         if ((pll_low_rate < rate) ||
4900                             (pll_low_rate > pll_high_rate)) {
4901                                 c->u.periph.pll_selected = c->u.periph.pll_high;
4902                                 return pll_high_rate;
4903                         }
4904                 }
4905         } else if (pll_low_rate < pll_high_rate) {
4906                 /* rounding down: to get here both plls able to round down */
4907                 c->u.periph.pll_selected = c->u.periph.pll_high;
4908                 return pll_high_rate;
4909         }
4910         c->u.periph.pll_selected = c->u.periph.pll_low;
4911         return pll_low_rate;
4912 }
4913
4914 static long tegra12_1xbus_round_rate(struct clk *c, unsigned long rate)
4915 {
4916         return tegra12_1xbus_round_updown(c, rate, true);
4917 }
4918
4919 static int tegra12_1xbus_set_rate(struct clk *c, unsigned long rate)
4920 {
4921         /* Compensate rate truncating during rounding */
4922         return tegra12_periph_clk_set_rate(c, rate + 1);
4923 }
4924
4925 static int tegra12_clk_1xbus_update(struct clk *c)
4926 {
4927         int ret;
4928         struct clk *new_parent;
4929         unsigned long rate, old_rate;
4930
4931         if (detach_shared_bus)
4932                 return 0;
4933
4934         rate = tegra12_clk_shared_bus_update(c, NULL, NULL, NULL);
4935
4936         old_rate = clk_get_rate_locked(c);
4937         pr_debug("\n1xbus %s: rate %lu on parent %s: new request %lu\n",
4938                  c->name, old_rate, c->parent->name, rate);
4939         if (rate == old_rate)
4940                 return 0;
4941
4942         if (!c->u.periph.min_div_low || !c->u.periph.min_div_high) {
4943                 unsigned long r, m = c->max_rate;
4944                 r = clk_get_rate(c->u.periph.pll_low);
4945                 c->u.periph.min_div_low = DIV_ROUND_UP(r, m) * c->mul;
4946                 r = clk_get_rate(c->u.periph.pll_high);
4947                 c->u.periph.min_div_high = DIV_ROUND_UP(r, m) * c->mul;
4948         }
4949
4950         new_parent = c->u.periph.pll_selected;
4951
4952         /*
4953          * The transition procedure below is guaranteed to switch to the target
4954          * parent/rate without violation of max clock limits. It would attempt
4955          * to switch without dip in bus rate if it is possible, but this cannot
4956          * be guaranteed (example: switch from 408 MHz : 1 to 624 MHz : 2 with
4957          * maximum bus limit 408 MHz will be executed as 408 => 204 => 312 MHz,
4958          * and there is no way to avoid rate dip in this case).
4959          */
4960         if (new_parent != c->parent) {
4961                 int interim_div = 0;
4962                 /* Switching to pll_high may over-clock bus if current divider
4963                    is too small - increase divider to safe value */
4964                 if ((new_parent == c->u.periph.pll_high) &&
4965                     (c->div < c->u.periph.min_div_high))
4966                         interim_div = c->u.periph.min_div_high;
4967
4968                 /* Switching to pll_low may dip down rate if current divider
4969                    is too big - decrease divider as much as we can */
4970                 if ((new_parent == c->u.periph.pll_low) &&
4971                     (c->div > c->u.periph.min_div_low) &&
4972                     (c->div > c->u.periph.min_div_high))
4973                         interim_div = c->u.periph.min_div_low;
4974
4975                 if (interim_div) {
4976                         u64 interim_rate = old_rate * c->div;
4977                         do_div(interim_rate, interim_div);
4978                         ret = clk_set_rate_locked(c, interim_rate);
4979                         if (ret) {
4980                                 pr_err("Failed to set %s rate to %lu\n",
4981                                        c->name, (unsigned long)interim_rate);
4982                                 return ret;
4983                         }
4984                         pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
4985                                  clk_get_rate_locked(c), c->parent->name);
4986                 }
4987
4988                 ret = clk_set_parent_locked(c, new_parent);
4989                 if (ret) {
4990                         pr_err("Failed to set %s parent %s\n",
4991                                c->name, new_parent->name);
4992                         return ret;
4993                 }
4994
4995                 old_rate = clk_get_rate_locked(c);
4996                 pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
4997                          old_rate, c->parent->name);
4998                 if (rate == old_rate)
4999                         return 0;
5000         }
5001
5002         ret = clk_set_rate_locked(c, rate);
5003         if (ret) {
5004                 pr_err("Failed to set %s rate to %lu\n", c->name, rate);
5005                 return ret;
5006         }
5007         pr_debug("1xbus %s: rate %lu on parent %s\n", c->name,
5008                  clk_get_rate_locked(c), c->parent->name);
5009         return 0;
5010
5011 }
5012
5013 static struct clk_ops tegra_1xbus_clk_ops = {
5014         .init                   = &tegra12_periph_clk_init,
5015         .enable                 = &tegra12_periph_clk_enable,
5016         .disable                = &tegra12_periph_clk_disable,
5017         .set_parent             = &tegra12_periph_clk_set_parent,
5018         .set_rate               = &tegra12_1xbus_set_rate,
5019         .round_rate             = &tegra12_1xbus_round_rate,
5020         .round_rate_updown      = &tegra12_1xbus_round_updown,
5021         .reset                  = &tegra12_periph_clk_reset,
5022         .shared_bus_update      = &tegra12_clk_1xbus_update,
5023 };
5024
5025 /* msenc clock propagation WAR for bug 1005168 */
5026 static int tegra12_msenc_clk_enable(struct clk *c)
5027 {
5028         int ret = tegra12_periph_clk_enable(c);
5029         if (ret)
5030                 return ret;
5031
5032         clk_writel(0, LVL2_CLK_GATE_OVRE);
5033         clk_writel(0x00400000, LVL2_CLK_GATE_OVRE);
5034         udelay(1);
5035         clk_writel(0, LVL2_CLK_GATE_OVRE);
5036         return 0;
5037 }
5038
5039 static struct clk_ops tegra_msenc_clk_ops = {
5040         .init                   = &tegra12_periph_clk_init,
5041         .enable                 = &tegra12_msenc_clk_enable,
5042         .disable                = &tegra12_periph_clk_disable,
5043         .set_parent             = &tegra12_periph_clk_set_parent,
5044         .set_rate               = &tegra12_periph_clk_set_rate,
5045         .round_rate             = &tegra12_periph_clk_round_rate,
5046         .reset                  = &tegra12_periph_clk_reset,
5047 };
5048 /* Periph extended clock configuration ops */
5049 static int
5050 tegra12_vi_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
5051 {
5052         if (p == TEGRA_CLK_VI_INP_SEL) {
5053                 u32 val = clk_readl(c->reg);
5054                 val &= ~PERIPH_CLK_VI_SEL_EX_MASK;
5055                 val |= (setting << PERIPH_CLK_VI_SEL_EX_SHIFT) &
5056                         PERIPH_CLK_VI_SEL_EX_MASK;
5057                 clk_writel(val, c->reg);
5058                 return 0;
5059         }
5060         return -EINVAL;
5061 }
5062
5063 static struct clk_ops tegra_vi_clk_ops = {
5064         .init                   = &tegra12_periph_clk_init,
5065         .enable                 = &tegra12_periph_clk_enable,
5066         .disable                = &tegra12_periph_clk_disable,
5067         .set_parent             = &tegra12_periph_clk_set_parent,
5068         .set_rate               = &tegra12_periph_clk_set_rate,
5069         .round_rate             = &tegra12_periph_clk_round_rate,
5070         .clk_cfg_ex             = &tegra12_vi_clk_cfg_ex,
5071         .reset                  = &tegra12_periph_clk_reset,
5072 };
5073
5074 static int
5075 tegra12_sor_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
5076 {
5077         if (p == TEGRA_CLK_SOR_CLK_SEL) {
5078                 u32 val = clk_readl(c->reg);
5079                 val &= ~PERIPH_CLK_SOR_CLK_SEL_MASK;
5080                 val |= (setting << PERIPH_CLK_SOR_CLK_SEL_SHIFT) &
5081                         PERIPH_CLK_SOR_CLK_SEL_MASK;
5082                 clk_writel(val, c->reg);
5083                 return 0;
5084         }
5085         return -EINVAL;
5086 }
5087
5088 static struct clk_ops tegra_sor_clk_ops = {
5089         .init                   = &tegra12_periph_clk_init,
5090         .enable                 = &tegra12_periph_clk_enable,
5091         .disable                = &tegra12_periph_clk_disable,
5092         .set_parent             = &tegra12_periph_clk_set_parent,
5093         .set_rate               = &tegra12_periph_clk_set_rate,
5094         .round_rate             = &tegra12_periph_clk_round_rate,
5095         .clk_cfg_ex             = &tegra12_sor_clk_cfg_ex,
5096         .reset                  = &tegra12_periph_clk_reset,
5097 };
5098
5099 static int
5100 tegra12_dtv_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
5101 {
5102         if (p == TEGRA_CLK_DTV_INVERT) {
5103                 u32 val = clk_readl(c->reg);
5104                 if (setting)
5105                         val |= PERIPH_CLK_DTV_POLARITY_INV;
5106                 else
5107                         val &= ~PERIPH_CLK_DTV_POLARITY_INV;
5108                 clk_writel(val, c->reg);
5109                 return 0;
5110         }
5111         return -EINVAL;
5112 }
5113
5114 static struct clk_ops tegra_dtv_clk_ops = {
5115         .init                   = &tegra12_periph_clk_init,
5116         .enable                 = &tegra12_periph_clk_enable,
5117         .disable                = &tegra12_periph_clk_disable,
5118         .set_parent             = &tegra12_periph_clk_set_parent,
5119         .set_rate               = &tegra12_periph_clk_set_rate,
5120         .round_rate             = &tegra12_periph_clk_round_rate,
5121         .clk_cfg_ex             = &tegra12_dtv_clk_cfg_ex,
5122         .reset                  = &tegra12_periph_clk_reset,
5123 };
5124
5125 static struct clk_ops tegra_dsi_clk_ops = {
5126         .init                   = &tegra12_periph_clk_init,
5127         .enable                 = &tegra12_periph_clk_enable,
5128         .disable                = &tegra12_periph_clk_disable,
5129         .set_rate               = &tegra12_periph_clk_set_rate,
5130         .round_rate             = &tegra12_periph_clk_round_rate,
5131         .reset                  = &tegra12_periph_clk_reset,
5132 };
5133
5134 /* pciex clock support only reset function */
5135 static void tegra12_pciex_clk_init(struct clk *c)
5136 {
5137         c->state = c->parent->state;
5138 }
5139
5140 static int tegra12_pciex_clk_enable(struct clk *c)
5141 {
5142         return 0;
5143 }
5144
5145 static void tegra12_pciex_clk_disable(struct clk *c)
5146 {
5147 }
5148
5149 static int tegra12_pciex_clk_set_rate(struct clk *c, unsigned long rate)
5150 {
5151         unsigned long parent_rate = clk_get_rate(c->parent);
5152
5153         /*
5154          * the only supported pcie configurations:
5155          * Gen1: plle = 100MHz, link at 250MHz
5156          * Gen2: plle = 100MHz, link at 500MHz
5157          */
5158         if (parent_rate == 100000000) {
5159                 if (rate == 500000000) {
5160                         c->mul = 5;
5161                         c->div = 1;
5162                         return 0;
5163                 } else if (rate == 250000000) {
5164                         c->mul = 5;
5165                         c->div = 2;
5166                         return 0;
5167                 }
5168         }
5169         return -EINVAL;
5170 }
5171
5172 static struct clk_ops tegra_pciex_clk_ops = {
5173         .init     = tegra12_pciex_clk_init,
5174         .enable   = tegra12_pciex_clk_enable,
5175         .disable  = tegra12_pciex_clk_disable,
5176         .set_rate = tegra12_pciex_clk_set_rate,
5177         .reset    = tegra12_periph_clk_reset,
5178 };
5179
5180 /* Output clock ops */
5181
5182 static DEFINE_SPINLOCK(clk_out_lock);
5183
5184 static void tegra12_clk_out_init(struct clk *c)
5185 {
5186         const struct clk_mux_sel *mux = 0;
5187         const struct clk_mux_sel *sel;
5188         u32 val = pmc_readl(c->reg);
5189
5190         c->state = (val & (0x1 << c->u.periph.clk_num)) ? ON : OFF;
5191         c->mul = 1;
5192         c->div = 1;
5193
5194         for (sel = c->inputs; sel->input != NULL; sel++) {
5195                 if (((val & periph_clk_source_mask(c)) >>
5196                      periph_clk_source_shift(c)) == sel->value)
5197                         mux = sel;
5198         }
5199         BUG_ON(!mux);
5200         c->parent = mux->input;
5201 }
5202
5203 static int tegra12_clk_out_enable(struct clk *c)
5204 {
5205         u32 val;
5206         unsigned long flags;
5207
5208         pr_debug("%s on clock %s\n", __func__, c->name);
5209
5210         spin_lock_irqsave(&clk_out_lock, flags);
5211         val = pmc_readl(c->reg);
5212         val |= (0x1 << c->u.periph.clk_num);
5213         pmc_writel(val, c->reg);
5214         pmc_readl(c->reg);
5215         spin_unlock_irqrestore(&clk_out_lock, flags);
5216
5217         return 0;
5218 }
5219
5220 static void tegra12_clk_out_disable(struct clk *c)
5221 {
5222         u32 val;
5223         unsigned long flags;
5224
5225         pr_debug("%s on clock %s\n", __func__, c->name);
5226
5227         spin_lock_irqsave(&clk_out_lock, flags);
5228         val = pmc_readl(c->reg);
5229         val &= ~(0x1 << c->u.periph.clk_num);
5230         pmc_writel(val, c->reg);
5231         pmc_readl(c->reg);
5232         spin_unlock_irqrestore(&clk_out_lock, flags);
5233 }
5234
5235 static int tegra12_clk_out_set_parent(struct clk *c, struct clk *p)
5236 {
5237         u32 val;
5238         unsigned long flags;
5239         const struct clk_mux_sel *sel;
5240
5241         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
5242
5243         for (sel = c->inputs; sel->input != NULL; sel++) {
5244                 if (sel->input == p) {
5245                         if (c->refcnt)
5246                                 clk_enable(p);
5247
5248                         spin_lock_irqsave(&clk_out_lock, flags);
5249                         val = pmc_readl(c->reg);
5250                         val &= ~periph_clk_source_mask(c);
5251                         val |= (sel->value << periph_clk_source_shift(c));
5252                         pmc_writel(val, c->reg);
5253                         pmc_readl(c->reg);
5254                         spin_unlock_irqrestore(&clk_out_lock, flags);
5255
5256                         if (c->refcnt && c->parent)
5257                                 clk_disable(c->parent);
5258
5259                         clk_reparent(c, p);
5260                         return 0;
5261                 }
5262         }
5263         return -EINVAL;
5264 }
5265
5266 static struct clk_ops tegra_clk_out_ops = {
5267         .init                   = &tegra12_clk_out_init,
5268         .enable                 = &tegra12_clk_out_enable,
5269         .disable                = &tegra12_clk_out_disable,
5270         .set_parent             = &tegra12_clk_out_set_parent,
5271 };
5272
5273
5274 /* External memory controller clock ops */
5275 static void tegra12_emc_clk_init(struct clk *c)
5276 {
5277         tegra12_periph_clk_init(c);
5278         tegra_emc_dram_type_init(c);
5279 }
5280
5281 static long tegra12_emc_clk_round_updown(struct clk *c, unsigned long rate,
5282                                          bool up)
5283 {
5284         unsigned long new_rate = max(rate, c->min_rate);
5285
5286         new_rate = tegra_emc_round_rate_updown(new_rate, up);
5287         if (IS_ERR_VALUE(new_rate))
5288                 new_rate = c->max_rate;
5289
5290         return new_rate;
5291 }
5292
5293 static long tegra12_emc_clk_round_rate(struct clk *c, unsigned long rate)
5294 {
5295         return tegra12_emc_clk_round_updown(c, rate, true);
5296 }
5297
5298 static int tegra12_emc_clk_set_rate(struct clk *c, unsigned long rate)
5299 {
5300         int ret;
5301         u32 div_value;
5302         struct clk *p;
5303
5304         if (tegra_platform_is_qt())
5305                 return 0;
5306         /* The tegra12x memory controller has an interlock with the clock
5307          * block that allows memory shadowed registers to be updated,
5308          * and then transfer them to the main registers at the same
5309          * time as the clock update without glitches. During clock change
5310          * operation both clock parent and divider may change simultaneously
5311          * to achieve requested rate. */
5312         p = tegra_emc_predict_parent(rate, &div_value);
5313         div_value += 2;         /* emc has fractional DIV_U71 divider */
5314         if (IS_ERR_OR_NULL(p)) {
5315                 pr_err("%s: Failed to predict emc parent for rate %lu\n",
5316                        __func__, rate);
5317                 return -EINVAL;
5318         }
5319
5320         if (p == c->parent) {
5321                 if (div_value == c->div)
5322                         return 0;
5323         } else if (c->refcnt)
5324                 clk_enable(p);
5325
5326         ret = tegra_emc_set_rate(rate);
5327         if (ret < 0)
5328                 return ret;
5329
5330         if (p != c->parent) {
5331                 if(c->refcnt && c->parent)
5332                         clk_disable(c->parent);
5333                 clk_reparent(c, p);
5334         }
5335         c->div = div_value;
5336         c->mul = 2;
5337         return 0;
5338 }
5339
5340 static int tegra12_clk_emc_bus_update(struct clk *bus)
5341 {
5342         struct clk *p = NULL;
5343         unsigned long rate, old_rate, parent_rate, backup_rate;
5344
5345         if (detach_shared_bus)
5346                 return 0;
5347
5348         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
5349
5350         old_rate = clk_get_rate_locked(bus);
5351         if (rate == old_rate)
5352                 return 0;
5353
5354         if (!tegra_emc_is_parent_ready(rate, &p, &parent_rate, &backup_rate)) {
5355                 if (bus->parent == p) {
5356                         /* need backup to re-lock current parent */
5357                         int ret;
5358                         if (IS_ERR_VALUE(backup_rate)) {
5359                                 pr_err("%s: No backup for %s rate %lu\n",
5360                                        __func__, bus->name, rate);
5361                                 return -EINVAL;
5362                         }
5363
5364                         /* set volatge for backup rate if going up */
5365                         if (backup_rate > old_rate) {
5366                                 ret = tegra_dvfs_set_rate(bus, backup_rate);
5367                                 if (ret) {
5368                                         pr_err("%s: dvfs failed on %s rate %lu\n",
5369                                               __func__, bus->name, backup_rate);
5370                                         return -EINVAL;
5371                                 }
5372                         }
5373
5374                         trace_clock_set_rate(bus->name, backup_rate, 0);
5375                         ret = bus->ops->set_rate(bus, backup_rate);
5376                         if (ret) {
5377                                 pr_err("%s: Failed to backup %s for rate %lu\n",
5378                                        __func__, bus->name, rate);
5379                                 return -EINVAL;
5380                         }
5381                         clk_rate_change_notify(bus, backup_rate);
5382                 }
5383                 if (p->refcnt) {
5384                         pr_err("%s: %s has other than emc child\n",
5385                                __func__, p->name);
5386                         return -EINVAL;
5387                 }
5388
5389                 if (clk_set_rate(p, parent_rate)) {
5390                         pr_err("%s: Failed to set %s rate %lu\n",
5391                                __func__, p->name, parent_rate);
5392                         return -EINVAL;
5393                 }
5394         }
5395
5396         return clk_set_rate_locked(bus, rate);
5397 }
5398
5399 static struct clk_ops tegra_emc_clk_ops = {
5400         .init                   = &tegra12_emc_clk_init,
5401         .enable                 = &tegra12_periph_clk_enable,
5402         .disable                = &tegra12_periph_clk_disable,
5403         .set_rate               = &tegra12_emc_clk_set_rate,
5404         .round_rate             = &tegra12_emc_clk_round_rate,
5405         .round_rate_updown      = &tegra12_emc_clk_round_updown,
5406         .reset                  = &tegra12_periph_clk_reset,
5407         .shared_bus_update      = &tegra12_clk_emc_bus_update,
5408 };
5409
5410 void tegra_mc_divider_update(struct clk *emc)
5411 {
5412         emc->child_bus->div = (clk_readl(emc->reg) &
5413                                PERIPH_CLK_SOURCE_EMC_MC_SAME) ? 1 : 2;
5414 }
5415
5416 static void tegra12_mc_clk_init(struct clk *c)
5417 {
5418         c->state = ON;
5419         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
5420                 c->state = OFF;
5421
5422         c->parent->child_bus = c;
5423         tegra_mc_divider_update(c->parent);
5424         c->mul = 1;
5425 }
5426
5427 static struct clk_ops tegra_mc_clk_ops = {
5428         .init                   = &tegra12_mc_clk_init,
5429         .enable                 = &tegra12_periph_clk_enable,
5430         .disable                = &tegra12_periph_clk_disable,
5431 };
5432
5433 /* Clock doubler ops (non-atomic shared register access) */
5434 static DEFINE_SPINLOCK(doubler_lock);
5435
5436 static void tegra12_clk_double_init(struct clk *c)
5437 {
5438         u32 val = clk_readl(c->reg);
5439         c->mul = val & (0x1 << c->reg_shift) ? 1 : 2;
5440         c->div = 1;
5441         c->state = ON;
5442         if (!(clk_readl(PERIPH_CLK_TO_ENB_REG(c)) & PERIPH_CLK_TO_BIT(c)))
5443                 c->state = OFF;
5444 };
5445
5446 static int tegra12_clk_double_set_rate(struct clk *c, unsigned long rate)
5447 {
5448         u32 val;
5449         unsigned long parent_rate = clk_get_rate(c->parent);
5450         unsigned long flags;
5451
5452         if (rate == parent_rate) {
5453                 spin_lock_irqsave(&doubler_lock, flags);
5454                 val = clk_readl(c->reg) | (0x1 << c->reg_shift);
5455                 clk_writel(val, c->reg);
5456                 c->mul = 1;
5457                 c->div = 1;
5458                 spin_unlock_irqrestore(&doubler_lock, flags);
5459                 return 0;
5460         } else if (rate == 2 * parent_rate) {
5461                 spin_lock_irqsave(&doubler_lock, flags);
5462                 val = clk_readl(c->reg) & (~(0x1 << c->reg_shift));
5463                 clk_writel(val, c->reg);
5464                 c->mul = 2;
5465                 c->div = 1;
5466                 spin_unlock_irqrestore(&doubler_lock, flags);
5467                 return 0;
5468         }
5469         return -EINVAL;
5470 }
5471
5472 static struct clk_ops tegra_clk_double_ops = {
5473         .init                   = &tegra12_clk_double_init,
5474         .enable                 = &tegra12_periph_clk_enable,
5475         .disable                = &tegra12_periph_clk_disable,
5476         .set_rate               = &tegra12_clk_double_set_rate,
5477 };
5478
5479 /* Audio sync clock ops */
5480 static int tegra12_sync_source_set_rate(struct clk *c, unsigned long rate)
5481 {
5482         c->rate = rate;
5483         return 0;
5484 }
5485
5486 static struct clk_ops tegra_sync_source_ops = {
5487         .set_rate               = &tegra12_sync_source_set_rate,
5488 };
5489
5490 static void tegra12_audio_sync_clk_init(struct clk *c)
5491 {
5492         int source;
5493         const struct clk_mux_sel *sel;
5494         u32 val = clk_readl(c->reg);
5495         c->state = (val & AUDIO_SYNC_DISABLE_BIT) ? OFF : ON;
5496         source = val & AUDIO_SYNC_SOURCE_MASK;
5497         for (sel = c->inputs; sel->input != NULL; sel++)
5498                 if (sel->value == source)
5499                         break;
5500         BUG_ON(sel->input == NULL);
5501         c->parent = sel->input;
5502 }
5503
5504 static int tegra12_audio_sync_clk_enable(struct clk *c)
5505 {
5506         u32 val = clk_readl(c->reg);
5507         clk_writel((val & (~AUDIO_SYNC_DISABLE_BIT)), c->reg);
5508         return 0;
5509 }
5510
5511 static void tegra12_audio_sync_clk_disable(struct clk *c)
5512 {
5513         u32 val = clk_readl(c->reg);
5514         clk_writel((val | AUDIO_SYNC_DISABLE_BIT), c->reg);
5515 }
5516
5517 static int tegra12_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
5518 {
5519         u32 val;
5520         const struct clk_mux_sel *sel;
5521         for (sel = c->inputs; sel->input != NULL; sel++) {
5522                 if (sel->input == p) {
5523                         val = clk_readl(c->reg);
5524                         val &= ~AUDIO_SYNC_SOURCE_MASK;
5525                         val |= sel->value;
5526
5527                         if (c->refcnt)
5528                                 clk_enable(p);
5529
5530                         clk_writel(val, c->reg);
5531
5532                         if (c->refcnt && c->parent)
5533                                 clk_disable(c->parent);
5534
5535                         clk_reparent(c, p);
5536                         return 0;
5537                 }
5538         }
5539
5540         return -EINVAL;
5541 }
5542
5543 static struct clk_ops tegra_audio_sync_clk_ops = {
5544         .init       = tegra12_audio_sync_clk_init,
5545         .enable     = tegra12_audio_sync_clk_enable,
5546         .disable    = tegra12_audio_sync_clk_disable,
5547         .set_parent = tegra12_audio_sync_clk_set_parent,
5548 };
5549
5550 /* cml0 (pcie), and cml1 (sata) clock ops */
5551 static void tegra12_cml_clk_init(struct clk *c)
5552 {
5553         u32 val = clk_readl(c->reg);
5554         c->state = val & (0x1 << c->u.periph.clk_num) ? ON : OFF;
5555 }
5556
5557 static int tegra12_cml_clk_enable(struct clk *c)
5558 {
5559         u32 val = clk_readl(c->reg);
5560         val |= (0x1 << c->u.periph.clk_num);
5561         clk_writel(val, c->reg);
5562         return 0;
5563 }
5564
5565 static void tegra12_cml_clk_disable(struct clk *c)
5566 {
5567         u32 val = clk_readl(c->reg);
5568         val &= ~(0x1 << c->u.periph.clk_num);
5569         clk_writel(val, c->reg);
5570 }
5571
5572 static struct clk_ops tegra_cml_clk_ops = {
5573         .init                   = &tegra12_cml_clk_init,
5574         .enable                 = &tegra12_cml_clk_enable,
5575         .disable                = &tegra12_cml_clk_disable,
5576 };
5577
5578
5579 /* cbus ops */
5580 /*
5581  * Some clocks require dynamic re-locking of source PLL in order to
5582  * achieve frequency scaling granularity that matches characterized
5583  * core voltage steps. The cbus clock creates a shared bus that
5584  * provides a virtual root for such clocks to hide and synchronize
5585  * parent PLL re-locking as well as backup operations.
5586 */
5587
5588 static void tegra12_clk_cbus_init(struct clk *c)
5589 {
5590         c->state = OFF;
5591         c->set = true;
5592 }
5593
5594 static int tegra12_clk_cbus_enable(struct clk *c)
5595 {
5596         return 0;
5597 }
5598
5599 static long tegra12_clk_cbus_round_updown(struct clk *c, unsigned long rate,
5600                                           bool up)
5601 {
5602         int i;
5603         const int *millivolts;
5604
5605         if (!c->dvfs) {
5606                 if (!c->min_rate)
5607                         c->min_rate = c->parent->min_rate;
5608                 rate = max(rate, c->min_rate);
5609                 return rate;
5610         }
5611
5612         /* update min now, since no dvfs table was available during init
5613            (skip placeholder entries set to 1 kHz) */
5614         if (!c->min_rate) {
5615                 for (i = 0; i < c->dvfs->num_freqs; i++) {
5616                         if (c->dvfs->freqs[i] > 1 * c->dvfs->freqs_mult) {
5617                                 c->min_rate = c->dvfs->freqs[i];
5618                                 break;
5619                         }
5620                 }
5621                 BUG_ON(!c->min_rate);
5622         }
5623         rate = max(rate, c->min_rate);
5624
5625         millivolts = tegra_dvfs_get_millivolts_pll(c->dvfs);
5626         for (i = 0; ; i++) {
5627                 unsigned long f = c->dvfs->freqs[i];
5628                 int mv = millivolts[i];
5629                 if ((f >= rate) || (mv >= c->dvfs->max_millivolts) ||
5630                     ((i + 1) >=  c->dvfs->num_freqs)) {
5631                         if (!up && i && (f > rate))
5632                                 i--;
5633                         break;
5634                 }
5635         }
5636         return c->dvfs->freqs[i];
5637 }
5638
5639 static long tegra12_clk_cbus_round_rate(struct clk *c, unsigned long rate)
5640 {
5641         return tegra12_clk_cbus_round_updown(c, rate, true);
5642 }
5643
5644 static int cbus_switch_one(struct clk *c, struct clk *p, u32 div, bool abort)
5645 {
5646         int ret = 0;
5647
5648         /* set new divider if it is bigger than the current one */
5649         if (c->div < c->mul * div) {
5650                 ret = clk_set_div(c, div);
5651                 if (ret) {
5652                         pr_err("%s: failed to set %s clock divider %u: %d\n",
5653                                __func__, c->name, div, ret);
5654                         if (abort)
5655                                 return ret;
5656                 }
5657         }
5658
5659         if (c->parent != p) {
5660                 ret = clk_set_parent(c, p);
5661                 if (ret) {
5662                         pr_err("%s: failed to set %s clock parent %s: %d\n",
5663                                __func__, c->name, p->name, ret);
5664                         if (abort)
5665                                 return ret;
5666                 }
5667         }
5668
5669         /* set new divider if it is smaller than the current one */
5670         if (c->div > c->mul * div) {
5671                 ret = clk_set_div(c, div);
5672                 if (ret)
5673                         pr_err("%s: failed to set %s clock divider %u: %d\n",
5674                                __func__, c->name, div, ret);
5675         }
5676
5677         return ret;
5678 }
5679
5680 static int cbus_backup(struct clk *c)
5681 {
5682         int ret;
5683         struct clk *user;
5684
5685         list_for_each_entry(user, &c->shared_bus_list,
5686                         u.shared_bus_user.node) {
5687                 struct clk *client = user->u.shared_bus_user.client;
5688                 if (client && (client->state == ON) &&
5689                     (client->parent == c->parent)) {
5690                         ret = cbus_switch_one(client,
5691                                               c->shared_bus_backup.input,
5692                                               c->shared_bus_backup.value *
5693                                               user->div, true);
5694                         if (ret)
5695                                 return ret;
5696                 }
5697         }
5698         return 0;
5699 }
5700
5701 static int cbus_dvfs_set_rate(struct clk *c, unsigned long rate)
5702 {
5703         int ret;
5704         struct clk *user;
5705
5706         list_for_each_entry(user, &c->shared_bus_list,
5707                         u.shared_bus_user.node) {
5708                 struct clk *client =  user->u.shared_bus_user.client;
5709                 if (client && client->refcnt && (client->parent == c->parent)) {
5710                         ret = tegra_dvfs_set_rate(c, rate);
5711                         if (ret)
5712                                 return ret;
5713                 }
5714         }
5715         return 0;
5716 }
5717
5718 static void cbus_restore(struct clk *c)
5719 {
5720         struct clk *user;
5721
5722         list_for_each_entry(user, &c->shared_bus_list,
5723                         u.shared_bus_user.node) {
5724                 if (user->u.shared_bus_user.client)
5725                         cbus_switch_one(user->u.shared_bus_user.client,
5726                                         c->parent, c->div * user->div, false);
5727         }
5728 }
5729
5730 static int get_next_backup_div(struct clk *c, unsigned long rate)
5731 {
5732         u32 div = c->div;
5733         unsigned long backup_rate = clk_get_rate(c->shared_bus_backup.input);
5734
5735         rate = max(rate, clk_get_rate_locked(c));
5736         rate = rate - (rate >> 2);      /* 25% margin for backup rate */
5737         if ((u64)rate * div < backup_rate)
5738                 div = DIV_ROUND_UP(backup_rate, rate);
5739
5740         BUG_ON(!div);
5741         return div;
5742 }
5743
5744 static int tegra12_clk_cbus_set_rate(struct clk *c, unsigned long rate)
5745 {
5746         int ret;
5747         bool dramp;
5748
5749         if (rate == 0)
5750                 return 0;
5751         if (tegra_platform_is_qt())
5752                 return 0;
5753         ret = clk_enable(c->parent);
5754         if (ret) {
5755                 pr_err("%s: failed to enable %s clock: %d\n",
5756                        __func__, c->name, ret);
5757                 return ret;
5758         }
5759
5760         dramp = tegra12_is_dyn_ramp(c->parent, rate * c->div, false);
5761         if (!dramp) {
5762                 c->shared_bus_backup.value = get_next_backup_div(c, rate);
5763                 ret = cbus_backup(c);
5764                 if (ret)
5765                         goto out;
5766         }
5767
5768         ret = clk_set_rate(c->parent, rate * c->div);
5769         if (ret) {
5770                 pr_err("%s: failed to set %s clock rate %lu: %d\n",
5771                        __func__, c->name, rate, ret);
5772                 goto out;
5773         }
5774
5775         /* Safe voltage setting is taken care of by cbus clock dvfs; the call
5776          * below only records requirements for each enabled client.
5777          */
5778         if (dramp)
5779                 ret = cbus_dvfs_set_rate(c, rate);
5780
5781         cbus_restore(c);
5782
5783 out:
5784         clk_disable(c->parent);
5785         return ret;
5786 }
5787
5788 static inline void cbus_move_enabled_user(
5789         struct clk *user, struct clk *dst, struct clk *src)
5790 {
5791         clk_enable(dst);
5792         list_move_tail(&user->u.shared_bus_user.node, &dst->shared_bus_list);
5793         clk_disable(src);
5794         clk_reparent(user, dst);
5795 }
5796
5797 #ifdef CONFIG_TEGRA_DYNAMIC_CBUS
5798 static int tegra12_clk_cbus_update(struct clk *bus)
5799 {
5800         int ret, mv;
5801         struct clk *slow = NULL;
5802         struct clk *top = NULL;
5803         unsigned long rate;
5804         unsigned long old_rate;
5805         unsigned long ceiling;
5806
5807         if (detach_shared_bus)
5808                 return 0;
5809
5810         rate = tegra12_clk_shared_bus_update(bus, &top, &slow, &ceiling);
5811
5812         /* use dvfs table of the slowest enabled client as cbus dvfs table */
5813         if (bus->dvfs && slow && (slow != bus->u.cbus.slow_user)) {
5814                 int i;
5815                 unsigned long *dest = &bus->dvfs->freqs[0];
5816                 unsigned long *src =
5817                         &slow->u.shared_bus_user.client->dvfs->freqs[0];
5818                 if (slow->div > 1)
5819                         for (i = 0; i < bus->dvfs->num_freqs; i++)
5820                                 dest[i] = src[i] * slow->div;
5821                 else
5822                         memcpy(dest, src, sizeof(*dest) * bus->dvfs->num_freqs);
5823         }
5824
5825         /* update bus state variables and rate */
5826         bus->u.cbus.slow_user = slow;
5827         bus->u.cbus.top_user = top;
5828
5829         rate = tegra12_clk_cap_shared_bus(bus, rate, ceiling);
5830         mv = tegra_dvfs_predict_millivolts(bus, rate);
5831         if (IS_ERR_VALUE(mv))
5832                 return -EINVAL;
5833
5834         if (bus->dvfs) {
5835                 mv -= bus->dvfs->cur_millivolts;
5836                 if (bus->refcnt && (mv > 0)) {
5837                         ret = tegra_dvfs_set_rate(bus, rate);
5838                         if (ret)
5839                                 return ret;
5840                 }
5841         }
5842
5843         old_rate = clk_get_rate_locked(bus);
5844         if (IS_ENABLED(CONFIG_TEGRA_MIGRATE_CBUS_USERS) || (old_rate != rate)) {
5845                 ret = bus->ops->set_rate(bus, rate);
5846                 if (ret)
5847                         return ret;
5848         }
5849
5850         if (bus->dvfs) {
5851                 if (bus->refcnt && (mv <= 0)) {
5852                         ret = tegra_dvfs_set_rate(bus, rate);
5853                         if (ret)
5854                                 return ret;
5855                 }
5856         }
5857
5858         clk_rate_change_notify(bus, rate);
5859         return 0;
5860 };
5861 #else
5862 static int tegra12_clk_cbus_update(struct clk *bus)
5863 {
5864         unsigned long rate, old_rate;
5865
5866         if (detach_shared_bus)
5867                 return 0;
5868
5869         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
5870
5871         old_rate = clk_get_rate_locked(bus);
5872         if (rate == old_rate)
5873                 return 0;
5874
5875         return clk_set_rate_locked(bus, rate);
5876 }
5877 #endif
5878
5879 static int tegra12_clk_cbus_migrate_users(struct clk *user)
5880 {
5881 #ifdef CONFIG_TEGRA_MIGRATE_CBUS_USERS
5882         struct clk *src_bus, *dst_bus, *top_user, *c;
5883         struct list_head *pos, *n;
5884
5885         if (!user->u.shared_bus_user.client || !user->inputs)
5886                 return 0;
5887
5888         /* Dual cbus on Tegra12 */
5889         src_bus = user->inputs[0].input;
5890         dst_bus = user->inputs[1].input;
5891
5892         if (!src_bus->u.cbus.top_user && !dst_bus->u.cbus.top_user)
5893                 return 0;
5894
5895         /* Make sure top user on the source bus is requesting highest rate */
5896         if (!src_bus->u.cbus.top_user || (dst_bus->u.cbus.top_user &&
5897                 bus_user_request_is_lower(src_bus->u.cbus.top_user,
5898                                            dst_bus->u.cbus.top_user)))
5899                 swap(src_bus, dst_bus);
5900
5901         /* If top user is the slow one on its own (source) bus, do nothing */
5902         top_user = src_bus->u.cbus.top_user;
5903         BUG_ON(!top_user->u.shared_bus_user.client);
5904         if (!bus_user_is_slower(src_bus->u.cbus.slow_user, top_user))
5905                 return 0;
5906
5907         /* If source bus top user is slower than all users on destination bus,
5908            move top user; otherwise move all users slower than the top one */
5909         if (!dst_bus->u.cbus.slow_user ||
5910             !bus_user_is_slower(dst_bus->u.cbus.slow_user, top_user)) {
5911                 cbus_move_enabled_user(top_user, dst_bus, src_bus);
5912         } else {
5913                 list_for_each_safe(pos, n, &src_bus->shared_bus_list) {
5914                         c = list_entry(pos, struct clk, u.shared_bus_user.node);
5915                         if (c->u.shared_bus_user.enabled &&
5916                             c->u.shared_bus_user.client &&
5917                             bus_user_is_slower(c, top_user))
5918                                 cbus_move_enabled_user(c, dst_bus, src_bus);
5919                 }
5920         }
5921
5922         /* Update destination bus 1st (move clients), then source */
5923         tegra_clk_shared_bus_update(dst_bus);
5924         tegra_clk_shared_bus_update(src_bus);
5925 #endif
5926         return 0;
5927 }
5928
5929 static struct clk_ops tegra_clk_cbus_ops = {
5930         .init = tegra12_clk_cbus_init,
5931         .enable = tegra12_clk_cbus_enable,
5932         .set_rate = tegra12_clk_cbus_set_rate,
5933         .round_rate = tegra12_clk_cbus_round_rate,
5934         .round_rate_updown = tegra12_clk_cbus_round_updown,
5935         .shared_bus_update = tegra12_clk_cbus_update,
5936 };
5937
5938 /* shared bus ops */
5939 /*
5940  * Some clocks may have multiple downstream users that need to request a
5941  * higher clock rate.  Shared bus clocks provide a unique shared_bus_user
5942  * clock to each user.  The frequency of the bus is set to the highest
5943  * enabled shared_bus_user clock, with a minimum value set by the
5944  * shared bus.
5945  *
5946  * Optionally shared bus may support users migration. Since shared bus and
5947  * its * children (users) have reversed rate relations: user rates determine
5948  * bus rate, * switching user from one parent/bus to another may change rates
5949  * of both parents. Therefore we need a cross-bus lock on top of individual
5950  * user and bus locks. For now, limit bus switch support to cbus only if
5951  * CONFIG_TEGRA_MIGRATE_CBUS_USERS is set.
5952  */
5953
5954 static unsigned long tegra12_clk_shared_bus_update(struct clk *bus,
5955         struct clk **bus_top, struct clk **bus_slow, unsigned long *rate_cap)
5956 {
5957         struct clk *c;
5958         struct clk *slow = NULL;
5959         struct clk *top = NULL;
5960
5961         unsigned long override_rate = 0;
5962         unsigned long top_rate = 0;
5963         unsigned long rate = bus->min_rate;
5964         unsigned long bw = 0;
5965         unsigned long iso_bw = 0;
5966         unsigned long ceiling = bus->max_rate;
5967         unsigned long ceiling_but_iso = bus->max_rate;
5968         u32 usage_flags = 0;
5969         bool rate_set = false;
5970
5971         list_for_each_entry(c, &bus->shared_bus_list,
5972                         u.shared_bus_user.node) {
5973                 bool cap_user = (c->u.shared_bus_user.mode == SHARED_CEILING) ||
5974                         (c->u.shared_bus_user.mode == SHARED_CEILING_BUT_ISO);
5975                 /*
5976                  * Ignore requests from disabled floor and bw users, and from
5977                  * auto-users riding the bus. Always honor ceiling users, even
5978                  * if they are disabled - we do not want to keep enabled parent
5979                  * bus just because ceiling is set. Ignore SCLK/AHB/APB dividers
5980                  * to propagate flat max request.
5981                  */
5982                 if (c->u.shared_bus_user.enabled || cap_user) {
5983                         unsigned long request_rate = c->u.shared_bus_user.rate;
5984                         if (!(c->flags & DIV_BUS))
5985                                 request_rate *= c->div ? : 1;
5986                         usage_flags |= c->u.shared_bus_user.usage_flag;
5987
5988                         if (!(c->flags & BUS_RATE_LIMIT))
5989                                 rate_set = true;
5990
5991                         switch (c->u.shared_bus_user.mode) {
5992                         case SHARED_ISO_BW:
5993                                 iso_bw += request_rate;
5994                                 if (iso_bw > bus->max_rate)
5995                                         iso_bw = bus->max_rate;
5996                                 /* fall thru */
5997                         case SHARED_BW:
5998                                 bw += request_rate;
5999                                 if (bw > bus->max_rate)
6000                                         bw = bus->max_rate;
6001                                 break;
6002                         case SHARED_CEILING_BUT_ISO:
6003                                 ceiling_but_iso =
6004                                         min(request_rate, ceiling_but_iso);
6005                                 break;
6006                         case SHARED_CEILING:
6007                                 ceiling = min(request_rate, ceiling);
6008                                 break;
6009                         case SHARED_OVERRIDE:
6010                                 if (override_rate == 0)
6011                                         override_rate = request_rate;
6012                                 break;
6013                         case SHARED_AUTO:
6014                                 break;
6015                         case SHARED_FLOOR:
6016                         default:
6017                                 rate = max(request_rate, rate);
6018                                 if (c->u.shared_bus_user.client
6019                                                         && request_rate) {
6020                                         if (top_rate < request_rate) {
6021                                                 top_rate = request_rate;
6022                                                 top = c;
6023                                         } else if ((top_rate == request_rate) &&
6024                                                 bus_user_is_slower(c, top)) {
6025                                                 top = c;
6026                                         }
6027                                 }
6028                         }
6029                         if (c->u.shared_bus_user.client &&
6030                                 (!slow || bus_user_is_slower(c, slow)))
6031                                 slow = c;
6032                 }
6033         }
6034
6035         if (bus->flags & PERIPH_EMC_ENB) {
6036                 unsigned long iso_bw_min;
6037                 bw = tegra_emc_apply_efficiency(
6038                         bw, iso_bw, bus->max_rate, usage_flags, &iso_bw_min);
6039                 if (bus->ops && bus->ops->round_rate)
6040                         iso_bw_min = bus->ops->round_rate(bus, iso_bw_min);
6041                 ceiling_but_iso = max(ceiling_but_iso, iso_bw_min);
6042         }
6043
6044         rate = override_rate ? : max(rate, bw);
6045         ceiling = min(ceiling, ceiling_but_iso);
6046         ceiling = override_rate ? bus->max_rate : ceiling;
6047         bus->override_rate = override_rate;
6048
6049         if (bus_top && bus_slow && rate_cap) {
6050                 /* If dynamic bus dvfs table, let the caller to complete
6051                    rounding and aggregation */
6052                 *bus_top = top;
6053                 *bus_slow = slow;
6054                 *rate_cap = ceiling;
6055         } else {
6056                 /*
6057                  * If satic bus dvfs table, complete rounding and aggregation.
6058                  * In case when no user requested bus rate, and bus retention
6059                  * is enabled, don't scale down - keep current rate.
6060                  */
6061                 if (!rate_set && (bus->shared_bus_flags & SHARED_BUS_RETENTION))
6062                         rate = clk_get_rate_locked(bus);
6063                 rate = tegra12_clk_cap_shared_bus(bus, rate, ceiling);
6064         }
6065
6066         return rate;
6067 };
6068
6069 static unsigned long tegra12_clk_cap_shared_bus(struct clk *bus,
6070         unsigned long rate, unsigned long ceiling)
6071 {
6072         if (bus->ops && bus->ops->round_rate_updown)
6073                 ceiling = bus->ops->round_rate_updown(bus, ceiling, false);
6074
6075         rate = min(rate, ceiling);
6076
6077         if (bus->ops && bus->ops->round_rate)
6078                 rate = bus->ops->round_rate(bus, rate);
6079
6080         return rate;
6081 }
6082
6083 static int tegra_clk_shared_bus_migrate_users(struct clk *user)
6084 {
6085         if (detach_shared_bus)
6086                 return 0;
6087
6088         /* Only cbus migration is supported */
6089         if (user->flags & PERIPH_ON_CBUS)
6090                 return tegra12_clk_cbus_migrate_users(user);
6091         return -ENOSYS;
6092 }
6093
6094 static void tegra_clk_shared_bus_user_init(struct clk *c)
6095 {
6096         c->max_rate = c->parent->max_rate;
6097         c->u.shared_bus_user.rate = c->parent->max_rate;
6098         c->state = OFF;
6099         c->set = true;
6100
6101         if ((c->u.shared_bus_user.mode == SHARED_CEILING) ||
6102             (c->u.shared_bus_user.mode == SHARED_CEILING_BUT_ISO)) {
6103                 c->state = ON;
6104                 c->refcnt++;
6105         }
6106
6107         if (c->u.shared_bus_user.client_id) {
6108                 struct clk *client =
6109                         tegra_get_clock_by_name(c->u.shared_bus_user.client_id);
6110                 if (!client) {
6111                         pr_err("%s: could not find clk %s\n", __func__,
6112                                c->u.shared_bus_user.client_id);
6113                         return;
6114                 }
6115
6116                 if ((client->state == ON) && !(client->flags & PERIPH_NO_ENB))
6117                         pr_info("%s: %s client %s left ON\n", __func__,
6118                                 c->parent->name, client->name);
6119
6120                 c->u.shared_bus_user.client = client;
6121                 c->u.shared_bus_user.client->flags |=
6122                         c->parent->flags & PERIPH_ON_CBUS;
6123                 c->flags |= c->parent->flags & PERIPH_ON_CBUS;
6124                 c->div = c->u.shared_bus_user.client_div ? : 1;
6125                 c->mul = 1;
6126         }
6127
6128         list_add_tail(&c->u.shared_bus_user.node,
6129                 &c->parent->shared_bus_list);
6130 }
6131
6132 static int tegra_clk_shared_bus_user_set_parent(struct clk *c, struct clk *p)
6133 {
6134         int ret;
6135         const struct clk_mux_sel *sel;
6136
6137         if (detach_shared_bus)
6138                 return 0;
6139
6140         if (c->parent == p)
6141                 return 0;
6142
6143         if (!(c->inputs && c->cross_clk_mutex && clk_cansleep(c)))
6144                 return -ENOSYS;
6145
6146         for (sel = c->inputs; sel->input != NULL; sel++) {
6147                 if (sel->input == p)
6148                         break;
6149         }
6150         if (!sel->input)
6151                 return -EINVAL;
6152
6153         if (c->refcnt)
6154                 clk_enable(p);
6155
6156         list_move_tail(&c->u.shared_bus_user.node, &p->shared_bus_list);
6157         ret = tegra_clk_shared_bus_update(p);
6158         if (ret) {
6159                 list_move_tail(&c->u.shared_bus_user.node,
6160                                &c->parent->shared_bus_list);
6161                 tegra_clk_shared_bus_update(c->parent);
6162                 clk_disable(p);
6163                 return ret;
6164         }
6165
6166         tegra_clk_shared_bus_update(c->parent);
6167
6168         if (c->refcnt)
6169                 clk_disable(c->parent);
6170
6171         clk_reparent(c, p);
6172
6173         return 0;
6174 }
6175
6176 static int tegra_clk_shared_bus_user_set_rate(struct clk *c, unsigned long rate)
6177 {
6178         int ret;
6179
6180         c->u.shared_bus_user.rate = rate;
6181         ret = tegra_clk_shared_bus_update(c->parent);
6182
6183         if (!ret && c->cross_clk_mutex && clk_cansleep(c))
6184                 tegra_clk_shared_bus_migrate_users(c);
6185
6186         return ret;
6187 }
6188
6189 static long tegra_clk_shared_bus_user_round_rate(
6190         struct clk *c, unsigned long rate)
6191 {
6192         /*
6193          * Defer rounding requests until aggregated. BW users must not be
6194          * rounded at all, others just clipped to bus range (some clients
6195          * may use round api to find limits). Ignore SCLK/AHB and AHB/APB
6196          * dividers to keep flat bus requests propagation.
6197          */
6198         if ((c->u.shared_bus_user.mode != SHARED_BW) &&
6199             (c->u.shared_bus_user.mode != SHARED_ISO_BW)) {
6200                 if (!(c->flags & DIV_BUS) && (c->div > 1))
6201                         rate *= c->div;
6202
6203                 if (rate > c->parent->max_rate)
6204                         rate = c->parent->max_rate;
6205                 else if (rate < c->parent->min_rate)
6206                         rate = c->parent->min_rate;
6207
6208                 if (!(c->flags & DIV_BUS) && (c->div > 1))
6209                         rate /= c->div;
6210         }
6211         return rate;
6212 }
6213
6214 static int tegra_clk_shared_bus_user_enable(struct clk *c)
6215 {
6216         int ret;
6217
6218         c->u.shared_bus_user.enabled = true;
6219         ret = tegra_clk_shared_bus_update(c->parent);
6220         if (!ret && c->u.shared_bus_user.client)
6221                 ret = clk_enable(c->u.shared_bus_user.client);
6222
6223         if (!ret && c->cross_clk_mutex && clk_cansleep(c))
6224                 tegra_clk_shared_bus_migrate_users(c);
6225
6226         return ret;
6227 }
6228
6229 static void tegra_clk_shared_bus_user_disable(struct clk *c)
6230 {
6231         if (c->u.shared_bus_user.client)
6232                 clk_disable(c->u.shared_bus_user.client);
6233         c->u.shared_bus_user.enabled = false;
6234         tegra_clk_shared_bus_update(c->parent);
6235
6236         if (c->cross_clk_mutex && clk_cansleep(c))
6237                 tegra_clk_shared_bus_migrate_users(c);
6238 }
6239
6240 static void tegra_clk_shared_bus_user_reset(struct clk *c, bool assert)
6241 {
6242         if (c->u.shared_bus_user.client) {
6243                 if (c->u.shared_bus_user.client->ops &&
6244                     c->u.shared_bus_user.client->ops->reset)
6245                         c->u.shared_bus_user.client->ops->reset(
6246                                 c->u.shared_bus_user.client, assert);
6247         }
6248 }
6249
6250 static struct clk_ops tegra_clk_shared_bus_user_ops = {
6251         .init = tegra_clk_shared_bus_user_init,
6252         .enable = tegra_clk_shared_bus_user_enable,
6253         .disable = tegra_clk_shared_bus_user_disable,
6254         .set_parent = tegra_clk_shared_bus_user_set_parent,
6255         .set_rate = tegra_clk_shared_bus_user_set_rate,
6256         .round_rate = tegra_clk_shared_bus_user_round_rate,
6257         .reset = tegra_clk_shared_bus_user_reset,
6258 };
6259
6260 /* shared bus connector ops (user/bus connector to cascade shared buses) */
6261 static int tegra12_clk_shared_connector_update(struct clk *bus)
6262 {
6263         unsigned long rate, old_rate;
6264
6265         if (detach_shared_bus)
6266                 return 0;
6267
6268         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
6269
6270         old_rate = clk_get_rate_locked(bus);
6271         if (rate == old_rate)
6272                 return 0;
6273
6274         return clk_set_rate_locked(bus, rate);
6275 }
6276
6277 static struct clk_ops tegra_clk_shared_connector_ops = {
6278         .init = tegra_clk_shared_bus_user_init,
6279         .enable = tegra_clk_shared_bus_user_enable,
6280         .disable = tegra_clk_shared_bus_user_disable,
6281         .set_parent = tegra_clk_shared_bus_user_set_parent,
6282         .set_rate = tegra_clk_shared_bus_user_set_rate,
6283         .round_rate = tegra_clk_shared_bus_user_round_rate,
6284         .reset = tegra_clk_shared_bus_user_reset,
6285         .shared_bus_update = tegra12_clk_shared_connector_update,
6286 };
6287
6288 /* coupled gate ops */
6289 /*
6290  * Some clocks may have common enable/disable control, but run at different
6291  * rates, and have different dvfs tables. Coupled gate clock synchronize
6292  * enable/disable operations for such clocks.
6293  */
6294
6295 static int tegra12_clk_coupled_gate_enable(struct clk *c)
6296 {
6297         int ret;
6298         const struct clk_mux_sel *sel;
6299
6300         BUG_ON(!c->inputs);
6301         pr_debug("%s on clock %s\n", __func__, c->name);
6302
6303         for (sel = c->inputs; sel->input != NULL; sel++) {
6304                 if (sel->input == c->parent)
6305                         continue;
6306
6307                 ret = clk_enable(sel->input);
6308                 if (ret) {
6309                         while (sel != c->inputs) {
6310                                 sel--;
6311                                 if (sel->input == c->parent)
6312                                         continue;
6313                                 clk_disable(sel->input);
6314                         }
6315                         return ret;
6316                 }
6317         }
6318
6319         return tegra12_periph_clk_enable(c);
6320 }
6321
6322 static void tegra12_clk_coupled_gate_disable(struct clk *c)
6323 {
6324         const struct clk_mux_sel *sel;
6325
6326         BUG_ON(!c->inputs);
6327         pr_debug("%s on clock %s\n", __func__, c->name);
6328
6329         tegra12_periph_clk_disable(c);
6330
6331         if (!c->refcnt) /* happens only on boot clean-up: don't propagate */
6332                 return;
6333
6334         for (sel = c->inputs; sel->input != NULL; sel++) {
6335                 if (sel->input == c->parent)
6336                         continue;
6337
6338                 if (sel->input->set)    /* enforce coupling after boot only */
6339                         clk_disable(sel->input);
6340         }
6341 }
6342
6343 static struct clk_ops tegra_clk_coupled_gate_ops = {
6344         .init                   = tegra12_periph_clk_init,
6345         .enable                 = tegra12_clk_coupled_gate_enable,
6346         .disable                = tegra12_clk_coupled_gate_disable,
6347         .reset                  = &tegra12_periph_clk_reset,
6348 };
6349
6350
6351 /*
6352  * AHB and APB shared bus operations
6353  * APB shared bus is a user of AHB shared bus
6354  * AHB shared bus is a user of SCLK complex shared bus
6355  * SCLK/AHB and AHB/APB dividers can be dynamically changed. When AHB and APB
6356  * users requests are propagated to SBUS target rate, current values of the
6357  * dividers are ignored, and flat maximum request is selected as SCLK bus final
6358  * target. Then the dividers will be re-evaluated, based on AHB and APB targets.
6359  * Both AHB and APB buses are always enabled.
6360  */
6361 static void tegra12_clk_ahb_apb_init(struct clk *c, struct clk *bus_clk)
6362 {
6363         tegra_clk_shared_bus_user_init(c);
6364         c->max_rate = bus_clk->max_rate;
6365         c->min_rate = bus_clk->min_rate;
6366         c->mul = bus_clk->mul;
6367         c->div = bus_clk->div;
6368
6369         c->u.shared_bus_user.rate = clk_get_rate(bus_clk);
6370         c->u.shared_bus_user.enabled = true;
6371         c->parent->child_bus = c;
6372 }
6373
6374 static void tegra12_clk_ahb_init(struct clk *c)
6375 {
6376         struct clk *bus_clk = c->parent->u.system.hclk;
6377         tegra12_clk_ahb_apb_init(c, bus_clk);
6378 }
6379
6380 static void tegra12_clk_apb_init(struct clk *c)
6381 {
6382         struct clk *bus_clk = c->parent->parent->u.system.pclk;
6383         tegra12_clk_ahb_apb_init(c, bus_clk);
6384 }
6385
6386 static int tegra12_clk_ahb_apb_update(struct clk *bus)
6387 {
6388         unsigned long rate;
6389
6390         if (detach_shared_bus)
6391                 return 0;
6392
6393         rate = tegra12_clk_shared_bus_update(bus, NULL, NULL, NULL);
6394         return clk_set_rate_locked(bus, rate);
6395 }
6396
6397 static struct clk_ops tegra_clk_ahb_ops = {
6398         .init = tegra12_clk_ahb_init,
6399         .set_rate = tegra_clk_shared_bus_user_set_rate,
6400         .round_rate = tegra_clk_shared_bus_user_round_rate,
6401         .shared_bus_update = tegra12_clk_ahb_apb_update,
6402 };
6403
6404 static struct clk_ops tegra_clk_apb_ops = {
6405         .init = tegra12_clk_apb_init,
6406         .set_rate = tegra_clk_shared_bus_user_set_rate,
6407         .round_rate = tegra_clk_shared_bus_user_round_rate,
6408         .shared_bus_update = tegra12_clk_ahb_apb_update,
6409 };
6410
6411 /* Clock definitions */
6412 static struct clk tegra_clk_32k = {
6413         .name = "clk_32k",
6414         .rate = 32768,
6415         .ops  = NULL,
6416         .max_rate = 32768,
6417 };
6418
6419 static struct clk tegra_clk_m = {
6420         .name      = "clk_m",
6421         .flags     = ENABLE_ON_INIT,
6422         .ops       = &tegra_clk_m_ops,
6423         .max_rate  = 48000000,
6424 };
6425
6426 static struct clk tegra_clk_m_div2 = {
6427         .name      = "clk_m_div2",
6428         .ops       = &tegra_clk_m_div_ops,
6429         .parent    = &tegra_clk_m,
6430         .mul       = 1,
6431         .div       = 2,
6432         .state     = ON,
6433         .max_rate  = 24000000,
6434 };
6435
6436 static struct clk tegra_clk_m_div4 = {
6437         .name      = "clk_m_div4",
6438         .ops       = &tegra_clk_m_div_ops,
6439         .parent    = &tegra_clk_m,
6440         .mul       = 1,
6441         .div       = 4,
6442         .state     = ON,
6443         .max_rate  = 12000000,
6444 };
6445
6446 static struct clk tegra_pll_ref = {
6447         .name      = "pll_ref",
6448         .flags     = ENABLE_ON_INIT,
6449         .ops       = &tegra_pll_ref_ops,
6450         .parent    = &tegra_clk_m,
6451         .max_rate  = 26000000,
6452 };
6453
6454 static struct clk_pll_freq_table tegra_pll_c_freq_table[] = {
6455         { 12000000, 600000000, 100, 1, 2},
6456         { 13000000, 600000000,  92, 1, 2},      /* actual: 598.0 MHz */
6457         { 16800000, 600000000,  71, 1, 2},      /* actual: 596.4 MHz */
6458         { 19200000, 600000000,  62, 1, 2},      /* actual: 595.2 MHz */
6459         { 26000000, 600000000,  92, 2, 2},      /* actual: 598.0 MHz */
6460         { 0, 0, 0, 0, 0, 0 },
6461 };
6462
6463 static struct clk tegra_pll_c = {
6464         .name      = "pll_c",
6465         .ops       = &tegra_pllxc_ops,
6466         .reg       = 0x80,
6467         .parent    = &tegra_pll_ref,
6468         .max_rate  = 1400000000,
6469         .u.pll = {
6470                 .input_min = 12000000,
6471                 .input_max = 800000000,
6472                 .cf_min    = 12000000,
6473                 .cf_max    = 19200000,  /* s/w policy, h/w capability 50 MHz */
6474                 .vco_min   = 600000000,
6475                 .vco_max   = 1400000000,
6476                 .freq_table = tegra_pll_c_freq_table,
6477                 .lock_delay = 300,
6478                 .misc1 = 0x88 - 0x80,
6479                 .round_p_to_pdiv = pllxc_round_p_to_pdiv,
6480         },
6481 };
6482
6483 static struct clk tegra_pll_c_out1 = {
6484         .name      = "pll_c_out1",
6485         .ops       = &tegra_pll_div_ops,
6486 #ifdef CONFIG_TEGRA_DUAL_CBUS
6487         .flags     = DIV_U71,
6488 #else
6489         .flags     = DIV_U71 | PERIPH_ON_CBUS,
6490 #endif
6491         .parent    = &tegra_pll_c,
6492         .reg       = 0x84,
6493         .reg_shift = 0,
6494         .max_rate  = 700000000,
6495 };
6496
6497 static struct clk_pll_freq_table tegra_pll_cx_freq_table[] = {
6498         { 12000000, 600000000, 100, 1, 2},
6499         { 13000000, 600000000,  92, 1, 2},      /* actual: 598.0 MHz */
6500         { 16800000, 600000000,  71, 1, 2},      /* actual: 596.4 MHz */
6501         { 19200000, 600000000,  62, 1, 2},      /* actual: 595.2 MHz */
6502         { 26000000, 600000000,  92, 2, 2},      /* actual: 598.0 MHz */
6503         { 0, 0, 0, 0, 0, 0 },
6504 };
6505
6506 static struct clk tegra_pll_c2 = {
6507         .name      = "pll_c2",
6508         .ops       = &tegra_pllcx_ops,
6509         .flags     = PLL_ALT_MISC_REG,
6510         .reg       = 0x4e8,
6511         .parent    = &tegra_pll_ref,
6512         .max_rate  = 1200000000,
6513         .u.pll = {
6514                 .input_min = 12000000,
6515                 .input_max = 48000000,
6516                 .cf_min    = 12000000,
6517                 .cf_max    = 19200000,
6518                 .vco_min   = 650000000,
6519                 .vco_max   = 1300000000,
6520                 .freq_table = tegra_pll_cx_freq_table,
6521                 .lock_delay = 360,
6522                 .misc1 = 0x4f0 - 0x4e8,
6523                 .round_p_to_pdiv = pllcx_round_p_to_pdiv,
6524         },
6525 };
6526
6527 static struct clk tegra_pll_c3 = {
6528         .name      = "pll_c3",
6529         .ops       = &tegra_pllcx_ops,
6530         .flags     = PLL_ALT_MISC_REG,
6531         .reg       = 0x4fc,
6532         .parent    = &tegra_pll_ref,
6533         .max_rate  = 1200000000,
6534         .u.pll = {
6535                 .input_min = 12000000,
6536                 .input_max = 48000000,
6537                 .cf_min    = 12000000,
6538                 .cf_max    = 19200000,
6539                 .vco_min   = 650000000,
6540                 .vco_max   = 1300000000,
6541                 .freq_table = tegra_pll_cx_freq_table,
6542                 .lock_delay = 360,
6543                 .misc1 = 0x504 - 0x4fc,
6544                 .round_p_to_pdiv = pllcx_round_p_to_pdiv,
6545         },
6546 };
6547
6548 static struct clk_pll_freq_table tegra_pll_m_freq_table[] = {
6549         { 12000000, 800000000, 66, 1, 1},       /* actual: 792.0 MHz */
6550         { 13000000, 800000000, 61, 1, 1},       /* actual: 793.0 MHz */
6551         { 16800000, 800000000, 47, 1, 1},       /* actual: 789.6 MHz */
6552         { 19200000, 800000000, 41, 1, 1},       /* actual: 787.2 MHz */
6553         { 26000000, 800000000, 61, 2, 1},       /* actual: 793.0 MHz */
6554         { 0, 0, 0, 0, 0, 0 },
6555 };
6556
6557 static struct clk tegra_pll_m = {
6558         .name      = "pll_m",
6559         .flags     = PLLM,
6560         .ops       = &tegra_pllm_ops,
6561         .reg       = 0x90,
6562         .parent    = &tegra_pll_ref,
6563         .max_rate  = 1200000000,
6564         .u.pll = {
6565                 .input_min = 12000000,
6566                 .input_max = 500000000,
6567                 .cf_min    = 12000000,
6568                 .cf_max    = 19200000,  /* s/w policy, h/w capability 50 MHz */
6569                 .vco_min   = 500000000,
6570                 .vco_max   = 1200000000,
6571                 .freq_table = tegra_pll_m_freq_table,
6572                 .lock_delay = 300,
6573                 .misc1 = 0x98 - 0x90,
6574                 .round_p_to_pdiv = pllm_round_p_to_pdiv,
6575         },
6576 };
6577
6578 static struct clk tegra_pll_m_out1 = {
6579         .name      = "pll_m_out1",
6580         .ops       = &tegra_pll_div_ops,
6581         .flags     = DIV_U71 | DIV_U71_INT,
6582         .parent    = &tegra_pll_m,
6583         .reg       = 0x94,
6584         .reg_shift = 0,
6585         .max_rate  = 1200000000,
6586 };
6587
6588 static struct clk_pll_freq_table tegra_pll_p_freq_table[] = {
6589         { 12000000, 408000000, 816, 12, 2, 8},
6590         { 13000000, 408000000, 816, 13, 2, 8},
6591         { 16800000, 408000000, 680, 14, 2, 8},
6592         { 19200000, 408000000, 680, 16, 2, 8},
6593         { 26000000, 408000000, 816, 26, 2, 8},
6594         { 0, 0, 0, 0, 0, 0 },
6595 };
6596
6597 static struct clk tegra_pll_p = {
6598         .name      = "pll_p",
6599         .flags     = ENABLE_ON_INIT | PLL_FIXED | PLL_HAS_CPCON,
6600         .ops       = &tegra_pllp_ops,
6601         .reg       = 0xa0,
6602         .parent    = &tegra_pll_ref,
6603         .max_rate  = 432000000,
6604         .u.pll = {
6605                 .input_min = 2000000,
6606                 .input_max = 31000000,
6607                 .cf_min    = 1000000,
6608                 .cf_max    = 6000000,
6609                 .vco_min   = 200000000,
6610                 .vco_max   = 700000000,
6611                 .freq_table = tegra_pll_p_freq_table,
6612                 .lock_delay = 300,
6613         },
6614 };
6615
6616 static struct clk tegra_pll_p_out1 = {
6617         .name      = "pll_p_out1",
6618         .ops       = &tegra_pll_div_ops,
6619         .flags     = DIV_U71 | DIV_U71_FIXED,
6620         .parent    = &tegra_pll_p,
6621         .reg       = 0xa4,
6622         .reg_shift = 0,
6623         .max_rate  = 432000000,
6624 };
6625
6626 static struct clk tegra_pll_p_out2 = {
6627         .name      = "pll_p_out2",
6628         .ops       = &tegra_pll_div_ops,
6629         .flags     = DIV_U71 | DIV_U71_FIXED | DIV_U71_INT,
6630         .parent    = &tegra_pll_p,
6631         .reg       = 0xa4,
6632         .reg_shift = 16,
6633         .max_rate  = 432000000,
6634 };
6635
6636 static struct clk tegra_pll_p_out3 = {
6637         .name      = "pll_p_out3",
6638         .ops       = &tegra_pll_div_ops,
6639         .flags     = DIV_U71 | DIV_U71_FIXED,
6640         .parent    = &tegra_pll_p,
6641         .reg       = 0xa8,
6642         .reg_shift = 0,
6643         .max_rate  = 432000000,
6644 };
6645
6646 static struct clk tegra_pll_p_out4 = {
6647         .name      = "pll_p_out4",
6648         .ops       = &tegra_pll_div_ops,
6649         .flags     = DIV_U71 | DIV_U71_FIXED,
6650         .parent    = &tegra_pll_p,
6651         .reg       = 0xa8,
6652         .reg_shift = 16,
6653         .max_rate  = 432000000,
6654 };
6655
6656 static struct clk tegra_pll_p_out5 = {
6657         .name      = "pll_p_out5",
6658         .ops       = &tegra_pll_div_ops,
6659         .flags     = DIV_U71 | DIV_U71_FIXED,
6660         .parent    = &tegra_pll_p,
6661         .reg       = 0x67c,
6662         .reg_shift = 16,
6663         .max_rate  = 432000000,
6664 };
6665
6666 static struct clk_pll_freq_table tegra_pll_a_freq_table[] = {
6667         {  9600000, 282240000, 147,  5, 1, 4},
6668         {  9600000, 368640000, 192,  5, 1, 4},
6669         {  9600000, 240000000, 200,  8, 1, 8},
6670
6671         { 28800000, 282240000, 245, 25, 1, 8},
6672         { 28800000, 368640000, 320, 25, 1, 8},
6673         { 28800000, 240000000, 200, 24, 1, 8},
6674         { 0, 0, 0, 0, 0, 0 },
6675 };
6676
6677 static struct clk tegra_pll_a = {
6678         .name      = "pll_a",
6679         .flags     = PLL_HAS_CPCON,
6680         .ops       = &tegra_pll_ops,
6681         .reg       = 0xb0,
6682         .parent    = &tegra_pll_p_out1,
6683         .max_rate  = 700000000,
6684         .u.pll = {
6685                 .input_min = 2000000,
6686                 .input_max = 31000000,
6687                 .cf_min    = 1000000,
6688                 .cf_max    = 6000000,
6689                 .vco_min   = 200000000,
6690                 .vco_max   = 700000000,
6691                 .freq_table = tegra_pll_a_freq_table,
6692                 .lock_delay = 300,
6693         },
6694 };
6695
6696 static struct clk tegra_pll_a_out0 = {
6697         .name      = "pll_a_out0",
6698         .ops       = &tegra_pll_div_ops,
6699         .flags     = DIV_U71,
6700         .parent    = &tegra_pll_a,
6701         .reg       = 0xb4,
6702         .reg_shift = 0,
6703         .max_rate  = 100000000,
6704 };
6705
6706 static struct clk_pll_freq_table tegra_pll_d_freq_table[] = {
6707         { 12000000, 216000000, 864, 12, 4, 12},
6708         { 13000000, 216000000, 864, 13, 4, 12},
6709         { 16800000, 216000000, 720, 14, 4, 12},
6710         { 19200000, 216000000, 720, 16, 4, 12},
6711         { 26000000, 216000000, 864, 26, 4, 12},
6712
6713         { 12000000, 594000000,  99,  2, 1,  8},
6714         { 13000000, 594000000, 594, 13, 1, 12},
6715         { 16800000, 594000000, 495, 14, 1, 12},
6716         { 19200000, 594000000, 495, 16, 1, 12},
6717         { 26000000, 594000000, 594, 26, 1, 12},
6718
6719         { 12000000, 1000000000, 1000, 12, 1, 12},
6720         { 13000000, 1000000000, 1000, 13, 1, 12},
6721         { 19200000, 1000000000, 625,  12, 1, 12},
6722         { 26000000, 1000000000, 1000, 26, 1, 12},
6723
6724         { 0, 0, 0, 0, 0, 0 },
6725 };
6726
6727 static struct clk tegra_pll_d = {
6728         .name      = "pll_d",
6729         .flags     = PLL_HAS_CPCON | PLLD,
6730         .ops       = &tegra_plld_ops,
6731         .reg       = 0xd0,
6732         .parent    = &tegra_pll_ref,
6733         .max_rate  = 1500000000,
6734         .u.pll = {
6735                 .input_min = 2000000,
6736                 .input_max = 40000000,
6737                 .cf_min    = 1000000,
6738                 .cf_max    = 6000000,
6739                 .vco_min   = 500000000,
6740                 .vco_max   = 1500000000,
6741                 .freq_table = tegra_pll_d_freq_table,
6742                 .lock_delay = 1000,
6743         },
6744 };
6745
6746 static struct clk tegra_pll_d_out0 = {
6747         .name      = "pll_d_out0",
6748         .ops       = &tegra_pll_div_ops,
6749         .flags     = DIV_2 | PLLD,
6750         .parent    = &tegra_pll_d,
6751         .max_rate  = 750000000,
6752 };
6753
6754 static struct clk_pll_freq_table tegra_pll_u_freq_table[] = {
6755         { 12000000, 480000000, 960, 12, 2, 12},
6756         { 13000000, 480000000, 960, 13, 2, 12},
6757         { 16800000, 480000000, 400, 7,  2, 5},
6758         { 19200000, 480000000, 200, 4,  2, 3},
6759         { 26000000, 480000000, 960, 26, 2, 12},
6760         { 0, 0, 0, 0, 0, 0 },
6761 };
6762
6763 static struct clk tegra_pll_u = {
6764         .name      = "pll_u",
6765         .flags     = PLL_HAS_CPCON | PLLU,
6766         .ops       = &tegra_pll_ops,
6767         .reg       = 0xc0,
6768         .parent    = &tegra_pll_ref,
6769         .max_rate  = 480000000,
6770         .u.pll = {
6771                 .input_min = 2000000,
6772                 .input_max = 40000000,
6773                 .cf_min    = 1000000,
6774                 .cf_max    = 6000000,
6775                 .vco_min   = 480000000,
6776                 .vco_max   = 960000000,
6777                 .freq_table = tegra_pll_u_freq_table,
6778                 .lock_delay = 1000,
6779         },
6780 };
6781
6782 static struct clk tegra_pll_u_480M = {
6783         .name      = "pll_u_480M",
6784         .flags     = PLLU,
6785         .ops       = &tegra_pll_div_ops,
6786         .reg       = 0xc0,
6787         .reg_shift = 22,
6788         .parent    = &tegra_pll_u,
6789         .mul       = 1,
6790         .div       = 1,
6791         .max_rate  = 480000000,
6792 };
6793
6794 static struct clk tegra_pll_u_60M = {
6795         .name      = "pll_u_60M",
6796         .flags     = PLLU,
6797         .ops       = &tegra_pll_div_ops,
6798         .reg       = 0xc0,
6799         .reg_shift = 23,
6800         .parent    = &tegra_pll_u,
6801         .mul       = 1,
6802         .div       = 8,
6803         .max_rate  = 60000000,
6804 };
6805
6806 static struct clk tegra_pll_u_48M = {
6807         .name      = "pll_u_48M",
6808         .flags     = PLLU,
6809         .ops       = &tegra_pll_div_ops,
6810         .reg       = 0xc0,
6811         .reg_shift = 25,
6812         .parent    = &tegra_pll_u,
6813         .mul       = 1,
6814         .div       = 10,
6815         .max_rate  = 48000000,
6816 };
6817
6818 static struct clk tegra_pll_u_12M = {
6819         .name      = "pll_u_12M",
6820         .flags     = PLLU,
6821         .ops       = &tegra_pll_div_ops,
6822         .reg       = 0xc0,
6823         .reg_shift = 21,
6824         .parent    = &tegra_pll_u,
6825         .mul       = 1,
6826         .div       = 40,
6827         .max_rate  = 12000000,
6828 };
6829
6830 static struct clk_pll_freq_table tegra_pll_x_freq_table[] = {
6831         /* 1 GHz */
6832         { 12000000, 1000000000, 83, 1, 1},      /* actual: 996.0 MHz */
6833         { 13000000, 1000000000, 76, 1, 1},      /* actual: 988.0 MHz */
6834         { 16800000, 1000000000, 59, 1, 1},      /* actual: 991.2 MHz */
6835         { 19200000, 1000000000, 52, 1, 1},      /* actual: 998.4 MHz */
6836         { 26000000, 1000000000, 76, 2, 1},      /* actual: 988.0 MHz */
6837
6838         { 0, 0, 0, 0, 0, 0 },
6839 };
6840
6841 static struct clk tegra_pll_x = {
6842         .name      = "pll_x",
6843         .flags     = PLL_ALT_MISC_REG | PLLX,
6844         .ops       = &tegra_pllxc_ops,
6845 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
6846         .reg       = 0xe0,
6847 #else
6848         .reg       = 0x0,
6849 #endif
6850         .parent    = &tegra_pll_ref,
6851         .max_rate  = 3000000000UL,
6852         .u.pll = {
6853                 .input_min = 12000000,
6854                 .input_max = 800000000,
6855                 .cf_min    = 12000000,
6856                 .cf_max    = 19200000,  /* s/w policy, h/w capability 50 MHz */
6857                 .vco_min   = 1200000000,
6858                 .vco_max   = 3000000000UL,
6859                 .freq_table = tegra_pll_x_freq_table,
6860                 .lock_delay = 300,
6861 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
6862                 .misc1 = 0x510 - 0xe0,
6863 #else
6864                 .misc1 = 0x8,
6865 #endif
6866                 .round_p_to_pdiv = pllxc_round_p_to_pdiv,
6867         },
6868 };
6869
6870 static struct clk tegra_pll_x_out0 = {
6871         .name      = "pll_x_out0",
6872         .ops       = &tegra_pll_div_ops,
6873         .flags     = DIV_2 | PLLX,
6874         .parent    = &tegra_pll_x,
6875         .max_rate  = 1500000000UL,
6876 };
6877
6878 static struct clk tegra_dfll_cpu = {
6879         .name      = "dfll_cpu",
6880         .flags     = DFLL,
6881         .ops       = &tegra_dfll_ops,
6882 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
6883         .reg       = 0x2f4,
6884 #else
6885         .reg       = 0x80,
6886 #endif
6887         .max_rate  = 3000000000UL,
6888 };
6889
6890 static struct clk_pll_freq_table tegra_pllc4_freq_table[] = {
6891         { 12000000, 600000000, 100, 1, 2},
6892         { 13000000, 600000000,  92, 1, 2},      /* actual: 598.0 MHz */
6893         { 16800000, 600000000,  71, 1, 2},      /* actual: 596.4 MHz */
6894         { 19200000, 600000000,  62, 1, 2},      /* actual: 595.2 MHz */
6895         { 26000000, 600000000,  92, 2, 2},      /* actual: 598.0 MHz */
6896         { 0, 0, 0, 0, 0, 0 },
6897 };
6898
6899 static struct clk tegra_pll_c4 = {
6900         .name      = "pll_c4",
6901         .flags     = PLL_ALT_MISC_REG,
6902         .ops       = &tegra_pllss_ops,
6903         .reg       = 0x5a4,
6904         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
6905         .max_rate  = 600000000,
6906         .u.pll = {
6907                 .input_min = 12000000,
6908                 .input_max = 1000000000,
6909                 .cf_min    = 12000000,
6910                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
6911                 .vco_min   = 600000000,
6912                 .vco_max   = 1200000000,
6913                 .freq_table = tegra_pllc4_freq_table,
6914                 .lock_delay = 300,
6915                 .misc1 = 0x8,
6916                 .round_p_to_pdiv = pllss_round_p_to_pdiv,
6917                 .cpcon_default = 0,
6918         },
6919 };
6920
6921 static struct clk_pll_freq_table tegra_plldp_freq_table[] = {
6922         { 12000000, 270000000,  90, 1, 4},
6923         { 13000000, 270000000,  83, 1, 4},      /* actual: 269.8 MHz */
6924         { 16800000, 270000000,  96, 1, 6},      /* actual: 268.8 MHz */
6925         { 19200000, 270000000,  84, 1, 6},      /* actual: 268.8 MHz */
6926         { 26000000, 270000000,  83, 2, 4},      /* actual: 269.8 MHz */
6927         { 0, 0, 0, 0, 0, 0 },
6928 };
6929
6930 static struct clk tegra_pll_dp = {
6931         .name      = "pll_dp",
6932         .flags     = PLL_ALT_MISC_REG,
6933         .ops       = &tegra_pllss_ops,
6934         .reg       = 0x590,
6935         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
6936         .max_rate  = 600000000,
6937         .u.pll = {
6938                 .input_min = 12000000,
6939                 .input_max = 1000000000,
6940                 .cf_min    = 12000000,
6941                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
6942                 .vco_min   = 600000000,
6943                 .vco_max   = 1200000000,
6944                 .freq_table = tegra_plldp_freq_table,
6945                 .lock_delay = 300,
6946                 .misc1 = 0x8,
6947                 .round_p_to_pdiv = pllss_round_p_to_pdiv,
6948                 .cpcon_default = 0,
6949         },
6950 };
6951
6952 static struct clk_pll_freq_table tegra_plld2_freq_table[] = {
6953         { 12000000, 594000000,  99, 1, 2,  3}, /*4kx2k @ 30Hz*/
6954         { 12000000, 297000000,  99, 1, 4,  1}, /*1080p @ 60Hz*/
6955         { 12000000, 148500000,  99, 1, 8,  1}, /* 720p @ 60Hz*/
6956         { 12000000,  54000000,  54, 1, 6,  1}, /* 480p @ 60Hz*/
6957         { 13000000, 594000000,  91, 1, 2},      /* actual: 591.5 MHz */
6958         { 16800000, 594000000,  71, 1, 2},      /* actual: 596.4 MHz */
6959         { 19200000, 594000000,  62, 1, 2},      /* actual: 595.2 MHz */
6960         { 26000000, 594000000,  91, 2, 2},      /* actual: 591.5 MHz */
6961         { 0, 0, 0, 0, 0, 0 },
6962 };
6963
6964 static struct clk tegra_pll_d2 = {
6965         .name      = "pll_d2",
6966         .flags     = PLL_ALT_MISC_REG,
6967         .ops       = &tegra_pllss_ops,
6968         .reg       = 0x4b8,
6969         .parent    = &tegra_pll_ref,    /* s/w policy, always tegra_pll_ref */
6970         .max_rate  = 600000000,
6971         .u.pll = {
6972                 .input_min = 12000000,
6973                 .input_max = 1000000000,
6974                 .cf_min    = 12000000,
6975                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
6976                 .vco_min   = 600000000,
6977                 .vco_max   = 1200000000,
6978                 .freq_table = tegra_plld2_freq_table,
6979                 .lock_delay = 300,
6980                 .misc1 = 0x570 - 0x4b8,
6981                 .round_p_to_pdiv = pllss_round_p_to_pdiv,
6982                 .cpcon_default = 0,
6983         },
6984 };
6985
6986 static struct clk tegra_pll_re_vco = {
6987         .name      = "pll_re_vco",
6988         .flags     = PLL_ALT_MISC_REG,
6989         .ops       = &tegra_pllre_ops,
6990         .reg       = 0x4c4,
6991         .parent    = &tegra_pll_ref,
6992         .max_rate  = 672000000,
6993         .u.pll = {
6994                 .input_min = 12000000,
6995                 .input_max = 1000000000,
6996                 .cf_min    = 12000000,
6997                 .cf_max    = 19200000,  /* s/w policy, h/w capability 38 MHz */
6998                 .vco_min   = 300000000,
6999                 .vco_max   = 672000000,
7000                 .lock_delay = 300,
7001                 .round_p_to_pdiv = pllre_round_p_to_pdiv,
7002         },
7003 };
7004
7005 static struct clk tegra_pll_re_out = {
7006         .name      = "pll_re_out",
7007         .ops       = &tegra_pllre_out_ops,
7008         .parent    = &tegra_pll_re_vco,
7009         .reg       = 0x4c4,
7010         .max_rate  = 672000000,
7011 };
7012
7013 static struct clk_pll_freq_table tegra_pll_e_freq_table[] = {
7014         /* PLLE special case: use cpcon field to store cml divider value */
7015         { 336000000, 100000000, 100, 21,  16, 11},
7016         { 312000000, 100000000, 200, 26,  24, 13},
7017         { 13000000,  100000000, 200, 1,  26, 13},
7018         { 12000000,  100000000, 200, 1,  24, 13},
7019         { 0, 0, 0, 0, 0, 0 },
7020 };
7021
7022 static struct clk tegra_pll_e = {
7023         .name      = "pll_e",
7024         .flags     = PLL_ALT_MISC_REG,
7025         .ops       = &tegra_plle_ops,
7026         .reg       = 0xe8,
7027         .max_rate  = 100000000,
7028         .u.pll = {
7029                 .input_min = 12000000,
7030                 .input_max = 1000000000,
7031                 .cf_min    = 12000000,
7032                 .cf_max    = 75000000,
7033                 .vco_min   = 1600000000,
7034                 .vco_max   = 2400000000U,
7035                 .freq_table = tegra_pll_e_freq_table,
7036                 .lock_delay = 300,
7037                 .fixed_rate = 100000000,
7038         },
7039 };
7040
7041
7042
7043 static struct clk tegra_cml0_clk = {
7044         .name      = "cml0",
7045         .parent    = &tegra_pll_e,
7046         .ops       = &tegra_cml_clk_ops,
7047         .reg       = PLLE_AUX,
7048         .max_rate  = 100000000,
7049         .u.periph  = {
7050                 .clk_num = 0,
7051         },
7052 };
7053
7054 static struct clk tegra_cml1_clk = {
7055         .name      = "cml1",
7056         .parent    = &tegra_pll_e,
7057         .ops       = &tegra_cml_clk_ops,
7058         .reg       = PLLE_AUX,
7059         .max_rate  = 100000000,
7060         .u.periph  = {
7061                 .clk_num   = 1,
7062         },
7063 };
7064
7065 static struct clk tegra_pciex_clk = {
7066         .name      = "pciex",
7067         .parent    = &tegra_pll_e,
7068         .ops       = &tegra_pciex_clk_ops,
7069         .max_rate  = 500000000,
7070         .u.periph  = {
7071                 .clk_num   = 74,
7072         },
7073 };
7074
7075 /* Audio sync clocks */
7076 #define SYNC_SOURCE(_id, _dev)                          \
7077         {                                               \
7078                 .name      = #_id "_sync",              \
7079                 .lookup    = {                          \
7080                         .dev_id    = #_dev ,            \
7081                         .con_id    = "ext_audio_sync",  \
7082                 },                                      \
7083                 .rate      = 24000000,                  \
7084                 .max_rate  = 24000000,                  \
7085                 .ops       = &tegra_sync_source_ops     \
7086         }
7087 static struct clk tegra_sync_source_list[] = {
7088         SYNC_SOURCE(spdif_in, tegra30-spdif),
7089         SYNC_SOURCE(i2s0, tegra30-i2s.0),
7090         SYNC_SOURCE(i2s1, tegra30-i2s.1),
7091         SYNC_SOURCE(i2s2, tegra30-i2s.2),
7092         SYNC_SOURCE(i2s3, tegra30-i2s.3),
7093         SYNC_SOURCE(i2s4, tegra30-i2s.4),
7094         SYNC_SOURCE(vimclk, vimclk),
7095 };
7096
7097 static struct clk_mux_sel mux_d_audio_clk[] = {
7098         { .input = &tegra_pll_a_out0,           .value = 0},
7099         { .input = &tegra_pll_p,                .value = 0x8000},
7100         { .input = &tegra_clk_m,                .value = 0xc000},
7101         { .input = &tegra_sync_source_list[0],  .value = 0xE000},
7102         { .input = &tegra_sync_source_list[1],  .value = 0xE001},
7103         { .input = &tegra_sync_source_list[2],  .value = 0xE002},
7104         { .input = &tegra_sync_source_list[3],  .value = 0xE003},
7105         { .input = &tegra_sync_source_list[4],  .value = 0xE004},
7106         { .input = &tegra_sync_source_list[5],  .value = 0xE005},
7107         { .input = &tegra_pll_a_out0,           .value = 0xE006},
7108         { .input = &tegra_sync_source_list[6],  .value = 0xE007},
7109         { 0, 0 }
7110 };
7111
7112 static struct clk_mux_sel mux_audio_sync_clk[] =
7113 {
7114         { .input = &tegra_sync_source_list[0],  .value = 0},
7115         { .input = &tegra_sync_source_list[1],  .value = 1},
7116         { .input = &tegra_sync_source_list[2],  .value = 2},
7117         { .input = &tegra_sync_source_list[3],  .value = 3},
7118         { .input = &tegra_sync_source_list[4],  .value = 4},
7119         { .input = &tegra_sync_source_list[5],  .value = 5},
7120         { .input = &tegra_pll_a_out0,           .value = 6},
7121         { .input = &tegra_sync_source_list[6],  .value = 7},
7122         { 0, 0 }
7123 };
7124
7125 #define AUDIO_SYNC_CLK(_id, _dev, _index)                       \
7126         {                                               \
7127                 .name      = #_id,                      \
7128                 .lookup    = {                          \
7129                         .dev_id    = #_dev,             \
7130                         .con_id    = "audio_sync",      \
7131                 },                                      \
7132                 .inputs    = mux_audio_sync_clk,        \
7133                 .reg       = 0x4A0 + (_index) * 4,      \
7134                 .max_rate  = 24000000,                  \
7135                 .ops       = &tegra_audio_sync_clk_ops  \
7136         }
7137 static struct clk tegra_clk_audio_list[] = {
7138         AUDIO_SYNC_CLK(audio0, tegra30-i2s.0, 0),
7139         AUDIO_SYNC_CLK(audio1, tegra30-i2s.1, 1),
7140         AUDIO_SYNC_CLK(audio2, tegra30-i2s.2, 2),
7141         AUDIO_SYNC_CLK(audio3, tegra30-i2s.3, 3),
7142         AUDIO_SYNC_CLK(audio4, tegra30-i2s.4, 4),
7143         AUDIO_SYNC_CLK(audio, tegra30-spdif, 5),        /* SPDIF */
7144 };
7145
7146 #define AUDIO_SYNC_2X_CLK(_id, _dev, _index)                            \
7147         {                                                       \
7148                 .name      = #_id "_2x",                        \
7149                 .lookup    = {                                  \
7150                         .dev_id    = #_dev,                     \
7151                         .con_id    = "audio_sync_2x"            \
7152                 },                                              \
7153                 .flags     = PERIPH_NO_RESET,                   \
7154                 .max_rate  = 48000000,                          \
7155                 .ops       = &tegra_clk_double_ops,             \
7156                 .reg       = 0x49C,                             \
7157                 .reg_shift = 24 + (_index),                     \
7158                 .parent    = &tegra_clk_audio_list[(_index)],   \
7159                 .u.periph = {                                   \
7160                         .clk_num = 113 + (_index),              \
7161                 },                                              \
7162         }
7163 static struct clk tegra_clk_audio_2x_list[] = {
7164         AUDIO_SYNC_2X_CLK(audio0, tegra30-i2s.0, 0),
7165         AUDIO_SYNC_2X_CLK(audio1, tegra30-i2s.1, 1),
7166         AUDIO_SYNC_2X_CLK(audio2, tegra30-i2s.2, 2),
7167         AUDIO_SYNC_2X_CLK(audio3, tegra30-i2s.3, 3),
7168         AUDIO_SYNC_2X_CLK(audio4, tegra30-i2s.4, 4),
7169         AUDIO_SYNC_2X_CLK(audio, tegra30-spdif, 5),     /* SPDIF */
7170 };
7171
7172 #define MUX_I2S_SPDIF(_id, _index)                                      \
7173 static struct clk_mux_sel mux_pllaout0_##_id##_2x_pllp_clkm[] = {       \
7174         {.input = &tegra_pll_a_out0, .value = 0},                       \
7175         {.input = &tegra_clk_audio_2x_list[(_index)], .value = 2},      \
7176         {.input = &tegra_pll_p, .value = 4},                            \
7177         {.input = &tegra_clk_m, .value = 6},                            \
7178         { 0, 0},                                                        \
7179 }
7180 MUX_I2S_SPDIF(audio0, 0);
7181 MUX_I2S_SPDIF(audio1, 1);
7182 MUX_I2S_SPDIF(audio2, 2);
7183 MUX_I2S_SPDIF(audio3, 3);
7184 MUX_I2S_SPDIF(audio4, 4);
7185 MUX_I2S_SPDIF(audio, 5);                /* SPDIF */
7186
7187 /* External clock outputs (through PMC) */
7188 #define MUX_EXTERN_OUT(_id)                                             \
7189 static struct clk_mux_sel mux_clkm_clkm2_clkm4_extern##_id[] = {        \
7190         {.input = &tegra_clk_m,         .value = 0},                    \
7191         {.input = &tegra_clk_m_div2,    .value = 1},                    \
7192         {.input = &tegra_clk_m_div4,    .value = 2},                    \
7193         {.input = NULL,                 .value = 3}, /* placeholder */  \
7194         { 0, 0},                                                        \
7195 }
7196 MUX_EXTERN_OUT(1);
7197 MUX_EXTERN_OUT(2);
7198 MUX_EXTERN_OUT(3);
7199
7200 static struct clk_mux_sel *mux_extern_out_list[] = {
7201         mux_clkm_clkm2_clkm4_extern1,
7202         mux_clkm_clkm2_clkm4_extern2,
7203         mux_clkm_clkm2_clkm4_extern3,
7204 };
7205
7206 #define CLK_OUT_CLK(_id, _max_rate)                                     \
7207         {                                                       \
7208                 .name      = "clk_out_" #_id,                   \
7209                 .lookup    = {                                  \
7210                         .dev_id    = "clk_out_" #_id,           \
7211                         .con_id    = "extern" #_id,             \
7212                 },                                              \
7213                 .ops       = &tegra_clk_out_ops,                \
7214                 .reg       = 0x1a8,                             \
7215                 .inputs    = mux_clkm_clkm2_clkm4_extern##_id,  \
7216                 .flags     = MUX_CLK_OUT,                       \
7217                 .max_rate  = _max_rate,                         \
7218                 .u.periph = {                                   \
7219                         .clk_num   = (_id - 1) * 8 + 2,         \
7220                 },                                              \
7221         }
7222 static struct clk tegra_clk_out_list[] = {
7223         CLK_OUT_CLK(1, 26000000),
7224         CLK_OUT_CLK(2, 40800000),
7225         CLK_OUT_CLK(3, 26000000),
7226 };
7227
7228 /* called after peripheral external clocks are initialized */
7229 static void init_clk_out_mux(void)
7230 {
7231         int i;
7232         struct clk *c;
7233
7234         /* output clock con_id is the name of peripheral
7235            external clock connected to input 3 of the output mux */
7236         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++) {
7237                 c = tegra_get_clock_by_name(
7238                         tegra_clk_out_list[i].lookup.con_id);
7239                 if (!c)
7240                         pr_err("%s: could not find clk %s\n", __func__,
7241                                tegra_clk_out_list[i].lookup.con_id);
7242                 mux_extern_out_list[i][3].input = c;
7243         }
7244 }
7245
7246 static struct clk_mux_sel mux_sclk[] = {
7247         { .input = &tegra_clk_m,        .value = 0},
7248         { .input = &tegra_pll_c_out1,   .value = 1},
7249         { .input = &tegra_pll_p_out4,   .value = 2},
7250         { .input = &tegra_pll_p,        .value = 3},
7251         { .input = &tegra_pll_p_out2,   .value = 4},
7252         { .input = &tegra_pll_c,        .value = 5},
7253         { .input = &tegra_clk_32k,      .value = 6},
7254         { .input = &tegra_pll_m_out1,   .value = 7},
7255         { 0, 0},
7256 };
7257
7258 static struct clk tegra_clk_sclk = {
7259         .name   = "sclk",
7260         .inputs = mux_sclk,
7261         .reg    = 0x28,
7262         .ops    = &tegra_super_ops,
7263         .max_rate = 420000000,
7264         .min_rate = 12000000,
7265 };
7266
7267 /* Peripheral muxes */
7268 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7269 static struct clk_mux_sel mux_cclk_g[] = {
7270         { .input = &tegra_clk_m,        .value = 0},
7271         { .input = &tegra_pll_c,        .value = 1},
7272         { .input = &tegra_clk_32k,      .value = 2},
7273         { .input = &tegra_pll_m,        .value = 3},
7274         { .input = &tegra_pll_p,        .value = 4},
7275         { .input = &tegra_pll_p_out4,   .value = 5},
7276         /* { .input = &tegra_pll_c2,    .value = 6}, - no use on tegra12x */
7277         /* { .input = &tegra_clk_c3,    .value = 7}, - no use on tegra12x */
7278         { .input = &tegra_pll_x,        .value = 8},
7279         { .input = &tegra_dfll_cpu,     .value = 15},
7280         { 0, 0},
7281 };
7282
7283 static struct clk_mux_sel mux_cclk_lp[] = {
7284         { .input = &tegra_clk_m,        .value = 0},
7285         { .input = &tegra_pll_c,        .value = 1},
7286         { .input = &tegra_clk_32k,      .value = 2},
7287         { .input = &tegra_pll_m,        .value = 3},
7288         { .input = &tegra_pll_p,        .value = 4},
7289         { .input = &tegra_pll_p_out4,   .value = 5},
7290         /* { .input = &tegra_pll_c2,    .value = 6}, - no use on tegra12x */
7291         /* { .input = &tegra_clk_c3,    .value = 7}, - no use on tegra12x */
7292         { .input = &tegra_pll_x_out0,   .value = 8},
7293         { .input = &tegra_pll_x,        .value = 8 | SUPER_LP_DIV2_BYPASS},
7294         { 0, 0},
7295 };
7296 #else
7297
7298 static struct clk_mux_sel mux_cclk_g[] = {
7299         { .input = &tegra_clk_m,        .value = 0},
7300         /* { .input = ,                 .value = 1}, - testclk */
7301         { .input = &tegra_clk_m,        .value = 2},
7302         { .input = &tegra_pll_ref,      .value = 3},
7303         { .input = &tegra_pll_m,        .value = 4},
7304         { .input = &tegra_pll_p,        .value = 5},
7305         { .input = &tegra_clk_sclk,     .value = 6},
7306         { .input = &tegra_clk_m,        .value = 7},
7307         { .input = &tegra_pll_x,        .value = 8},
7308         /* { .input = ,                 .value = 9},  - High jitter DFLL */
7309         /* { .input = ,                 .value = 14}, - High jitter PLLX */
7310         { .input = &tegra_dfll_cpu,     .value = 15},
7311         { 0, 0},
7312 };
7313
7314 #endif
7315
7316 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7317 static struct clk tegra_clk_cclk_g = {
7318         .name   = "cclk_g",
7319         .flags  = DIV_U71 | DIV_U71_INT | MUX,
7320         .inputs = mux_cclk_g,
7321         .reg    = 0x368,
7322         .ops    = &tegra_super_ops,
7323         .max_rate = 3000000000UL,
7324 };
7325
7326 static struct clk tegra_clk_cclk_lp = {
7327         .name   = "cclk_lp",
7328         .flags  = DIV_2 | DIV_U71 | DIV_U71_INT | MUX,
7329         .inputs = mux_cclk_lp,
7330         .reg    = 0x370,
7331         .ops    = &tegra_super_ops,
7332         .max_rate = 1350000000,
7333 };
7334 #else
7335
7336 static struct clk tegra_clk_cclk_g = {
7337         .name   = "cclk_g",
7338         .flags  = DIV_U71 | DIV_U71_INT | MUX,
7339         .inputs = mux_cclk_g,
7340         .reg    = 0x20,
7341         .ops    = &tegra13_super_cclk_ops,
7342         .max_rate = 3000000000UL,
7343 };
7344
7345 #endif
7346
7347 static struct raw_notifier_head cpu_g_rate_change_nh;
7348
7349 static struct clk tegra_clk_virtual_cpu_g = {
7350         .name      = "cpu_g",
7351         .parent    = &tegra_clk_cclk_g,
7352         .ops       = &tegra_cpu_ops,
7353         .max_rate  = 3000000000UL,
7354         .min_rate  = 3187500,
7355         .u.cpu = {
7356                 .main      = &tegra_pll_x,
7357 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7358                 .backup    = &tegra_pll_p_out4,
7359 #else
7360                 .backup    = &tegra_pll_p,
7361 #endif
7362                 .dynamic   = &tegra_dfll_cpu,
7363                 .mode      = MODE_G,
7364         },
7365         .rate_change_nh = &cpu_g_rate_change_nh,
7366 };
7367
7368 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7369 static struct clk tegra_clk_virtual_cpu_lp = {
7370         .name      = "cpu_lp",
7371         .parent    = &tegra_clk_cclk_lp,
7372         .ops       = &tegra_cpu_ops,
7373         .max_rate  = 1350000000,
7374         .min_rate  = 3187500,
7375         .u.cpu = {
7376                 .main      = &tegra_pll_x,
7377                 .backup    = &tegra_pll_p_out4,
7378                 .mode      = MODE_LP,
7379         },
7380 };
7381 #endif
7382
7383 static struct clk_mux_sel mux_cpu_cmplx[] = {
7384         { .input = &tegra_clk_virtual_cpu_g,    .value = 0},
7385 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
7386         { .input = &tegra_clk_virtual_cpu_lp,   .value = 1},
7387 #endif
7388         { 0, 0},
7389 };
7390
7391 static struct clk tegra_clk_cpu_cmplx = {
7392         .name      = "cpu",
7393         .inputs    = mux_cpu_cmplx,
7394         .ops       = &tegra_cpu_cmplx_ops,
7395         .max_rate  = 3000000000UL,
7396 };
7397
7398 static struct clk tegra_clk_cop = {
7399         .name      = "cop",
7400         .parent    = &tegra_clk_sclk,
7401         .ops       = &tegra_cop_ops,
7402         .max_rate  = 408000000,
7403 };
7404
7405 static struct clk tegra_clk_hclk = {
7406         .name           = "hclk",
7407         .flags          = DIV_BUS,
7408         .parent         = &tegra_clk_sclk,
7409         .reg            = 0x30,
7410         .reg_shift      = 4,
7411         .ops            = &tegra_bus_ops,
7412         .max_rate       = 408000000,
7413         .min_rate       = 12000000,
7414 };
7415
7416 static struct clk tegra_clk_pclk = {
7417         .name           = "pclk",
7418         .flags          = DIV_BUS,
7419         .parent         = &tegra_clk_hclk,
7420         .reg            = 0x30,
7421         .reg_shift      = 0,
7422         .ops            = &tegra_bus_ops,
7423         .max_rate       = 204000000,
7424         .min_rate       = 12000000,
7425 };
7426
7427 static struct raw_notifier_head sbus_rate_change_nh;
7428
7429 static struct clk tegra_clk_sbus_cmplx = {
7430         .name      = "sbus",
7431         .parent    = &tegra_clk_sclk,
7432         .ops       = &tegra_sbus_cmplx_ops,
7433         .u.system  = {
7434                 .pclk = &tegra_clk_pclk,
7435                 .hclk = &tegra_clk_hclk,
7436                 .sclk_low = &tegra_pll_p_out2,
7437 #ifdef CONFIG_TEGRA_PLLM_SCALED
7438                 .sclk_high = &tegra_pll_c_out1,
7439 #else
7440                 .sclk_high = &tegra_pll_m_out1,
7441 #endif
7442         },
7443         .rate_change_nh = &sbus_rate_change_nh,
7444 };
7445
7446 static struct clk tegra_clk_ahb = {
7447         .name      = "ahb.sclk",
7448         .flags     = DIV_BUS,
7449         .parent    = &tegra_clk_sbus_cmplx,
7450         .ops       = &tegra_clk_ahb_ops,
7451 };
7452
7453 static struct clk tegra_clk_apb = {
7454         .name      = "apb.sclk",
7455         .flags     = DIV_BUS,
7456         .parent    = &tegra_clk_ahb,
7457         .ops       = &tegra_clk_apb_ops,
7458 };
7459
7460 static struct clk tegra_clk_blink = {
7461         .name           = "blink",
7462         .parent         = &tegra_clk_32k,
7463         .reg            = 0x40,
7464         .ops            = &tegra_blink_clk_ops,
7465         .max_rate       = 32768,
7466 };
7467
7468
7469 /* Multimedia modules muxes */
7470 static struct clk_mux_sel mux_pllm_pllc2_c_c3_pllp_plla[] = {
7471         { .input = &tegra_pll_m,  .value = 0},
7472         { .input = &tegra_pll_c2, .value = 1},
7473         { .input = &tegra_pll_c,  .value = 2},
7474         { .input = &tegra_pll_c3, .value = 3},
7475         { .input = &tegra_pll_p,  .value = 4},
7476         { .input = &tegra_pll_a_out0, .value = 6},
7477         { 0, 0},
7478 };
7479
7480 static struct clk_mux_sel mux_pllm_pllc_pllp_plla[] = {
7481         { .input = &tegra_pll_m, .value = 0},
7482         { .input = &tegra_pll_c, .value = 2},
7483         { .input = &tegra_pll_p, .value = 4},
7484         { .input = &tegra_pll_a_out0, .value = 6},
7485         { 0, 0},
7486 };
7487
7488 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_v2[] = {
7489         { .input = &tegra_pll_m, .value = 0},
7490         { .input = &tegra_pll_c, .value = 1},
7491         { .input = &tegra_pll_p, .value = 2},
7492         { .input = &tegra_pll_a_out0, .value = 3},
7493         /* Skip C2(4) */
7494         /* Skip C2(5) */
7495         { 0, 0},
7496 };
7497
7498 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_pllc2_c3_clkm[] = {
7499         { .input = &tegra_pll_m, .value = 0},
7500         { .input = &tegra_pll_c, .value = 1},
7501         { .input = &tegra_pll_p, .value = 2},
7502         { .input = &tegra_pll_a_out0, .value = 3},
7503         { .input = &tegra_pll_c2, .value = 4},
7504         { .input = &tegra_pll_c3, .value = 5},
7505         { .input = &tegra_clk_m, .value = 6},
7506         { 0, 0},
7507 };
7508
7509 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_pllc4[] = {
7510         { .input = &tegra_pll_m, .value = 0},
7511         /* Skip C2(1) */
7512         { .input = &tegra_pll_c, .value = 2},
7513         /* Skip C2(3) */
7514         { .input = &tegra_pll_p, .value = 4},
7515         { .input = &tegra_pll_a_out0, .value = 6},
7516         { .input = &tegra_pll_c4, .value = 7},
7517         { 0, 0},
7518 };
7519
7520 static struct clk_mux_sel mux_pllm_pllc_pllp_plla_clkm_pllc4[] = {
7521         { .input = &tegra_pll_m, .value = 0},
7522         { .input = &tegra_pll_c, .value = 1},
7523         { .input = &tegra_pll_p, .value = 2},
7524         { .input = &tegra_pll_a_out0, .value = 3},
7525         /* Skip C2(4) & C3(5) */
7526         { .input = &tegra_clk_m, .value = 6},
7527         { .input = &tegra_pll_c4, .value = 7},
7528         { 0, 0},
7529 };
7530
7531 static struct clk_mux_sel mux_plla_pllc_pllp_clkm[] = {
7532         { .input = &tegra_pll_a_out0, .value = 0},
7533         { .input = &tegra_pll_c, .value = 2},
7534         { .input = &tegra_pll_p, .value = 4},
7535         { .input = &tegra_clk_m, .value = 6},
7536         { 0, 0},
7537 };
7538
7539 /* EMC muxes */
7540 /* FIXME: add EMC latency mux */
7541 static struct clk_mux_sel mux_pllm_pllc_pllp_clkm[] = {
7542         { .input = &tegra_pll_m, .value = 0},
7543         { .input = &tegra_pll_c, .value = 1},
7544         { .input = &tegra_pll_p, .value = 2},
7545         { .input = &tegra_clk_m, .value = 3},
7546         { .input = &tegra_pll_m, .value = 4}, /* low jitter PLLM output */
7547         /* { .input = &tegra_pll_c2, .value = 5}, - no use on tegra12x */
7548         /* { .input = &tegra_pll_c3, .value = 6}, - no use on tegra12x */
7549         { .input = &tegra_pll_c, .value = 7}, /* low jitter PLLC output */
7550         { 0, 0},
7551 };
7552
7553
7554 /* Display subsystem muxes */
7555 static struct clk_mux_sel mux_pllp_pllm_plld_plla_pllc_plld2_clkm[] = {
7556         {.input = &tegra_pll_p, .value = 0},
7557         {.input = &tegra_pll_m, .value = 1},
7558         {.input = &tegra_pll_d_out0, .value = 2},
7559         {.input = &tegra_pll_a_out0, .value = 3},
7560         {.input = &tegra_pll_c, .value = 4},
7561         {.input = &tegra_pll_d2, .value = 5},
7562         {.input = &tegra_clk_m, .value = 6},
7563         { 0, 0},
7564 };
7565
7566 static struct clk_mux_sel mux_plld_out0[] = {
7567         { .input = &tegra_pll_d_out0,  .value = 0},
7568         { 0, 0},
7569 };
7570
7571 /* Peripheral muxes */
7572 static struct clk_mux_sel mux_pllp_pllc_clkm[] = {
7573         {.input = &tegra_pll_p,     .value = 0},
7574         {.input = &tegra_pll_c,     .value = 2},
7575         {.input = &tegra_clk_m,     .value = 6},
7576         { 0, 0},
7577 };
7578
7579 static struct clk_mux_sel mux_pllp_pllc_clkm1[] = {
7580         {.input = &tegra_pll_p,     .value = 0},
7581         {.input = &tegra_pll_c,     .value = 1},
7582         {.input = &tegra_clk_m,     .value = 3},
7583         { 0, 0},
7584 };
7585
7586 static struct clk_mux_sel mux_pllp_pllc2_c_c3_pllm_clkm[] = {
7587         { .input = &tegra_pll_p,  .value = 0},
7588         { .input = &tegra_pll_c2, .value = 1},
7589         { .input = &tegra_pll_c,  .value = 2},
7590         { .input = &tegra_pll_c3, .value = 3},
7591         { .input = &tegra_pll_m,  .value = 4},
7592         { .input = &tegra_clk_m,  .value = 6},
7593         { 0, 0},
7594 };
7595
7596 static struct clk_mux_sel mux_pllp_pllc_pllm_clkm[] = {
7597         { .input = &tegra_pll_p, .value = 0},
7598         { .input = &tegra_pll_c, .value = 2},
7599         { .input = &tegra_pll_m, .value = 4},
7600         { .input = &tegra_clk_m, .value = 6},
7601         { 0, 0},
7602 };
7603
7604 static struct clk_mux_sel mux_pllp_pllc_pllm[] = {
7605         {.input = &tegra_pll_p,     .value = 0},
7606         {.input = &tegra_pll_c,     .value = 2},
7607         {.input = &tegra_pll_m,     .value = 4},
7608         { 0, 0},
7609 };
7610
7611 static struct clk_mux_sel mux_pllp_clkm_clk32_plle[] = {
7612         { .input = &tegra_pll_p,    .value = 0},
7613         { .input = &tegra_clk_m,    .value = 1},
7614         { .input = &tegra_clk_32k,  .value = 2},
7615         { .input = &tegra_pll_e,    .value = 3},
7616         { 0, 0},
7617 };
7618
7619 static struct clk_mux_sel mux_pllp_clkm[] = {
7620         { .input = &tegra_pll_p, .value = 0},
7621         { .input = &tegra_clk_m, .value = 6},
7622         { 0, 0},
7623 };
7624
7625 static struct clk_mux_sel mux_pllp_pllc_clk32_clkm[] = {
7626         {.input = &tegra_pll_p,     .value = 0},
7627         {.input = &tegra_pll_c,     .value = 2},
7628         {.input = &tegra_clk_32k,   .value = 4},
7629         {.input = &tegra_clk_m,     .value = 6},
7630         { 0, 0},
7631 };
7632
7633 static struct clk_mux_sel mux_pllp_pllc_clkm_clk32[] = {
7634         {.input = &tegra_pll_p,     .value = 0},
7635         {.input = &tegra_pll_c,     .value = 2},
7636         {.input = &tegra_clk_m,     .value = 4},
7637         {.input = &tegra_clk_32k,   .value = 6},
7638         { 0, 0},
7639 };
7640
7641 static struct clk_mux_sel mux_plla_clk32_pllp_clkm_plle[] = {
7642         { .input = &tegra_pll_a_out0, .value = 0},
7643         { .input = &tegra_clk_32k,    .value = 1},
7644         { .input = &tegra_pll_p,      .value = 2},
7645         { .input = &tegra_clk_m,      .value = 3},
7646         { .input = &tegra_pll_e,      .value = 4},
7647         { 0, 0},
7648 };
7649
7650 static struct clk_mux_sel mux_clkm_pllp_pllc_pllre[] = {
7651         { .input = &tegra_clk_m,  .value = 0},
7652         { .input = &tegra_pll_p,  .value = 1},
7653         { .input = &tegra_pll_c,  .value = 3},
7654         { .input = &tegra_pll_re_out,  .value = 5},
7655         { 0, 0},
7656 };
7657
7658 static struct clk_mux_sel mux_clkm_48M_pllp_480M[] = {
7659         { .input = &tegra_clk_m,      .value = 0},
7660         { .input = &tegra_pll_u_48M,  .value = 2},
7661         { .input = &tegra_pll_p,      .value = 4},
7662         { .input = &tegra_pll_u_480M, .value = 6},
7663         { 0, 0},
7664 };
7665
7666 static struct clk_mux_sel mux_clkm_pllre_clk32_480M_pllc_ref[] = {
7667         { .input = &tegra_clk_m,      .value = 0},
7668         { .input = &tegra_pll_re_out, .value = 1},
7669         { .input = &tegra_clk_32k,    .value = 2},
7670         { .input = &tegra_pll_u_480M, .value = 3},
7671         { .input = &tegra_pll_c,      .value = 4},
7672         { .input = &tegra_pll_ref,    .value = 7},
7673         { 0, 0},
7674 };
7675
7676 static struct clk_mux_sel mux_pllp3_pllc_clkm[] = {
7677         { .input = &tegra_pll_p_out3, .value = 0},
7678         { .input = &tegra_pll_c,  .value = 1},
7679         { .input = &tegra_clk_m,  .value = 3},
7680         { 0, 0},
7681 };
7682
7683 /* Single clock source ("fake") muxes */
7684 static struct clk_mux_sel mux_clk_m[] = {
7685         { .input = &tegra_clk_m, .value = 0},
7686         { 0, 0},
7687 };
7688
7689 static struct clk_mux_sel mux_pllp_out3[] = {
7690         { .input = &tegra_pll_p_out3, .value = 0},
7691         { 0, 0},
7692 };
7693
7694 static struct clk_mux_sel mux_clk_32k[] = {
7695         { .input = &tegra_clk_32k, .value = 0},
7696         { 0, 0},
7697 };
7698
7699 static struct clk_mux_sel mux_plld[] = {
7700         { .input = &tegra_pll_d_out0, .value = 1},
7701         { 0, 0},
7702 };
7703
7704
7705 static struct raw_notifier_head emc_rate_change_nh;
7706
7707 static struct clk tegra_clk_emc = {
7708         .name = "emc",
7709         .ops = &tegra_emc_clk_ops,
7710         .reg = 0x19c,
7711         .max_rate = 1200000000,
7712         .min_rate = 12750000,
7713         .inputs = mux_pllm_pllc_pllp_clkm,
7714         .flags = MUX | DIV_U71 | PERIPH_EMC_ENB,
7715         .u.periph = {
7716                 .clk_num = 57,
7717         },
7718         .rate_change_nh = &emc_rate_change_nh,
7719 };
7720
7721 static struct clk tegra_clk_mc = {
7722         .name = "mc",
7723         .ops = &tegra_mc_clk_ops,
7724         .max_rate =  533000000,
7725         .parent = &tegra_clk_emc,
7726         .flags = PERIPH_NO_RESET,
7727         .u.periph = {
7728                 .clk_num = 32,
7729         },
7730 };
7731
7732 static struct raw_notifier_head host1x_rate_change_nh;
7733
7734 static struct clk tegra_clk_host1x = {
7735         .name      = "host1x",
7736         .lookup    = {
7737                 .dev_id = "host1x",
7738         },
7739         .ops       = &tegra_1xbus_clk_ops,
7740         .reg       = 0x180,
7741         .inputs    = mux_pllm_pllc_pllp_plla,
7742         .flags     = MUX | DIV_U71 | DIV_U71_INT,
7743         .max_rate  = 500000000,
7744         .min_rate  = 12000000,
7745         .u.periph = {
7746                 .clk_num   = 28,
7747                 .pll_low = &tegra_pll_p,
7748 #ifdef CONFIG_TEGRA_PLLM_SCALED
7749                 .pll_high = &tegra_pll_c,
7750 #else
7751                 .pll_high = &tegra_pll_m,
7752 #endif
7753         },
7754         .rate_change_nh = &host1x_rate_change_nh,
7755 };
7756
7757 static struct raw_notifier_head mselect_rate_change_nh;
7758
7759 static struct clk tegra_clk_mselect = {
7760         .name      = "mselect",
7761         .lookup    = {
7762                 .dev_id = "mselect",
7763         },
7764         .ops       = &tegra_1xbus_clk_ops,
7765         .reg       = 0x3b4,
7766         .inputs    = mux_pllp_clkm,
7767         .flags     = MUX | DIV_U71 | DIV_U71_INT,
7768         .max_rate  = 408000000,
7769         .min_rate  = 12000000,
7770         .u.periph = {
7771                 .clk_num   = 99,
7772                 .pll_low = &tegra_pll_p,
7773                 .pll_high = &tegra_pll_p,
7774                 .threshold = 408000000,
7775         },
7776         .rate_change_nh = &mselect_rate_change_nh,
7777 };
7778
7779 #ifdef CONFIG_TEGRA_DUAL_CBUS
7780
7781 static struct raw_notifier_head c2bus_rate_change_nh;
7782 static struct raw_notifier_head c3bus_rate_change_nh;
7783
7784 static struct clk tegra_clk_c2bus = {
7785         .name      = "c2bus",
7786         .parent    = &tegra_pll_c2,
7787         .ops       = &tegra_clk_cbus_ops,
7788         .max_rate  = 700000000,
7789         .mul       = 1,
7790         .div       = 1,
7791         .flags     = PERIPH_ON_CBUS,
7792         .shared_bus_backup = {
7793                 .input = &tegra_pll_p,
7794         },
7795         .rate_change_nh = &c2bus_rate_change_nh,
7796 };
7797 static struct clk tegra_clk_c3bus = {
7798         .name      = "c3bus",
7799         .parent    = &tegra_pll_c3,
7800         .ops       = &tegra_clk_cbus_ops,
7801         .max_rate  = 900000000,
7802         .mul       = 1,
7803         .div       = 1,
7804         .flags     = PERIPH_ON_CBUS,
7805         .shared_bus_backup = {
7806                 .input = &tegra_pll_p,
7807         },
7808         .rate_change_nh = &c3bus_rate_change_nh,
7809 };
7810
7811 #ifdef CONFIG_TEGRA_MIGRATE_CBUS_USERS
7812 static DEFINE_MUTEX(cbus_mutex);
7813 #define CROSS_CBUS_MUTEX (&cbus_mutex)
7814 #else
7815 #define CROSS_CBUS_MUTEX NULL
7816 #endif
7817
7818
7819 static struct clk_mux_sel mux_clk_cbus[] = {
7820         { .input = &tegra_clk_c2bus, .value = 0},
7821         { .input = &tegra_clk_c3bus, .value = 1},
7822         { 0, 0},
7823 };
7824
7825 #define DUAL_CBUS_CLK(_name, _dev, _con, _parent, _id, _div, _mode)\
7826         {                                               \
7827                 .name      = _name,                     \
7828                 .lookup    = {                          \
7829                         .dev_id    = _dev,              \
7830                         .con_id    = _con,              \
7831                 },                                      \
7832                 .ops = &tegra_clk_shared_bus_user_ops,  \
7833                 .parent = _parent,                      \
7834                 .inputs = mux_clk_cbus,                 \
7835                 .flags = MUX,                           \
7836                 .u.shared_bus_user = {                  \
7837                         .client_id = _id,               \
7838                         .client_div = _div,             \
7839                         .mode = _mode,                  \
7840                 },                                      \
7841                 .cross_clk_mutex = CROSS_CBUS_MUTEX,    \
7842         }
7843
7844 #else
7845
7846 static struct raw_notifier_head cbus_rate_change_nh;
7847
7848 static struct clk tegra_clk_cbus = {
7849         .name      = "cbus",
7850         .parent    = &tegra_pll_c,
7851         .ops       = &tegra_clk_cbus_ops,
7852         .max_rate  = 700000000,
7853         .mul       = 1,
7854         .div       = 2,
7855         .flags     = PERIPH_ON_CBUS,
7856         .shared_bus_backup = {
7857                 .input = &tegra_pll_p,
7858         },
7859         .rate_change_nh = &cbus_rate_change_nh,
7860 };
7861 #endif
7862
7863 static struct clk_ops tegra_clk_gpu_ops = {
7864         .enable         = &tegra12_periph_clk_enable,
7865         .disable        = &tegra12_periph_clk_disable,
7866         .reset          = &tegra12_periph_clk_reset,
7867 };
7868
7869 /* This is a dummy clock for gpu. The enable/disable/reset routine controls
7870    input clock of the actual gpu clock. The input clock itself has a fixed
7871    frequency. The actual gpu clock's frequency is controlled by gpu driver,
7872    not here in clock framework. However, we assoicate this dummy clock with
7873    dvfs to control voltage of gpu rail along with frequency change of actual
7874    gpu clock. So frequency here and in dvfs are based on the acutal gpu clock. */
7875 static struct clk tegra_clk_gpu = {
7876         .name      = "gpu_ref",
7877         .ops       = &tegra_clk_gpu_ops,
7878         .parent    = &tegra_pll_ref,
7879         .u.periph  = {
7880                 .clk_num = 184,
7881         },
7882         .max_rate  = 48000000,
7883         .min_rate  = 12000000,
7884 };
7885
7886 #define RATE_GRANULARITY        100000 /* 0.1 MHz */
7887 #if defined(CONFIG_TEGRA_CLOCK_DEBUG_FUNC)
7888 static int gbus_round_pass_thru;
7889 void tegra_gbus_round_pass_thru_enable(bool enable)
7890 {
7891         if (enable)
7892                 gbus_round_pass_thru = 1;
7893         else
7894                 gbus_round_pass_thru = 0;
7895 }
7896 EXPORT_SYMBOL(tegra_gbus_round_pass_thru_enable);
7897 #else
7898 #define gbus_round_pass_thru    0
7899 #endif
7900
7901 static void tegra12_clk_gbus_init(struct clk *c)
7902 {
7903         unsigned long rate;
7904         bool enabled;
7905
7906         pr_debug("%s on clock %s (export ops %s)\n", __func__,
7907                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
7908
7909         if (!c->u.export_clk.ops || !c->u.export_clk.ops->init)
7910                 return;
7911
7912         c->u.export_clk.ops->init(c->u.export_clk.ops->data, &rate, &enabled);
7913         c->div = clk_get_rate(c->parent) / RATE_GRANULARITY;
7914         c->mul = rate / RATE_GRANULARITY;
7915         c->state = enabled ? ON : OFF;
7916 }
7917
7918 static int tegra12_clk_gbus_enable(struct clk *c)
7919 {
7920         pr_debug("%s on clock %s (export ops %s)\n", __func__,
7921                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
7922
7923         if (!c->u.export_clk.ops || !c->u.export_clk.ops->enable)
7924                 return -ENOENT;
7925
7926         return c->u.export_clk.ops->enable(c->u.export_clk.ops->data);
7927 }
7928
7929 static void tegra12_clk_gbus_disable(struct clk *c)
7930 {
7931         pr_debug("%s on clock %s (export ops %s)\n", __func__,
7932                  c->name, c->u.export_clk.ops ? "ready" : "not ready");
7933
7934         if (!c->u.export_clk.ops || !c->u.export_clk.ops->disable)
7935                 return;
7936
7937         c->u.export_clk.ops->disable(c->u.export_clk.ops->data);
7938 }
7939
7940 static int tegra12_clk_gbus_set_rate(struct clk *c, unsigned long rate)
7941 {
7942         int ret;
7943
7944         pr_debug("%s %lu on clock %s (export ops %s)\n", __func__,
7945                  rate, c->name, c->u.export_clk.ops ? "ready" : "not ready");
7946
7947         if (!c->u.export_clk.ops || !c->u.export_clk.ops->set_rate)
7948                 return -ENOENT;
7949
7950         ret = c->u.export_clk.ops->set_rate(c->u.export_clk.ops->data, &rate);
7951         if (!ret)
7952                 c->mul = rate / RATE_GRANULARITY;
7953         return ret;
7954 }
7955
7956 static long tegra12_clk_gbus_round_updown(struct clk *c, unsigned long rate,
7957                                           bool up)
7958 {
7959         return gbus_round_pass_thru ? rate :
7960                 tegra12_clk_cbus_round_updown(c, rate, up);
7961 }
7962
7963 static long tegra12_clk_gbus_round_rate(struct clk *c, unsigned long rate)
7964 {
7965         return tegra12_clk_gbus_round_updown(c, rate, true);
7966 }
7967
7968 static struct clk_ops tegra_clk_gbus_ops = {
7969         .init           = tegra12_clk_gbus_init,
7970         .enable         = tegra12_clk_gbus_enable,
7971         .disable        = tegra12_clk_gbus_disable,
7972         .set_rate       = tegra12_clk_gbus_set_rate,
7973         .round_rate     = tegra12_clk_gbus_round_rate,
7974         .round_rate_updown = tegra12_clk_gbus_round_updown,
7975         .shared_bus_update = tegra12_clk_shared_connector_update, /* re-use */
7976 };
7977
7978 static struct raw_notifier_head gbus_rate_change_nh;
7979
7980 static struct clk tegra_clk_gbus = {
7981         .name      = "gbus",
7982         .ops       = &tegra_clk_gbus_ops,
7983         .parent    = &tegra_clk_gpu,
7984         .max_rate  = 1032000000,
7985         .shared_bus_flags = SHARED_BUS_RETENTION,
7986         .rate_change_nh = &gbus_rate_change_nh,
7987 };
7988
7989 static void tegra12_camera_mclk_init(struct clk *c)
7990 {
7991         c->state = OFF;
7992         c->set = true;
7993
7994         if (!strcmp(c->name, "mclk")) {
7995                 c->parent = tegra_get_clock_by_name("vi_sensor");
7996                 c->max_rate = c->parent->max_rate;
7997         } else if (!strcmp(c->name, "mclk2")) {
7998                 c->parent = tegra_get_clock_by_name("vi_sensor2");
7999                 c->max_rate = c->parent->max_rate;
8000         }
8001 }
8002
8003 static int tegra12_camera_mclk_set_rate(struct clk *c, unsigned long rate)
8004 {
8005         return clk_set_rate(c->parent, rate);
8006 }
8007
8008 static struct clk_ops tegra_camera_mclk_ops = {
8009         .init     = tegra12_camera_mclk_init,
8010         .enable   = tegra12_periph_clk_enable,
8011         .disable  = tegra12_periph_clk_disable,
8012         .set_rate = tegra12_camera_mclk_set_rate,
8013 };
8014
8015 static struct clk tegra_camera_mclk = {
8016         .name = "mclk",
8017         .ops = &tegra_camera_mclk_ops,
8018         .u.periph = {
8019                 .clk_num = 92, /* csus */
8020         },
8021         .flags = PERIPH_NO_RESET,
8022 };
8023
8024 static struct clk tegra_camera_mclk2 = {
8025         .name = "mclk2",
8026         .ops = &tegra_camera_mclk_ops,
8027         .u.periph = {
8028                 .clk_num = 171, /* vim2_clk */
8029         },
8030         .flags = PERIPH_NO_RESET,
8031 };
8032
8033 static struct clk tegra_clk_isp = {
8034         .name = "isp",
8035         .ops = &tegra_periph_clk_ops,
8036         .reg = 0x144,
8037         .max_rate = 700000000,
8038         .inputs = mux_pllm_pllc_pllp_plla_clkm_pllc4,
8039         .flags = MUX | DIV_U71 | PERIPH_NO_ENB | PERIPH_NO_RESET,
8040 };
8041
8042 static struct clk_mux_sel mux_isp[] = {
8043         { .input = &tegra_clk_isp, .value = 0},
8044         { 0, 0},
8045 };
8046
8047 static struct raw_notifier_head c4bus_rate_change_nh;
8048
8049 static struct clk tegra_clk_c4bus = {
8050         .name      = "c4bus",
8051         .parent    = &tegra_pll_c4,
8052         .ops       = &tegra_clk_cbus_ops,
8053         .max_rate  = 700000000,
8054         .mul       = 1,
8055         .div       = 1,
8056         .shared_bus_backup = {
8057                 .input = &tegra_pll_p,
8058         },
8059         .rate_change_nh = &c4bus_rate_change_nh,
8060 };
8061
8062 #define PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
8063         {                                               \
8064                 .name      = _name,                     \
8065                 .lookup    = {                          \
8066                         .dev_id    = _dev,              \
8067                         .con_id    = _con,              \
8068                 },                                      \
8069                 .ops       = &tegra_periph_clk_ops,     \
8070                 .reg       = _reg,                      \
8071                 .inputs    = _inputs,                   \
8072                 .flags     = _flags,                    \
8073                 .max_rate  = _max,                      \
8074                 .u.periph = {                           \
8075                         .clk_num   = _clk_num,          \
8076                 },                                      \
8077         }
8078
8079 #define PERIPH_CLK_EX(_name, _dev, _con, _clk_num, _reg, _max, _inputs, \
8080                         _flags, _ops)                                   \
8081         {                                               \
8082                 .name      = _name,                     \
8083                 .lookup    = {                          \
8084                         .dev_id    = _dev,              \
8085                         .con_id    = _con,              \
8086                 },                                      \
8087                 .ops       = _ops,                      \
8088                 .reg       = _reg,                      \
8089                 .inputs    = _inputs,                   \
8090                 .flags     = _flags,                    \
8091                 .max_rate  = _max,                      \
8092                 .u.periph = {                           \
8093                         .clk_num   = _clk_num,          \
8094                 },                                      \
8095         }
8096
8097 #define D_AUDIO_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
8098         {                                               \
8099                 .name      = _name,                     \
8100                 .lookup    = {                          \
8101                         .dev_id    = _dev,              \
8102                         .con_id    = _con,              \
8103                 },                                      \
8104                 .ops       = &tegra_periph_clk_ops,     \
8105                 .reg       = _reg,                      \
8106                 .inputs    = _inputs,                   \
8107                 .flags     = _flags,                    \
8108                 .max_rate  = _max,                      \
8109                 .u.periph = {                           \
8110                         .clk_num   = _clk_num,          \
8111                         .src_mask  = 0xE01F << 16,      \
8112                         .src_shift = 16,                \
8113                 },                                      \
8114         }
8115
8116 #define SHARED_CLK(_name, _dev, _con, _parent, _id, _div, _mode)\
8117         {                                               \
8118                 .name      = _name,                     \
8119                 .lookup    = {                          \
8120                         .dev_id    = _dev,              \
8121                         .con_id    = _con,              \
8122                 },                                      \
8123                 .ops = &tegra_clk_shared_bus_user_ops,  \
8124                 .parent = _parent,                      \
8125                 .u.shared_bus_user = {                  \
8126                         .client_id = _id,               \
8127                         .client_div = _div,             \
8128                         .mode = _mode,                  \
8129                 },                                      \
8130         }
8131 #define SHARED_LIMIT(_name, _dev, _con, _parent, _id, _div, _mode)\
8132         {                                               \
8133                 .name      = _name,                     \
8134                 .lookup    = {                          \
8135                         .dev_id    = _dev,              \
8136                         .con_id    = _con,              \
8137                 },                                      \
8138                 .ops = &tegra_clk_shared_bus_user_ops,  \
8139                 .parent = _parent,                      \
8140                 .flags     = BUS_RATE_LIMIT,            \
8141                 .u.shared_bus_user = {                  \
8142                         .client_id = _id,               \
8143                         .client_div = _div,             \
8144                         .mode = _mode,                  \
8145                 },                                      \
8146         }
8147 #define SHARED_CONNECT(_name, _dev, _con, _parent, _id, _div, _mode)\
8148         {                                               \
8149                 .name      = _name,                     \
8150                 .lookup    = {                          \
8151                         .dev_id    = _dev,              \
8152                         .con_id    = _con,              \
8153                 },                                      \
8154                 .ops = &tegra_clk_shared_connector_ops, \
8155                 .parent = _parent,                      \
8156                 .u.shared_bus_user = {                  \
8157                         .client_id = _id,               \
8158                         .client_div = _div,             \
8159                         .mode = _mode,                  \
8160                 },                                      \
8161         }
8162
8163 #define SHARED_EMC_CLK(_name, _dev, _con, _parent, _id, _div, _mode, _flag)\
8164         {                                               \
8165                 .name      = _name,                     \
8166                 .lookup    = {                          \
8167                         .dev_id    = _dev,              \
8168                         .con_id    = _con,              \
8169                 },                                      \
8170                 .ops = &tegra_clk_shared_bus_user_ops,  \
8171                 .parent = _parent,                      \
8172                 .u.shared_bus_user = {                  \
8173                         .client_id = _id,               \
8174                         .client_div = _div,             \
8175                         .mode = _mode,                  \
8176                         .usage_flag = _flag,            \
8177                 },                                      \
8178         }
8179
8180 static DEFINE_MUTEX(sbus_cross_mutex);
8181 #define SHARED_SCLK(_name, _dev, _con, _parent, _id, _div, _mode)\
8182         {                                               \
8183                 .name = _name,                          \
8184                 .lookup = {                             \
8185                         .dev_id = _dev,                 \
8186                         .con_id = _con,                 \
8187                 },                                      \
8188                 .ops = &tegra_clk_shared_bus_user_ops,  \
8189                 .parent = _parent,                      \
8190                 .u.shared_bus_user = {                  \
8191                         .client_id = _id,               \
8192                         .client_div = _div,             \
8193                         .mode = _mode,                  \
8194                 },                                      \
8195                 .cross_clk_mutex = &sbus_cross_mutex,   \
8196 }
8197
8198 struct clk tegra_list_clks[] = {
8199         PERIPH_CLK("apbdma",    "tegra-apbdma",         NULL,   34,     0,      26000000,  mux_clk_m,                   0),
8200         PERIPH_CLK("rtc",       "rtc-tegra",            NULL,   4,      0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB),
8201         PERIPH_CLK("kbc",       "tegra-kbc",            NULL,   36,     0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET | PERIPH_ON_APB),
8202         PERIPH_CLK("timer",     "timer",                NULL,   5,      0,      26000000,  mux_clk_m,                   0),
8203         PERIPH_CLK("kfuse",     "kfuse-tegra",          NULL,   40,     0,      26000000,  mux_clk_m,                   PERIPH_ON_APB),
8204         PERIPH_CLK("fuse",      "fuse-tegra",           "fuse", 39,     0,      26000000,  mux_clk_m,                   PERIPH_ON_APB),
8205         PERIPH_CLK("fuse_burn", "fuse-tegra",           "fuse_burn",    39,     0,      26000000,  mux_clk_m,           PERIPH_ON_APB),
8206         PERIPH_CLK("apbif",     "tegra30-ahub",         "apbif", 107,   0,      26000000,  mux_clk_m,                   PERIPH_ON_APB),
8207         PERIPH_CLK("i2s0",      "tegra30-i2s.0",        NULL,   30,     0x1d8,  24576000,  mux_pllaout0_audio0_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8208         PERIPH_CLK("i2s1",      "tegra30-i2s.1",        NULL,   11,     0x100,  24576000,  mux_pllaout0_audio1_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8209         PERIPH_CLK("i2s2",      "tegra30-i2s.2",        NULL,   18,     0x104,  24576000,  mux_pllaout0_audio2_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8210         PERIPH_CLK("i2s3",      "tegra30-i2s.3",        NULL,   101,    0x3bc,  24576000,  mux_pllaout0_audio3_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8211         PERIPH_CLK("i2s4",      "tegra30-i2s.4",        NULL,   102,    0x3c0,  24576000,  mux_pllaout0_audio4_2x_pllp_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8212         PERIPH_CLK("spdif_out", "tegra30-spdif",        "spdif_out",    10,     0x108,   24576000, mux_pllaout0_audio_2x_pllp_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8213         PERIPH_CLK("spdif_in",  "tegra30-spdif",        "spdif_in",     10,     0x10c,  100000000, mux_pllp_pllc_pllm,          MUX | DIV_U71 | PERIPH_ON_APB),
8214         PERIPH_CLK("pwm",       "tegra-pwm",            NULL,   17,     0x110,  48000000, mux_pllp_pllc_clk32_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8215         D_AUDIO_CLK("d_audio",  "tegra30-ahub",         "d_audio",      106,    0x3d0,  48000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8216         D_AUDIO_CLK("dam0",     "tegra30-dam.0",        NULL,   108,    0x3d8,  40000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8217         D_AUDIO_CLK("dam1",     "tegra30-dam.1",        NULL,   109,    0x3dc,  40000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8218         D_AUDIO_CLK("dam2",     "tegra30-dam.2",        NULL,   110,    0x3e0,  40000000,  mux_d_audio_clk,     MUX | DIV_U71 | PERIPH_ON_APB),
8219         PERIPH_CLK("adx",       "adx",                  NULL,   154,    0x638,  24580000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8220         PERIPH_CLK("adx1",      "adx1",                 NULL,   180,    0x670,  24580000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8221         PERIPH_CLK("amx",       "amx",                  NULL,   153,    0x63c,  24600000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8222         PERIPH_CLK("amx1",      "amx1",                 NULL,   185,    0x674,  24600000, mux_plla_pllc_pllp_clkm,      MUX | DIV_U71 | PERIPH_ON_APB),
8223         PERIPH_CLK("afc0",      "tegra124-afc.0",       NULL,   186,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8224         PERIPH_CLK("afc1",      "tegra124-afc.1",       NULL,   187,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8225         PERIPH_CLK("afc2",      "tegra124-afc.2",       NULL,   188,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8226         PERIPH_CLK("afc3",      "tegra124-afc.3",       NULL,   189,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8227         PERIPH_CLK("afc4",      "tegra124-afc.4",       NULL,   190,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8228         PERIPH_CLK("afc5",      "tegra124-afc.5",       NULL,   191,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8229         PERIPH_CLK("hda",       "tegra30-hda",          "hda",   125,   0x428,  108000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8230         PERIPH_CLK("hda2codec_2x",      "tegra30-hda",  "hda2codec",   111,     0x3e4,  48000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8231         PERIPH_CLK("hda2hdmi",  "tegra30-hda",          "hda2hdmi",     128,    0,      48000000,  mux_clk_m,                   PERIPH_ON_APB),
8232         PERIPH_CLK("sbc1",      "spi-tegra114.0",       NULL,   41,     0x134, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8233         PERIPH_CLK("sbc2",      "spi-tegra114.1",       NULL,   44,     0x118, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8234         PERIPH_CLK("sbc3",      "spi-tegra114.2",       NULL,   46,     0x11c, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8235         PERIPH_CLK("sbc4",      "spi-tegra114.3",       NULL,   68,     0x1b4, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8236         PERIPH_CLK("sbc5",      "spi-tegra114.4",       NULL,   104,    0x3c8, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8237         PERIPH_CLK("sbc6",      "spi-tegra114.5",       NULL,   105,    0x3cc, 33000000, mux_pllp_pllc_pllm_clkm,       MUX | DIV_U71 | PERIPH_ON_APB),
8238         PERIPH_CLK("sata_oob",  "tegra_sata_oob",       NULL,   123,    0x420,  216000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8239         PERIPH_CLK("sata",      "tegra_sata",           NULL,   124,    0x424,  216000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8240         PERIPH_CLK("sata_cold", "tegra_sata_cold",      NULL,   129,    0,      48000000,  mux_clk_m,                   PERIPH_ON_APB),
8241         PERIPH_CLK("vfir",      "vfir",                 NULL,   7,      0x168,  72000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8242         PERIPH_CLK("sdmmc1",    "sdhci-tegra.0",        NULL,   14,     0x150,  208000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8243         PERIPH_CLK("sdmmc2",    "sdhci-tegra.1",        NULL,   9,      0x154,  200000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8244         PERIPH_CLK("sdmmc3",    "sdhci-tegra.2",        NULL,   69,     0x1bc,  208000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8245         PERIPH_CLK("sdmmc4",    "sdhci-tegra.3",        NULL,   15,     0x164,  200000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8246         PERIPH_CLK("sdmmc1_ddr", "sdhci-tegra.0",       "ddr",  14,     0x150,  100000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8247         PERIPH_CLK("sdmmc3_ddr", "sdhci-tegra.2",       "ddr",  69,     0x1bc,  100000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8248         PERIPH_CLK("sdmmc4_ddr", "sdhci-tegra.3",       "ddr",  15,     0x164,  102000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8249         PERIPH_CLK("vcp",       "tegra-avp",            "vcp",  29,     0,      250000000, mux_clk_m,                   0),
8250         PERIPH_CLK("bsea",      "tegra-avp",            "bsea", 62,     0,      250000000, mux_clk_m,                   0),
8251         PERIPH_CLK("bsev",      "tegra-aes",            "bsev", 63,     0,      250000000, mux_clk_m,                   0),
8252         PERIPH_CLK("cec",       "tegra_cec",            NULL,   136,    0,      250000000, mux_clk_m,                   PERIPH_ON_APB),
8253         PERIPH_CLK("vde",       "vde",                  NULL,   61,     0x1c8,  600000000, mux_pllp_pllc2_c_c3_pllm_clkm,       MUX | DIV_U71 | DIV_U71_INT),
8254         PERIPH_CLK("csite",     "csite",                NULL,   73,     0x1d4,  144000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8255         PERIPH_CLK("la",        "la",                   NULL,   76,     0x1f8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8256         PERIPH_CLK("trace",     "trace",                NULL,   77,     0x634,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8257         PERIPH_CLK("owr",       "tegra_w1",             NULL,   71,     0x1cc,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8258         PERIPH_CLK("nor",       "tegra-nor",            NULL,   42,     0x1d0,  127000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
8259         PERIPH_CLK("mipi",      "mipi",                 NULL,   50,     0x174,  60000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71 | PERIPH_ON_APB),
8260         PERIPH_CLK("i2c1",      "tegra12-i2c.0",        "div-clk",      12,     0x124,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8261         PERIPH_CLK("i2c2",      "tegra12-i2c.1",        "div-clk",      54,     0x198,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8262         PERIPH_CLK("i2c3",      "tegra12-i2c.2",        "div-clk",      67,     0x1b8,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8263         PERIPH_CLK("i2c4",      "tegra12-i2c.3",        "div-clk",      103,    0x3c4,  136000000, mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8264         PERIPH_CLK("i2c5",      "tegra12-i2c.4",        "div-clk",      47,     0x128,  136000000,  mux_pllp_clkm,      MUX | DIV_U16 | PERIPH_ON_APB),
8265         PERIPH_CLK("i2c6",      "tegra12-i2c.5",        "div-clk",      166,    0x65c,  58300000,  mux_pllp_clkm,       MUX | DIV_U16 | PERIPH_ON_APB),
8266         PERIPH_CLK("mipi-cal",  "mipi-cal",             NULL,   56,     0,      60000000,  mux_clk_m,                   PERIPH_ON_APB),
8267         PERIPH_CLK("mipi-cal-fixed", "mipi-cal-fixed",  NULL,   0,      0,      108000000, mux_pllp_out3,       PERIPH_NO_ENB),
8268         PERIPH_CLK("uarta",     "serial-tegra.0",               NULL,   6,      0x178,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8269         PERIPH_CLK("uartb",     "serial-tegra.1",               NULL,   7,      0x17c,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8270         PERIPH_CLK("uartc",     "serial-tegra.2",               NULL,   55,     0x1a0,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8271         PERIPH_CLK("uartd",     "serial-tegra.3",               NULL,   65,     0x1c0,  800000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U151 | DIV_U151_UART | PERIPH_ON_APB),
8272         PERIPH_CLK("vic03",     "vic03",                NULL,   178,    0x678,  900000000, mux_pllm_pllc_pllp_plla_pllc2_c3_clkm,       MUX | DIV_U71),
8273         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),
8274         PERIPH_CLK("vi_sensor",  NULL,                  "vi_sensor",    164,    0x1a8,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET),
8275         PERIPH_CLK("vi_sensor2", NULL,                  "vi_sensor2",   165,    0x658,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET),
8276         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),
8277         PERIPH_CLK("tsec",      "tsec",                 NULL,   83,     0x1f4,  900000000, mux_pllp_pllc2_c_c3_pllm_clkm,       MUX | DIV_U71 | DIV_U71_INT),
8278         PERIPH_CLK_EX("dtv",    "dtv",                  NULL,   79,     0x1dc,  250000000, mux_clk_m,                   PERIPH_ON_APB,  &tegra_dtv_clk_ops),
8279         PERIPH_CLK("hdmi",      "hdmi",                 NULL,   51,     0x18c,  594000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | DIV_U71),
8280         PERIPH_CLK("disp1",     "tegradc.0",            NULL,   27,     0x138,  600000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX),
8281         PERIPH_CLK("disp2",     "tegradc.1",            NULL,   26,     0x13c,  600000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX),
8282         PERIPH_CLK_EX("sor0",   "sor0",                 NULL,   182,    0x414,  540000000, mux_pllp_pllm_plld_plla_pllc_plld2_clkm,     MUX | DIV_U71, &tegra_sor_clk_ops),
8283         PERIPH_CLK("dpaux",     "dpaux",                NULL,   181,    0,      24000000, mux_clk_m,                    0),
8284         PERIPH_CLK("usbd",      "tegra-udc.0",          NULL,   22,     0,      480000000, mux_clk_m,                   0),
8285         PERIPH_CLK("usb2",      "tegra-ehci.1",         NULL,   58,     0,      480000000, mux_clk_m,                   0),
8286         PERIPH_CLK("usb3",      "tegra-ehci.2",         NULL,   59,     0,      480000000, mux_clk_m,                   0),
8287         PERIPH_CLK_EX("dsia",   "tegradc.0",            "dsia", 48,     0xd0,   750000000, mux_plld_out0,               PLLD,   &tegra_dsi_clk_ops),
8288         PERIPH_CLK_EX("dsib",   "tegradc.1",            "dsib", 82,     0x4b8,  750000000, mux_plld_out0,               PLLD,   &tegra_dsi_clk_ops),
8289         PERIPH_CLK("dsi1-fixed", "tegradc.0",           "dsi-fixed",    0,      0,      108000000, mux_pllp_out3,       PERIPH_NO_ENB),
8290         PERIPH_CLK("dsi2-fixed", "tegradc.1",           "dsi-fixed",    0,      0,      108000000, mux_pllp_out3,       PERIPH_NO_ENB),
8291         PERIPH_CLK("csi",       "vi",                   "csi",  52,     0,      750000000, mux_plld,                    PLLD),
8292         PERIPH_CLK("ispa",      "isp",                  "ispa", 23,     0,      700000000, mux_isp,                     PERIPH_ON_APB),
8293         PERIPH_CLK("ispb",      "isp",                  "ispb", 3,      0,      700000000, mux_isp,                     PERIPH_ON_APB),
8294         PERIPH_CLK("csus",      "vi",                   "csus", 92,     0,      150000000, mux_clk_m,                   PERIPH_NO_RESET),
8295         PERIPH_CLK("vim2_clk",  "vi",                   "vim2_clk",     171,    0,      150000000, mux_clk_m,           PERIPH_NO_RESET),
8296         PERIPH_CLK("cilab",     "vi",                   "cilab", 144,   0x614,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8297         PERIPH_CLK("cilcd",     "vi",                   "cilcd", 145,   0x618,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8298         PERIPH_CLK("cile",      "vi",                   "cile",  146,   0x61c,  102000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8299         PERIPH_CLK("dsialp",    "tegradc.0",            "dsialp", 147,  0x620,  156000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8300         PERIPH_CLK("dsiblp",    "tegradc.1",            "dsiblp", 148,  0x624,  156000000, mux_pllp_pllc_clkm,          MUX | DIV_U71),
8301         PERIPH_CLK("entropy",   "entropy",              NULL, 149,      0x628,  102000000, mux_pllp_clkm_clk32_plle,    MUX | DIV_U71),
8302         PERIPH_CLK("hdmi_audio", "hdmi_audio",          NULL, 176,      0x668,  48000000,  mux_pllp_pllc_clkm1,         MUX | DIV_U71 | PERIPH_NO_RESET),
8303         PERIPH_CLK("clk72mhz",  "clk72mhz",             NULL, 177,      0x66c,  102000000, mux_pllp3_pllc_clkm,         MUX | DIV_U71 | PERIPH_NO_RESET),
8304
8305         PERIPH_CLK("tsensor",   "tegra-tsensor",        NULL,   100,    0x3b8,   12000000, mux_pllp_pllc_clkm_clk32,    MUX | DIV_U71 | PERIPH_ON_APB),
8306         PERIPH_CLK("actmon",    "actmon",               NULL,   119,    0x3e8,  216000000, mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71),
8307         PERIPH_CLK("extern1",   "extern1",              NULL,   120,    0x3ec,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71),
8308         PERIPH_CLK("extern2",   "extern2",              NULL,   121,    0x3f0,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71),
8309         PERIPH_CLK("extern3",   "extern3",              NULL,   122,    0x3f4,  216000000, mux_plla_clk32_pllp_clkm_plle,       MUX | DIV_U71),
8310         PERIPH_CLK("i2cslow",   "i2cslow",              NULL,   81,     0x3fc,  26000000,  mux_pllp_pllc_clk32_clkm,    MUX | DIV_U71 | PERIPH_ON_APB),
8311         PERIPH_CLK("pcie",      "tegra-pcie",           "pcie", 70,     0,      250000000, mux_clk_m,                   0),
8312         PERIPH_CLK("afi",       "tegra-pcie",           "afi",  72,     0,      250000000, mux_clk_m,                   0),
8313         PERIPH_CLK("se",        "se",                   NULL,   127,    0x42c,  600000000, mux_pllp_pllc2_c_c3_pllm_clkm,       MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB),
8314         PERIPH_CLK("cl_dvfs_ref", "tegra_cl_dvfs",      "ref",  155,    0x62c,  54000000,  mux_pllp_clkm,               MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB),
8315         PERIPH_CLK("cl_dvfs_soc", "tegra_cl_dvfs",      "soc",  155,    0x630,  54000000,  mux_pllp_clkm,               MUX | DIV_U71 | DIV_U71_INT | PERIPH_ON_APB),
8316         PERIPH_CLK("soc_therm", "soc_therm",            NULL,   78,     0x644,  136000000, mux_pllm_pllc_pllp_plla_v2,  MUX | DIV_U71 | PERIPH_ON_APB),
8317
8318         PERIPH_CLK("dds",       "dds",                  NULL,   150,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8319         PERIPH_CLK("dp2",       "dp2",                  NULL,   152,    0,      26000000, mux_clk_m,                    PERIPH_ON_APB),
8320
8321         SHARED_SCLK("automotive.hclk", "automotive",    "hclk", &tegra_clk_ahb,        NULL, 0, 0),
8322         SHARED_SCLK("automotive.pclk", "automotive",    "pclk", &tegra_clk_apb,        NULL, 0, 0),
8323
8324         SHARED_SCLK("avp.sclk",  "tegra-avp",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8325         SHARED_SCLK("bsea.sclk", "tegra-aes",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8326         SHARED_SCLK("usbd.sclk", "tegra-udc.0",         "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8327         SHARED_SCLK("usb1.sclk", "tegra-ehci.0",        "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8328         SHARED_SCLK("usb2.sclk", "tegra-ehci.1",        "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8329         SHARED_SCLK("usb3.sclk", "tegra-ehci.2",        "sclk", &tegra_clk_ahb,        NULL, 0, 0),
8330         SHARED_SCLK("wake.sclk", "wake_sclk",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8331         SHARED_SCLK("automotive.sclk", "automotive",    "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8332         SHARED_SCLK("via.sclk", "tegra_vi.0",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8333         SHARED_SCLK("vib.sclk", "tegra_vi.1",           "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8334         SHARED_SCLK("ispa.sclk",        "tegra_isp.0",          "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8335         SHARED_SCLK("ispb.sclk",        "tegra_isp.1",          "sclk", &tegra_clk_sbus_cmplx, NULL, 0, 0),
8336         SHARED_SCLK("mon.avp",  "tegra_actmon",         "avp",  &tegra_clk_sbus_cmplx, NULL, 0, 0),
8337         SHARED_SCLK("cap.sclk", "cap_sclk",             NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING),
8338         SHARED_SCLK("cap.vcore.sclk",   "cap.vcore.sclk", NULL, &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING),
8339         SHARED_SCLK("cap.throttle.sclk", "cap_throttle", NULL,  &tegra_clk_sbus_cmplx, NULL, 0, SHARED_CEILING),
8340         SHARED_SCLK("floor.sclk", "floor_sclk",         NULL,   &tegra_clk_sbus_cmplx, NULL, 0, 0),
8341         SHARED_SCLK("override.sclk", "override_sclk",   NULL,   &tegra_clk_sbus_cmplx, NULL, 0, SHARED_OVERRIDE),
8342         SHARED_SCLK("sbc1.sclk", "tegra12-spi.0",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8343         SHARED_SCLK("sbc2.sclk", "tegra12-spi.1",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8344         SHARED_SCLK("sbc3.sclk", "tegra12-spi.2",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8345         SHARED_SCLK("sbc4.sclk", "tegra12-spi.3",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8346         SHARED_SCLK("sbc5.sclk", "tegra12-spi.4",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8347         SHARED_SCLK("sbc6.sclk", "tegra12-spi.5",       "sclk", &tegra_clk_apb,        NULL, 0, 0),
8348
8349         SHARED_EMC_CLK("avp.emc",       "tegra-avp",    "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8350         SHARED_EMC_CLK("mon_cpu.emc",   "tegra_mon", "cpu_emc", &tegra_clk_emc, NULL, 0, 0, 0),
8351 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
8352         SHARED_EMC_CLK("cpu.emc",       "tegra-cpu", "cpu_emc", &tegra_clk_emc, NULL, 0, 0, 0),
8353 #endif
8354         SHARED_EMC_CLK("disp1.emc",     "tegradc.0",    "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_DC1)),
8355         SHARED_EMC_CLK("disp2.emc",     "tegradc.1",    "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_DC2)),
8356         SHARED_EMC_CLK("disp1.la.emc",  "tegradc.0",    "emc.la",       &tegra_clk_emc, NULL, 0, 0, 0),
8357         SHARED_EMC_CLK("disp2.la.emc",  "tegradc.1",    "emc.la",       &tegra_clk_emc, NULL, 0, 0, 0),
8358         SHARED_EMC_CLK("hdmi.emc",      "hdmi",         "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8359         SHARED_EMC_CLK("usbd.emc",      "tegra-udc.0",  "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8360         SHARED_EMC_CLK("usb1.emc",      "tegra-ehci.0", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8361         SHARED_EMC_CLK("usb2.emc",      "tegra-ehci.1", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8362         SHARED_EMC_CLK("usb3.emc",      "tegra-ehci.2", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8363         SHARED_EMC_CLK("sdmmc3.emc",    "sdhci-tegra.2","emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8364         SHARED_EMC_CLK("sdmmc4.emc",    "sdhci-tegra.3","emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8365         SHARED_EMC_CLK("mon.emc",       "tegra_actmon", "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8366         SHARED_EMC_CLK("cap.emc",       "cap.emc",      NULL,   &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0),
8367         SHARED_EMC_CLK("cap.vcore.emc", "cap.vcore.emc", NULL,  &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0),
8368         SHARED_EMC_CLK("cap.throttle.emc", "cap_throttle", NULL, &tegra_clk_emc, NULL, 0, SHARED_CEILING_BUT_ISO, 0),
8369         SHARED_EMC_CLK("3d.emc",        "tegra_gk20a.0", "emc", &tegra_clk_emc, NULL, 0, 0, BIT(EMC_USER_3D)),
8370         SHARED_EMC_CLK("msenc.emc",     "tegra_msenc",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_BW,     BIT(EMC_USER_MSENC)),
8371         SHARED_EMC_CLK("tsec.emc",      "tegra_tsec",   "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8372         SHARED_EMC_CLK("via.emc",       "tegra_vi.0",   "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI)),
8373         SHARED_EMC_CLK("vib.emc",       "tegra_vi.1",   "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_VI2)),
8374         SHARED_EMC_CLK("ispa.emc",      "tegra_isp.0",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_ISP1)),
8375         SHARED_EMC_CLK("ispb.emc",      "tegra_isp.1",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_ISO_BW, BIT(EMC_USER_ISP2)),
8376         SHARED_EMC_CLK("iso.emc",       "iso",          "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8377         SHARED_EMC_CLK("override.emc", "override.emc",  NULL,   &tegra_clk_emc, NULL, 0, SHARED_OVERRIDE, 0),
8378         SHARED_EMC_CLK("vic.emc",       "tegra_vic03.0",        "emc",  &tegra_clk_emc, NULL, 0, 0, 0),
8379         SHARED_EMC_CLK("battery.emc",   "battery_edp",  "emc",  &tegra_clk_emc, NULL, 0, SHARED_CEILING, 0),
8380         SHARED_LIMIT("floor.emc",       "floor.emc",    NULL,   &tegra_clk_emc, NULL,  0, 0),
8381         SHARED_LIMIT("floor.profile.emc", "profile.emc", "floor", &tegra_clk_emc, NULL,  0, 0),
8382
8383 #ifdef CONFIG_TEGRA_DUAL_CBUS
8384         DUAL_CBUS_CLK("msenc.cbus",     "tegra_msenc",          "msenc", &tegra_clk_c2bus, "msenc", 0, 0),
8385         DUAL_CBUS_CLK("vde.cbus",       "tegra-avp",            "vde",   &tegra_clk_c2bus, "vde",   0, 0),
8386         DUAL_CBUS_CLK("se.cbus",        "tegra12-se",           NULL,    &tegra_clk_c2bus, "se",    0, 0),
8387         SHARED_LIMIT("cap.c2bus",       "cap.c2bus",            NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING),
8388         SHARED_LIMIT("cap.vcore.c2bus", "cap.vcore.c2bus",      NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING),
8389         SHARED_LIMIT("cap.throttle.c2bus", "cap_throttle",      NULL,    &tegra_clk_c2bus, NULL,    0, SHARED_CEILING),
8390         SHARED_LIMIT("floor.c2bus",     "floor.c2bus",          NULL,    &tegra_clk_c2bus, NULL,    0, 0),
8391         SHARED_CLK("override.c2bus",    "override.c2bus",       NULL,    &tegra_clk_c2bus, NULL,  0, SHARED_OVERRIDE),
8392
8393         DUAL_CBUS_CLK("vic03.cbus",     "tegra_vic03.0",                "vic03", &tegra_clk_c3bus, "vic03", 0, 0),
8394         DUAL_CBUS_CLK("tsec.cbus",      "tegra_tsec",           "tsec",  &tegra_clk_c3bus,  "tsec", 0, 0),
8395         SHARED_LIMIT("cap.c3bus",       "cap.c3bus",            NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING),
8396         SHARED_LIMIT("cap.vcore.c3bus", "cap.vcore.c3bus",      NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING),
8397         SHARED_LIMIT("cap.throttle.c3bus", "cap_throttle",      NULL,    &tegra_clk_c3bus, NULL,    0, SHARED_CEILING),
8398         SHARED_LIMIT("floor.c3bus",     "floor.c3bus",          NULL,    &tegra_clk_c3bus, NULL,    0, 0),
8399         SHARED_CLK("override.c3bus",    "override.c3bus",       NULL,    &tegra_clk_c3bus, NULL,  0, SHARED_OVERRIDE),
8400 #else
8401         SHARED_CLK("vic03.cbus",  "tegra_vic03.0",      "vic03", &tegra_clk_cbus, "vic03", 0, 0),
8402         SHARED_CLK("msenc.cbus","tegra_msenc",          "msenc",&tegra_clk_cbus, "msenc", 0, 0),
8403         SHARED_CLK("tsec.cbus", "tegra_tsec",           "tsec", &tegra_clk_cbus, "tsec", 0, 0),
8404         SHARED_CLK("vde.cbus",  "tegra-avp",            "vde",  &tegra_clk_cbus, "vde", 0, 0),
8405         SHARED_CLK("se.cbus",   "tegra12-se",           NULL,   &tegra_clk_cbus, "se",  0, 0),
8406         SHARED_LIMIT("cap.cbus", "cap.cbus",            NULL,   &tegra_clk_cbus, NULL,  0, SHARED_CEILING),
8407         SHARED_LIMIT("cap.vcore.cbus", "cap.vcore.cbus", NULL,  &tegra_clk_cbus, NULL,  0, SHARED_CEILING),
8408         SHARED_LIMIT("cap.throttle.cbus", "cap_throttle", NULL, &tegra_clk_cbus, NULL,  0, SHARED_CEILING),
8409         SHARED_LIMIT("floor.cbus", "floor.cbus",        NULL,   &tegra_clk_cbus, NULL,  0, 0),
8410         SHARED_CLK("override.cbus", "override.cbus",    NULL,   &tegra_clk_cbus, NULL,  0, SHARED_OVERRIDE),
8411 #endif
8412         SHARED_CLK("gk20a.gbus",        "tegra_gk20a",  "gpu",  &tegra_clk_gbus, NULL,  0, 0),
8413         SHARED_LIMIT("cap.gbus",        "cap.gbus",     NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8414         SHARED_LIMIT("edp.gbus",        "edp.gbus",     NULL,   &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8415         SHARED_LIMIT("battery.gbus",    "battery_edp",  "gpu",  &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8416         SHARED_LIMIT("cap.throttle.gbus", "cap_throttle", NULL, &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8417         SHARED_LIMIT("cap.profile.gbus", "profile.gbus", "cap", &tegra_clk_gbus, NULL,  0, SHARED_CEILING),
8418         SHARED_CLK("override.gbus",     "override.gbus", NULL,  &tegra_clk_gbus, NULL,  0, SHARED_OVERRIDE),
8419         SHARED_LIMIT("floor.gbus",      "floor.gbus",   NULL,   &tegra_clk_gbus, NULL,  0, 0),
8420         SHARED_LIMIT("floor.profile.gbus", "profile.gbus", "floor", &tegra_clk_gbus, NULL,  0, 0),
8421
8422         SHARED_CLK("automotive.host1x", "automotive",   "host1x", &tegra_clk_host1x, NULL,  0, 0),
8423         SHARED_CLK("nv.host1x", "tegra_host1x",         "host1x", &tegra_clk_host1x, NULL,  0, 0),
8424         SHARED_CLK("vi.host1x", "tegra_vi",             "host1x", &tegra_clk_host1x, NULL,  0, 0),
8425         SHARED_LIMIT("cap.host1x", "cap.host1x",        NULL,     &tegra_clk_host1x, NULL,  0, SHARED_CEILING),
8426         SHARED_LIMIT("cap.vcore.host1x", "cap.vcore.host1x", NULL, &tegra_clk_host1x, NULL,  0, SHARED_CEILING),
8427         SHARED_LIMIT("floor.host1x", "floor.host1x",    NULL,     &tegra_clk_host1x, NULL,  0, 0),
8428         SHARED_CLK("override.host1x", "override.host1x", NULL,    &tegra_clk_host1x, NULL,  0, SHARED_OVERRIDE),
8429
8430         SHARED_CLK("cpu.mselect",         "cpu",        "mselect",   &tegra_clk_mselect, NULL,  0, 0),
8431         SHARED_CLK("pcie.mselect",        "tegra_pcie", "mselect",   &tegra_clk_mselect, NULL,  0, 0),
8432         SHARED_CLK("automotive.mselect",  "automotive", "mselect",   &tegra_clk_mselect, NULL,  0, 0),
8433         SHARED_LIMIT("cap.vcore.mselect", "cap.vcore.mselect", NULL, &tegra_clk_mselect, NULL,  0, SHARED_CEILING),
8434         SHARED_CLK("override.mselect",    "override.mselect",  NULL, &tegra_clk_mselect, NULL,  0, SHARED_OVERRIDE),
8435 };
8436
8437 /* VI, ISP buses */
8438 static struct clk tegra_visp_clks[] = {
8439         SHARED_CONNECT("vi.c4bus",      "vi.c4bus",     NULL,   &tegra_clk_c4bus,   "vi",    0, 0),
8440         SHARED_CONNECT("isp.c4bus",     "isp.c4bus",    NULL,   &tegra_clk_c4bus,   "isp",   0, 0),
8441         SHARED_CLK("override.c4bus",    "override.c4bus", NULL, &tegra_clk_c4bus,    NULL,   0, SHARED_OVERRIDE),
8442
8443         SHARED_CLK("via.vi.c4bus",      "via.vi",       NULL,   &tegra_visp_clks[0], NULL,   0, 0),
8444         SHARED_CLK("vib.vi.c4bus",      "vib.vi",       NULL,   &tegra_visp_clks[0], NULL,   0, 0),
8445
8446         SHARED_CLK("ispa.isp.c4bus",    "ispa.isp",     NULL,   &tegra_visp_clks[1], "ispa", 0, 0),
8447         SHARED_CLK("ispb.isp.c4bus",    "ispb.isp",     NULL,   &tegra_visp_clks[1], "ispb", 0, 0),
8448 };
8449
8450 /* XUSB clocks */
8451 #define XUSB_ID "tegra-xhci"
8452 /* xusb common clock gate - enabled on init and never disabled */
8453 static void tegra12_xusb_gate_clk_init(struct clk *c)
8454 {
8455         tegra12_periph_clk_enable(c);
8456 }
8457
8458 static struct clk_ops tegra_xusb_gate_clk_ops = {
8459         .init    = tegra12_xusb_gate_clk_init,
8460 };
8461
8462 static struct clk tegra_clk_xusb_gate = {
8463         .name      = "xusb_gate",
8464         .flags     = ENABLE_ON_INIT | PERIPH_NO_RESET,
8465         .ops       = &tegra_xusb_gate_clk_ops,
8466         .rate      = 12000000,
8467         .max_rate  = 48000000,
8468         .u.periph = {
8469                 .clk_num   = 143,
8470         },
8471 };
8472
8473 static struct clk tegra_xusb_source_clks[] = {
8474         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),
8475         PERIPH_CLK("xusb_falcon_src",   XUSB_ID, "falcon_src",  143,    0x604,  336000000, mux_clkm_pllp_pllc_pllre,    MUX | DIV_U71 | PERIPH_NO_RESET),
8476         PERIPH_CLK("xusb_fs_src",       XUSB_ID, "fs_src",      143,    0x608,   48000000, mux_clkm_48M_pllp_480M,      MUX | DIV_U71 | PERIPH_NO_RESET),
8477         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),
8478         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),
8479         SHARED_EMC_CLK("xusb.emc",      XUSB_ID, "emc", &tegra_clk_emc, NULL,   0,      SHARED_BW, 0),
8480 };
8481
8482 static struct clk tegra_xusb_ss_div2 = {
8483         .name      = "xusb_ss_div2",
8484         .ops       = &tegra_clk_m_div_ops,
8485         .parent    = &tegra_xusb_source_clks[3],
8486         .mul       = 1,
8487         .div       = 2,
8488         .state     = OFF,
8489         .max_rate  = 61200000,
8490 };
8491
8492 static struct clk_mux_sel mux_ss_div2_pllu_60M[] = {
8493         { .input = &tegra_xusb_ss_div2, .value = 0},
8494         { .input = &tegra_pll_u_60M,    .value = 1},
8495         { 0, 0},
8496 };
8497
8498 static struct clk tegra_xusb_hs_src = {
8499         .name      = "xusb_hs_src",
8500         .lookup    = {
8501                 .dev_id    = XUSB_ID,
8502                 .con_id    = "hs_src",
8503         },
8504         .ops       = &tegra_periph_clk_ops,
8505         .reg       = 0x610,
8506         .inputs    = mux_ss_div2_pllu_60M,
8507         .flags     = MUX | PLLU | PERIPH_NO_ENB,
8508         .max_rate  = 60000000,
8509         .u.periph = {
8510                 .src_mask  = 0x1 << 25,
8511                 .src_shift = 25,
8512         },
8513 };
8514
8515 static struct clk_mux_sel mux_xusb_host[] = {
8516         { .input = &tegra_xusb_source_clks[0], .value = 0},
8517         { .input = &tegra_xusb_source_clks[1], .value = 1},
8518         { .input = &tegra_xusb_source_clks[2], .value = 2},
8519         { .input = &tegra_xusb_hs_src,         .value = 5},
8520         { 0, 0},
8521 };
8522
8523 static struct clk_mux_sel mux_xusb_ss[] = {
8524         { .input = &tegra_xusb_source_clks[3], .value = 3},
8525         { .input = &tegra_xusb_source_clks[0], .value = 0},
8526         { .input = &tegra_xusb_source_clks[1], .value = 1},
8527         { 0, 0},
8528 };
8529
8530 static struct clk_mux_sel mux_xusb_dev[] = {
8531         { .input = &tegra_xusb_source_clks[4], .value = 4},
8532         { .input = &tegra_xusb_source_clks[2], .value = 2},
8533         { .input = &tegra_xusb_source_clks[3], .value = 3},
8534         { 0, 0},
8535 };
8536
8537 static struct clk tegra_xusb_coupled_clks[] = {
8538         PERIPH_CLK_EX("xusb_host", XUSB_ID, "host", 89, 0, 350000000, mux_xusb_host, 0, &tegra_clk_coupled_gate_ops),
8539         PERIPH_CLK_EX("xusb_ss",   XUSB_ID, "ss",  156, 0, 350000000, mux_xusb_ss,   0, &tegra_clk_coupled_gate_ops),
8540         PERIPH_CLK_EX("xusb_dev",  XUSB_ID, "dev",  95, 0, 120000000, mux_xusb_dev,  0, &tegra_clk_coupled_gate_ops),
8541 };
8542
8543 #define CLK_DUPLICATE(_name, _dev, _con)                \
8544         {                                               \
8545                 .name   = _name,                        \
8546                 .lookup = {                             \
8547                         .dev_id = _dev,                 \
8548                         .con_id         = _con,         \
8549                 },                                      \
8550         }
8551
8552 /* Some clocks may be used by different drivers depending on the board
8553  * configuration.  List those here to register them twice in the clock lookup
8554  * table under two names.
8555  */
8556 struct clk_duplicate tegra_clk_duplicates[] = {
8557         CLK_DUPLICATE("uarta",  "serial8250.0", NULL),
8558         CLK_DUPLICATE("uartb",  "serial8250.1", NULL),
8559         CLK_DUPLICATE("uartc",  "serial8250.2", NULL),
8560         CLK_DUPLICATE("uartd",  "serial8250.3", NULL),
8561         CLK_DUPLICATE("usbd", XUSB_ID, "utmip-pad"),
8562         CLK_DUPLICATE("usbd", "utmip-pad", NULL),
8563         CLK_DUPLICATE("usbd", "tegra-ehci.0", NULL),
8564         CLK_DUPLICATE("usbd", "tegra-otg", NULL),
8565         CLK_DUPLICATE("disp1", "tegra_dc_dsi_vs1.0", NULL),
8566         CLK_DUPLICATE("disp1.emc", "tegra_dc_dsi_vs1.0", "emc"),
8567         CLK_DUPLICATE("disp1", "tegra_dc_dsi_vs1.1", NULL),
8568         CLK_DUPLICATE("disp1.emc", "tegra_dc_dsi_vs1.1", "emc"),
8569         CLK_DUPLICATE("hdmi", "tegradc.0", "hdmi"),
8570         CLK_DUPLICATE("hdmi", "tegradc.1", "hdmi"),
8571         CLK_DUPLICATE("dsib", "tegradc.0", "dsib"),
8572         CLK_DUPLICATE("dsia", "tegradc.1", "dsia"),
8573         CLK_DUPLICATE("dsiblp", "tegradc.0", "dsiblp"),
8574         CLK_DUPLICATE("dsialp", "tegradc.1", "dsialp"),
8575         CLK_DUPLICATE("dsia", "tegra_dc_dsi_vs1.0", "dsia"),
8576         CLK_DUPLICATE("dsia", "tegra_dc_dsi_vs1.1", "dsia"),
8577         CLK_DUPLICATE("dsialp", "tegra_dc_dsi_vs1.0", "dsialp"),
8578         CLK_DUPLICATE("dsialp", "tegra_dc_dsi_vs1.1", "dsialp"),
8579         CLK_DUPLICATE("dsi1-fixed", "tegra_dc_dsi_vs1.0", "dsi-fixed"),
8580         CLK_DUPLICATE("dsi1-fixed", "tegra_dc_dsi_vs1.1", "dsi-fixed"),
8581         CLK_DUPLICATE("cop", "tegra-avp", "cop"),
8582         CLK_DUPLICATE("bsev", "tegra-avp", "bsev"),
8583         CLK_DUPLICATE("cop", "nvavp", "cop"),
8584         CLK_DUPLICATE("bsev", "nvavp", "bsev"),
8585         CLK_DUPLICATE("vde", "tegra-aes", "vde"),
8586         CLK_DUPLICATE("bsea", "tegra-aes", "bsea"),
8587         CLK_DUPLICATE("bsea", "nvavp", "bsea"),
8588         CLK_DUPLICATE("cml1", "tegra_sata_cml", NULL),
8589         CLK_DUPLICATE("cml0", "tegra_pcie", "cml"),
8590         CLK_DUPLICATE("pciex", "tegra_pcie", "pciex"),
8591         CLK_DUPLICATE("clk_m", NULL, "apb_pclk"),
8592         CLK_DUPLICATE("i2c1", "tegra-i2c-slave.0", NULL),
8593         CLK_DUPLICATE("i2c2", "tegra-i2c-slave.1", NULL),
8594         CLK_DUPLICATE("i2c3", "tegra-i2c-slave.2", NULL),
8595         CLK_DUPLICATE("i2c4", "tegra-i2c-slave.3", NULL),
8596         CLK_DUPLICATE("i2c5", "tegra-i2c-slave.4", NULL),
8597         CLK_DUPLICATE("cl_dvfs_ref", "tegra12-i2c.4", NULL),
8598         CLK_DUPLICATE("cl_dvfs_soc", "tegra12-i2c.4", NULL),
8599         CLK_DUPLICATE("sbc1", "tegra11-spi-slave.0", NULL),
8600         CLK_DUPLICATE("sbc2", "tegra11-spi-slave.1", NULL),
8601         CLK_DUPLICATE("sbc3", "tegra11-spi-slave.2", NULL),
8602         CLK_DUPLICATE("sbc4", "tegra11-spi-slave.3", NULL),
8603         CLK_DUPLICATE("sbc5", "tegra11-spi-slave.4", NULL),
8604         CLK_DUPLICATE("sbc6", "tegra11-spi-slave.5", NULL),
8605         CLK_DUPLICATE("vcp", "nvavp", "vcp"),
8606         CLK_DUPLICATE("avp.sclk", "nvavp", "sclk"),
8607         CLK_DUPLICATE("avp.emc", "nvavp", "emc"),
8608         CLK_DUPLICATE("vde.cbus", "nvavp", "vde"),
8609         CLK_DUPLICATE("i2c5", "tegra_cl_dvfs", "i2c"),
8610         CLK_DUPLICATE("cpu_g", "tegra_cl_dvfs", "safe_dvfs"),
8611         CLK_DUPLICATE("actmon", "tegra_host1x", "actmon"),
8612         CLK_DUPLICATE("gpu_ref", "tegra_gk20a.0", "PLLG_ref"),
8613         CLK_DUPLICATE("gbus", "tegra_gk20a.0", "PLLG_out"),
8614         CLK_DUPLICATE("pll_p_out5", "tegra_gk20a.0", "pwr"),
8615         CLK_DUPLICATE("ispa.isp.c4bus", "tegra_isp.0", "isp"),
8616         CLK_DUPLICATE("ispb.isp.c4bus", "tegra_isp.1", "isp"),
8617         CLK_DUPLICATE("via.vi.c4bus", "tegra_vi.0", "vi"),
8618         CLK_DUPLICATE("vib.vi.c4bus", "tegra_vi.1", "vi"),
8619         CLK_DUPLICATE("csi", "tegra_vi.0", "csi"),
8620         CLK_DUPLICATE("csi", "tegra_vi.1", "csi"),
8621         CLK_DUPLICATE("csus", "tegra_vi.0", "csus"),
8622         CLK_DUPLICATE("vim2_clk", "tegra_vi.1", "vim2_clk"),
8623         CLK_DUPLICATE("cilab", "tegra_vi.0", "cilab"),
8624         CLK_DUPLICATE("cilcd", "tegra_vi.1", "cilcd"),
8625         CLK_DUPLICATE("cile", "tegra_vi.1", "cile"),
8626         CLK_DUPLICATE("i2s0", NULL, "i2s0"),
8627         CLK_DUPLICATE("i2s1", NULL, "i2s1"),
8628         CLK_DUPLICATE("i2s2", NULL, "i2s2"),
8629         CLK_DUPLICATE("i2s3", NULL, "i2s3"),
8630         CLK_DUPLICATE("i2s4", NULL, "i2s4"),
8631         CLK_DUPLICATE("dam0", NULL, "dam0"),
8632         CLK_DUPLICATE("dam1", NULL, "dam1"),
8633         CLK_DUPLICATE("dam2", NULL, "dam2"),
8634         CLK_DUPLICATE("spdif_in", NULL, "spdif_in"),
8635         CLK_DUPLICATE("mclk", NULL, "default_mclk"),
8636         CLK_DUPLICATE("amx", NULL, "amx"),
8637         CLK_DUPLICATE("amx1", NULL, "amx1"),
8638         CLK_DUPLICATE("adx", NULL, "adx"),
8639         CLK_DUPLICATE("adx1", NULL, "adx1"),
8640         CLK_DUPLICATE("afc0", NULL, "afc0"),
8641         CLK_DUPLICATE("afc1", NULL, "afc1"),
8642         CLK_DUPLICATE("afc2", NULL, "afc2"),
8643         CLK_DUPLICATE("afc3", NULL, "afc3"),
8644         CLK_DUPLICATE("afc4", NULL, "afc4"),
8645         CLK_DUPLICATE("afc5", NULL, "afc5"),
8646         CLK_DUPLICATE("amx", "tegra124-amx.0", NULL),
8647         CLK_DUPLICATE("amx1", "tegra124-amx.1", NULL),
8648         CLK_DUPLICATE("adx", "tegra124-adx.0", NULL),
8649         CLK_DUPLICATE("adx1", "tegra124-adx.1", NULL),
8650         CLK_DUPLICATE("amx", "tegra30-ahub-apbif", "amx"),
8651         CLK_DUPLICATE("amx1", "tegra30-ahub-apbif", "amx1"),
8652         CLK_DUPLICATE("adx", "tegra30-ahub-apbif", "adx"),
8653         CLK_DUPLICATE("adx1", "tegra30-ahub-apbif", "adx1"),
8654         CLK_DUPLICATE("d_audio", "tegra30-ahub-xbar", "d_audio"),
8655         CLK_DUPLICATE("apbif", "tegra30-ahub-apbif", "apbif"),
8656         CLK_DUPLICATE("afc0", "tegra30-ahub-apbif", "afc0"),
8657         CLK_DUPLICATE("afc1", "tegra30-ahub-apbif", "afc1"),
8658         CLK_DUPLICATE("afc2", "tegra30-ahub-apbif", "afc2"),
8659         CLK_DUPLICATE("afc3", "tegra30-ahub-apbif", "afc3"),
8660         CLK_DUPLICATE("afc4", "tegra30-ahub-apbif", "afc4"),
8661         CLK_DUPLICATE("afc5", "tegra30-ahub-apbif", "afc5"),
8662         CLK_DUPLICATE("cpu_g", "tegra_simon", "cpu"),
8663 };
8664
8665 struct clk *tegra_ptr_clks[] = {
8666         &tegra_clk_32k,
8667         &tegra_clk_m,
8668         &tegra_clk_m_div2,
8669         &tegra_clk_m_div4,
8670         &tegra_pll_ref,
8671         &tegra_pll_m,
8672         &tegra_pll_m_out1,
8673         &tegra_pll_c,
8674         &tegra_pll_c_out1,
8675         &tegra_pll_c2,
8676         &tegra_pll_c3,
8677         &tegra_pll_p,
8678         &tegra_pll_p_out1,
8679         &tegra_pll_p_out2,
8680         &tegra_pll_p_out3,
8681         &tegra_pll_p_out4,
8682         &tegra_pll_p_out5,
8683         &tegra_pll_a,
8684         &tegra_pll_a_out0,
8685         &tegra_pll_d,
8686         &tegra_pll_d_out0,
8687         &tegra_clk_xusb_gate,
8688         &tegra_pll_u,
8689         &tegra_pll_u_480M,
8690         &tegra_pll_u_60M,
8691         &tegra_pll_u_48M,
8692         &tegra_pll_u_12M,
8693         &tegra_pll_x,
8694         &tegra_pll_x_out0,
8695         &tegra_dfll_cpu,
8696         &tegra_pll_d2,
8697         &tegra_pll_c4,
8698         &tegra_pll_dp,
8699         &tegra_pll_re_vco,
8700         &tegra_pll_re_out,
8701         &tegra_pll_e,
8702         &tegra_cml0_clk,
8703         &tegra_cml1_clk,
8704         &tegra_pciex_clk,
8705         &tegra_clk_cclk_g,
8706 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8707         &tegra_clk_cclk_lp,
8708 #endif
8709         &tegra_clk_sclk,
8710         &tegra_clk_hclk,
8711         &tegra_clk_pclk,
8712         &tegra_clk_virtual_cpu_g,
8713 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8714         &tegra_clk_virtual_cpu_lp,
8715 #endif
8716         &tegra_clk_cpu_cmplx,
8717         &tegra_clk_blink,
8718         &tegra_clk_cop,
8719         &tegra_clk_sbus_cmplx,
8720         &tegra_clk_ahb,
8721         &tegra_clk_apb,
8722         &tegra_clk_emc,
8723         &tegra_clk_mc,
8724         &tegra_clk_host1x,
8725         &tegra_clk_mselect,
8726 #ifdef CONFIG_TEGRA_DUAL_CBUS
8727         &tegra_clk_c2bus,
8728         &tegra_clk_c3bus,
8729 #else
8730         &tegra_clk_cbus,
8731 #endif
8732         &tegra_clk_gpu,
8733         &tegra_clk_gbus,
8734         &tegra_clk_isp,
8735         &tegra_clk_c4bus,
8736 };
8737
8738 struct clk *tegra_ptr_camera_mclks[] = {
8739         &tegra_camera_mclk,
8740         &tegra_camera_mclk2,
8741 };
8742
8743 /*
8744  * Use this API only when all the clocks are not registered to the clock
8745  * subsystem.
8746  */
8747 static struct clk *query_clk_from_list(char *clk_name)
8748 {
8749         int i;
8750
8751         if (!clk_name)
8752                 return NULL;
8753
8754         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
8755                 if (!strcmp(tegra_list_clks[i].name, clk_name))
8756                         return &tegra_list_clks[i];
8757
8758         return NULL;
8759 }
8760
8761 /*
8762  * Handle special clocks to check if they can be set to safe rate
8763  */
8764 static bool tegra12_periph_is_special_reset(struct clk *c)
8765 {
8766         struct clk *temp;
8767
8768         if (!strcmp(c->name, "isp")) {
8769                 /* Make sure that ispa and ispb are in reset */
8770
8771                 /*
8772                  * Since clocks may not have been registered by this time,
8773                  * so query clock structure directly from the list
8774                  */
8775                 temp = query_clk_from_list("ispa");
8776                 if (!temp)
8777                         return false;
8778
8779                 /* If ispa is not in reset, return false */
8780                 if (!IS_PERIPH_IN_RESET(temp))
8781                         return false;
8782
8783                 temp = query_clk_from_list("ispb");
8784                 if (!temp)
8785                         return false;
8786
8787                 /* If ispb is not in reset, return false */
8788                 if (!IS_PERIPH_IN_RESET(temp))
8789                         return false;
8790
8791                 return true;
8792         }
8793
8794         if (!strcmp(c->name, "vi_sensor") || !strcmp(c->name, "vi_sensor2")) {
8795                 temp = query_clk_from_list("vi");
8796                 if (!temp)
8797                         return false;
8798
8799                 /* If vi is not in reset, return false */
8800                 if (!IS_PERIPH_IN_RESET(temp))
8801                         return false;
8802
8803                 return true;
8804         }
8805
8806
8807         if (!strcmp(c->name, "hdmi_audio")) {
8808                 temp = query_clk_from_list("hdmi");
8809                 if (!temp)
8810                         return false;
8811
8812                 /* If hdmi is not in reset, return false */
8813                 if (!IS_PERIPH_IN_RESET(temp))
8814                         return false;
8815
8816                 return true;
8817         }
8818
8819         return false;
8820 }
8821
8822 /* Return true from this function if the target rate can be locked without
8823    switching pll clients to back-up source */
8824 static bool tegra12_is_dyn_ramp(
8825         struct clk *c, unsigned long rate, bool from_vco_min)
8826 {
8827 #if PLLCX_USE_DYN_RAMP
8828         /* PLLC2, PLLC3 support dynamic ramp only when output divider <= 8 */
8829         if ((c == &tegra_pll_c2) || (c == &tegra_pll_c3)) {
8830                 struct clk_pll_freq_table cfg, old_cfg;
8831                 unsigned long input_rate = clk_get_rate(c->parent);
8832
8833                 u32 val = clk_readl(c->reg + PLL_BASE);
8834                 PLL_BASE_PARSE(PLLCX, old_cfg, val);
8835                 old_cfg.p = pllcx_p[old_cfg.p];
8836
8837                 if (!pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, NULL)) {
8838                         if ((cfg.n == old_cfg.n) ||
8839                             PLLCX_IS_DYN(cfg.p, old_cfg.p))
8840                                 return true;
8841                 }
8842         }
8843 #endif
8844
8845 #if PLLXC_USE_DYN_RAMP
8846         /* PPLX, PLLC support dynamic ramp when changing NDIV only */
8847         if ((c == &tegra_pll_x) || (c == &tegra_pll_c)) {
8848                 struct clk_pll_freq_table cfg, old_cfg;
8849                 unsigned long input_rate = clk_get_rate(c->parent);
8850
8851                 if (from_vco_min) {
8852                         old_cfg.m = PLL_FIXED_MDIV(c, input_rate);
8853                         old_cfg.p = 1;
8854                 } else {
8855                         if (c->flags & PLLX)
8856                                 u32 val = clk_readlx(c->reg + PLL_BASE);
8857                         else
8858                                 u32 val = clk_readl(c->reg + PLL_BASE);
8859                         PLL_BASE_PARSE(PLLXC, old_cfg, val);
8860                         old_cfg.p = pllxc_p[old_cfg.p];
8861                 }
8862
8863                 if (!pll_dyn_ramp_find_cfg(c, &cfg, rate, input_rate, NULL)) {
8864                         if ((cfg.m == old_cfg.m) && (cfg.p == old_cfg.p))
8865                                 return true;
8866                 }
8867         }
8868 #endif
8869         return false;
8870 }
8871
8872 /* DFLL late init called with CPU clock lock taken */
8873 static void __init tegra12_dfll_cpu_late_init(struct clk *c)
8874 {
8875 #ifdef CONFIG_ARCH_TEGRA_HAS_CL_DVFS
8876         int ret;
8877         struct clk *cpu = &tegra_clk_virtual_cpu_g;
8878
8879         if (!cpu || !cpu->dvfs) {
8880                 pr_err("%s: CPU dvfs is not present\n", __func__);
8881                 return;
8882         }
8883         tegra_dvfs_set_dfll_tune_trimmers(cpu->dvfs, tune_cpu_trimmers);
8884
8885         /* release dfll clock source reset, init cl_dvfs control logic, and
8886            move dfll to initialized state, so it can be used as CPU source */
8887         tegra_periph_reset_deassert(c);
8888         ret = tegra_init_cl_dvfs();
8889         if (!ret) {
8890                 c->state = OFF;
8891                 if (tegra_platform_is_silicon()) {
8892                         if (tegra_override_dfll_range !=
8893                                         TEGRA_USE_DFLL_CDEV_CNTRL)
8894                                 use_dfll = CONFIG_TEGRA_USE_DFLL_RANGE;
8895 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
8896                         if (tegra_cpu_speedo_id() == 0)
8897                                 use_dfll = 0;
8898 #endif
8899                 }
8900                 tegra_dvfs_set_dfll_range(cpu->dvfs, use_dfll);
8901                 tegra_cl_dvfs_debug_init(c);
8902                 pr_info("Tegra CPU DFLL is initialized with use_dfll = %d\n",
8903                         use_dfll);
8904         }
8905 #endif
8906 }
8907
8908 /*
8909  * Backup pll is used as transitional CPU clock source while main pll is
8910  * relocking; in addition all CPU rates below backup level are sourced from
8911  * backup pll only. Target backup levels for each CPU mode are selected high
8912  * enough to avoid voltage droop when CPU clock is switched between backup and
8913  * main plls. Actual backup rates will be rounded to match backup source fixed
8914  * frequency. Backup rates are also used as stay-on-backup thresholds, and must
8915  * be kept the same in G and LP mode (will need to add a separate stay-on-backup
8916  * parameter to allow different backup rates if necessary).
8917  *
8918  * Sbus threshold must be exact factor of pll_p rate.
8919  */
8920 #define CPU_G_BACKUP_RATE_TARGET        200000000
8921 #define CPU_LP_BACKUP_RATE_TARGET       200000000
8922
8923 static void tegra12_pllp_init_dependencies(unsigned long pllp_rate)
8924 {
8925 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8926         u32 div;
8927         unsigned long backup_rate;
8928 #endif
8929
8930         switch (pllp_rate) {
8931         case 216000000:
8932                 tegra_pll_p_out1.u.pll_div.default_rate = 28800000;
8933                 tegra_pll_p_out3.u.pll_div.default_rate = 72000000;
8934                 tegra_clk_sbus_cmplx.u.system.threshold = 108000000;
8935                 tegra_clk_host1x.u.periph.threshold = 108000000;
8936                 break;
8937         case 408000000:
8938                 tegra_pll_p_out1.u.pll_div.default_rate = 9600000;
8939                 tegra_pll_p_out3.u.pll_div.default_rate = 102000000;
8940                 tegra_clk_sbus_cmplx.u.system.threshold = 204000000;
8941                 tegra_clk_host1x.u.periph.threshold = 204000000;
8942                 break;
8943         case 204000000:
8944                 tegra_pll_p_out1.u.pll_div.default_rate = 4800000;
8945                 tegra_pll_p_out3.u.pll_div.default_rate = 102000000;
8946                 tegra_clk_sbus_cmplx.u.system.threshold = 204000000;
8947                 tegra_clk_host1x.u.periph.threshold = 204000000;
8948                 break;
8949         default:
8950                 pr_err("tegra: PLLP rate: %lu is not supported\n", pllp_rate);
8951                 BUG();
8952         }
8953         pr_info("tegra: PLLP fixed rate: %lu\n", pllp_rate);
8954
8955 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
8956         div = pllp_rate / CPU_G_BACKUP_RATE_TARGET;
8957         backup_rate = pllp_rate / div;
8958         tegra_clk_virtual_cpu_g.u.cpu.backup_rate = backup_rate;
8959
8960         div = pllp_rate / CPU_LP_BACKUP_RATE_TARGET;
8961         backup_rate = pllp_rate / div;
8962         tegra_clk_virtual_cpu_lp.u.cpu.backup_rate = backup_rate;
8963 #else
8964         tegra_clk_virtual_cpu_g.u.cpu.backup_rate = pllp_rate;
8965 #endif
8966 }
8967
8968 static void tegra12_init_one_clock(struct clk *c)
8969 {
8970         clk_init(c);
8971         INIT_LIST_HEAD(&c->shared_bus_list);
8972         if (!c->lookup.dev_id && !c->lookup.con_id)
8973                 c->lookup.con_id = c->name;
8974         c->lookup.clk = c;
8975         clkdev_add(&c->lookup);
8976 }
8977
8978 /* Direct access to CPU clock sources fot CPU idle driver */
8979 int tegra12_cpu_g_idle_rate_exchange(unsigned long *rate)
8980 {
8981         int ret = 0;
8982         struct clk *dfll = tegra_clk_cpu_cmplx.parent->u.cpu.dynamic;
8983         unsigned long old_rate, new_rate, flags;
8984
8985         if (!dfll || !tegra_dvfs_rail_is_dfll_mode(tegra_cpu_rail))
8986                 return -EPERM;
8987
8988         /* Clipping min to oscillator rate is pretty much arbitrary */
8989         new_rate = max(*rate, tegra_clk_m.rate);
8990
8991         clk_lock_save(dfll, &flags);
8992
8993         old_rate = clk_get_rate_locked(dfll);
8994         *rate = old_rate;
8995         if (new_rate != old_rate)
8996                 ret = clk_set_rate_locked(dfll, new_rate);
8997
8998         clk_unlock_restore(dfll, &flags);
8999         return ret;
9000 }
9001
9002 int tegra12_cpu_lp_idle_rate_exchange(unsigned long *rate)
9003 {
9004         int ret = 0;
9005         struct clk *backup = tegra_clk_cpu_cmplx.parent->u.cpu.backup;
9006         unsigned long old_rate, flags;
9007         unsigned long new_rate = min(
9008                 *rate, tegra_clk_cpu_cmplx.parent->u.cpu.backup_rate);
9009
9010         clk_lock_save(backup, &flags);
9011
9012         old_rate = clk_get_rate_locked(backup);
9013         *rate = old_rate;
9014         if (new_rate != old_rate)
9015                 ret = clk_set_rate_locked(backup, new_rate);
9016
9017         clk_unlock_restore(backup, &flags);
9018         return ret;
9019 }
9020
9021 void tegra_edp_throttle_cpu_now(u8 factor)
9022 {
9023         /* empty definition for tegra12 */
9024         return;
9025 }
9026
9027 bool tegra_clk_is_parent_allowed(struct clk *c, struct clk *p)
9028 {
9029         /*
9030          * Most of the Tegra12 multimedia and peripheral muxes include pll_c2
9031          * and pll_c3 as possible inputs. However, per clock policy these plls
9032          * are allowed to be used only by handful devices aggregated on cbus.
9033          * For all others, instead of enforcing policy at run-time in this
9034          * function, we simply stripped out pll_c2 and pll_c3 options from the
9035          * respective muxes statically.
9036          */
9037
9038         /*
9039          * In configuration with dual cbus pll_c can be used as a scaled clock
9040          * source for EMC only when pll_m is fixed, or as a general fixed rate
9041          * clock source for EMC and other peripherals if pll_m is scaled. In
9042          * configuration with single cbus pll_c can be used as a scaled cbus
9043          * clock source only. No direct use for pll_c by super clocks.
9044          */
9045         if ((p == &tegra_pll_c) && (c != &tegra_pll_c_out1)) {
9046                 if (c->ops == &tegra_super_ops)
9047                         return false;
9048 #ifdef CONFIG_TEGRA_DUAL_CBUS
9049 #ifndef CONFIG_TEGRA_PLLM_SCALED
9050                 return c->flags & PERIPH_EMC_ENB;
9051 #endif
9052 #else
9053                 return c->flags & PERIPH_ON_CBUS;
9054 #endif
9055         }
9056
9057         /*
9058          * In any configuration pll_m must not be used as a clock source for
9059          * cbus modules. If pll_m is scaled it can be used as EMC source only.
9060          * Otherwise fixed rate pll_m can be used as clock source for EMC and
9061          * other peripherals. No direct use for pll_m by super clocks.
9062          */
9063         if ((p == &tegra_pll_m) && (c != &tegra_pll_m_out1)) {
9064                 if (c->ops == &tegra_super_ops)
9065                         return false;
9066
9067                 if (c->flags & PERIPH_ON_CBUS)
9068                         return false;
9069 #ifdef CONFIG_TEGRA_PLLM_SCALED
9070                 return c->flags & PERIPH_EMC_ENB;
9071 #endif
9072         }
9073         return true;
9074 }
9075
9076 /* Internal LA may request some clocks to be enabled on init via TRANSACTION
9077    SCRATCH register settings */
9078 void __init tegra12x_clk_init_la(void)
9079 {
9080         struct clk *c;
9081         u32 reg = readl((void *)
9082                 ((uintptr_t)misc_gp_base + MISC_GP_TRANSACTOR_SCRATCH_0));
9083
9084         if (!(reg & MISC_GP_TRANSACTOR_SCRATCH_LA_ENABLE))
9085                 return;
9086
9087         c = tegra_get_clock_by_name("la");
9088         if (WARN(!c, "%s: could not find la clk\n", __func__))
9089                 return;
9090         clk_enable(c);
9091
9092         if (reg & MISC_GP_TRANSACTOR_SCRATCH_DDS_ENABLE) {
9093                 c = tegra_get_clock_by_name("dds");
9094                 if (WARN(!c, "%s: could not find la clk\n", __func__))
9095                         return;
9096                 clk_enable(c);
9097         }
9098         if (reg & MISC_GP_TRANSACTOR_SCRATCH_DP2_ENABLE) {
9099                 c = tegra_get_clock_by_name("dp2");
9100                 if (WARN(!c, "%s: could not find la clk\n", __func__))
9101                         return;
9102                 clk_enable(c);
9103
9104                 c = tegra_get_clock_by_name("hdmi");
9105                 if (WARN(!c, "%s: could not find la clk\n", __func__))
9106                         return;
9107                 clk_enable(c);
9108         }
9109 }
9110
9111 #ifdef CONFIG_CPU_FREQ
9112
9113 /*
9114  * Frequency table index must be sequential starting at 0 and frequencies
9115  * must be ascending.
9116  */
9117 #define CPU_FREQ_STEP 102000 /* 102MHz cpu_g table step */
9118 #define CPU_FREQ_TABLE_MAX_SIZE (2 * MAX_DVFS_FREQS + 1)
9119
9120 static struct cpufreq_frequency_table freq_table[CPU_FREQ_TABLE_MAX_SIZE];
9121 static struct tegra_cpufreq_table_data freq_table_data;
9122
9123 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
9124 struct tegra_cpufreq_table_data *tegra_cpufreq_table_get(void)
9125 {
9126         int i, j;
9127         bool g_vmin_done = false;
9128         unsigned int freq, lp_backup_freq, g_vmin_freq, g_start_freq, max_freq;
9129         struct clk *cpu_clk_g = tegra_get_clock_by_name("cpu_g");
9130         struct clk *cpu_clk_lp = tegra_get_clock_by_name("cpu_lp");
9131
9132         /* Initialize once */
9133         if (freq_table_data.freq_table)
9134                 return &freq_table_data;
9135
9136         /* Clean table */
9137         for (i = 0; i < CPU_FREQ_TABLE_MAX_SIZE; i++) {
9138                 freq_table[i].index = i;
9139                 freq_table[i].frequency = CPUFREQ_TABLE_END;
9140         }
9141
9142         lp_backup_freq = cpu_clk_lp->u.cpu.backup_rate / 1000;
9143         if (!lp_backup_freq) {
9144                 WARN(1, "%s: cannot make cpufreq table: no LP CPU backup rate\n",
9145                      __func__);
9146                 return NULL;
9147         }
9148         if (!cpu_clk_lp->dvfs) {
9149                 WARN(1, "%s: cannot make cpufreq table: no LP CPU dvfs\n",
9150                      __func__);
9151                 return NULL;
9152         }
9153         if (!cpu_clk_g->dvfs) {
9154                 WARN(1, "%s: cannot make cpufreq table: no G CPU dvfs\n",
9155                      __func__);
9156                 return NULL;
9157         }
9158         g_vmin_freq = cpu_clk_g->dvfs->freqs[0] / 1000;
9159         if (g_vmin_freq < lp_backup_freq) {
9160                 WARN(1, "%s: cannot make cpufreq table: LP CPU backup rate"
9161                         " exceeds G CPU rate at Vmin\n", __func__);
9162                 return NULL;
9163         }
9164         /* Avoid duplicate frequency if g_vim_freq is already part of table */
9165         if (g_vmin_freq == lp_backup_freq)
9166                 g_vmin_done = true;
9167
9168         /* Start with backup frequencies */
9169         i = 0;
9170         freq = lp_backup_freq;
9171         freq_table[i++].frequency = freq/4;
9172         freq_table[i++].frequency = freq/2;
9173         freq_table[i++].frequency = freq;
9174
9175         /* Throttle low index at backup level*/
9176         freq_table_data.throttle_lowest_index = i - 1;
9177
9178         /*
9179          * Next, set table steps along LP CPU dvfs ladder, but make sure G CPU
9180          * dvfs rate at minimum voltage is not missed (if it happens to be below
9181          * LP maximum rate)
9182          */
9183         max_freq = cpu_clk_lp->max_rate / 1000;
9184         for (j = 0; j < cpu_clk_lp->dvfs->num_freqs; j++) {
9185                 freq = cpu_clk_lp->dvfs->freqs[j] / 1000;
9186                 if (freq <= lp_backup_freq)
9187                         continue;
9188
9189                 if (!g_vmin_done && (freq >= g_vmin_freq)) {
9190                         g_vmin_done = true;
9191                         if (freq > g_vmin_freq)
9192                                 freq_table[i++].frequency = g_vmin_freq;
9193                 }
9194                 freq_table[i++].frequency = freq;
9195
9196                 if (freq == max_freq)
9197                         break;
9198         }
9199
9200         /* Set G CPU min rate at least one table step below LP maximum */
9201         cpu_clk_g->min_rate = min(freq_table[i-2].frequency, g_vmin_freq)*1000;
9202
9203         /* Suspend index at max LP CPU */
9204         freq_table_data.suspend_index = i - 1;
9205
9206         /* Fill in "hole" (if any) between LP CPU maximum rate and G CPU dvfs
9207            ladder rate at minimum voltage */
9208         if (freq < g_vmin_freq) {
9209                 int n = (g_vmin_freq - freq) / CPU_FREQ_STEP;
9210                 for (j = 0; j <= n; j++) {
9211                         freq = g_vmin_freq - CPU_FREQ_STEP * (n - j);
9212                         freq_table[i++].frequency = freq;
9213                 }
9214         }
9215
9216         /* Now, step along the rest of G CPU dvfs ladder */
9217         g_start_freq = freq;
9218         max_freq = cpu_clk_g->max_rate / 1000;
9219         for (j = 0; j < cpu_clk_g->dvfs->num_freqs; j++) {
9220                 freq = cpu_clk_g->dvfs->freqs[j] / 1000;
9221                 if (freq > g_start_freq)
9222                         freq_table[i++].frequency = freq;
9223                 if (freq == max_freq)
9224                         break;
9225         }
9226
9227         /* Throttle high index one step below maximum */
9228         BUG_ON(i >= CPU_FREQ_TABLE_MAX_SIZE);
9229         freq_table_data.throttle_highest_index = i - 2;
9230         freq_table_data.freq_table = freq_table;
9231         return &freq_table_data;
9232 }
9233
9234 #else
9235
9236 #define GRANULARITY_KHZ   25500
9237 #define GRANULARITY_END 1020000
9238 #define CPU_THROTTLE_FREQ 408000
9239 #define CPU_SUSPEND_FREQ  408000
9240
9241 struct tegra_cpufreq_table_data *tegra_cpufreq_table_get(void)
9242 {
9243         int i, j;
9244         unsigned int freq, max_freq, cpu_min_freq;
9245         struct clk *cpu_clk_g = tegra_get_clock_by_name("cpu_g");
9246
9247         /* Initialize once */
9248         if (freq_table_data.freq_table)
9249                 return &freq_table_data;
9250
9251         /* Clean table */
9252         for (i = 0; i < CPU_FREQ_TABLE_MAX_SIZE; i++) {
9253                 freq_table[i].index = i;
9254                 freq_table[i].frequency = CPUFREQ_TABLE_END;
9255         }
9256
9257         if (!cpu_clk_g->dvfs) {
9258                 WARN(1, "%s: cannot make cpufreq table: no CPU dvfs\n",
9259                      __func__);
9260                 return NULL;
9261         }
9262
9263         cpu_min_freq = 204000;
9264
9265         cpu_clk_g->min_rate = cpu_min_freq*1000;
9266
9267         i = 0;
9268         freq_table[i++].frequency = cpu_min_freq;
9269         for (j=1; j <= (GRANULARITY_END - cpu_min_freq)/GRANULARITY_KHZ; j++)
9270                 freq_table[i++].frequency = cpu_min_freq + j*GRANULARITY_KHZ;
9271
9272         /* Now, step along the rest of G CPU dvfs ladder */
9273         max_freq = cpu_clk_g->max_rate / 1000;
9274         for (j = 0; j < cpu_clk_g->dvfs->num_freqs; j++) {
9275                 freq = cpu_clk_g->dvfs->freqs[j] / 1000;
9276                 if (freq > GRANULARITY_END)
9277                         freq_table[i++].frequency = freq;
9278                 if (freq == max_freq)
9279                         break;
9280         }
9281
9282         freq_table_data.throttle_lowest_index = 0;
9283         freq_table_data.suspend_index = 0;
9284
9285         for (j = 1; j < i; j++) {
9286                 if ((freq_table[j].frequency > CPU_THROTTLE_FREQ) &&
9287                         (freq_table[j-1].frequency <= CPU_THROTTLE_FREQ))
9288                         freq_table_data.throttle_lowest_index = j - 1;
9289                 if ((freq_table[j].frequency > CPU_SUSPEND_FREQ) &&
9290                         (freq_table[j-1].frequency <= CPU_SUSPEND_FREQ))
9291                         freq_table_data.suspend_index = j - 1;
9292         }
9293
9294         /* Throttle high index one step below maximum */
9295         BUG_ON(i >= CPU_FREQ_TABLE_MAX_SIZE);
9296         freq_table_data.throttle_highest_index = i - 2;
9297         freq_table_data.freq_table = freq_table;
9298         return &freq_table_data;
9299 }
9300
9301 #endif
9302
9303 /* EMC/CPU frequency ratio for power/performance optimization */
9304 unsigned long tegra_emc_to_cpu_ratio(unsigned long cpu_rate)
9305 {
9306         static unsigned long emc_max_rate;
9307
9308         if (emc_max_rate == 0)
9309                 emc_max_rate = clk_round_rate(
9310                         tegra_get_clock_by_name("emc"), ULONG_MAX);
9311
9312         /* Vote on memory bus frequency based on cpu frequency;
9313            cpu rate is in kHz, emc rate is in Hz */
9314         if (cpu_rate >= 1300000)
9315                 return emc_max_rate;    /* cpu >= 1.3GHz, emc max */
9316         else if (cpu_rate >= 975000)
9317                 return 550000000;       /* cpu >= 975 MHz, emc 550 MHz */
9318         else if (cpu_rate >= 725000)
9319                 return  350000000;      /* cpu >= 725 MHz, emc 350 MHz */
9320         else if (cpu_rate >= 500000)
9321                 return  150000000;      /* cpu >= 500 MHz, emc 150 MHz */
9322         else if (cpu_rate >= 275000)
9323                 return  50000000;       /* cpu >= 275 MHz, emc 50 MHz */
9324         else
9325                 return 0;               /* emc min */
9326 }
9327
9328 #ifdef CONFIG_ARCH_TEGRA_13x_SOC
9329 /* EMC/CPU frequency operational requirement limit */
9330 unsigned long tegra_emc_cpu_limit(unsigned long cpu_rate)
9331 {
9332         static unsigned long last_emc_rate;
9333         unsigned long emc_rate;
9334
9335         /* Vote on memory bus frequency based on cpu frequency;
9336            cpu rate is in kHz, emc rate is in Hz */
9337
9338         if ((tegra_revision != TEGRA_REVISION_A01) &&
9339             (tegra_revision != TEGRA_REVISION_A02))
9340                 return 0; /* no frequency dependency for A03+ revisions */
9341
9342         if (cpu_rate > 1020000)
9343                 emc_rate = 600000000;   /* cpu > 1.02GHz, emc 600MHz */
9344         else
9345                 emc_rate = 300000000;   /* 300MHz floor always */
9346
9347         /* When going down, allow some time for CPU DFLL to settle */
9348         if (emc_rate < last_emc_rate)
9349                 udelay(200);            /* FIXME: to be characterized */
9350
9351         last_emc_rate = emc_rate;
9352         return emc_rate;
9353 }
9354 #endif
9355
9356 int tegra_update_mselect_rate(unsigned long cpu_rate)
9357 {
9358         static struct clk *mselect; /* statics init to 0 */
9359
9360         unsigned long mselect_rate;
9361
9362         if (!mselect) {
9363                 mselect = tegra_get_clock_by_name("cpu.mselect");
9364                 if (!mselect)
9365                         return -ENODEV;
9366         }
9367
9368         /* Vote on mselect frequency based on cpu frequency:
9369            keep mselect at half of cpu rate up to 102 MHz;
9370            cpu rate is in kHz, mselect rate is in Hz */
9371         mselect_rate = DIV_ROUND_UP(cpu_rate, 2) * 1000;
9372         mselect_rate = min(mselect_rate, 102000000UL);
9373         return clk_set_rate(mselect, mselect_rate);
9374 }
9375 #endif
9376
9377 #ifdef CONFIG_PM_SLEEP
9378 static u32 clk_rst_suspend[RST_DEVICES_NUM + CLK_OUT_ENB_NUM +
9379                            PERIPH_CLK_SOURCE_NUM + 25];
9380
9381 static int tegra12_clk_suspend(void)
9382 {
9383         unsigned long off;
9384         u32 *ctx = clk_rst_suspend;
9385
9386         *ctx++ = clk_readl(OSC_CTRL) & OSC_CTRL_MASK;
9387         *ctx++ = clk_readl(CPU_SOFTRST_CTRL);
9388         *ctx++ = clk_readl(CPU_SOFTRST_CTRL1);
9389         *ctx++ = clk_readl(CPU_SOFTRST_CTRL2);
9390
9391         *ctx++ = clk_readl(tegra_pll_p_out1.reg);
9392         *ctx++ = clk_readl(tegra_pll_p_out3.reg);
9393         *ctx++ = clk_readl(tegra_pll_p_out5.reg);
9394
9395         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_BASE);
9396         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
9397         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_BASE);
9398         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
9399
9400         *ctx++ = clk_readl(tegra_pll_m_out1.reg);
9401         *ctx++ = clk_readl(tegra_pll_a_out0.reg);
9402         *ctx++ = clk_readl(tegra_pll_c_out1.reg);
9403
9404 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
9405         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg);
9406         *ctx++ = clk_readl(tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
9407 #endif
9408
9409         *ctx++ = clk_readl(tegra_clk_sclk.reg);
9410         *ctx++ = clk_readl(tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
9411         *ctx++ = clk_readl(tegra_clk_pclk.reg);
9412
9413         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_LA;
9414                         off += 4) {
9415                 if (off == PERIPH_CLK_SOURCE_EMC)
9416                         continue;
9417                 *ctx++ = clk_readl(off);
9418         }
9419         for (off = PERIPH_CLK_SOURCE_MSELECT; off <= PERIPH_CLK_SOURCE_SE;
9420                         off+=4) {
9421                 *ctx++ = clk_readl(off);
9422         }
9423         for (off = AUDIO_DLY_CLK; off <= AUDIO_SYNC_CLK_SPDIF; off+=4) {
9424                 *ctx++ = clk_readl(off);
9425         }
9426         for (off = PERIPH_CLK_SOURCE_XUSB_HOST;
9427                 off <= PERIPH_CLK_SOURCE_VIC; off += 4)
9428                 *ctx++ = clk_readl(off);
9429
9430         *ctx++ = clk_readl(RST_DEVICES_L);
9431         *ctx++ = clk_readl(RST_DEVICES_H);
9432         *ctx++ = clk_readl(RST_DEVICES_U);
9433         *ctx++ = clk_readl(RST_DEVICES_V);
9434         *ctx++ = clk_readl(RST_DEVICES_W);
9435         *ctx++ = clk_readl(RST_DEVICES_X);
9436
9437         *ctx++ = clk_readl(CLK_OUT_ENB_L);
9438         *ctx++ = clk_readl(CLK_OUT_ENB_H);
9439         *ctx++ = clk_readl(CLK_OUT_ENB_U);
9440         *ctx++ = clk_readl(CLK_OUT_ENB_V);
9441         *ctx++ = clk_readl(CLK_OUT_ENB_W);
9442         *ctx++ = clk_readl(CLK_OUT_ENB_X);
9443
9444         *ctx++ = clk_readlx(tegra_clk_cclk_g.reg);
9445         *ctx++ = clk_readlx(tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
9446
9447         *ctx++ = clk_readl(SPARE_REG);
9448         *ctx++ = clk_readl(MISC_CLK_ENB);
9449         *ctx++ = clk_readl(CLK_MASK_ARM);
9450
9451         *ctx++ = clk_get_rate_all_locked(&tegra_clk_emc);
9452
9453         pr_debug("%s: suspend entries: %d, suspend array: %u\n", __func__,
9454                 (s32)(ctx - clk_rst_suspend), (u32)ARRAY_SIZE(clk_rst_suspend));
9455         BUG_ON((ctx - clk_rst_suspend) > ARRAY_SIZE(clk_rst_suspend));
9456         return 0;
9457 }
9458
9459 static void tegra12_clk_resume(void)
9460 {
9461         unsigned long off, rate;
9462         const u32 *ctx = clk_rst_suspend;
9463         u32 val;
9464         u32 plla_base;
9465         u32 plld_base;
9466         u32 pll_p_out12, pll_p_out34;
9467         u32 pll_a_out0, pll_m_out1, pll_c_out1;
9468         struct clk *p;
9469
9470         /* FIXME: OSC_CTRL already restored by warm boot code? */
9471         val = clk_readl(OSC_CTRL) & ~OSC_CTRL_MASK;
9472         val |= *ctx++;
9473         clk_writel(val, OSC_CTRL);
9474         clk_writel(*ctx++, CPU_SOFTRST_CTRL);
9475         clk_writel(*ctx++, CPU_SOFTRST_CTRL1);
9476         clk_writel(*ctx++, CPU_SOFTRST_CTRL2);
9477
9478         /* Since we are going to reset devices and switch clock sources in this
9479          * function, plls and secondary dividers is required to be enabled. The
9480          * actual value will be restored back later. Note that boot plls: pllm,
9481          * pllp, and pllu are already configured and enabled
9482          */
9483         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
9484         val |= val << 16;
9485         pll_p_out12 = *ctx++;
9486         clk_writel(pll_p_out12 | val, tegra_pll_p_out1.reg);
9487         pll_p_out34 = *ctx++;
9488         clk_writel(pll_p_out34 | val, tegra_pll_p_out3.reg);
9489
9490         /* Restore as is, GPU is rail-gated, anyway */
9491         clk_writel(*ctx++, tegra_pll_p_out5.reg);
9492
9493         tegra12_pllss_clk_resume_enable(&tegra_pll_c4);
9494         tegra12_pllss_clk_resume_enable(&tegra_pll_d2);
9495         tegra12_pllss_clk_resume_enable(&tegra_pll_dp);
9496         tegra12_pllcx_clk_resume_enable(&tegra_pll_c2);
9497         tegra12_pllcx_clk_resume_enable(&tegra_pll_c3);
9498         tegra12_pllxc_clk_resume_enable(&tegra_pll_c);
9499         tegra12_pllxc_clk_resume_enable(&tegra_pll_x);
9500         tegra12_pllre_clk_resume_enable(&tegra_pll_re_out);
9501
9502         plla_base = *ctx++;
9503         clk_writel(*ctx++, tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
9504         clk_writel(plla_base | PLL_BASE_ENABLE, tegra_pll_a.reg + PLL_BASE);
9505
9506         plld_base = *ctx++;
9507         clk_writel(*ctx++, tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
9508         clk_writel(plld_base | PLL_BASE_ENABLE, tegra_pll_d.reg + PLL_BASE);
9509
9510         udelay(1000);
9511
9512         val = PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
9513         pll_m_out1 = *ctx++;
9514         clk_writel(pll_m_out1 | val, tegra_pll_m_out1.reg);
9515         pll_a_out0 = *ctx++;
9516         clk_writel(pll_a_out0 | val, tegra_pll_a_out0.reg);
9517         pll_c_out1 = *ctx++;
9518         clk_writel(pll_c_out1 | val, tegra_pll_c_out1.reg);
9519
9520 #ifndef CONFIG_ARCH_TEGRA_13x_SOC
9521         val = *ctx++;
9522         tegra12_super_clk_resume(&tegra_clk_cclk_lp,
9523                 tegra_clk_virtual_cpu_lp.u.cpu.backup, val);
9524         clk_writel(*ctx++, tegra_clk_cclk_lp.reg + SUPER_CLK_DIVIDER);
9525 #endif
9526
9527         clk_writel(*ctx++, tegra_clk_sclk.reg);
9528         clk_writel(*ctx++, tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
9529         clk_writel(*ctx++, tegra_clk_pclk.reg);
9530
9531         /* enable all clocks before configuring clock sources */
9532         clk_writel(CLK_OUT_ENB_L_RESET_MASK, CLK_OUT_ENB_L);
9533         clk_writel(CLK_OUT_ENB_H_RESET_MASK, CLK_OUT_ENB_H);
9534         clk_writel(CLK_OUT_ENB_U_RESET_MASK, CLK_OUT_ENB_U);
9535         clk_writel(CLK_OUT_ENB_V_RESET_MASK, CLK_OUT_ENB_V);
9536         clk_writel(CLK_OUT_ENB_W_RESET_MASK, CLK_OUT_ENB_W);
9537         clk_writel(CLK_OUT_ENB_X_RESET_MASK, CLK_OUT_ENB_X);
9538         wmb();
9539
9540         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_LA;
9541                         off += 4) {
9542                 if (off == PERIPH_CLK_SOURCE_EMC)
9543                         continue;
9544                 clk_writel(*ctx++, off);
9545         }
9546         for (off = PERIPH_CLK_SOURCE_MSELECT; off <= PERIPH_CLK_SOURCE_SE;
9547                         off += 4) {
9548                 clk_writel(*ctx++, off);
9549         }
9550         for (off = AUDIO_DLY_CLK; off <= AUDIO_SYNC_CLK_SPDIF; off+=4) {
9551                 clk_writel(*ctx++, off);
9552         }
9553         for (off = PERIPH_CLK_SOURCE_XUSB_HOST;
9554                 off <= PERIPH_CLK_SOURCE_VIC; off += 4)
9555                 clk_writel(*ctx++, off);
9556
9557         udelay(RESET_PROPAGATION_DELAY);
9558
9559         clk_writel(*ctx++, RST_DEVICES_L);
9560         clk_writel(*ctx++, RST_DEVICES_H);
9561         clk_writel(*ctx++, RST_DEVICES_U);
9562         clk_writel(*ctx++, RST_DEVICES_V);
9563         clk_writel(*ctx++, RST_DEVICES_W);
9564         clk_writel(*ctx++, RST_DEVICES_X);
9565         wmb();
9566
9567         clk_writel(*ctx++, CLK_OUT_ENB_L);
9568         clk_writel(*ctx++, CLK_OUT_ENB_H);
9569         clk_writel(*ctx++, CLK_OUT_ENB_U);
9570
9571         /* For LP0 resume, clk to lpcpu is required to be on */
9572         val = *ctx++;
9573         val |= CLK_OUT_ENB_V_CLK_ENB_CPULP_EN;
9574         clk_writel(val, CLK_OUT_ENB_V);
9575
9576         clk_writel(*ctx++, CLK_OUT_ENB_W);
9577         clk_writel(*ctx++, CLK_OUT_ENB_X);
9578         wmb();
9579
9580         /* DFLL resume after cl_dvfs and i2c5 clocks are resumed */
9581         tegra12_dfll_clk_resume(&tegra_dfll_cpu);
9582
9583         /* CPU G clock restored after DFLL and PLLs */
9584         clk_writelx(*ctx++, tegra_clk_cclk_g.reg);
9585         clk_writelx(*ctx++, tegra_clk_cclk_g.reg + SUPER_CLK_DIVIDER);
9586
9587         clk_writel(*ctx++, SPARE_REG);
9588         clk_writel(*ctx++, MISC_CLK_ENB);
9589         clk_writel(*ctx++, CLK_MASK_ARM);
9590
9591         /* Restore back the actual pll and secondary divider values */
9592         clk_writel(pll_p_out12, tegra_pll_p_out1.reg);
9593         clk_writel(pll_p_out34, tegra_pll_p_out3.reg);
9594
9595         p = &tegra_pll_c4;
9596         if (p->state == OFF)
9597                 tegra12_pllss_clk_disable(p);
9598         p = &tegra_pll_d2;
9599         if (p->state == OFF)
9600                 tegra12_pllss_clk_disable(p);
9601         p = &tegra_pll_dp;
9602         if (p->state == OFF)
9603                 tegra12_pllss_clk_disable(p);
9604         p = &tegra_pll_c2;
9605         if (p->state == OFF)
9606                 tegra12_pllcx_clk_disable(p);
9607         p = &tegra_pll_c3;
9608         if (p->state == OFF)
9609                 tegra12_pllcx_clk_disable(p);
9610         p = &tegra_pll_c;
9611         if (p->state == OFF)
9612                 tegra12_pllxc_clk_disable(p);
9613         p = &tegra_pll_x;
9614         if (p->state == OFF)
9615                 tegra12_pllxc_clk_disable(p);
9616         p = &tegra_pll_re_vco;
9617         if (p->state == OFF)
9618                 tegra12_pllre_clk_disable(p);
9619
9620         clk_writel(plla_base, tegra_pll_a.reg + PLL_BASE);
9621         clk_writel(plld_base, tegra_pll_d.reg + PLL_BASE);
9622
9623         clk_writel(pll_m_out1, tegra_pll_m_out1.reg);
9624         clk_writel(pll_a_out0, tegra_pll_a_out0.reg);
9625         clk_writel(pll_c_out1, tegra_pll_c_out1.reg);
9626
9627         /* Since EMC clock is not restored, and may not preserve parent across
9628            suspend, update current state, and mark EMC DFS as out of sync */
9629         p = tegra_clk_emc.parent;
9630         tegra12_periph_clk_init(&tegra_clk_emc);
9631
9632         /* Turn Off pll_m if it was OFF before suspend, and emc was not switched
9633            to pll_m across suspend; re-init pll_m to sync s/w and h/w states */
9634         if ((tegra_pll_m.state == OFF) &&
9635             (&tegra_pll_m != tegra_clk_emc.parent))
9636                 tegra12_pllm_clk_disable(&tegra_pll_m);
9637         tegra12_pllm_clk_init(&tegra_pll_m);
9638
9639         if (p != tegra_clk_emc.parent) {
9640                 pr_debug("EMC parent(refcount) across suspend: %s(%d) : %s(%d)",
9641                         p->name, p->refcnt, tegra_clk_emc.parent->name,
9642                         tegra_clk_emc.parent->refcnt);
9643
9644                 /* emc switched to the new parent by low level code, but ref
9645                    count and s/w state need to be updated */
9646                 clk_disable_locked(p);
9647                 clk_enable_locked(tegra_clk_emc.parent);
9648         }
9649
9650         rate = clk_get_rate_all_locked(&tegra_clk_emc);
9651         if (*ctx != rate) {
9652                 tegra_dvfs_set_rate(&tegra_clk_emc, rate);
9653                 if (p == tegra_clk_emc.parent) {
9654                         rate = clk_get_rate_all_locked(p);
9655                         tegra_dvfs_set_rate(p, rate);
9656                 }
9657         }
9658         tegra_emc_timing_invalidate();
9659
9660         tegra12_pll_clk_init(&tegra_pll_u); /* Re-init utmi parameters */
9661         tegra12_plle_clk_resume(&tegra_pll_e); /* Restore plle parent as pll_re_vco */
9662         tegra12_pllp_clk_resume(&tegra_pll_p); /* Fire a bug if not restored */
9663         tegra12_mc_holdoff_enable();
9664 }
9665
9666 static struct syscore_ops tegra_clk_syscore_ops = {
9667         .suspend = tegra12_clk_suspend,
9668         .resume = tegra12_clk_resume,
9669         .save = tegra12_clk_suspend,
9670         .restore = tegra12_clk_resume,
9671 };
9672 #endif
9673
9674 /* Tegra12 CPU clock and reset control functions */
9675 static void tegra12_wait_cpu_in_reset(u32 cpu)
9676 {
9677         unsigned int reg;
9678
9679         do {
9680                 reg = readl(reg_clk_base +
9681                             TEGRA30_CLK_RST_CONTROLLER_CPU_CMPLX_STATUS);
9682                 cpu_relax();
9683         } while (!(reg & (1 << cpu)));  /* check CPU been reset or not */
9684
9685         return;
9686 }
9687
9688 static void tegra12_put_cpu_in_reset(u32 cpu)
9689 {
9690         writel(CPU_RESET(cpu),
9691                reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
9692         dmb();
9693 }
9694
9695 static void tegra12_cpu_out_of_reset(u32 cpu)
9696 {
9697         writel(CPU_RESET(cpu),
9698                reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
9699         wmb();
9700 }
9701
9702 static void tegra12_enable_cpu_clock(u32 cpu)
9703 {
9704         unsigned int reg;
9705
9706         writel(CPU_CLOCK(cpu),
9707                reg_clk_base + TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
9708         reg = readl(reg_clk_base +
9709                     TEGRA30_CLK_RST_CONTROLLER_CLK_CPU_CMPLX_CLR);
9710 }
9711 static void tegra12_disable_cpu_clock(u32 cpu)
9712 {
9713 }
9714
9715 static struct tegra_cpu_car_ops tegra12_cpu_car_ops = {
9716         .wait_for_reset = tegra12_wait_cpu_in_reset,
9717         .put_in_reset   = tegra12_put_cpu_in_reset,
9718         .out_of_reset   = tegra12_cpu_out_of_reset,
9719         .enable_clock   = tegra12_enable_cpu_clock,
9720         .disable_clock  = tegra12_disable_cpu_clock,
9721 };
9722
9723 void __init tegra12_cpu_car_ops_init(void)
9724 {
9725         tegra_cpu_car_ops = &tegra12_cpu_car_ops;
9726 }
9727
9728 static void tegra12_init_xusb_clocks(void)
9729 {
9730         int i;
9731
9732         for (i = 0; i < ARRAY_SIZE(tegra_xusb_source_clks); i++)
9733                 tegra12_init_one_clock(&tegra_xusb_source_clks[i]);
9734
9735         tegra12_init_one_clock(&tegra_xusb_ss_div2);
9736         tegra12_init_one_clock(&tegra_xusb_hs_src);
9737
9738         for (i = 0; i < ARRAY_SIZE(tegra_xusb_coupled_clks); i++)
9739                 tegra12_init_one_clock(&tegra_xusb_coupled_clks[i]);
9740 }
9741
9742 #ifdef CONFIG_TEGRA_PREINIT_CLOCKS
9743
9744 #define CLK_RSTENB_DEV_V_0_AUDIO_BIT    (1 << 10)
9745 #define CLK_RSTENB_DEV_V_0_3D2_BIT      (1 << 2)
9746
9747 #define CLK_RSTENB_DEV_L_0_HOST1X_BIT   (1 << 28)
9748 #define CLK_RSTENB_DEV_L_0_DISP1_BIT    (1 << 27)
9749 #define CLK_RSTENB_DEV_L_0_3D_BIT       (1 << 24)
9750 #define CLK_RSTENB_DEV_L_0_ISP_BIT      (1 << 23)
9751 #define CLK_RSTENB_DEV_L_0_2D_BIT       (1 << 21)
9752 #define CLK_RSTENB_DEV_L_0_VI_BIT       (1 << 20)
9753 #define CLK_RSTENB_DEV_L_0_EPP_BIT      (1 << 19)
9754
9755 #define CLK_RSTENB_DEV_H_0_VDE_BIT      (1 << 29)
9756 #define CLK_RSTENB_DEV_H_0_MPE_BIT      (1 << 28)
9757
9758 #define CLK_RSTENB_DEV_U_0_CSITE_BIT    (1 << 9)
9759
9760 #define CLK_RSTENB_DEV_X_0_HDMI_AUDIO_BIT       (1 << 16)
9761
9762 #define HOST1X_CLK_REG_OFFSET           0x180
9763 #define HOST1X_CLK_SRC_SHIFT            30
9764 #define HOST1X_CLK_SRC_MASK             (0x3 << HOST1X_CLK_SRC_SHIFT)
9765 #define HOST1X_CLK_SRC_PLLM_OUT0        0
9766 #define HOST1X_CLK_SRC_PLLC_OUT0        1
9767 #define HOST1X_CLK_SRC_PLLP_OUT0        2
9768 #define HOST1X_CLK_SRC_PLLA_OUT0        3
9769 #define HOST1X_CLK_SRC_DEFAULT (\
9770                 HOST1X_CLK_SRC_PLLP_OUT0 << HOST1X_CLK_SRC_SHIFT)
9771 #define HOST1X_CLK_IDLE_DIV_SHIFT       8
9772 #define HOST1X_CLK_IDLE_DIV_MASK        (0xff << HOST1X_CLK_IDLE_DIV_SHIFT)
9773 #define HOST1X_CLK_IDLE_DIV_DEFAULT     (0 << HOST1X_CLK_IDLE_DIV_SHIFT)
9774 #define HOST1X_CLK_DIV_SHIFT            0
9775 #define HOST1X_CLK_DIV_MASK             (0xff << HOST1X_CLK_DIV_SHIFT)
9776 #define HOST1X_CLK_DIV_DEFAULT          (3 << HOST1X_CLK_DIV_SHIFT)
9777
9778 #define VCLK_SRC_SHIFT                  30
9779 #define VCLK_SRC_MASK                   (0x3 << VCLK_SRC_SHIFT)
9780 #define VCLK_SRC_PLLM_OUT0              0
9781 #define VCLK_SRC_PLLC_OUT0              1
9782 #define VCLK_SRC_PLLP_OUT0              2
9783 #define VCLK_SRC_PLLA_OUT0              3
9784 #define VCLK_SRC_DEFAULT                (VCLK_SRC_PLLM_OUT0 << VCLK_SRC_SHIFT)
9785 #define VCLK_IDLE_DIV_SHIFT             8
9786 #define VCLK_IDLE_DIV_MASK              (0xff << VCLK_IDLE_DIV_SHIFT)
9787 #define VCLK_IDLE_DIV_DEFAULT           (0 << VCLK_IDLE_DIV_SHIFT)
9788 #define VCLK_DIV_SHIFT                  0
9789 #define VCLK_DIV_MASK                   (0xff << VCLK_DIV_SHIFT)
9790 #define VCLK_DIV_DEFAULT                (0xa << VCLK_DIV_SHIFT)
9791
9792 #define ISP_CLK_REG_OFFSET              0x144
9793 #define VI_CLK_REG_OFFSET               0x148
9794 #define  VI_SENSOR_CLK_REG_OFFSET       0x1a8
9795 #define  VI_SENSOR2_CLK_REG_OFFSET      0x658
9796 #define  VI_CLK_DIV_DEFAULT             (0x12 << VCLK_DIV_SHIFT)
9797 #define G3D_CLK_REG_OFFSET              0x158
9798 #define G2D_CLK_REG_OFFSET              0x15c
9799 #define EPP_CLK_REG_OFFSET              0x16c
9800 #define MPE_CLK_REG_OFFSET              0x170
9801 #define VDE_CLK_REG_OFFSET              0x170
9802 #define G3D2_CLK_REG_OFFSET             0x3b0
9803 #define HDMI_AUDIO_CLK_REG_OFFSET       0x668
9804 #define  HDMI_AUDIO_CLK_DIV_DEFAULT     (0x12 << VCLK_DIV_SHIFT)
9805 #define CSITE_CLK_REG_OFFSET            0x1d4
9806 #define  CSITE_CLK_DIV_DEFAULT          (0x4 << VCLK_DIV_SHIFT)
9807
9808 static void __init clk_setbit(u32 reg, u32 bit)
9809 {
9810         u32 val = clk_readl(reg);
9811
9812         if ((val & bit) == bit)
9813                 return;
9814         val |= bit;
9815         clk_writel(val, reg);
9816         udelay(2);
9817 }
9818
9819 static void __init clk_clrbit(u32 reg, u32 bit)
9820 {
9821         u32 val = clk_readl(reg);
9822
9823         if ((val & bit) == 0)
9824                 return;
9825         val &= ~bit;
9826         clk_writel(val, reg);
9827         udelay(2);
9828 }
9829
9830 static void __init clk_setbits(u32 reg, u32 bits, u32 mask)
9831 {
9832         u32 val = clk_readl(reg);
9833
9834         if ((val & mask) == bits)
9835                 return;
9836         val &= ~mask;
9837         val |= bits;
9838         clk_writel(val, reg);
9839         udelay(2);
9840 }
9841
9842 static void __init vclk_init(int tag, u32 src, u32 rebit)
9843 {
9844         u32 rst, enb;
9845
9846         switch (tag) {
9847         case 'L':
9848                 rst = RST_DEVICES_L;
9849                 enb = CLK_OUT_ENB_L;
9850                 break;
9851         case 'H':
9852                 rst = RST_DEVICES_H;
9853                 enb = CLK_OUT_ENB_H;
9854                 break;
9855         case 'U':
9856                 rst = RST_DEVICES_U;
9857                 enb = CLK_OUT_ENB_U;
9858                 break;
9859         case 'V':
9860                 rst = RST_DEVICES_V;
9861                 enb = CLK_OUT_ENB_V;
9862                 break;
9863         case 'W':
9864                 rst = RST_DEVICES_W;
9865                 enb = CLK_OUT_ENB_W;
9866                 break;
9867         case 'X':
9868                 rst = RST_DEVICES_X;
9869                 enb = CLK_OUT_ENB_X;
9870                 break;
9871         default:
9872                 /* Quietly ignore. */
9873                 return;
9874         }
9875
9876         clk_setbit(rst, rebit);
9877         clk_clrbit(enb, rebit);
9878
9879         clk_setbits(src, VCLK_SRC_DEFAULT, VCLK_SRC_MASK);
9880         clk_setbits(src, VCLK_DIV_DEFAULT, VCLK_DIV_MASK);
9881
9882         clk_clrbit(rst, rebit);
9883 }
9884
9885 static int __init tegra_soc_preinit_clocks(void)
9886 {
9887         /*
9888          * Make sure host1x clock configuration has:
9889          *      HOST1X_CLK_SRC    : PLLP_OUT0.
9890          *      HOST1X_CLK_DIVISOR: >2 to start from safe enough frequency.
9891          */
9892         clk_setbit(RST_DEVICES_L, CLK_RSTENB_DEV_L_0_HOST1X_BIT);
9893         clk_setbit(CLK_OUT_ENB_L, CLK_RSTENB_DEV_L_0_HOST1X_BIT);
9894         clk_setbits(HOST1X_CLK_REG_OFFSET,
9895                     HOST1X_CLK_DIV_DEFAULT, HOST1X_CLK_DIV_MASK);
9896         clk_setbits(HOST1X_CLK_REG_OFFSET,
9897                     HOST1X_CLK_IDLE_DIV_DEFAULT, HOST1X_CLK_IDLE_DIV_MASK);
9898         clk_setbits(HOST1X_CLK_REG_OFFSET,
9899                     HOST1X_CLK_SRC_DEFAULT, HOST1X_CLK_SRC_MASK);
9900         clk_clrbit(RST_DEVICES_L, CLK_RSTENB_DEV_L_0_HOST1X_BIT);
9901
9902         /*
9903          *  Make sure vi clock configuration has:
9904          *      VI_CLK_DIVISOR: 0x12
9905          *      VI_SENSOR_CLK_DIVISOR:  0x12
9906          *      VI_SENSOR2_CLK_DIVISOR: 0x12
9907          */
9908         clk_setbit(RST_DEVICES_L, CLK_RSTENB_DEV_L_0_VI_BIT);
9909         clk_setbit(CLK_OUT_ENB_L, CLK_RSTENB_DEV_L_0_VI_BIT);
9910         clk_setbits(VI_CLK_REG_OFFSET,
9911                     VCLK_SRC_DEFAULT, VCLK_SRC_MASK);
9912         clk_setbits(VI_CLK_REG_OFFSET, VI_CLK_DIV_DEFAULT, VCLK_DIV_MASK);
9913         clk_setbits(VI_SENSOR_CLK_REG_OFFSET, VCLK_SRC_DEFAULT, VCLK_SRC_MASK);
9914         clk_setbits(VI_SENSOR_CLK_REG_OFFSET,
9915                     VI_CLK_DIV_DEFAULT, VCLK_DIV_MASK);
9916         clk_setbits(VI_SENSOR2_CLK_REG_OFFSET, VCLK_SRC_DEFAULT, VCLK_SRC_MASK);
9917         clk_setbits(VI_SENSOR2_CLK_REG_OFFSET,
9918                     VI_CLK_DIV_DEFAULT, VCLK_DIV_MASK);
9919         clk_clrbit(RST_DEVICES_L, CLK_RSTENB_DEV_L_0_VI_BIT);
9920
9921         /*
9922          *  Make sure hdmi_audio clock configuration has:
9923          *      HDMI_AUDIO_CLK_DIVISOR: 0x12
9924          */
9925         clk_setbit(RST_DEVICES_X, CLK_RSTENB_DEV_X_0_HDMI_AUDIO_BIT);
9926         clk_setbit(CLK_OUT_ENB_X, CLK_RSTENB_DEV_X_0_HDMI_AUDIO_BIT);
9927         clk_setbits(HDMI_AUDIO_CLK_REG_OFFSET,
9928                     HDMI_AUDIO_CLK_DIV_DEFAULT, VCLK_DIV_MASK);
9929         clk_clrbit(RST_DEVICES_X, CLK_RSTENB_DEV_X_0_HDMI_AUDIO_BIT);
9930
9931         /*
9932          *  Make sure csite clock configuration has:
9933          *      CSITE_CLK_DIVISOR:      0x4
9934          */
9935         clk_setbit(RST_DEVICES_U, CLK_RSTENB_DEV_U_0_CSITE_BIT);
9936         clk_setbit(CLK_OUT_ENB_U, CLK_RSTENB_DEV_U_0_CSITE_BIT);
9937         clk_setbits(CSITE_CLK_REG_OFFSET, CSITE_CLK_DIV_DEFAULT, VCLK_DIV_MASK);
9938         clk_clrbit(RST_DEVICES_U, CLK_RSTENB_DEV_U_0_CSITE_BIT);
9939
9940         /* Pre-initialize Video clocks. */
9941         vclk_init('L', G3D_CLK_REG_OFFSET, CLK_RSTENB_DEV_L_0_3D_BIT);
9942         vclk_init('L', G2D_CLK_REG_OFFSET, CLK_RSTENB_DEV_L_0_2D_BIT);
9943         vclk_init('L', ISP_CLK_REG_OFFSET, CLK_RSTENB_DEV_L_0_ISP_BIT);
9944         vclk_init('L', EPP_CLK_REG_OFFSET, CLK_RSTENB_DEV_L_0_EPP_BIT);
9945         vclk_init('H', VDE_CLK_REG_OFFSET, CLK_RSTENB_DEV_H_0_VDE_BIT);
9946         vclk_init('H', MPE_CLK_REG_OFFSET, CLK_RSTENB_DEV_H_0_MPE_BIT);
9947         vclk_init('V', G3D2_CLK_REG_OFFSET, CLK_RSTENB_DEV_V_0_3D2_BIT);
9948
9949         return 0;
9950 }
9951 #endif /* CONFIG_TEGRA_PREINIT_CLOCKS */
9952
9953 void __init tegra12x_init_clocks(void)
9954 {
9955         int i;
9956         struct clk *c;
9957
9958
9959 #ifdef CONFIG_TEGRA_PREINIT_CLOCKS
9960         tegra_soc_preinit_clocks();
9961 #endif /* CONFIG_TEGRA_PREINIT_CLOCKS */
9962
9963         for (i = 0; i < ARRAY_SIZE(tegra_ptr_clks); i++)
9964                 tegra12_init_one_clock(tegra_ptr_clks[i]);
9965
9966         /* Fix bug in simulator clock routing */
9967         if (tegra_platform_is_linsim()) {
9968                 for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++) {
9969                         if (!strcmp("msenc", tegra_list_clks[i].name)) {
9970                                 tegra_list_clks[i].u.periph.clk_num = 60;
9971                                 tegra_list_clks[i].reg = 0x170;
9972                                 tegra_list_clks[i].flags &= ~MUX8;
9973                         }
9974                 }
9975         }
9976
9977         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
9978                 tegra12_init_one_clock(&tegra_list_clks[i]);
9979
9980         for (i = 0; i < ARRAY_SIZE(tegra_visp_clks); i++)
9981                 tegra12_init_one_clock(&tegra_visp_clks[i]);
9982
9983         for (i = 0; i < ARRAY_SIZE(tegra_ptr_camera_mclks); i++)
9984                 tegra12_init_one_clock(tegra_ptr_camera_mclks[i]);
9985
9986         for (i = 0; i < ARRAY_SIZE(tegra_sync_source_list); i++)
9987                 tegra12_init_one_clock(&tegra_sync_source_list[i]);
9988         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_list); i++)
9989                 tegra12_init_one_clock(&tegra_clk_audio_list[i]);
9990         for (i = 0; i < ARRAY_SIZE(tegra_clk_audio_2x_list); i++)
9991                 tegra12_init_one_clock(&tegra_clk_audio_2x_list[i]);
9992
9993         init_clk_out_mux();
9994         for (i = 0; i < ARRAY_SIZE(tegra_clk_out_list); i++)
9995                 tegra12_init_one_clock(&tegra_clk_out_list[i]);
9996
9997         tegra12_init_xusb_clocks();
9998
9999         for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
10000                 c = tegra_get_clock_by_name(tegra_clk_duplicates[i].name);
10001                 if (!c) {
10002                         pr_err("%s: Unknown duplicate clock %s\n", __func__,
10003                                 tegra_clk_duplicates[i].name);
10004                         continue;
10005                 }
10006
10007                 tegra_clk_duplicates[i].lookup.clk = c;
10008                 clkdev_add(&tegra_clk_duplicates[i].lookup);
10009         }
10010
10011         /* Initialize to default */
10012         tegra_init_cpu_edp_limits(0);
10013
10014         tegra12_cpu_car_ops_init();
10015
10016         /* Tegra12 allows to change dividers of disabled clocks */
10017         tegra_clk_set_disabled_div_all();
10018
10019 #ifdef CONFIG_PM_SLEEP
10020         register_syscore_ops(&tegra_clk_syscore_ops);
10021 #endif
10022
10023 }
10024
10025 static int __init tegra12x_clk_late_init(void)
10026 {
10027         clk_disable(&tegra_pll_d);
10028         clk_disable(&tegra_pll_re_vco);
10029         return 0;
10030 }
10031 late_initcall(tegra12x_clk_late_init);