ccu_mux.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2016 Maxime Ripard
  3. * Maxime Ripard <maxime.ripard@free-electrons.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include "ccu_gate.h"
  12. #include "ccu_mux.h"
  13. void ccu_mux_helper_adjust_parent_for_prediv(struct ccu_common *common,
  14. struct ccu_mux_internal *cm,
  15. int parent_index,
  16. unsigned long *parent_rate)
  17. {
  18. u8 prediv = 1;
  19. u32 reg;
  20. if (!((common->features & CCU_FEATURE_FIXED_PREDIV) ||
  21. (common->features & CCU_FEATURE_VARIABLE_PREDIV)))
  22. return;
  23. reg = readl(common->base + common->reg);
  24. if (parent_index < 0) {
  25. parent_index = reg >> cm->shift;
  26. parent_index &= (1 << cm->width) - 1;
  27. }
  28. if (common->features & CCU_FEATURE_FIXED_PREDIV)
  29. if (parent_index == cm->fixed_prediv.index)
  30. prediv = cm->fixed_prediv.div;
  31. if (common->features & CCU_FEATURE_VARIABLE_PREDIV)
  32. if (parent_index == cm->variable_prediv.index) {
  33. u8 div;
  34. div = reg >> cm->variable_prediv.shift;
  35. div &= (1 << cm->variable_prediv.width) - 1;
  36. prediv = div + 1;
  37. }
  38. *parent_rate = *parent_rate / prediv;
  39. }
  40. int ccu_mux_helper_determine_rate(struct ccu_common *common,
  41. struct ccu_mux_internal *cm,
  42. struct clk_rate_request *req,
  43. unsigned long (*round)(struct ccu_mux_internal *,
  44. unsigned long,
  45. unsigned long,
  46. void *),
  47. void *data)
  48. {
  49. unsigned long best_parent_rate = 0, best_rate = 0;
  50. struct clk_hw *best_parent, *hw = &common->hw;
  51. unsigned int i;
  52. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  53. unsigned long tmp_rate, parent_rate;
  54. struct clk_hw *parent;
  55. parent = clk_hw_get_parent_by_index(hw, i);
  56. if (!parent)
  57. continue;
  58. parent_rate = clk_hw_get_rate(parent);
  59. ccu_mux_helper_adjust_parent_for_prediv(common, cm, i,
  60. &parent_rate);
  61. tmp_rate = round(cm, clk_hw_get_rate(parent), req->rate, data);
  62. if (tmp_rate == req->rate) {
  63. best_parent = parent;
  64. best_parent_rate = parent_rate;
  65. best_rate = tmp_rate;
  66. goto out;
  67. }
  68. if ((req->rate - tmp_rate) < (req->rate - best_rate)) {
  69. best_rate = tmp_rate;
  70. best_parent_rate = parent_rate;
  71. best_parent = parent;
  72. }
  73. }
  74. if (best_rate == 0)
  75. return -EINVAL;
  76. out:
  77. req->best_parent_hw = best_parent;
  78. req->best_parent_rate = best_parent_rate;
  79. req->rate = best_rate;
  80. return 0;
  81. }
  82. u8 ccu_mux_helper_get_parent(struct ccu_common *common,
  83. struct ccu_mux_internal *cm)
  84. {
  85. u32 reg;
  86. u8 parent;
  87. reg = readl(common->base + common->reg);
  88. parent = reg >> cm->shift;
  89. parent &= (1 << cm->width) - 1;
  90. return parent;
  91. }
  92. int ccu_mux_helper_set_parent(struct ccu_common *common,
  93. struct ccu_mux_internal *cm,
  94. u8 index)
  95. {
  96. unsigned long flags;
  97. u32 reg;
  98. spin_lock_irqsave(common->lock, flags);
  99. reg = readl(common->base + common->reg);
  100. reg &= ~GENMASK(cm->width + cm->shift - 1, cm->shift);
  101. writel(reg | (index << cm->shift), common->base + common->reg);
  102. spin_unlock_irqrestore(common->lock, flags);
  103. return 0;
  104. }
  105. static void ccu_mux_disable(struct clk_hw *hw)
  106. {
  107. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  108. return ccu_gate_helper_disable(&cm->common, cm->enable);
  109. }
  110. static int ccu_mux_enable(struct clk_hw *hw)
  111. {
  112. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  113. return ccu_gate_helper_enable(&cm->common, cm->enable);
  114. }
  115. static int ccu_mux_is_enabled(struct clk_hw *hw)
  116. {
  117. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  118. return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
  119. }
  120. static u8 ccu_mux_get_parent(struct clk_hw *hw)
  121. {
  122. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  123. return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
  124. }
  125. static int ccu_mux_set_parent(struct clk_hw *hw, u8 index)
  126. {
  127. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  128. return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
  129. }
  130. static unsigned long ccu_mux_recalc_rate(struct clk_hw *hw,
  131. unsigned long parent_rate)
  132. {
  133. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  134. ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
  135. &parent_rate);
  136. return parent_rate;
  137. }
  138. const struct clk_ops ccu_mux_ops = {
  139. .disable = ccu_mux_disable,
  140. .enable = ccu_mux_enable,
  141. .is_enabled = ccu_mux_is_enabled,
  142. .get_parent = ccu_mux_get_parent,
  143. .set_parent = ccu_mux_set_parent,
  144. .determine_rate = __clk_mux_determine_rate,
  145. .recalc_rate = ccu_mux_recalc_rate,
  146. };