clk-gate.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static void clk_gate_set_bit(struct clk_gate *gate)
  28. {
  29. u32 reg;
  30. unsigned long flags = 0;
  31. if (gate->lock)
  32. spin_lock_irqsave(gate->lock, flags);
  33. reg = readl(gate->reg);
  34. reg |= BIT(gate->bit_idx);
  35. writel(reg, gate->reg);
  36. if (gate->lock)
  37. spin_unlock_irqrestore(gate->lock, flags);
  38. }
  39. static void clk_gate_clear_bit(struct clk_gate *gate)
  40. {
  41. u32 reg;
  42. unsigned long flags = 0;
  43. if (gate->lock)
  44. spin_lock_irqsave(gate->lock, flags);
  45. reg = readl(gate->reg);
  46. reg &= ~BIT(gate->bit_idx);
  47. writel(reg, gate->reg);
  48. if (gate->lock)
  49. spin_unlock_irqrestore(gate->lock, flags);
  50. }
  51. static int clk_gate_enable(struct clk_hw *hw)
  52. {
  53. struct clk_gate *gate = to_clk_gate(hw);
  54. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  55. clk_gate_clear_bit(gate);
  56. else
  57. clk_gate_set_bit(gate);
  58. return 0;
  59. }
  60. static void clk_gate_disable(struct clk_hw *hw)
  61. {
  62. struct clk_gate *gate = to_clk_gate(hw);
  63. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  64. clk_gate_set_bit(gate);
  65. else
  66. clk_gate_clear_bit(gate);
  67. }
  68. static int clk_gate_is_enabled(struct clk_hw *hw)
  69. {
  70. u32 reg;
  71. struct clk_gate *gate = to_clk_gate(hw);
  72. reg = readl(gate->reg);
  73. /* if a set bit disables this clk, flip it before masking */
  74. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  75. reg ^= BIT(gate->bit_idx);
  76. reg &= BIT(gate->bit_idx);
  77. return reg ? 1 : 0;
  78. }
  79. const struct clk_ops clk_gate_ops = {
  80. .enable = clk_gate_enable,
  81. .disable = clk_gate_disable,
  82. .is_enabled = clk_gate_is_enabled,
  83. };
  84. EXPORT_SYMBOL_GPL(clk_gate_ops);
  85. /**
  86. * clk_register_gate - register a gate clock with the clock framework
  87. * @dev: device that is registering this clock
  88. * @name: name of this clock
  89. * @parent_name: name of this clock's parent
  90. * @flags: framework-specific flags for this clock
  91. * @reg: register address to control gating of this clock
  92. * @bit_idx: which bit in the register controls gating of this clock
  93. * @clk_gate_flags: gate-specific flags for this clock
  94. * @lock: shared register lock for this clock
  95. */
  96. struct clk *clk_register_gate(struct device *dev, const char *name,
  97. const char *parent_name, unsigned long flags,
  98. void __iomem *reg, u8 bit_idx,
  99. u8 clk_gate_flags, spinlock_t *lock)
  100. {
  101. struct clk_gate *gate;
  102. struct clk *clk;
  103. /* allocate the gate */
  104. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  105. if (!gate) {
  106. pr_err("%s: could not allocate gated clk\n", __func__);
  107. return ERR_PTR(-ENOMEM);
  108. }
  109. /* struct clk_gate assignments */
  110. gate->reg = reg;
  111. gate->bit_idx = bit_idx;
  112. gate->flags = clk_gate_flags;
  113. gate->lock = lock;
  114. /* register the clock */
  115. clk = clk_register(dev, name,
  116. &clk_gate_ops, &gate->hw,
  117. (parent_name ? &parent_name : NULL),
  118. (parent_name ? 1 : 0),
  119. flags);
  120. if (IS_ERR(clk))
  121. kfree(gate);
  122. return clk;
  123. }