clk-gate2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010-2011 Canonical Ltd <[email protected]>
  4. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <[email protected]>
  5. *
  6. * Gated clock implementation
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/export.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/io.h>
  13. #include <linux/err.h>
  14. #include <linux/string.h>
  15. #include "clk.h"
  16. /**
  17. * DOC: basic gateable clock which can gate and ungate its output
  18. *
  19. * Traits of this clock:
  20. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  21. * enable - clk_enable and clk_disable are functional & control gating
  22. * rate - inherits rate from parent. No clk_set_rate support
  23. * parent - fixed parent. No clk_set_parent support
  24. */
  25. struct clk_gate2 {
  26. struct clk_hw hw;
  27. void __iomem *reg;
  28. u8 bit_idx;
  29. u8 cgr_val;
  30. u8 cgr_mask;
  31. u8 flags;
  32. spinlock_t *lock;
  33. unsigned int *share_count;
  34. };
  35. #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
  36. static void clk_gate2_do_shared_clks(struct clk_hw *hw, bool enable)
  37. {
  38. struct clk_gate2 *gate = to_clk_gate2(hw);
  39. u32 reg;
  40. reg = readl(gate->reg);
  41. reg &= ~(gate->cgr_mask << gate->bit_idx);
  42. if (enable)
  43. reg |= (gate->cgr_val & gate->cgr_mask) << gate->bit_idx;
  44. writel(reg, gate->reg);
  45. }
  46. static int clk_gate2_enable(struct clk_hw *hw)
  47. {
  48. struct clk_gate2 *gate = to_clk_gate2(hw);
  49. unsigned long flags;
  50. spin_lock_irqsave(gate->lock, flags);
  51. if (gate->share_count && (*gate->share_count)++ > 0)
  52. goto out;
  53. clk_gate2_do_shared_clks(hw, true);
  54. out:
  55. spin_unlock_irqrestore(gate->lock, flags);
  56. return 0;
  57. }
  58. static void clk_gate2_disable(struct clk_hw *hw)
  59. {
  60. struct clk_gate2 *gate = to_clk_gate2(hw);
  61. unsigned long flags;
  62. spin_lock_irqsave(gate->lock, flags);
  63. if (gate->share_count) {
  64. if (WARN_ON(*gate->share_count == 0))
  65. goto out;
  66. else if (--(*gate->share_count) > 0)
  67. goto out;
  68. }
  69. clk_gate2_do_shared_clks(hw, false);
  70. out:
  71. spin_unlock_irqrestore(gate->lock, flags);
  72. }
  73. static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx,
  74. u8 cgr_val, u8 cgr_mask)
  75. {
  76. u32 val = readl(reg);
  77. if (((val >> bit_idx) & cgr_mask) == cgr_val)
  78. return 1;
  79. return 0;
  80. }
  81. static int clk_gate2_is_enabled(struct clk_hw *hw)
  82. {
  83. struct clk_gate2 *gate = to_clk_gate2(hw);
  84. unsigned long flags;
  85. int ret = 0;
  86. spin_lock_irqsave(gate->lock, flags);
  87. ret = clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx,
  88. gate->cgr_val, gate->cgr_mask);
  89. spin_unlock_irqrestore(gate->lock, flags);
  90. return ret;
  91. }
  92. static void clk_gate2_disable_unused(struct clk_hw *hw)
  93. {
  94. struct clk_gate2 *gate = to_clk_gate2(hw);
  95. unsigned long flags;
  96. spin_lock_irqsave(gate->lock, flags);
  97. if (!gate->share_count || *gate->share_count == 0)
  98. clk_gate2_do_shared_clks(hw, false);
  99. spin_unlock_irqrestore(gate->lock, flags);
  100. }
  101. static const struct clk_ops clk_gate2_ops = {
  102. .enable = clk_gate2_enable,
  103. .disable = clk_gate2_disable,
  104. .disable_unused = clk_gate2_disable_unused,
  105. .is_enabled = clk_gate2_is_enabled,
  106. };
  107. struct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name,
  108. const char *parent_name, unsigned long flags,
  109. void __iomem *reg, u8 bit_idx, u8 cgr_val, u8 cgr_mask,
  110. u8 clk_gate2_flags, spinlock_t *lock,
  111. unsigned int *share_count)
  112. {
  113. struct clk_gate2 *gate;
  114. struct clk_hw *hw;
  115. struct clk_init_data init;
  116. int ret;
  117. gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
  118. if (!gate)
  119. return ERR_PTR(-ENOMEM);
  120. /* struct clk_gate2 assignments */
  121. gate->reg = reg;
  122. gate->bit_idx = bit_idx;
  123. gate->cgr_val = cgr_val;
  124. gate->cgr_mask = cgr_mask;
  125. gate->flags = clk_gate2_flags;
  126. gate->lock = lock;
  127. gate->share_count = share_count;
  128. init.name = name;
  129. init.ops = &clk_gate2_ops;
  130. init.flags = flags;
  131. init.parent_names = parent_name ? &parent_name : NULL;
  132. init.num_parents = parent_name ? 1 : 0;
  133. gate->hw.init = &init;
  134. hw = &gate->hw;
  135. ret = clk_hw_register(dev, hw);
  136. if (ret) {
  137. kfree(gate);
  138. return ERR_PTR(ret);
  139. }
  140. return hw;
  141. }
  142. EXPORT_SYMBOL_GPL(clk_hw_register_gate2);