clk-cpumux.c 2.7 KB

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