Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 MundoReader S.L. |
| 3 | * Author: Heiko Stuebner <heiko@sntech.de> |
| 4 | * |
Xing Zheng | 9c4d6e55 | 2015-11-05 15:33:57 +0800 | [diff] [blame] | 5 | * Copyright (c) 2015 Rockchip Electronics Co. Ltd. |
| 6 | * Author: Xing Zheng <zhengxing@rock-chips.com> |
| 7 | * |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <asm/div64.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/delay.h> |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 23 | #include <linux/clk-provider.h> |
| 24 | #include <linux/regmap.h> |
Xing Zheng | 9c4d6e55 | 2015-11-05 15:33:57 +0800 | [diff] [blame] | 25 | #include <linux/clk.h> |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 26 | #include "clk.h" |
| 27 | |
| 28 | #define PLL_MODE_MASK 0x3 |
| 29 | #define PLL_MODE_SLOW 0x0 |
| 30 | #define PLL_MODE_NORM 0x1 |
| 31 | #define PLL_MODE_DEEP 0x2 |
| 32 | |
| 33 | struct rockchip_clk_pll { |
| 34 | struct clk_hw hw; |
| 35 | |
| 36 | struct clk_mux pll_mux; |
| 37 | const struct clk_ops *pll_mux_ops; |
| 38 | |
| 39 | struct notifier_block clk_nb; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 40 | |
| 41 | void __iomem *reg_base; |
| 42 | int lock_offset; |
| 43 | unsigned int lock_shift; |
| 44 | enum rockchip_pll_type type; |
Heiko Stuebner | 4f8a7c5 | 2014-11-20 20:38:50 +0100 | [diff] [blame] | 45 | u8 flags; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 46 | const struct rockchip_pll_rate_table *rate_table; |
| 47 | unsigned int rate_count; |
| 48 | spinlock_t *lock; |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 49 | |
| 50 | struct rockchip_clk_provider *ctx; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | #define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw) |
| 54 | #define to_rockchip_clk_pll_nb(nb) \ |
| 55 | container_of(nb, struct rockchip_clk_pll, clk_nb) |
| 56 | |
| 57 | static const struct rockchip_pll_rate_table *rockchip_get_pll_settings( |
| 58 | struct rockchip_clk_pll *pll, unsigned long rate) |
| 59 | { |
| 60 | const struct rockchip_pll_rate_table *rate_table = pll->rate_table; |
| 61 | int i; |
| 62 | |
| 63 | for (i = 0; i < pll->rate_count; i++) { |
| 64 | if (rate == rate_table[i].rate) |
| 65 | return &rate_table[i]; |
| 66 | } |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | static long rockchip_pll_round_rate(struct clk_hw *hw, |
| 72 | unsigned long drate, unsigned long *prate) |
| 73 | { |
| 74 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 75 | const struct rockchip_pll_rate_table *rate_table = pll->rate_table; |
| 76 | int i; |
| 77 | |
| 78 | /* Assumming rate_table is in descending order */ |
| 79 | for (i = 0; i < pll->rate_count; i++) { |
| 80 | if (drate >= rate_table[i].rate) |
| 81 | return rate_table[i].rate; |
| 82 | } |
| 83 | |
| 84 | /* return minimum supported value */ |
| 85 | return rate_table[i - 1].rate; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Wait for the pll to reach the locked state. |
| 90 | * The calling set_rate function is responsible for making sure the |
| 91 | * grf regmap is available. |
| 92 | */ |
| 93 | static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll) |
| 94 | { |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 95 | struct regmap *grf = rockchip_clk_get_grf(pll->ctx); |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 96 | unsigned int val; |
| 97 | int delay = 24000000, ret; |
| 98 | |
Shawn Lin | eb4e10c | 2016-02-15 11:33:25 +0800 | [diff] [blame] | 99 | if (IS_ERR(grf)) { |
| 100 | pr_err("%s: grf regmap not available\n", __func__); |
| 101 | return PTR_ERR(grf); |
| 102 | } |
| 103 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 104 | while (delay > 0) { |
| 105 | ret = regmap_read(grf, pll->lock_offset, &val); |
| 106 | if (ret) { |
| 107 | pr_err("%s: failed to read pll lock status: %d\n", |
| 108 | __func__, ret); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | if (val & BIT(pll->lock_shift)) |
| 113 | return 0; |
| 114 | delay--; |
| 115 | } |
| 116 | |
| 117 | pr_err("%s: timeout waiting for pll to lock\n", __func__); |
| 118 | return -ETIMEDOUT; |
| 119 | } |
| 120 | |
| 121 | /** |
Xing Zheng | 9c4d6e55 | 2015-11-05 15:33:57 +0800 | [diff] [blame] | 122 | * PLL used in RK3036 |
| 123 | */ |
| 124 | |
| 125 | #define RK3036_PLLCON(i) (i * 0x4) |
| 126 | #define RK3036_PLLCON0_FBDIV_MASK 0xfff |
| 127 | #define RK3036_PLLCON0_FBDIV_SHIFT 0 |
| 128 | #define RK3036_PLLCON0_POSTDIV1_MASK 0x7 |
| 129 | #define RK3036_PLLCON0_POSTDIV1_SHIFT 12 |
| 130 | #define RK3036_PLLCON1_REFDIV_MASK 0x3f |
| 131 | #define RK3036_PLLCON1_REFDIV_SHIFT 0 |
| 132 | #define RK3036_PLLCON1_POSTDIV2_MASK 0x7 |
| 133 | #define RK3036_PLLCON1_POSTDIV2_SHIFT 6 |
| 134 | #define RK3036_PLLCON1_DSMPD_MASK 0x1 |
| 135 | #define RK3036_PLLCON1_DSMPD_SHIFT 12 |
| 136 | #define RK3036_PLLCON2_FRAC_MASK 0xffffff |
| 137 | #define RK3036_PLLCON2_FRAC_SHIFT 0 |
| 138 | |
| 139 | #define RK3036_PLLCON1_PWRDOWN (1 << 13) |
| 140 | |
| 141 | static void rockchip_rk3036_pll_get_params(struct rockchip_clk_pll *pll, |
| 142 | struct rockchip_pll_rate_table *rate) |
| 143 | { |
| 144 | u32 pllcon; |
| 145 | |
| 146 | pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(0)); |
| 147 | rate->fbdiv = ((pllcon >> RK3036_PLLCON0_FBDIV_SHIFT) |
| 148 | & RK3036_PLLCON0_FBDIV_MASK); |
| 149 | rate->postdiv1 = ((pllcon >> RK3036_PLLCON0_POSTDIV1_SHIFT) |
| 150 | & RK3036_PLLCON0_POSTDIV1_MASK); |
| 151 | |
| 152 | pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(1)); |
| 153 | rate->refdiv = ((pllcon >> RK3036_PLLCON1_REFDIV_SHIFT) |
| 154 | & RK3036_PLLCON1_REFDIV_MASK); |
| 155 | rate->postdiv2 = ((pllcon >> RK3036_PLLCON1_POSTDIV2_SHIFT) |
| 156 | & RK3036_PLLCON1_POSTDIV2_MASK); |
| 157 | rate->dsmpd = ((pllcon >> RK3036_PLLCON1_DSMPD_SHIFT) |
| 158 | & RK3036_PLLCON1_DSMPD_MASK); |
| 159 | |
| 160 | pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2)); |
| 161 | rate->frac = ((pllcon >> RK3036_PLLCON2_FRAC_SHIFT) |
| 162 | & RK3036_PLLCON2_FRAC_MASK); |
| 163 | } |
| 164 | |
| 165 | static unsigned long rockchip_rk3036_pll_recalc_rate(struct clk_hw *hw, |
| 166 | unsigned long prate) |
| 167 | { |
| 168 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 169 | struct rockchip_pll_rate_table cur; |
| 170 | u64 rate64 = prate; |
| 171 | |
| 172 | rockchip_rk3036_pll_get_params(pll, &cur); |
| 173 | |
| 174 | rate64 *= cur.fbdiv; |
| 175 | do_div(rate64, cur.refdiv); |
| 176 | |
| 177 | if (cur.dsmpd == 0) { |
| 178 | /* fractional mode */ |
| 179 | u64 frac_rate64 = prate * cur.frac; |
| 180 | |
| 181 | do_div(frac_rate64, cur.refdiv); |
| 182 | rate64 += frac_rate64 >> 24; |
| 183 | } |
| 184 | |
| 185 | do_div(rate64, cur.postdiv1); |
| 186 | do_div(rate64, cur.postdiv2); |
| 187 | |
| 188 | return (unsigned long)rate64; |
| 189 | } |
| 190 | |
| 191 | static int rockchip_rk3036_pll_set_params(struct rockchip_clk_pll *pll, |
| 192 | const struct rockchip_pll_rate_table *rate) |
| 193 | { |
| 194 | const struct clk_ops *pll_mux_ops = pll->pll_mux_ops; |
| 195 | struct clk_mux *pll_mux = &pll->pll_mux; |
| 196 | struct rockchip_pll_rate_table cur; |
| 197 | u32 pllcon; |
| 198 | int rate_change_remuxed = 0; |
| 199 | int cur_parent; |
| 200 | int ret; |
| 201 | |
| 202 | pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n", |
| 203 | __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv, |
| 204 | rate->postdiv2, rate->dsmpd, rate->frac); |
| 205 | |
| 206 | rockchip_rk3036_pll_get_params(pll, &cur); |
| 207 | cur.rate = 0; |
| 208 | |
| 209 | cur_parent = pll_mux_ops->get_parent(&pll_mux->hw); |
| 210 | if (cur_parent == PLL_MODE_NORM) { |
| 211 | pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW); |
| 212 | rate_change_remuxed = 1; |
| 213 | } |
| 214 | |
| 215 | /* update pll values */ |
| 216 | writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3036_PLLCON0_FBDIV_MASK, |
| 217 | RK3036_PLLCON0_FBDIV_SHIFT) | |
| 218 | HIWORD_UPDATE(rate->postdiv1, RK3036_PLLCON0_POSTDIV1_MASK, |
| 219 | RK3036_PLLCON0_POSTDIV1_SHIFT), |
| 220 | pll->reg_base + RK3036_PLLCON(0)); |
| 221 | |
| 222 | writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3036_PLLCON1_REFDIV_MASK, |
| 223 | RK3036_PLLCON1_REFDIV_SHIFT) | |
| 224 | HIWORD_UPDATE(rate->postdiv2, RK3036_PLLCON1_POSTDIV2_MASK, |
| 225 | RK3036_PLLCON1_POSTDIV2_SHIFT) | |
| 226 | HIWORD_UPDATE(rate->dsmpd, RK3036_PLLCON1_DSMPD_MASK, |
| 227 | RK3036_PLLCON1_DSMPD_SHIFT), |
| 228 | pll->reg_base + RK3036_PLLCON(1)); |
| 229 | |
| 230 | /* GPLL CON2 is not HIWORD_MASK */ |
| 231 | pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2)); |
| 232 | pllcon &= ~(RK3036_PLLCON2_FRAC_MASK << RK3036_PLLCON2_FRAC_SHIFT); |
| 233 | pllcon |= rate->frac << RK3036_PLLCON2_FRAC_SHIFT; |
| 234 | writel_relaxed(pllcon, pll->reg_base + RK3036_PLLCON(2)); |
| 235 | |
| 236 | /* wait for the pll to lock */ |
| 237 | ret = rockchip_pll_wait_lock(pll); |
| 238 | if (ret) { |
| 239 | pr_warn("%s: pll update unsucessful, trying to restore old params\n", |
| 240 | __func__); |
| 241 | rockchip_rk3036_pll_set_params(pll, &cur); |
| 242 | } |
| 243 | |
| 244 | if (rate_change_remuxed) |
| 245 | pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static int rockchip_rk3036_pll_set_rate(struct clk_hw *hw, unsigned long drate, |
| 251 | unsigned long prate) |
| 252 | { |
| 253 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 254 | const struct rockchip_pll_rate_table *rate; |
| 255 | unsigned long old_rate = rockchip_rk3036_pll_recalc_rate(hw, prate); |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 256 | struct regmap *grf = rockchip_clk_get_grf(pll->ctx); |
Xing Zheng | 9c4d6e55 | 2015-11-05 15:33:57 +0800 | [diff] [blame] | 257 | |
| 258 | if (IS_ERR(grf)) { |
| 259 | pr_debug("%s: grf regmap not available, aborting rate change\n", |
| 260 | __func__); |
| 261 | return PTR_ERR(grf); |
| 262 | } |
| 263 | |
| 264 | pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n", |
| 265 | __func__, __clk_get_name(hw->clk), old_rate, drate, prate); |
| 266 | |
| 267 | /* Get required rate settings from table */ |
| 268 | rate = rockchip_get_pll_settings(pll, drate); |
| 269 | if (!rate) { |
| 270 | pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__, |
| 271 | drate, __clk_get_name(hw->clk)); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | return rockchip_rk3036_pll_set_params(pll, rate); |
| 276 | } |
| 277 | |
| 278 | static int rockchip_rk3036_pll_enable(struct clk_hw *hw) |
| 279 | { |
| 280 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 281 | |
| 282 | writel(HIWORD_UPDATE(0, RK3036_PLLCON1_PWRDOWN, 0), |
| 283 | pll->reg_base + RK3036_PLLCON(1)); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static void rockchip_rk3036_pll_disable(struct clk_hw *hw) |
| 289 | { |
| 290 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 291 | |
| 292 | writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN, |
| 293 | RK3036_PLLCON1_PWRDOWN, 0), |
| 294 | pll->reg_base + RK3036_PLLCON(1)); |
| 295 | } |
| 296 | |
| 297 | static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw) |
| 298 | { |
| 299 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 300 | u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1)); |
| 301 | |
| 302 | return !(pllcon & RK3036_PLLCON1_PWRDOWN); |
| 303 | } |
| 304 | |
| 305 | static void rockchip_rk3036_pll_init(struct clk_hw *hw) |
| 306 | { |
| 307 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 308 | const struct rockchip_pll_rate_table *rate; |
| 309 | struct rockchip_pll_rate_table cur; |
| 310 | unsigned long drate; |
| 311 | |
| 312 | if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE)) |
| 313 | return; |
| 314 | |
| 315 | drate = clk_hw_get_rate(hw); |
| 316 | rate = rockchip_get_pll_settings(pll, drate); |
| 317 | |
| 318 | /* when no rate setting for the current rate, rely on clk_set_rate */ |
| 319 | if (!rate) |
| 320 | return; |
| 321 | |
| 322 | rockchip_rk3036_pll_get_params(pll, &cur); |
| 323 | |
| 324 | pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk), |
| 325 | drate); |
| 326 | pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n", |
| 327 | cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2, |
| 328 | cur.dsmpd, cur.frac); |
| 329 | pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n", |
| 330 | rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2, |
| 331 | rate->dsmpd, rate->frac); |
| 332 | |
| 333 | if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 || |
| 334 | rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 || |
| 335 | rate->dsmpd != cur.dsmpd || rate->frac != cur.frac) { |
| 336 | struct clk *parent = clk_get_parent(hw->clk); |
| 337 | |
| 338 | if (!parent) { |
| 339 | pr_warn("%s: parent of %s not available\n", |
| 340 | __func__, __clk_get_name(hw->clk)); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n", |
| 345 | __func__, __clk_get_name(hw->clk)); |
| 346 | rockchip_rk3036_pll_set_params(pll, rate); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = { |
| 351 | .recalc_rate = rockchip_rk3036_pll_recalc_rate, |
| 352 | .enable = rockchip_rk3036_pll_enable, |
| 353 | .disable = rockchip_rk3036_pll_disable, |
| 354 | .is_enabled = rockchip_rk3036_pll_is_enabled, |
| 355 | }; |
| 356 | |
| 357 | static const struct clk_ops rockchip_rk3036_pll_clk_ops = { |
| 358 | .recalc_rate = rockchip_rk3036_pll_recalc_rate, |
| 359 | .round_rate = rockchip_pll_round_rate, |
| 360 | .set_rate = rockchip_rk3036_pll_set_rate, |
| 361 | .enable = rockchip_rk3036_pll_enable, |
| 362 | .disable = rockchip_rk3036_pll_disable, |
| 363 | .is_enabled = rockchip_rk3036_pll_is_enabled, |
| 364 | .init = rockchip_rk3036_pll_init, |
| 365 | }; |
| 366 | |
| 367 | /** |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 368 | * PLL used in RK3066, RK3188 and RK3288 |
| 369 | */ |
| 370 | |
| 371 | #define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1) |
| 372 | |
| 373 | #define RK3066_PLLCON(i) (i * 0x4) |
| 374 | #define RK3066_PLLCON0_OD_MASK 0xf |
| 375 | #define RK3066_PLLCON0_OD_SHIFT 0 |
| 376 | #define RK3066_PLLCON0_NR_MASK 0x3f |
| 377 | #define RK3066_PLLCON0_NR_SHIFT 8 |
| 378 | #define RK3066_PLLCON1_NF_MASK 0x1fff |
| 379 | #define RK3066_PLLCON1_NF_SHIFT 0 |
Douglas Anderson | 2bbfe00 | 2015-07-21 13:41:23 -0700 | [diff] [blame] | 380 | #define RK3066_PLLCON2_NB_MASK 0xfff |
| 381 | #define RK3066_PLLCON2_NB_SHIFT 0 |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 382 | #define RK3066_PLLCON3_RESET (1 << 5) |
| 383 | #define RK3066_PLLCON3_PWRDOWN (1 << 1) |
| 384 | #define RK3066_PLLCON3_BYPASS (1 << 0) |
| 385 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 386 | static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll, |
| 387 | struct rockchip_pll_rate_table *rate) |
| 388 | { |
| 389 | u32 pllcon; |
| 390 | |
| 391 | pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0)); |
| 392 | rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT) |
| 393 | & RK3066_PLLCON0_NR_MASK) + 1; |
| 394 | rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT) |
| 395 | & RK3066_PLLCON0_OD_MASK) + 1; |
| 396 | |
| 397 | pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1)); |
| 398 | rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT) |
| 399 | & RK3066_PLLCON1_NF_MASK) + 1; |
| 400 | |
| 401 | pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2)); |
| 402 | rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT) |
| 403 | & RK3066_PLLCON2_NB_MASK) + 1; |
| 404 | } |
| 405 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 406 | static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw, |
| 407 | unsigned long prate) |
| 408 | { |
| 409 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 410 | struct rockchip_pll_rate_table cur; |
| 411 | u64 rate64 = prate; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 412 | u32 pllcon; |
| 413 | |
| 414 | pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3)); |
| 415 | if (pllcon & RK3066_PLLCON3_BYPASS) { |
| 416 | pr_debug("%s: pll %s is bypassed\n", __func__, |
Stephen Boyd | 4c34875 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 417 | clk_hw_get_name(hw)); |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 418 | return prate; |
| 419 | } |
| 420 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 421 | rockchip_rk3066_pll_get_params(pll, &cur); |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 422 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 423 | rate64 *= cur.nf; |
| 424 | do_div(rate64, cur.nr); |
| 425 | do_div(rate64, cur.no); |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 426 | |
| 427 | return (unsigned long)rate64; |
| 428 | } |
| 429 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 430 | static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll, |
| 431 | const struct rockchip_pll_rate_table *rate) |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 432 | { |
Doug Anderson | 9c030ea | 2014-09-15 21:07:57 -0700 | [diff] [blame] | 433 | const struct clk_ops *pll_mux_ops = pll->pll_mux_ops; |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 434 | struct clk_mux *pll_mux = &pll->pll_mux; |
| 435 | struct rockchip_pll_rate_table cur; |
Doug Anderson | 9c030ea | 2014-09-15 21:07:57 -0700 | [diff] [blame] | 436 | int rate_change_remuxed = 0; |
| 437 | int cur_parent; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 438 | int ret; |
| 439 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 440 | pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n", |
| 441 | __func__, rate->rate, rate->nr, rate->no, rate->nf); |
| 442 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 443 | rockchip_rk3066_pll_get_params(pll, &cur); |
| 444 | cur.rate = 0; |
| 445 | |
Doug Anderson | 9c030ea | 2014-09-15 21:07:57 -0700 | [diff] [blame] | 446 | cur_parent = pll_mux_ops->get_parent(&pll_mux->hw); |
| 447 | if (cur_parent == PLL_MODE_NORM) { |
| 448 | pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW); |
| 449 | rate_change_remuxed = 1; |
| 450 | } |
| 451 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 452 | /* enter reset mode */ |
| 453 | writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0), |
| 454 | pll->reg_base + RK3066_PLLCON(3)); |
| 455 | |
| 456 | /* update pll values */ |
| 457 | writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK, |
| 458 | RK3066_PLLCON0_NR_SHIFT) | |
| 459 | HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK, |
| 460 | RK3066_PLLCON0_OD_SHIFT), |
| 461 | pll->reg_base + RK3066_PLLCON(0)); |
| 462 | |
| 463 | writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK, |
| 464 | RK3066_PLLCON1_NF_SHIFT), |
| 465 | pll->reg_base + RK3066_PLLCON(1)); |
Douglas Anderson | 2bbfe00 | 2015-07-21 13:41:23 -0700 | [diff] [blame] | 466 | writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK, |
| 467 | RK3066_PLLCON2_NB_SHIFT), |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 468 | pll->reg_base + RK3066_PLLCON(2)); |
| 469 | |
| 470 | /* leave reset and wait the reset_delay */ |
| 471 | writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0), |
| 472 | pll->reg_base + RK3066_PLLCON(3)); |
| 473 | udelay(RK3066_PLL_RESET_DELAY(rate->nr)); |
| 474 | |
| 475 | /* wait for the pll to lock */ |
| 476 | ret = rockchip_pll_wait_lock(pll); |
| 477 | if (ret) { |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 478 | pr_warn("%s: pll update unsucessful, trying to restore old params\n", |
| 479 | __func__); |
| 480 | rockchip_rk3066_pll_set_params(pll, &cur); |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Doug Anderson | 9c030ea | 2014-09-15 21:07:57 -0700 | [diff] [blame] | 483 | if (rate_change_remuxed) |
| 484 | pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM); |
| 485 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 486 | return ret; |
| 487 | } |
| 488 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 489 | static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate, |
| 490 | unsigned long prate) |
| 491 | { |
| 492 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 493 | const struct rockchip_pll_rate_table *rate; |
| 494 | unsigned long old_rate = rockchip_rk3066_pll_recalc_rate(hw, prate); |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 495 | struct regmap *grf = rockchip_clk_get_grf(pll->ctx); |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 496 | |
| 497 | if (IS_ERR(grf)) { |
| 498 | pr_debug("%s: grf regmap not available, aborting rate change\n", |
| 499 | __func__); |
| 500 | return PTR_ERR(grf); |
| 501 | } |
| 502 | |
| 503 | pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n", |
| 504 | __func__, clk_hw_get_name(hw), old_rate, drate, prate); |
| 505 | |
| 506 | /* Get required rate settings from table */ |
| 507 | rate = rockchip_get_pll_settings(pll, drate); |
| 508 | if (!rate) { |
| 509 | pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__, |
| 510 | drate, clk_hw_get_name(hw)); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | return rockchip_rk3066_pll_set_params(pll, rate); |
| 515 | } |
| 516 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 517 | static int rockchip_rk3066_pll_enable(struct clk_hw *hw) |
| 518 | { |
| 519 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 520 | |
| 521 | writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0), |
| 522 | pll->reg_base + RK3066_PLLCON(3)); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static void rockchip_rk3066_pll_disable(struct clk_hw *hw) |
| 528 | { |
| 529 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 530 | |
| 531 | writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN, |
| 532 | RK3066_PLLCON3_PWRDOWN, 0), |
| 533 | pll->reg_base + RK3066_PLLCON(3)); |
| 534 | } |
| 535 | |
| 536 | static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw) |
| 537 | { |
| 538 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 539 | u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3)); |
| 540 | |
| 541 | return !(pllcon & RK3066_PLLCON3_PWRDOWN); |
| 542 | } |
| 543 | |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 544 | static void rockchip_rk3066_pll_init(struct clk_hw *hw) |
| 545 | { |
| 546 | struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw); |
| 547 | const struct rockchip_pll_rate_table *rate; |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 548 | struct rockchip_pll_rate_table cur; |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 549 | unsigned long drate; |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 550 | |
| 551 | if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE)) |
| 552 | return; |
| 553 | |
Stephen Boyd | 4c34875 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 554 | drate = clk_hw_get_rate(hw); |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 555 | rate = rockchip_get_pll_settings(pll, drate); |
| 556 | |
| 557 | /* when no rate setting for the current rate, rely on clk_set_rate */ |
| 558 | if (!rate) |
| 559 | return; |
| 560 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 561 | rockchip_rk3066_pll_get_params(pll, &cur); |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 562 | |
Douglas Anderson | 2bbfe00 | 2015-07-21 13:41:23 -0700 | [diff] [blame] | 563 | pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n", |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 564 | __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr, |
| 565 | rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb); |
| 566 | if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf |
| 567 | || rate->nb != cur.nb) { |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 568 | struct regmap *grf = rockchip_clk_get_grf(pll->ctx); |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 569 | |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 570 | if (IS_ERR(grf)) |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 571 | return; |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 572 | |
| 573 | pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n", |
Stephen Boyd | 4c34875 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 574 | __func__, clk_hw_get_name(hw)); |
Heiko Stübner | 8334c0e | 2015-10-01 11:38:35 +0200 | [diff] [blame] | 575 | rockchip_rk3066_pll_set_params(pll, rate); |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 579 | static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = { |
| 580 | .recalc_rate = rockchip_rk3066_pll_recalc_rate, |
| 581 | .enable = rockchip_rk3066_pll_enable, |
| 582 | .disable = rockchip_rk3066_pll_disable, |
| 583 | .is_enabled = rockchip_rk3066_pll_is_enabled, |
| 584 | }; |
| 585 | |
| 586 | static const struct clk_ops rockchip_rk3066_pll_clk_ops = { |
| 587 | .recalc_rate = rockchip_rk3066_pll_recalc_rate, |
| 588 | .round_rate = rockchip_pll_round_rate, |
| 589 | .set_rate = rockchip_rk3066_pll_set_rate, |
| 590 | .enable = rockchip_rk3066_pll_enable, |
| 591 | .disable = rockchip_rk3066_pll_disable, |
| 592 | .is_enabled = rockchip_rk3066_pll_is_enabled, |
Heiko Stuebner | 0bb66d3 | 2014-11-20 20:38:52 +0100 | [diff] [blame] | 593 | .init = rockchip_rk3066_pll_init, |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 594 | }; |
| 595 | |
| 596 | /* |
| 597 | * Common registering of pll clocks |
| 598 | */ |
| 599 | |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 600 | struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx, |
| 601 | enum rockchip_pll_type pll_type, |
Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 602 | const char *name, const char *const *parent_names, |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 603 | u8 num_parents, int con_offset, int grf_lock_offset, |
| 604 | int lock_shift, int mode_offset, int mode_shift, |
| 605 | struct rockchip_pll_rate_table *rate_table, |
| 606 | u8 clk_pll_flags) |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 607 | { |
| 608 | const char *pll_parents[3]; |
| 609 | struct clk_init_data init; |
| 610 | struct rockchip_clk_pll *pll; |
| 611 | struct clk_mux *pll_mux; |
| 612 | struct clk *pll_clk, *mux_clk; |
| 613 | char pll_name[20]; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 614 | |
| 615 | if (num_parents != 2) { |
| 616 | pr_err("%s: needs two parent clocks\n", __func__); |
| 617 | return ERR_PTR(-EINVAL); |
| 618 | } |
| 619 | |
| 620 | /* name the actual pll */ |
| 621 | snprintf(pll_name, sizeof(pll_name), "pll_%s", name); |
| 622 | |
| 623 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 624 | if (!pll) |
| 625 | return ERR_PTR(-ENOMEM); |
| 626 | |
Heiko Stuebner | 1089737 | 2015-08-19 15:06:55 +0200 | [diff] [blame] | 627 | /* create the mux on top of the real pll */ |
| 628 | pll->pll_mux_ops = &clk_mux_ops; |
| 629 | pll_mux = &pll->pll_mux; |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 630 | pll_mux->reg = ctx->reg_base + mode_offset; |
Heiko Stuebner | 1089737 | 2015-08-19 15:06:55 +0200 | [diff] [blame] | 631 | pll_mux->shift = mode_shift; |
| 632 | pll_mux->mask = PLL_MODE_MASK; |
| 633 | pll_mux->flags = 0; |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 634 | pll_mux->lock = &ctx->lock; |
Heiko Stuebner | 1089737 | 2015-08-19 15:06:55 +0200 | [diff] [blame] | 635 | pll_mux->hw.init = &init; |
| 636 | |
Xing Zheng | 9c4d6e55 | 2015-11-05 15:33:57 +0800 | [diff] [blame] | 637 | if (pll_type == pll_rk3036 || pll_type == pll_rk3066) |
Heiko Stuebner | 1089737 | 2015-08-19 15:06:55 +0200 | [diff] [blame] | 638 | pll_mux->flags |= CLK_MUX_HIWORD_MASK; |
| 639 | |
| 640 | /* the actual muxing is xin24m, pll-output, xin32k */ |
| 641 | pll_parents[0] = parent_names[0]; |
| 642 | pll_parents[1] = pll_name; |
| 643 | pll_parents[2] = parent_names[1]; |
| 644 | |
| 645 | init.name = name; |
| 646 | init.flags = CLK_SET_RATE_PARENT; |
| 647 | init.ops = pll->pll_mux_ops; |
| 648 | init.parent_names = pll_parents; |
| 649 | init.num_parents = ARRAY_SIZE(pll_parents); |
| 650 | |
| 651 | mux_clk = clk_register(NULL, &pll_mux->hw); |
| 652 | if (IS_ERR(mux_clk)) |
| 653 | goto err_mux; |
| 654 | |
| 655 | /* now create the actual pll */ |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 656 | init.name = pll_name; |
| 657 | |
| 658 | /* keep all plls untouched for now */ |
| 659 | init.flags = CLK_IGNORE_UNUSED; |
| 660 | |
| 661 | init.parent_names = &parent_names[0]; |
| 662 | init.num_parents = 1; |
| 663 | |
| 664 | if (rate_table) { |
| 665 | int len; |
| 666 | |
| 667 | /* find count of rates in rate_table */ |
| 668 | for (len = 0; rate_table[len].rate != 0; ) |
| 669 | len++; |
| 670 | |
| 671 | pll->rate_count = len; |
| 672 | pll->rate_table = kmemdup(rate_table, |
| 673 | pll->rate_count * |
| 674 | sizeof(struct rockchip_pll_rate_table), |
| 675 | GFP_KERNEL); |
| 676 | WARN(!pll->rate_table, |
| 677 | "%s: could not allocate rate table for %s\n", |
| 678 | __func__, name); |
| 679 | } |
| 680 | |
| 681 | switch (pll_type) { |
Xing Zheng | 9c4d6e55 | 2015-11-05 15:33:57 +0800 | [diff] [blame] | 682 | case pll_rk3036: |
| 683 | if (!pll->rate_table) |
| 684 | init.ops = &rockchip_rk3036_pll_clk_norate_ops; |
| 685 | else |
| 686 | init.ops = &rockchip_rk3036_pll_clk_ops; |
| 687 | break; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 688 | case pll_rk3066: |
| 689 | if (!pll->rate_table) |
| 690 | init.ops = &rockchip_rk3066_pll_clk_norate_ops; |
| 691 | else |
| 692 | init.ops = &rockchip_rk3066_pll_clk_ops; |
| 693 | break; |
| 694 | default: |
| 695 | pr_warn("%s: Unknown pll type for pll clk %s\n", |
| 696 | __func__, name); |
| 697 | } |
| 698 | |
| 699 | pll->hw.init = &init; |
| 700 | pll->type = pll_type; |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 701 | pll->reg_base = ctx->reg_base + con_offset; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 702 | pll->lock_offset = grf_lock_offset; |
| 703 | pll->lock_shift = lock_shift; |
Heiko Stuebner | 4f8a7c5 | 2014-11-20 20:38:50 +0100 | [diff] [blame] | 704 | pll->flags = clk_pll_flags; |
Xing Zheng | ef1d9fe | 2016-03-09 10:37:04 +0800 | [diff] [blame^] | 705 | pll->lock = &ctx->lock; |
| 706 | pll->ctx = ctx; |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 707 | |
| 708 | pll_clk = clk_register(NULL, &pll->hw); |
| 709 | if (IS_ERR(pll_clk)) { |
| 710 | pr_err("%s: failed to register pll clock %s : %ld\n", |
| 711 | __func__, name, PTR_ERR(pll_clk)); |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 712 | goto err_pll; |
| 713 | } |
| 714 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 715 | return mux_clk; |
| 716 | |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 717 | err_pll: |
Heiko Stuebner | 1089737 | 2015-08-19 15:06:55 +0200 | [diff] [blame] | 718 | clk_unregister(mux_clk); |
| 719 | mux_clk = pll_clk; |
| 720 | err_mux: |
Heiko Stübner | 90c5902 | 2014-07-03 01:59:10 +0200 | [diff] [blame] | 721 | kfree(pll); |
| 722 | return mux_clk; |
| 723 | } |