clk-cpumux.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/mfd/syscon.h>
  8. #include <linux/slab.h>
  9. #include "clk-mtk.h"
  10. #include "clk-cpumux.h"
  11. static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
  12. {
  13. return container_of(_hw, struct mtk_clk_cpumux, hw);
  14. }
  15. static u8 clk_cpumux_get_parent(struct clk_hw *hw)
  16. {
  17. struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
  18. unsigned int val;
  19. regmap_read(mux->regmap, mux->reg, &val);
  20. val >>= mux->shift;
  21. val &= mux->mask;
  22. return val;
  23. }
  24. static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index)
  25. {
  26. struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
  27. u32 mask, val;
  28. val = index << mux->shift;
  29. mask = mux->mask << mux->shift;
  30. return regmap_update_bits(mux->regmap, mux->reg, mask, val);
  31. }
  32. static const struct clk_ops clk_cpumux_ops = {
  33. .get_parent = clk_cpumux_get_parent,
  34. .set_parent = clk_cpumux_set_parent,
  35. };
  36. static struct clk *
  37. mtk_clk_register_cpumux(const struct mtk_composite *mux,
  38. struct regmap *regmap)
  39. {
  40. struct mtk_clk_cpumux *cpumux;
  41. struct clk *clk;
  42. struct clk_init_data init;
  43. cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
  44. if (!cpumux)
  45. return ERR_PTR(-ENOMEM);
  46. init.name = mux->name;
  47. init.ops = &clk_cpumux_ops;
  48. init.parent_names = mux->parent_names;
  49. init.num_parents = mux->num_parents;
  50. init.flags = mux->flags;
  51. cpumux->reg = mux->mux_reg;
  52. cpumux->shift = mux->mux_shift;
  53. cpumux->mask = BIT(mux->mux_width) - 1;
  54. cpumux->regmap = regmap;
  55. cpumux->hw.init = &init;
  56. clk = clk_register(NULL, &cpumux->hw);
  57. if (IS_ERR(clk))
  58. kfree(cpumux);
  59. return clk;
  60. }
  61. int mtk_clk_register_cpumuxes(struct device_node *node,
  62. const struct mtk_composite *clks, int num,
  63. struct clk_onecell_data *clk_data)
  64. {
  65. int i;
  66. struct clk *clk;
  67. struct regmap *regmap;
  68. regmap = syscon_node_to_regmap(node);
  69. if (IS_ERR(regmap)) {
  70. pr_err("Cannot find regmap for %pOF: %ld\n", node,
  71. PTR_ERR(regmap));
  72. return PTR_ERR(regmap);
  73. }
  74. for (i = 0; i < num; i++) {
  75. const struct mtk_composite *mux = &clks[i];
  76. clk = mtk_clk_register_cpumux(mux, regmap);
  77. if (IS_ERR(clk)) {
  78. pr_err("Failed to register clk %s: %ld\n",
  79. mux->name, PTR_ERR(clk));
  80. continue;
  81. }
  82. clk_data->clks[mux->id] = clk;
  83. }
  84. return 0;
  85. }