clk-audio-divider.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2017 AmLogic, Inc.
  3. * Author: Jerome Brunet <jbrunet@baylibre.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * i2s master clock divider: The algorithm of the generic clk-divider used with
  19. * a very precise clock parent such as the mpll tends to select a low divider
  20. * factor. This gives poor results with this particular divider, especially with
  21. * high frequencies (> 100 MHz)
  22. *
  23. * This driver try to select the maximum possible divider with the rate the
  24. * upstream clock can provide.
  25. */
  26. #include <linux/clk-provider.h>
  27. #include "clkc.h"
  28. static inline struct meson_clk_audio_div_data *
  29. meson_clk_audio_div_data(struct clk_regmap *clk)
  30. {
  31. return (struct meson_clk_audio_div_data *)clk->data;
  32. }
  33. static int _div_round(unsigned long parent_rate, unsigned long rate,
  34. unsigned long flags)
  35. {
  36. if (flags & CLK_DIVIDER_ROUND_CLOSEST)
  37. return DIV_ROUND_CLOSEST_ULL((u64)parent_rate, rate);
  38. return DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  39. }
  40. static int _get_val(unsigned long parent_rate, unsigned long rate)
  41. {
  42. return DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
  43. }
  44. static int _valid_divider(unsigned int width, int divider)
  45. {
  46. int max_divider = 1 << width;
  47. return clamp(divider, 1, max_divider);
  48. }
  49. static unsigned long audio_divider_recalc_rate(struct clk_hw *hw,
  50. unsigned long parent_rate)
  51. {
  52. struct clk_regmap *clk = to_clk_regmap(hw);
  53. struct meson_clk_audio_div_data *adiv = meson_clk_audio_div_data(clk);
  54. unsigned long divider;
  55. divider = meson_parm_read(clk->map, &adiv->div);
  56. return DIV_ROUND_UP_ULL((u64)parent_rate, divider);
  57. }
  58. static long audio_divider_round_rate(struct clk_hw *hw,
  59. unsigned long rate,
  60. unsigned long *parent_rate)
  61. {
  62. struct clk_regmap *clk = to_clk_regmap(hw);
  63. struct meson_clk_audio_div_data *adiv = meson_clk_audio_div_data(clk);
  64. unsigned long max_prate;
  65. int divider;
  66. if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
  67. divider = _div_round(*parent_rate, rate, adiv->flags);
  68. divider = _valid_divider(adiv->div.width, divider);
  69. return DIV_ROUND_UP_ULL((u64)*parent_rate, divider);
  70. }
  71. /* Get the maximum parent rate */
  72. max_prate = clk_hw_round_rate(clk_hw_get_parent(hw), ULONG_MAX);
  73. /* Get the corresponding rounded down divider */
  74. divider = max_prate / rate;
  75. divider = _valid_divider(adiv->div.width, divider);
  76. /* Get actual rate of the parent */
  77. *parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw),
  78. divider * rate);
  79. return DIV_ROUND_UP_ULL((u64)*parent_rate, divider);
  80. }
  81. static int audio_divider_set_rate(struct clk_hw *hw,
  82. unsigned long rate,
  83. unsigned long parent_rate)
  84. {
  85. struct clk_regmap *clk = to_clk_regmap(hw);
  86. struct meson_clk_audio_div_data *adiv = meson_clk_audio_div_data(clk);
  87. int val = _get_val(parent_rate, rate);
  88. meson_parm_write(clk->map, &adiv->div, val);
  89. return 0;
  90. }
  91. const struct clk_ops meson_clk_audio_divider_ro_ops = {
  92. .recalc_rate = audio_divider_recalc_rate,
  93. .round_rate = audio_divider_round_rate,
  94. };
  95. const struct clk_ops meson_clk_audio_divider_ops = {
  96. .recalc_rate = audio_divider_recalc_rate,
  97. .round_rate = audio_divider_round_rate,
  98. .set_rate = audio_divider_set_rate,
  99. };