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