ccu_nm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. m = reg >> nm->m.shift;
  66. m &= (1 << nm->m.width) - 1;
  67. return parent_rate * (n + 1) / (m + 1);
  68. }
  69. static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
  70. unsigned long *parent_rate)
  71. {
  72. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  73. struct _ccu_nm _nm;
  74. _nm.min_n = nm->n.min;
  75. _nm.max_n = 1 << nm->n.width;
  76. _nm.min_m = 1;
  77. _nm.max_m = nm->m.max ?: 1 << nm->m.width;
  78. ccu_nm_find_best(*parent_rate, rate, &_nm);
  79. return *parent_rate * _nm.n / _nm.m;
  80. }
  81. static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
  82. unsigned long parent_rate)
  83. {
  84. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  85. struct _ccu_nm _nm;
  86. unsigned long flags;
  87. u32 reg;
  88. if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
  89. return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
  90. else
  91. ccu_frac_helper_disable(&nm->common, &nm->frac);
  92. _nm.min_n = 1;
  93. _nm.max_n = 1 << nm->n.width;
  94. _nm.min_m = 1;
  95. _nm.max_m = nm->m.max ?: 1 << nm->m.width;
  96. ccu_nm_find_best(parent_rate, rate, &_nm);
  97. spin_lock_irqsave(nm->common.lock, flags);
  98. reg = readl(nm->common.base + nm->common.reg);
  99. reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
  100. reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
  101. writel(reg | ((_nm.m - 1) << nm->m.shift) | ((_nm.n - 1) << nm->n.shift),
  102. nm->common.base + nm->common.reg);
  103. spin_unlock_irqrestore(nm->common.lock, flags);
  104. ccu_helper_wait_for_lock(&nm->common, nm->lock);
  105. return 0;
  106. }
  107. const struct clk_ops ccu_nm_ops = {
  108. .disable = ccu_nm_disable,
  109. .enable = ccu_nm_enable,
  110. .is_enabled = ccu_nm_is_enabled,
  111. .recalc_rate = ccu_nm_recalc_rate,
  112. .round_rate = ccu_nm_round_rate,
  113. .set_rate = ccu_nm_set_rate,
  114. };