xref: /openbmc/linux/drivers/media/i2c/ccs-pll.c (revision 9ec6e5b18e6660ccc7b1777a4a4108c6c1723c40)
19e05bbacSSakari Ailus // SPDX-License-Identifier: GPL-2.0-only
29e05bbacSSakari Ailus /*
39e05bbacSSakari Ailus  * drivers/media/i2c/ccs-pll.c
49e05bbacSSakari Ailus  *
59e05bbacSSakari Ailus  * Generic MIPI CCS/SMIA/SMIA++ PLL calculator
69e05bbacSSakari Ailus  *
79e05bbacSSakari Ailus  * Copyright (C) 2020 Intel Corporation
89e05bbacSSakari Ailus  * Copyright (C) 2011--2012 Nokia Corporation
97389d01cSSakari Ailus  * Contact: Sakari Ailus <sakari.ailus@linux.intel.com>
109e05bbacSSakari Ailus  */
119e05bbacSSakari Ailus 
129e05bbacSSakari Ailus #include <linux/device.h>
139e05bbacSSakari Ailus #include <linux/gcd.h>
149e05bbacSSakari Ailus #include <linux/lcm.h>
159e05bbacSSakari Ailus #include <linux/module.h>
169e05bbacSSakari Ailus 
179e05bbacSSakari Ailus #include "ccs-pll.h"
189e05bbacSSakari Ailus 
199e05bbacSSakari Ailus /* Return an even number or one. */
209e05bbacSSakari Ailus static inline uint32_t clk_div_even(uint32_t a)
219e05bbacSSakari Ailus {
229e05bbacSSakari Ailus 	return max_t(uint32_t, 1, a & ~1);
239e05bbacSSakari Ailus }
249e05bbacSSakari Ailus 
259e05bbacSSakari Ailus /* Return an even number or one. */
269e05bbacSSakari Ailus static inline uint32_t clk_div_even_up(uint32_t a)
279e05bbacSSakari Ailus {
289e05bbacSSakari Ailus 	if (a == 1)
299e05bbacSSakari Ailus 		return 1;
309e05bbacSSakari Ailus 	return (a + 1) & ~1;
319e05bbacSSakari Ailus }
329e05bbacSSakari Ailus 
339e05bbacSSakari Ailus static inline uint32_t is_one_or_even(uint32_t a)
349e05bbacSSakari Ailus {
359e05bbacSSakari Ailus 	if (a == 1)
369e05bbacSSakari Ailus 		return 1;
379e05bbacSSakari Ailus 	if (a & 1)
389e05bbacSSakari Ailus 		return 0;
399e05bbacSSakari Ailus 
409e05bbacSSakari Ailus 	return 1;
419e05bbacSSakari Ailus }
429e05bbacSSakari Ailus 
43482e75e7SSakari Ailus static inline uint32_t one_or_more(uint32_t a)
44482e75e7SSakari Ailus {
45482e75e7SSakari Ailus 	return a ?: 1;
46482e75e7SSakari Ailus }
47482e75e7SSakari Ailus 
489e05bbacSSakari Ailus static int bounds_check(struct device *dev, uint32_t val,
49f25d3962SSakari Ailus 			uint32_t min, uint32_t max, const char *prefix,
50f25d3962SSakari Ailus 			char *str)
519e05bbacSSakari Ailus {
529e05bbacSSakari Ailus 	if (val >= min && val <= max)
539e05bbacSSakari Ailus 		return 0;
549e05bbacSSakari Ailus 
55f25d3962SSakari Ailus 	dev_dbg(dev, "%s_%s out of bounds: %d (%d--%d)\n", prefix,
56f25d3962SSakari Ailus 		str, val, min, max);
579e05bbacSSakari Ailus 
589e05bbacSSakari Ailus 	return -EINVAL;
599e05bbacSSakari Ailus }
609e05bbacSSakari Ailus 
61fadfe884SSakari Ailus #define PLL_OP 1
62fadfe884SSakari Ailus #define PLL_VT 2
63fadfe884SSakari Ailus 
64fadfe884SSakari Ailus static const char *pll_string(unsigned int which)
65fadfe884SSakari Ailus {
66fadfe884SSakari Ailus 	switch (which) {
67fadfe884SSakari Ailus 	case PLL_OP:
68fadfe884SSakari Ailus 		return "op";
69fadfe884SSakari Ailus 	case PLL_VT:
70fadfe884SSakari Ailus 		return "vt";
71fadfe884SSakari Ailus 	}
72fadfe884SSakari Ailus 
73fadfe884SSakari Ailus 	return NULL;
74fadfe884SSakari Ailus }
75fadfe884SSakari Ailus 
76fadfe884SSakari Ailus #define PLL_FL(f) CCS_PLL_FLAG_##f
77fadfe884SSakari Ailus 
789e05bbacSSakari Ailus static void print_pll(struct device *dev, struct ccs_pll *pll)
799e05bbacSSakari Ailus {
80fadfe884SSakari Ailus 	const struct {
81fadfe884SSakari Ailus 		struct ccs_pll_branch_fr *fr;
82fadfe884SSakari Ailus 		struct ccs_pll_branch_bk *bk;
83fadfe884SSakari Ailus 		unsigned int which;
84fadfe884SSakari Ailus 	} branches[] = {
85fadfe884SSakari Ailus 		{ &pll->vt_fr, &pll->vt_bk, PLL_VT },
86f25d3962SSakari Ailus 		{ &pll->op_fr, &pll->op_bk, PLL_OP }
87fadfe884SSakari Ailus 	}, *br;
88fadfe884SSakari Ailus 	unsigned int i;
899e05bbacSSakari Ailus 
90fadfe884SSakari Ailus 	dev_dbg(dev, "ext_clk_freq_hz\t\t%u\n", pll->ext_clk_freq_hz);
91fadfe884SSakari Ailus 
92fadfe884SSakari Ailus 	for (i = 0, br = branches; i < ARRAY_SIZE(branches); i++, br++) {
93fadfe884SSakari Ailus 		const char *s = pll_string(br->which);
94fadfe884SSakari Ailus 
95fadfe884SSakari Ailus 		if (br->which == PLL_VT) {
96fadfe884SSakari Ailus 			dev_dbg(dev, "%s_pre_pll_clk_div\t\t%u\n",  s,
97fadfe884SSakari Ailus 				br->fr->pre_pll_clk_div);
98fadfe884SSakari Ailus 			dev_dbg(dev, "%s_pll_multiplier\t\t%u\n",  s,
99fadfe884SSakari Ailus 				br->fr->pll_multiplier);
100fadfe884SSakari Ailus 
101fadfe884SSakari Ailus 			dev_dbg(dev, "%s_pll_ip_clk_freq_hz\t%u\n", s,
102fadfe884SSakari Ailus 				br->fr->pll_ip_clk_freq_hz);
103fadfe884SSakari Ailus 			dev_dbg(dev, "%s_pll_op_clk_freq_hz\t%u\n", s,
104fadfe884SSakari Ailus 				br->fr->pll_op_clk_freq_hz);
1059e05bbacSSakari Ailus 		}
106fadfe884SSakari Ailus 
107fadfe884SSakari Ailus 		if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) ||
108fadfe884SSakari Ailus 		    br->which == PLL_VT) {
109fadfe884SSakari Ailus 			dev_dbg(dev, "%s_sys_clk_div\t\t%u\n",  s,
110fadfe884SSakari Ailus 				br->bk->sys_clk_div);
111fadfe884SSakari Ailus 			dev_dbg(dev, "%s_pix_clk_div\t\t%u\n", s,
112fadfe884SSakari Ailus 				br->bk->pix_clk_div);
113fadfe884SSakari Ailus 
114fadfe884SSakari Ailus 			dev_dbg(dev, "%s_sys_clk_freq_hz\t%u\n", s,
115fadfe884SSakari Ailus 				br->bk->sys_clk_freq_hz);
116fadfe884SSakari Ailus 			dev_dbg(dev, "%s_pix_clk_freq_hz\t%u\n", s,
117fadfe884SSakari Ailus 				br->bk->pix_clk_freq_hz);
118fadfe884SSakari Ailus 		}
119fadfe884SSakari Ailus 	}
120fadfe884SSakari Ailus 
121fadfe884SSakari Ailus 	dev_dbg(dev, "flags%s%s%s%s%s%s\n",
122fadfe884SSakari Ailus 		pll->flags & PLL_FL(LANE_SPEED_MODEL) ? " lane-speed" : "",
123fadfe884SSakari Ailus 		pll->flags & PLL_FL(LINK_DECOUPLED) ? " link-decoupled" : "",
124fadfe884SSakari Ailus 		pll->flags & PLL_FL(EXT_IP_PLL_DIVIDER) ?
125fadfe884SSakari Ailus 		" ext-ip-pll-divider" : "",
126fadfe884SSakari Ailus 		pll->flags & PLL_FL(FLEXIBLE_OP_PIX_CLK_DIV) ?
127fadfe884SSakari Ailus 		" flexible-op-pix-div" : "",
128fadfe884SSakari Ailus 		pll->flags & PLL_FL(FIFO_DERATING) ? " fifo-derating" : "",
129fadfe884SSakari Ailus 		pll->flags & PLL_FL(FIFO_OVERRATING) ? " fifo-overrating" : "");
1309e05bbacSSakari Ailus }
1319e05bbacSSakari Ailus 
132f25d3962SSakari Ailus static int check_fr_bounds(struct device *dev,
133415ddd99SSakari Ailus 			   const struct ccs_pll_limits *lim,
134f25d3962SSakari Ailus 			   struct ccs_pll *pll, unsigned int which)
1359e05bbacSSakari Ailus {
136f25d3962SSakari Ailus 	const struct ccs_pll_branch_limits_fr *lim_fr;
137f25d3962SSakari Ailus 	struct ccs_pll_branch_fr *pll_fr;
138f25d3962SSakari Ailus 	const char *s = pll_string(which);
1399e05bbacSSakari Ailus 	int rval;
1409e05bbacSSakari Ailus 
141f25d3962SSakari Ailus 	if (which == PLL_OP) {
142f25d3962SSakari Ailus 		lim_fr = &lim->op_fr;
143f25d3962SSakari Ailus 		pll_fr = &pll->op_fr;
144f25d3962SSakari Ailus 	} else {
145f25d3962SSakari Ailus 		lim_fr = &lim->vt_fr;
146f25d3962SSakari Ailus 		pll_fr = &pll->vt_fr;
147f25d3962SSakari Ailus 	}
1489e05bbacSSakari Ailus 
149f25d3962SSakari Ailus 	rval = bounds_check(dev, pll_fr->pre_pll_clk_div,
150f25d3962SSakari Ailus 			    lim_fr->min_pre_pll_clk_div,
151f25d3962SSakari Ailus 			    lim_fr->max_pre_pll_clk_div, s, "pre_pll_clk_div");
152f25d3962SSakari Ailus 
153f25d3962SSakari Ailus 	if (!rval)
154f25d3962SSakari Ailus 		rval = bounds_check(dev, pll_fr->pll_ip_clk_freq_hz,
155f25d3962SSakari Ailus 				    lim_fr->min_pll_ip_clk_freq_hz,
156f25d3962SSakari Ailus 				    lim_fr->max_pll_ip_clk_freq_hz,
157f25d3962SSakari Ailus 				    s, "pll_ip_clk_freq_hz");
158f25d3962SSakari Ailus 	if (!rval)
159f25d3962SSakari Ailus 		rval = bounds_check(dev, pll_fr->pll_multiplier,
160f25d3962SSakari Ailus 				    lim_fr->min_pll_multiplier,
161f25d3962SSakari Ailus 				    lim_fr->max_pll_multiplier,
162f25d3962SSakari Ailus 				    s, "pll_multiplier");
163f25d3962SSakari Ailus 	if (!rval)
164f25d3962SSakari Ailus 		rval = bounds_check(dev, pll_fr->pll_op_clk_freq_hz,
165f25d3962SSakari Ailus 				    lim_fr->min_pll_op_clk_freq_hz,
166f25d3962SSakari Ailus 				    lim_fr->max_pll_op_clk_freq_hz,
167f25d3962SSakari Ailus 				    s, "pll_op_clk_freq_hz");
168f25d3962SSakari Ailus 
1699e05bbacSSakari Ailus 	return rval;
170f25d3962SSakari Ailus }
1719e05bbacSSakari Ailus 
172f25d3962SSakari Ailus static int check_bk_bounds(struct device *dev,
173f25d3962SSakari Ailus 			   const struct ccs_pll_limits *lim,
174f25d3962SSakari Ailus 			   struct ccs_pll *pll, unsigned int which)
175f25d3962SSakari Ailus {
176f25d3962SSakari Ailus 	const struct ccs_pll_branch_limits_bk *lim_bk;
177f25d3962SSakari Ailus 	struct ccs_pll_branch_bk *pll_bk;
178f25d3962SSakari Ailus 	const char *s = pll_string(which);
179f25d3962SSakari Ailus 	int rval;
1809e05bbacSSakari Ailus 
181f25d3962SSakari Ailus 	if (which == PLL_OP) {
182f25d3962SSakari Ailus 		if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)
183f25d3962SSakari Ailus 			return 0;
184f25d3962SSakari Ailus 
185f25d3962SSakari Ailus 		lim_bk = &lim->op_bk;
186f25d3962SSakari Ailus 		pll_bk = &pll->op_bk;
187f25d3962SSakari Ailus 	} else {
188f25d3962SSakari Ailus 		lim_bk = &lim->vt_bk;
189f25d3962SSakari Ailus 		pll_bk = &pll->vt_bk;
190f25d3962SSakari Ailus 	}
191f25d3962SSakari Ailus 
192f25d3962SSakari Ailus 	rval = bounds_check(dev, pll_bk->sys_clk_div,
193f25d3962SSakari Ailus 			    lim_bk->min_sys_clk_div,
194f25d3962SSakari Ailus 			    lim_bk->max_sys_clk_div, s, "op_sys_clk_div");
195f25d3962SSakari Ailus 	if (!rval)
196f25d3962SSakari Ailus 		rval = bounds_check(dev, pll_bk->sys_clk_freq_hz,
197f25d3962SSakari Ailus 				    lim_bk->min_sys_clk_freq_hz,
198f25d3962SSakari Ailus 				    lim_bk->max_sys_clk_freq_hz,
199f25d3962SSakari Ailus 				    s, "sys_clk_freq_hz");
200f25d3962SSakari Ailus 	if (!rval)
201f25d3962SSakari Ailus 		rval = bounds_check(dev, pll_bk->sys_clk_div,
202f25d3962SSakari Ailus 				    lim_bk->min_sys_clk_div,
203f25d3962SSakari Ailus 				    lim_bk->max_sys_clk_div,
204f25d3962SSakari Ailus 				    s, "sys_clk_div");
205f25d3962SSakari Ailus 	if (!rval)
206f25d3962SSakari Ailus 		rval = bounds_check(dev, pll_bk->pix_clk_freq_hz,
207f25d3962SSakari Ailus 				    lim_bk->min_pix_clk_freq_hz,
208f25d3962SSakari Ailus 				    lim_bk->max_pix_clk_freq_hz,
209f25d3962SSakari Ailus 				    s, "pix_clk_freq_hz");
210f25d3962SSakari Ailus 
211f25d3962SSakari Ailus 	return rval;
212f25d3962SSakari Ailus }
213f25d3962SSakari Ailus 
214f25d3962SSakari Ailus static int check_ext_bounds(struct device *dev, struct ccs_pll *pll)
215f25d3962SSakari Ailus {
21638c94eb8SSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_FIFO_DERATING) &&
21738c94eb8SSakari Ailus 	    pll->pixel_rate_pixel_array > pll->pixel_rate_csi) {
21838c94eb8SSakari Ailus 		dev_dbg(dev, "device does not support derating\n");
21938c94eb8SSakari Ailus 		return -EINVAL;
22038c94eb8SSakari Ailus 	}
22138c94eb8SSakari Ailus 
22238c94eb8SSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_FIFO_OVERRATING) &&
22338c94eb8SSakari Ailus 	    pll->pixel_rate_pixel_array < pll->pixel_rate_csi) {
22438c94eb8SSakari Ailus 		dev_dbg(dev, "device does not support overrating\n");
22538c94eb8SSakari Ailus 		return -EINVAL;
22638c94eb8SSakari Ailus 	}
22738c94eb8SSakari Ailus 
228f25d3962SSakari Ailus 	return 0;
2299e05bbacSSakari Ailus }
2309e05bbacSSakari Ailus 
231*9ec6e5b1SSakari Ailus static void
232*9ec6e5b1SSakari Ailus ccs_pll_find_vt_sys_div(struct device *dev, const struct ccs_pll_limits *lim,
233*9ec6e5b1SSakari Ailus 			struct ccs_pll *pll, struct ccs_pll_branch_fr *pll_fr,
234*9ec6e5b1SSakari Ailus 			uint16_t min_vt_div, uint16_t max_vt_div,
235*9ec6e5b1SSakari Ailus 			uint16_t *min_sys_div, uint16_t *max_sys_div)
236*9ec6e5b1SSakari Ailus {
237*9ec6e5b1SSakari Ailus 	/*
238*9ec6e5b1SSakari Ailus 	 * Find limits for sys_clk_div. Not all values are possible with all
239*9ec6e5b1SSakari Ailus 	 * values of pix_clk_div.
240*9ec6e5b1SSakari Ailus 	 */
241*9ec6e5b1SSakari Ailus 	*min_sys_div = lim->vt_bk.min_sys_clk_div;
242*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "min_sys_div: %u\n", *min_sys_div);
243*9ec6e5b1SSakari Ailus 	*min_sys_div = max_t(uint16_t, *min_sys_div,
244*9ec6e5b1SSakari Ailus 			     DIV_ROUND_UP(min_vt_div,
245*9ec6e5b1SSakari Ailus 					  lim->vt_bk.max_pix_clk_div));
246*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", *min_sys_div);
247*9ec6e5b1SSakari Ailus 	*min_sys_div = max_t(uint16_t, *min_sys_div,
248*9ec6e5b1SSakari Ailus 			     pll_fr->pll_op_clk_freq_hz
249*9ec6e5b1SSakari Ailus 			     / lim->vt_bk.max_sys_clk_freq_hz);
250*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", *min_sys_div);
251*9ec6e5b1SSakari Ailus 	*min_sys_div = clk_div_even_up(*min_sys_div);
252*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "min_sys_div: one or even: %u\n", *min_sys_div);
253*9ec6e5b1SSakari Ailus 
254*9ec6e5b1SSakari Ailus 	*max_sys_div = lim->vt_bk.max_sys_clk_div;
255*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "max_sys_div: %u\n", *max_sys_div);
256*9ec6e5b1SSakari Ailus 	*max_sys_div = min_t(uint16_t, *max_sys_div,
257*9ec6e5b1SSakari Ailus 			     DIV_ROUND_UP(max_vt_div,
258*9ec6e5b1SSakari Ailus 					  lim->vt_bk.min_pix_clk_div));
259*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", *max_sys_div);
260*9ec6e5b1SSakari Ailus 	*max_sys_div = min_t(uint16_t, *max_sys_div,
261*9ec6e5b1SSakari Ailus 			     DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz,
262*9ec6e5b1SSakari Ailus 					  lim->vt_bk.min_pix_clk_freq_hz));
263*9ec6e5b1SSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", *max_sys_div);
264*9ec6e5b1SSakari Ailus }
265*9ec6e5b1SSakari Ailus 
2668030aa4fSSakari Ailus #define CPHY_CONST		7
2678030aa4fSSakari Ailus #define DPHY_CONST		16
2688030aa4fSSakari Ailus #define PHY_CONST_DIV		16
2698030aa4fSSakari Ailus 
2703e2db036SSakari Ailus static void
271a38836b2SSakari Ailus ccs_pll_calculate_vt(struct device *dev, const struct ccs_pll_limits *lim,
2723e2db036SSakari Ailus 		     const struct ccs_pll_branch_limits_bk *op_lim_bk,
2733e2db036SSakari Ailus 		     struct ccs_pll *pll, struct ccs_pll_branch_fr *pll_fr,
2743e2db036SSakari Ailus 		     struct ccs_pll_branch_bk *op_pll_bk, bool cphy,
2753e2db036SSakari Ailus 		     uint32_t phy_const)
2763e2db036SSakari Ailus {
277594f1e93SSakari Ailus 	uint16_t sys_div;
278594f1e93SSakari Ailus 	uint16_t best_pix_div = SHRT_MAX >> 1;
279594f1e93SSakari Ailus 	uint16_t vt_op_binning_div;
280594f1e93SSakari Ailus 	uint16_t min_vt_div, max_vt_div, vt_div;
281594f1e93SSakari Ailus 	uint16_t min_sys_div, max_sys_div;
2823e2db036SSakari Ailus 
283a38836b2SSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)
284a38836b2SSakari Ailus 		goto out_calc_pixel_rate;
285a38836b2SSakari Ailus 
2863e2db036SSakari Ailus 	/*
28738c94eb8SSakari Ailus 	 * Find out whether a sensor supports derating. If it does not, VT and
28838c94eb8SSakari Ailus 	 * OP domains are required to run at the same pixel rate.
28938c94eb8SSakari Ailus 	 */
29038c94eb8SSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_FIFO_DERATING)) {
29138c94eb8SSakari Ailus 		min_vt_div =
29238c94eb8SSakari Ailus 			op_pll_bk->sys_clk_div * op_pll_bk->pix_clk_div
29338c94eb8SSakari Ailus 			* pll->vt_lanes * phy_const
29438c94eb8SSakari Ailus 			/ pll->op_lanes / PHY_CONST_DIV;
29538c94eb8SSakari Ailus 	} else {
29638c94eb8SSakari Ailus 		/*
2973e2db036SSakari Ailus 		 * Some sensors perform analogue binning and some do this
2983e2db036SSakari Ailus 		 * digitally. The ones doing this digitally can be roughly be
2993e2db036SSakari Ailus 		 * found out using this formula. The ones doing this digitally
3003e2db036SSakari Ailus 		 * should run at higher clock rate, so smaller divisor is used
3013e2db036SSakari Ailus 		 * on video timing side.
3023e2db036SSakari Ailus 		 */
3033e2db036SSakari Ailus 		if (lim->min_line_length_pck_bin > lim->min_line_length_pck
3043e2db036SSakari Ailus 		    / pll->binning_horizontal)
3053e2db036SSakari Ailus 			vt_op_binning_div = pll->binning_horizontal;
3063e2db036SSakari Ailus 		else
3073e2db036SSakari Ailus 			vt_op_binning_div = 1;
3083e2db036SSakari Ailus 		dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div);
3093e2db036SSakari Ailus 
3103e2db036SSakari Ailus 		/*
3113e2db036SSakari Ailus 		 * Profile 2 supports vt_pix_clk_div E [4, 10]
3123e2db036SSakari Ailus 		 *
3133e2db036SSakari Ailus 		 * Horizontal binning can be used as a base for difference in
3143e2db036SSakari Ailus 		 * divisors. One must make sure that horizontal blanking is
3153e2db036SSakari Ailus 		 * enough to accommodate the CSI-2 sync codes.
3163e2db036SSakari Ailus 		 *
3173e2db036SSakari Ailus 		 * Take scaling factor and number of VT lanes into account as well.
3183e2db036SSakari Ailus 		 *
3193e2db036SSakari Ailus 		 * Find absolute limits for the factor of vt divider.
3203e2db036SSakari Ailus 		 */
3213e2db036SSakari Ailus 		dev_dbg(dev, "scale_m: %u\n", pll->scale_m);
32238c94eb8SSakari Ailus 		min_vt_div =
32338c94eb8SSakari Ailus 			DIV_ROUND_UP(pll->bits_per_pixel
32438c94eb8SSakari Ailus 				     * op_pll_bk->sys_clk_div * pll->scale_n
32538c94eb8SSakari Ailus 				     * pll->vt_lanes * phy_const,
32638c94eb8SSakari Ailus 				     (pll->flags &
32738c94eb8SSakari Ailus 				      CCS_PLL_FLAG_LANE_SPEED_MODEL ?
3283e2db036SSakari Ailus 				      pll->csi2.lanes : 1)
3293e2db036SSakari Ailus 				     * vt_op_binning_div * pll->scale_m
3303e2db036SSakari Ailus 				     * PHY_CONST_DIV);
33138c94eb8SSakari Ailus 	}
3323e2db036SSakari Ailus 
3333e2db036SSakari Ailus 	/* Find smallest and biggest allowed vt divisor. */
3343e2db036SSakari Ailus 	dev_dbg(dev, "min_vt_div: %u\n", min_vt_div);
335594f1e93SSakari Ailus 	min_vt_div = max_t(uint16_t, min_vt_div,
3363e2db036SSakari Ailus 			   DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz,
3373e2db036SSakari Ailus 					lim->vt_bk.max_pix_clk_freq_hz));
3383e2db036SSakari Ailus 	dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n",
3393e2db036SSakari Ailus 		min_vt_div);
340594f1e93SSakari Ailus 	min_vt_div = max_t(uint16_t, min_vt_div, lim->vt_bk.min_pix_clk_div
3413e2db036SSakari Ailus 						 * lim->vt_bk.min_sys_clk_div);
3423e2db036SSakari Ailus 	dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div);
3433e2db036SSakari Ailus 
3443e2db036SSakari Ailus 	max_vt_div = lim->vt_bk.max_sys_clk_div * lim->vt_bk.max_pix_clk_div;
3453e2db036SSakari Ailus 	dev_dbg(dev, "max_vt_div: %u\n", max_vt_div);
346594f1e93SSakari Ailus 	max_vt_div = min_t(uint16_t, max_vt_div,
3473e2db036SSakari Ailus 			   DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz,
3483e2db036SSakari Ailus 				      lim->vt_bk.min_pix_clk_freq_hz));
3493e2db036SSakari Ailus 	dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n",
3503e2db036SSakari Ailus 		max_vt_div);
3513e2db036SSakari Ailus 
352*9ec6e5b1SSakari Ailus 	ccs_pll_find_vt_sys_div(dev, lim, pll, pll_fr, min_vt_div,
353*9ec6e5b1SSakari Ailus 				max_vt_div, &min_sys_div, &max_sys_div);
3543e2db036SSakari Ailus 
3553e2db036SSakari Ailus 	/*
3563e2db036SSakari Ailus 	 * Find pix_div such that a legal pix_div * sys_div results
3573e2db036SSakari Ailus 	 * into a value which is not smaller than div, the desired
3583e2db036SSakari Ailus 	 * divisor.
3593e2db036SSakari Ailus 	 */
36036154b68SSakari Ailus 	for (vt_div = min_vt_div; vt_div <= max_vt_div; vt_div++) {
36136154b68SSakari Ailus 		uint16_t __max_sys_div = vt_div & 1 ? 1 : max_sys_div;
36236154b68SSakari Ailus 
36336154b68SSakari Ailus 		for (sys_div = min_sys_div; sys_div <= __max_sys_div;
3643e2db036SSakari Ailus 		     sys_div += 2 - (sys_div & 1)) {
36536154b68SSakari Ailus 			uint16_t pix_div;
3663e2db036SSakari Ailus 			uint16_t rounded_div;
3673e2db036SSakari Ailus 
36836154b68SSakari Ailus 			pix_div = DIV_ROUND_UP(vt_div, sys_div);
36936154b68SSakari Ailus 
3703e2db036SSakari Ailus 			if (pix_div < lim->vt_bk.min_pix_clk_div
3713e2db036SSakari Ailus 			    || pix_div > lim->vt_bk.max_pix_clk_div) {
3723e2db036SSakari Ailus 				dev_dbg(dev,
3733e2db036SSakari Ailus 					"pix_div %u too small or too big (%u--%u)\n",
3743e2db036SSakari Ailus 					pix_div,
3753e2db036SSakari Ailus 					lim->vt_bk.min_pix_clk_div,
3763e2db036SSakari Ailus 					lim->vt_bk.max_pix_clk_div);
3773e2db036SSakari Ailus 				continue;
3783e2db036SSakari Ailus 			}
3793e2db036SSakari Ailus 
3803e2db036SSakari Ailus 			rounded_div = roundup(vt_div, best_pix_div);
3813e2db036SSakari Ailus 
3823e2db036SSakari Ailus 			/* Check if this one is better. */
3833e2db036SSakari Ailus 			if (pix_div * sys_div <= rounded_div)
3843e2db036SSakari Ailus 				best_pix_div = pix_div;
3853e2db036SSakari Ailus 
3863e2db036SSakari Ailus 			/* Bail out if we've already found the best value. */
3873e2db036SSakari Ailus 			if (vt_div == rounded_div)
3883e2db036SSakari Ailus 				break;
3893e2db036SSakari Ailus 		}
390594f1e93SSakari Ailus 		if (best_pix_div < SHRT_MAX >> 1)
3913e2db036SSakari Ailus 			break;
3923e2db036SSakari Ailus 	}
3933e2db036SSakari Ailus 
3943e2db036SSakari Ailus 	pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div);
3953e2db036SSakari Ailus 	pll->vt_bk.pix_clk_div = best_pix_div;
3963e2db036SSakari Ailus 
3973e2db036SSakari Ailus 	pll->vt_bk.sys_clk_freq_hz =
3983e2db036SSakari Ailus 		pll_fr->pll_op_clk_freq_hz / pll->vt_bk.sys_clk_div;
3993e2db036SSakari Ailus 	pll->vt_bk.pix_clk_freq_hz =
4003e2db036SSakari Ailus 		pll->vt_bk.sys_clk_freq_hz / pll->vt_bk.pix_clk_div;
401a38836b2SSakari Ailus 
402a38836b2SSakari Ailus out_calc_pixel_rate:
403a38836b2SSakari Ailus 	pll->pixel_rate_pixel_array =
404a38836b2SSakari Ailus 		pll->vt_bk.pix_clk_freq_hz * pll->vt_lanes;
4053e2db036SSakari Ailus }
4063e2db036SSakari Ailus 
4079e05bbacSSakari Ailus /*
4089e05bbacSSakari Ailus  * Heuristically guess the PLL tree for a given common multiplier and
4099e05bbacSSakari Ailus  * divisor. Begin with the operational timing and continue to video
4109e05bbacSSakari Ailus  * timing once operational timing has been verified.
4119e05bbacSSakari Ailus  *
4129e05bbacSSakari Ailus  * @mul is the PLL multiplier and @div is the common divisor
4139e05bbacSSakari Ailus  * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL
4149e05bbacSSakari Ailus  * multiplier will be a multiple of @mul.
4159e05bbacSSakari Ailus  *
4169e05bbacSSakari Ailus  * @return Zero on success, error code on error.
4179e05bbacSSakari Ailus  */
4189e05bbacSSakari Ailus static int
419a38836b2SSakari Ailus ccs_pll_calculate_op(struct device *dev, const struct ccs_pll_limits *lim,
420415ddd99SSakari Ailus 		     const struct ccs_pll_branch_limits_fr *op_lim_fr,
421415ddd99SSakari Ailus 		     const struct ccs_pll_branch_limits_bk *op_lim_bk,
422415ddd99SSakari Ailus 		     struct ccs_pll *pll, struct ccs_pll_branch_fr *op_pll_fr,
423415ddd99SSakari Ailus 		     struct ccs_pll_branch_bk *op_pll_bk, uint32_t mul,
4248030aa4fSSakari Ailus 		     uint32_t div, uint32_t l, bool cphy, uint32_t phy_const)
4259e05bbacSSakari Ailus {
4269e05bbacSSakari Ailus 	/*
4279e05bbacSSakari Ailus 	 * Higher multipliers (and divisors) are often required than
4289e05bbacSSakari Ailus 	 * necessitated by the external clock and the output clocks.
4299e05bbacSSakari Ailus 	 * There are limits for all values in the clock tree. These
4309e05bbacSSakari Ailus 	 * are the minimum and maximum multiplier for mul.
4319e05bbacSSakari Ailus 	 */
4329e05bbacSSakari Ailus 	uint32_t more_mul_min, more_mul_max;
4339e05bbacSSakari Ailus 	uint32_t more_mul_factor;
434e583e654SSakari Ailus 	uint32_t i;
4359e05bbacSSakari Ailus 
4369e05bbacSSakari Ailus 	/*
4379e05bbacSSakari Ailus 	 * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be
4389e05bbacSSakari Ailus 	 * too high.
4399e05bbacSSakari Ailus 	 */
440415ddd99SSakari Ailus 	dev_dbg(dev, "op_pre_pll_clk_div %u\n", op_pll_fr->pre_pll_clk_div);
4419e05bbacSSakari Ailus 
4429e05bbacSSakari Ailus 	/* Don't go above max pll multiplier. */
443415ddd99SSakari Ailus 	more_mul_max = op_lim_fr->max_pll_multiplier / mul;
444415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_pll_multiplier check: %u\n",
4459e05bbacSSakari Ailus 		more_mul_max);
4469e05bbacSSakari Ailus 	/* Don't go above max pll op frequency. */
4479e05bbacSSakari Ailus 	more_mul_max =
4489e05bbacSSakari Ailus 		min_t(uint32_t,
4499e05bbacSSakari Ailus 		      more_mul_max,
450415ddd99SSakari Ailus 		      op_lim_fr->max_pll_op_clk_freq_hz
451ae502e08SSakari Ailus 		      / (pll->ext_clk_freq_hz /
452ae502e08SSakari Ailus 			 op_pll_fr->pre_pll_clk_div * mul));
453415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_pll_op_clk_freq_hz check: %u\n",
4549e05bbacSSakari Ailus 		more_mul_max);
4559e05bbacSSakari Ailus 	/* Don't go above the division capability of op sys clock divider. */
4569e05bbacSSakari Ailus 	more_mul_max = min(more_mul_max,
457415ddd99SSakari Ailus 			   op_lim_bk->max_sys_clk_div * op_pll_fr->pre_pll_clk_div
4589e05bbacSSakari Ailus 			   / div);
4599e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n",
4609e05bbacSSakari Ailus 		more_mul_max);
461c64cf71dSSakari Ailus 	/* Ensure we won't go above max_pll_multiplier. */
46282ab97c8SSakari Ailus 	more_mul_max = min(more_mul_max, op_lim_fr->max_pll_multiplier / mul);
4639e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n",
4649e05bbacSSakari Ailus 		more_mul_max);
4659e05bbacSSakari Ailus 
466415ddd99SSakari Ailus 	/* Ensure we won't go below min_pll_op_clk_freq_hz. */
467415ddd99SSakari Ailus 	more_mul_min = DIV_ROUND_UP(op_lim_fr->min_pll_op_clk_freq_hz,
468415ddd99SSakari Ailus 				    pll->ext_clk_freq_hz /
469415ddd99SSakari Ailus 				    op_pll_fr->pre_pll_clk_div * mul);
470415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_op_clk_freq_hz check: %u\n",
4719e05bbacSSakari Ailus 		more_mul_min);
4729e05bbacSSakari Ailus 	/* Ensure we won't go below min_pll_multiplier. */
4739e05bbacSSakari Ailus 	more_mul_min = max(more_mul_min,
474415ddd99SSakari Ailus 			   DIV_ROUND_UP(op_lim_fr->min_pll_multiplier, mul));
475415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_multiplier check: %u\n",
4769e05bbacSSakari Ailus 		more_mul_min);
4779e05bbacSSakari Ailus 
4789e05bbacSSakari Ailus 	if (more_mul_min > more_mul_max) {
4799e05bbacSSakari Ailus 		dev_dbg(dev,
4809e05bbacSSakari Ailus 			"unable to compute more_mul_min and more_mul_max\n");
4819e05bbacSSakari Ailus 		return -EINVAL;
4829e05bbacSSakari Ailus 	}
4839e05bbacSSakari Ailus 
484415ddd99SSakari Ailus 	more_mul_factor = lcm(div, op_pll_fr->pre_pll_clk_div) / div;
4859e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor);
486415ddd99SSakari Ailus 	more_mul_factor = lcm(more_mul_factor, op_lim_bk->min_sys_clk_div);
4879e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n",
4889e05bbacSSakari Ailus 		more_mul_factor);
4899e05bbacSSakari Ailus 	i = roundup(more_mul_min, more_mul_factor);
4909e05bbacSSakari Ailus 	if (!is_one_or_even(i))
4919e05bbacSSakari Ailus 		i <<= 1;
4929e05bbacSSakari Ailus 
4939e05bbacSSakari Ailus 	dev_dbg(dev, "final more_mul: %u\n", i);
4949e05bbacSSakari Ailus 	if (i > more_mul_max) {
4959e05bbacSSakari Ailus 		dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max);
4969e05bbacSSakari Ailus 		return -EINVAL;
4979e05bbacSSakari Ailus 	}
4989e05bbacSSakari Ailus 
499415ddd99SSakari Ailus 	op_pll_fr->pll_multiplier = mul * i;
500415ddd99SSakari Ailus 	op_pll_bk->sys_clk_div = div * i / op_pll_fr->pre_pll_clk_div;
501415ddd99SSakari Ailus 	dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll_bk->sys_clk_div);
5029e05bbacSSakari Ailus 
503415ddd99SSakari Ailus 	op_pll_fr->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz
504415ddd99SSakari Ailus 		/ op_pll_fr->pre_pll_clk_div;
5059e05bbacSSakari Ailus 
506415ddd99SSakari Ailus 	op_pll_fr->pll_op_clk_freq_hz = op_pll_fr->pll_ip_clk_freq_hz
507415ddd99SSakari Ailus 		* op_pll_fr->pll_multiplier;
5089e05bbacSSakari Ailus 
509c4c0b222SSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL)
510cac8f5d2SSakari Ailus 		op_pll_bk->pix_clk_div = pll->bits_per_pixel
5118030aa4fSSakari Ailus 			* pll->op_lanes * phy_const
5128030aa4fSSakari Ailus 			/ PHY_CONST_DIV / pll->csi2.lanes / l;
513c4c0b222SSakari Ailus 	else
5148030aa4fSSakari Ailus 		op_pll_bk->pix_clk_div =
5158030aa4fSSakari Ailus 			pll->bits_per_pixel * phy_const / PHY_CONST_DIV / l;
516c4c0b222SSakari Ailus 
517415ddd99SSakari Ailus 	op_pll_bk->pix_clk_freq_hz =
518415ddd99SSakari Ailus 		op_pll_bk->sys_clk_freq_hz / op_pll_bk->pix_clk_div;
519c4c0b222SSakari Ailus 
520cac8f5d2SSakari Ailus 	dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll_bk->pix_clk_div);
521cac8f5d2SSakari Ailus 
522a38836b2SSakari Ailus 	return 0;
5239e05bbacSSakari Ailus }
5249e05bbacSSakari Ailus 
525415ddd99SSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
5269e05bbacSSakari Ailus 		      struct ccs_pll *pll)
5279e05bbacSSakari Ailus {
528415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_fr *op_lim_fr = &lim->vt_fr;
529415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_bk *op_lim_bk = &lim->op_bk;
530415ddd99SSakari Ailus 	struct ccs_pll_branch_fr *op_pll_fr = &pll->vt_fr;
531415ddd99SSakari Ailus 	struct ccs_pll_branch_bk *op_pll_bk = &pll->op_bk;
5328030aa4fSSakari Ailus 	bool cphy = pll->bus_type == CCS_PLL_BUS_TYPE_CSI2_CPHY;
5338030aa4fSSakari Ailus 	uint32_t phy_const = cphy ? CPHY_CONST : DPHY_CONST;
534415ddd99SSakari Ailus 	uint16_t min_op_pre_pll_clk_div;
535415ddd99SSakari Ailus 	uint16_t max_op_pre_pll_clk_div;
5369e05bbacSSakari Ailus 	uint32_t mul, div;
537c4c0b222SSakari Ailus 	uint32_t l = (!pll->op_bits_per_lane ||
538c4c0b222SSakari Ailus 		      pll->op_bits_per_lane >= pll->bits_per_pixel) ? 1 : 2;
539e583e654SSakari Ailus 	uint32_t i;
5409e05bbacSSakari Ailus 	int rval = -EINVAL;
5419e05bbacSSakari Ailus 
542cac8f5d2SSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL)) {
543cac8f5d2SSakari Ailus 		pll->op_lanes = 1;
544cac8f5d2SSakari Ailus 		pll->vt_lanes = 1;
545cac8f5d2SSakari Ailus 	}
5469490a227SSakari Ailus 
547d7172c0eSSakari Ailus 	if (!pll->op_lanes || !pll->vt_lanes || !pll->bits_per_pixel ||
548d7172c0eSSakari Ailus 	    !pll->ext_clk_freq_hz || !pll->link_freq || !pll->scale_m ||
549d7172c0eSSakari Ailus 	    !op_lim_fr->min_pll_ip_clk_freq_hz ||
550d7172c0eSSakari Ailus 	    !op_lim_fr->max_pll_ip_clk_freq_hz ||
551d7172c0eSSakari Ailus 	    !op_lim_fr->min_pll_op_clk_freq_hz ||
552d7172c0eSSakari Ailus 	    !op_lim_fr->max_pll_op_clk_freq_hz ||
553d7172c0eSSakari Ailus 	    !op_lim_bk->max_sys_clk_div || !op_lim_fr->max_pll_multiplier)
554d7172c0eSSakari Ailus 		return -EINVAL;
555d7172c0eSSakari Ailus 
5569490a227SSakari Ailus 	/*
5579490a227SSakari Ailus 	 * Make sure op_pix_clk_div will be integer --- unless flexible
5589490a227SSakari Ailus 	 * op_pix_clk_div is supported
5599490a227SSakari Ailus 	 */
5609490a227SSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV) &&
5619490a227SSakari Ailus 	    (pll->bits_per_pixel * pll->op_lanes) % (pll->csi2.lanes * l)) {
5629490a227SSakari Ailus 		dev_dbg(dev, "op_pix_clk_div not an integer (bpp %u, op lanes %u, lanes %u, l %u)\n",
5639490a227SSakari Ailus 			pll->bits_per_pixel, pll->op_lanes, pll->csi2.lanes, l);
5649490a227SSakari Ailus 		return -EINVAL;
5659490a227SSakari Ailus 	}
5669490a227SSakari Ailus 
567cac8f5d2SSakari Ailus 	dev_dbg(dev, "vt_lanes: %u\n", pll->vt_lanes);
568cac8f5d2SSakari Ailus 	dev_dbg(dev, "op_lanes: %u\n", pll->op_lanes);
569cac8f5d2SSakari Ailus 
5709e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
5719e05bbacSSakari Ailus 		/*
5729e05bbacSSakari Ailus 		 * If there's no OP PLL at all, use the VT values
5739e05bbacSSakari Ailus 		 * instead. The OP values are ignored for the rest of
5749e05bbacSSakari Ailus 		 * the PLL calculation.
5759e05bbacSSakari Ailus 		 */
576415ddd99SSakari Ailus 		op_lim_fr = &lim->vt_fr;
577415ddd99SSakari Ailus 		op_lim_bk = &lim->vt_bk;
578415ddd99SSakari Ailus 		op_pll_bk = &pll->vt_bk;
5799e05bbacSSakari Ailus 	}
5809e05bbacSSakari Ailus 
5819e05bbacSSakari Ailus 	dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal,
5829e05bbacSSakari Ailus 		pll->binning_vertical);
5839e05bbacSSakari Ailus 
5849e05bbacSSakari Ailus 	switch (pll->bus_type) {
58547b6eaf3SSakari Ailus 	case CCS_PLL_BUS_TYPE_CSI2_DPHY:
5869e05bbacSSakari Ailus 		/* CSI transfers 2 bits per clock per lane; thus times 2 */
587cab27256SSakari Ailus 		op_pll_bk->sys_clk_freq_hz = pll->link_freq * 2
588cac8f5d2SSakari Ailus 			* (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
589ae502e08SSakari Ailus 			   1 : pll->csi2.lanes);
5909e05bbacSSakari Ailus 		break;
5918030aa4fSSakari Ailus 	case CCS_PLL_BUS_TYPE_CSI2_CPHY:
5928030aa4fSSakari Ailus 		op_pll_bk->sys_clk_freq_hz =
5938030aa4fSSakari Ailus 			pll->link_freq
5948030aa4fSSakari Ailus 			* (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
5958030aa4fSSakari Ailus 			   1 : pll->csi2.lanes);
5968030aa4fSSakari Ailus 		break;
5979e05bbacSSakari Ailus 	default:
5989e05bbacSSakari Ailus 		return -EINVAL;
5999e05bbacSSakari Ailus 	}
6009e05bbacSSakari Ailus 
601cac8f5d2SSakari Ailus 	pll->pixel_rate_csi =
6028030aa4fSSakari Ailus 		div_u64((uint64_t)op_pll_bk->sys_clk_freq_hz
603cac8f5d2SSakari Ailus 			* (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
6048030aa4fSSakari Ailus 			   pll->csi2.lanes : 1) * PHY_CONST_DIV,
6058030aa4fSSakari Ailus 			phy_const * pll->bits_per_pixel * l);
606cac8f5d2SSakari Ailus 
607415ddd99SSakari Ailus 	/* Figure out limits for OP pre-pll divider based on extclk */
608415ddd99SSakari Ailus 	dev_dbg(dev, "min / max op_pre_pll_clk_div: %u / %u\n",
609415ddd99SSakari Ailus 		op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div);
610415ddd99SSakari Ailus 	max_op_pre_pll_clk_div =
611415ddd99SSakari Ailus 		min_t(uint16_t, op_lim_fr->max_pre_pll_clk_div,
6129e05bbacSSakari Ailus 		      clk_div_even(pll->ext_clk_freq_hz /
613415ddd99SSakari Ailus 				   op_lim_fr->min_pll_ip_clk_freq_hz));
614415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
615415ddd99SSakari Ailus 		max_t(uint16_t, op_lim_fr->min_pre_pll_clk_div,
6169e05bbacSSakari Ailus 		      clk_div_even_up(
6179e05bbacSSakari Ailus 			      DIV_ROUND_UP(pll->ext_clk_freq_hz,
618415ddd99SSakari Ailus 					   op_lim_fr->max_pll_ip_clk_freq_hz)));
619415ddd99SSakari Ailus 	dev_dbg(dev, "pre-pll check: min / max op_pre_pll_clk_div: %u / %u\n",
620415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
6219e05bbacSSakari Ailus 
622cab27256SSakari Ailus 	i = gcd(op_pll_bk->sys_clk_freq_hz, pll->ext_clk_freq_hz);
623cab27256SSakari Ailus 	mul = op_pll_bk->sys_clk_freq_hz / i;
6249e05bbacSSakari Ailus 	div = pll->ext_clk_freq_hz / i;
6259e05bbacSSakari Ailus 	dev_dbg(dev, "mul %u / div %u\n", mul, div);
6269e05bbacSSakari Ailus 
627415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
628415ddd99SSakari Ailus 		max_t(uint16_t, min_op_pre_pll_clk_div,
6299e05bbacSSakari Ailus 		      clk_div_even_up(
630482e75e7SSakari Ailus 			      mul /
631482e75e7SSakari Ailus 			      one_or_more(
632482e75e7SSakari Ailus 				      DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz,
633482e75e7SSakari Ailus 						   pll->ext_clk_freq_hz))));
634415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n",
635415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
6369e05bbacSSakari Ailus 
637415ddd99SSakari Ailus 	for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div;
638415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div;
6394e1e8d24SSakari Ailus 	     op_pll_fr->pre_pll_clk_div +=
6404e1e8d24SSakari Ailus 		     (pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER) ? 1 :
6414e1e8d24SSakari Ailus 		     2 - (op_pll_fr->pre_pll_clk_div & 1)) {
642a38836b2SSakari Ailus 		rval = ccs_pll_calculate_op(dev, lim, op_lim_fr, op_lim_bk, pll,
6438030aa4fSSakari Ailus 					    op_pll_fr, op_pll_bk, mul, div, l,
6448030aa4fSSakari Ailus 					    cphy, phy_const);
6459e05bbacSSakari Ailus 		if (rval)
6469e05bbacSSakari Ailus 			continue;
6479e05bbacSSakari Ailus 
648f25d3962SSakari Ailus 		rval = check_fr_bounds(dev, lim, pll, PLL_VT);
649f25d3962SSakari Ailus 		if (rval)
650f25d3962SSakari Ailus 			continue;
651f25d3962SSakari Ailus 
652f25d3962SSakari Ailus 		rval = check_bk_bounds(dev, lim, pll, PLL_OP);
653f25d3962SSakari Ailus 		if (rval)
654f25d3962SSakari Ailus 			continue;
655f25d3962SSakari Ailus 
656a38836b2SSakari Ailus 		ccs_pll_calculate_vt(dev, lim, op_lim_bk, pll, op_pll_fr,
657a38836b2SSakari Ailus 				     op_pll_bk, cphy, phy_const);
658a38836b2SSakari Ailus 
659f25d3962SSakari Ailus 		rval = check_bk_bounds(dev, lim, pll, PLL_VT);
660f25d3962SSakari Ailus 		if (rval)
661f25d3962SSakari Ailus 			continue;
662f25d3962SSakari Ailus 		rval = check_ext_bounds(dev, pll);
663a38836b2SSakari Ailus 		if (rval)
664a38836b2SSakari Ailus 			continue;
665a38836b2SSakari Ailus 
6669e05bbacSSakari Ailus 		print_pll(dev, pll);
667a38836b2SSakari Ailus 
6689e05bbacSSakari Ailus 		return 0;
6699e05bbacSSakari Ailus 	}
6709e05bbacSSakari Ailus 
6719e05bbacSSakari Ailus 	dev_dbg(dev, "unable to compute pre_pll divisor\n");
6729e05bbacSSakari Ailus 
6739e05bbacSSakari Ailus 	return rval;
6749e05bbacSSakari Ailus }
6759e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate);
6769e05bbacSSakari Ailus 
6777389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
6789e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator");
679b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2");
680