1 /* 2 * Copyright (c) 2013, The Linux Foundation. All rights reserved. 3 * 4 * This software is licensed under the terms of the GNU General Public 5 * License version 2, as published by the Free Software Foundation, and 6 * may be copied, distributed, and modified under those terms. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/bitops.h> 16 #include <linux/err.h> 17 #include <linux/export.h> 18 #include <linux/clk-provider.h> 19 #include <linux/regmap.h> 20 21 #include <asm/div64.h> 22 23 #include "clk-rcg.h" 24 #include "common.h" 25 26 static u32 ns_to_src(struct src_sel *s, u32 ns) 27 { 28 ns >>= s->src_sel_shift; 29 ns &= SRC_SEL_MASK; 30 return ns; 31 } 32 33 static u32 src_to_ns(struct src_sel *s, u8 src, u32 ns) 34 { 35 u32 mask; 36 37 mask = SRC_SEL_MASK; 38 mask <<= s->src_sel_shift; 39 ns &= ~mask; 40 41 ns |= src << s->src_sel_shift; 42 return ns; 43 } 44 45 static u8 clk_rcg_get_parent(struct clk_hw *hw) 46 { 47 struct clk_rcg *rcg = to_clk_rcg(hw); 48 int num_parents = __clk_get_num_parents(hw->clk); 49 u32 ns; 50 int i; 51 52 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 53 ns = ns_to_src(&rcg->s, ns); 54 for (i = 0; i < num_parents; i++) 55 if (ns == rcg->s.parent_map[i]) 56 return i; 57 58 return -EINVAL; 59 } 60 61 static int reg_to_bank(struct clk_dyn_rcg *rcg, u32 bank) 62 { 63 bank &= BIT(rcg->mux_sel_bit); 64 return !!bank; 65 } 66 67 static u8 clk_dyn_rcg_get_parent(struct clk_hw *hw) 68 { 69 struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); 70 int num_parents = __clk_get_num_parents(hw->clk); 71 u32 ns, ctl; 72 int bank; 73 int i; 74 struct src_sel *s; 75 76 regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &ctl); 77 bank = reg_to_bank(rcg, ctl); 78 s = &rcg->s[bank]; 79 80 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 81 ns = ns_to_src(s, ns); 82 83 for (i = 0; i < num_parents; i++) 84 if (ns == s->parent_map[i]) 85 return i; 86 87 return -EINVAL; 88 } 89 90 static int clk_rcg_set_parent(struct clk_hw *hw, u8 index) 91 { 92 struct clk_rcg *rcg = to_clk_rcg(hw); 93 u32 ns; 94 95 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 96 ns = src_to_ns(&rcg->s, rcg->s.parent_map[index], ns); 97 regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); 98 99 return 0; 100 } 101 102 static u32 md_to_m(struct mn *mn, u32 md) 103 { 104 md >>= mn->m_val_shift; 105 md &= BIT(mn->width) - 1; 106 return md; 107 } 108 109 static u32 ns_to_pre_div(struct pre_div *p, u32 ns) 110 { 111 ns >>= p->pre_div_shift; 112 ns &= BIT(p->pre_div_width) - 1; 113 return ns; 114 } 115 116 static u32 pre_div_to_ns(struct pre_div *p, u8 pre_div, u32 ns) 117 { 118 u32 mask; 119 120 mask = BIT(p->pre_div_width) - 1; 121 mask <<= p->pre_div_shift; 122 ns &= ~mask; 123 124 ns |= pre_div << p->pre_div_shift; 125 return ns; 126 } 127 128 static u32 mn_to_md(struct mn *mn, u32 m, u32 n, u32 md) 129 { 130 u32 mask, mask_w; 131 132 mask_w = BIT(mn->width) - 1; 133 mask = (mask_w << mn->m_val_shift) | mask_w; 134 md &= ~mask; 135 136 if (n) { 137 m <<= mn->m_val_shift; 138 md |= m; 139 md |= ~n & mask_w; 140 } 141 142 return md; 143 } 144 145 static u32 ns_m_to_n(struct mn *mn, u32 ns, u32 m) 146 { 147 ns = ~ns >> mn->n_val_shift; 148 ns &= BIT(mn->width) - 1; 149 return ns + m; 150 } 151 152 static u32 reg_to_mnctr_mode(struct mn *mn, u32 val) 153 { 154 val >>= mn->mnctr_mode_shift; 155 val &= MNCTR_MODE_MASK; 156 return val; 157 } 158 159 static u32 mn_to_ns(struct mn *mn, u32 m, u32 n, u32 ns) 160 { 161 u32 mask; 162 163 mask = BIT(mn->width) - 1; 164 mask <<= mn->n_val_shift; 165 ns &= ~mask; 166 167 if (n) { 168 n = n - m; 169 n = ~n; 170 n &= BIT(mn->width) - 1; 171 n <<= mn->n_val_shift; 172 ns |= n; 173 } 174 175 return ns; 176 } 177 178 static u32 mn_to_reg(struct mn *mn, u32 m, u32 n, u32 val) 179 { 180 u32 mask; 181 182 mask = MNCTR_MODE_MASK << mn->mnctr_mode_shift; 183 mask |= BIT(mn->mnctr_en_bit); 184 val &= ~mask; 185 186 if (n) { 187 val |= BIT(mn->mnctr_en_bit); 188 val |= MNCTR_MODE_DUAL << mn->mnctr_mode_shift; 189 } 190 191 return val; 192 } 193 194 static void configure_bank(struct clk_dyn_rcg *rcg, const struct freq_tbl *f) 195 { 196 u32 ns, md, ctl, *regp; 197 int bank, new_bank; 198 struct mn *mn; 199 struct pre_div *p; 200 struct src_sel *s; 201 bool enabled; 202 u32 md_reg; 203 u32 bank_reg; 204 bool banked_mn = !!rcg->mn[1].width; 205 struct clk_hw *hw = &rcg->clkr.hw; 206 207 enabled = __clk_is_enabled(hw->clk); 208 209 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 210 regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &ctl); 211 212 if (banked_mn) { 213 regp = &ctl; 214 bank_reg = rcg->clkr.enable_reg; 215 } else { 216 regp = &ns; 217 bank_reg = rcg->ns_reg; 218 } 219 220 bank = reg_to_bank(rcg, *regp); 221 new_bank = enabled ? !bank : bank; 222 223 if (banked_mn) { 224 mn = &rcg->mn[new_bank]; 225 md_reg = rcg->md_reg[new_bank]; 226 227 ns |= BIT(mn->mnctr_reset_bit); 228 regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); 229 230 regmap_read(rcg->clkr.regmap, md_reg, &md); 231 md = mn_to_md(mn, f->m, f->n, md); 232 regmap_write(rcg->clkr.regmap, md_reg, md); 233 234 ns = mn_to_ns(mn, f->m, f->n, ns); 235 regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); 236 237 ctl = mn_to_reg(mn, f->m, f->n, ctl); 238 regmap_write(rcg->clkr.regmap, rcg->clkr.enable_reg, ctl); 239 240 ns &= ~BIT(mn->mnctr_reset_bit); 241 regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); 242 } else { 243 p = &rcg->p[new_bank]; 244 ns = pre_div_to_ns(p, f->pre_div - 1, ns); 245 } 246 247 s = &rcg->s[new_bank]; 248 ns = src_to_ns(s, s->parent_map[f->src], ns); 249 regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); 250 251 if (enabled) { 252 *regp ^= BIT(rcg->mux_sel_bit); 253 regmap_write(rcg->clkr.regmap, bank_reg, *regp); 254 } 255 } 256 257 static int clk_dyn_rcg_set_parent(struct clk_hw *hw, u8 index) 258 { 259 struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); 260 u32 ns, ctl, md, reg; 261 int bank; 262 struct freq_tbl f = { 0 }; 263 bool banked_mn = !!rcg->mn[1].width; 264 265 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 266 regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &ctl); 267 reg = banked_mn ? ctl : ns; 268 269 bank = reg_to_bank(rcg, reg); 270 271 if (banked_mn) { 272 regmap_read(rcg->clkr.regmap, rcg->md_reg[bank], &md); 273 f.m = md_to_m(&rcg->mn[bank], md); 274 f.n = ns_m_to_n(&rcg->mn[bank], ns, f.m); 275 } else { 276 f.pre_div = ns_to_pre_div(&rcg->p[bank], ns) + 1; 277 } 278 f.src = index; 279 280 configure_bank(rcg, &f); 281 282 return 0; 283 } 284 285 /* 286 * Calculate m/n:d rate 287 * 288 * parent_rate m 289 * rate = ----------- x --- 290 * pre_div n 291 */ 292 static unsigned long 293 calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 pre_div) 294 { 295 if (pre_div) 296 rate /= pre_div + 1; 297 298 if (mode) { 299 u64 tmp = rate; 300 tmp *= m; 301 do_div(tmp, n); 302 rate = tmp; 303 } 304 305 return rate; 306 } 307 308 static unsigned long 309 clk_rcg_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) 310 { 311 struct clk_rcg *rcg = to_clk_rcg(hw); 312 u32 pre_div, m = 0, n = 0, ns, md, mode = 0; 313 struct mn *mn = &rcg->mn; 314 315 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 316 pre_div = ns_to_pre_div(&rcg->p, ns); 317 318 if (rcg->mn.width) { 319 regmap_read(rcg->clkr.regmap, rcg->md_reg, &md); 320 m = md_to_m(mn, md); 321 n = ns_m_to_n(mn, ns, m); 322 /* MN counter mode is in hw.enable_reg sometimes */ 323 if (rcg->clkr.enable_reg != rcg->ns_reg) 324 regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &mode); 325 else 326 mode = ns; 327 mode = reg_to_mnctr_mode(mn, mode); 328 } 329 330 return calc_rate(parent_rate, m, n, mode, pre_div); 331 } 332 333 static unsigned long 334 clk_dyn_rcg_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) 335 { 336 struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); 337 u32 m, n, pre_div, ns, md, mode, reg; 338 int bank; 339 struct mn *mn; 340 bool banked_mn = !!rcg->mn[1].width; 341 342 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 343 344 if (banked_mn) 345 regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, ®); 346 else 347 reg = ns; 348 349 bank = reg_to_bank(rcg, reg); 350 351 if (banked_mn) { 352 mn = &rcg->mn[bank]; 353 regmap_read(rcg->clkr.regmap, rcg->md_reg[bank], &md); 354 m = md_to_m(mn, md); 355 n = ns_m_to_n(mn, ns, m); 356 mode = reg_to_mnctr_mode(mn, reg); 357 return calc_rate(parent_rate, m, n, mode, 0); 358 } else { 359 pre_div = ns_to_pre_div(&rcg->p[bank], ns); 360 return calc_rate(parent_rate, 0, 0, 0, pre_div); 361 } 362 } 363 364 static long _freq_tbl_determine_rate(struct clk_hw *hw, 365 const struct freq_tbl *f, unsigned long rate, 366 unsigned long *p_rate, struct clk **p) 367 { 368 unsigned long clk_flags; 369 370 f = qcom_find_freq(f, rate); 371 if (!f) 372 return -EINVAL; 373 374 clk_flags = __clk_get_flags(hw->clk); 375 *p = clk_get_parent_by_index(hw->clk, f->src); 376 if (clk_flags & CLK_SET_RATE_PARENT) { 377 rate = rate * f->pre_div; 378 if (f->n) { 379 u64 tmp = rate; 380 tmp = tmp * f->n; 381 do_div(tmp, f->m); 382 rate = tmp; 383 } 384 } else { 385 rate = __clk_get_rate(*p); 386 } 387 *p_rate = rate; 388 389 return f->freq; 390 } 391 392 static long clk_rcg_determine_rate(struct clk_hw *hw, unsigned long rate, 393 unsigned long *p_rate, struct clk **p) 394 { 395 struct clk_rcg *rcg = to_clk_rcg(hw); 396 397 return _freq_tbl_determine_rate(hw, rcg->freq_tbl, rate, p_rate, p); 398 } 399 400 static long clk_dyn_rcg_determine_rate(struct clk_hw *hw, unsigned long rate, 401 unsigned long *p_rate, struct clk **p) 402 { 403 struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); 404 405 return _freq_tbl_determine_rate(hw, rcg->freq_tbl, rate, p_rate, p); 406 } 407 408 static long clk_rcg_bypass_determine_rate(struct clk_hw *hw, unsigned long rate, 409 unsigned long *p_rate, struct clk **p) 410 { 411 struct clk_rcg *rcg = to_clk_rcg(hw); 412 const struct freq_tbl *f = rcg->freq_tbl; 413 414 *p = clk_get_parent_by_index(hw->clk, f->src); 415 *p_rate = __clk_round_rate(*p, rate); 416 417 return *p_rate; 418 } 419 420 static int __clk_rcg_set_rate(struct clk_rcg *rcg, const struct freq_tbl *f) 421 { 422 u32 ns, md, ctl; 423 struct mn *mn = &rcg->mn; 424 u32 mask = 0; 425 unsigned int reset_reg; 426 427 if (rcg->mn.reset_in_cc) 428 reset_reg = rcg->clkr.enable_reg; 429 else 430 reset_reg = rcg->ns_reg; 431 432 if (rcg->mn.width) { 433 mask = BIT(mn->mnctr_reset_bit); 434 regmap_update_bits(rcg->clkr.regmap, reset_reg, mask, mask); 435 436 regmap_read(rcg->clkr.regmap, rcg->md_reg, &md); 437 md = mn_to_md(mn, f->m, f->n, md); 438 regmap_write(rcg->clkr.regmap, rcg->md_reg, md); 439 440 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 441 /* MN counter mode is in hw.enable_reg sometimes */ 442 if (rcg->clkr.enable_reg != rcg->ns_reg) { 443 regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &ctl); 444 ctl = mn_to_reg(mn, f->m, f->n, ctl); 445 regmap_write(rcg->clkr.regmap, rcg->clkr.enable_reg, ctl); 446 } else { 447 ns = mn_to_reg(mn, f->m, f->n, ns); 448 } 449 ns = mn_to_ns(mn, f->m, f->n, ns); 450 } else { 451 regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); 452 } 453 454 ns = pre_div_to_ns(&rcg->p, f->pre_div - 1, ns); 455 regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); 456 457 regmap_update_bits(rcg->clkr.regmap, reset_reg, mask, 0); 458 459 return 0; 460 } 461 462 static int clk_rcg_set_rate(struct clk_hw *hw, unsigned long rate, 463 unsigned long parent_rate) 464 { 465 struct clk_rcg *rcg = to_clk_rcg(hw); 466 const struct freq_tbl *f; 467 468 f = qcom_find_freq(rcg->freq_tbl, rate); 469 if (!f) 470 return -EINVAL; 471 472 return __clk_rcg_set_rate(rcg, f); 473 } 474 475 static int clk_rcg_bypass_set_rate(struct clk_hw *hw, unsigned long rate, 476 unsigned long parent_rate) 477 { 478 struct clk_rcg *rcg = to_clk_rcg(hw); 479 480 return __clk_rcg_set_rate(rcg, rcg->freq_tbl); 481 } 482 483 static int __clk_dyn_rcg_set_rate(struct clk_hw *hw, unsigned long rate) 484 { 485 struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); 486 const struct freq_tbl *f; 487 488 f = qcom_find_freq(rcg->freq_tbl, rate); 489 if (!f) 490 return -EINVAL; 491 492 configure_bank(rcg, f); 493 494 return 0; 495 } 496 497 static int clk_dyn_rcg_set_rate(struct clk_hw *hw, unsigned long rate, 498 unsigned long parent_rate) 499 { 500 return __clk_dyn_rcg_set_rate(hw, rate); 501 } 502 503 static int clk_dyn_rcg_set_rate_and_parent(struct clk_hw *hw, 504 unsigned long rate, unsigned long parent_rate, u8 index) 505 { 506 return __clk_dyn_rcg_set_rate(hw, rate); 507 } 508 509 const struct clk_ops clk_rcg_ops = { 510 .enable = clk_enable_regmap, 511 .disable = clk_disable_regmap, 512 .get_parent = clk_rcg_get_parent, 513 .set_parent = clk_rcg_set_parent, 514 .recalc_rate = clk_rcg_recalc_rate, 515 .determine_rate = clk_rcg_determine_rate, 516 .set_rate = clk_rcg_set_rate, 517 }; 518 EXPORT_SYMBOL_GPL(clk_rcg_ops); 519 520 const struct clk_ops clk_rcg_bypass_ops = { 521 .enable = clk_enable_regmap, 522 .disable = clk_disable_regmap, 523 .get_parent = clk_rcg_get_parent, 524 .set_parent = clk_rcg_set_parent, 525 .recalc_rate = clk_rcg_recalc_rate, 526 .determine_rate = clk_rcg_bypass_determine_rate, 527 .set_rate = clk_rcg_bypass_set_rate, 528 }; 529 EXPORT_SYMBOL_GPL(clk_rcg_bypass_ops); 530 531 const struct clk_ops clk_dyn_rcg_ops = { 532 .enable = clk_enable_regmap, 533 .is_enabled = clk_is_enabled_regmap, 534 .disable = clk_disable_regmap, 535 .get_parent = clk_dyn_rcg_get_parent, 536 .set_parent = clk_dyn_rcg_set_parent, 537 .recalc_rate = clk_dyn_rcg_recalc_rate, 538 .determine_rate = clk_dyn_rcg_determine_rate, 539 .set_rate = clk_dyn_rcg_set_rate, 540 .set_rate_and_parent = clk_dyn_rcg_set_rate_and_parent, 541 }; 542 EXPORT_SYMBOL_GPL(clk_dyn_rcg_ops); 543