clk-mux-zynqmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Zynq UltraScale+ MPSoC mux
  4. *
  5. * Copyright (C) 2016-2018 Xilinx
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/slab.h>
  9. #include "clk-zynqmp.h"
  10. /*
  11. * DOC: basic adjustable multiplexer clock that cannot gate
  12. *
  13. * Traits of this clock:
  14. * prepare - clk_prepare only ensures that parents are prepared
  15. * enable - clk_enable only ensures that parents are enabled
  16. * rate - rate is only affected by parent switching. No clk_set_rate support
  17. * parent - parent is adjustable through clk_set_parent
  18. */
  19. /**
  20. * struct zynqmp_clk_mux - multiplexer clock
  21. *
  22. * @hw: handle between common and hardware-specific interfaces
  23. * @flags: hardware-specific flags
  24. * @clk_id: Id of clock
  25. */
  26. struct zynqmp_clk_mux {
  27. struct clk_hw hw;
  28. u8 flags;
  29. u32 clk_id;
  30. };
  31. #define to_zynqmp_clk_mux(_hw) container_of(_hw, struct zynqmp_clk_mux, hw)
  32. /**
  33. * zynqmp_clk_mux_get_parent() - Get parent of clock
  34. * @hw: handle between common and hardware-specific interfaces
  35. *
  36. * Return: Parent index
  37. */
  38. static u8 zynqmp_clk_mux_get_parent(struct clk_hw *hw)
  39. {
  40. struct zynqmp_clk_mux *mux = to_zynqmp_clk_mux(hw);
  41. const char *clk_name = clk_hw_get_name(hw);
  42. u32 clk_id = mux->clk_id;
  43. u32 val;
  44. int ret;
  45. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  46. ret = eemi_ops->clock_getparent(clk_id, &val);
  47. if (ret)
  48. pr_warn_once("%s() getparent failed for clock: %s, ret = %d\n",
  49. __func__, clk_name, ret);
  50. return val;
  51. }
  52. /**
  53. * zynqmp_clk_mux_set_parent() - Set parent of clock
  54. * @hw: handle between common and hardware-specific interfaces
  55. * @index: Parent index
  56. *
  57. * Return: 0 on success else error+reason
  58. */
  59. static int zynqmp_clk_mux_set_parent(struct clk_hw *hw, u8 index)
  60. {
  61. struct zynqmp_clk_mux *mux = to_zynqmp_clk_mux(hw);
  62. const char *clk_name = clk_hw_get_name(hw);
  63. u32 clk_id = mux->clk_id;
  64. int ret;
  65. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  66. ret = eemi_ops->clock_setparent(clk_id, index);
  67. if (ret)
  68. pr_warn_once("%s() set parent failed for clock: %s, ret = %d\n",
  69. __func__, clk_name, ret);
  70. return ret;
  71. }
  72. static const struct clk_ops zynqmp_clk_mux_ops = {
  73. .get_parent = zynqmp_clk_mux_get_parent,
  74. .set_parent = zynqmp_clk_mux_set_parent,
  75. .determine_rate = __clk_mux_determine_rate,
  76. };
  77. static const struct clk_ops zynqmp_clk_mux_ro_ops = {
  78. .get_parent = zynqmp_clk_mux_get_parent,
  79. };
  80. /**
  81. * zynqmp_clk_register_mux() - Register a mux table with the clock
  82. * framework
  83. * @name: Name of this clock
  84. * @clk_id: Id of this clock
  85. * @parents: Name of this clock's parents
  86. * @num_parents: Number of parents
  87. * @nodes: Clock topology node
  88. *
  89. * Return: clock hardware of the registered clock mux
  90. */
  91. struct clk_hw *zynqmp_clk_register_mux(const char *name, u32 clk_id,
  92. const char * const *parents,
  93. u8 num_parents,
  94. const struct clock_topology *nodes)
  95. {
  96. struct zynqmp_clk_mux *mux;
  97. struct clk_hw *hw;
  98. struct clk_init_data init;
  99. int ret;
  100. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  101. if (!mux)
  102. return ERR_PTR(-ENOMEM);
  103. init.name = name;
  104. if (nodes->type_flag & CLK_MUX_READ_ONLY)
  105. init.ops = &zynqmp_clk_mux_ro_ops;
  106. else
  107. init.ops = &zynqmp_clk_mux_ops;
  108. init.flags = nodes->flag;
  109. init.parent_names = parents;
  110. init.num_parents = num_parents;
  111. mux->flags = nodes->type_flag;
  112. mux->hw.init = &init;
  113. mux->clk_id = clk_id;
  114. hw = &mux->hw;
  115. ret = clk_hw_register(NULL, hw);
  116. if (ret) {
  117. kfree(hw);
  118. hw = ERR_PTR(ret);
  119. }
  120. return hw;
  121. }
  122. EXPORT_SYMBOL_GPL(zynqmp_clk_register_mux);