clk-gate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "clk-mtk.h"
  21. #include "clk-gate.h"
  22. static int mtk_cg_bit_is_cleared(struct clk_hw *hw)
  23. {
  24. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  25. u32 val;
  26. regmap_read(cg->regmap, cg->sta_ofs, &val);
  27. val &= BIT(cg->bit);
  28. return val == 0;
  29. }
  30. static int mtk_cg_bit_is_set(struct clk_hw *hw)
  31. {
  32. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  33. u32 val;
  34. regmap_read(cg->regmap, cg->sta_ofs, &val);
  35. val &= BIT(cg->bit);
  36. return val != 0;
  37. }
  38. static void mtk_cg_set_bit(struct clk_hw *hw)
  39. {
  40. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  41. regmap_write(cg->regmap, cg->set_ofs, BIT(cg->bit));
  42. }
  43. static void mtk_cg_clr_bit(struct clk_hw *hw)
  44. {
  45. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  46. regmap_write(cg->regmap, cg->clr_ofs, BIT(cg->bit));
  47. }
  48. static void mtk_cg_set_bit_no_setclr(struct clk_hw *hw)
  49. {
  50. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  51. u32 cgbit = BIT(cg->bit);
  52. regmap_update_bits(cg->regmap, cg->sta_ofs, cgbit, cgbit);
  53. }
  54. static void mtk_cg_clr_bit_no_setclr(struct clk_hw *hw)
  55. {
  56. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  57. u32 cgbit = BIT(cg->bit);
  58. regmap_update_bits(cg->regmap, cg->sta_ofs, cgbit, 0);
  59. }
  60. static int mtk_cg_enable(struct clk_hw *hw)
  61. {
  62. mtk_cg_clr_bit(hw);
  63. return 0;
  64. }
  65. static void mtk_cg_disable(struct clk_hw *hw)
  66. {
  67. mtk_cg_set_bit(hw);
  68. }
  69. static int mtk_cg_enable_inv(struct clk_hw *hw)
  70. {
  71. mtk_cg_set_bit(hw);
  72. return 0;
  73. }
  74. static void mtk_cg_disable_inv(struct clk_hw *hw)
  75. {
  76. mtk_cg_clr_bit(hw);
  77. }
  78. static int mtk_cg_enable_no_setclr(struct clk_hw *hw)
  79. {
  80. mtk_cg_clr_bit_no_setclr(hw);
  81. return 0;
  82. }
  83. static void mtk_cg_disable_no_setclr(struct clk_hw *hw)
  84. {
  85. mtk_cg_set_bit_no_setclr(hw);
  86. }
  87. static int mtk_cg_enable_inv_no_setclr(struct clk_hw *hw)
  88. {
  89. mtk_cg_set_bit_no_setclr(hw);
  90. return 0;
  91. }
  92. static void mtk_cg_disable_inv_no_setclr(struct clk_hw *hw)
  93. {
  94. mtk_cg_clr_bit_no_setclr(hw);
  95. }
  96. const struct clk_ops mtk_clk_gate_ops_setclr = {
  97. .is_enabled = mtk_cg_bit_is_cleared,
  98. .enable = mtk_cg_enable,
  99. .disable = mtk_cg_disable,
  100. };
  101. const struct clk_ops mtk_clk_gate_ops_setclr_inv = {
  102. .is_enabled = mtk_cg_bit_is_set,
  103. .enable = mtk_cg_enable_inv,
  104. .disable = mtk_cg_disable_inv,
  105. };
  106. const struct clk_ops mtk_clk_gate_ops_no_setclr = {
  107. .is_enabled = mtk_cg_bit_is_cleared,
  108. .enable = mtk_cg_enable_no_setclr,
  109. .disable = mtk_cg_disable_no_setclr,
  110. };
  111. const struct clk_ops mtk_clk_gate_ops_no_setclr_inv = {
  112. .is_enabled = mtk_cg_bit_is_set,
  113. .enable = mtk_cg_enable_inv_no_setclr,
  114. .disable = mtk_cg_disable_inv_no_setclr,
  115. };
  116. struct clk *mtk_clk_register_gate(
  117. const char *name,
  118. const char *parent_name,
  119. struct regmap *regmap,
  120. int set_ofs,
  121. int clr_ofs,
  122. int sta_ofs,
  123. u8 bit,
  124. const struct clk_ops *ops)
  125. {
  126. struct mtk_clk_gate *cg;
  127. struct clk *clk;
  128. struct clk_init_data init = {};
  129. cg = kzalloc(sizeof(*cg), GFP_KERNEL);
  130. if (!cg)
  131. return ERR_PTR(-ENOMEM);
  132. init.name = name;
  133. init.flags = CLK_SET_RATE_PARENT;
  134. init.parent_names = parent_name ? &parent_name : NULL;
  135. init.num_parents = parent_name ? 1 : 0;
  136. init.ops = ops;
  137. cg->regmap = regmap;
  138. cg->set_ofs = set_ofs;
  139. cg->clr_ofs = clr_ofs;
  140. cg->sta_ofs = sta_ofs;
  141. cg->bit = bit;
  142. cg->hw.init = &init;
  143. clk = clk_register(NULL, &cg->hw);
  144. if (IS_ERR(clk))
  145. kfree(cg);
  146. return clk;
  147. }