clk-cpumux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2015 Linaro Ltd.
  3. * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk-provider.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/slab.h>
  17. #include "clk-mtk.h"
  18. #include "clk-cpumux.h"
  19. #define WORKAROUND_318_WARNING 1
  20. struct mtk_clk_cpumux {
  21. struct clk_hw hw;
  22. struct regmap *regmap;
  23. u32 reg;
  24. u32 mask;
  25. u8 shift;
  26. };
  27. static inline struct mtk_clk_cpumux *to_clk_mux(struct clk_hw *_hw)
  28. {
  29. return container_of(_hw, struct mtk_clk_cpumux, hw);
  30. }
  31. static u8 clk_cpumux_get_parent(struct clk_hw *hw)
  32. {
  33. struct mtk_clk_cpumux *mux = to_clk_mux(hw);
  34. int num_parents = __clk_get_num_parents(hw->clk);
  35. unsigned int val;
  36. regmap_read(mux->regmap, mux->reg, &val);
  37. val >>= mux->shift;
  38. val &= mux->mask;
  39. if (val >= num_parents)
  40. return -EINVAL;
  41. return val;
  42. }
  43. static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index)
  44. {
  45. struct mtk_clk_cpumux *mux = to_clk_mux(hw);
  46. u32 mask, val;
  47. val = index << mux->shift;
  48. mask = mux->mask << mux->shift;
  49. return regmap_update_bits(mux->regmap, mux->reg, mask, val);
  50. }
  51. static const struct clk_ops clk_cpumux_ops = {
  52. .get_parent = clk_cpumux_get_parent,
  53. .set_parent = clk_cpumux_set_parent,
  54. };
  55. static struct clk __init *mtk_clk_register_cpumux(const struct mtk_composite *mux,
  56. struct regmap *regmap)
  57. {
  58. struct mtk_clk_cpumux *cpumux;
  59. struct clk *clk;
  60. struct clk_init_data init;
  61. cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
  62. if (!cpumux)
  63. return ERR_PTR(-ENOMEM);
  64. init.name = mux->name;
  65. init.ops = &clk_cpumux_ops;
  66. #if WORKAROUND_318_WARNING
  67. init.parent_names = (const char **)mux->parent_names;
  68. #else
  69. init.parent_names = mux->parent_names;
  70. #endif
  71. init.num_parents = mux->num_parents;
  72. init.flags = mux->flags;
  73. cpumux->reg = mux->mux_reg;
  74. cpumux->shift = mux->mux_shift;
  75. cpumux->mask = BIT(mux->mux_width) - 1;
  76. cpumux->regmap = regmap;
  77. cpumux->hw.init = &init;
  78. clk = clk_register(NULL, &cpumux->hw);
  79. if (IS_ERR(clk))
  80. kfree(cpumux);
  81. return clk;
  82. }
  83. int __init mtk_clk_register_cpumuxes(struct device_node *node,
  84. const struct mtk_composite *clks, int num,
  85. struct clk_onecell_data *clk_data)
  86. {
  87. int i;
  88. struct clk *clk;
  89. struct regmap *regmap;
  90. regmap = syscon_node_to_regmap(node);
  91. if (IS_ERR(regmap)) {
  92. pr_err("Cannot find regmap for %s: %ld\n", node->full_name,
  93. PTR_ERR(regmap));
  94. return PTR_ERR(regmap);
  95. }
  96. for (i = 0; i < num; i++) {
  97. const struct mtk_composite *mux = &clks[i];
  98. clk = mtk_clk_register_cpumux(mux, regmap);
  99. if (IS_ERR(clk)) {
  100. pr_err("Failed to register clk %s: %ld\n",
  101. mux->name, PTR_ERR(clk));
  102. continue;
  103. }
  104. clk_data->clks[mux->id] = clk;
  105. }
  106. return 0;
  107. }