aptina-pll.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Aptina Sensor PLL Configuration
  4. *
  5. * Copyright (C) 2012 Laurent Pinchart <[email protected]>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/gcd.h>
  9. #include <linux/kernel.h>
  10. #include <linux/lcm.h>
  11. #include <linux/module.h>
  12. #include "aptina-pll.h"
  13. int aptina_pll_calculate(struct device *dev,
  14. const struct aptina_pll_limits *limits,
  15. struct aptina_pll *pll)
  16. {
  17. unsigned int mf_min;
  18. unsigned int mf_max;
  19. unsigned int p1_min;
  20. unsigned int p1_max;
  21. unsigned int p1;
  22. unsigned int div;
  23. dev_dbg(dev, "PLL: ext clock %u pix clock %u\n",
  24. pll->ext_clock, pll->pix_clock);
  25. if (pll->ext_clock < limits->ext_clock_min ||
  26. pll->ext_clock > limits->ext_clock_max) {
  27. dev_err(dev, "pll: invalid external clock frequency.\n");
  28. return -EINVAL;
  29. }
  30. if (pll->pix_clock == 0 || pll->pix_clock > limits->pix_clock_max) {
  31. dev_err(dev, "pll: invalid pixel clock frequency.\n");
  32. return -EINVAL;
  33. }
  34. /* Compute the multiplier M and combined N*P1 divisor. */
  35. div = gcd(pll->pix_clock, pll->ext_clock);
  36. pll->m = pll->pix_clock / div;
  37. div = pll->ext_clock / div;
  38. /* We now have the smallest M and N*P1 values that will result in the
  39. * desired pixel clock frequency, but they might be out of the valid
  40. * range. Compute the factor by which we should multiply them given the
  41. * following constraints:
  42. *
  43. * - minimum/maximum multiplier
  44. * - minimum/maximum multiplier output clock frequency assuming the
  45. * minimum/maximum N value
  46. * - minimum/maximum combined N*P1 divisor
  47. */
  48. mf_min = DIV_ROUND_UP(limits->m_min, pll->m);
  49. mf_min = max(mf_min, limits->out_clock_min /
  50. (pll->ext_clock / limits->n_min * pll->m));
  51. mf_min = max(mf_min, limits->n_min * limits->p1_min / div);
  52. mf_max = limits->m_max / pll->m;
  53. mf_max = min(mf_max, limits->out_clock_max /
  54. (pll->ext_clock / limits->n_max * pll->m));
  55. mf_max = min(mf_max, DIV_ROUND_UP(limits->n_max * limits->p1_max, div));
  56. dev_dbg(dev, "pll: mf min %u max %u\n", mf_min, mf_max);
  57. if (mf_min > mf_max) {
  58. dev_err(dev, "pll: no valid combined N*P1 divisor.\n");
  59. return -EINVAL;
  60. }
  61. /*
  62. * We're looking for the highest acceptable P1 value for which a
  63. * multiplier factor MF exists that fulfills the following conditions:
  64. *
  65. * 1. p1 is in the [p1_min, p1_max] range given by the limits and is
  66. * even
  67. * 2. mf is in the [mf_min, mf_max] range computed above
  68. * 3. div * mf is a multiple of p1, in order to compute
  69. * n = div * mf / p1
  70. * m = pll->m * mf
  71. * 4. the internal clock frequency, given by ext_clock / n, is in the
  72. * [int_clock_min, int_clock_max] range given by the limits
  73. * 5. the output clock frequency, given by ext_clock / n * m, is in the
  74. * [out_clock_min, out_clock_max] range given by the limits
  75. *
  76. * The first naive approach is to iterate over all p1 values acceptable
  77. * according to (1) and all mf values acceptable according to (2), and
  78. * stop at the first combination that fulfills (3), (4) and (5). This
  79. * has a O(n^2) complexity.
  80. *
  81. * Instead of iterating over all mf values in the [mf_min, mf_max] range
  82. * we can compute the mf increment between two acceptable values
  83. * according to (3) with
  84. *
  85. * mf_inc = p1 / gcd(div, p1) (6)
  86. *
  87. * and round the minimum up to the nearest multiple of mf_inc. This will
  88. * restrict the number of mf values to be checked.
  89. *
  90. * Furthermore, conditions (4) and (5) only restrict the range of
  91. * acceptable p1 and mf values by modifying the minimum and maximum
  92. * limits. (5) can be expressed as
  93. *
  94. * ext_clock / (div * mf / p1) * m * mf >= out_clock_min
  95. * ext_clock / (div * mf / p1) * m * mf <= out_clock_max
  96. *
  97. * or
  98. *
  99. * p1 >= out_clock_min * div / (ext_clock * m) (7)
  100. * p1 <= out_clock_max * div / (ext_clock * m)
  101. *
  102. * Similarly, (4) can be expressed as
  103. *
  104. * mf >= ext_clock * p1 / (int_clock_max * div) (8)
  105. * mf <= ext_clock * p1 / (int_clock_min * div)
  106. *
  107. * We can thus iterate over the restricted p1 range defined by the
  108. * combination of (1) and (7), and then compute the restricted mf range
  109. * defined by the combination of (2), (6) and (8). If the resulting mf
  110. * range is not empty, any value in the mf range is acceptable. We thus
  111. * select the mf lwoer bound and the corresponding p1 value.
  112. */
  113. if (limits->p1_min == 0) {
  114. dev_err(dev, "pll: P1 minimum value must be >0.\n");
  115. return -EINVAL;
  116. }
  117. p1_min = max(limits->p1_min, DIV_ROUND_UP(limits->out_clock_min * div,
  118. pll->ext_clock * pll->m));
  119. p1_max = min(limits->p1_max, limits->out_clock_max * div /
  120. (pll->ext_clock * pll->m));
  121. for (p1 = p1_max & ~1; p1 >= p1_min; p1 -= 2) {
  122. unsigned int mf_inc = p1 / gcd(div, p1);
  123. unsigned int mf_high;
  124. unsigned int mf_low;
  125. mf_low = roundup(max(mf_min, DIV_ROUND_UP(pll->ext_clock * p1,
  126. limits->int_clock_max * div)), mf_inc);
  127. mf_high = min(mf_max, pll->ext_clock * p1 /
  128. (limits->int_clock_min * div));
  129. if (mf_low > mf_high)
  130. continue;
  131. pll->n = div * mf_low / p1;
  132. pll->m *= mf_low;
  133. pll->p1 = p1;
  134. dev_dbg(dev, "PLL: N %u M %u P1 %u\n", pll->n, pll->m, pll->p1);
  135. return 0;
  136. }
  137. dev_err(dev, "pll: no valid N and P1 divisors found.\n");
  138. return -EINVAL;
  139. }
  140. EXPORT_SYMBOL_GPL(aptina_pll_calculate);
  141. MODULE_DESCRIPTION("Aptina PLL Helpers");
  142. MODULE_AUTHOR("Laurent Pinchart <[email protected]>");
  143. MODULE_LICENSE("GPL v2");