clk-gate.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 flags = 0;
  45. u32 reg;
  46. set ^= enable;
  47. if (gate->lock)
  48. spin_lock_irqsave(gate->lock, flags);
  49. reg = readl(gate->reg);
  50. if (set)
  51. reg |= BIT(gate->bit_idx);
  52. else
  53. reg &= ~BIT(gate->bit_idx);
  54. writel(reg, gate->reg);
  55. if (gate->lock)
  56. spin_unlock_irqrestore(gate->lock, flags);
  57. }
  58. static int clk_gate_enable(struct clk_hw *hw)
  59. {
  60. clk_gate_endisable(hw, 1);
  61. return 0;
  62. }
  63. static void clk_gate_disable(struct clk_hw *hw)
  64. {
  65. clk_gate_endisable(hw, 0);
  66. }
  67. static int clk_gate_is_enabled(struct clk_hw *hw)
  68. {
  69. u32 reg;
  70. struct clk_gate *gate = to_clk_gate(hw);
  71. reg = readl(gate->reg);
  72. /* if a set bit disables this clk, flip it before masking */
  73. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  74. reg ^= BIT(gate->bit_idx);
  75. reg &= BIT(gate->bit_idx);
  76. return reg ? 1 : 0;
  77. }
  78. const struct clk_ops clk_gate_ops = {
  79. .enable = clk_gate_enable,
  80. .disable = clk_gate_disable,
  81. .is_enabled = clk_gate_is_enabled,
  82. };
  83. EXPORT_SYMBOL_GPL(clk_gate_ops);
  84. /**
  85. * clk_register_gate - register a gate clock with the clock framework
  86. * @dev: device that is registering this clock
  87. * @name: name of this clock
  88. * @parent_name: name of this clock's parent
  89. * @flags: framework-specific flags for this clock
  90. * @reg: register address to control gating of this clock
  91. * @bit_idx: which bit in the register controls gating of this clock
  92. * @clk_gate_flags: gate-specific flags for this clock
  93. * @lock: shared register lock for this clock
  94. */
  95. struct clk *clk_register_gate(struct device *dev, const char *name,
  96. const char *parent_name, unsigned long flags,
  97. void __iomem *reg, u8 bit_idx,
  98. u8 clk_gate_flags, spinlock_t *lock)
  99. {
  100. struct clk_gate *gate;
  101. struct clk *clk;
  102. struct clk_init_data init;
  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. init.name = name;
  110. init.ops = &clk_gate_ops;
  111. init.flags = flags | CLK_IS_BASIC;
  112. init.parent_names = (parent_name ? &parent_name: NULL);
  113. init.num_parents = (parent_name ? 1 : 0);
  114. /* struct clk_gate assignments */
  115. gate->reg = reg;
  116. gate->bit_idx = bit_idx;
  117. gate->flags = clk_gate_flags;
  118. gate->lock = lock;
  119. gate->hw.init = &init;
  120. clk = clk_register(dev, &gate->hw);
  121. if (IS_ERR(clk))
  122. kfree(gate);
  123. return clk;
  124. }