clk-mux.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 MediaTek Inc.
  4. * Author: Owen Chen <[email protected]>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/compiler_types.h>
  9. #include <linux/container_of.h>
  10. #include <linux/err.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/regmap.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/slab.h>
  16. #include "clk-mux.h"
  17. struct mtk_clk_mux {
  18. struct clk_hw hw;
  19. struct regmap *regmap;
  20. const struct mtk_mux *data;
  21. spinlock_t *lock;
  22. bool reparent;
  23. };
  24. static inline struct mtk_clk_mux *to_mtk_clk_mux(struct clk_hw *hw)
  25. {
  26. return container_of(hw, struct mtk_clk_mux, hw);
  27. }
  28. static int mtk_clk_mux_enable_setclr(struct clk_hw *hw)
  29. {
  30. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  31. unsigned long flags = 0;
  32. if (mux->lock)
  33. spin_lock_irqsave(mux->lock, flags);
  34. else
  35. __acquire(mux->lock);
  36. regmap_write(mux->regmap, mux->data->clr_ofs,
  37. BIT(mux->data->gate_shift));
  38. /*
  39. * If the parent has been changed when the clock was disabled, it will
  40. * not be effective yet. Set the update bit to ensure the mux gets
  41. * updated.
  42. */
  43. if (mux->reparent && mux->data->upd_shift >= 0) {
  44. regmap_write(mux->regmap, mux->data->upd_ofs,
  45. BIT(mux->data->upd_shift));
  46. mux->reparent = false;
  47. }
  48. if (mux->lock)
  49. spin_unlock_irqrestore(mux->lock, flags);
  50. else
  51. __release(mux->lock);
  52. return 0;
  53. }
  54. static void mtk_clk_mux_disable_setclr(struct clk_hw *hw)
  55. {
  56. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  57. regmap_write(mux->regmap, mux->data->set_ofs,
  58. BIT(mux->data->gate_shift));
  59. }
  60. static int mtk_clk_mux_is_enabled(struct clk_hw *hw)
  61. {
  62. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  63. u32 val;
  64. regmap_read(mux->regmap, mux->data->mux_ofs, &val);
  65. return (val & BIT(mux->data->gate_shift)) == 0;
  66. }
  67. static u8 mtk_clk_mux_get_parent(struct clk_hw *hw)
  68. {
  69. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  70. u32 mask = GENMASK(mux->data->mux_width - 1, 0);
  71. u32 val;
  72. regmap_read(mux->regmap, mux->data->mux_ofs, &val);
  73. val = (val >> mux->data->mux_shift) & mask;
  74. return val;
  75. }
  76. static int mtk_clk_mux_set_parent_setclr_lock(struct clk_hw *hw, u8 index)
  77. {
  78. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  79. u32 mask = GENMASK(mux->data->mux_width - 1, 0);
  80. u32 val, orig;
  81. unsigned long flags = 0;
  82. if (mux->lock)
  83. spin_lock_irqsave(mux->lock, flags);
  84. else
  85. __acquire(mux->lock);
  86. regmap_read(mux->regmap, mux->data->mux_ofs, &orig);
  87. val = (orig & ~(mask << mux->data->mux_shift))
  88. | (index << mux->data->mux_shift);
  89. if (val != orig) {
  90. regmap_write(mux->regmap, mux->data->clr_ofs,
  91. mask << mux->data->mux_shift);
  92. regmap_write(mux->regmap, mux->data->set_ofs,
  93. index << mux->data->mux_shift);
  94. if (mux->data->upd_shift >= 0) {
  95. regmap_write(mux->regmap, mux->data->upd_ofs,
  96. BIT(mux->data->upd_shift));
  97. mux->reparent = true;
  98. }
  99. }
  100. if (mux->lock)
  101. spin_unlock_irqrestore(mux->lock, flags);
  102. else
  103. __release(mux->lock);
  104. return 0;
  105. }
  106. static int mtk_clk_mux_determine_rate(struct clk_hw *hw,
  107. struct clk_rate_request *req)
  108. {
  109. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  110. return clk_mux_determine_rate_flags(hw, req, mux->data->flags);
  111. }
  112. const struct clk_ops mtk_mux_clr_set_upd_ops = {
  113. .get_parent = mtk_clk_mux_get_parent,
  114. .set_parent = mtk_clk_mux_set_parent_setclr_lock,
  115. .determine_rate = mtk_clk_mux_determine_rate,
  116. };
  117. EXPORT_SYMBOL_GPL(mtk_mux_clr_set_upd_ops);
  118. const struct clk_ops mtk_mux_gate_clr_set_upd_ops = {
  119. .enable = mtk_clk_mux_enable_setclr,
  120. .disable = mtk_clk_mux_disable_setclr,
  121. .is_enabled = mtk_clk_mux_is_enabled,
  122. .get_parent = mtk_clk_mux_get_parent,
  123. .set_parent = mtk_clk_mux_set_parent_setclr_lock,
  124. .determine_rate = mtk_clk_mux_determine_rate,
  125. };
  126. EXPORT_SYMBOL_GPL(mtk_mux_gate_clr_set_upd_ops);
  127. static struct clk_hw *mtk_clk_register_mux(const struct mtk_mux *mux,
  128. struct regmap *regmap,
  129. spinlock_t *lock)
  130. {
  131. struct mtk_clk_mux *clk_mux;
  132. struct clk_init_data init = {};
  133. int ret;
  134. clk_mux = kzalloc(sizeof(*clk_mux), GFP_KERNEL);
  135. if (!clk_mux)
  136. return ERR_PTR(-ENOMEM);
  137. init.name = mux->name;
  138. init.flags = mux->flags | CLK_SET_RATE_PARENT;
  139. init.parent_names = mux->parent_names;
  140. init.num_parents = mux->num_parents;
  141. init.ops = mux->ops;
  142. clk_mux->regmap = regmap;
  143. clk_mux->data = mux;
  144. clk_mux->lock = lock;
  145. clk_mux->hw.init = &init;
  146. ret = clk_hw_register(NULL, &clk_mux->hw);
  147. if (ret) {
  148. kfree(clk_mux);
  149. return ERR_PTR(ret);
  150. }
  151. return &clk_mux->hw;
  152. }
  153. static void mtk_clk_unregister_mux(struct clk_hw *hw)
  154. {
  155. struct mtk_clk_mux *mux;
  156. if (!hw)
  157. return;
  158. mux = to_mtk_clk_mux(hw);
  159. clk_hw_unregister(hw);
  160. kfree(mux);
  161. }
  162. int mtk_clk_register_muxes(const struct mtk_mux *muxes,
  163. int num, struct device_node *node,
  164. spinlock_t *lock,
  165. struct clk_hw_onecell_data *clk_data)
  166. {
  167. struct regmap *regmap;
  168. struct clk_hw *hw;
  169. int i;
  170. regmap = device_node_to_regmap(node);
  171. if (IS_ERR(regmap)) {
  172. pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
  173. return PTR_ERR(regmap);
  174. }
  175. for (i = 0; i < num; i++) {
  176. const struct mtk_mux *mux = &muxes[i];
  177. if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
  178. pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
  179. node, mux->id);
  180. continue;
  181. }
  182. hw = mtk_clk_register_mux(mux, regmap, lock);
  183. if (IS_ERR(hw)) {
  184. pr_err("Failed to register clk %s: %pe\n", mux->name,
  185. hw);
  186. goto err;
  187. }
  188. clk_data->hws[mux->id] = hw;
  189. }
  190. return 0;
  191. err:
  192. while (--i >= 0) {
  193. const struct mtk_mux *mux = &muxes[i];
  194. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  195. continue;
  196. mtk_clk_unregister_mux(clk_data->hws[mux->id]);
  197. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  198. }
  199. return PTR_ERR(hw);
  200. }
  201. EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);
  202. void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
  203. struct clk_hw_onecell_data *clk_data)
  204. {
  205. int i;
  206. if (!clk_data)
  207. return;
  208. for (i = num; i > 0; i--) {
  209. const struct mtk_mux *mux = &muxes[i - 1];
  210. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  211. continue;
  212. mtk_clk_unregister_mux(clk_data->hws[mux->id]);
  213. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  214. }
  215. }
  216. EXPORT_SYMBOL_GPL(mtk_clk_unregister_muxes);
  217. /*
  218. * This clock notifier is called when the frequency of the parent
  219. * PLL clock is to be changed. The idea is to switch the parent to a
  220. * stable clock, such as the main oscillator, while the PLL frequency
  221. * stabilizes.
  222. */
  223. static int mtk_clk_mux_notifier_cb(struct notifier_block *nb,
  224. unsigned long event, void *_data)
  225. {
  226. struct clk_notifier_data *data = _data;
  227. struct clk_hw *hw = __clk_get_hw(data->clk);
  228. struct mtk_mux_nb *mux_nb = to_mtk_mux_nb(nb);
  229. int ret = 0;
  230. switch (event) {
  231. case PRE_RATE_CHANGE:
  232. mux_nb->original_index = mux_nb->ops->get_parent(hw);
  233. ret = mux_nb->ops->set_parent(hw, mux_nb->bypass_index);
  234. break;
  235. case POST_RATE_CHANGE:
  236. case ABORT_RATE_CHANGE:
  237. ret = mux_nb->ops->set_parent(hw, mux_nb->original_index);
  238. break;
  239. }
  240. return notifier_from_errno(ret);
  241. }
  242. int devm_mtk_clk_mux_notifier_register(struct device *dev, struct clk *clk,
  243. struct mtk_mux_nb *mux_nb)
  244. {
  245. mux_nb->nb.notifier_call = mtk_clk_mux_notifier_cb;
  246. return devm_clk_notifier_register(dev, clk, &mux_nb->nb);
  247. }
  248. EXPORT_SYMBOL_GPL(devm_mtk_clk_mux_notifier_register);
  249. MODULE_LICENSE("GPL");