clk-gate.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  3. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Gated clock implementation
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/err.h>
  16. #include <linux/string.h>
  17. /**
  18. * DOC: basic gatable clock which can gate and ungate it's ouput
  19. *
  20. * Traits of this clock:
  21. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  22. * enable - clk_enable and clk_disable are functional & control gating
  23. * rate - inherits rate from parent. No clk_set_rate support
  24. * parent - fixed parent. No clk_set_parent support
  25. */
  26. /*
  27. * It works on following logic:
  28. *
  29. * For enabling clock, enable = 1
  30. * set2dis = 1 -> clear bit -> set = 0
  31. * set2dis = 0 -> set bit -> set = 1
  32. *
  33. * For disabling clock, enable = 0
  34. * set2dis = 1 -> set bit -> set = 1
  35. * set2dis = 0 -> clear bit -> set = 0
  36. *
  37. * So, result is always: enable xor set2dis.
  38. */
  39. static void clk_gate_endisable(struct clk_hw *hw, int enable)
  40. {
  41. struct clk_gate *gate = to_clk_gate(hw);
  42. int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
  43. unsigned long uninitialized_var(flags);
  44. u32 reg;
  45. set ^= enable;
  46. if (gate->lock)
  47. spin_lock_irqsave(gate->lock, flags);
  48. else
  49. __acquire(gate->lock);
  50. if (gate->flags & CLK_GATE_HIWORD_MASK) {
  51. reg = BIT(gate->bit_idx + 16);
  52. if (set)
  53. reg |= BIT(gate->bit_idx);
  54. } else {
  55. reg = clk_readl(gate->reg);
  56. if (set)
  57. reg |= BIT(gate->bit_idx);
  58. else
  59. reg &= ~BIT(gate->bit_idx);
  60. }
  61. clk_writel(reg, gate->reg);
  62. if (gate->lock)
  63. spin_unlock_irqrestore(gate->lock, flags);
  64. else
  65. __release(gate->lock);
  66. }
  67. static int clk_gate_enable(struct clk_hw *hw)
  68. {
  69. clk_gate_endisable(hw, 1);
  70. return 0;
  71. }
  72. static void clk_gate_disable(struct clk_hw *hw)
  73. {
  74. clk_gate_endisable(hw, 0);
  75. }
  76. int clk_gate_is_enabled(struct clk_hw *hw)
  77. {
  78. u32 reg;
  79. struct clk_gate *gate = to_clk_gate(hw);
  80. reg = clk_readl(gate->reg);
  81. /* if a set bit disables this clk, flip it before masking */
  82. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  83. reg ^= BIT(gate->bit_idx);
  84. reg &= BIT(gate->bit_idx);
  85. return reg ? 1 : 0;
  86. }
  87. EXPORT_SYMBOL_GPL(clk_gate_is_enabled);
  88. const struct clk_ops clk_gate_ops = {
  89. .enable = clk_gate_enable,
  90. .disable = clk_gate_disable,
  91. .is_enabled = clk_gate_is_enabled,
  92. };
  93. EXPORT_SYMBOL_GPL(clk_gate_ops);
  94. /**
  95. * clk_hw_register_gate - register a gate clock with the clock framework
  96. * @dev: device that is registering this clock
  97. * @name: name of this clock
  98. * @parent_name: name of this clock's parent
  99. * @flags: framework-specific flags for this clock
  100. * @reg: register address to control gating of this clock
  101. * @bit_idx: which bit in the register controls gating of this clock
  102. * @clk_gate_flags: gate-specific flags for this clock
  103. * @lock: shared register lock for this clock
  104. */
  105. struct clk_hw *clk_hw_register_gate(struct device *dev, const char *name,
  106. const char *parent_name, unsigned long flags,
  107. void __iomem *reg, u8 bit_idx,
  108. u8 clk_gate_flags, spinlock_t *lock)
  109. {
  110. struct clk_gate *gate;
  111. struct clk_hw *hw;
  112. struct clk_init_data init;
  113. int ret;
  114. if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
  115. if (bit_idx > 15) {
  116. pr_err("gate bit exceeds LOWORD field\n");
  117. return ERR_PTR(-EINVAL);
  118. }
  119. }
  120. /* allocate the gate */
  121. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  122. if (!gate)
  123. return ERR_PTR(-ENOMEM);
  124. init.name = name;
  125. init.ops = &clk_gate_ops;
  126. init.flags = flags | CLK_IS_BASIC;
  127. init.parent_names = parent_name ? &parent_name : NULL;
  128. init.num_parents = parent_name ? 1 : 0;
  129. /* struct clk_gate assignments */
  130. gate->reg = reg;
  131. gate->bit_idx = bit_idx;
  132. gate->flags = clk_gate_flags;
  133. gate->lock = lock;
  134. gate->hw.init = &init;
  135. hw = &gate->hw;
  136. ret = clk_hw_register(dev, hw);
  137. if (ret) {
  138. kfree(gate);
  139. hw = ERR_PTR(ret);
  140. }
  141. return hw;
  142. }
  143. EXPORT_SYMBOL_GPL(clk_hw_register_gate);
  144. struct clk *clk_register_gate(struct device *dev, const char *name,
  145. const char *parent_name, unsigned long flags,
  146. void __iomem *reg, u8 bit_idx,
  147. u8 clk_gate_flags, spinlock_t *lock)
  148. {
  149. struct clk_hw *hw;
  150. hw = clk_hw_register_gate(dev, name, parent_name, flags, reg,
  151. bit_idx, clk_gate_flags, lock);
  152. if (IS_ERR(hw))
  153. return ERR_CAST(hw);
  154. return hw->clk;
  155. }
  156. EXPORT_SYMBOL_GPL(clk_register_gate);
  157. void clk_unregister_gate(struct clk *clk)
  158. {
  159. struct clk_gate *gate;
  160. struct clk_hw *hw;
  161. hw = __clk_get_hw(clk);
  162. if (!hw)
  163. return;
  164. gate = to_clk_gate(hw);
  165. clk_unregister(clk);
  166. kfree(gate);
  167. }
  168. EXPORT_SYMBOL_GPL(clk_unregister_gate);
  169. void clk_hw_unregister_gate(struct clk_hw *hw)
  170. {
  171. struct clk_gate *gate;
  172. gate = to_clk_gate(hw);
  173. clk_hw_unregister(hw);
  174. kfree(gate);
  175. }
  176. EXPORT_SYMBOL_GPL(clk_hw_unregister_gate);