clk-mux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/compiler_types.h>
  9. #include <linux/container_of.h>
  10. #include <linux/dev_printk.h>
  11. #include <linux/err.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/regmap.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/slab.h>
  17. #include "clk-mtk.h"
  18. #include "clk-mux.h"
  19. #define MTK_WAIT_FENC_DONE_US 30
  20. struct mtk_clk_mux {
  21. struct clk_hw hw;
  22. struct regmap *regmap;
  23. struct regmap *regmap_hwv;
  24. const struct mtk_mux *data;
  25. spinlock_t *lock;
  26. bool reparent;
  27. };
  28. static inline struct mtk_clk_mux *to_mtk_clk_mux(struct clk_hw *hw)
  29. {
  30. return container_of(hw, struct mtk_clk_mux, hw);
  31. }
  32. static int mtk_clk_mux_fenc_enable_setclr(struct clk_hw *hw)
  33. {
  34. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  35. unsigned long flags;
  36. u32 val;
  37. int ret;
  38. if (mux->lock)
  39. spin_lock_irqsave(mux->lock, flags);
  40. else
  41. __acquire(mux->lock);
  42. regmap_write(mux->regmap, mux->data->clr_ofs,
  43. BIT(mux->data->gate_shift));
  44. ret = regmap_read_poll_timeout_atomic(mux->regmap, mux->data->fenc_sta_mon_ofs,
  45. val, val & BIT(mux->data->fenc_shift), 1,
  46. MTK_WAIT_FENC_DONE_US);
  47. if (mux->lock)
  48. spin_unlock_irqrestore(mux->lock, flags);
  49. else
  50. __release(mux->lock);
  51. return ret;
  52. }
  53. static int mtk_clk_mux_enable_setclr(struct clk_hw *hw)
  54. {
  55. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  56. unsigned long flags = 0;
  57. if (mux->lock)
  58. spin_lock_irqsave(mux->lock, flags);
  59. else
  60. __acquire(mux->lock);
  61. regmap_write(mux->regmap, mux->data->clr_ofs,
  62. BIT(mux->data->gate_shift));
  63. /*
  64. * If the parent has been changed when the clock was disabled, it will
  65. * not be effective yet. Set the update bit to ensure the mux gets
  66. * updated.
  67. */
  68. if (mux->reparent && mux->data->upd_shift >= 0) {
  69. regmap_write(mux->regmap, mux->data->upd_ofs,
  70. BIT(mux->data->upd_shift));
  71. mux->reparent = false;
  72. }
  73. if (mux->lock)
  74. spin_unlock_irqrestore(mux->lock, flags);
  75. else
  76. __release(mux->lock);
  77. return 0;
  78. }
  79. static void mtk_clk_mux_disable_setclr(struct clk_hw *hw)
  80. {
  81. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  82. regmap_write(mux->regmap, mux->data->set_ofs,
  83. BIT(mux->data->gate_shift));
  84. }
  85. static int mtk_clk_mux_fenc_is_enabled(struct clk_hw *hw)
  86. {
  87. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  88. u32 val;
  89. regmap_read(mux->regmap, mux->data->fenc_sta_mon_ofs, &val);
  90. return !!(val & BIT(mux->data->fenc_shift));
  91. }
  92. static int mtk_clk_mux_is_enabled(struct clk_hw *hw)
  93. {
  94. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  95. u32 val;
  96. regmap_read(mux->regmap, mux->data->mux_ofs, &val);
  97. return (val & BIT(mux->data->gate_shift)) == 0;
  98. }
  99. static int mtk_clk_mux_hwv_fenc_enable(struct clk_hw *hw)
  100. {
  101. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  102. u32 val;
  103. int ret;
  104. regmap_write(mux->regmap_hwv, mux->data->hwv_set_ofs,
  105. BIT(mux->data->gate_shift));
  106. ret = regmap_read_poll_timeout_atomic(mux->regmap_hwv, mux->data->hwv_sta_ofs,
  107. val, val & BIT(mux->data->gate_shift), 0,
  108. MTK_WAIT_HWV_DONE_US);
  109. if (ret)
  110. return ret;
  111. ret = regmap_read_poll_timeout_atomic(mux->regmap, mux->data->fenc_sta_mon_ofs,
  112. val, val & BIT(mux->data->fenc_shift), 1,
  113. MTK_WAIT_FENC_DONE_US);
  114. return ret;
  115. }
  116. static void mtk_clk_mux_hwv_disable(struct clk_hw *hw)
  117. {
  118. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  119. u32 val;
  120. regmap_write(mux->regmap_hwv, mux->data->hwv_clr_ofs,
  121. BIT(mux->data->gate_shift));
  122. regmap_read_poll_timeout_atomic(mux->regmap_hwv, mux->data->hwv_sta_ofs,
  123. val, (val & BIT(mux->data->gate_shift)),
  124. 0, MTK_WAIT_HWV_DONE_US);
  125. }
  126. static u8 mtk_clk_mux_get_parent(struct clk_hw *hw)
  127. {
  128. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  129. u32 mask = GENMASK(mux->data->mux_width - 1, 0);
  130. u32 val;
  131. regmap_read(mux->regmap, mux->data->mux_ofs, &val);
  132. val = (val >> mux->data->mux_shift) & mask;
  133. if (mux->data->parent_index) {
  134. int i;
  135. for (i = 0; i < mux->data->num_parents; i++)
  136. if (mux->data->parent_index[i] == val)
  137. return i;
  138. /* Not found: return an impossible index to generate error */
  139. return mux->data->num_parents + 1;
  140. }
  141. return val;
  142. }
  143. static int mtk_clk_mux_set_parent_setclr_lock(struct clk_hw *hw, u8 index)
  144. {
  145. struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
  146. u32 mask = GENMASK(mux->data->mux_width - 1, 0);
  147. u32 val, orig;
  148. unsigned long flags = 0;
  149. if (mux->lock)
  150. spin_lock_irqsave(mux->lock, flags);
  151. else
  152. __acquire(mux->lock);
  153. if (mux->data->parent_index)
  154. index = mux->data->parent_index[index];
  155. regmap_read(mux->regmap, mux->data->mux_ofs, &orig);
  156. val = (orig & ~(mask << mux->data->mux_shift))
  157. | (index << mux->data->mux_shift);
  158. if (val != orig) {
  159. regmap_write(mux->regmap, mux->data->clr_ofs,
  160. mask << mux->data->mux_shift);
  161. regmap_write(mux->regmap, mux->data->set_ofs,
  162. index << mux->data->mux_shift);
  163. if (mux->data->upd_shift >= 0) {
  164. regmap_write(mux->regmap, mux->data->upd_ofs,
  165. BIT(mux->data->upd_shift));
  166. mux->reparent = true;
  167. }
  168. }
  169. if (mux->lock)
  170. spin_unlock_irqrestore(mux->lock, flags);
  171. else
  172. __release(mux->lock);
  173. return 0;
  174. }
  175. static int mtk_clk_mux_determine_rate(struct clk_hw *hw,
  176. struct clk_rate_request *req)
  177. {
  178. return clk_mux_determine_rate_flags(hw, req, 0);
  179. }
  180. static bool mtk_clk_mux_uses_hwv(const struct clk_ops *ops)
  181. {
  182. if (ops == &mtk_mux_gate_hwv_fenc_clr_set_upd_ops)
  183. return true;
  184. return false;
  185. }
  186. const struct clk_ops mtk_mux_clr_set_upd_ops = {
  187. .get_parent = mtk_clk_mux_get_parent,
  188. .set_parent = mtk_clk_mux_set_parent_setclr_lock,
  189. .determine_rate = mtk_clk_mux_determine_rate,
  190. };
  191. EXPORT_SYMBOL_GPL(mtk_mux_clr_set_upd_ops);
  192. const struct clk_ops mtk_mux_gate_clr_set_upd_ops = {
  193. .enable = mtk_clk_mux_enable_setclr,
  194. .disable = mtk_clk_mux_disable_setclr,
  195. .is_enabled = mtk_clk_mux_is_enabled,
  196. .get_parent = mtk_clk_mux_get_parent,
  197. .set_parent = mtk_clk_mux_set_parent_setclr_lock,
  198. .determine_rate = mtk_clk_mux_determine_rate,
  199. };
  200. EXPORT_SYMBOL_GPL(mtk_mux_gate_clr_set_upd_ops);
  201. const struct clk_ops mtk_mux_gate_fenc_clr_set_upd_ops = {
  202. .enable = mtk_clk_mux_fenc_enable_setclr,
  203. .disable = mtk_clk_mux_disable_setclr,
  204. .is_enabled = mtk_clk_mux_fenc_is_enabled,
  205. .get_parent = mtk_clk_mux_get_parent,
  206. .set_parent = mtk_clk_mux_set_parent_setclr_lock,
  207. .determine_rate = mtk_clk_mux_determine_rate,
  208. };
  209. EXPORT_SYMBOL_GPL(mtk_mux_gate_fenc_clr_set_upd_ops);
  210. const struct clk_ops mtk_mux_gate_hwv_fenc_clr_set_upd_ops = {
  211. .enable = mtk_clk_mux_hwv_fenc_enable,
  212. .disable = mtk_clk_mux_hwv_disable,
  213. .is_enabled = mtk_clk_mux_fenc_is_enabled,
  214. .get_parent = mtk_clk_mux_get_parent,
  215. .set_parent = mtk_clk_mux_set_parent_setclr_lock,
  216. .determine_rate = mtk_clk_mux_determine_rate,
  217. };
  218. EXPORT_SYMBOL_GPL(mtk_mux_gate_hwv_fenc_clr_set_upd_ops);
  219. static struct clk_hw *mtk_clk_register_mux(struct device *dev,
  220. const struct mtk_mux *mux,
  221. struct regmap *regmap,
  222. struct regmap *regmap_hwv,
  223. spinlock_t *lock)
  224. {
  225. struct mtk_clk_mux *clk_mux;
  226. struct clk_init_data init = {};
  227. int ret;
  228. clk_mux = kzalloc_obj(*clk_mux);
  229. if (!clk_mux)
  230. return ERR_PTR(-ENOMEM);
  231. init.name = mux->name;
  232. init.flags = mux->flags;
  233. init.parent_names = mux->parent_names;
  234. init.num_parents = mux->num_parents;
  235. init.ops = mux->ops;
  236. if (mtk_clk_mux_uses_hwv(init.ops) && !regmap_hwv)
  237. return dev_err_ptr_probe(
  238. dev, -ENXIO,
  239. "regmap not found for hardware voter clocks\n");
  240. clk_mux->regmap = regmap;
  241. clk_mux->regmap_hwv = regmap_hwv;
  242. clk_mux->data = mux;
  243. clk_mux->lock = lock;
  244. clk_mux->hw.init = &init;
  245. ret = clk_hw_register(dev, &clk_mux->hw);
  246. if (ret) {
  247. kfree(clk_mux);
  248. return ERR_PTR(ret);
  249. }
  250. return &clk_mux->hw;
  251. }
  252. static void mtk_clk_unregister_mux(struct clk_hw *hw)
  253. {
  254. struct mtk_clk_mux *mux;
  255. if (!hw)
  256. return;
  257. mux = to_mtk_clk_mux(hw);
  258. clk_hw_unregister(hw);
  259. kfree(mux);
  260. }
  261. int mtk_clk_register_muxes(struct device *dev,
  262. const struct mtk_mux *muxes,
  263. int num, struct device_node *node,
  264. spinlock_t *lock,
  265. struct clk_hw_onecell_data *clk_data)
  266. {
  267. struct regmap *regmap;
  268. struct regmap *regmap_hwv;
  269. struct clk_hw *hw;
  270. int i;
  271. regmap = device_node_to_regmap(node);
  272. if (IS_ERR(regmap)) {
  273. pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
  274. return PTR_ERR(regmap);
  275. }
  276. regmap_hwv = mtk_clk_get_hwv_regmap(node);
  277. if (IS_ERR(regmap_hwv))
  278. return dev_err_probe(
  279. dev, PTR_ERR(regmap_hwv),
  280. "Cannot find hardware voter regmap for %pOF\n", node);
  281. for (i = 0; i < num; i++) {
  282. const struct mtk_mux *mux = &muxes[i];
  283. if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
  284. pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
  285. node, mux->id);
  286. continue;
  287. }
  288. hw = mtk_clk_register_mux(dev, mux, regmap, regmap_hwv, lock);
  289. if (IS_ERR(hw)) {
  290. pr_err("Failed to register clk %s: %pe\n", mux->name,
  291. hw);
  292. goto err;
  293. }
  294. clk_data->hws[mux->id] = hw;
  295. }
  296. return 0;
  297. err:
  298. while (--i >= 0) {
  299. const struct mtk_mux *mux = &muxes[i];
  300. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  301. continue;
  302. mtk_clk_unregister_mux(clk_data->hws[mux->id]);
  303. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  304. }
  305. return PTR_ERR(hw);
  306. }
  307. EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);
  308. void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
  309. struct clk_hw_onecell_data *clk_data)
  310. {
  311. int i;
  312. if (!clk_data)
  313. return;
  314. for (i = num; i > 0; i--) {
  315. const struct mtk_mux *mux = &muxes[i - 1];
  316. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  317. continue;
  318. mtk_clk_unregister_mux(clk_data->hws[mux->id]);
  319. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  320. }
  321. }
  322. EXPORT_SYMBOL_GPL(mtk_clk_unregister_muxes);
  323. /*
  324. * This clock notifier is called when the frequency of the parent
  325. * PLL clock is to be changed. The idea is to switch the parent to a
  326. * stable clock, such as the main oscillator, while the PLL frequency
  327. * stabilizes.
  328. */
  329. static int mtk_clk_mux_notifier_cb(struct notifier_block *nb,
  330. unsigned long event, void *_data)
  331. {
  332. struct clk_notifier_data *data = _data;
  333. struct clk_hw *hw = __clk_get_hw(data->clk);
  334. struct mtk_mux_nb *mux_nb = to_mtk_mux_nb(nb);
  335. int ret = 0;
  336. switch (event) {
  337. case PRE_RATE_CHANGE:
  338. mux_nb->original_index = mux_nb->ops->get_parent(hw);
  339. ret = mux_nb->ops->set_parent(hw, mux_nb->bypass_index);
  340. break;
  341. case POST_RATE_CHANGE:
  342. case ABORT_RATE_CHANGE:
  343. ret = mux_nb->ops->set_parent(hw, mux_nb->original_index);
  344. break;
  345. }
  346. return notifier_from_errno(ret);
  347. }
  348. int devm_mtk_clk_mux_notifier_register(struct device *dev, struct clk *clk,
  349. struct mtk_mux_nb *mux_nb)
  350. {
  351. mux_nb->nb.notifier_call = mtk_clk_mux_notifier_cb;
  352. return devm_clk_notifier_register(dev, clk, &mux_nb->nb);
  353. }
  354. EXPORT_SYMBOL_GPL(devm_mtk_clk_mux_notifier_register);
  355. MODULE_LICENSE("GPL");