clk-cpumux.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 Linaro Ltd.
  4. * Author: Pi-Cheng Chen <[email protected]>
  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. .get_parent = clk_cpumux_get_parent,
  45. .set_parent = clk_cpumux_set_parent,
  46. };
  47. static struct clk_hw *
  48. mtk_clk_register_cpumux(const struct mtk_composite *mux,
  49. struct regmap *regmap)
  50. {
  51. struct mtk_clk_cpumux *cpumux;
  52. int ret;
  53. struct clk_init_data init;
  54. cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
  55. if (!cpumux)
  56. return ERR_PTR(-ENOMEM);
  57. init.name = mux->name;
  58. init.ops = &clk_cpumux_ops;
  59. init.parent_names = mux->parent_names;
  60. init.num_parents = mux->num_parents;
  61. init.flags = mux->flags;
  62. cpumux->reg = mux->mux_reg;
  63. cpumux->shift = mux->mux_shift;
  64. cpumux->mask = BIT(mux->mux_width) - 1;
  65. cpumux->regmap = regmap;
  66. cpumux->hw.init = &init;
  67. ret = clk_hw_register(NULL, &cpumux->hw);
  68. if (ret) {
  69. kfree(cpumux);
  70. return ERR_PTR(ret);
  71. }
  72. return &cpumux->hw;
  73. }
  74. static void mtk_clk_unregister_cpumux(struct clk_hw *hw)
  75. {
  76. struct mtk_clk_cpumux *cpumux;
  77. if (!hw)
  78. return;
  79. cpumux = to_mtk_clk_cpumux(hw);
  80. clk_hw_unregister(hw);
  81. kfree(cpumux);
  82. }
  83. int mtk_clk_register_cpumuxes(struct device_node *node,
  84. const struct mtk_composite *clks, int num,
  85. struct clk_hw_onecell_data *clk_data)
  86. {
  87. int i;
  88. struct clk_hw *hw;
  89. struct regmap *regmap;
  90. regmap = device_node_to_regmap(node);
  91. if (IS_ERR(regmap)) {
  92. pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
  93. return PTR_ERR(regmap);
  94. }
  95. for (i = 0; i < num; i++) {
  96. const struct mtk_composite *mux = &clks[i];
  97. if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
  98. pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
  99. node, mux->id);
  100. continue;
  101. }
  102. hw = mtk_clk_register_cpumux(mux, regmap);
  103. if (IS_ERR(hw)) {
  104. pr_err("Failed to register clk %s: %pe\n", mux->name,
  105. hw);
  106. goto err;
  107. }
  108. clk_data->hws[mux->id] = hw;
  109. }
  110. return 0;
  111. err:
  112. while (--i >= 0) {
  113. const struct mtk_composite *mux = &clks[i];
  114. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  115. continue;
  116. mtk_clk_unregister_cpumux(clk_data->hws[mux->id]);
  117. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  118. }
  119. return PTR_ERR(hw);
  120. }
  121. EXPORT_SYMBOL_GPL(mtk_clk_register_cpumuxes);
  122. void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
  123. struct clk_hw_onecell_data *clk_data)
  124. {
  125. int i;
  126. for (i = num; i > 0; i--) {
  127. const struct mtk_composite *mux = &clks[i - 1];
  128. if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
  129. continue;
  130. mtk_clk_unregister_cpumux(clk_data->hws[mux->id]);
  131. clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
  132. }
  133. }
  134. EXPORT_SYMBOL_GPL(mtk_clk_unregister_cpumuxes);
  135. MODULE_LICENSE("GPL");