clk-gate.c 3.3 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. EXPORT_SYMBOL_GPL(clk_gate_enable);
  61. static void clk_gate_disable(struct clk_hw *hw)
  62. {
  63. struct clk_gate *gate = to_clk_gate(hw);
  64. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  65. clk_gate_set_bit(gate);
  66. else
  67. clk_gate_clear_bit(gate);
  68. }
  69. EXPORT_SYMBOL_GPL(clk_gate_disable);
  70. static int clk_gate_is_enabled(struct clk_hw *hw)
  71. {
  72. u32 reg;
  73. struct clk_gate *gate = to_clk_gate(hw);
  74. reg = readl(gate->reg);
  75. /* if a set bit disables this clk, flip it before masking */
  76. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  77. reg ^= BIT(gate->bit_idx);
  78. reg &= BIT(gate->bit_idx);
  79. return reg ? 1 : 0;
  80. }
  81. EXPORT_SYMBOL_GPL(clk_gate_is_enabled);
  82. struct clk_ops clk_gate_ops = {
  83. .enable = clk_gate_enable,
  84. .disable = clk_gate_disable,
  85. .is_enabled = clk_gate_is_enabled,
  86. };
  87. EXPORT_SYMBOL_GPL(clk_gate_ops);
  88. struct clk *clk_register_gate(struct device *dev, const char *name,
  89. const char *parent_name, unsigned long flags,
  90. void __iomem *reg, u8 bit_idx,
  91. u8 clk_gate_flags, spinlock_t *lock)
  92. {
  93. struct clk_gate *gate;
  94. struct clk *clk;
  95. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  96. if (!gate) {
  97. pr_err("%s: could not allocate gated clk\n", __func__);
  98. return NULL;
  99. }
  100. /* struct clk_gate assignments */
  101. gate->reg = reg;
  102. gate->bit_idx = bit_idx;
  103. gate->flags = clk_gate_flags;
  104. gate->lock = lock;
  105. if (parent_name) {
  106. gate->parent[0] = kstrdup(parent_name, GFP_KERNEL);
  107. if (!gate->parent[0])
  108. goto out;
  109. }
  110. clk = clk_register(dev, name,
  111. &clk_gate_ops, &gate->hw,
  112. gate->parent,
  113. (parent_name ? 1 : 0),
  114. flags);
  115. if (clk)
  116. return clk;
  117. out:
  118. kfree(gate->parent[0]);
  119. kfree(gate);
  120. return NULL;
  121. }