1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 4 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> 5 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> 6 * 7 * Adjustable divider clock implementation 8 */ 9 10 #include <linux/clk-provider.h> 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include <linux/io.h> 15 #include <linux/err.h> 16 #include <linux/string.h> 17 #include <linux/log2.h> 18 #include <linux/overflow.h> 19 20 /* 21 * DOC: basic adjustable divider clock that cannot gate 22 * 23 * Traits of this clock: 24 * prepare - clk_prepare only ensures that parents are prepared 25 * enable - clk_enable only ensures that parents are enabled 26 * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor) 27 * parent - fixed parent. No clk_set_parent support 28 */ 29 30 static inline u32 clk_div_readl(struct clk_divider *divider) 31 { 32 if (divider->flags & CLK_DIVIDER_BIG_ENDIAN) 33 return ioread32be(divider->reg); 34 35 return readl(divider->reg); 36 } 37 38 static inline void clk_div_writel(struct clk_divider *divider, u32 val) 39 { 40 if (divider->flags & CLK_DIVIDER_BIG_ENDIAN) 41 iowrite32be(val, divider->reg); 42 else 43 writel(val, divider->reg); 44 } 45 46 static unsigned int _get_table_maxdiv(const struct clk_div_table *table, 47 u8 width) 48 { 49 unsigned int maxdiv = 0, mask = clk_div_mask(width); 50 const struct clk_div_table *clkt; 51 52 for (clkt = table; clkt->div; clkt++) 53 if (clkt->div > maxdiv && clkt->val <= mask) 54 maxdiv = clkt->div; 55 return maxdiv; 56 } 57 58 static unsigned int _get_table_mindiv(const struct clk_div_table *table) 59 { 60 unsigned int mindiv = UINT_MAX; 61 const struct clk_div_table *clkt; 62 63 for (clkt = table; clkt->div; clkt++) 64 if (clkt->div < mindiv) 65 mindiv = clkt->div; 66 return mindiv; 67 } 68 69 static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, 70 unsigned long flags) 71 { 72 if (flags & CLK_DIVIDER_ONE_BASED) 73 return clk_div_mask(width); 74 if (flags & CLK_DIVIDER_POWER_OF_TWO) 75 return 1 << clk_div_mask(width); 76 if (flags & CLK_DIVIDER_EVEN_INTEGERS) 77 return 2 * (clk_div_mask(width) + 1); 78 if (table) 79 return _get_table_maxdiv(table, width); 80 return clk_div_mask(width) + 1; 81 } 82 83 static unsigned int _get_table_div(const struct clk_div_table *table, 84 unsigned int val) 85 { 86 const struct clk_div_table *clkt; 87 88 for (clkt = table; clkt->div; clkt++) 89 if (clkt->val == val) 90 return clkt->div; 91 return 0; 92 } 93 94 static unsigned int _get_div(const struct clk_div_table *table, 95 unsigned int val, unsigned long flags, u8 width) 96 { 97 if (flags & CLK_DIVIDER_ONE_BASED) 98 return val; 99 if (flags & CLK_DIVIDER_POWER_OF_TWO) 100 return 1 << val; 101 if (flags & CLK_DIVIDER_MAX_AT_ZERO) 102 return val ? val : clk_div_mask(width) + 1; 103 if (flags & CLK_DIVIDER_EVEN_INTEGERS) 104 return 2 * (val + 1); 105 if (table) 106 return _get_table_div(table, val); 107 return val + 1; 108 } 109 110 static unsigned int _get_table_val(const struct clk_div_table *table, 111 unsigned int div) 112 { 113 const struct clk_div_table *clkt; 114 115 for (clkt = table; clkt->div; clkt++) 116 if (clkt->div == div) 117 return clkt->val; 118 return 0; 119 } 120 121 static unsigned int _get_val(const struct clk_div_table *table, 122 unsigned int div, unsigned long flags, u8 width) 123 { 124 if (flags & CLK_DIVIDER_ONE_BASED) 125 return div; 126 if (flags & CLK_DIVIDER_POWER_OF_TWO) 127 return __ffs(div); 128 if (flags & CLK_DIVIDER_MAX_AT_ZERO) 129 return (div == clk_div_mask(width) + 1) ? 0 : div; 130 if (flags & CLK_DIVIDER_EVEN_INTEGERS) 131 return (div >> 1) - 1; 132 if (table) 133 return _get_table_val(table, div); 134 return div - 1; 135 } 136 137 unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, 138 unsigned int val, 139 const struct clk_div_table *table, 140 unsigned long flags, unsigned long width) 141 { 142 unsigned int div; 143 144 div = _get_div(table, val, flags, width); 145 if (!div) { 146 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), 147 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 148 clk_hw_get_name(hw)); 149 return parent_rate; 150 } 151 152 return DIV_ROUND_UP_ULL((u64)parent_rate, div); 153 } 154 EXPORT_SYMBOL_GPL(divider_recalc_rate); 155 156 static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, 157 unsigned long parent_rate) 158 { 159 struct clk_divider *divider = to_clk_divider(hw); 160 unsigned int val; 161 162 val = clk_div_readl(divider) >> divider->shift; 163 val &= clk_div_mask(divider->width); 164 165 return divider_recalc_rate(hw, parent_rate, val, divider->table, 166 divider->flags, divider->width); 167 } 168 169 static bool _is_valid_table_div(const struct clk_div_table *table, 170 unsigned int div) 171 { 172 const struct clk_div_table *clkt; 173 174 for (clkt = table; clkt->div; clkt++) 175 if (clkt->div == div) 176 return true; 177 return false; 178 } 179 180 static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, 181 unsigned long flags) 182 { 183 if (flags & CLK_DIVIDER_POWER_OF_TWO) 184 return is_power_of_2(div); 185 if (table) 186 return _is_valid_table_div(table, div); 187 return true; 188 } 189 190 static int _round_up_table(const struct clk_div_table *table, int div) 191 { 192 const struct clk_div_table *clkt; 193 int up = INT_MAX; 194 195 for (clkt = table; clkt->div; clkt++) { 196 if (clkt->div == div) 197 return clkt->div; 198 else if (clkt->div < div) 199 continue; 200 201 if ((clkt->div - div) < (up - div)) 202 up = clkt->div; 203 } 204 205 return up; 206 } 207 208 static int _round_down_table(const struct clk_div_table *table, int div) 209 { 210 const struct clk_div_table *clkt; 211 int down = _get_table_mindiv(table); 212 213 for (clkt = table; clkt->div; clkt++) { 214 if (clkt->div == div) 215 return clkt->div; 216 else if (clkt->div > div) 217 continue; 218 219 if ((div - clkt->div) < (div - down)) 220 down = clkt->div; 221 } 222 223 return down; 224 } 225 226 static int _div_round_up(const struct clk_div_table *table, 227 unsigned long parent_rate, unsigned long rate, 228 unsigned long flags) 229 { 230 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 231 232 if (flags & CLK_DIVIDER_POWER_OF_TWO) 233 div = __roundup_pow_of_two(div); 234 if (table) 235 div = _round_up_table(table, div); 236 237 return div; 238 } 239 240 static int _div_round_closest(const struct clk_div_table *table, 241 unsigned long parent_rate, unsigned long rate, 242 unsigned long flags) 243 { 244 int up, down; 245 unsigned long up_rate, down_rate; 246 247 up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 248 down = parent_rate / rate; 249 250 if (flags & CLK_DIVIDER_POWER_OF_TWO) { 251 up = __roundup_pow_of_two(up); 252 down = __rounddown_pow_of_two(down); 253 } else if (table) { 254 up = _round_up_table(table, up); 255 down = _round_down_table(table, down); 256 } 257 258 up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); 259 down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); 260 261 return (rate - up_rate) <= (down_rate - rate) ? up : down; 262 } 263 264 static int _div_round(const struct clk_div_table *table, 265 unsigned long parent_rate, unsigned long rate, 266 unsigned long flags) 267 { 268 if (flags & CLK_DIVIDER_ROUND_CLOSEST) 269 return _div_round_closest(table, parent_rate, rate, flags); 270 271 return _div_round_up(table, parent_rate, rate, flags); 272 } 273 274 static bool _is_best_div(unsigned long rate, unsigned long now, 275 unsigned long best, unsigned long flags) 276 { 277 if (flags & CLK_DIVIDER_ROUND_CLOSEST) 278 return abs(rate - now) < abs(rate - best); 279 280 return now <= rate && now > best; 281 } 282 283 static int _next_div(const struct clk_div_table *table, int div, 284 unsigned long flags) 285 { 286 div++; 287 288 if (flags & CLK_DIVIDER_POWER_OF_TWO) 289 return __roundup_pow_of_two(div); 290 if (table) 291 return _round_up_table(table, div); 292 293 return div; 294 } 295 296 static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent, 297 unsigned long rate, 298 unsigned long *best_parent_rate, 299 const struct clk_div_table *table, u8 width, 300 unsigned long flags) 301 { 302 int i, bestdiv = 0; 303 unsigned long parent_rate, best = 0, now, maxdiv; 304 unsigned long parent_rate_saved = *best_parent_rate; 305 unsigned long target_parent_rate; 306 307 if (!rate) 308 rate = 1; 309 310 maxdiv = _get_maxdiv(table, width, flags); 311 312 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { 313 parent_rate = *best_parent_rate; 314 bestdiv = _div_round(table, parent_rate, rate, flags); 315 bestdiv = bestdiv == 0 ? 1 : bestdiv; 316 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; 317 return bestdiv; 318 } 319 320 for (i = _next_div(table, 0, flags); i <= maxdiv; 321 i = _next_div(table, i, flags)) { 322 bool overflow = check_mul_overflow(rate, (unsigned long)i, &target_parent_rate); 323 324 if (!overflow && target_parent_rate == parent_rate_saved) { 325 /* 326 * It's the most ideal case if the requested rate can be 327 * divided from parent clock without needing to change 328 * parent rate, so return the divider immediately. 329 */ 330 *best_parent_rate = parent_rate_saved; 331 return i; 332 } 333 /* 334 * Clamp target_parent_rate to ULONG_MAX on overflow. The true 335 * required parent rate exceeds what can be represented, so ask 336 * the parent for the highest rate it can produce. There is no 337 * point continuing the loop past this since larger dividers 338 * only move further from the requested rate. 339 */ 340 if (overflow) 341 target_parent_rate = ULONG_MAX; 342 parent_rate = clk_hw_round_rate(parent, target_parent_rate); 343 now = DIV_ROUND_UP_ULL((u64)parent_rate, i); 344 if (_is_best_div(rate, now, best, flags)) { 345 bestdiv = i; 346 best = now; 347 *best_parent_rate = parent_rate; 348 } 349 if (overflow) 350 break; 351 } 352 353 if (!bestdiv) { 354 bestdiv = _get_maxdiv(table, width, flags); 355 *best_parent_rate = clk_hw_round_rate(parent, 1); 356 } 357 358 return bestdiv; 359 } 360 361 int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req, 362 const struct clk_div_table *table, u8 width, 363 unsigned long flags) 364 { 365 int div; 366 367 div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate, 368 &req->best_parent_rate, table, width, flags); 369 370 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div); 371 372 return 0; 373 } 374 EXPORT_SYMBOL_GPL(divider_determine_rate); 375 376 int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req, 377 const struct clk_div_table *table, u8 width, 378 unsigned long flags, unsigned int val) 379 { 380 int div; 381 382 div = _get_div(table, val, flags, width); 383 384 /* Even a read-only clock can propagate a rate change */ 385 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) { 386 if (!req->best_parent_hw) 387 return -EINVAL; 388 389 req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw, 390 req->rate * div); 391 } 392 393 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div); 394 395 return 0; 396 } 397 EXPORT_SYMBOL_GPL(divider_ro_determine_rate); 398 399 static int clk_divider_determine_rate(struct clk_hw *hw, 400 struct clk_rate_request *req) 401 { 402 struct clk_divider *divider = to_clk_divider(hw); 403 404 /* if read only, just return current value */ 405 if (divider->flags & CLK_DIVIDER_READ_ONLY) { 406 u32 val; 407 408 val = clk_div_readl(divider) >> divider->shift; 409 val &= clk_div_mask(divider->width); 410 411 return divider_ro_determine_rate(hw, req, divider->table, 412 divider->width, 413 divider->flags, val); 414 } 415 416 return divider_determine_rate(hw, req, divider->table, divider->width, 417 divider->flags); 418 } 419 420 int divider_get_val(unsigned long rate, unsigned long parent_rate, 421 const struct clk_div_table *table, u8 width, 422 unsigned long flags) 423 { 424 unsigned int div, value; 425 426 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 427 428 if (!_is_valid_div(table, div, flags)) 429 return -EINVAL; 430 431 value = _get_val(table, div, flags, width); 432 433 return min_t(unsigned int, value, clk_div_mask(width)); 434 } 435 EXPORT_SYMBOL_GPL(divider_get_val); 436 437 static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 438 unsigned long parent_rate) 439 { 440 struct clk_divider *divider = to_clk_divider(hw); 441 int value; 442 unsigned long flags = 0; 443 u32 val; 444 445 value = divider_get_val(rate, parent_rate, divider->table, 446 divider->width, divider->flags); 447 if (value < 0) 448 return value; 449 450 if (divider->lock) 451 spin_lock_irqsave(divider->lock, flags); 452 else 453 __acquire(divider->lock); 454 455 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 456 val = clk_div_mask(divider->width) << (divider->shift + 16); 457 } else { 458 val = clk_div_readl(divider); 459 val &= ~(clk_div_mask(divider->width) << divider->shift); 460 } 461 val |= (u32)value << divider->shift; 462 clk_div_writel(divider, val); 463 464 if (divider->lock) 465 spin_unlock_irqrestore(divider->lock, flags); 466 else 467 __release(divider->lock); 468 469 return 0; 470 } 471 472 const struct clk_ops clk_divider_ops = { 473 .recalc_rate = clk_divider_recalc_rate, 474 .determine_rate = clk_divider_determine_rate, 475 .set_rate = clk_divider_set_rate, 476 }; 477 EXPORT_SYMBOL_GPL(clk_divider_ops); 478 479 const struct clk_ops clk_divider_ro_ops = { 480 .recalc_rate = clk_divider_recalc_rate, 481 .determine_rate = clk_divider_determine_rate, 482 }; 483 EXPORT_SYMBOL_GPL(clk_divider_ro_ops); 484 485 struct clk_hw *__clk_hw_register_divider(struct device *dev, 486 struct device_node *np, const char *name, 487 const char *parent_name, const struct clk_hw *parent_hw, 488 const struct clk_parent_data *parent_data, unsigned long flags, 489 void __iomem *reg, u8 shift, u8 width, 490 unsigned long clk_divider_flags, 491 const struct clk_div_table *table, spinlock_t *lock) 492 { 493 struct clk_divider *div; 494 struct clk_hw *hw; 495 struct clk_init_data init = {}; 496 int ret; 497 498 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 499 if (width + shift > 16) { 500 pr_warn("divider value exceeds LOWORD field\n"); 501 return ERR_PTR(-EINVAL); 502 } 503 } 504 505 /* allocate the divider */ 506 div = kzalloc_obj(*div); 507 if (!div) 508 return ERR_PTR(-ENOMEM); 509 510 init.name = name; 511 if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) 512 init.ops = &clk_divider_ro_ops; 513 else 514 init.ops = &clk_divider_ops; 515 init.flags = flags; 516 init.parent_names = parent_name ? &parent_name : NULL; 517 init.parent_hws = parent_hw ? &parent_hw : NULL; 518 init.parent_data = parent_data; 519 if (parent_name || parent_hw || parent_data) 520 init.num_parents = 1; 521 else 522 init.num_parents = 0; 523 524 /* struct clk_divider assignments */ 525 div->reg = reg; 526 div->shift = shift; 527 div->width = width; 528 div->flags = clk_divider_flags; 529 div->lock = lock; 530 div->hw.init = &init; 531 div->table = table; 532 533 /* register the clock */ 534 hw = &div->hw; 535 ret = clk_hw_register(dev, hw); 536 if (ret) { 537 kfree(div); 538 hw = ERR_PTR(ret); 539 } 540 541 return hw; 542 } 543 EXPORT_SYMBOL_GPL(__clk_hw_register_divider); 544 545 /** 546 * clk_register_divider_table - register a table based divider clock with 547 * the clock framework 548 * @dev: device registering this clock 549 * @name: name of this clock 550 * @parent_name: name of clock's parent 551 * @flags: framework-specific flags 552 * @reg: register address to adjust divider 553 * @shift: number of bits to shift the bitfield 554 * @width: width of the bitfield 555 * @clk_divider_flags: divider-specific flags for this clock 556 * @table: array of divider/value pairs ending with a div set to 0 557 * @lock: shared register lock for this clock 558 */ 559 struct clk *clk_register_divider_table(struct device *dev, const char *name, 560 const char *parent_name, unsigned long flags, 561 void __iomem *reg, u8 shift, u8 width, 562 unsigned long clk_divider_flags, 563 const struct clk_div_table *table, spinlock_t *lock) 564 { 565 struct clk_hw *hw; 566 567 hw = __clk_hw_register_divider(dev, NULL, name, parent_name, NULL, 568 NULL, flags, reg, shift, width, clk_divider_flags, 569 table, lock); 570 if (IS_ERR(hw)) 571 return ERR_CAST(hw); 572 return hw->clk; 573 } 574 EXPORT_SYMBOL_GPL(clk_register_divider_table); 575 576 void clk_unregister_divider(struct clk *clk) 577 { 578 struct clk_divider *div; 579 struct clk_hw *hw; 580 581 hw = __clk_get_hw(clk); 582 if (!hw) 583 return; 584 585 div = to_clk_divider(hw); 586 587 clk_unregister(clk); 588 kfree(div); 589 } 590 EXPORT_SYMBOL_GPL(clk_unregister_divider); 591 592 /** 593 * clk_hw_unregister_divider - unregister a clk divider 594 * @hw: hardware-specific clock data to unregister 595 */ 596 void clk_hw_unregister_divider(struct clk_hw *hw) 597 { 598 struct clk_divider *div; 599 600 div = to_clk_divider(hw); 601 602 clk_hw_unregister(hw); 603 kfree(div); 604 } 605 EXPORT_SYMBOL_GPL(clk_hw_unregister_divider); 606 607 static void devm_clk_hw_release_divider(struct device *dev, void *res) 608 { 609 clk_hw_unregister_divider(*(struct clk_hw **)res); 610 } 611 612 struct clk_hw *__devm_clk_hw_register_divider(struct device *dev, 613 struct device_node *np, const char *name, 614 const char *parent_name, const struct clk_hw *parent_hw, 615 const struct clk_parent_data *parent_data, unsigned long flags, 616 void __iomem *reg, u8 shift, u8 width, 617 unsigned long clk_divider_flags, 618 const struct clk_div_table *table, spinlock_t *lock) 619 { 620 struct clk_hw **ptr, *hw; 621 622 ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL); 623 if (!ptr) 624 return ERR_PTR(-ENOMEM); 625 626 hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw, 627 parent_data, flags, reg, shift, width, 628 clk_divider_flags, table, lock); 629 630 if (!IS_ERR(hw)) { 631 *ptr = hw; 632 devres_add(dev, ptr); 633 } else { 634 devres_free(ptr); 635 } 636 637 return hw; 638 } 639 EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider); 640