clk-mux-zynqmp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 on success or number of parents in case of error
  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. ret = zynqmp_pm_clock_getparent(clk_id, &val);
  46. if (ret) {
  47. pr_debug("%s() getparent failed for clock: %s, ret = %d\n",
  48. __func__, clk_name, ret);
  49. /*
  50. * clk_core_get_parent_by_index() takes num_parents as incorrect
  51. * index which is exactly what I want to return here
  52. */
  53. return clk_hw_get_num_parents(hw);
  54. }
  55. return val;
  56. }
  57. /**
  58. * zynqmp_clk_mux_set_parent() - Set parent of clock
  59. * @hw: handle between common and hardware-specific interfaces
  60. * @index: Parent index
  61. *
  62. * Return: 0 on success else error+reason
  63. */
  64. static int zynqmp_clk_mux_set_parent(struct clk_hw *hw, u8 index)
  65. {
  66. struct zynqmp_clk_mux *mux = to_zynqmp_clk_mux(hw);
  67. const char *clk_name = clk_hw_get_name(hw);
  68. u32 clk_id = mux->clk_id;
  69. int ret;
  70. ret = zynqmp_pm_clock_setparent(clk_id, index);
  71. if (ret)
  72. pr_debug("%s() set parent failed for clock: %s, ret = %d\n",
  73. __func__, clk_name, ret);
  74. return ret;
  75. }
  76. static const struct clk_ops zynqmp_clk_mux_ops = {
  77. .get_parent = zynqmp_clk_mux_get_parent,
  78. .set_parent = zynqmp_clk_mux_set_parent,
  79. .determine_rate = __clk_mux_determine_rate,
  80. };
  81. static const struct clk_ops zynqmp_clk_mux_ro_ops = {
  82. .get_parent = zynqmp_clk_mux_get_parent,
  83. };
  84. static inline unsigned long zynqmp_clk_map_mux_ccf_flags(
  85. const u32 zynqmp_type_flag)
  86. {
  87. unsigned long ccf_flag = 0;
  88. if (zynqmp_type_flag & ZYNQMP_CLK_MUX_INDEX_ONE)
  89. ccf_flag |= CLK_MUX_INDEX_ONE;
  90. if (zynqmp_type_flag & ZYNQMP_CLK_MUX_INDEX_BIT)
  91. ccf_flag |= CLK_MUX_INDEX_BIT;
  92. if (zynqmp_type_flag & ZYNQMP_CLK_MUX_HIWORD_MASK)
  93. ccf_flag |= CLK_MUX_HIWORD_MASK;
  94. if (zynqmp_type_flag & ZYNQMP_CLK_MUX_READ_ONLY)
  95. ccf_flag |= CLK_MUX_READ_ONLY;
  96. if (zynqmp_type_flag & ZYNQMP_CLK_MUX_ROUND_CLOSEST)
  97. ccf_flag |= CLK_MUX_ROUND_CLOSEST;
  98. if (zynqmp_type_flag & ZYNQMP_CLK_MUX_BIG_ENDIAN)
  99. ccf_flag |= CLK_MUX_BIG_ENDIAN;
  100. return ccf_flag;
  101. }
  102. /**
  103. * zynqmp_clk_register_mux() - Register a mux table with the clock
  104. * framework
  105. * @name: Name of this clock
  106. * @clk_id: Id of this clock
  107. * @parents: Name of this clock's parents
  108. * @num_parents: Number of parents
  109. * @nodes: Clock topology node
  110. *
  111. * Return: clock hardware of the registered clock mux
  112. */
  113. struct clk_hw *zynqmp_clk_register_mux(const char *name, u32 clk_id,
  114. const char * const *parents,
  115. u8 num_parents,
  116. const struct clock_topology *nodes)
  117. {
  118. struct zynqmp_clk_mux *mux;
  119. struct clk_hw *hw;
  120. struct clk_init_data init;
  121. int ret;
  122. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  123. if (!mux)
  124. return ERR_PTR(-ENOMEM);
  125. init.name = name;
  126. if (nodes->type_flag & CLK_MUX_READ_ONLY)
  127. init.ops = &zynqmp_clk_mux_ro_ops;
  128. else
  129. init.ops = &zynqmp_clk_mux_ops;
  130. init.flags = zynqmp_clk_map_common_ccf_flags(nodes->flag);
  131. init.parent_names = parents;
  132. init.num_parents = num_parents;
  133. mux->flags = zynqmp_clk_map_mux_ccf_flags(nodes->type_flag);
  134. mux->hw.init = &init;
  135. mux->clk_id = clk_id;
  136. hw = &mux->hw;
  137. ret = clk_hw_register(NULL, hw);
  138. if (ret) {
  139. kfree(mux);
  140. hw = ERR_PTR(ret);
  141. }
  142. return hw;
  143. }