clk-mux.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 MediaTek Inc.
  4. * Author: Owen Chen <owen.chen@mediatek.com>
  5. */
  6. #include <linux/of.h>
  7. #include <linux/of_address.h>
  8. #include <linux/slab.h>
  9. #include <linux/mfd/syscon.h>
  10. #include "clk-mtk.h"
  11. #include "clk-mux.h"
  12. #if defined(CONFIG_MACH_MT6768)
  13. void mm_polling(struct clk_hw *hw);
  14. #endif
  15. static inline struct mtk_clk_mux
  16. *to_mtk_clk_mux(struct clk_hw *hw)
  17. {
  18. return container_of(hw, struct mtk_clk_mux, hw);
  19. }
  20. static int mtk_mux_enable(struct clk_hw *hw)
  21. {
  22. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  23. u32 mask = 0;
  24. unsigned long flags = 0;
  25. int ret = 0;
  26. if (mux->gate_shift < 0)
  27. return 0;
  28. mask = BIT(mux->gate_shift);
  29. if (mux->lock)
  30. spin_lock_irqsave(mux->lock, flags);
  31. else
  32. __acquire(mux->lock);
  33. ret = regmap_update_bits(mux->regmap, mux->mux_ofs, mask, 0);
  34. if (mux->lock)
  35. spin_unlock_irqrestore(mux->lock, flags);
  36. else
  37. __release(mux->lock);
  38. return ret;
  39. }
  40. static void mtk_mux_disable(struct clk_hw *hw)
  41. {
  42. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  43. u32 mask = 0;
  44. unsigned long flags = 0;
  45. if (mux->gate_shift < 0)
  46. return;
  47. mask = BIT(mux->gate_shift);
  48. if (mux->lock)
  49. spin_lock_irqsave(mux->lock, flags);
  50. else
  51. __acquire(mux->lock);
  52. regmap_update_bits(mux->regmap, mux->mux_ofs, mask, mask);
  53. if (mux->lock)
  54. spin_unlock_irqrestore(mux->lock, flags);
  55. else
  56. __release(mux->lock);
  57. }
  58. static int mtk_mux_enable_setclr(struct clk_hw *hw)
  59. {
  60. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  61. u32 val = 0;
  62. unsigned long flags = 0;
  63. if (mux->gate_shift < 0)
  64. return 0;
  65. if (mux->lock)
  66. spin_lock_irqsave(mux->lock, flags);
  67. else
  68. __acquire(mux->lock);
  69. val = BIT(mux->gate_shift);
  70. regmap_write(mux->regmap, mux->clr_ofs,
  71. val);
  72. if (mux->lock)
  73. spin_unlock_irqrestore(mux->lock, flags);
  74. else
  75. __release(mux->lock);
  76. return 0;
  77. }
  78. static void mtk_mux_disable_setclr(struct clk_hw *hw)
  79. {
  80. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  81. unsigned long flags = 0;
  82. if (mux->gate_shift < 0)
  83. return;
  84. if (mux->lock)
  85. spin_lock_irqsave(mux->lock, flags);
  86. else
  87. __acquire(mux->lock);
  88. regmap_write(mux->regmap, mux->set_ofs,
  89. BIT(mux->gate_shift));
  90. if (mux->lock)
  91. spin_unlock_irqrestore(mux->lock, flags);
  92. else
  93. __release(mux->lock);
  94. }
  95. static int mtk_mux_is_enabled(struct clk_hw *hw)
  96. {
  97. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  98. u32 val = 0;
  99. if (mux->gate_shift < 0)
  100. return true;
  101. regmap_read(mux->regmap, mux->mux_ofs, &val);
  102. return (val & BIT(mux->gate_shift)) == 0;
  103. }
  104. static u8 mtk_mux_get_parent(struct clk_hw *hw)
  105. {
  106. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  107. int num_parents = clk_hw_get_num_parents(hw);
  108. u32 mask = GENMASK(mux->mux_width - 1, 0);
  109. u32 val = 0;
  110. regmap_read(mux->regmap, mux->mux_ofs, &val);
  111. val = (val >> mux->mux_shift) & mask;
  112. if (val >= num_parents)
  113. return -EINVAL;
  114. return val;
  115. }
  116. static int mtk_mux_set_parent(struct clk_hw *hw, u8 index)
  117. {
  118. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  119. u32 mask = GENMASK(mux->mux_width - 1, 0)
  120. << mux->mux_shift;
  121. unsigned long flags = 0;
  122. if (mux->lock)
  123. spin_lock_irqsave(mux->lock, flags);
  124. else
  125. __acquire(mux->lock);
  126. regmap_update_bits(mux->regmap, mux->mux_ofs, mask,
  127. index << mux->mux_shift);
  128. if (mux->upd_shift >= 0)
  129. regmap_write(mux->regmap, mux->upd_ofs,
  130. BIT(mux->upd_shift));
  131. if (mux->lock)
  132. spin_unlock_irqrestore(mux->lock, flags);
  133. else
  134. __release(mux->lock);
  135. return 0;
  136. }
  137. static int mtk_mux_set_parent_setclr(struct clk_hw *hw, u8 index)
  138. {
  139. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  140. u32 mask = GENMASK(mux->mux_width - 1, 0);
  141. u32 val = 0, orig = 0;
  142. unsigned long flags = 0;
  143. if (mux->lock)
  144. spin_lock_irqsave(mux->lock, flags);
  145. else
  146. __acquire(mux->lock);
  147. regmap_read(mux->regmap, mux->mux_ofs, &orig);
  148. val = (orig & ~(mask << mux->mux_shift))
  149. | (index << mux->mux_shift);
  150. if (val != orig) {
  151. regmap_write(mux->regmap, mux->clr_ofs,
  152. mask << mux->mux_shift);
  153. regmap_write(mux->regmap, mux->set_ofs,
  154. index << mux->mux_shift);
  155. #if defined(CONFIG_MACH_MT6768)
  156. /*
  157. * Workaround for mm dvfs. Poll mm rdma reg before
  158. * clkmux switching.
  159. */
  160. if (!strcmp(__clk_get_name(hw->clk), "mm_sel"))
  161. mm_polling(hw);
  162. #endif
  163. if (mux->upd_shift >= 0)
  164. regmap_write(mux->regmap, mux->upd_ofs,
  165. BIT(mux->upd_shift));
  166. }
  167. if (mux->lock)
  168. spin_unlock_irqrestore(mux->lock, flags);
  169. else
  170. __release(mux->lock);
  171. return 0;
  172. }
  173. const struct clk_ops mtk_mux_ops = {
  174. .get_parent = mtk_mux_get_parent,
  175. .set_parent = mtk_mux_set_parent,
  176. };
  177. const struct clk_ops mtk_mux_clr_set_upd_ops = {
  178. .get_parent = mtk_mux_get_parent,
  179. .set_parent = mtk_mux_set_parent_setclr,
  180. };
  181. const struct clk_ops mtk_mux_gate_ops = {
  182. .enable = mtk_mux_enable,
  183. .disable = mtk_mux_disable,
  184. .is_enabled = mtk_mux_is_enabled,
  185. .get_parent = mtk_mux_get_parent,
  186. .set_parent = mtk_mux_set_parent,
  187. .determine_rate = NULL,
  188. };
  189. const struct clk_ops mtk_mux_gate_clr_set_upd_ops = {
  190. .enable = mtk_mux_enable_setclr,
  191. .disable = mtk_mux_disable_setclr,
  192. .is_enabled = mtk_mux_is_enabled,
  193. .get_parent = mtk_mux_get_parent,
  194. .set_parent = mtk_mux_set_parent_setclr,
  195. .determine_rate = NULL,
  196. };
  197. struct clk *mtk_clk_register_mux(const struct mtk_mux *mux,
  198. struct regmap *regmap,
  199. spinlock_t *lock)
  200. {
  201. struct mtk_clk_mux *clk_mux = NULL;
  202. struct clk_init_data init = {};
  203. struct clk *clk;
  204. clk_mux = kzalloc(sizeof(*clk_mux), GFP_KERNEL);
  205. if (!clk_mux)
  206. return ERR_PTR(-ENOMEM);
  207. init.name = mux->name;
  208. init.flags = (mux->flags) | CLK_SET_RATE_PARENT;
  209. init.parent_names = mux->parent_names;
  210. init.num_parents = mux->num_parents;
  211. init.ops = mux->ops;
  212. clk_mux->regmap = regmap;
  213. clk_mux->name = mux->name;
  214. clk_mux->mux_ofs = mux->mux_ofs;
  215. clk_mux->set_ofs = mux->set_ofs;
  216. clk_mux->clr_ofs = mux->clr_ofs;
  217. clk_mux->upd_ofs = mux->upd_ofs;
  218. clk_mux->mux_shift = mux->mux_shift;
  219. clk_mux->mux_width = mux->mux_width;
  220. clk_mux->gate_shift = mux->gate_shift;
  221. clk_mux->upd_shift = mux->upd_shift;
  222. clk_mux->lock = lock;
  223. clk_mux->hw.init = &init;
  224. clk = clk_register(NULL, &clk_mux->hw);
  225. if (IS_ERR(clk)) {
  226. kfree(clk_mux);
  227. return clk;
  228. }
  229. return clk;
  230. }
  231. int mtk_clk_register_muxes(const struct mtk_mux *muxes,
  232. int num, struct device_node *node,
  233. spinlock_t *lock,
  234. struct clk_onecell_data *clk_data)
  235. {
  236. struct regmap *regmap;
  237. struct clk *clk;
  238. int i;
  239. regmap = syscon_node_to_regmap(node);
  240. if (IS_ERR(regmap)) {
  241. pr_notice("Cannot find regmap for %pOF: %ld\n", node,
  242. PTR_ERR(regmap));
  243. return PTR_ERR(regmap);
  244. }
  245. for (i = 0; i < num; i++) {
  246. const struct mtk_mux *mux = &muxes[i];
  247. if (IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
  248. clk = mtk_clk_register_mux(mux, regmap, lock);
  249. if (IS_ERR(clk)) {
  250. pr_notice("Failed to register clk %s: %ld\n",
  251. mux->name, PTR_ERR(clk));
  252. continue;
  253. }
  254. clk_data->clks[mux->id] = clk;
  255. }
  256. }
  257. return 0;
  258. }