Kukjin Kim | bf46aae | 2012-04-15 21:13:29 -0700 | [diff] [blame] | 1 | /* |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 2 | * Copyright 2009 Samsung Electronics Co., Ltd. |
| 3 | * http://www.samsung.com/ |
| 4 | * |
| 5 | * S5P - Common clock support |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/clk.h> |
Kay Sievers | edbaa60 | 2011-12-21 16:26:03 -0800 | [diff] [blame] | 19 | #include <linux/device.h> |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 20 | #include <linux/io.h> |
| 21 | #include <asm/div64.h> |
| 22 | |
Seungwhan Youn | d4b34c6 | 2010-10-14 10:39:08 +0900 | [diff] [blame] | 23 | #include <mach/regs-clock.h> |
| 24 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 25 | #include <plat/clock.h> |
| 26 | #include <plat/clock-clksrc.h> |
| 27 | #include <plat/s5p-clock.h> |
| 28 | |
| 29 | /* fin_apll, fin_mpll and fin_epll are all the same clock, which we call |
| 30 | * clk_ext_xtal_mux. |
| 31 | */ |
| 32 | struct clk clk_ext_xtal_mux = { |
| 33 | .name = "ext_xtal", |
| 34 | .id = -1, |
| 35 | }; |
| 36 | |
Thomas Abraham | f001d5b | 2010-04-19 20:05:08 +0900 | [diff] [blame] | 37 | struct clk clk_xusbxti = { |
| 38 | .name = "xusbxti", |
| 39 | .id = -1, |
| 40 | }; |
| 41 | |
Thomas Abraham | a443a63 | 2010-05-14 16:27:28 +0900 | [diff] [blame] | 42 | struct clk s5p_clk_27m = { |
Kukjin Kim | 0c1945d | 2010-02-24 16:40:36 +0900 | [diff] [blame] | 43 | .name = "clk_27m", |
| 44 | .id = -1, |
| 45 | .rate = 27000000, |
| 46 | }; |
| 47 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 48 | /* 48MHz USB Phy clock output */ |
| 49 | struct clk clk_48m = { |
| 50 | .name = "clk_48m", |
| 51 | .id = -1, |
| 52 | .rate = 48000000, |
| 53 | }; |
| 54 | |
| 55 | /* APLL clock output |
| 56 | * No need .ctrlbit, this is always on |
| 57 | */ |
| 58 | struct clk clk_fout_apll = { |
| 59 | .name = "fout_apll", |
| 60 | .id = -1, |
| 61 | }; |
| 62 | |
Kukjin Kim | 87b3c6e | 2012-01-22 21:46:13 +0900 | [diff] [blame] | 63 | /* BPLL clock output */ |
| 64 | |
| 65 | struct clk clk_fout_bpll = { |
| 66 | .name = "fout_bpll", |
| 67 | .id = -1, |
| 68 | }; |
| 69 | |
Kisoo Yu | 57b317f | 2012-04-24 14:54:15 -0700 | [diff] [blame^] | 70 | struct clk clk_fout_bpll_div2 = { |
| 71 | .name = "fout_bpll_div2", |
| 72 | .id = -1, |
| 73 | }; |
| 74 | |
Kukjin Kim | 87b3c6e | 2012-01-22 21:46:13 +0900 | [diff] [blame] | 75 | /* CPLL clock output */ |
| 76 | |
| 77 | struct clk clk_fout_cpll = { |
| 78 | .name = "fout_cpll", |
| 79 | .id = -1, |
| 80 | }; |
| 81 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 82 | /* MPLL clock output |
| 83 | * No need .ctrlbit, this is always on |
| 84 | */ |
| 85 | struct clk clk_fout_mpll = { |
| 86 | .name = "fout_mpll", |
| 87 | .id = -1, |
| 88 | }; |
| 89 | |
Kisoo Yu | 57b317f | 2012-04-24 14:54:15 -0700 | [diff] [blame^] | 90 | struct clk clk_fout_mpll_div2 = { |
| 91 | .name = "fout_mpll_div2", |
| 92 | .id = -1, |
| 93 | }; |
| 94 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 95 | /* EPLL clock output */ |
| 96 | struct clk clk_fout_epll = { |
| 97 | .name = "fout_epll", |
| 98 | .id = -1, |
| 99 | .ctrlbit = (1 << 31), |
| 100 | }; |
| 101 | |
Kukjin Kim | 3109e55 | 2010-09-01 15:35:30 +0900 | [diff] [blame] | 102 | /* DPLL clock output */ |
| 103 | struct clk clk_fout_dpll = { |
| 104 | .name = "fout_dpll", |
| 105 | .id = -1, |
| 106 | .ctrlbit = (1 << 31), |
| 107 | }; |
| 108 | |
Thomas Abraham | f445dbd | 2010-05-17 09:38:52 +0900 | [diff] [blame] | 109 | /* VPLL clock output */ |
| 110 | struct clk clk_fout_vpll = { |
| 111 | .name = "fout_vpll", |
| 112 | .id = -1, |
| 113 | .ctrlbit = (1 << 31), |
| 114 | }; |
| 115 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 116 | /* Possible clock sources for APLL Mux */ |
| 117 | static struct clk *clk_src_apll_list[] = { |
| 118 | [0] = &clk_fin_apll, |
| 119 | [1] = &clk_fout_apll, |
| 120 | }; |
| 121 | |
| 122 | struct clksrc_sources clk_src_apll = { |
| 123 | .sources = clk_src_apll_list, |
| 124 | .nr_sources = ARRAY_SIZE(clk_src_apll_list), |
| 125 | }; |
| 126 | |
Kukjin Kim | 87b3c6e | 2012-01-22 21:46:13 +0900 | [diff] [blame] | 127 | /* Possible clock sources for BPLL Mux */ |
| 128 | static struct clk *clk_src_bpll_list[] = { |
| 129 | [0] = &clk_fin_bpll, |
| 130 | [1] = &clk_fout_bpll, |
| 131 | }; |
| 132 | |
| 133 | struct clksrc_sources clk_src_bpll = { |
| 134 | .sources = clk_src_bpll_list, |
| 135 | .nr_sources = ARRAY_SIZE(clk_src_bpll_list), |
| 136 | }; |
| 137 | |
Kisoo Yu | 57b317f | 2012-04-24 14:54:15 -0700 | [diff] [blame^] | 138 | static struct clk *clk_src_bpll_fout_list[] = { |
| 139 | [0] = &clk_fout_bpll_div2, |
| 140 | [1] = &clk_fout_bpll, |
| 141 | }; |
| 142 | |
| 143 | struct clksrc_sources clk_src_bpll_fout = { |
| 144 | .sources = clk_src_bpll_fout_list, |
| 145 | .nr_sources = ARRAY_SIZE(clk_src_bpll_fout_list), |
| 146 | }; |
| 147 | |
Kukjin Kim | 87b3c6e | 2012-01-22 21:46:13 +0900 | [diff] [blame] | 148 | /* Possible clock sources for CPLL Mux */ |
| 149 | static struct clk *clk_src_cpll_list[] = { |
| 150 | [0] = &clk_fin_cpll, |
| 151 | [1] = &clk_fout_cpll, |
| 152 | }; |
| 153 | |
| 154 | struct clksrc_sources clk_src_cpll = { |
| 155 | .sources = clk_src_cpll_list, |
| 156 | .nr_sources = ARRAY_SIZE(clk_src_cpll_list), |
| 157 | }; |
| 158 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 159 | /* Possible clock sources for MPLL Mux */ |
| 160 | static struct clk *clk_src_mpll_list[] = { |
| 161 | [0] = &clk_fin_mpll, |
| 162 | [1] = &clk_fout_mpll, |
| 163 | }; |
| 164 | |
| 165 | struct clksrc_sources clk_src_mpll = { |
| 166 | .sources = clk_src_mpll_list, |
| 167 | .nr_sources = ARRAY_SIZE(clk_src_mpll_list), |
| 168 | }; |
| 169 | |
Kisoo Yu | 57b317f | 2012-04-24 14:54:15 -0700 | [diff] [blame^] | 170 | static struct clk *clk_src_mpll_fout_list[] = { |
| 171 | [0] = &clk_fout_mpll_div2, |
| 172 | [1] = &clk_fout_mpll, |
| 173 | }; |
| 174 | |
| 175 | struct clksrc_sources clk_src_mpll_fout = { |
| 176 | .sources = clk_src_mpll_fout_list, |
| 177 | .nr_sources = ARRAY_SIZE(clk_src_mpll_fout_list), |
| 178 | }; |
| 179 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 180 | /* Possible clock sources for EPLL Mux */ |
| 181 | static struct clk *clk_src_epll_list[] = { |
| 182 | [0] = &clk_fin_epll, |
| 183 | [1] = &clk_fout_epll, |
| 184 | }; |
| 185 | |
| 186 | struct clksrc_sources clk_src_epll = { |
| 187 | .sources = clk_src_epll_list, |
| 188 | .nr_sources = ARRAY_SIZE(clk_src_epll_list), |
| 189 | }; |
| 190 | |
Kukjin Kim | 3109e55 | 2010-09-01 15:35:30 +0900 | [diff] [blame] | 191 | /* Possible clock sources for DPLL Mux */ |
| 192 | static struct clk *clk_src_dpll_list[] = { |
| 193 | [0] = &clk_fin_dpll, |
| 194 | [1] = &clk_fout_dpll, |
| 195 | }; |
| 196 | |
| 197 | struct clksrc_sources clk_src_dpll = { |
| 198 | .sources = clk_src_dpll_list, |
| 199 | .nr_sources = ARRAY_SIZE(clk_src_dpll_list), |
| 200 | }; |
| 201 | |
Kukjin Kim | 0c1945d | 2010-02-24 16:40:36 +0900 | [diff] [blame] | 202 | struct clk clk_vpll = { |
| 203 | .name = "vpll", |
| 204 | .id = -1, |
| 205 | }; |
| 206 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 207 | int s5p_gatectrl(void __iomem *reg, struct clk *clk, int enable) |
| 208 | { |
| 209 | unsigned int ctrlbit = clk->ctrlbit; |
| 210 | u32 con; |
| 211 | |
| 212 | con = __raw_readl(reg); |
| 213 | con = enable ? (con | ctrlbit) : (con & ~ctrlbit); |
| 214 | __raw_writel(con, reg); |
| 215 | return 0; |
| 216 | } |
| 217 | |
Seungwhan Youn | d4b34c6 | 2010-10-14 10:39:08 +0900 | [diff] [blame] | 218 | int s5p_epll_enable(struct clk *clk, int enable) |
| 219 | { |
| 220 | unsigned int ctrlbit = clk->ctrlbit; |
| 221 | unsigned int epll_con = __raw_readl(S5P_EPLL_CON) & ~ctrlbit; |
| 222 | |
| 223 | if (enable) |
| 224 | __raw_writel(epll_con | ctrlbit, S5P_EPLL_CON); |
| 225 | else |
| 226 | __raw_writel(epll_con, S5P_EPLL_CON); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | unsigned long s5p_epll_get_rate(struct clk *clk) |
| 232 | { |
| 233 | return clk->rate; |
| 234 | } |
| 235 | |
Naveen Krishna Chatradhi | 65f5eaa | 2011-07-18 14:44:19 +0900 | [diff] [blame] | 236 | int s5p_spdif_set_rate(struct clk *clk, unsigned long rate) |
| 237 | { |
| 238 | struct clk *pclk; |
| 239 | int ret; |
| 240 | |
| 241 | pclk = clk_get_parent(clk); |
| 242 | if (IS_ERR(pclk)) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | ret = pclk->ops->set_rate(pclk, rate); |
| 246 | clk_put(pclk); |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | unsigned long s5p_spdif_get_rate(struct clk *clk) |
| 252 | { |
| 253 | struct clk *pclk; |
| 254 | int rate; |
| 255 | |
| 256 | pclk = clk_get_parent(clk); |
| 257 | if (IS_ERR(pclk)) |
| 258 | return -EINVAL; |
| 259 | |
Naveen Krishna Chatradhi | 5d747c6 | 2011-08-19 20:52:29 +0900 | [diff] [blame] | 260 | rate = pclk->ops->get_rate(pclk); |
Naveen Krishna Chatradhi | 65f5eaa | 2011-07-18 14:44:19 +0900 | [diff] [blame] | 261 | clk_put(pclk); |
| 262 | |
| 263 | return rate; |
| 264 | } |
| 265 | |
| 266 | struct clk_ops s5p_sclk_spdif_ops = { |
| 267 | .set_rate = s5p_spdif_set_rate, |
| 268 | .get_rate = s5p_spdif_get_rate, |
| 269 | }; |
| 270 | |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 271 | static struct clk *s5p_clks[] __initdata = { |
| 272 | &clk_ext_xtal_mux, |
| 273 | &clk_48m, |
Kukjin Kim | 0c1945d | 2010-02-24 16:40:36 +0900 | [diff] [blame] | 274 | &s5p_clk_27m, |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 275 | &clk_fout_apll, |
| 276 | &clk_fout_mpll, |
| 277 | &clk_fout_epll, |
Kukjin Kim | 3109e55 | 2010-09-01 15:35:30 +0900 | [diff] [blame] | 278 | &clk_fout_dpll, |
Thomas Abraham | f445dbd | 2010-05-17 09:38:52 +0900 | [diff] [blame] | 279 | &clk_fout_vpll, |
Kukjin Kim | 0c1945d | 2010-02-24 16:40:36 +0900 | [diff] [blame] | 280 | &clk_vpll, |
Thomas Abraham | 8fb9d2d | 2010-05-28 11:41:16 +0900 | [diff] [blame] | 281 | &clk_xusbxti, |
Kukjin Kim | 1a0e8a5 | 2010-01-14 08:13:37 +0900 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | void __init s5p_register_clocks(unsigned long xtal_freq) |
| 285 | { |
| 286 | int ret; |
| 287 | |
| 288 | clk_ext_xtal_mux.rate = xtal_freq; |
| 289 | |
| 290 | ret = s3c24xx_register_clocks(s5p_clks, ARRAY_SIZE(s5p_clks)); |
| 291 | if (ret > 0) |
| 292 | printk(KERN_ERR "Failed to register s5p clocks\n"); |
| 293 | } |