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