clk-gate2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 cgr_val;
  32. u8 flags;
  33. spinlock_t *lock;
  34. unsigned int *share_count;
  35. };
  36. #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
  37. static int clk_gate2_enable(struct clk_hw *hw)
  38. {
  39. struct clk_gate2 *gate = to_clk_gate2(hw);
  40. u32 reg;
  41. unsigned long flags = 0;
  42. spin_lock_irqsave(gate->lock, flags);
  43. if (gate->share_count && (*gate->share_count)++ > 0)
  44. goto out;
  45. reg = readl(gate->reg);
  46. reg &= ~(3 << gate->bit_idx);
  47. reg |= gate->cgr_val << gate->bit_idx;
  48. writel(reg, gate->reg);
  49. out:
  50. spin_unlock_irqrestore(gate->lock, flags);
  51. return 0;
  52. }
  53. static void clk_gate2_disable(struct clk_hw *hw)
  54. {
  55. struct clk_gate2 *gate = to_clk_gate2(hw);
  56. u32 reg;
  57. unsigned long flags = 0;
  58. spin_lock_irqsave(gate->lock, flags);
  59. if (gate->share_count) {
  60. if (WARN_ON(*gate->share_count == 0))
  61. goto out;
  62. else if (--(*gate->share_count) > 0)
  63. goto out;
  64. }
  65. reg = readl(gate->reg);
  66. reg &= ~(3 << gate->bit_idx);
  67. writel(reg, gate->reg);
  68. out:
  69. spin_unlock_irqrestore(gate->lock, flags);
  70. }
  71. static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
  72. {
  73. u32 val = readl(reg);
  74. if (((val >> bit_idx) & 1) == 1)
  75. return 1;
  76. return 0;
  77. }
  78. static int clk_gate2_is_enabled(struct clk_hw *hw)
  79. {
  80. struct clk_gate2 *gate = to_clk_gate2(hw);
  81. return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
  82. }
  83. static void clk_gate2_disable_unused(struct clk_hw *hw)
  84. {
  85. struct clk_gate2 *gate = to_clk_gate2(hw);
  86. unsigned long flags = 0;
  87. u32 reg;
  88. spin_lock_irqsave(gate->lock, flags);
  89. if (!gate->share_count || *gate->share_count == 0) {
  90. reg = readl(gate->reg);
  91. reg &= ~(3 << gate->bit_idx);
  92. writel(reg, gate->reg);
  93. }
  94. spin_unlock_irqrestore(gate->lock, flags);
  95. }
  96. static struct clk_ops clk_gate2_ops = {
  97. .enable = clk_gate2_enable,
  98. .disable = clk_gate2_disable,
  99. .disable_unused = clk_gate2_disable_unused,
  100. .is_enabled = clk_gate2_is_enabled,
  101. };
  102. struct clk *clk_register_gate2(struct device *dev, const char *name,
  103. const char *parent_name, unsigned long flags,
  104. void __iomem *reg, u8 bit_idx, u8 cgr_val,
  105. u8 clk_gate2_flags, spinlock_t *lock,
  106. unsigned int *share_count)
  107. {
  108. struct clk_gate2 *gate;
  109. struct clk *clk;
  110. struct clk_init_data init;
  111. gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
  112. if (!gate)
  113. return ERR_PTR(-ENOMEM);
  114. /* struct clk_gate2 assignments */
  115. gate->reg = reg;
  116. gate->bit_idx = bit_idx;
  117. gate->cgr_val = cgr_val;
  118. gate->flags = clk_gate2_flags;
  119. gate->lock = lock;
  120. gate->share_count = share_count;
  121. init.name = name;
  122. init.ops = &clk_gate2_ops;
  123. init.flags = flags;
  124. init.parent_names = parent_name ? &parent_name : NULL;
  125. init.num_parents = parent_name ? 1 : 0;
  126. gate->hw.init = &init;
  127. clk = clk_register(dev, &gate->hw);
  128. if (IS_ERR(clk))
  129. kfree(gate);
  130. return clk;
  131. }