clk-gate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (gate->flags & CLK_GATE_HIWORD_MASK) {
  50. reg = BIT(gate->bit_idx + 16);
  51. if (set)
  52. reg |= BIT(gate->bit_idx);
  53. } else {
  54. reg = clk_readl(gate->reg);
  55. if (set)
  56. reg |= BIT(gate->bit_idx);
  57. else
  58. reg &= ~BIT(gate->bit_idx);
  59. }
  60. clk_writel(reg, gate->reg);
  61. if (gate->lock)
  62. spin_unlock_irqrestore(gate->lock, flags);
  63. }
  64. static int clk_gate_enable(struct clk_hw *hw)
  65. {
  66. clk_gate_endisable(hw, 1);
  67. return 0;
  68. }
  69. static void clk_gate_disable(struct clk_hw *hw)
  70. {
  71. clk_gate_endisable(hw, 0);
  72. }
  73. static int clk_gate_is_enabled(struct clk_hw *hw)
  74. {
  75. u32 reg;
  76. struct clk_gate *gate = to_clk_gate(hw);
  77. reg = clk_readl(gate->reg);
  78. /* if a set bit disables this clk, flip it before masking */
  79. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  80. reg ^= BIT(gate->bit_idx);
  81. reg &= BIT(gate->bit_idx);
  82. return reg ? 1 : 0;
  83. }
  84. const struct clk_ops clk_gate_ops = {
  85. .enable = clk_gate_enable,
  86. .disable = clk_gate_disable,
  87. .is_enabled = clk_gate_is_enabled,
  88. };
  89. EXPORT_SYMBOL_GPL(clk_gate_ops);
  90. /**
  91. * clk_register_gate - register a gate clock with the clock framework
  92. * @dev: device that is registering this clock
  93. * @name: name of this clock
  94. * @parent_name: name of this clock's parent
  95. * @flags: framework-specific flags for this clock
  96. * @reg: register address to control gating of this clock
  97. * @bit_idx: which bit in the register controls gating of this clock
  98. * @clk_gate_flags: gate-specific flags for this clock
  99. * @lock: shared register lock for this clock
  100. */
  101. struct clk *clk_register_gate(struct device *dev, const char *name,
  102. const char *parent_name, unsigned long flags,
  103. void __iomem *reg, u8 bit_idx,
  104. u8 clk_gate_flags, spinlock_t *lock)
  105. {
  106. struct clk_gate *gate;
  107. struct clk *clk;
  108. struct clk_init_data init;
  109. if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
  110. if (bit_idx > 15) {
  111. pr_err("gate bit exceeds LOWORD field\n");
  112. return ERR_PTR(-EINVAL);
  113. }
  114. }
  115. /* allocate the gate */
  116. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  117. if (!gate)
  118. return ERR_PTR(-ENOMEM);
  119. init.name = name;
  120. init.ops = &clk_gate_ops;
  121. init.flags = flags | CLK_IS_BASIC;
  122. init.parent_names = (parent_name ? &parent_name: NULL);
  123. init.num_parents = (parent_name ? 1 : 0);
  124. /* struct clk_gate assignments */
  125. gate->reg = reg;
  126. gate->bit_idx = bit_idx;
  127. gate->flags = clk_gate_flags;
  128. gate->lock = lock;
  129. gate->hw.init = &init;
  130. clk = clk_register(dev, &gate->hw);
  131. if (IS_ERR(clk))
  132. kfree(gate);
  133. return clk;
  134. }
  135. EXPORT_SYMBOL_GPL(clk_register_gate);
  136. void clk_unregister_gate(struct clk *clk)
  137. {
  138. struct clk_gate *gate;
  139. struct clk_hw *hw;
  140. hw = __clk_get_hw(clk);
  141. if (!hw)
  142. return;
  143. gate = to_clk_gate(hw);
  144. clk_unregister(clk);
  145. kfree(gate);
  146. }
  147. EXPORT_SYMBOL_GPL(clk_unregister_gate);