1 /* 2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> 3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst 10 */ 11 12 #include <linux/clk.h> 13 #include <linux/clk-provider.h> 14 #include <linux/clk/clk-conf.h> 15 #include <linux/module.h> 16 #include <linux/mutex.h> 17 #include <linux/spinlock.h> 18 #include <linux/err.h> 19 #include <linux/list.h> 20 #include <linux/slab.h> 21 #include <linux/of.h> 22 #include <linux/device.h> 23 #include <linux/init.h> 24 #include <linux/pm_runtime.h> 25 #include <linux/sched.h> 26 #include <linux/clkdev.h> 27 28 #include "clk.h" 29 30 static DEFINE_SPINLOCK(enable_lock); 31 static DEFINE_MUTEX(prepare_lock); 32 33 static struct task_struct *prepare_owner; 34 static struct task_struct *enable_owner; 35 36 static int prepare_refcnt; 37 static int enable_refcnt; 38 39 static HLIST_HEAD(clk_root_list); 40 static HLIST_HEAD(clk_orphan_list); 41 static LIST_HEAD(clk_notifier_list); 42 43 /*** private data structures ***/ 44 45 struct clk_core { 46 const char *name; 47 const struct clk_ops *ops; 48 struct clk_hw *hw; 49 struct module *owner; 50 struct device *dev; 51 struct clk_core *parent; 52 const char **parent_names; 53 struct clk_core **parents; 54 u8 num_parents; 55 u8 new_parent_index; 56 unsigned long rate; 57 unsigned long req_rate; 58 unsigned long new_rate; 59 struct clk_core *new_parent; 60 struct clk_core *new_child; 61 unsigned long flags; 62 bool orphan; 63 unsigned int enable_count; 64 unsigned int prepare_count; 65 unsigned int protect_count; 66 unsigned long min_rate; 67 unsigned long max_rate; 68 unsigned long accuracy; 69 int phase; 70 struct clk_duty duty; 71 struct hlist_head children; 72 struct hlist_node child_node; 73 struct hlist_head clks; 74 unsigned int notifier_count; 75 #ifdef CONFIG_DEBUG_FS 76 struct dentry *dentry; 77 struct hlist_node debug_node; 78 #endif 79 struct kref ref; 80 }; 81 82 #define CREATE_TRACE_POINTS 83 #include <trace/events/clk.h> 84 85 struct clk { 86 struct clk_core *core; 87 const char *dev_id; 88 const char *con_id; 89 unsigned long min_rate; 90 unsigned long max_rate; 91 unsigned int exclusive_count; 92 struct hlist_node clks_node; 93 }; 94 95 /*** runtime pm ***/ 96 static int clk_pm_runtime_get(struct clk_core *core) 97 { 98 int ret = 0; 99 100 if (!core->dev) 101 return 0; 102 103 ret = pm_runtime_get_sync(core->dev); 104 return ret < 0 ? ret : 0; 105 } 106 107 static void clk_pm_runtime_put(struct clk_core *core) 108 { 109 if (!core->dev) 110 return; 111 112 pm_runtime_put_sync(core->dev); 113 } 114 115 /*** locking ***/ 116 static void clk_prepare_lock(void) 117 { 118 if (!mutex_trylock(&prepare_lock)) { 119 if (prepare_owner == current) { 120 prepare_refcnt++; 121 return; 122 } 123 mutex_lock(&prepare_lock); 124 } 125 WARN_ON_ONCE(prepare_owner != NULL); 126 WARN_ON_ONCE(prepare_refcnt != 0); 127 prepare_owner = current; 128 prepare_refcnt = 1; 129 } 130 131 static void clk_prepare_unlock(void) 132 { 133 WARN_ON_ONCE(prepare_owner != current); 134 WARN_ON_ONCE(prepare_refcnt == 0); 135 136 if (--prepare_refcnt) 137 return; 138 prepare_owner = NULL; 139 mutex_unlock(&prepare_lock); 140 } 141 142 static unsigned long clk_enable_lock(void) 143 __acquires(enable_lock) 144 { 145 unsigned long flags; 146 147 /* 148 * On UP systems, spin_trylock_irqsave() always returns true, even if 149 * we already hold the lock. So, in that case, we rely only on 150 * reference counting. 151 */ 152 if (!IS_ENABLED(CONFIG_SMP) || 153 !spin_trylock_irqsave(&enable_lock, flags)) { 154 if (enable_owner == current) { 155 enable_refcnt++; 156 __acquire(enable_lock); 157 if (!IS_ENABLED(CONFIG_SMP)) 158 local_save_flags(flags); 159 return flags; 160 } 161 spin_lock_irqsave(&enable_lock, flags); 162 } 163 WARN_ON_ONCE(enable_owner != NULL); 164 WARN_ON_ONCE(enable_refcnt != 0); 165 enable_owner = current; 166 enable_refcnt = 1; 167 return flags; 168 } 169 170 static void clk_enable_unlock(unsigned long flags) 171 __releases(enable_lock) 172 { 173 WARN_ON_ONCE(enable_owner != current); 174 WARN_ON_ONCE(enable_refcnt == 0); 175 176 if (--enable_refcnt) { 177 __release(enable_lock); 178 return; 179 } 180 enable_owner = NULL; 181 spin_unlock_irqrestore(&enable_lock, flags); 182 } 183 184 static bool clk_core_rate_is_protected(struct clk_core *core) 185 { 186 return core->protect_count; 187 } 188 189 static bool clk_core_is_prepared(struct clk_core *core) 190 { 191 bool ret = false; 192 193 /* 194 * .is_prepared is optional for clocks that can prepare 195 * fall back to software usage counter if it is missing 196 */ 197 if (!core->ops->is_prepared) 198 return core->prepare_count; 199 200 if (!clk_pm_runtime_get(core)) { 201 ret = core->ops->is_prepared(core->hw); 202 clk_pm_runtime_put(core); 203 } 204 205 return ret; 206 } 207 208 static bool clk_core_is_enabled(struct clk_core *core) 209 { 210 bool ret = false; 211 212 /* 213 * .is_enabled is only mandatory for clocks that gate 214 * fall back to software usage counter if .is_enabled is missing 215 */ 216 if (!core->ops->is_enabled) 217 return core->enable_count; 218 219 /* 220 * Check if clock controller's device is runtime active before 221 * calling .is_enabled callback. If not, assume that clock is 222 * disabled, because we might be called from atomic context, from 223 * which pm_runtime_get() is not allowed. 224 * This function is called mainly from clk_disable_unused_subtree, 225 * which ensures proper runtime pm activation of controller before 226 * taking enable spinlock, but the below check is needed if one tries 227 * to call it from other places. 228 */ 229 if (core->dev) { 230 pm_runtime_get_noresume(core->dev); 231 if (!pm_runtime_active(core->dev)) { 232 ret = false; 233 goto done; 234 } 235 } 236 237 ret = core->ops->is_enabled(core->hw); 238 done: 239 if (core->dev) 240 pm_runtime_put(core->dev); 241 242 return ret; 243 } 244 245 /*** helper functions ***/ 246 247 const char *__clk_get_name(const struct clk *clk) 248 { 249 return !clk ? NULL : clk->core->name; 250 } 251 EXPORT_SYMBOL_GPL(__clk_get_name); 252 253 const char *clk_hw_get_name(const struct clk_hw *hw) 254 { 255 return hw->core->name; 256 } 257 EXPORT_SYMBOL_GPL(clk_hw_get_name); 258 259 struct clk_hw *__clk_get_hw(struct clk *clk) 260 { 261 return !clk ? NULL : clk->core->hw; 262 } 263 EXPORT_SYMBOL_GPL(__clk_get_hw); 264 265 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw) 266 { 267 return hw->core->num_parents; 268 } 269 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents); 270 271 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw) 272 { 273 return hw->core->parent ? hw->core->parent->hw : NULL; 274 } 275 EXPORT_SYMBOL_GPL(clk_hw_get_parent); 276 277 static struct clk_core *__clk_lookup_subtree(const char *name, 278 struct clk_core *core) 279 { 280 struct clk_core *child; 281 struct clk_core *ret; 282 283 if (!strcmp(core->name, name)) 284 return core; 285 286 hlist_for_each_entry(child, &core->children, child_node) { 287 ret = __clk_lookup_subtree(name, child); 288 if (ret) 289 return ret; 290 } 291 292 return NULL; 293 } 294 295 static struct clk_core *clk_core_lookup(const char *name) 296 { 297 struct clk_core *root_clk; 298 struct clk_core *ret; 299 300 if (!name) 301 return NULL; 302 303 /* search the 'proper' clk tree first */ 304 hlist_for_each_entry(root_clk, &clk_root_list, child_node) { 305 ret = __clk_lookup_subtree(name, root_clk); 306 if (ret) 307 return ret; 308 } 309 310 /* if not found, then search the orphan tree */ 311 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { 312 ret = __clk_lookup_subtree(name, root_clk); 313 if (ret) 314 return ret; 315 } 316 317 return NULL; 318 } 319 320 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core, 321 u8 index) 322 { 323 if (!core || index >= core->num_parents) 324 return NULL; 325 326 if (!core->parents[index]) 327 core->parents[index] = 328 clk_core_lookup(core->parent_names[index]); 329 330 return core->parents[index]; 331 } 332 333 struct clk_hw * 334 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index) 335 { 336 struct clk_core *parent; 337 338 parent = clk_core_get_parent_by_index(hw->core, index); 339 340 return !parent ? NULL : parent->hw; 341 } 342 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index); 343 344 unsigned int __clk_get_enable_count(struct clk *clk) 345 { 346 return !clk ? 0 : clk->core->enable_count; 347 } 348 349 static unsigned long clk_core_get_rate_nolock(struct clk_core *core) 350 { 351 unsigned long ret; 352 353 if (!core) { 354 ret = 0; 355 goto out; 356 } 357 358 ret = core->rate; 359 360 if (!core->num_parents) 361 goto out; 362 363 if (!core->parent) 364 ret = 0; 365 366 out: 367 return ret; 368 } 369 370 unsigned long clk_hw_get_rate(const struct clk_hw *hw) 371 { 372 return clk_core_get_rate_nolock(hw->core); 373 } 374 EXPORT_SYMBOL_GPL(clk_hw_get_rate); 375 376 static unsigned long __clk_get_accuracy(struct clk_core *core) 377 { 378 if (!core) 379 return 0; 380 381 return core->accuracy; 382 } 383 384 unsigned long __clk_get_flags(struct clk *clk) 385 { 386 return !clk ? 0 : clk->core->flags; 387 } 388 EXPORT_SYMBOL_GPL(__clk_get_flags); 389 390 unsigned long clk_hw_get_flags(const struct clk_hw *hw) 391 { 392 return hw->core->flags; 393 } 394 EXPORT_SYMBOL_GPL(clk_hw_get_flags); 395 396 bool clk_hw_is_prepared(const struct clk_hw *hw) 397 { 398 return clk_core_is_prepared(hw->core); 399 } 400 401 bool clk_hw_rate_is_protected(const struct clk_hw *hw) 402 { 403 return clk_core_rate_is_protected(hw->core); 404 } 405 406 bool clk_hw_is_enabled(const struct clk_hw *hw) 407 { 408 return clk_core_is_enabled(hw->core); 409 } 410 411 bool __clk_is_enabled(struct clk *clk) 412 { 413 if (!clk) 414 return false; 415 416 return clk_core_is_enabled(clk->core); 417 } 418 EXPORT_SYMBOL_GPL(__clk_is_enabled); 419 420 static bool mux_is_better_rate(unsigned long rate, unsigned long now, 421 unsigned long best, unsigned long flags) 422 { 423 if (flags & CLK_MUX_ROUND_CLOSEST) 424 return abs(now - rate) < abs(best - rate); 425 426 return now <= rate && now > best; 427 } 428 429 int clk_mux_determine_rate_flags(struct clk_hw *hw, 430 struct clk_rate_request *req, 431 unsigned long flags) 432 { 433 struct clk_core *core = hw->core, *parent, *best_parent = NULL; 434 int i, num_parents, ret; 435 unsigned long best = 0; 436 struct clk_rate_request parent_req = *req; 437 438 /* if NO_REPARENT flag set, pass through to current parent */ 439 if (core->flags & CLK_SET_RATE_NO_REPARENT) { 440 parent = core->parent; 441 if (core->flags & CLK_SET_RATE_PARENT) { 442 ret = __clk_determine_rate(parent ? parent->hw : NULL, 443 &parent_req); 444 if (ret) 445 return ret; 446 447 best = parent_req.rate; 448 } else if (parent) { 449 best = clk_core_get_rate_nolock(parent); 450 } else { 451 best = clk_core_get_rate_nolock(core); 452 } 453 454 goto out; 455 } 456 457 /* find the parent that can provide the fastest rate <= rate */ 458 num_parents = core->num_parents; 459 for (i = 0; i < num_parents; i++) { 460 parent = clk_core_get_parent_by_index(core, i); 461 if (!parent) 462 continue; 463 464 if (core->flags & CLK_SET_RATE_PARENT) { 465 parent_req = *req; 466 ret = __clk_determine_rate(parent->hw, &parent_req); 467 if (ret) 468 continue; 469 } else { 470 parent_req.rate = clk_core_get_rate_nolock(parent); 471 } 472 473 if (mux_is_better_rate(req->rate, parent_req.rate, 474 best, flags)) { 475 best_parent = parent; 476 best = parent_req.rate; 477 } 478 } 479 480 if (!best_parent) 481 return -EINVAL; 482 483 out: 484 if (best_parent) 485 req->best_parent_hw = best_parent->hw; 486 req->best_parent_rate = best; 487 req->rate = best; 488 489 return 0; 490 } 491 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags); 492 493 struct clk *__clk_lookup(const char *name) 494 { 495 struct clk_core *core = clk_core_lookup(name); 496 497 return !core ? NULL : core->hw->clk; 498 } 499 500 static void clk_core_get_boundaries(struct clk_core *core, 501 unsigned long *min_rate, 502 unsigned long *max_rate) 503 { 504 struct clk *clk_user; 505 506 *min_rate = core->min_rate; 507 *max_rate = core->max_rate; 508 509 hlist_for_each_entry(clk_user, &core->clks, clks_node) 510 *min_rate = max(*min_rate, clk_user->min_rate); 511 512 hlist_for_each_entry(clk_user, &core->clks, clks_node) 513 *max_rate = min(*max_rate, clk_user->max_rate); 514 } 515 516 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate, 517 unsigned long max_rate) 518 { 519 hw->core->min_rate = min_rate; 520 hw->core->max_rate = max_rate; 521 } 522 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range); 523 524 /* 525 * Helper for finding best parent to provide a given frequency. This can be used 526 * directly as a determine_rate callback (e.g. for a mux), or from a more 527 * complex clock that may combine a mux with other operations. 528 */ 529 int __clk_mux_determine_rate(struct clk_hw *hw, 530 struct clk_rate_request *req) 531 { 532 return clk_mux_determine_rate_flags(hw, req, 0); 533 } 534 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate); 535 536 int __clk_mux_determine_rate_closest(struct clk_hw *hw, 537 struct clk_rate_request *req) 538 { 539 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST); 540 } 541 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest); 542 543 /*** clk api ***/ 544 545 static void clk_core_rate_unprotect(struct clk_core *core) 546 { 547 lockdep_assert_held(&prepare_lock); 548 549 if (!core) 550 return; 551 552 if (WARN(core->protect_count == 0, 553 "%s already unprotected\n", core->name)) 554 return; 555 556 if (--core->protect_count > 0) 557 return; 558 559 clk_core_rate_unprotect(core->parent); 560 } 561 562 static int clk_core_rate_nuke_protect(struct clk_core *core) 563 { 564 int ret; 565 566 lockdep_assert_held(&prepare_lock); 567 568 if (!core) 569 return -EINVAL; 570 571 if (core->protect_count == 0) 572 return 0; 573 574 ret = core->protect_count; 575 core->protect_count = 1; 576 clk_core_rate_unprotect(core); 577 578 return ret; 579 } 580 581 /** 582 * clk_rate_exclusive_put - release exclusivity over clock rate control 583 * @clk: the clk over which the exclusivity is released 584 * 585 * clk_rate_exclusive_put() completes a critical section during which a clock 586 * consumer cannot tolerate any other consumer making any operation on the 587 * clock which could result in a rate change or rate glitch. Exclusive clocks 588 * cannot have their rate changed, either directly or indirectly due to changes 589 * further up the parent chain of clocks. As a result, clocks up parent chain 590 * also get under exclusive control of the calling consumer. 591 * 592 * If exlusivity is claimed more than once on clock, even by the same consumer, 593 * the rate effectively gets locked as exclusivity can't be preempted. 594 * 595 * Calls to clk_rate_exclusive_put() must be balanced with calls to 596 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return 597 * error status. 598 */ 599 void clk_rate_exclusive_put(struct clk *clk) 600 { 601 if (!clk) 602 return; 603 604 clk_prepare_lock(); 605 606 /* 607 * if there is something wrong with this consumer protect count, stop 608 * here before messing with the provider 609 */ 610 if (WARN_ON(clk->exclusive_count <= 0)) 611 goto out; 612 613 clk_core_rate_unprotect(clk->core); 614 clk->exclusive_count--; 615 out: 616 clk_prepare_unlock(); 617 } 618 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put); 619 620 static void clk_core_rate_protect(struct clk_core *core) 621 { 622 lockdep_assert_held(&prepare_lock); 623 624 if (!core) 625 return; 626 627 if (core->protect_count == 0) 628 clk_core_rate_protect(core->parent); 629 630 core->protect_count++; 631 } 632 633 static void clk_core_rate_restore_protect(struct clk_core *core, int count) 634 { 635 lockdep_assert_held(&prepare_lock); 636 637 if (!core) 638 return; 639 640 if (count == 0) 641 return; 642 643 clk_core_rate_protect(core); 644 core->protect_count = count; 645 } 646 647 /** 648 * clk_rate_exclusive_get - get exclusivity over the clk rate control 649 * @clk: the clk over which the exclusity of rate control is requested 650 * 651 * clk_rate_exlusive_get() begins a critical section during which a clock 652 * consumer cannot tolerate any other consumer making any operation on the 653 * clock which could result in a rate change or rate glitch. Exclusive clocks 654 * cannot have their rate changed, either directly or indirectly due to changes 655 * further up the parent chain of clocks. As a result, clocks up parent chain 656 * also get under exclusive control of the calling consumer. 657 * 658 * If exlusivity is claimed more than once on clock, even by the same consumer, 659 * the rate effectively gets locked as exclusivity can't be preempted. 660 * 661 * Calls to clk_rate_exclusive_get() should be balanced with calls to 662 * clk_rate_exclusive_put(). Calls to this function may sleep. 663 * Returns 0 on success, -EERROR otherwise 664 */ 665 int clk_rate_exclusive_get(struct clk *clk) 666 { 667 if (!clk) 668 return 0; 669 670 clk_prepare_lock(); 671 clk_core_rate_protect(clk->core); 672 clk->exclusive_count++; 673 clk_prepare_unlock(); 674 675 return 0; 676 } 677 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get); 678 679 static void clk_core_unprepare(struct clk_core *core) 680 { 681 lockdep_assert_held(&prepare_lock); 682 683 if (!core) 684 return; 685 686 if (WARN(core->prepare_count == 0, 687 "%s already unprepared\n", core->name)) 688 return; 689 690 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL, 691 "Unpreparing critical %s\n", core->name)) 692 return; 693 694 if (--core->prepare_count > 0) 695 return; 696 697 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name); 698 699 trace_clk_unprepare(core); 700 701 if (core->ops->unprepare) 702 core->ops->unprepare(core->hw); 703 704 clk_pm_runtime_put(core); 705 706 trace_clk_unprepare_complete(core); 707 clk_core_unprepare(core->parent); 708 } 709 710 static void clk_core_unprepare_lock(struct clk_core *core) 711 { 712 clk_prepare_lock(); 713 clk_core_unprepare(core); 714 clk_prepare_unlock(); 715 } 716 717 /** 718 * clk_unprepare - undo preparation of a clock source 719 * @clk: the clk being unprepared 720 * 721 * clk_unprepare may sleep, which differentiates it from clk_disable. In a 722 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk 723 * if the operation may sleep. One example is a clk which is accessed over 724 * I2c. In the complex case a clk gate operation may require a fast and a slow 725 * part. It is this reason that clk_unprepare and clk_disable are not mutually 726 * exclusive. In fact clk_disable must be called before clk_unprepare. 727 */ 728 void clk_unprepare(struct clk *clk) 729 { 730 if (IS_ERR_OR_NULL(clk)) 731 return; 732 733 clk_core_unprepare_lock(clk->core); 734 } 735 EXPORT_SYMBOL_GPL(clk_unprepare); 736 737 static int clk_core_prepare(struct clk_core *core) 738 { 739 int ret = 0; 740 741 lockdep_assert_held(&prepare_lock); 742 743 if (!core) 744 return 0; 745 746 if (core->prepare_count == 0) { 747 ret = clk_pm_runtime_get(core); 748 if (ret) 749 return ret; 750 751 ret = clk_core_prepare(core->parent); 752 if (ret) 753 goto runtime_put; 754 755 trace_clk_prepare(core); 756 757 if (core->ops->prepare) 758 ret = core->ops->prepare(core->hw); 759 760 trace_clk_prepare_complete(core); 761 762 if (ret) 763 goto unprepare; 764 } 765 766 core->prepare_count++; 767 768 return 0; 769 unprepare: 770 clk_core_unprepare(core->parent); 771 runtime_put: 772 clk_pm_runtime_put(core); 773 return ret; 774 } 775 776 static int clk_core_prepare_lock(struct clk_core *core) 777 { 778 int ret; 779 780 clk_prepare_lock(); 781 ret = clk_core_prepare(core); 782 clk_prepare_unlock(); 783 784 return ret; 785 } 786 787 /** 788 * clk_prepare - prepare a clock source 789 * @clk: the clk being prepared 790 * 791 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple 792 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the 793 * operation may sleep. One example is a clk which is accessed over I2c. In 794 * the complex case a clk ungate operation may require a fast and a slow part. 795 * It is this reason that clk_prepare and clk_enable are not mutually 796 * exclusive. In fact clk_prepare must be called before clk_enable. 797 * Returns 0 on success, -EERROR otherwise. 798 */ 799 int clk_prepare(struct clk *clk) 800 { 801 if (!clk) 802 return 0; 803 804 return clk_core_prepare_lock(clk->core); 805 } 806 EXPORT_SYMBOL_GPL(clk_prepare); 807 808 static void clk_core_disable(struct clk_core *core) 809 { 810 lockdep_assert_held(&enable_lock); 811 812 if (!core) 813 return; 814 815 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name)) 816 return; 817 818 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL, 819 "Disabling critical %s\n", core->name)) 820 return; 821 822 if (--core->enable_count > 0) 823 return; 824 825 trace_clk_disable_rcuidle(core); 826 827 if (core->ops->disable) 828 core->ops->disable(core->hw); 829 830 trace_clk_disable_complete_rcuidle(core); 831 832 clk_core_disable(core->parent); 833 } 834 835 static void clk_core_disable_lock(struct clk_core *core) 836 { 837 unsigned long flags; 838 839 flags = clk_enable_lock(); 840 clk_core_disable(core); 841 clk_enable_unlock(flags); 842 } 843 844 /** 845 * clk_disable - gate a clock 846 * @clk: the clk being gated 847 * 848 * clk_disable must not sleep, which differentiates it from clk_unprepare. In 849 * a simple case, clk_disable can be used instead of clk_unprepare to gate a 850 * clk if the operation is fast and will never sleep. One example is a 851 * SoC-internal clk which is controlled via simple register writes. In the 852 * complex case a clk gate operation may require a fast and a slow part. It is 853 * this reason that clk_unprepare and clk_disable are not mutually exclusive. 854 * In fact clk_disable must be called before clk_unprepare. 855 */ 856 void clk_disable(struct clk *clk) 857 { 858 if (IS_ERR_OR_NULL(clk)) 859 return; 860 861 clk_core_disable_lock(clk->core); 862 } 863 EXPORT_SYMBOL_GPL(clk_disable); 864 865 static int clk_core_enable(struct clk_core *core) 866 { 867 int ret = 0; 868 869 lockdep_assert_held(&enable_lock); 870 871 if (!core) 872 return 0; 873 874 if (WARN(core->prepare_count == 0, 875 "Enabling unprepared %s\n", core->name)) 876 return -ESHUTDOWN; 877 878 if (core->enable_count == 0) { 879 ret = clk_core_enable(core->parent); 880 881 if (ret) 882 return ret; 883 884 trace_clk_enable_rcuidle(core); 885 886 if (core->ops->enable) 887 ret = core->ops->enable(core->hw); 888 889 trace_clk_enable_complete_rcuidle(core); 890 891 if (ret) { 892 clk_core_disable(core->parent); 893 return ret; 894 } 895 } 896 897 core->enable_count++; 898 return 0; 899 } 900 901 static int clk_core_enable_lock(struct clk_core *core) 902 { 903 unsigned long flags; 904 int ret; 905 906 flags = clk_enable_lock(); 907 ret = clk_core_enable(core); 908 clk_enable_unlock(flags); 909 910 return ret; 911 } 912 913 /** 914 * clk_enable - ungate a clock 915 * @clk: the clk being ungated 916 * 917 * clk_enable must not sleep, which differentiates it from clk_prepare. In a 918 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk 919 * if the operation will never sleep. One example is a SoC-internal clk which 920 * is controlled via simple register writes. In the complex case a clk ungate 921 * operation may require a fast and a slow part. It is this reason that 922 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare 923 * must be called before clk_enable. Returns 0 on success, -EERROR 924 * otherwise. 925 */ 926 int clk_enable(struct clk *clk) 927 { 928 if (!clk) 929 return 0; 930 931 return clk_core_enable_lock(clk->core); 932 } 933 EXPORT_SYMBOL_GPL(clk_enable); 934 935 static int clk_core_prepare_enable(struct clk_core *core) 936 { 937 int ret; 938 939 ret = clk_core_prepare_lock(core); 940 if (ret) 941 return ret; 942 943 ret = clk_core_enable_lock(core); 944 if (ret) 945 clk_core_unprepare_lock(core); 946 947 return ret; 948 } 949 950 static void clk_core_disable_unprepare(struct clk_core *core) 951 { 952 clk_core_disable_lock(core); 953 clk_core_unprepare_lock(core); 954 } 955 956 static void clk_unprepare_unused_subtree(struct clk_core *core) 957 { 958 struct clk_core *child; 959 960 lockdep_assert_held(&prepare_lock); 961 962 hlist_for_each_entry(child, &core->children, child_node) 963 clk_unprepare_unused_subtree(child); 964 965 if (core->prepare_count) 966 return; 967 968 if (core->flags & CLK_IGNORE_UNUSED) 969 return; 970 971 if (clk_pm_runtime_get(core)) 972 return; 973 974 if (clk_core_is_prepared(core)) { 975 trace_clk_unprepare(core); 976 if (core->ops->unprepare_unused) 977 core->ops->unprepare_unused(core->hw); 978 else if (core->ops->unprepare) 979 core->ops->unprepare(core->hw); 980 trace_clk_unprepare_complete(core); 981 } 982 983 clk_pm_runtime_put(core); 984 } 985 986 static void clk_disable_unused_subtree(struct clk_core *core) 987 { 988 struct clk_core *child; 989 unsigned long flags; 990 991 lockdep_assert_held(&prepare_lock); 992 993 hlist_for_each_entry(child, &core->children, child_node) 994 clk_disable_unused_subtree(child); 995 996 if (core->flags & CLK_OPS_PARENT_ENABLE) 997 clk_core_prepare_enable(core->parent); 998 999 if (clk_pm_runtime_get(core)) 1000 goto unprepare_out; 1001 1002 flags = clk_enable_lock(); 1003 1004 if (core->enable_count) 1005 goto unlock_out; 1006 1007 if (core->flags & CLK_IGNORE_UNUSED) 1008 goto unlock_out; 1009 1010 /* 1011 * some gate clocks have special needs during the disable-unused 1012 * sequence. call .disable_unused if available, otherwise fall 1013 * back to .disable 1014 */ 1015 if (clk_core_is_enabled(core)) { 1016 trace_clk_disable(core); 1017 if (core->ops->disable_unused) 1018 core->ops->disable_unused(core->hw); 1019 else if (core->ops->disable) 1020 core->ops->disable(core->hw); 1021 trace_clk_disable_complete(core); 1022 } 1023 1024 unlock_out: 1025 clk_enable_unlock(flags); 1026 clk_pm_runtime_put(core); 1027 unprepare_out: 1028 if (core->flags & CLK_OPS_PARENT_ENABLE) 1029 clk_core_disable_unprepare(core->parent); 1030 } 1031 1032 static bool clk_ignore_unused; 1033 static int __init clk_ignore_unused_setup(char *__unused) 1034 { 1035 clk_ignore_unused = true; 1036 return 1; 1037 } 1038 __setup("clk_ignore_unused", clk_ignore_unused_setup); 1039 1040 static int clk_disable_unused(void) 1041 { 1042 struct clk_core *core; 1043 1044 if (clk_ignore_unused) { 1045 pr_warn("clk: Not disabling unused clocks\n"); 1046 return 0; 1047 } 1048 1049 clk_prepare_lock(); 1050 1051 hlist_for_each_entry(core, &clk_root_list, child_node) 1052 clk_disable_unused_subtree(core); 1053 1054 hlist_for_each_entry(core, &clk_orphan_list, child_node) 1055 clk_disable_unused_subtree(core); 1056 1057 hlist_for_each_entry(core, &clk_root_list, child_node) 1058 clk_unprepare_unused_subtree(core); 1059 1060 hlist_for_each_entry(core, &clk_orphan_list, child_node) 1061 clk_unprepare_unused_subtree(core); 1062 1063 clk_prepare_unlock(); 1064 1065 return 0; 1066 } 1067 late_initcall_sync(clk_disable_unused); 1068 1069 static int clk_core_determine_round_nolock(struct clk_core *core, 1070 struct clk_rate_request *req) 1071 { 1072 long rate; 1073 1074 lockdep_assert_held(&prepare_lock); 1075 1076 if (!core) 1077 return 0; 1078 1079 /* 1080 * At this point, core protection will be disabled if 1081 * - if the provider is not protected at all 1082 * - if the calling consumer is the only one which has exclusivity 1083 * over the provider 1084 */ 1085 if (clk_core_rate_is_protected(core)) { 1086 req->rate = core->rate; 1087 } else if (core->ops->determine_rate) { 1088 return core->ops->determine_rate(core->hw, req); 1089 } else if (core->ops->round_rate) { 1090 rate = core->ops->round_rate(core->hw, req->rate, 1091 &req->best_parent_rate); 1092 if (rate < 0) 1093 return rate; 1094 1095 req->rate = rate; 1096 } else { 1097 return -EINVAL; 1098 } 1099 1100 return 0; 1101 } 1102 1103 static void clk_core_init_rate_req(struct clk_core * const core, 1104 struct clk_rate_request *req) 1105 { 1106 struct clk_core *parent; 1107 1108 if (WARN_ON(!core || !req)) 1109 return; 1110 1111 parent = core->parent; 1112 if (parent) { 1113 req->best_parent_hw = parent->hw; 1114 req->best_parent_rate = parent->rate; 1115 } else { 1116 req->best_parent_hw = NULL; 1117 req->best_parent_rate = 0; 1118 } 1119 } 1120 1121 static bool clk_core_can_round(struct clk_core * const core) 1122 { 1123 if (core->ops->determine_rate || core->ops->round_rate) 1124 return true; 1125 1126 return false; 1127 } 1128 1129 static int clk_core_round_rate_nolock(struct clk_core *core, 1130 struct clk_rate_request *req) 1131 { 1132 lockdep_assert_held(&prepare_lock); 1133 1134 if (!core) { 1135 req->rate = 0; 1136 return 0; 1137 } 1138 1139 clk_core_init_rate_req(core, req); 1140 1141 if (clk_core_can_round(core)) 1142 return clk_core_determine_round_nolock(core, req); 1143 else if (core->flags & CLK_SET_RATE_PARENT) 1144 return clk_core_round_rate_nolock(core->parent, req); 1145 1146 req->rate = core->rate; 1147 return 0; 1148 } 1149 1150 /** 1151 * __clk_determine_rate - get the closest rate actually supported by a clock 1152 * @hw: determine the rate of this clock 1153 * @req: target rate request 1154 * 1155 * Useful for clk_ops such as .set_rate and .determine_rate. 1156 */ 1157 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req) 1158 { 1159 if (!hw) { 1160 req->rate = 0; 1161 return 0; 1162 } 1163 1164 return clk_core_round_rate_nolock(hw->core, req); 1165 } 1166 EXPORT_SYMBOL_GPL(__clk_determine_rate); 1167 1168 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate) 1169 { 1170 int ret; 1171 struct clk_rate_request req; 1172 1173 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); 1174 req.rate = rate; 1175 1176 ret = clk_core_round_rate_nolock(hw->core, &req); 1177 if (ret) 1178 return 0; 1179 1180 return req.rate; 1181 } 1182 EXPORT_SYMBOL_GPL(clk_hw_round_rate); 1183 1184 /** 1185 * clk_round_rate - round the given rate for a clk 1186 * @clk: the clk for which we are rounding a rate 1187 * @rate: the rate which is to be rounded 1188 * 1189 * Takes in a rate as input and rounds it to a rate that the clk can actually 1190 * use which is then returned. If clk doesn't support round_rate operation 1191 * then the parent rate is returned. 1192 */ 1193 long clk_round_rate(struct clk *clk, unsigned long rate) 1194 { 1195 struct clk_rate_request req; 1196 int ret; 1197 1198 if (!clk) 1199 return 0; 1200 1201 clk_prepare_lock(); 1202 1203 if (clk->exclusive_count) 1204 clk_core_rate_unprotect(clk->core); 1205 1206 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); 1207 req.rate = rate; 1208 1209 ret = clk_core_round_rate_nolock(clk->core, &req); 1210 1211 if (clk->exclusive_count) 1212 clk_core_rate_protect(clk->core); 1213 1214 clk_prepare_unlock(); 1215 1216 if (ret) 1217 return ret; 1218 1219 return req.rate; 1220 } 1221 EXPORT_SYMBOL_GPL(clk_round_rate); 1222 1223 /** 1224 * __clk_notify - call clk notifier chain 1225 * @core: clk that is changing rate 1226 * @msg: clk notifier type (see include/linux/clk.h) 1227 * @old_rate: old clk rate 1228 * @new_rate: new clk rate 1229 * 1230 * Triggers a notifier call chain on the clk rate-change notification 1231 * for 'clk'. Passes a pointer to the struct clk and the previous 1232 * and current rates to the notifier callback. Intended to be called by 1233 * internal clock code only. Returns NOTIFY_DONE from the last driver 1234 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if 1235 * a driver returns that. 1236 */ 1237 static int __clk_notify(struct clk_core *core, unsigned long msg, 1238 unsigned long old_rate, unsigned long new_rate) 1239 { 1240 struct clk_notifier *cn; 1241 struct clk_notifier_data cnd; 1242 int ret = NOTIFY_DONE; 1243 1244 cnd.old_rate = old_rate; 1245 cnd.new_rate = new_rate; 1246 1247 list_for_each_entry(cn, &clk_notifier_list, node) { 1248 if (cn->clk->core == core) { 1249 cnd.clk = cn->clk; 1250 ret = srcu_notifier_call_chain(&cn->notifier_head, msg, 1251 &cnd); 1252 if (ret & NOTIFY_STOP_MASK) 1253 return ret; 1254 } 1255 } 1256 1257 return ret; 1258 } 1259 1260 /** 1261 * __clk_recalc_accuracies 1262 * @core: first clk in the subtree 1263 * 1264 * Walks the subtree of clks starting with clk and recalculates accuracies as 1265 * it goes. Note that if a clk does not implement the .recalc_accuracy 1266 * callback then it is assumed that the clock will take on the accuracy of its 1267 * parent. 1268 */ 1269 static void __clk_recalc_accuracies(struct clk_core *core) 1270 { 1271 unsigned long parent_accuracy = 0; 1272 struct clk_core *child; 1273 1274 lockdep_assert_held(&prepare_lock); 1275 1276 if (core->parent) 1277 parent_accuracy = core->parent->accuracy; 1278 1279 if (core->ops->recalc_accuracy) 1280 core->accuracy = core->ops->recalc_accuracy(core->hw, 1281 parent_accuracy); 1282 else 1283 core->accuracy = parent_accuracy; 1284 1285 hlist_for_each_entry(child, &core->children, child_node) 1286 __clk_recalc_accuracies(child); 1287 } 1288 1289 static long clk_core_get_accuracy(struct clk_core *core) 1290 { 1291 unsigned long accuracy; 1292 1293 clk_prepare_lock(); 1294 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) 1295 __clk_recalc_accuracies(core); 1296 1297 accuracy = __clk_get_accuracy(core); 1298 clk_prepare_unlock(); 1299 1300 return accuracy; 1301 } 1302 1303 /** 1304 * clk_get_accuracy - return the accuracy of clk 1305 * @clk: the clk whose accuracy is being returned 1306 * 1307 * Simply returns the cached accuracy of the clk, unless 1308 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be 1309 * issued. 1310 * If clk is NULL then returns 0. 1311 */ 1312 long clk_get_accuracy(struct clk *clk) 1313 { 1314 if (!clk) 1315 return 0; 1316 1317 return clk_core_get_accuracy(clk->core); 1318 } 1319 EXPORT_SYMBOL_GPL(clk_get_accuracy); 1320 1321 static unsigned long clk_recalc(struct clk_core *core, 1322 unsigned long parent_rate) 1323 { 1324 unsigned long rate = parent_rate; 1325 1326 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { 1327 rate = core->ops->recalc_rate(core->hw, parent_rate); 1328 clk_pm_runtime_put(core); 1329 } 1330 return rate; 1331 } 1332 1333 /** 1334 * __clk_recalc_rates 1335 * @core: first clk in the subtree 1336 * @msg: notification type (see include/linux/clk.h) 1337 * 1338 * Walks the subtree of clks starting with clk and recalculates rates as it 1339 * goes. Note that if a clk does not implement the .recalc_rate callback then 1340 * it is assumed that the clock will take on the rate of its parent. 1341 * 1342 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, 1343 * if necessary. 1344 */ 1345 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg) 1346 { 1347 unsigned long old_rate; 1348 unsigned long parent_rate = 0; 1349 struct clk_core *child; 1350 1351 lockdep_assert_held(&prepare_lock); 1352 1353 old_rate = core->rate; 1354 1355 if (core->parent) 1356 parent_rate = core->parent->rate; 1357 1358 core->rate = clk_recalc(core, parent_rate); 1359 1360 /* 1361 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE 1362 * & ABORT_RATE_CHANGE notifiers 1363 */ 1364 if (core->notifier_count && msg) 1365 __clk_notify(core, msg, old_rate, core->rate); 1366 1367 hlist_for_each_entry(child, &core->children, child_node) 1368 __clk_recalc_rates(child, msg); 1369 } 1370 1371 static unsigned long clk_core_get_rate(struct clk_core *core) 1372 { 1373 unsigned long rate; 1374 1375 clk_prepare_lock(); 1376 1377 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) 1378 __clk_recalc_rates(core, 0); 1379 1380 rate = clk_core_get_rate_nolock(core); 1381 clk_prepare_unlock(); 1382 1383 return rate; 1384 } 1385 1386 /** 1387 * clk_get_rate - return the rate of clk 1388 * @clk: the clk whose rate is being returned 1389 * 1390 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag 1391 * is set, which means a recalc_rate will be issued. 1392 * If clk is NULL then returns 0. 1393 */ 1394 unsigned long clk_get_rate(struct clk *clk) 1395 { 1396 if (!clk) 1397 return 0; 1398 1399 return clk_core_get_rate(clk->core); 1400 } 1401 EXPORT_SYMBOL_GPL(clk_get_rate); 1402 1403 static int clk_fetch_parent_index(struct clk_core *core, 1404 struct clk_core *parent) 1405 { 1406 int i; 1407 1408 if (!parent) 1409 return -EINVAL; 1410 1411 for (i = 0; i < core->num_parents; i++) 1412 if (clk_core_get_parent_by_index(core, i) == parent) 1413 return i; 1414 1415 return -EINVAL; 1416 } 1417 1418 /* 1419 * Update the orphan status of @core and all its children. 1420 */ 1421 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan) 1422 { 1423 struct clk_core *child; 1424 1425 core->orphan = is_orphan; 1426 1427 hlist_for_each_entry(child, &core->children, child_node) 1428 clk_core_update_orphan_status(child, is_orphan); 1429 } 1430 1431 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent) 1432 { 1433 bool was_orphan = core->orphan; 1434 1435 hlist_del(&core->child_node); 1436 1437 if (new_parent) { 1438 bool becomes_orphan = new_parent->orphan; 1439 1440 /* avoid duplicate POST_RATE_CHANGE notifications */ 1441 if (new_parent->new_child == core) 1442 new_parent->new_child = NULL; 1443 1444 hlist_add_head(&core->child_node, &new_parent->children); 1445 1446 if (was_orphan != becomes_orphan) 1447 clk_core_update_orphan_status(core, becomes_orphan); 1448 } else { 1449 hlist_add_head(&core->child_node, &clk_orphan_list); 1450 if (!was_orphan) 1451 clk_core_update_orphan_status(core, true); 1452 } 1453 1454 core->parent = new_parent; 1455 } 1456 1457 static struct clk_core *__clk_set_parent_before(struct clk_core *core, 1458 struct clk_core *parent) 1459 { 1460 unsigned long flags; 1461 struct clk_core *old_parent = core->parent; 1462 1463 /* 1464 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock 1465 * 1466 * 2. Migrate prepare state between parents and prevent race with 1467 * clk_enable(). 1468 * 1469 * If the clock is not prepared, then a race with 1470 * clk_enable/disable() is impossible since we already have the 1471 * prepare lock (future calls to clk_enable() need to be preceded by 1472 * a clk_prepare()). 1473 * 1474 * If the clock is prepared, migrate the prepared state to the new 1475 * parent and also protect against a race with clk_enable() by 1476 * forcing the clock and the new parent on. This ensures that all 1477 * future calls to clk_enable() are practically NOPs with respect to 1478 * hardware and software states. 1479 * 1480 * See also: Comment for clk_set_parent() below. 1481 */ 1482 1483 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */ 1484 if (core->flags & CLK_OPS_PARENT_ENABLE) { 1485 clk_core_prepare_enable(old_parent); 1486 clk_core_prepare_enable(parent); 1487 } 1488 1489 /* migrate prepare count if > 0 */ 1490 if (core->prepare_count) { 1491 clk_core_prepare_enable(parent); 1492 clk_core_enable_lock(core); 1493 } 1494 1495 /* update the clk tree topology */ 1496 flags = clk_enable_lock(); 1497 clk_reparent(core, parent); 1498 clk_enable_unlock(flags); 1499 1500 return old_parent; 1501 } 1502 1503 static void __clk_set_parent_after(struct clk_core *core, 1504 struct clk_core *parent, 1505 struct clk_core *old_parent) 1506 { 1507 /* 1508 * Finish the migration of prepare state and undo the changes done 1509 * for preventing a race with clk_enable(). 1510 */ 1511 if (core->prepare_count) { 1512 clk_core_disable_lock(core); 1513 clk_core_disable_unprepare(old_parent); 1514 } 1515 1516 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ 1517 if (core->flags & CLK_OPS_PARENT_ENABLE) { 1518 clk_core_disable_unprepare(parent); 1519 clk_core_disable_unprepare(old_parent); 1520 } 1521 } 1522 1523 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent, 1524 u8 p_index) 1525 { 1526 unsigned long flags; 1527 int ret = 0; 1528 struct clk_core *old_parent; 1529 1530 old_parent = __clk_set_parent_before(core, parent); 1531 1532 trace_clk_set_parent(core, parent); 1533 1534 /* change clock input source */ 1535 if (parent && core->ops->set_parent) 1536 ret = core->ops->set_parent(core->hw, p_index); 1537 1538 trace_clk_set_parent_complete(core, parent); 1539 1540 if (ret) { 1541 flags = clk_enable_lock(); 1542 clk_reparent(core, old_parent); 1543 clk_enable_unlock(flags); 1544 __clk_set_parent_after(core, old_parent, parent); 1545 1546 return ret; 1547 } 1548 1549 __clk_set_parent_after(core, parent, old_parent); 1550 1551 return 0; 1552 } 1553 1554 /** 1555 * __clk_speculate_rates 1556 * @core: first clk in the subtree 1557 * @parent_rate: the "future" rate of clk's parent 1558 * 1559 * Walks the subtree of clks starting with clk, speculating rates as it 1560 * goes and firing off PRE_RATE_CHANGE notifications as necessary. 1561 * 1562 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending 1563 * pre-rate change notifications and returns early if no clks in the 1564 * subtree have subscribed to the notifications. Note that if a clk does not 1565 * implement the .recalc_rate callback then it is assumed that the clock will 1566 * take on the rate of its parent. 1567 */ 1568 static int __clk_speculate_rates(struct clk_core *core, 1569 unsigned long parent_rate) 1570 { 1571 struct clk_core *child; 1572 unsigned long new_rate; 1573 int ret = NOTIFY_DONE; 1574 1575 lockdep_assert_held(&prepare_lock); 1576 1577 new_rate = clk_recalc(core, parent_rate); 1578 1579 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ 1580 if (core->notifier_count) 1581 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); 1582 1583 if (ret & NOTIFY_STOP_MASK) { 1584 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", 1585 __func__, core->name, ret); 1586 goto out; 1587 } 1588 1589 hlist_for_each_entry(child, &core->children, child_node) { 1590 ret = __clk_speculate_rates(child, new_rate); 1591 if (ret & NOTIFY_STOP_MASK) 1592 break; 1593 } 1594 1595 out: 1596 return ret; 1597 } 1598 1599 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate, 1600 struct clk_core *new_parent, u8 p_index) 1601 { 1602 struct clk_core *child; 1603 1604 core->new_rate = new_rate; 1605 core->new_parent = new_parent; 1606 core->new_parent_index = p_index; 1607 /* include clk in new parent's PRE_RATE_CHANGE notifications */ 1608 core->new_child = NULL; 1609 if (new_parent && new_parent != core->parent) 1610 new_parent->new_child = core; 1611 1612 hlist_for_each_entry(child, &core->children, child_node) { 1613 child->new_rate = clk_recalc(child, new_rate); 1614 clk_calc_subtree(child, child->new_rate, NULL, 0); 1615 } 1616 } 1617 1618 /* 1619 * calculate the new rates returning the topmost clock that has to be 1620 * changed. 1621 */ 1622 static struct clk_core *clk_calc_new_rates(struct clk_core *core, 1623 unsigned long rate) 1624 { 1625 struct clk_core *top = core; 1626 struct clk_core *old_parent, *parent; 1627 unsigned long best_parent_rate = 0; 1628 unsigned long new_rate; 1629 unsigned long min_rate; 1630 unsigned long max_rate; 1631 int p_index = 0; 1632 long ret; 1633 1634 /* sanity */ 1635 if (IS_ERR_OR_NULL(core)) 1636 return NULL; 1637 1638 /* save parent rate, if it exists */ 1639 parent = old_parent = core->parent; 1640 if (parent) 1641 best_parent_rate = parent->rate; 1642 1643 clk_core_get_boundaries(core, &min_rate, &max_rate); 1644 1645 /* find the closest rate and parent clk/rate */ 1646 if (clk_core_can_round(core)) { 1647 struct clk_rate_request req; 1648 1649 req.rate = rate; 1650 req.min_rate = min_rate; 1651 req.max_rate = max_rate; 1652 1653 clk_core_init_rate_req(core, &req); 1654 1655 ret = clk_core_determine_round_nolock(core, &req); 1656 if (ret < 0) 1657 return NULL; 1658 1659 best_parent_rate = req.best_parent_rate; 1660 new_rate = req.rate; 1661 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; 1662 1663 if (new_rate < min_rate || new_rate > max_rate) 1664 return NULL; 1665 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { 1666 /* pass-through clock without adjustable parent */ 1667 core->new_rate = core->rate; 1668 return NULL; 1669 } else { 1670 /* pass-through clock with adjustable parent */ 1671 top = clk_calc_new_rates(parent, rate); 1672 new_rate = parent->new_rate; 1673 goto out; 1674 } 1675 1676 /* some clocks must be gated to change parent */ 1677 if (parent != old_parent && 1678 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { 1679 pr_debug("%s: %s not gated but wants to reparent\n", 1680 __func__, core->name); 1681 return NULL; 1682 } 1683 1684 /* try finding the new parent index */ 1685 if (parent && core->num_parents > 1) { 1686 p_index = clk_fetch_parent_index(core, parent); 1687 if (p_index < 0) { 1688 pr_debug("%s: clk %s can not be parent of clk %s\n", 1689 __func__, parent->name, core->name); 1690 return NULL; 1691 } 1692 } 1693 1694 if ((core->flags & CLK_SET_RATE_PARENT) && parent && 1695 best_parent_rate != parent->rate) 1696 top = clk_calc_new_rates(parent, best_parent_rate); 1697 1698 out: 1699 clk_calc_subtree(core, new_rate, parent, p_index); 1700 1701 return top; 1702 } 1703 1704 /* 1705 * Notify about rate changes in a subtree. Always walk down the whole tree 1706 * so that in case of an error we can walk down the whole tree again and 1707 * abort the change. 1708 */ 1709 static struct clk_core *clk_propagate_rate_change(struct clk_core *core, 1710 unsigned long event) 1711 { 1712 struct clk_core *child, *tmp_clk, *fail_clk = NULL; 1713 int ret = NOTIFY_DONE; 1714 1715 if (core->rate == core->new_rate) 1716 return NULL; 1717 1718 if (core->notifier_count) { 1719 ret = __clk_notify(core, event, core->rate, core->new_rate); 1720 if (ret & NOTIFY_STOP_MASK) 1721 fail_clk = core; 1722 } 1723 1724 hlist_for_each_entry(child, &core->children, child_node) { 1725 /* Skip children who will be reparented to another clock */ 1726 if (child->new_parent && child->new_parent != core) 1727 continue; 1728 tmp_clk = clk_propagate_rate_change(child, event); 1729 if (tmp_clk) 1730 fail_clk = tmp_clk; 1731 } 1732 1733 /* handle the new child who might not be in core->children yet */ 1734 if (core->new_child) { 1735 tmp_clk = clk_propagate_rate_change(core->new_child, event); 1736 if (tmp_clk) 1737 fail_clk = tmp_clk; 1738 } 1739 1740 return fail_clk; 1741 } 1742 1743 /* 1744 * walk down a subtree and set the new rates notifying the rate 1745 * change on the way 1746 */ 1747 static void clk_change_rate(struct clk_core *core) 1748 { 1749 struct clk_core *child; 1750 struct hlist_node *tmp; 1751 unsigned long old_rate; 1752 unsigned long best_parent_rate = 0; 1753 bool skip_set_rate = false; 1754 struct clk_core *old_parent; 1755 struct clk_core *parent = NULL; 1756 1757 old_rate = core->rate; 1758 1759 if (core->new_parent) { 1760 parent = core->new_parent; 1761 best_parent_rate = core->new_parent->rate; 1762 } else if (core->parent) { 1763 parent = core->parent; 1764 best_parent_rate = core->parent->rate; 1765 } 1766 1767 if (clk_pm_runtime_get(core)) 1768 return; 1769 1770 if (core->flags & CLK_SET_RATE_UNGATE) { 1771 unsigned long flags; 1772 1773 clk_core_prepare(core); 1774 flags = clk_enable_lock(); 1775 clk_core_enable(core); 1776 clk_enable_unlock(flags); 1777 } 1778 1779 if (core->new_parent && core->new_parent != core->parent) { 1780 old_parent = __clk_set_parent_before(core, core->new_parent); 1781 trace_clk_set_parent(core, core->new_parent); 1782 1783 if (core->ops->set_rate_and_parent) { 1784 skip_set_rate = true; 1785 core->ops->set_rate_and_parent(core->hw, core->new_rate, 1786 best_parent_rate, 1787 core->new_parent_index); 1788 } else if (core->ops->set_parent) { 1789 core->ops->set_parent(core->hw, core->new_parent_index); 1790 } 1791 1792 trace_clk_set_parent_complete(core, core->new_parent); 1793 __clk_set_parent_after(core, core->new_parent, old_parent); 1794 } 1795 1796 if (core->flags & CLK_OPS_PARENT_ENABLE) 1797 clk_core_prepare_enable(parent); 1798 1799 trace_clk_set_rate(core, core->new_rate); 1800 1801 if (!skip_set_rate && core->ops->set_rate) 1802 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); 1803 1804 trace_clk_set_rate_complete(core, core->new_rate); 1805 1806 core->rate = clk_recalc(core, best_parent_rate); 1807 1808 if (core->flags & CLK_SET_RATE_UNGATE) { 1809 unsigned long flags; 1810 1811 flags = clk_enable_lock(); 1812 clk_core_disable(core); 1813 clk_enable_unlock(flags); 1814 clk_core_unprepare(core); 1815 } 1816 1817 if (core->flags & CLK_OPS_PARENT_ENABLE) 1818 clk_core_disable_unprepare(parent); 1819 1820 if (core->notifier_count && old_rate != core->rate) 1821 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); 1822 1823 if (core->flags & CLK_RECALC_NEW_RATES) 1824 (void)clk_calc_new_rates(core, core->new_rate); 1825 1826 /* 1827 * Use safe iteration, as change_rate can actually swap parents 1828 * for certain clock types. 1829 */ 1830 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { 1831 /* Skip children who will be reparented to another clock */ 1832 if (child->new_parent && child->new_parent != core) 1833 continue; 1834 clk_change_rate(child); 1835 } 1836 1837 /* handle the new child who might not be in core->children yet */ 1838 if (core->new_child) 1839 clk_change_rate(core->new_child); 1840 1841 clk_pm_runtime_put(core); 1842 } 1843 1844 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core, 1845 unsigned long req_rate) 1846 { 1847 int ret, cnt; 1848 struct clk_rate_request req; 1849 1850 lockdep_assert_held(&prepare_lock); 1851 1852 if (!core) 1853 return 0; 1854 1855 /* simulate what the rate would be if it could be freely set */ 1856 cnt = clk_core_rate_nuke_protect(core); 1857 if (cnt < 0) 1858 return cnt; 1859 1860 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate); 1861 req.rate = req_rate; 1862 1863 ret = clk_core_round_rate_nolock(core, &req); 1864 1865 /* restore the protection */ 1866 clk_core_rate_restore_protect(core, cnt); 1867 1868 return ret ? 0 : req.rate; 1869 } 1870 1871 static int clk_core_set_rate_nolock(struct clk_core *core, 1872 unsigned long req_rate) 1873 { 1874 struct clk_core *top, *fail_clk; 1875 unsigned long rate; 1876 int ret = 0; 1877 1878 if (!core) 1879 return 0; 1880 1881 rate = clk_core_req_round_rate_nolock(core, req_rate); 1882 1883 /* bail early if nothing to do */ 1884 if (rate == clk_core_get_rate_nolock(core)) 1885 return 0; 1886 1887 /* fail on a direct rate set of a protected provider */ 1888 if (clk_core_rate_is_protected(core)) 1889 return -EBUSY; 1890 1891 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count) 1892 return -EBUSY; 1893 1894 /* calculate new rates and get the topmost changed clock */ 1895 top = clk_calc_new_rates(core, req_rate); 1896 if (!top) 1897 return -EINVAL; 1898 1899 ret = clk_pm_runtime_get(core); 1900 if (ret) 1901 return ret; 1902 1903 /* notify that we are about to change rates */ 1904 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); 1905 if (fail_clk) { 1906 pr_debug("%s: failed to set %s rate\n", __func__, 1907 fail_clk->name); 1908 clk_propagate_rate_change(top, ABORT_RATE_CHANGE); 1909 ret = -EBUSY; 1910 goto err; 1911 } 1912 1913 /* change the rates */ 1914 clk_change_rate(top); 1915 1916 core->req_rate = req_rate; 1917 err: 1918 clk_pm_runtime_put(core); 1919 1920 return ret; 1921 } 1922 1923 /** 1924 * clk_set_rate - specify a new rate for clk 1925 * @clk: the clk whose rate is being changed 1926 * @rate: the new rate for clk 1927 * 1928 * In the simplest case clk_set_rate will only adjust the rate of clk. 1929 * 1930 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to 1931 * propagate up to clk's parent; whether or not this happens depends on the 1932 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged 1933 * after calling .round_rate then upstream parent propagation is ignored. If 1934 * *parent_rate comes back with a new rate for clk's parent then we propagate 1935 * up to clk's parent and set its rate. Upward propagation will continue 1936 * until either a clk does not support the CLK_SET_RATE_PARENT flag or 1937 * .round_rate stops requesting changes to clk's parent_rate. 1938 * 1939 * Rate changes are accomplished via tree traversal that also recalculates the 1940 * rates for the clocks and fires off POST_RATE_CHANGE notifiers. 1941 * 1942 * Returns 0 on success, -EERROR otherwise. 1943 */ 1944 int clk_set_rate(struct clk *clk, unsigned long rate) 1945 { 1946 int ret; 1947 1948 if (!clk) 1949 return 0; 1950 1951 /* prevent racing with updates to the clock topology */ 1952 clk_prepare_lock(); 1953 1954 if (clk->exclusive_count) 1955 clk_core_rate_unprotect(clk->core); 1956 1957 ret = clk_core_set_rate_nolock(clk->core, rate); 1958 1959 if (clk->exclusive_count) 1960 clk_core_rate_protect(clk->core); 1961 1962 clk_prepare_unlock(); 1963 1964 return ret; 1965 } 1966 EXPORT_SYMBOL_GPL(clk_set_rate); 1967 1968 /** 1969 * clk_set_rate_exclusive - specify a new rate get exclusive control 1970 * @clk: the clk whose rate is being changed 1971 * @rate: the new rate for clk 1972 * 1973 * This is a combination of clk_set_rate() and clk_rate_exclusive_get() 1974 * within a critical section 1975 * 1976 * This can be used initially to ensure that at least 1 consumer is 1977 * statisfied when several consumers are competing for exclusivity over the 1978 * same clock provider. 1979 * 1980 * The exclusivity is not applied if setting the rate failed. 1981 * 1982 * Calls to clk_rate_exclusive_get() should be balanced with calls to 1983 * clk_rate_exclusive_put(). 1984 * 1985 * Returns 0 on success, -EERROR otherwise. 1986 */ 1987 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) 1988 { 1989 int ret; 1990 1991 if (!clk) 1992 return 0; 1993 1994 /* prevent racing with updates to the clock topology */ 1995 clk_prepare_lock(); 1996 1997 /* 1998 * The temporary protection removal is not here, on purpose 1999 * This function is meant to be used instead of clk_rate_protect, 2000 * so before the consumer code path protect the clock provider 2001 */ 2002 2003 ret = clk_core_set_rate_nolock(clk->core, rate); 2004 if (!ret) { 2005 clk_core_rate_protect(clk->core); 2006 clk->exclusive_count++; 2007 } 2008 2009 clk_prepare_unlock(); 2010 2011 return ret; 2012 } 2013 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive); 2014 2015 /** 2016 * clk_set_rate_range - set a rate range for a clock source 2017 * @clk: clock source 2018 * @min: desired minimum clock rate in Hz, inclusive 2019 * @max: desired maximum clock rate in Hz, inclusive 2020 * 2021 * Returns success (0) or negative errno. 2022 */ 2023 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) 2024 { 2025 int ret = 0; 2026 unsigned long old_min, old_max, rate; 2027 2028 if (!clk) 2029 return 0; 2030 2031 if (min > max) { 2032 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", 2033 __func__, clk->core->name, clk->dev_id, clk->con_id, 2034 min, max); 2035 return -EINVAL; 2036 } 2037 2038 clk_prepare_lock(); 2039 2040 if (clk->exclusive_count) 2041 clk_core_rate_unprotect(clk->core); 2042 2043 /* Save the current values in case we need to rollback the change */ 2044 old_min = clk->min_rate; 2045 old_max = clk->max_rate; 2046 clk->min_rate = min; 2047 clk->max_rate = max; 2048 2049 rate = clk_core_get_rate_nolock(clk->core); 2050 if (rate < min || rate > max) { 2051 /* 2052 * FIXME: 2053 * We are in bit of trouble here, current rate is outside the 2054 * the requested range. We are going try to request appropriate 2055 * range boundary but there is a catch. It may fail for the 2056 * usual reason (clock broken, clock protected, etc) but also 2057 * because: 2058 * - round_rate() was not favorable and fell on the wrong 2059 * side of the boundary 2060 * - the determine_rate() callback does not really check for 2061 * this corner case when determining the rate 2062 */ 2063 2064 if (rate < min) 2065 rate = min; 2066 else 2067 rate = max; 2068 2069 ret = clk_core_set_rate_nolock(clk->core, rate); 2070 if (ret) { 2071 /* rollback the changes */ 2072 clk->min_rate = old_min; 2073 clk->max_rate = old_max; 2074 } 2075 } 2076 2077 if (clk->exclusive_count) 2078 clk_core_rate_protect(clk->core); 2079 2080 clk_prepare_unlock(); 2081 2082 return ret; 2083 } 2084 EXPORT_SYMBOL_GPL(clk_set_rate_range); 2085 2086 /** 2087 * clk_set_min_rate - set a minimum clock rate for a clock source 2088 * @clk: clock source 2089 * @rate: desired minimum clock rate in Hz, inclusive 2090 * 2091 * Returns success (0) or negative errno. 2092 */ 2093 int clk_set_min_rate(struct clk *clk, unsigned long rate) 2094 { 2095 if (!clk) 2096 return 0; 2097 2098 return clk_set_rate_range(clk, rate, clk->max_rate); 2099 } 2100 EXPORT_SYMBOL_GPL(clk_set_min_rate); 2101 2102 /** 2103 * clk_set_max_rate - set a maximum clock rate for a clock source 2104 * @clk: clock source 2105 * @rate: desired maximum clock rate in Hz, inclusive 2106 * 2107 * Returns success (0) or negative errno. 2108 */ 2109 int clk_set_max_rate(struct clk *clk, unsigned long rate) 2110 { 2111 if (!clk) 2112 return 0; 2113 2114 return clk_set_rate_range(clk, clk->min_rate, rate); 2115 } 2116 EXPORT_SYMBOL_GPL(clk_set_max_rate); 2117 2118 /** 2119 * clk_get_parent - return the parent of a clk 2120 * @clk: the clk whose parent gets returned 2121 * 2122 * Simply returns clk->parent. Returns NULL if clk is NULL. 2123 */ 2124 struct clk *clk_get_parent(struct clk *clk) 2125 { 2126 struct clk *parent; 2127 2128 if (!clk) 2129 return NULL; 2130 2131 clk_prepare_lock(); 2132 /* TODO: Create a per-user clk and change callers to call clk_put */ 2133 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; 2134 clk_prepare_unlock(); 2135 2136 return parent; 2137 } 2138 EXPORT_SYMBOL_GPL(clk_get_parent); 2139 2140 static struct clk_core *__clk_init_parent(struct clk_core *core) 2141 { 2142 u8 index = 0; 2143 2144 if (core->num_parents > 1 && core->ops->get_parent) 2145 index = core->ops->get_parent(core->hw); 2146 2147 return clk_core_get_parent_by_index(core, index); 2148 } 2149 2150 static void clk_core_reparent(struct clk_core *core, 2151 struct clk_core *new_parent) 2152 { 2153 clk_reparent(core, new_parent); 2154 __clk_recalc_accuracies(core); 2155 __clk_recalc_rates(core, POST_RATE_CHANGE); 2156 } 2157 2158 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent) 2159 { 2160 if (!hw) 2161 return; 2162 2163 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); 2164 } 2165 2166 /** 2167 * clk_has_parent - check if a clock is a possible parent for another 2168 * @clk: clock source 2169 * @parent: parent clock source 2170 * 2171 * This function can be used in drivers that need to check that a clock can be 2172 * the parent of another without actually changing the parent. 2173 * 2174 * Returns true if @parent is a possible parent for @clk, false otherwise. 2175 */ 2176 bool clk_has_parent(struct clk *clk, struct clk *parent) 2177 { 2178 struct clk_core *core, *parent_core; 2179 2180 /* NULL clocks should be nops, so return success if either is NULL. */ 2181 if (!clk || !parent) 2182 return true; 2183 2184 core = clk->core; 2185 parent_core = parent->core; 2186 2187 /* Optimize for the case where the parent is already the parent. */ 2188 if (core->parent == parent_core) 2189 return true; 2190 2191 return match_string(core->parent_names, core->num_parents, 2192 parent_core->name) >= 0; 2193 } 2194 EXPORT_SYMBOL_GPL(clk_has_parent); 2195 2196 static int clk_core_set_parent_nolock(struct clk_core *core, 2197 struct clk_core *parent) 2198 { 2199 int ret = 0; 2200 int p_index = 0; 2201 unsigned long p_rate = 0; 2202 2203 lockdep_assert_held(&prepare_lock); 2204 2205 if (!core) 2206 return 0; 2207 2208 if (core->parent == parent) 2209 return 0; 2210 2211 /* verify ops for for multi-parent clks */ 2212 if (core->num_parents > 1 && !core->ops->set_parent) 2213 return -EPERM; 2214 2215 /* check that we are allowed to re-parent if the clock is in use */ 2216 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) 2217 return -EBUSY; 2218 2219 if (clk_core_rate_is_protected(core)) 2220 return -EBUSY; 2221 2222 /* try finding the new parent index */ 2223 if (parent) { 2224 p_index = clk_fetch_parent_index(core, parent); 2225 if (p_index < 0) { 2226 pr_debug("%s: clk %s can not be parent of clk %s\n", 2227 __func__, parent->name, core->name); 2228 return p_index; 2229 } 2230 p_rate = parent->rate; 2231 } 2232 2233 ret = clk_pm_runtime_get(core); 2234 if (ret) 2235 return ret; 2236 2237 /* propagate PRE_RATE_CHANGE notifications */ 2238 ret = __clk_speculate_rates(core, p_rate); 2239 2240 /* abort if a driver objects */ 2241 if (ret & NOTIFY_STOP_MASK) 2242 goto runtime_put; 2243 2244 /* do the re-parent */ 2245 ret = __clk_set_parent(core, parent, p_index); 2246 2247 /* propagate rate an accuracy recalculation accordingly */ 2248 if (ret) { 2249 __clk_recalc_rates(core, ABORT_RATE_CHANGE); 2250 } else { 2251 __clk_recalc_rates(core, POST_RATE_CHANGE); 2252 __clk_recalc_accuracies(core); 2253 } 2254 2255 runtime_put: 2256 clk_pm_runtime_put(core); 2257 2258 return ret; 2259 } 2260 2261 /** 2262 * clk_set_parent - switch the parent of a mux clk 2263 * @clk: the mux clk whose input we are switching 2264 * @parent: the new input to clk 2265 * 2266 * Re-parent clk to use parent as its new input source. If clk is in 2267 * prepared state, the clk will get enabled for the duration of this call. If 2268 * that's not acceptable for a specific clk (Eg: the consumer can't handle 2269 * that, the reparenting is glitchy in hardware, etc), use the 2270 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared. 2271 * 2272 * After successfully changing clk's parent clk_set_parent will update the 2273 * clk topology, sysfs topology and propagate rate recalculation via 2274 * __clk_recalc_rates. 2275 * 2276 * Returns 0 on success, -EERROR otherwise. 2277 */ 2278 int clk_set_parent(struct clk *clk, struct clk *parent) 2279 { 2280 int ret; 2281 2282 if (!clk) 2283 return 0; 2284 2285 clk_prepare_lock(); 2286 2287 if (clk->exclusive_count) 2288 clk_core_rate_unprotect(clk->core); 2289 2290 ret = clk_core_set_parent_nolock(clk->core, 2291 parent ? parent->core : NULL); 2292 2293 if (clk->exclusive_count) 2294 clk_core_rate_protect(clk->core); 2295 2296 clk_prepare_unlock(); 2297 2298 return ret; 2299 } 2300 EXPORT_SYMBOL_GPL(clk_set_parent); 2301 2302 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees) 2303 { 2304 int ret = -EINVAL; 2305 2306 lockdep_assert_held(&prepare_lock); 2307 2308 if (!core) 2309 return 0; 2310 2311 if (clk_core_rate_is_protected(core)) 2312 return -EBUSY; 2313 2314 trace_clk_set_phase(core, degrees); 2315 2316 if (core->ops->set_phase) { 2317 ret = core->ops->set_phase(core->hw, degrees); 2318 if (!ret) 2319 core->phase = degrees; 2320 } 2321 2322 trace_clk_set_phase_complete(core, degrees); 2323 2324 return ret; 2325 } 2326 2327 /** 2328 * clk_set_phase - adjust the phase shift of a clock signal 2329 * @clk: clock signal source 2330 * @degrees: number of degrees the signal is shifted 2331 * 2332 * Shifts the phase of a clock signal by the specified 2333 * degrees. Returns 0 on success, -EERROR otherwise. 2334 * 2335 * This function makes no distinction about the input or reference 2336 * signal that we adjust the clock signal phase against. For example 2337 * phase locked-loop clock signal generators we may shift phase with 2338 * respect to feedback clock signal input, but for other cases the 2339 * clock phase may be shifted with respect to some other, unspecified 2340 * signal. 2341 * 2342 * Additionally the concept of phase shift does not propagate through 2343 * the clock tree hierarchy, which sets it apart from clock rates and 2344 * clock accuracy. A parent clock phase attribute does not have an 2345 * impact on the phase attribute of a child clock. 2346 */ 2347 int clk_set_phase(struct clk *clk, int degrees) 2348 { 2349 int ret; 2350 2351 if (!clk) 2352 return 0; 2353 2354 /* sanity check degrees */ 2355 degrees %= 360; 2356 if (degrees < 0) 2357 degrees += 360; 2358 2359 clk_prepare_lock(); 2360 2361 if (clk->exclusive_count) 2362 clk_core_rate_unprotect(clk->core); 2363 2364 ret = clk_core_set_phase_nolock(clk->core, degrees); 2365 2366 if (clk->exclusive_count) 2367 clk_core_rate_protect(clk->core); 2368 2369 clk_prepare_unlock(); 2370 2371 return ret; 2372 } 2373 EXPORT_SYMBOL_GPL(clk_set_phase); 2374 2375 static int clk_core_get_phase(struct clk_core *core) 2376 { 2377 int ret; 2378 2379 clk_prepare_lock(); 2380 /* Always try to update cached phase if possible */ 2381 if (core->ops->get_phase) 2382 core->phase = core->ops->get_phase(core->hw); 2383 ret = core->phase; 2384 clk_prepare_unlock(); 2385 2386 return ret; 2387 } 2388 2389 /** 2390 * clk_get_phase - return the phase shift of a clock signal 2391 * @clk: clock signal source 2392 * 2393 * Returns the phase shift of a clock node in degrees, otherwise returns 2394 * -EERROR. 2395 */ 2396 int clk_get_phase(struct clk *clk) 2397 { 2398 if (!clk) 2399 return 0; 2400 2401 return clk_core_get_phase(clk->core); 2402 } 2403 EXPORT_SYMBOL_GPL(clk_get_phase); 2404 2405 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core) 2406 { 2407 /* Assume a default value of 50% */ 2408 core->duty.num = 1; 2409 core->duty.den = 2; 2410 } 2411 2412 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core); 2413 2414 static int clk_core_update_duty_cycle_nolock(struct clk_core *core) 2415 { 2416 struct clk_duty *duty = &core->duty; 2417 int ret = 0; 2418 2419 if (!core->ops->get_duty_cycle) 2420 return clk_core_update_duty_cycle_parent_nolock(core); 2421 2422 ret = core->ops->get_duty_cycle(core->hw, duty); 2423 if (ret) 2424 goto reset; 2425 2426 /* Don't trust the clock provider too much */ 2427 if (duty->den == 0 || duty->num > duty->den) { 2428 ret = -EINVAL; 2429 goto reset; 2430 } 2431 2432 return 0; 2433 2434 reset: 2435 clk_core_reset_duty_cycle_nolock(core); 2436 return ret; 2437 } 2438 2439 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core) 2440 { 2441 int ret = 0; 2442 2443 if (core->parent && 2444 core->flags & CLK_DUTY_CYCLE_PARENT) { 2445 ret = clk_core_update_duty_cycle_nolock(core->parent); 2446 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); 2447 } else { 2448 clk_core_reset_duty_cycle_nolock(core); 2449 } 2450 2451 return ret; 2452 } 2453 2454 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core, 2455 struct clk_duty *duty); 2456 2457 static int clk_core_set_duty_cycle_nolock(struct clk_core *core, 2458 struct clk_duty *duty) 2459 { 2460 int ret; 2461 2462 lockdep_assert_held(&prepare_lock); 2463 2464 if (clk_core_rate_is_protected(core)) 2465 return -EBUSY; 2466 2467 trace_clk_set_duty_cycle(core, duty); 2468 2469 if (!core->ops->set_duty_cycle) 2470 return clk_core_set_duty_cycle_parent_nolock(core, duty); 2471 2472 ret = core->ops->set_duty_cycle(core->hw, duty); 2473 if (!ret) 2474 memcpy(&core->duty, duty, sizeof(*duty)); 2475 2476 trace_clk_set_duty_cycle_complete(core, duty); 2477 2478 return ret; 2479 } 2480 2481 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core, 2482 struct clk_duty *duty) 2483 { 2484 int ret = 0; 2485 2486 if (core->parent && 2487 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) { 2488 ret = clk_core_set_duty_cycle_nolock(core->parent, duty); 2489 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); 2490 } 2491 2492 return ret; 2493 } 2494 2495 /** 2496 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal 2497 * @clk: clock signal source 2498 * @num: numerator of the duty cycle ratio to be applied 2499 * @den: denominator of the duty cycle ratio to be applied 2500 * 2501 * Apply the duty cycle ratio if the ratio is valid and the clock can 2502 * perform this operation 2503 * 2504 * Returns (0) on success, a negative errno otherwise. 2505 */ 2506 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den) 2507 { 2508 int ret; 2509 struct clk_duty duty; 2510 2511 if (!clk) 2512 return 0; 2513 2514 /* sanity check the ratio */ 2515 if (den == 0 || num > den) 2516 return -EINVAL; 2517 2518 duty.num = num; 2519 duty.den = den; 2520 2521 clk_prepare_lock(); 2522 2523 if (clk->exclusive_count) 2524 clk_core_rate_unprotect(clk->core); 2525 2526 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty); 2527 2528 if (clk->exclusive_count) 2529 clk_core_rate_protect(clk->core); 2530 2531 clk_prepare_unlock(); 2532 2533 return ret; 2534 } 2535 EXPORT_SYMBOL_GPL(clk_set_duty_cycle); 2536 2537 static int clk_core_get_scaled_duty_cycle(struct clk_core *core, 2538 unsigned int scale) 2539 { 2540 struct clk_duty *duty = &core->duty; 2541 int ret; 2542 2543 clk_prepare_lock(); 2544 2545 ret = clk_core_update_duty_cycle_nolock(core); 2546 if (!ret) 2547 ret = mult_frac(scale, duty->num, duty->den); 2548 2549 clk_prepare_unlock(); 2550 2551 return ret; 2552 } 2553 2554 /** 2555 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal 2556 * @clk: clock signal source 2557 * @scale: scaling factor to be applied to represent the ratio as an integer 2558 * 2559 * Returns the duty cycle ratio of a clock node multiplied by the provided 2560 * scaling factor, or negative errno on error. 2561 */ 2562 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale) 2563 { 2564 if (!clk) 2565 return 0; 2566 2567 return clk_core_get_scaled_duty_cycle(clk->core, scale); 2568 } 2569 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle); 2570 2571 /** 2572 * clk_is_match - check if two clk's point to the same hardware clock 2573 * @p: clk compared against q 2574 * @q: clk compared against p 2575 * 2576 * Returns true if the two struct clk pointers both point to the same hardware 2577 * clock node. Put differently, returns true if struct clk *p and struct clk *q 2578 * share the same struct clk_core object. 2579 * 2580 * Returns false otherwise. Note that two NULL clks are treated as matching. 2581 */ 2582 bool clk_is_match(const struct clk *p, const struct clk *q) 2583 { 2584 /* trivial case: identical struct clk's or both NULL */ 2585 if (p == q) 2586 return true; 2587 2588 /* true if clk->core pointers match. Avoid dereferencing garbage */ 2589 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q)) 2590 if (p->core == q->core) 2591 return true; 2592 2593 return false; 2594 } 2595 EXPORT_SYMBOL_GPL(clk_is_match); 2596 2597 /*** debugfs support ***/ 2598 2599 #ifdef CONFIG_DEBUG_FS 2600 #include <linux/debugfs.h> 2601 2602 static struct dentry *rootdir; 2603 static int inited = 0; 2604 static DEFINE_MUTEX(clk_debug_lock); 2605 static HLIST_HEAD(clk_debug_list); 2606 2607 static struct hlist_head *all_lists[] = { 2608 &clk_root_list, 2609 &clk_orphan_list, 2610 NULL, 2611 }; 2612 2613 static struct hlist_head *orphan_list[] = { 2614 &clk_orphan_list, 2615 NULL, 2616 }; 2617 2618 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c, 2619 int level) 2620 { 2621 if (!c) 2622 return; 2623 2624 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n", 2625 level * 3 + 1, "", 2626 30 - level * 3, c->name, 2627 c->enable_count, c->prepare_count, c->protect_count, 2628 clk_core_get_rate(c), clk_core_get_accuracy(c), 2629 clk_core_get_phase(c), 2630 clk_core_get_scaled_duty_cycle(c, 100000)); 2631 } 2632 2633 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c, 2634 int level) 2635 { 2636 struct clk_core *child; 2637 2638 if (!c) 2639 return; 2640 2641 clk_summary_show_one(s, c, level); 2642 2643 hlist_for_each_entry(child, &c->children, child_node) 2644 clk_summary_show_subtree(s, child, level + 1); 2645 } 2646 2647 static int clk_summary_show(struct seq_file *s, void *data) 2648 { 2649 struct clk_core *c; 2650 struct hlist_head **lists = (struct hlist_head **)s->private; 2651 2652 seq_puts(s, " enable prepare protect duty\n"); 2653 seq_puts(s, " clock count count count rate accuracy phase cycle\n"); 2654 seq_puts(s, "---------------------------------------------------------------------------------------------\n"); 2655 2656 clk_prepare_lock(); 2657 2658 for (; *lists; lists++) 2659 hlist_for_each_entry(c, *lists, child_node) 2660 clk_summary_show_subtree(s, c, 0); 2661 2662 clk_prepare_unlock(); 2663 2664 return 0; 2665 } 2666 DEFINE_SHOW_ATTRIBUTE(clk_summary); 2667 2668 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level) 2669 { 2670 if (!c) 2671 return; 2672 2673 /* This should be JSON format, i.e. elements separated with a comma */ 2674 seq_printf(s, "\"%s\": { ", c->name); 2675 seq_printf(s, "\"enable_count\": %d,", c->enable_count); 2676 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); 2677 seq_printf(s, "\"protect_count\": %d,", c->protect_count); 2678 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c)); 2679 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c)); 2680 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c)); 2681 seq_printf(s, "\"duty_cycle\": %u", 2682 clk_core_get_scaled_duty_cycle(c, 100000)); 2683 } 2684 2685 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level) 2686 { 2687 struct clk_core *child; 2688 2689 if (!c) 2690 return; 2691 2692 clk_dump_one(s, c, level); 2693 2694 hlist_for_each_entry(child, &c->children, child_node) { 2695 seq_putc(s, ','); 2696 clk_dump_subtree(s, child, level + 1); 2697 } 2698 2699 seq_putc(s, '}'); 2700 } 2701 2702 static int clk_dump_show(struct seq_file *s, void *data) 2703 { 2704 struct clk_core *c; 2705 bool first_node = true; 2706 struct hlist_head **lists = (struct hlist_head **)s->private; 2707 2708 seq_putc(s, '{'); 2709 clk_prepare_lock(); 2710 2711 for (; *lists; lists++) { 2712 hlist_for_each_entry(c, *lists, child_node) { 2713 if (!first_node) 2714 seq_putc(s, ','); 2715 first_node = false; 2716 clk_dump_subtree(s, c, 0); 2717 } 2718 } 2719 2720 clk_prepare_unlock(); 2721 2722 seq_puts(s, "}\n"); 2723 return 0; 2724 } 2725 DEFINE_SHOW_ATTRIBUTE(clk_dump); 2726 2727 static const struct { 2728 unsigned long flag; 2729 const char *name; 2730 } clk_flags[] = { 2731 #define ENTRY(f) { f, #f } 2732 ENTRY(CLK_SET_RATE_GATE), 2733 ENTRY(CLK_SET_PARENT_GATE), 2734 ENTRY(CLK_SET_RATE_PARENT), 2735 ENTRY(CLK_IGNORE_UNUSED), 2736 ENTRY(CLK_IS_BASIC), 2737 ENTRY(CLK_GET_RATE_NOCACHE), 2738 ENTRY(CLK_SET_RATE_NO_REPARENT), 2739 ENTRY(CLK_GET_ACCURACY_NOCACHE), 2740 ENTRY(CLK_RECALC_NEW_RATES), 2741 ENTRY(CLK_SET_RATE_UNGATE), 2742 ENTRY(CLK_IS_CRITICAL), 2743 ENTRY(CLK_OPS_PARENT_ENABLE), 2744 ENTRY(CLK_DUTY_CYCLE_PARENT), 2745 #undef ENTRY 2746 }; 2747 2748 static int clk_flags_show(struct seq_file *s, void *data) 2749 { 2750 struct clk_core *core = s->private; 2751 unsigned long flags = core->flags; 2752 unsigned int i; 2753 2754 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) { 2755 if (flags & clk_flags[i].flag) { 2756 seq_printf(s, "%s\n", clk_flags[i].name); 2757 flags &= ~clk_flags[i].flag; 2758 } 2759 } 2760 if (flags) { 2761 /* Unknown flags */ 2762 seq_printf(s, "0x%lx\n", flags); 2763 } 2764 2765 return 0; 2766 } 2767 DEFINE_SHOW_ATTRIBUTE(clk_flags); 2768 2769 static int possible_parents_show(struct seq_file *s, void *data) 2770 { 2771 struct clk_core *core = s->private; 2772 int i; 2773 2774 for (i = 0; i < core->num_parents - 1; i++) 2775 seq_printf(s, "%s ", core->parent_names[i]); 2776 2777 seq_printf(s, "%s\n", core->parent_names[i]); 2778 2779 return 0; 2780 } 2781 DEFINE_SHOW_ATTRIBUTE(possible_parents); 2782 2783 static int clk_duty_cycle_show(struct seq_file *s, void *data) 2784 { 2785 struct clk_core *core = s->private; 2786 struct clk_duty *duty = &core->duty; 2787 2788 seq_printf(s, "%u/%u\n", duty->num, duty->den); 2789 2790 return 0; 2791 } 2792 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle); 2793 2794 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry) 2795 { 2796 struct dentry *root; 2797 2798 if (!core || !pdentry) 2799 return; 2800 2801 root = debugfs_create_dir(core->name, pdentry); 2802 core->dentry = root; 2803 2804 debugfs_create_ulong("clk_rate", 0444, root, &core->rate); 2805 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy); 2806 debugfs_create_u32("clk_phase", 0444, root, &core->phase); 2807 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops); 2808 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count); 2809 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count); 2810 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count); 2811 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count); 2812 debugfs_create_file("clk_duty_cycle", 0444, root, core, 2813 &clk_duty_cycle_fops); 2814 2815 if (core->num_parents > 1) 2816 debugfs_create_file("clk_possible_parents", 0444, root, core, 2817 &possible_parents_fops); 2818 2819 if (core->ops->debug_init) 2820 core->ops->debug_init(core->hw, core->dentry); 2821 } 2822 2823 /** 2824 * clk_debug_register - add a clk node to the debugfs clk directory 2825 * @core: the clk being added to the debugfs clk directory 2826 * 2827 * Dynamically adds a clk to the debugfs clk directory if debugfs has been 2828 * initialized. Otherwise it bails out early since the debugfs clk directory 2829 * will be created lazily by clk_debug_init as part of a late_initcall. 2830 */ 2831 static void clk_debug_register(struct clk_core *core) 2832 { 2833 mutex_lock(&clk_debug_lock); 2834 hlist_add_head(&core->debug_node, &clk_debug_list); 2835 if (inited) 2836 clk_debug_create_one(core, rootdir); 2837 mutex_unlock(&clk_debug_lock); 2838 } 2839 2840 /** 2841 * clk_debug_unregister - remove a clk node from the debugfs clk directory 2842 * @core: the clk being removed from the debugfs clk directory 2843 * 2844 * Dynamically removes a clk and all its child nodes from the 2845 * debugfs clk directory if clk->dentry points to debugfs created by 2846 * clk_debug_register in __clk_core_init. 2847 */ 2848 static void clk_debug_unregister(struct clk_core *core) 2849 { 2850 mutex_lock(&clk_debug_lock); 2851 hlist_del_init(&core->debug_node); 2852 debugfs_remove_recursive(core->dentry); 2853 core->dentry = NULL; 2854 mutex_unlock(&clk_debug_lock); 2855 } 2856 2857 /** 2858 * clk_debug_init - lazily populate the debugfs clk directory 2859 * 2860 * clks are often initialized very early during boot before memory can be 2861 * dynamically allocated and well before debugfs is setup. This function 2862 * populates the debugfs clk directory once at boot-time when we know that 2863 * debugfs is setup. It should only be called once at boot-time, all other clks 2864 * added dynamically will be done so with clk_debug_register. 2865 */ 2866 static int __init clk_debug_init(void) 2867 { 2868 struct clk_core *core; 2869 2870 rootdir = debugfs_create_dir("clk", NULL); 2871 2872 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists, 2873 &clk_summary_fops); 2874 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists, 2875 &clk_dump_fops); 2876 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list, 2877 &clk_summary_fops); 2878 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list, 2879 &clk_dump_fops); 2880 2881 mutex_lock(&clk_debug_lock); 2882 hlist_for_each_entry(core, &clk_debug_list, debug_node) 2883 clk_debug_create_one(core, rootdir); 2884 2885 inited = 1; 2886 mutex_unlock(&clk_debug_lock); 2887 2888 return 0; 2889 } 2890 late_initcall(clk_debug_init); 2891 #else 2892 static inline void clk_debug_register(struct clk_core *core) { } 2893 static inline void clk_debug_reparent(struct clk_core *core, 2894 struct clk_core *new_parent) 2895 { 2896 } 2897 static inline void clk_debug_unregister(struct clk_core *core) 2898 { 2899 } 2900 #endif 2901 2902 /** 2903 * __clk_core_init - initialize the data structures in a struct clk_core 2904 * @core: clk_core being initialized 2905 * 2906 * Initializes the lists in struct clk_core, queries the hardware for the 2907 * parent and rate and sets them both. 2908 */ 2909 static int __clk_core_init(struct clk_core *core) 2910 { 2911 int i, ret; 2912 struct clk_core *orphan; 2913 struct hlist_node *tmp2; 2914 unsigned long rate; 2915 2916 if (!core) 2917 return -EINVAL; 2918 2919 clk_prepare_lock(); 2920 2921 ret = clk_pm_runtime_get(core); 2922 if (ret) 2923 goto unlock; 2924 2925 /* check to see if a clock with this name is already registered */ 2926 if (clk_core_lookup(core->name)) { 2927 pr_debug("%s: clk %s already initialized\n", 2928 __func__, core->name); 2929 ret = -EEXIST; 2930 goto out; 2931 } 2932 2933 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */ 2934 if (core->ops->set_rate && 2935 !((core->ops->round_rate || core->ops->determine_rate) && 2936 core->ops->recalc_rate)) { 2937 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n", 2938 __func__, core->name); 2939 ret = -EINVAL; 2940 goto out; 2941 } 2942 2943 if (core->ops->set_parent && !core->ops->get_parent) { 2944 pr_err("%s: %s must implement .get_parent & .set_parent\n", 2945 __func__, core->name); 2946 ret = -EINVAL; 2947 goto out; 2948 } 2949 2950 if (core->num_parents > 1 && !core->ops->get_parent) { 2951 pr_err("%s: %s must implement .get_parent as it has multi parents\n", 2952 __func__, core->name); 2953 ret = -EINVAL; 2954 goto out; 2955 } 2956 2957 if (core->ops->set_rate_and_parent && 2958 !(core->ops->set_parent && core->ops->set_rate)) { 2959 pr_err("%s: %s must implement .set_parent & .set_rate\n", 2960 __func__, core->name); 2961 ret = -EINVAL; 2962 goto out; 2963 } 2964 2965 /* throw a WARN if any entries in parent_names are NULL */ 2966 for (i = 0; i < core->num_parents; i++) 2967 WARN(!core->parent_names[i], 2968 "%s: invalid NULL in %s's .parent_names\n", 2969 __func__, core->name); 2970 2971 core->parent = __clk_init_parent(core); 2972 2973 /* 2974 * Populate core->parent if parent has already been clk_core_init'd. If 2975 * parent has not yet been clk_core_init'd then place clk in the orphan 2976 * list. If clk doesn't have any parents then place it in the root 2977 * clk list. 2978 * 2979 * Every time a new clk is clk_init'd then we walk the list of orphan 2980 * clocks and re-parent any that are children of the clock currently 2981 * being clk_init'd. 2982 */ 2983 if (core->parent) { 2984 hlist_add_head(&core->child_node, 2985 &core->parent->children); 2986 core->orphan = core->parent->orphan; 2987 } else if (!core->num_parents) { 2988 hlist_add_head(&core->child_node, &clk_root_list); 2989 core->orphan = false; 2990 } else { 2991 hlist_add_head(&core->child_node, &clk_orphan_list); 2992 core->orphan = true; 2993 } 2994 2995 /* 2996 * optional platform-specific magic 2997 * 2998 * The .init callback is not used by any of the basic clock types, but 2999 * exists for weird hardware that must perform initialization magic. 3000 * Please consider other ways of solving initialization problems before 3001 * using this callback, as its use is discouraged. 3002 */ 3003 if (core->ops->init) 3004 core->ops->init(core->hw); 3005 3006 /* 3007 * Set clk's accuracy. The preferred method is to use 3008 * .recalc_accuracy. For simple clocks and lazy developers the default 3009 * fallback is to use the parent's accuracy. If a clock doesn't have a 3010 * parent (or is orphaned) then accuracy is set to zero (perfect 3011 * clock). 3012 */ 3013 if (core->ops->recalc_accuracy) 3014 core->accuracy = core->ops->recalc_accuracy(core->hw, 3015 __clk_get_accuracy(core->parent)); 3016 else if (core->parent) 3017 core->accuracy = core->parent->accuracy; 3018 else 3019 core->accuracy = 0; 3020 3021 /* 3022 * Set clk's phase. 3023 * Since a phase is by definition relative to its parent, just 3024 * query the current clock phase, or just assume it's in phase. 3025 */ 3026 if (core->ops->get_phase) 3027 core->phase = core->ops->get_phase(core->hw); 3028 else 3029 core->phase = 0; 3030 3031 /* 3032 * Set clk's duty cycle. 3033 */ 3034 clk_core_update_duty_cycle_nolock(core); 3035 3036 /* 3037 * Set clk's rate. The preferred method is to use .recalc_rate. For 3038 * simple clocks and lazy developers the default fallback is to use the 3039 * parent's rate. If a clock doesn't have a parent (or is orphaned) 3040 * then rate is set to zero. 3041 */ 3042 if (core->ops->recalc_rate) 3043 rate = core->ops->recalc_rate(core->hw, 3044 clk_core_get_rate_nolock(core->parent)); 3045 else if (core->parent) 3046 rate = core->parent->rate; 3047 else 3048 rate = 0; 3049 core->rate = core->req_rate = rate; 3050 3051 /* 3052 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks 3053 * don't get accidentally disabled when walking the orphan tree and 3054 * reparenting clocks 3055 */ 3056 if (core->flags & CLK_IS_CRITICAL) { 3057 unsigned long flags; 3058 3059 clk_core_prepare(core); 3060 3061 flags = clk_enable_lock(); 3062 clk_core_enable(core); 3063 clk_enable_unlock(flags); 3064 } 3065 3066 /* 3067 * walk the list of orphan clocks and reparent any that newly finds a 3068 * parent. 3069 */ 3070 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { 3071 struct clk_core *parent = __clk_init_parent(orphan); 3072 3073 /* 3074 * We need to use __clk_set_parent_before() and _after() to 3075 * to properly migrate any prepare/enable count of the orphan 3076 * clock. This is important for CLK_IS_CRITICAL clocks, which 3077 * are enabled during init but might not have a parent yet. 3078 */ 3079 if (parent) { 3080 /* update the clk tree topology */ 3081 __clk_set_parent_before(orphan, parent); 3082 __clk_set_parent_after(orphan, parent, NULL); 3083 __clk_recalc_accuracies(orphan); 3084 __clk_recalc_rates(orphan, 0); 3085 } 3086 } 3087 3088 kref_init(&core->ref); 3089 out: 3090 clk_pm_runtime_put(core); 3091 unlock: 3092 clk_prepare_unlock(); 3093 3094 if (!ret) 3095 clk_debug_register(core); 3096 3097 return ret; 3098 } 3099 3100 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id, 3101 const char *con_id) 3102 { 3103 struct clk *clk; 3104 3105 /* This is to allow this function to be chained to others */ 3106 if (IS_ERR_OR_NULL(hw)) 3107 return ERR_CAST(hw); 3108 3109 clk = kzalloc(sizeof(*clk), GFP_KERNEL); 3110 if (!clk) 3111 return ERR_PTR(-ENOMEM); 3112 3113 clk->core = hw->core; 3114 clk->dev_id = dev_id; 3115 clk->con_id = kstrdup_const(con_id, GFP_KERNEL); 3116 clk->max_rate = ULONG_MAX; 3117 3118 clk_prepare_lock(); 3119 hlist_add_head(&clk->clks_node, &hw->core->clks); 3120 clk_prepare_unlock(); 3121 3122 return clk; 3123 } 3124 3125 void __clk_free_clk(struct clk *clk) 3126 { 3127 clk_prepare_lock(); 3128 hlist_del(&clk->clks_node); 3129 clk_prepare_unlock(); 3130 3131 kfree_const(clk->con_id); 3132 kfree(clk); 3133 } 3134 3135 /** 3136 * clk_register - allocate a new clock, register it and return an opaque cookie 3137 * @dev: device that is registering this clock 3138 * @hw: link to hardware-specific clock data 3139 * 3140 * clk_register is the primary interface for populating the clock tree with new 3141 * clock nodes. It returns a pointer to the newly allocated struct clk which 3142 * cannot be dereferenced by driver code but may be used in conjunction with the 3143 * rest of the clock API. In the event of an error clk_register will return an 3144 * error code; drivers must test for an error code after calling clk_register. 3145 */ 3146 struct clk *clk_register(struct device *dev, struct clk_hw *hw) 3147 { 3148 int i, ret; 3149 struct clk_core *core; 3150 3151 core = kzalloc(sizeof(*core), GFP_KERNEL); 3152 if (!core) { 3153 ret = -ENOMEM; 3154 goto fail_out; 3155 } 3156 3157 core->name = kstrdup_const(hw->init->name, GFP_KERNEL); 3158 if (!core->name) { 3159 ret = -ENOMEM; 3160 goto fail_name; 3161 } 3162 3163 if (WARN_ON(!hw->init->ops)) { 3164 ret = -EINVAL; 3165 goto fail_ops; 3166 } 3167 core->ops = hw->init->ops; 3168 3169 if (dev && pm_runtime_enabled(dev)) 3170 core->dev = dev; 3171 if (dev && dev->driver) 3172 core->owner = dev->driver->owner; 3173 core->hw = hw; 3174 core->flags = hw->init->flags; 3175 core->num_parents = hw->init->num_parents; 3176 core->min_rate = 0; 3177 core->max_rate = ULONG_MAX; 3178 hw->core = core; 3179 3180 /* allocate local copy in case parent_names is __initdata */ 3181 core->parent_names = kcalloc(core->num_parents, sizeof(char *), 3182 GFP_KERNEL); 3183 3184 if (!core->parent_names) { 3185 ret = -ENOMEM; 3186 goto fail_parent_names; 3187 } 3188 3189 3190 /* copy each string name in case parent_names is __initdata */ 3191 for (i = 0; i < core->num_parents; i++) { 3192 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i], 3193 GFP_KERNEL); 3194 if (!core->parent_names[i]) { 3195 ret = -ENOMEM; 3196 goto fail_parent_names_copy; 3197 } 3198 } 3199 3200 /* avoid unnecessary string look-ups of clk_core's possible parents. */ 3201 core->parents = kcalloc(core->num_parents, sizeof(*core->parents), 3202 GFP_KERNEL); 3203 if (!core->parents) { 3204 ret = -ENOMEM; 3205 goto fail_parents; 3206 }; 3207 3208 INIT_HLIST_HEAD(&core->clks); 3209 3210 hw->clk = __clk_create_clk(hw, NULL, NULL); 3211 if (IS_ERR(hw->clk)) { 3212 ret = PTR_ERR(hw->clk); 3213 goto fail_parents; 3214 } 3215 3216 ret = __clk_core_init(core); 3217 if (!ret) 3218 return hw->clk; 3219 3220 __clk_free_clk(hw->clk); 3221 hw->clk = NULL; 3222 3223 fail_parents: 3224 kfree(core->parents); 3225 fail_parent_names_copy: 3226 while (--i >= 0) 3227 kfree_const(core->parent_names[i]); 3228 kfree(core->parent_names); 3229 fail_parent_names: 3230 fail_ops: 3231 kfree_const(core->name); 3232 fail_name: 3233 kfree(core); 3234 fail_out: 3235 return ERR_PTR(ret); 3236 } 3237 EXPORT_SYMBOL_GPL(clk_register); 3238 3239 /** 3240 * clk_hw_register - register a clk_hw and return an error code 3241 * @dev: device that is registering this clock 3242 * @hw: link to hardware-specific clock data 3243 * 3244 * clk_hw_register is the primary interface for populating the clock tree with 3245 * new clock nodes. It returns an integer equal to zero indicating success or 3246 * less than zero indicating failure. Drivers must test for an error code after 3247 * calling clk_hw_register(). 3248 */ 3249 int clk_hw_register(struct device *dev, struct clk_hw *hw) 3250 { 3251 return PTR_ERR_OR_ZERO(clk_register(dev, hw)); 3252 } 3253 EXPORT_SYMBOL_GPL(clk_hw_register); 3254 3255 /* Free memory allocated for a clock. */ 3256 static void __clk_release(struct kref *ref) 3257 { 3258 struct clk_core *core = container_of(ref, struct clk_core, ref); 3259 int i = core->num_parents; 3260 3261 lockdep_assert_held(&prepare_lock); 3262 3263 kfree(core->parents); 3264 while (--i >= 0) 3265 kfree_const(core->parent_names[i]); 3266 3267 kfree(core->parent_names); 3268 kfree_const(core->name); 3269 kfree(core); 3270 } 3271 3272 /* 3273 * Empty clk_ops for unregistered clocks. These are used temporarily 3274 * after clk_unregister() was called on a clock and until last clock 3275 * consumer calls clk_put() and the struct clk object is freed. 3276 */ 3277 static int clk_nodrv_prepare_enable(struct clk_hw *hw) 3278 { 3279 return -ENXIO; 3280 } 3281 3282 static void clk_nodrv_disable_unprepare(struct clk_hw *hw) 3283 { 3284 WARN_ON_ONCE(1); 3285 } 3286 3287 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate, 3288 unsigned long parent_rate) 3289 { 3290 return -ENXIO; 3291 } 3292 3293 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index) 3294 { 3295 return -ENXIO; 3296 } 3297 3298 static const struct clk_ops clk_nodrv_ops = { 3299 .enable = clk_nodrv_prepare_enable, 3300 .disable = clk_nodrv_disable_unprepare, 3301 .prepare = clk_nodrv_prepare_enable, 3302 .unprepare = clk_nodrv_disable_unprepare, 3303 .set_rate = clk_nodrv_set_rate, 3304 .set_parent = clk_nodrv_set_parent, 3305 }; 3306 3307 /** 3308 * clk_unregister - unregister a currently registered clock 3309 * @clk: clock to unregister 3310 */ 3311 void clk_unregister(struct clk *clk) 3312 { 3313 unsigned long flags; 3314 3315 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) 3316 return; 3317 3318 clk_debug_unregister(clk->core); 3319 3320 clk_prepare_lock(); 3321 3322 if (clk->core->ops == &clk_nodrv_ops) { 3323 pr_err("%s: unregistered clock: %s\n", __func__, 3324 clk->core->name); 3325 goto unlock; 3326 } 3327 /* 3328 * Assign empty clock ops for consumers that might still hold 3329 * a reference to this clock. 3330 */ 3331 flags = clk_enable_lock(); 3332 clk->core->ops = &clk_nodrv_ops; 3333 clk_enable_unlock(flags); 3334 3335 if (!hlist_empty(&clk->core->children)) { 3336 struct clk_core *child; 3337 struct hlist_node *t; 3338 3339 /* Reparent all children to the orphan list. */ 3340 hlist_for_each_entry_safe(child, t, &clk->core->children, 3341 child_node) 3342 clk_core_set_parent_nolock(child, NULL); 3343 } 3344 3345 hlist_del_init(&clk->core->child_node); 3346 3347 if (clk->core->prepare_count) 3348 pr_warn("%s: unregistering prepared clock: %s\n", 3349 __func__, clk->core->name); 3350 3351 if (clk->core->protect_count) 3352 pr_warn("%s: unregistering protected clock: %s\n", 3353 __func__, clk->core->name); 3354 3355 kref_put(&clk->core->ref, __clk_release); 3356 unlock: 3357 clk_prepare_unlock(); 3358 } 3359 EXPORT_SYMBOL_GPL(clk_unregister); 3360 3361 /** 3362 * clk_hw_unregister - unregister a currently registered clk_hw 3363 * @hw: hardware-specific clock data to unregister 3364 */ 3365 void clk_hw_unregister(struct clk_hw *hw) 3366 { 3367 clk_unregister(hw->clk); 3368 } 3369 EXPORT_SYMBOL_GPL(clk_hw_unregister); 3370 3371 static void devm_clk_release(struct device *dev, void *res) 3372 { 3373 clk_unregister(*(struct clk **)res); 3374 } 3375 3376 static void devm_clk_hw_release(struct device *dev, void *res) 3377 { 3378 clk_hw_unregister(*(struct clk_hw **)res); 3379 } 3380 3381 /** 3382 * devm_clk_register - resource managed clk_register() 3383 * @dev: device that is registering this clock 3384 * @hw: link to hardware-specific clock data 3385 * 3386 * Managed clk_register(). Clocks returned from this function are 3387 * automatically clk_unregister()ed on driver detach. See clk_register() for 3388 * more information. 3389 */ 3390 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) 3391 { 3392 struct clk *clk; 3393 struct clk **clkp; 3394 3395 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL); 3396 if (!clkp) 3397 return ERR_PTR(-ENOMEM); 3398 3399 clk = clk_register(dev, hw); 3400 if (!IS_ERR(clk)) { 3401 *clkp = clk; 3402 devres_add(dev, clkp); 3403 } else { 3404 devres_free(clkp); 3405 } 3406 3407 return clk; 3408 } 3409 EXPORT_SYMBOL_GPL(devm_clk_register); 3410 3411 /** 3412 * devm_clk_hw_register - resource managed clk_hw_register() 3413 * @dev: device that is registering this clock 3414 * @hw: link to hardware-specific clock data 3415 * 3416 * Managed clk_hw_register(). Clocks registered by this function are 3417 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register() 3418 * for more information. 3419 */ 3420 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw) 3421 { 3422 struct clk_hw **hwp; 3423 int ret; 3424 3425 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL); 3426 if (!hwp) 3427 return -ENOMEM; 3428 3429 ret = clk_hw_register(dev, hw); 3430 if (!ret) { 3431 *hwp = hw; 3432 devres_add(dev, hwp); 3433 } else { 3434 devres_free(hwp); 3435 } 3436 3437 return ret; 3438 } 3439 EXPORT_SYMBOL_GPL(devm_clk_hw_register); 3440 3441 static int devm_clk_match(struct device *dev, void *res, void *data) 3442 { 3443 struct clk *c = res; 3444 if (WARN_ON(!c)) 3445 return 0; 3446 return c == data; 3447 } 3448 3449 static int devm_clk_hw_match(struct device *dev, void *res, void *data) 3450 { 3451 struct clk_hw *hw = res; 3452 3453 if (WARN_ON(!hw)) 3454 return 0; 3455 return hw == data; 3456 } 3457 3458 /** 3459 * devm_clk_unregister - resource managed clk_unregister() 3460 * @clk: clock to unregister 3461 * 3462 * Deallocate a clock allocated with devm_clk_register(). Normally 3463 * this function will not need to be called and the resource management 3464 * code will ensure that the resource is freed. 3465 */ 3466 void devm_clk_unregister(struct device *dev, struct clk *clk) 3467 { 3468 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); 3469 } 3470 EXPORT_SYMBOL_GPL(devm_clk_unregister); 3471 3472 /** 3473 * devm_clk_hw_unregister - resource managed clk_hw_unregister() 3474 * @dev: device that is unregistering the hardware-specific clock data 3475 * @hw: link to hardware-specific clock data 3476 * 3477 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally 3478 * this function will not need to be called and the resource management 3479 * code will ensure that the resource is freed. 3480 */ 3481 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw) 3482 { 3483 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match, 3484 hw)); 3485 } 3486 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister); 3487 3488 /* 3489 * clkdev helpers 3490 */ 3491 int __clk_get(struct clk *clk) 3492 { 3493 struct clk_core *core = !clk ? NULL : clk->core; 3494 3495 if (core) { 3496 if (!try_module_get(core->owner)) 3497 return 0; 3498 3499 kref_get(&core->ref); 3500 } 3501 return 1; 3502 } 3503 3504 void __clk_put(struct clk *clk) 3505 { 3506 struct module *owner; 3507 3508 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) 3509 return; 3510 3511 clk_prepare_lock(); 3512 3513 /* 3514 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a 3515 * given user should be balanced with calls to clk_rate_exclusive_put() 3516 * and by that same consumer 3517 */ 3518 if (WARN_ON(clk->exclusive_count)) { 3519 /* We voiced our concern, let's sanitize the situation */ 3520 clk->core->protect_count -= (clk->exclusive_count - 1); 3521 clk_core_rate_unprotect(clk->core); 3522 clk->exclusive_count = 0; 3523 } 3524 3525 hlist_del(&clk->clks_node); 3526 if (clk->min_rate > clk->core->req_rate || 3527 clk->max_rate < clk->core->req_rate) 3528 clk_core_set_rate_nolock(clk->core, clk->core->req_rate); 3529 3530 owner = clk->core->owner; 3531 kref_put(&clk->core->ref, __clk_release); 3532 3533 clk_prepare_unlock(); 3534 3535 module_put(owner); 3536 3537 kfree(clk); 3538 } 3539 3540 /*** clk rate change notifiers ***/ 3541 3542 /** 3543 * clk_notifier_register - add a clk rate change notifier 3544 * @clk: struct clk * to watch 3545 * @nb: struct notifier_block * with callback info 3546 * 3547 * Request notification when clk's rate changes. This uses an SRCU 3548 * notifier because we want it to block and notifier unregistrations are 3549 * uncommon. The callbacks associated with the notifier must not 3550 * re-enter into the clk framework by calling any top-level clk APIs; 3551 * this will cause a nested prepare_lock mutex. 3552 * 3553 * In all notification cases (pre, post and abort rate change) the original 3554 * clock rate is passed to the callback via struct clk_notifier_data.old_rate 3555 * and the new frequency is passed via struct clk_notifier_data.new_rate. 3556 * 3557 * clk_notifier_register() must be called from non-atomic context. 3558 * Returns -EINVAL if called with null arguments, -ENOMEM upon 3559 * allocation failure; otherwise, passes along the return value of 3560 * srcu_notifier_chain_register(). 3561 */ 3562 int clk_notifier_register(struct clk *clk, struct notifier_block *nb) 3563 { 3564 struct clk_notifier *cn; 3565 int ret = -ENOMEM; 3566 3567 if (!clk || !nb) 3568 return -EINVAL; 3569 3570 clk_prepare_lock(); 3571 3572 /* search the list of notifiers for this clk */ 3573 list_for_each_entry(cn, &clk_notifier_list, node) 3574 if (cn->clk == clk) 3575 break; 3576 3577 /* if clk wasn't in the notifier list, allocate new clk_notifier */ 3578 if (cn->clk != clk) { 3579 cn = kzalloc(sizeof(*cn), GFP_KERNEL); 3580 if (!cn) 3581 goto out; 3582 3583 cn->clk = clk; 3584 srcu_init_notifier_head(&cn->notifier_head); 3585 3586 list_add(&cn->node, &clk_notifier_list); 3587 } 3588 3589 ret = srcu_notifier_chain_register(&cn->notifier_head, nb); 3590 3591 clk->core->notifier_count++; 3592 3593 out: 3594 clk_prepare_unlock(); 3595 3596 return ret; 3597 } 3598 EXPORT_SYMBOL_GPL(clk_notifier_register); 3599 3600 /** 3601 * clk_notifier_unregister - remove a clk rate change notifier 3602 * @clk: struct clk * 3603 * @nb: struct notifier_block * with callback info 3604 * 3605 * Request no further notification for changes to 'clk' and frees memory 3606 * allocated in clk_notifier_register. 3607 * 3608 * Returns -EINVAL if called with null arguments; otherwise, passes 3609 * along the return value of srcu_notifier_chain_unregister(). 3610 */ 3611 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) 3612 { 3613 struct clk_notifier *cn = NULL; 3614 int ret = -EINVAL; 3615 3616 if (!clk || !nb) 3617 return -EINVAL; 3618 3619 clk_prepare_lock(); 3620 3621 list_for_each_entry(cn, &clk_notifier_list, node) 3622 if (cn->clk == clk) 3623 break; 3624 3625 if (cn->clk == clk) { 3626 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); 3627 3628 clk->core->notifier_count--; 3629 3630 /* XXX the notifier code should handle this better */ 3631 if (!cn->notifier_head.head) { 3632 srcu_cleanup_notifier_head(&cn->notifier_head); 3633 list_del(&cn->node); 3634 kfree(cn); 3635 } 3636 3637 } else { 3638 ret = -ENOENT; 3639 } 3640 3641 clk_prepare_unlock(); 3642 3643 return ret; 3644 } 3645 EXPORT_SYMBOL_GPL(clk_notifier_unregister); 3646 3647 #ifdef CONFIG_OF 3648 /** 3649 * struct of_clk_provider - Clock provider registration structure 3650 * @link: Entry in global list of clock providers 3651 * @node: Pointer to device tree node of clock provider 3652 * @get: Get clock callback. Returns NULL or a struct clk for the 3653 * given clock specifier 3654 * @data: context pointer to be passed into @get callback 3655 */ 3656 struct of_clk_provider { 3657 struct list_head link; 3658 3659 struct device_node *node; 3660 struct clk *(*get)(struct of_phandle_args *clkspec, void *data); 3661 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data); 3662 void *data; 3663 }; 3664 3665 static const struct of_device_id __clk_of_table_sentinel 3666 __used __section(__clk_of_table_end); 3667 3668 static LIST_HEAD(of_clk_providers); 3669 static DEFINE_MUTEX(of_clk_mutex); 3670 3671 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, 3672 void *data) 3673 { 3674 return data; 3675 } 3676 EXPORT_SYMBOL_GPL(of_clk_src_simple_get); 3677 3678 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data) 3679 { 3680 return data; 3681 } 3682 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get); 3683 3684 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) 3685 { 3686 struct clk_onecell_data *clk_data = data; 3687 unsigned int idx = clkspec->args[0]; 3688 3689 if (idx >= clk_data->clk_num) { 3690 pr_err("%s: invalid clock index %u\n", __func__, idx); 3691 return ERR_PTR(-EINVAL); 3692 } 3693 3694 return clk_data->clks[idx]; 3695 } 3696 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); 3697 3698 struct clk_hw * 3699 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data) 3700 { 3701 struct clk_hw_onecell_data *hw_data = data; 3702 unsigned int idx = clkspec->args[0]; 3703 3704 if (idx >= hw_data->num) { 3705 pr_err("%s: invalid index %u\n", __func__, idx); 3706 return ERR_PTR(-EINVAL); 3707 } 3708 3709 return hw_data->hws[idx]; 3710 } 3711 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get); 3712 3713 /** 3714 * of_clk_add_provider() - Register a clock provider for a node 3715 * @np: Device node pointer associated with clock provider 3716 * @clk_src_get: callback for decoding clock 3717 * @data: context pointer for @clk_src_get callback. 3718 */ 3719 int of_clk_add_provider(struct device_node *np, 3720 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, 3721 void *data), 3722 void *data) 3723 { 3724 struct of_clk_provider *cp; 3725 int ret; 3726 3727 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 3728 if (!cp) 3729 return -ENOMEM; 3730 3731 cp->node = of_node_get(np); 3732 cp->data = data; 3733 cp->get = clk_src_get; 3734 3735 mutex_lock(&of_clk_mutex); 3736 list_add(&cp->link, &of_clk_providers); 3737 mutex_unlock(&of_clk_mutex); 3738 pr_debug("Added clock from %pOF\n", np); 3739 3740 ret = of_clk_set_defaults(np, true); 3741 if (ret < 0) 3742 of_clk_del_provider(np); 3743 3744 return ret; 3745 } 3746 EXPORT_SYMBOL_GPL(of_clk_add_provider); 3747 3748 /** 3749 * of_clk_add_hw_provider() - Register a clock provider for a node 3750 * @np: Device node pointer associated with clock provider 3751 * @get: callback for decoding clk_hw 3752 * @data: context pointer for @get callback. 3753 */ 3754 int of_clk_add_hw_provider(struct device_node *np, 3755 struct clk_hw *(*get)(struct of_phandle_args *clkspec, 3756 void *data), 3757 void *data) 3758 { 3759 struct of_clk_provider *cp; 3760 int ret; 3761 3762 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 3763 if (!cp) 3764 return -ENOMEM; 3765 3766 cp->node = of_node_get(np); 3767 cp->data = data; 3768 cp->get_hw = get; 3769 3770 mutex_lock(&of_clk_mutex); 3771 list_add(&cp->link, &of_clk_providers); 3772 mutex_unlock(&of_clk_mutex); 3773 pr_debug("Added clk_hw provider from %pOF\n", np); 3774 3775 ret = of_clk_set_defaults(np, true); 3776 if (ret < 0) 3777 of_clk_del_provider(np); 3778 3779 return ret; 3780 } 3781 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider); 3782 3783 static void devm_of_clk_release_provider(struct device *dev, void *res) 3784 { 3785 of_clk_del_provider(*(struct device_node **)res); 3786 } 3787 3788 int devm_of_clk_add_hw_provider(struct device *dev, 3789 struct clk_hw *(*get)(struct of_phandle_args *clkspec, 3790 void *data), 3791 void *data) 3792 { 3793 struct device_node **ptr, *np; 3794 int ret; 3795 3796 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr), 3797 GFP_KERNEL); 3798 if (!ptr) 3799 return -ENOMEM; 3800 3801 np = dev->of_node; 3802 ret = of_clk_add_hw_provider(np, get, data); 3803 if (!ret) { 3804 *ptr = np; 3805 devres_add(dev, ptr); 3806 } else { 3807 devres_free(ptr); 3808 } 3809 3810 return ret; 3811 } 3812 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider); 3813 3814 /** 3815 * of_clk_del_provider() - Remove a previously registered clock provider 3816 * @np: Device node pointer associated with clock provider 3817 */ 3818 void of_clk_del_provider(struct device_node *np) 3819 { 3820 struct of_clk_provider *cp; 3821 3822 mutex_lock(&of_clk_mutex); 3823 list_for_each_entry(cp, &of_clk_providers, link) { 3824 if (cp->node == np) { 3825 list_del(&cp->link); 3826 of_node_put(cp->node); 3827 kfree(cp); 3828 break; 3829 } 3830 } 3831 mutex_unlock(&of_clk_mutex); 3832 } 3833 EXPORT_SYMBOL_GPL(of_clk_del_provider); 3834 3835 static int devm_clk_provider_match(struct device *dev, void *res, void *data) 3836 { 3837 struct device_node **np = res; 3838 3839 if (WARN_ON(!np || !*np)) 3840 return 0; 3841 3842 return *np == data; 3843 } 3844 3845 void devm_of_clk_del_provider(struct device *dev) 3846 { 3847 int ret; 3848 3849 ret = devres_release(dev, devm_of_clk_release_provider, 3850 devm_clk_provider_match, dev->of_node); 3851 3852 WARN_ON(ret); 3853 } 3854 EXPORT_SYMBOL(devm_of_clk_del_provider); 3855 3856 static struct clk_hw * 3857 __of_clk_get_hw_from_provider(struct of_clk_provider *provider, 3858 struct of_phandle_args *clkspec) 3859 { 3860 struct clk *clk; 3861 3862 if (provider->get_hw) 3863 return provider->get_hw(clkspec, provider->data); 3864 3865 clk = provider->get(clkspec, provider->data); 3866 if (IS_ERR(clk)) 3867 return ERR_CAST(clk); 3868 return __clk_get_hw(clk); 3869 } 3870 3871 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec, 3872 const char *dev_id, const char *con_id) 3873 { 3874 struct of_clk_provider *provider; 3875 struct clk *clk = ERR_PTR(-EPROBE_DEFER); 3876 struct clk_hw *hw; 3877 3878 if (!clkspec) 3879 return ERR_PTR(-EINVAL); 3880 3881 /* Check if we have such a provider in our array */ 3882 mutex_lock(&of_clk_mutex); 3883 list_for_each_entry(provider, &of_clk_providers, link) { 3884 if (provider->node == clkspec->np) { 3885 hw = __of_clk_get_hw_from_provider(provider, clkspec); 3886 clk = __clk_create_clk(hw, dev_id, con_id); 3887 } 3888 3889 if (!IS_ERR(clk)) { 3890 if (!__clk_get(clk)) { 3891 __clk_free_clk(clk); 3892 clk = ERR_PTR(-ENOENT); 3893 } 3894 3895 break; 3896 } 3897 } 3898 mutex_unlock(&of_clk_mutex); 3899 3900 return clk; 3901 } 3902 3903 /** 3904 * of_clk_get_from_provider() - Lookup a clock from a clock provider 3905 * @clkspec: pointer to a clock specifier data structure 3906 * 3907 * This function looks up a struct clk from the registered list of clock 3908 * providers, an input is a clock specifier data structure as returned 3909 * from the of_parse_phandle_with_args() function call. 3910 */ 3911 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) 3912 { 3913 return __of_clk_get_from_provider(clkspec, NULL, __func__); 3914 } 3915 EXPORT_SYMBOL_GPL(of_clk_get_from_provider); 3916 3917 /** 3918 * of_clk_get_parent_count() - Count the number of clocks a device node has 3919 * @np: device node to count 3920 * 3921 * Returns: The number of clocks that are possible parents of this node 3922 */ 3923 unsigned int of_clk_get_parent_count(struct device_node *np) 3924 { 3925 int count; 3926 3927 count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); 3928 if (count < 0) 3929 return 0; 3930 3931 return count; 3932 } 3933 EXPORT_SYMBOL_GPL(of_clk_get_parent_count); 3934 3935 const char *of_clk_get_parent_name(struct device_node *np, int index) 3936 { 3937 struct of_phandle_args clkspec; 3938 struct property *prop; 3939 const char *clk_name; 3940 const __be32 *vp; 3941 u32 pv; 3942 int rc; 3943 int count; 3944 struct clk *clk; 3945 3946 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, 3947 &clkspec); 3948 if (rc) 3949 return NULL; 3950 3951 index = clkspec.args_count ? clkspec.args[0] : 0; 3952 count = 0; 3953 3954 /* if there is an indices property, use it to transfer the index 3955 * specified into an array offset for the clock-output-names property. 3956 */ 3957 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { 3958 if (index == pv) { 3959 index = count; 3960 break; 3961 } 3962 count++; 3963 } 3964 /* We went off the end of 'clock-indices' without finding it */ 3965 if (prop && !vp) 3966 return NULL; 3967 3968 if (of_property_read_string_index(clkspec.np, "clock-output-names", 3969 index, 3970 &clk_name) < 0) { 3971 /* 3972 * Best effort to get the name if the clock has been 3973 * registered with the framework. If the clock isn't 3974 * registered, we return the node name as the name of 3975 * the clock as long as #clock-cells = 0. 3976 */ 3977 clk = of_clk_get_from_provider(&clkspec); 3978 if (IS_ERR(clk)) { 3979 if (clkspec.args_count == 0) 3980 clk_name = clkspec.np->name; 3981 else 3982 clk_name = NULL; 3983 } else { 3984 clk_name = __clk_get_name(clk); 3985 clk_put(clk); 3986 } 3987 } 3988 3989 3990 of_node_put(clkspec.np); 3991 return clk_name; 3992 } 3993 EXPORT_SYMBOL_GPL(of_clk_get_parent_name); 3994 3995 /** 3996 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return 3997 * number of parents 3998 * @np: Device node pointer associated with clock provider 3999 * @parents: pointer to char array that hold the parents' names 4000 * @size: size of the @parents array 4001 * 4002 * Return: number of parents for the clock node. 4003 */ 4004 int of_clk_parent_fill(struct device_node *np, const char **parents, 4005 unsigned int size) 4006 { 4007 unsigned int i = 0; 4008 4009 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL) 4010 i++; 4011 4012 return i; 4013 } 4014 EXPORT_SYMBOL_GPL(of_clk_parent_fill); 4015 4016 struct clock_provider { 4017 void (*clk_init_cb)(struct device_node *); 4018 struct device_node *np; 4019 struct list_head node; 4020 }; 4021 4022 /* 4023 * This function looks for a parent clock. If there is one, then it 4024 * checks that the provider for this parent clock was initialized, in 4025 * this case the parent clock will be ready. 4026 */ 4027 static int parent_ready(struct device_node *np) 4028 { 4029 int i = 0; 4030 4031 while (true) { 4032 struct clk *clk = of_clk_get(np, i); 4033 4034 /* this parent is ready we can check the next one */ 4035 if (!IS_ERR(clk)) { 4036 clk_put(clk); 4037 i++; 4038 continue; 4039 } 4040 4041 /* at least one parent is not ready, we exit now */ 4042 if (PTR_ERR(clk) == -EPROBE_DEFER) 4043 return 0; 4044 4045 /* 4046 * Here we make assumption that the device tree is 4047 * written correctly. So an error means that there is 4048 * no more parent. As we didn't exit yet, then the 4049 * previous parent are ready. If there is no clock 4050 * parent, no need to wait for them, then we can 4051 * consider their absence as being ready 4052 */ 4053 return 1; 4054 } 4055 } 4056 4057 /** 4058 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree 4059 * @np: Device node pointer associated with clock provider 4060 * @index: clock index 4061 * @flags: pointer to top-level framework flags 4062 * 4063 * Detects if the clock-critical property exists and, if so, sets the 4064 * corresponding CLK_IS_CRITICAL flag. 4065 * 4066 * Do not use this function. It exists only for legacy Device Tree 4067 * bindings, such as the one-clock-per-node style that are outdated. 4068 * Those bindings typically put all clock data into .dts and the Linux 4069 * driver has no clock data, thus making it impossible to set this flag 4070 * correctly from the driver. Only those drivers may call 4071 * of_clk_detect_critical from their setup functions. 4072 * 4073 * Return: error code or zero on success 4074 */ 4075 int of_clk_detect_critical(struct device_node *np, 4076 int index, unsigned long *flags) 4077 { 4078 struct property *prop; 4079 const __be32 *cur; 4080 uint32_t idx; 4081 4082 if (!np || !flags) 4083 return -EINVAL; 4084 4085 of_property_for_each_u32(np, "clock-critical", prop, cur, idx) 4086 if (index == idx) 4087 *flags |= CLK_IS_CRITICAL; 4088 4089 return 0; 4090 } 4091 4092 /** 4093 * of_clk_init() - Scan and init clock providers from the DT 4094 * @matches: array of compatible values and init functions for providers. 4095 * 4096 * This function scans the device tree for matching clock providers 4097 * and calls their initialization functions. It also does it by trying 4098 * to follow the dependencies. 4099 */ 4100 void __init of_clk_init(const struct of_device_id *matches) 4101 { 4102 const struct of_device_id *match; 4103 struct device_node *np; 4104 struct clock_provider *clk_provider, *next; 4105 bool is_init_done; 4106 bool force = false; 4107 LIST_HEAD(clk_provider_list); 4108 4109 if (!matches) 4110 matches = &__clk_of_table; 4111 4112 /* First prepare the list of the clocks providers */ 4113 for_each_matching_node_and_match(np, matches, &match) { 4114 struct clock_provider *parent; 4115 4116 if (!of_device_is_available(np)) 4117 continue; 4118 4119 parent = kzalloc(sizeof(*parent), GFP_KERNEL); 4120 if (!parent) { 4121 list_for_each_entry_safe(clk_provider, next, 4122 &clk_provider_list, node) { 4123 list_del(&clk_provider->node); 4124 of_node_put(clk_provider->np); 4125 kfree(clk_provider); 4126 } 4127 of_node_put(np); 4128 return; 4129 } 4130 4131 parent->clk_init_cb = match->data; 4132 parent->np = of_node_get(np); 4133 list_add_tail(&parent->node, &clk_provider_list); 4134 } 4135 4136 while (!list_empty(&clk_provider_list)) { 4137 is_init_done = false; 4138 list_for_each_entry_safe(clk_provider, next, 4139 &clk_provider_list, node) { 4140 if (force || parent_ready(clk_provider->np)) { 4141 4142 /* Don't populate platform devices */ 4143 of_node_set_flag(clk_provider->np, 4144 OF_POPULATED); 4145 4146 clk_provider->clk_init_cb(clk_provider->np); 4147 of_clk_set_defaults(clk_provider->np, true); 4148 4149 list_del(&clk_provider->node); 4150 of_node_put(clk_provider->np); 4151 kfree(clk_provider); 4152 is_init_done = true; 4153 } 4154 } 4155 4156 /* 4157 * We didn't manage to initialize any of the 4158 * remaining providers during the last loop, so now we 4159 * initialize all the remaining ones unconditionally 4160 * in case the clock parent was not mandatory 4161 */ 4162 if (!is_init_done) 4163 force = true; 4164 } 4165 } 4166 #endif 4167