clk-gate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  27. /*
  28. * It works on following logic:
  29. *
  30. * For enabling clock, enable = 1
  31. * set2dis = 1 -> clear bit -> set = 0
  32. * set2dis = 0 -> set bit -> set = 1
  33. *
  34. * For disabling clock, enable = 0
  35. * set2dis = 1 -> set bit -> set = 1
  36. * set2dis = 0 -> clear bit -> set = 0
  37. *
  38. * So, result is always: enable xor set2dis.
  39. */
  40. static void clk_gate_endisable(struct clk_hw *hw, int enable)
  41. {
  42. struct clk_gate *gate = to_clk_gate(hw);
  43. int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
  44. unsigned long uninitialized_var(flags);
  45. u32 reg;
  46. set ^= enable;
  47. if (gate->lock)
  48. spin_lock_irqsave(gate->lock, flags);
  49. else
  50. __acquire(gate->lock);
  51. if (gate->flags & CLK_GATE_HIWORD_MASK) {
  52. reg = BIT(gate->bit_idx + 16);
  53. if (set)
  54. reg |= BIT(gate->bit_idx);
  55. } else {
  56. reg = clk_readl(gate->reg);
  57. if (set)
  58. reg |= BIT(gate->bit_idx);
  59. else
  60. reg &= ~BIT(gate->bit_idx);
  61. }
  62. clk_writel(reg, gate->reg);
  63. if (gate->lock)
  64. spin_unlock_irqrestore(gate->lock, flags);
  65. else
  66. __release(gate->lock);
  67. }
  68. static int clk_gate_enable(struct clk_hw *hw)
  69. {
  70. clk_gate_endisable(hw, 1);
  71. return 0;
  72. }
  73. static void clk_gate_disable(struct clk_hw *hw)
  74. {
  75. clk_gate_endisable(hw, 0);
  76. }
  77. static int clk_gate_is_enabled(struct clk_hw *hw)
  78. {
  79. u32 reg;
  80. struct clk_gate *gate = to_clk_gate(hw);
  81. reg = clk_readl(gate->reg);
  82. /* if a set bit disables this clk, flip it before masking */
  83. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  84. reg ^= BIT(gate->bit_idx);
  85. reg &= BIT(gate->bit_idx);
  86. return reg ? 1 : 0;
  87. }
  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_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 *clk_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 *clk;
  112. struct clk_init_data init;
  113. if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
  114. if (bit_idx > 15) {
  115. pr_err("gate bit exceeds LOWORD field\n");
  116. return ERR_PTR(-EINVAL);
  117. }
  118. }
  119. /* allocate the gate */
  120. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  121. if (!gate)
  122. return ERR_PTR(-ENOMEM);
  123. init.name = name;
  124. init.ops = &clk_gate_ops;
  125. init.flags = flags | CLK_IS_BASIC;
  126. init.parent_names = (parent_name ? &parent_name: NULL);
  127. init.num_parents = (parent_name ? 1 : 0);
  128. /* struct clk_gate assignments */
  129. gate->reg = reg;
  130. gate->bit_idx = bit_idx;
  131. gate->flags = clk_gate_flags;
  132. gate->lock = lock;
  133. gate->hw.init = &init;
  134. clk = clk_register(dev, &gate->hw);
  135. if (IS_ERR(clk))
  136. kfree(gate);
  137. return clk;
  138. }
  139. EXPORT_SYMBOL_GPL(clk_register_gate);
  140. void clk_unregister_gate(struct clk *clk)
  141. {
  142. struct clk_gate *gate;
  143. struct clk_hw *hw;
  144. hw = __clk_get_hw(clk);
  145. if (!hw)
  146. return;
  147. gate = to_clk_gate(hw);
  148. clk_unregister(clk);
  149. kfree(gate);
  150. }
  151. EXPORT_SYMBOL_GPL(clk_unregister_gate);