ccu_gate.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2016 Maxime Ripard
  3. * Maxime Ripard <maxime.ripard@free-electrons.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include "ccu_gate.h"
  12. void ccu_gate_helper_disable(struct ccu_common *common, u32 gate)
  13. {
  14. unsigned long flags;
  15. u32 reg;
  16. if (!gate)
  17. return;
  18. spin_lock_irqsave(common->lock, flags);
  19. reg = readl(common->base + common->reg);
  20. writel(reg & ~gate, common->base + common->reg);
  21. spin_unlock_irqrestore(common->lock, flags);
  22. }
  23. static void ccu_gate_disable(struct clk_hw *hw)
  24. {
  25. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  26. return ccu_gate_helper_disable(&cg->common, cg->enable);
  27. }
  28. int ccu_gate_helper_enable(struct ccu_common *common, u32 gate)
  29. {
  30. unsigned long flags;
  31. u32 reg;
  32. if (!gate)
  33. return 0;
  34. spin_lock_irqsave(common->lock, flags);
  35. reg = readl(common->base + common->reg);
  36. writel(reg | gate, common->base + common->reg);
  37. spin_unlock_irqrestore(common->lock, flags);
  38. return 0;
  39. }
  40. static int ccu_gate_enable(struct clk_hw *hw)
  41. {
  42. struct ccu_gate *cg = hw_to_ccu_gate(hw);
  43. return ccu_gate_helper_enable(&cg->common, cg->enable);
  44. }
  45. int ccu_gate_helper_is_enabled(struct ccu_common *common, u32 gate)
  46. {
  47. if (!gate)
  48. return 1;
  49. return readl(common->base + common->reg) & gate;
  50. }
  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. };