ccu_nm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_frac.h"
  12. #include "ccu_gate.h"
  13. #include "ccu_nm.h"
  14. struct _ccu_nm {
  15. unsigned long n, min_n, max_n;
  16. unsigned long m, min_m, max_m;
  17. };
  18. static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
  19. struct _ccu_nm *nm)
  20. {
  21. unsigned long best_rate = 0;
  22. unsigned long best_n = 0, best_m = 0;
  23. unsigned long _n, _m;
  24. for (_n = nm->min_n; _n <= nm->max_n; _n++) {
  25. for (_m = nm->min_m; _m <= nm->max_m; _m++) {
  26. unsigned long tmp_rate = parent * _n / _m;
  27. if (tmp_rate > rate)
  28. continue;
  29. if ((rate - tmp_rate) < (rate - best_rate)) {
  30. best_rate = tmp_rate;
  31. best_n = _n;
  32. best_m = _m;
  33. }
  34. }
  35. }
  36. nm->n = best_n;
  37. nm->m = best_m;
  38. }
  39. static void ccu_nm_disable(struct clk_hw *hw)
  40. {
  41. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  42. return ccu_gate_helper_disable(&nm->common, nm->enable);
  43. }
  44. static int ccu_nm_enable(struct clk_hw *hw)
  45. {
  46. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  47. return ccu_gate_helper_enable(&nm->common, nm->enable);
  48. }
  49. static int ccu_nm_is_enabled(struct clk_hw *hw)
  50. {
  51. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  52. return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
  53. }
  54. static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
  55. unsigned long parent_rate)
  56. {
  57. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  58. unsigned long n, m;
  59. u32 reg;
  60. if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac))
  61. return ccu_frac_helper_read_rate(&nm->common, &nm->frac);
  62. reg = readl(nm->common.base + nm->common.reg);
  63. n = reg >> nm->n.shift;
  64. n &= (1 << nm->n.width) - 1;
  65. n += nm->n.offset;
  66. if (!n)
  67. n++;
  68. m = reg >> nm->m.shift;
  69. m &= (1 << nm->m.width) - 1;
  70. m += nm->m.offset;
  71. if (!m)
  72. m++;
  73. return parent_rate * n / m;
  74. }
  75. static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
  76. unsigned long *parent_rate)
  77. {
  78. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  79. struct _ccu_nm _nm;
  80. _nm.min_n = nm->n.min ?: 1;
  81. _nm.max_n = nm->n.max ?: 1 << nm->n.width;
  82. _nm.min_m = 1;
  83. _nm.max_m = nm->m.max ?: 1 << nm->m.width;
  84. ccu_nm_find_best(*parent_rate, rate, &_nm);
  85. return *parent_rate * _nm.n / _nm.m;
  86. }
  87. static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
  88. unsigned long parent_rate)
  89. {
  90. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  91. struct _ccu_nm _nm;
  92. unsigned long flags;
  93. u32 reg;
  94. if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
  95. return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
  96. else
  97. ccu_frac_helper_disable(&nm->common, &nm->frac);
  98. _nm.min_n = nm->n.min ?: 1;
  99. _nm.max_n = nm->n.max ?: 1 << nm->n.width;
  100. _nm.min_m = 1;
  101. _nm.max_m = nm->m.max ?: 1 << nm->m.width;
  102. ccu_nm_find_best(parent_rate, rate, &_nm);
  103. spin_lock_irqsave(nm->common.lock, flags);
  104. reg = readl(nm->common.base + nm->common.reg);
  105. reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
  106. reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
  107. reg |= (_nm.n - nm->n.offset) << nm->n.shift;
  108. reg |= (_nm.m - nm->m.offset) << nm->m.shift;
  109. writel(reg, nm->common.base + nm->common.reg);
  110. spin_unlock_irqrestore(nm->common.lock, flags);
  111. ccu_helper_wait_for_lock(&nm->common, nm->lock);
  112. return 0;
  113. }
  114. const struct clk_ops ccu_nm_ops = {
  115. .disable = ccu_nm_disable,
  116. .enable = ccu_nm_enable,
  117. .is_enabled = ccu_nm_is_enabled,
  118. .recalc_rate = ccu_nm_recalc_rate,
  119. .round_rate = ccu_nm_round_rate,
  120. .set_rate = ccu_nm_set_rate,
  121. };