ccu_common.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2016 Maxime Ripard
  3. *
  4. * Maxime Ripard <maxime.ripard@free-electrons.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/slab.h>
  19. #include "ccu_common.h"
  20. #include "ccu_reset.h"
  21. static DEFINE_SPINLOCK(ccu_lock);
  22. void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
  23. {
  24. void __iomem *addr;
  25. u32 reg;
  26. if (!lock)
  27. return;
  28. if (common->features & CCU_FEATURE_LOCK_REG)
  29. addr = common->base + common->lock_reg;
  30. else
  31. addr = common->base + common->reg;
  32. WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
  33. }
  34. int sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
  35. const struct sunxi_ccu_desc *desc)
  36. {
  37. struct ccu_reset *reset;
  38. int i, ret;
  39. for (i = 0; i < desc->num_ccu_clks; i++) {
  40. struct ccu_common *cclk = desc->ccu_clks[i];
  41. if (!cclk)
  42. continue;
  43. cclk->base = reg;
  44. cclk->lock = &ccu_lock;
  45. }
  46. for (i = 0; i < desc->hw_clks->num ; i++) {
  47. struct clk_hw *hw = desc->hw_clks->hws[i];
  48. if (!hw)
  49. continue;
  50. ret = clk_hw_register(NULL, hw);
  51. if (ret) {
  52. pr_err("Couldn't register clock %s\n",
  53. clk_hw_get_name(hw));
  54. goto err_clk_unreg;
  55. }
  56. }
  57. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
  58. desc->hw_clks);
  59. if (ret)
  60. goto err_clk_unreg;
  61. reset = kzalloc(sizeof(*reset), GFP_KERNEL);
  62. if (!reset) {
  63. ret = -ENOMEM;
  64. goto err_alloc_reset;
  65. }
  66. reset->rcdev.of_node = node;
  67. reset->rcdev.ops = &ccu_reset_ops;
  68. reset->rcdev.owner = THIS_MODULE;
  69. reset->rcdev.nr_resets = desc->num_resets;
  70. reset->base = reg;
  71. reset->lock = &ccu_lock;
  72. reset->reset_map = desc->resets;
  73. ret = reset_controller_register(&reset->rcdev);
  74. if (ret)
  75. goto err_of_clk_unreg;
  76. return 0;
  77. err_of_clk_unreg:
  78. kfree(reset);
  79. err_alloc_reset:
  80. of_clk_del_provider(node);
  81. err_clk_unreg:
  82. while (--i >= 0) {
  83. struct clk_hw *hw = desc->hw_clks->hws[i];
  84. if (!hw)
  85. continue;
  86. clk_hw_unregister(hw);
  87. }
  88. return ret;
  89. }