clk-gate2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "clk.h"
  18. /**
  19. * DOC: basic gatable clock which can gate and ungate it's ouput
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  23. * enable - clk_enable and clk_disable are functional & control gating
  24. * rate - inherits rate from parent. No clk_set_rate support
  25. * parent - fixed parent. No clk_set_parent support
  26. */
  27. struct clk_gate2 {
  28. struct clk_hw hw;
  29. void __iomem *reg;
  30. u8 bit_idx;
  31. u8 flags;
  32. spinlock_t *lock;
  33. unsigned int *share_count;
  34. };
  35. #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
  36. static int clk_gate2_enable(struct clk_hw *hw)
  37. {
  38. struct clk_gate2 *gate = to_clk_gate2(hw);
  39. u32 reg;
  40. unsigned long flags = 0;
  41. spin_lock_irqsave(gate->lock, flags);
  42. if (gate->share_count && (*gate->share_count)++ > 0)
  43. goto out;
  44. reg = readl(gate->reg);
  45. reg |= 3 << gate->bit_idx;
  46. writel(reg, gate->reg);
  47. out:
  48. spin_unlock_irqrestore(gate->lock, flags);
  49. return 0;
  50. }
  51. static void clk_gate2_disable(struct clk_hw *hw)
  52. {
  53. struct clk_gate2 *gate = to_clk_gate2(hw);
  54. u32 reg;
  55. unsigned long flags = 0;
  56. spin_lock_irqsave(gate->lock, flags);
  57. if (gate->share_count) {
  58. if (WARN_ON(*gate->share_count == 0))
  59. goto out;
  60. else if (--(*gate->share_count) > 0)
  61. goto out;
  62. }
  63. reg = readl(gate->reg);
  64. reg &= ~(3 << gate->bit_idx);
  65. writel(reg, gate->reg);
  66. out:
  67. spin_unlock_irqrestore(gate->lock, flags);
  68. }
  69. static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
  70. {
  71. u32 val = readl(reg);
  72. if (((val >> bit_idx) & 1) == 1)
  73. return 1;
  74. return 0;
  75. }
  76. static int clk_gate2_is_enabled(struct clk_hw *hw)
  77. {
  78. struct clk_gate2 *gate = to_clk_gate2(hw);
  79. return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
  80. }
  81. static void clk_gate2_disable_unused(struct clk_hw *hw)
  82. {
  83. struct clk_gate2 *gate = to_clk_gate2(hw);
  84. unsigned long flags = 0;
  85. u32 reg;
  86. spin_lock_irqsave(gate->lock, flags);
  87. if (!gate->share_count || *gate->share_count == 0) {
  88. reg = readl(gate->reg);
  89. reg &= ~(3 << gate->bit_idx);
  90. writel(reg, gate->reg);
  91. }
  92. spin_unlock_irqrestore(gate->lock, flags);
  93. }
  94. static struct clk_ops clk_gate2_ops = {
  95. .enable = clk_gate2_enable,
  96. .disable = clk_gate2_disable,
  97. .disable_unused = clk_gate2_disable_unused,
  98. .is_enabled = clk_gate2_is_enabled,
  99. };
  100. struct clk *clk_register_gate2(struct device *dev, const char *name,
  101. const char *parent_name, unsigned long flags,
  102. void __iomem *reg, u8 bit_idx,
  103. u8 clk_gate2_flags, spinlock_t *lock,
  104. unsigned int *share_count)
  105. {
  106. struct clk_gate2 *gate;
  107. struct clk *clk;
  108. struct clk_init_data init;
  109. gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
  110. if (!gate)
  111. return ERR_PTR(-ENOMEM);
  112. /* struct clk_gate2 assignments */
  113. gate->reg = reg;
  114. gate->bit_idx = bit_idx;
  115. gate->flags = clk_gate2_flags;
  116. gate->lock = lock;
  117. gate->share_count = share_count;
  118. init.name = name;
  119. init.ops = &clk_gate2_ops;
  120. init.flags = flags;
  121. init.parent_names = parent_name ? &parent_name : NULL;
  122. init.num_parents = parent_name ? 1 : 0;
  123. gate->hw.init = &init;
  124. clk = clk_register(dev, &gate->hw);
  125. if (IS_ERR(clk))
  126. kfree(gate);
  127. return clk;
  128. }