0253-clk-bcm2835-Support-for-clock-parent-selection.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. From 8578eeecb147439b7286e14cd0ce8a5078851f56 Mon Sep 17 00:00:00 2001
  2. From: Remi Pommarel <repk@triplefau.lt>
  3. Date: Sun, 6 Dec 2015 17:22:47 +0100
  4. Subject: [PATCH 253/381] clk: bcm2835: Support for clock parent selection
  5. Some bcm2835 clocks used by hardware (like "PWM" or "H264") can have multiple
  6. parent clocks. These clocks divide the rate of a parent which can be selected by
  7. setting the proper bits in the clock control register.
  8. Previously all these parents where handled by a mux clock. But a mux clock
  9. cannot be used because updating clock control register to select parent needs a
  10. password to be xor'd with the parent index.
  11. This patch get rid of mux clock and make these clocks handle their own parent,
  12. allowing them to select the one to use.
  13. Signed-off-by: Remi Pommarel <repk@triplefau.lt>
  14. Reviewed-by: Eric Anholt <eric@anholt.net>
  15. Signed-off-by: Michael Turquette <mturquette@baylibre.com>
  16. (cherry picked from commit 6d18b8adbe679b5947aa822b676efff230acc5f6)
  17. ---
  18. drivers/clk/bcm/clk-bcm2835.c | 122 ++++++++++++++++++++++++++----------------
  19. 1 file changed, 77 insertions(+), 45 deletions(-)
  20. --- a/drivers/clk/bcm/clk-bcm2835.c
  21. +++ b/drivers/clk/bcm/clk-bcm2835.c
  22. @@ -1216,16 +1216,6 @@ static long bcm2835_clock_rate_from_divi
  23. return temp;
  24. }
  25. -static long bcm2835_clock_round_rate(struct clk_hw *hw,
  26. - unsigned long rate,
  27. - unsigned long *parent_rate)
  28. -{
  29. - struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  30. - u32 div = bcm2835_clock_choose_div(hw, rate, *parent_rate, false);
  31. -
  32. - return bcm2835_clock_rate_from_divisor(clock, *parent_rate, div);
  33. -}
  34. -
  35. static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
  36. unsigned long parent_rate)
  37. {
  38. @@ -1297,13 +1287,75 @@ static int bcm2835_clock_set_rate(struct
  39. return 0;
  40. }
  41. +static int bcm2835_clock_determine_rate(struct clk_hw *hw,
  42. + struct clk_rate_request *req)
  43. +{
  44. + struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  45. + struct clk_hw *parent, *best_parent = NULL;
  46. + unsigned long rate, best_rate = 0;
  47. + unsigned long prate, best_prate = 0;
  48. + size_t i;
  49. + u32 div;
  50. +
  51. + /*
  52. + * Select parent clock that results in the closest but lower rate
  53. + */
  54. + for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
  55. + parent = clk_hw_get_parent_by_index(hw, i);
  56. + if (!parent)
  57. + continue;
  58. + prate = clk_hw_get_rate(parent);
  59. + div = bcm2835_clock_choose_div(hw, req->rate, prate, true);
  60. + rate = bcm2835_clock_rate_from_divisor(clock, prate, div);
  61. + if (rate > best_rate && rate <= req->rate) {
  62. + best_parent = parent;
  63. + best_prate = prate;
  64. + best_rate = rate;
  65. + }
  66. + }
  67. +
  68. + if (!best_parent)
  69. + return -EINVAL;
  70. +
  71. + req->best_parent_hw = best_parent;
  72. + req->best_parent_rate = best_prate;
  73. +
  74. + req->rate = best_rate;
  75. +
  76. + return 0;
  77. +}
  78. +
  79. +static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
  80. +{
  81. + struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  82. + struct bcm2835_cprman *cprman = clock->cprman;
  83. + const struct bcm2835_clock_data *data = clock->data;
  84. + u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
  85. +
  86. + cprman_write(cprman, data->ctl_reg, src);
  87. + return 0;
  88. +}
  89. +
  90. +static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
  91. +{
  92. + struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  93. + struct bcm2835_cprman *cprman = clock->cprman;
  94. + const struct bcm2835_clock_data *data = clock->data;
  95. + u32 src = cprman_read(cprman, data->ctl_reg);
  96. +
  97. + return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
  98. +}
  99. +
  100. +
  101. static const struct clk_ops bcm2835_clock_clk_ops = {
  102. .is_prepared = bcm2835_clock_is_on,
  103. .prepare = bcm2835_clock_on,
  104. .unprepare = bcm2835_clock_off,
  105. .recalc_rate = bcm2835_clock_get_rate,
  106. .set_rate = bcm2835_clock_set_rate,
  107. - .round_rate = bcm2835_clock_round_rate,
  108. + .determine_rate = bcm2835_clock_determine_rate,
  109. + .set_parent = bcm2835_clock_set_parent,
  110. + .get_parent = bcm2835_clock_get_parent,
  111. };
  112. static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
  113. @@ -1319,7 +1371,9 @@ static const struct clk_ops bcm2835_vpu_
  114. .is_prepared = bcm2835_vpu_clock_is_on,
  115. .recalc_rate = bcm2835_clock_get_rate,
  116. .set_rate = bcm2835_clock_set_rate,
  117. - .round_rate = bcm2835_clock_round_rate,
  118. + .determine_rate = bcm2835_clock_determine_rate,
  119. + .set_parent = bcm2835_clock_set_parent,
  120. + .get_parent = bcm2835_clock_get_parent,
  121. };
  122. static struct clk *bcm2835_register_pll(struct bcm2835_cprman *cprman,
  123. @@ -1413,45 +1467,23 @@ static struct clk *bcm2835_register_cloc
  124. {
  125. struct bcm2835_clock *clock;
  126. struct clk_init_data init;
  127. - const char *parent;
  128. + const char *parents[1 << CM_SRC_BITS];
  129. + size_t i;
  130. /*
  131. - * Most of the clock generators have a mux field, so we
  132. - * instantiate a generic mux as our parent to handle it.
  133. + * Replace our "xosc" references with the oscillator's
  134. + * actual name.
  135. */
  136. - if (data->num_mux_parents) {
  137. - const char *parents[1 << CM_SRC_BITS];
  138. - int i;
  139. -
  140. - parent = devm_kasprintf(cprman->dev, GFP_KERNEL,
  141. - "mux_%s", data->name);
  142. - if (!parent)
  143. - return NULL;
  144. -
  145. - /*
  146. - * Replace our "xosc" references with the oscillator's
  147. - * actual name.
  148. - */
  149. - for (i = 0; i < data->num_mux_parents; i++) {
  150. - if (strcmp(data->parents[i], "xosc") == 0)
  151. - parents[i] = cprman->osc_name;
  152. - else
  153. - parents[i] = data->parents[i];
  154. - }
  155. -
  156. - clk_register_mux(cprman->dev, parent,
  157. - parents, data->num_mux_parents,
  158. - CLK_SET_RATE_PARENT,
  159. - cprman->regs + data->ctl_reg,
  160. - CM_SRC_SHIFT, CM_SRC_BITS,
  161. - 0, &cprman->regs_lock);
  162. - } else {
  163. - parent = data->parents[0];
  164. + for (i = 0; i < data->num_mux_parents; i++) {
  165. + if (strcmp(data->parents[i], "xosc") == 0)
  166. + parents[i] = cprman->osc_name;
  167. + else
  168. + parents[i] = data->parents[i];
  169. }
  170. memset(&init, 0, sizeof(init));
  171. - init.parent_names = &parent;
  172. - init.num_parents = 1;
  173. + init.parent_names = parents;
  174. + init.num_parents = data->num_mux_parents;
  175. init.name = data->name;
  176. init.flags = CLK_IGNORE_UNUSED;