clk-gate-v1.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: James Liao <jamesjj.liao@mediatek.com>
  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. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/ratelimit.h>
  21. #include "clk-mtk-v1.h"
  22. #include "clk-gate-v1.h"
  23. #if !defined(MT_CCF_DEBUG) || !defined(MT_CCF_BRINGUP)
  24. #if defined(CONFIG_ARCH_MT6755)
  25. #define MT_CCF_DEBUG 0
  26. #define MT_CCF_BRINGUP 0
  27. #elif defined(CONFIG_ARCH_MT6797)
  28. #define MT_CCF_DEBUG 1
  29. #define MT_CCF_BRINGUP 0
  30. #else
  31. #define MT_CCF_DEBUG 0
  32. #define MT_CCF_BRINGUP 0
  33. #endif
  34. #endif
  35. /*
  36. * clk_gate
  37. */
  38. static void cg_set_mask(struct mtk_clk_gate *cg, u32 mask)
  39. {
  40. u32 r;
  41. #if MT_CCF_DEBUG
  42. pr_debug_ratelimited("[CCF] %s: %s, mask=%u, bit=%u, flags=%u\n",
  43. __func__, __clk_get_name(cg->hw.clk), mask,
  44. cg->bit, cg->flags);
  45. #endif /* MT_CCF_DEBUG */
  46. if (cg->flags & CLK_GATE_NO_SETCLR_REG) {
  47. r = readl_relaxed(cg->sta_addr) | mask;
  48. writel_relaxed(r, cg->sta_addr);
  49. } else
  50. writel_relaxed(mask, cg->set_addr);
  51. }
  52. static void cg_clr_mask(struct mtk_clk_gate *cg, u32 mask)
  53. {
  54. u32 r;
  55. #if MT_CCF_DEBUG
  56. pr_debug_ratelimited("[CCF] %s: %s, mask=%u, bit=%u, flags=%u\n",
  57. __func__, __clk_get_name(cg->hw.clk), mask,
  58. cg->bit, cg->flags);
  59. #endif /* MT_CCF_DEBUG */
  60. if (cg->flags & CLK_GATE_NO_SETCLR_REG) {
  61. r = readl_relaxed(cg->sta_addr) & ~mask;
  62. writel_relaxed(r, cg->sta_addr);
  63. } else
  64. writel_relaxed(mask, cg->clr_addr);
  65. }
  66. static int cg_enable(struct clk_hw *hw)
  67. {
  68. unsigned long flags = 0;
  69. struct mtk_clk_gate *cg = to_clk_gate(hw);
  70. u32 mask = BIT(cg->bit);
  71. #if MT_CCF_BRINGUP
  72. pr_debug_ratelimited("[CCF] %s: %s, bit: %u\n", __func__,
  73. __clk_get_name(hw->clk), cg->bit);
  74. return 0;
  75. #endif /* MT_CCF_BRINGUP */
  76. mtk_clk_lock(flags);
  77. if (cg->flags & CLK_GATE_INVERSE)
  78. cg_set_mask(cg, mask);
  79. else
  80. cg_clr_mask(cg, mask);
  81. mtk_clk_unlock(flags);
  82. return 0;
  83. }
  84. static void cg_disable(struct clk_hw *hw)
  85. {
  86. unsigned long flags = 0;
  87. struct mtk_clk_gate *cg = to_clk_gate(hw);
  88. u32 mask = BIT(cg->bit);
  89. #if MT_CCF_BRINGUP
  90. pr_debug_ratelimited("[CCF] %s: %s, bit: %u\n", __func__,
  91. __clk_get_name(hw->clk), cg->bit);
  92. return;
  93. #endif /* MT_CCF_BRINGUP */
  94. mtk_clk_lock(flags);
  95. if (cg->flags & CLK_GATE_INVERSE)
  96. cg_clr_mask(cg, mask);
  97. else
  98. cg_set_mask(cg, mask);
  99. mtk_clk_unlock(flags);
  100. }
  101. static int cg_is_enabled(struct clk_hw *hw)
  102. {
  103. struct mtk_clk_gate *cg = to_clk_gate(hw);
  104. u32 mask;
  105. u32 val;
  106. int r;
  107. #if MT_CCF_BRINGUP
  108. pr_debug_ratelimited("[CCF] %s: %s\n", __func__, __clk_get_name(hw->clk));
  109. return 1;
  110. #endif /* MT_CCF_BRINGUP */
  111. mask = BIT(cg->bit);
  112. val = mask & readl(cg->sta_addr);
  113. r = (cg->flags & CLK_GATE_INVERSE) ? (val != 0) : (val == 0);
  114. pr_debug("[CCF] %s: %d, %s, bit[%d]\n", __func__, r,
  115. __clk_get_name(hw->clk), (int)cg->bit);
  116. return r;
  117. }
  118. static const struct clk_ops mtk_clk_gate_ops = {
  119. .is_enabled = cg_is_enabled,
  120. .enable = cg_enable,
  121. .disable = cg_disable,
  122. };
  123. struct clk *mtk_clk_register_gate(
  124. const char *name,
  125. const char *parent_name,
  126. void __iomem *set_addr,
  127. void __iomem *clr_addr,
  128. void __iomem *sta_addr,
  129. u8 bit,
  130. u32 flags)
  131. {
  132. struct mtk_clk_gate *cg;
  133. struct clk *clk;
  134. struct clk_init_data init;
  135. #if MT_CCF_DEBUG
  136. pr_debug("[CCF] name: %s, bit: %d\n", name, (int)bit);
  137. #endif /* MT_CCF_DEBUG */
  138. cg = kzalloc(sizeof(*cg), GFP_KERNEL);
  139. if (!cg)
  140. return ERR_PTR(-ENOMEM);
  141. init.name = name;
  142. init.flags = CLK_IGNORE_UNUSED;
  143. init.parent_names = parent_name ? &parent_name : NULL;
  144. init.num_parents = parent_name ? 1 : 0;
  145. init.ops = &mtk_clk_gate_ops;
  146. cg->set_addr = set_addr;
  147. cg->clr_addr = clr_addr;
  148. cg->sta_addr = sta_addr;
  149. cg->bit = bit;
  150. cg->flags = flags;
  151. cg->hw.init = &init;
  152. clk = clk_register(NULL, &cg->hw);
  153. if (IS_ERR(clk))
  154. kfree(cg);
  155. return clk;
  156. }