aptina-pll.c 5.2 KB

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