ccu_gate.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Maxime Ripard
  4. * Maxime Ripard <[email protected]>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include "ccu_gate.h"
  9. void ccu_gate_helper_disable(struct ccu_common *common, u32 gate)
  10. {
  11. unsigned long flags;
  12. u32 reg;
  13. if (!gate)
  14. return;
  15. spin_lock_irqsave(common->lock, flags);
  16. reg = readl(common->base + common->reg);
  17. writel(reg & ~gate, common->base + common->reg);
  18. spin_unlock_irqrestore(common->lock, flags);
  19. }
  20. EXPORT_SYMBOL_NS_GPL(ccu_gate_helper_disable, SUNXI_CCU);
  21. static void ccu_gate_disable(struct clk_hw *hw)
  22. {
  23. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  24. return ccu_gate_helper_disable(&cg->common, cg->enable);
  25. }
  26. int ccu_gate_helper_enable(struct ccu_common *common, u32 gate)
  27. {
  28. unsigned long flags;
  29. u32 reg;
  30. if (!gate)
  31. return 0;
  32. spin_lock_irqsave(common->lock, flags);
  33. reg = readl(common->base + common->reg);
  34. writel(reg | gate, common->base + common->reg);
  35. spin_unlock_irqrestore(common->lock, flags);
  36. return 0;
  37. }
  38. EXPORT_SYMBOL_NS_GPL(ccu_gate_helper_enable, SUNXI_CCU);
  39. static int ccu_gate_enable(struct clk_hw *hw)
  40. {
  41. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  42. return ccu_gate_helper_enable(&cg->common, cg->enable);
  43. }
  44. int ccu_gate_helper_is_enabled(struct ccu_common *common, u32 gate)
  45. {
  46. if (!gate)
  47. return 1;
  48. return readl(common->base + common->reg) & gate;
  49. }
  50. EXPORT_SYMBOL_NS_GPL(ccu_gate_helper_is_enabled, SUNXI_CCU);
  51. static int ccu_gate_is_enabled(struct clk_hw *hw)
  52. {
  53. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  54. return ccu_gate_helper_is_enabled(&cg->common, cg->enable);
  55. }
  56. static unsigned long ccu_gate_recalc_rate(struct clk_hw *hw,
  57. unsigned long parent_rate)
  58. {
  59. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  60. unsigned long rate = parent_rate;
  61. if (cg->common.features & CCU_FEATURE_ALL_PREDIV)
  62. rate /= cg->common.prediv;
  63. return rate;
  64. }
  65. static long ccu_gate_round_rate(struct clk_hw *hw, unsigned long rate,
  66. unsigned long *prate)
  67. {
  68. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  69. int div = 1;
  70. if (cg->common.features & CCU_FEATURE_ALL_PREDIV)
  71. div = cg->common.prediv;
  72. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  73. unsigned long best_parent = rate;
  74. if (cg->common.features & CCU_FEATURE_ALL_PREDIV)
  75. best_parent *= div;
  76. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  77. }
  78. return *prate / div;
  79. }
  80. static int ccu_gate_set_rate(struct clk_hw *hw, unsigned long rate,
  81. unsigned long parent_rate)
  82. {
  83. /*
  84. * We must report success but we can do so unconditionally because
  85. * clk_factor_round_rate returns values that ensure this call is a
  86. * nop.
  87. */
  88. return 0;
  89. }
  90. const struct clk_ops ccu_gate_ops = {
  91. .disable = ccu_gate_disable,
  92. .enable = ccu_gate_enable,
  93. .is_enabled = ccu_gate_is_enabled,
  94. .round_rate = ccu_gate_round_rate,
  95. .set_rate = ccu_gate_set_rate,
  96. .recalc_rate = ccu_gate_recalc_rate,
  97. };
  98. EXPORT_SYMBOL_NS_GPL(ccu_gate_ops, SUNXI_CCU);