clk-cpumux.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 Linaro Ltd.
  4. * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/container_of.h>
  8. #include <linux/err.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/regmap.h>
  12. #include <linux/slab.h>
  13. #include "clk-mtk.h"
  14. #include "clk-cpumux.h"
  15. struct mtk_clk_cpumux {
  16. struct clk_hw hw;
  17. struct regmap *regmap;
  18. u32 reg;
  19. u32 mask;
  20. u8 shift;
  21. };
  22. static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
  23. {
  24. return container_of(_hw, struct mtk_clk_cpumux, hw);
  25. }
  26. static u8 clk_cpumux_get_parent(struct clk_hw *hw)
  27. {
  28. struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
  29. unsigned int val;
  30. regmap_read(mux->regmap, mux->reg, &val);
  31. val >>= mux->shift;
  32. val &= mux->mask;
  33. return val;
  34. }
  35. static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index)
  36. {
  37. struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
  38. u32 mask, val;
  39. val = index << mux->shift;
  40. mask = mux->mask << mux->shift;
  41. return regmap_update_bits(mux->regmap, mux->reg, mask, val);
  42. }
  43. static const struct clk_ops clk_cpumux_ops = {
  44. .determine_rate = clk_hw_determine_rate_no_reparent,
  45. .get_parent = clk_cpumux_get_parent,
  46. .set_parent = clk_cpumux_set_parent,
  47. };
  48. static struct clk_hw *
  49. mtk_clk_register_cpumux(struct device *dev, const struct mtk_composite *mux,
  50. struct regmap *regmap)
  51. {
  52. struct mtk_clk_cpumux *cpumux;
  53. int ret;
  54. struct clk_init_data init;
  55. cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
  56. if (!cpumux)
  57. return ERR_PTR(-ENOMEM);
  58. init.name = mux->name;
  59. init.ops = &clk_cpumux_ops;
  60. init.parent_names = mux->parent_names;
  61. init.num_parents = mux->num_parents;
  62. init.flags = mux->flags;
  63. cpumux->reg = mux->mux_reg;
  64. cpumux->shift = mux->mux_shift;
  65. cpumux->mask = BIT(mux->mux_width) - 1;
  66. cpumux->regmap = regmap;
  67. cpumux->hw.init = &init;
  68. ret = clk_hw_register(dev, &cpumux->hw);
  69. if (ret) {
  70. kfree(cpumux);
  71. return ERR_PTR(ret);
  72. }
  73. return &cpumux->hw;
  74. }
  75. static void mtk_clk_unregister_cpumux(struct clk_hw *hw)
  76. {
  77. struct mtk_clk_cpumux *cpumux;
  78. if (!hw)
  79. return;
  80. cpumux = to_mtk_clk_cpumux(hw);
  81. clk_hw_unregister(hw);
  82. kfree(cpumux);
  83. }
  84. int mtk_clk_register_cpumuxes(struct device *dev, struct device_node *node,
  85. const struct mtk_composite *clks, int num,
  86. struct clk_hw_onecell_data *clk_data)
  87. {
  88. int i;
  89. struct clk_hw *hw;
  90. struct regmap *regmap;
  91. regmap = device_node_to_regmap(node);
  92. if (IS_ERR(regmap)) {
  93. pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
  94. return PTR_ERR(regmap);
  95. }
  96. for (i = 0; i < num; i++) {
  97. const struct mtk_composite *mux = &clks[i];
  98. if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
  99. pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
  100. node, mux->id);
  101. continue;
  102. }
  103. hw = mtk_clk_register_cpumux(dev, mux, regmap);
  104. if (IS_ERR(hw)) {
  105. pr_err("Failed to register clk %s: %pe\n", mux->name,
  106. hw);
  107. goto err;
  108. }
  109. clk_data->hws[mux->id] = hw;
  110. }
  111. return 0;
  112. err:
  113. while (--i >= 0) {
  114. const struct mtk_composite *mux = &clks[i];
  115. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  116. continue;
  117. mtk_clk_unregister_cpumux(clk_data->hws[mux->id]);
  118. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  119. }
  120. return PTR_ERR(hw);
  121. }
  122. EXPORT_SYMBOL_GPL(mtk_clk_register_cpumuxes);
  123. void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
  124. struct clk_hw_onecell_data *clk_data)
  125. {
  126. int i;
  127. for (i = num; i > 0; i--) {
  128. const struct mtk_composite *mux = &clks[i - 1];
  129. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  130. continue;
  131. mtk_clk_unregister_cpumux(clk_data->hws[mux->id]);
  132. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  133. }
  134. }
  135. EXPORT_SYMBOL_GPL(mtk_clk_unregister_cpumuxes);
  136. MODULE_LICENSE("GPL");