aptina-pll.c 5.9 KB

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