ccu_common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. u32 reg;
  25. if (!lock)
  26. return;
  27. WARN_ON(readl_relaxed_poll_timeout(common->base + common->reg, reg,
  28. reg & lock, 100, 70000));
  29. }
  30. int sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
  31. const struct sunxi_ccu_desc *desc)
  32. {
  33. struct ccu_reset *reset;
  34. int i, ret;
  35. for (i = 0; i < desc->num_ccu_clks; i++) {
  36. struct ccu_common *cclk = desc->ccu_clks[i];
  37. if (!cclk)
  38. continue;
  39. cclk->base = reg;
  40. cclk->lock = &ccu_lock;
  41. }
  42. for (i = 0; i < desc->hw_clks->num ; i++) {
  43. struct clk_hw *hw = desc->hw_clks->hws[i];
  44. if (!hw)
  45. continue;
  46. ret = clk_hw_register(NULL, hw);
  47. if (ret) {
  48. pr_err("Couldn't register clock %s\n",
  49. clk_hw_get_name(hw));
  50. goto err_clk_unreg;
  51. }
  52. }
  53. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
  54. desc->hw_clks);
  55. if (ret)
  56. goto err_clk_unreg;
  57. reset = kzalloc(sizeof(*reset), GFP_KERNEL);
  58. if (!reset) {
  59. ret = -ENOMEM;
  60. goto err_alloc_reset;
  61. }
  62. reset->rcdev.of_node = node;
  63. reset->rcdev.ops = &ccu_reset_ops;
  64. reset->rcdev.owner = THIS_MODULE;
  65. reset->rcdev.nr_resets = desc->num_resets;
  66. reset->base = reg;
  67. reset->lock = &ccu_lock;
  68. reset->reset_map = desc->resets;
  69. ret = reset_controller_register(&reset->rcdev);
  70. if (ret)
  71. goto err_of_clk_unreg;
  72. return 0;
  73. err_of_clk_unreg:
  74. kfree(reset);
  75. err_alloc_reset:
  76. of_clk_del_provider(node);
  77. err_clk_unreg:
  78. while (--i >= 0) {
  79. struct clk_hw *hw = desc->hw_clks->hws[i];
  80. if (!hw)
  81. continue;
  82. clk_hw_unregister(hw);
  83. }
  84. return ret;
  85. }