xref: /openbmc/linux/drivers/media/i2c/ccs-pll.c (revision 47b6eaf36eba143860cf4e772a1108b1ef05520d)
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 
439e05bbacSSakari Ailus static int bounds_check(struct device *dev, uint32_t val,
449e05bbacSSakari Ailus 			uint32_t min, uint32_t max, char *str)
459e05bbacSSakari Ailus {
469e05bbacSSakari Ailus 	if (val >= min && val <= max)
479e05bbacSSakari Ailus 		return 0;
489e05bbacSSakari Ailus 
499e05bbacSSakari Ailus 	dev_dbg(dev, "%s out of bounds: %d (%d--%d)\n", str, val, min, max);
509e05bbacSSakari Ailus 
519e05bbacSSakari Ailus 	return -EINVAL;
529e05bbacSSakari Ailus }
539e05bbacSSakari Ailus 
549e05bbacSSakari Ailus static void print_pll(struct device *dev, struct ccs_pll *pll)
559e05bbacSSakari Ailus {
56415ddd99SSakari Ailus 	dev_dbg(dev, "pre_pll_clk_div\t%u\n",  pll->vt_fr.pre_pll_clk_div);
57415ddd99SSakari Ailus 	dev_dbg(dev, "pll_multiplier \t%u\n",  pll->vt_fr.pll_multiplier);
589e05bbacSSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)) {
59415ddd99SSakari Ailus 		dev_dbg(dev, "op_sys_clk_div \t%u\n", pll->op_bk.sys_clk_div);
60415ddd99SSakari Ailus 		dev_dbg(dev, "op_pix_clk_div \t%u\n", pll->op_bk.pix_clk_div);
619e05bbacSSakari Ailus 	}
62415ddd99SSakari Ailus 	dev_dbg(dev, "vt_sys_clk_div \t%u\n",  pll->vt_bk.sys_clk_div);
63415ddd99SSakari Ailus 	dev_dbg(dev, "vt_pix_clk_div \t%u\n",  pll->vt_bk.pix_clk_div);
649e05bbacSSakari Ailus 
659e05bbacSSakari Ailus 	dev_dbg(dev, "ext_clk_freq_hz \t%u\n", pll->ext_clk_freq_hz);
66415ddd99SSakari Ailus 	dev_dbg(dev, "pll_ip_clk_freq_hz \t%u\n", pll->vt_fr.pll_ip_clk_freq_hz);
67415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op_clk_freq_hz \t%u\n", pll->vt_fr.pll_op_clk_freq_hz);
689e05bbacSSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)) {
699e05bbacSSakari Ailus 		dev_dbg(dev, "op_sys_clk_freq_hz \t%u\n",
70415ddd99SSakari Ailus 			pll->op_bk.sys_clk_freq_hz);
719e05bbacSSakari Ailus 		dev_dbg(dev, "op_pix_clk_freq_hz \t%u\n",
72415ddd99SSakari Ailus 			pll->op_bk.pix_clk_freq_hz);
739e05bbacSSakari Ailus 	}
74415ddd99SSakari Ailus 	dev_dbg(dev, "vt_sys_clk_freq_hz \t%u\n", pll->vt_bk.sys_clk_freq_hz);
75415ddd99SSakari Ailus 	dev_dbg(dev, "vt_pix_clk_freq_hz \t%u\n", pll->vt_bk.pix_clk_freq_hz);
769e05bbacSSakari Ailus }
779e05bbacSSakari Ailus 
789e05bbacSSakari Ailus static int check_all_bounds(struct device *dev,
79415ddd99SSakari Ailus 			    const struct ccs_pll_limits *lim,
80415ddd99SSakari Ailus 			    const struct ccs_pll_branch_limits_fr *op_lim_fr,
81415ddd99SSakari Ailus 			    const struct ccs_pll_branch_limits_bk *op_lim_bk,
82415ddd99SSakari Ailus 			    struct ccs_pll *pll,
83415ddd99SSakari Ailus 			    struct ccs_pll_branch_fr *op_pll_fr,
84415ddd99SSakari Ailus 			    struct ccs_pll_branch_bk *op_pll_bk)
859e05bbacSSakari Ailus {
869e05bbacSSakari Ailus 	int rval;
879e05bbacSSakari Ailus 
88415ddd99SSakari Ailus 	rval = bounds_check(dev, op_pll_fr->pll_ip_clk_freq_hz,
89415ddd99SSakari Ailus 			    op_lim_fr->min_pll_ip_clk_freq_hz,
90415ddd99SSakari Ailus 			    op_lim_fr->max_pll_ip_clk_freq_hz,
919e05bbacSSakari Ailus 			    "pll_ip_clk_freq_hz");
929e05bbacSSakari Ailus 	if (!rval)
939e05bbacSSakari Ailus 		rval = bounds_check(
94415ddd99SSakari Ailus 			dev, op_pll_fr->pll_multiplier,
95415ddd99SSakari Ailus 			op_lim_fr->min_pll_multiplier,
96415ddd99SSakari Ailus 			op_lim_fr->max_pll_multiplier, "pll_multiplier");
979e05bbacSSakari Ailus 	if (!rval)
989e05bbacSSakari Ailus 		rval = bounds_check(
99415ddd99SSakari Ailus 			dev, op_pll_fr->pll_op_clk_freq_hz,
100415ddd99SSakari Ailus 			op_lim_fr->min_pll_op_clk_freq_hz,
101415ddd99SSakari Ailus 			op_lim_fr->max_pll_op_clk_freq_hz, "pll_op_clk_freq_hz");
1029e05bbacSSakari Ailus 	if (!rval)
1039e05bbacSSakari Ailus 		rval = bounds_check(
104415ddd99SSakari Ailus 			dev, op_pll_bk->sys_clk_div,
105415ddd99SSakari Ailus 			op_lim_bk->min_sys_clk_div, op_lim_bk->max_sys_clk_div,
1069e05bbacSSakari Ailus 			"op_sys_clk_div");
1079e05bbacSSakari Ailus 	if (!rval)
1089e05bbacSSakari Ailus 		rval = bounds_check(
109415ddd99SSakari Ailus 			dev, op_pll_bk->sys_clk_freq_hz,
110415ddd99SSakari Ailus 			op_lim_bk->min_sys_clk_freq_hz,
111415ddd99SSakari Ailus 			op_lim_bk->max_sys_clk_freq_hz,
1129e05bbacSSakari Ailus 			"op_sys_clk_freq_hz");
1139e05bbacSSakari Ailus 	if (!rval)
1149e05bbacSSakari Ailus 		rval = bounds_check(
115415ddd99SSakari Ailus 			dev, op_pll_bk->pix_clk_freq_hz,
116415ddd99SSakari Ailus 			op_lim_bk->min_pix_clk_freq_hz,
117415ddd99SSakari Ailus 			op_lim_bk->max_pix_clk_freq_hz,
1189e05bbacSSakari Ailus 			"op_pix_clk_freq_hz");
1199e05bbacSSakari Ailus 
1209e05bbacSSakari Ailus 	/*
1219e05bbacSSakari Ailus 	 * If there are no OP clocks, the VT clocks are contained in
1229e05bbacSSakari Ailus 	 * the OP clock struct.
1239e05bbacSSakari Ailus 	 */
1249e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)
1259e05bbacSSakari Ailus 		return rval;
1269e05bbacSSakari Ailus 
1279e05bbacSSakari Ailus 	if (!rval)
1289e05bbacSSakari Ailus 		rval = bounds_check(
129415ddd99SSakari Ailus 			dev, pll->vt_bk.sys_clk_freq_hz,
130415ddd99SSakari Ailus 			lim->vt_bk.min_sys_clk_freq_hz,
131415ddd99SSakari Ailus 			lim->vt_bk.max_sys_clk_freq_hz,
1329e05bbacSSakari Ailus 			"vt_sys_clk_freq_hz");
1339e05bbacSSakari Ailus 	if (!rval)
1349e05bbacSSakari Ailus 		rval = bounds_check(
135415ddd99SSakari Ailus 			dev, pll->vt_bk.pix_clk_freq_hz,
136415ddd99SSakari Ailus 			lim->vt_bk.min_pix_clk_freq_hz,
137415ddd99SSakari Ailus 			lim->vt_bk.max_pix_clk_freq_hz,
1389e05bbacSSakari Ailus 			"vt_pix_clk_freq_hz");
1399e05bbacSSakari Ailus 
1409e05bbacSSakari Ailus 	return rval;
1419e05bbacSSakari Ailus }
1429e05bbacSSakari Ailus 
1439e05bbacSSakari Ailus /*
1449e05bbacSSakari Ailus  * Heuristically guess the PLL tree for a given common multiplier and
1459e05bbacSSakari Ailus  * divisor. Begin with the operational timing and continue to video
1469e05bbacSSakari Ailus  * timing once operational timing has been verified.
1479e05bbacSSakari Ailus  *
1489e05bbacSSakari Ailus  * @mul is the PLL multiplier and @div is the common divisor
1499e05bbacSSakari Ailus  * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL
1509e05bbacSSakari Ailus  * multiplier will be a multiple of @mul.
1519e05bbacSSakari Ailus  *
1529e05bbacSSakari Ailus  * @return Zero on success, error code on error.
1539e05bbacSSakari Ailus  */
1549e05bbacSSakari Ailus static int
155415ddd99SSakari Ailus __ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
156415ddd99SSakari Ailus 		    const struct ccs_pll_branch_limits_fr *op_lim_fr,
157415ddd99SSakari Ailus 		    const struct ccs_pll_branch_limits_bk *op_lim_bk,
158415ddd99SSakari Ailus 		    struct ccs_pll *pll, struct ccs_pll_branch_fr *op_pll_fr,
159415ddd99SSakari Ailus 		    struct ccs_pll_branch_bk *op_pll_bk, uint32_t mul,
160415ddd99SSakari Ailus 		    uint32_t div, uint32_t lane_op_clock_ratio)
1619e05bbacSSakari Ailus {
1629e05bbacSSakari Ailus 	uint32_t sys_div;
1639e05bbacSSakari Ailus 	uint32_t best_pix_div = INT_MAX >> 1;
1649e05bbacSSakari Ailus 	uint32_t vt_op_binning_div;
1659e05bbacSSakari Ailus 	/*
1669e05bbacSSakari Ailus 	 * Higher multipliers (and divisors) are often required than
1679e05bbacSSakari Ailus 	 * necessitated by the external clock and the output clocks.
1689e05bbacSSakari Ailus 	 * There are limits for all values in the clock tree. These
1699e05bbacSSakari Ailus 	 * are the minimum and maximum multiplier for mul.
1709e05bbacSSakari Ailus 	 */
1719e05bbacSSakari Ailus 	uint32_t more_mul_min, more_mul_max;
1729e05bbacSSakari Ailus 	uint32_t more_mul_factor;
1739e05bbacSSakari Ailus 	uint32_t min_vt_div, max_vt_div, vt_div;
1749e05bbacSSakari Ailus 	uint32_t min_sys_div, max_sys_div;
1759e05bbacSSakari Ailus 	unsigned int i;
1769e05bbacSSakari Ailus 
1779e05bbacSSakari Ailus 	/*
1789e05bbacSSakari Ailus 	 * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be
1799e05bbacSSakari Ailus 	 * too high.
1809e05bbacSSakari Ailus 	 */
181415ddd99SSakari Ailus 	dev_dbg(dev, "op_pre_pll_clk_div %u\n", op_pll_fr->pre_pll_clk_div);
1829e05bbacSSakari Ailus 
1839e05bbacSSakari Ailus 	/* Don't go above max pll multiplier. */
184415ddd99SSakari Ailus 	more_mul_max = op_lim_fr->max_pll_multiplier / mul;
185415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_pll_multiplier check: %u\n",
1869e05bbacSSakari Ailus 		more_mul_max);
1879e05bbacSSakari Ailus 	/* Don't go above max pll op frequency. */
1889e05bbacSSakari Ailus 	more_mul_max =
1899e05bbacSSakari Ailus 		min_t(uint32_t,
1909e05bbacSSakari Ailus 		      more_mul_max,
191415ddd99SSakari Ailus 		      op_lim_fr->max_pll_op_clk_freq_hz
192415ddd99SSakari Ailus 		      / (pll->ext_clk_freq_hz / op_pll_fr->pre_pll_clk_div * mul));
193415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_pll_op_clk_freq_hz check: %u\n",
1949e05bbacSSakari Ailus 		more_mul_max);
1959e05bbacSSakari Ailus 	/* Don't go above the division capability of op sys clock divider. */
1969e05bbacSSakari Ailus 	more_mul_max = min(more_mul_max,
197415ddd99SSakari Ailus 			   op_lim_bk->max_sys_clk_div * op_pll_fr->pre_pll_clk_div
1989e05bbacSSakari Ailus 			   / div);
1999e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n",
2009e05bbacSSakari Ailus 		more_mul_max);
2019e05bbacSSakari Ailus 	/* Ensure we won't go above min_pll_multiplier. */
2029e05bbacSSakari Ailus 	more_mul_max = min(more_mul_max,
203415ddd99SSakari Ailus 			   DIV_ROUND_UP(op_lim_fr->max_pll_multiplier, mul));
2049e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n",
2059e05bbacSSakari Ailus 		more_mul_max);
2069e05bbacSSakari Ailus 
207415ddd99SSakari Ailus 	/* Ensure we won't go below min_pll_op_clk_freq_hz. */
208415ddd99SSakari Ailus 	more_mul_min = DIV_ROUND_UP(op_lim_fr->min_pll_op_clk_freq_hz,
209415ddd99SSakari Ailus 				    pll->ext_clk_freq_hz /
210415ddd99SSakari Ailus 				    op_pll_fr->pre_pll_clk_div * mul);
211415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_op_clk_freq_hz check: %u\n",
2129e05bbacSSakari Ailus 		more_mul_min);
2139e05bbacSSakari Ailus 	/* Ensure we won't go below min_pll_multiplier. */
2149e05bbacSSakari Ailus 	more_mul_min = max(more_mul_min,
215415ddd99SSakari Ailus 			   DIV_ROUND_UP(op_lim_fr->min_pll_multiplier, mul));
216415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_multiplier check: %u\n",
2179e05bbacSSakari Ailus 		more_mul_min);
2189e05bbacSSakari Ailus 
2199e05bbacSSakari Ailus 	if (more_mul_min > more_mul_max) {
2209e05bbacSSakari Ailus 		dev_dbg(dev,
2219e05bbacSSakari Ailus 			"unable to compute more_mul_min and more_mul_max\n");
2229e05bbacSSakari Ailus 		return -EINVAL;
2239e05bbacSSakari Ailus 	}
2249e05bbacSSakari Ailus 
225415ddd99SSakari Ailus 	more_mul_factor = lcm(div, op_pll_fr->pre_pll_clk_div) / div;
2269e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor);
227415ddd99SSakari Ailus 	more_mul_factor = lcm(more_mul_factor, op_lim_bk->min_sys_clk_div);
2289e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n",
2299e05bbacSSakari Ailus 		more_mul_factor);
2309e05bbacSSakari Ailus 	i = roundup(more_mul_min, more_mul_factor);
2319e05bbacSSakari Ailus 	if (!is_one_or_even(i))
2329e05bbacSSakari Ailus 		i <<= 1;
2339e05bbacSSakari Ailus 
2349e05bbacSSakari Ailus 	dev_dbg(dev, "final more_mul: %u\n", i);
2359e05bbacSSakari Ailus 	if (i > more_mul_max) {
2369e05bbacSSakari Ailus 		dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max);
2379e05bbacSSakari Ailus 		return -EINVAL;
2389e05bbacSSakari Ailus 	}
2399e05bbacSSakari Ailus 
240415ddd99SSakari Ailus 	op_pll_fr->pll_multiplier = mul * i;
241415ddd99SSakari Ailus 	op_pll_bk->sys_clk_div = div * i / op_pll_fr->pre_pll_clk_div;
242415ddd99SSakari Ailus 	dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll_bk->sys_clk_div);
2439e05bbacSSakari Ailus 
244415ddd99SSakari Ailus 	op_pll_fr->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz
245415ddd99SSakari Ailus 		/ op_pll_fr->pre_pll_clk_div;
2469e05bbacSSakari Ailus 
247415ddd99SSakari Ailus 	op_pll_fr->pll_op_clk_freq_hz = op_pll_fr->pll_ip_clk_freq_hz
248415ddd99SSakari Ailus 		* op_pll_fr->pll_multiplier;
2499e05bbacSSakari Ailus 
2509e05bbacSSakari Ailus 	/* Derive pll_op_clk_freq_hz. */
251415ddd99SSakari Ailus 	op_pll_bk->sys_clk_freq_hz =
252415ddd99SSakari Ailus 		op_pll_fr->pll_op_clk_freq_hz / op_pll_bk->sys_clk_div;
2539e05bbacSSakari Ailus 
254415ddd99SSakari Ailus 	op_pll_bk->pix_clk_div = pll->bits_per_pixel;
255415ddd99SSakari Ailus 	dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll_bk->pix_clk_div);
2569e05bbacSSakari Ailus 
257415ddd99SSakari Ailus 	op_pll_bk->pix_clk_freq_hz =
258415ddd99SSakari Ailus 		op_pll_bk->sys_clk_freq_hz / op_pll_bk->pix_clk_div;
2599e05bbacSSakari Ailus 
2609e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
2619e05bbacSSakari Ailus 		/* No OP clocks --- VT clocks are used instead. */
2629e05bbacSSakari Ailus 		goto out_skip_vt_calc;
2639e05bbacSSakari Ailus 	}
2649e05bbacSSakari Ailus 
2659e05bbacSSakari Ailus 	/*
2669e05bbacSSakari Ailus 	 * Some sensors perform analogue binning and some do this
2679e05bbacSSakari Ailus 	 * digitally. The ones doing this digitally can be roughly be
2689e05bbacSSakari Ailus 	 * found out using this formula. The ones doing this digitally
2699e05bbacSSakari Ailus 	 * should run at higher clock rate, so smaller divisor is used
2709e05bbacSSakari Ailus 	 * on video timing side.
2719e05bbacSSakari Ailus 	 */
272415ddd99SSakari Ailus 	if (lim->min_line_length_pck_bin > lim->min_line_length_pck
2739e05bbacSSakari Ailus 	    / pll->binning_horizontal)
2749e05bbacSSakari Ailus 		vt_op_binning_div = pll->binning_horizontal;
2759e05bbacSSakari Ailus 	else
2769e05bbacSSakari Ailus 		vt_op_binning_div = 1;
2779e05bbacSSakari Ailus 	dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div);
2789e05bbacSSakari Ailus 
2799e05bbacSSakari Ailus 	/*
2809e05bbacSSakari Ailus 	 * Profile 2 supports vt_pix_clk_div E [4, 10]
2819e05bbacSSakari Ailus 	 *
2829e05bbacSSakari Ailus 	 * Horizontal binning can be used as a base for difference in
2839e05bbacSSakari Ailus 	 * divisors. One must make sure that horizontal blanking is
2849e05bbacSSakari Ailus 	 * enough to accommodate the CSI-2 sync codes.
2859e05bbacSSakari Ailus 	 *
2869e05bbacSSakari Ailus 	 * Take scaling factor into account as well.
2879e05bbacSSakari Ailus 	 *
2889e05bbacSSakari Ailus 	 * Find absolute limits for the factor of vt divider.
2899e05bbacSSakari Ailus 	 */
2909e05bbacSSakari Ailus 	dev_dbg(dev, "scale_m: %u\n", pll->scale_m);
291415ddd99SSakari Ailus 	min_vt_div = DIV_ROUND_UP(op_pll_bk->pix_clk_div
292415ddd99SSakari Ailus 				  * op_pll_bk->sys_clk_div * pll->scale_n,
2939e05bbacSSakari Ailus 				  lane_op_clock_ratio * vt_op_binning_div
2949e05bbacSSakari Ailus 				  * pll->scale_m);
2959e05bbacSSakari Ailus 
2969e05bbacSSakari Ailus 	/* Find smallest and biggest allowed vt divisor. */
2979e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: %u\n", min_vt_div);
2989e05bbacSSakari Ailus 	min_vt_div = max(min_vt_div,
299415ddd99SSakari Ailus 			 DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
300415ddd99SSakari Ailus 				      lim->vt_bk.max_pix_clk_freq_hz));
3019e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n",
3029e05bbacSSakari Ailus 		min_vt_div);
3039e05bbacSSakari Ailus 	min_vt_div = max_t(uint32_t, min_vt_div,
304415ddd99SSakari Ailus 			   lim->vt_bk.min_pix_clk_div
305415ddd99SSakari Ailus 			   * lim->vt_bk.min_sys_clk_div);
3069e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div);
3079e05bbacSSakari Ailus 
308415ddd99SSakari Ailus 	max_vt_div = lim->vt_bk.max_sys_clk_div * lim->vt_bk.max_pix_clk_div;
3099e05bbacSSakari Ailus 	dev_dbg(dev, "max_vt_div: %u\n", max_vt_div);
3109e05bbacSSakari Ailus 	max_vt_div = min(max_vt_div,
311415ddd99SSakari Ailus 			 DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
312415ddd99SSakari Ailus 				      lim->vt_bk.min_pix_clk_freq_hz));
3139e05bbacSSakari Ailus 	dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n",
3149e05bbacSSakari Ailus 		max_vt_div);
3159e05bbacSSakari Ailus 
3169e05bbacSSakari Ailus 	/*
3179e05bbacSSakari Ailus 	 * Find limitsits for sys_clk_div. Not all values are possible
3189e05bbacSSakari Ailus 	 * with all values of pix_clk_div.
3199e05bbacSSakari Ailus 	 */
320415ddd99SSakari Ailus 	min_sys_div = lim->vt_bk.min_sys_clk_div;
3219e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: %u\n", min_sys_div);
3229e05bbacSSakari Ailus 	min_sys_div = max(min_sys_div,
3239e05bbacSSakari Ailus 			  DIV_ROUND_UP(min_vt_div,
324415ddd99SSakari Ailus 				       lim->vt_bk.max_pix_clk_div));
3259e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", min_sys_div);
3269e05bbacSSakari Ailus 	min_sys_div = max(min_sys_div,
327415ddd99SSakari Ailus 			  op_pll_fr->pll_op_clk_freq_hz
328415ddd99SSakari Ailus 			  / lim->vt_bk.max_sys_clk_freq_hz);
3299e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", min_sys_div);
3309e05bbacSSakari Ailus 	min_sys_div = clk_div_even_up(min_sys_div);
3319e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: one or even: %u\n", min_sys_div);
3329e05bbacSSakari Ailus 
333415ddd99SSakari Ailus 	max_sys_div = lim->vt_bk.max_sys_clk_div;
3349e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: %u\n", max_sys_div);
3359e05bbacSSakari Ailus 	max_sys_div = min(max_sys_div,
3369e05bbacSSakari Ailus 			  DIV_ROUND_UP(max_vt_div,
337415ddd99SSakari Ailus 				       lim->vt_bk.min_pix_clk_div));
3389e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", max_sys_div);
3399e05bbacSSakari Ailus 	max_sys_div = min(max_sys_div,
340415ddd99SSakari Ailus 			  DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
341415ddd99SSakari Ailus 				       lim->vt_bk.min_pix_clk_freq_hz));
3429e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", max_sys_div);
3439e05bbacSSakari Ailus 
3449e05bbacSSakari Ailus 	/*
3459e05bbacSSakari Ailus 	 * Find pix_div such that a legal pix_div * sys_div results
3469e05bbacSSakari Ailus 	 * into a value which is not smaller than div, the desired
3479e05bbacSSakari Ailus 	 * divisor.
3489e05bbacSSakari Ailus 	 */
3499e05bbacSSakari Ailus 	for (vt_div = min_vt_div; vt_div <= max_vt_div;
3509e05bbacSSakari Ailus 	     vt_div += 2 - (vt_div & 1)) {
3519e05bbacSSakari Ailus 		for (sys_div = min_sys_div;
3529e05bbacSSakari Ailus 		     sys_div <= max_sys_div;
3539e05bbacSSakari Ailus 		     sys_div += 2 - (sys_div & 1)) {
3549e05bbacSSakari Ailus 			uint16_t pix_div = DIV_ROUND_UP(vt_div, sys_div);
3559c1a0d9eSSakari Ailus 			uint16_t rounded_div;
3569e05bbacSSakari Ailus 
357415ddd99SSakari Ailus 			if (pix_div < lim->vt_bk.min_pix_clk_div
358415ddd99SSakari Ailus 			    || pix_div > lim->vt_bk.max_pix_clk_div) {
3599e05bbacSSakari Ailus 				dev_dbg(dev,
3609e05bbacSSakari Ailus 					"pix_div %u too small or too big (%u--%u)\n",
3619e05bbacSSakari Ailus 					pix_div,
362415ddd99SSakari Ailus 					lim->vt_bk.min_pix_clk_div,
363415ddd99SSakari Ailus 					lim->vt_bk.max_pix_clk_div);
3649e05bbacSSakari Ailus 				continue;
3659e05bbacSSakari Ailus 			}
3669e05bbacSSakari Ailus 
3679c1a0d9eSSakari Ailus 			rounded_div = roundup(vt_div, best_pix_div);
3689c1a0d9eSSakari Ailus 
3699e05bbacSSakari Ailus 			/* Check if this one is better. */
3709c1a0d9eSSakari Ailus 			if (pix_div * sys_div <= rounded_div)
3719e05bbacSSakari Ailus 				best_pix_div = pix_div;
3729c1a0d9eSSakari Ailus 
3739c1a0d9eSSakari Ailus 			/* Bail out if we've already found the best value. */
3749c1a0d9eSSakari Ailus 			if (vt_div == rounded_div)
3759c1a0d9eSSakari Ailus 				break;
3769e05bbacSSakari Ailus 		}
3779e05bbacSSakari Ailus 		if (best_pix_div < INT_MAX >> 1)
3789e05bbacSSakari Ailus 			break;
3799e05bbacSSakari Ailus 	}
3809e05bbacSSakari Ailus 
3819454432aSSakari Ailus 	pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div);
382415ddd99SSakari Ailus 	pll->vt_bk.pix_clk_div = best_pix_div;
3839e05bbacSSakari Ailus 
384415ddd99SSakari Ailus 	pll->vt_bk.sys_clk_freq_hz =
385415ddd99SSakari Ailus 		op_pll_fr->pll_op_clk_freq_hz / pll->vt_bk.sys_clk_div;
386415ddd99SSakari Ailus 	pll->vt_bk.pix_clk_freq_hz =
387415ddd99SSakari Ailus 		pll->vt_bk.sys_clk_freq_hz / pll->vt_bk.pix_clk_div;
3889e05bbacSSakari Ailus 
3899e05bbacSSakari Ailus out_skip_vt_calc:
3909e05bbacSSakari Ailus 	pll->pixel_rate_csi =
391415ddd99SSakari Ailus 		op_pll_bk->pix_clk_freq_hz * lane_op_clock_ratio;
392415ddd99SSakari Ailus 	pll->pixel_rate_pixel_array = pll->vt_bk.pix_clk_freq_hz;
3939e05bbacSSakari Ailus 
394415ddd99SSakari Ailus 	return check_all_bounds(dev, lim, op_lim_fr, op_lim_bk, pll, op_pll_fr,
395415ddd99SSakari Ailus 				op_pll_bk);
3969e05bbacSSakari Ailus }
3979e05bbacSSakari Ailus 
398415ddd99SSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
3999e05bbacSSakari Ailus 		      struct ccs_pll *pll)
4009e05bbacSSakari Ailus {
401415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_fr *op_lim_fr = &lim->vt_fr;
402415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_bk *op_lim_bk = &lim->op_bk;
403415ddd99SSakari Ailus 	struct ccs_pll_branch_fr *op_pll_fr = &pll->vt_fr;
404415ddd99SSakari Ailus 	struct ccs_pll_branch_bk *op_pll_bk = &pll->op_bk;
405415ddd99SSakari Ailus 	uint16_t min_op_pre_pll_clk_div;
406415ddd99SSakari Ailus 	uint16_t max_op_pre_pll_clk_div;
4079e05bbacSSakari Ailus 	uint32_t lane_op_clock_ratio;
4089e05bbacSSakari Ailus 	uint32_t mul, div;
4099e05bbacSSakari Ailus 	unsigned int i;
4109e05bbacSSakari Ailus 	int rval = -EINVAL;
4119e05bbacSSakari Ailus 
4129e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
4139e05bbacSSakari Ailus 		/*
4149e05bbacSSakari Ailus 		 * If there's no OP PLL at all, use the VT values
4159e05bbacSSakari Ailus 		 * instead. The OP values are ignored for the rest of
4169e05bbacSSakari Ailus 		 * the PLL calculation.
4179e05bbacSSakari Ailus 		 */
418415ddd99SSakari Ailus 		op_lim_fr = &lim->vt_fr;
419415ddd99SSakari Ailus 		op_lim_bk = &lim->vt_bk;
420415ddd99SSakari Ailus 		op_pll_bk = &pll->vt_bk;
4219e05bbacSSakari Ailus 	}
4229e05bbacSSakari Ailus 
4239e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_OP_PIX_CLOCK_PER_LANE)
4249e05bbacSSakari Ailus 		lane_op_clock_ratio = pll->csi2.lanes;
4259e05bbacSSakari Ailus 	else
4269e05bbacSSakari Ailus 		lane_op_clock_ratio = 1;
4279e05bbacSSakari Ailus 	dev_dbg(dev, "lane_op_clock_ratio: %u\n", lane_op_clock_ratio);
4289e05bbacSSakari Ailus 
4299e05bbacSSakari Ailus 	dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal,
4309e05bbacSSakari Ailus 		pll->binning_vertical);
4319e05bbacSSakari Ailus 
4329e05bbacSSakari Ailus 	switch (pll->bus_type) {
433*47b6eaf3SSakari Ailus 	case CCS_PLL_BUS_TYPE_CSI2_DPHY:
4349e05bbacSSakari Ailus 		/* CSI transfers 2 bits per clock per lane; thus times 2 */
435415ddd99SSakari Ailus 		op_pll_fr->pll_op_clk_freq_hz = pll->link_freq * 2
4369e05bbacSSakari Ailus 			* (pll->csi2.lanes / lane_op_clock_ratio);
4379e05bbacSSakari Ailus 		break;
4389e05bbacSSakari Ailus 	default:
4399e05bbacSSakari Ailus 		return -EINVAL;
4409e05bbacSSakari Ailus 	}
4419e05bbacSSakari Ailus 
442415ddd99SSakari Ailus 	/* Figure out limits for OP pre-pll divider based on extclk */
443415ddd99SSakari Ailus 	dev_dbg(dev, "min / max op_pre_pll_clk_div: %u / %u\n",
444415ddd99SSakari Ailus 		op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div);
445415ddd99SSakari Ailus 	max_op_pre_pll_clk_div =
446415ddd99SSakari Ailus 		min_t(uint16_t, op_lim_fr->max_pre_pll_clk_div,
4479e05bbacSSakari Ailus 		      clk_div_even(pll->ext_clk_freq_hz /
448415ddd99SSakari Ailus 				   op_lim_fr->min_pll_ip_clk_freq_hz));
449415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
450415ddd99SSakari Ailus 		max_t(uint16_t, op_lim_fr->min_pre_pll_clk_div,
4519e05bbacSSakari Ailus 		      clk_div_even_up(
4529e05bbacSSakari Ailus 			      DIV_ROUND_UP(pll->ext_clk_freq_hz,
453415ddd99SSakari Ailus 					   op_lim_fr->max_pll_ip_clk_freq_hz)));
454415ddd99SSakari Ailus 	dev_dbg(dev, "pre-pll check: min / max op_pre_pll_clk_div: %u / %u\n",
455415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4569e05bbacSSakari Ailus 
457415ddd99SSakari Ailus 	i = gcd(op_pll_fr->pll_op_clk_freq_hz, pll->ext_clk_freq_hz);
458415ddd99SSakari Ailus 	mul = op_pll_fr->pll_op_clk_freq_hz / i;
4599e05bbacSSakari Ailus 	div = pll->ext_clk_freq_hz / i;
4609e05bbacSSakari Ailus 	dev_dbg(dev, "mul %u / div %u\n", mul, div);
4619e05bbacSSakari Ailus 
462415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
463415ddd99SSakari Ailus 		max_t(uint16_t, min_op_pre_pll_clk_div,
4649e05bbacSSakari Ailus 		      clk_div_even_up(
4659e05bbacSSakari Ailus 			      DIV_ROUND_UP(mul * pll->ext_clk_freq_hz,
466415ddd99SSakari Ailus 					   op_lim_fr->max_pll_op_clk_freq_hz)));
467415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n",
468415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4699e05bbacSSakari Ailus 
470415ddd99SSakari Ailus 	for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div;
471415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div;
472415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div += 2 - (op_pll_fr->pre_pll_clk_div & 1)) {
473415ddd99SSakari Ailus 		rval = __ccs_pll_calculate(dev, lim, op_lim_fr, op_lim_bk, pll,
474415ddd99SSakari Ailus 					   op_pll_fr, op_pll_bk, mul, div,
475415ddd99SSakari Ailus 					   lane_op_clock_ratio);
4769e05bbacSSakari Ailus 		if (rval)
4779e05bbacSSakari Ailus 			continue;
4789e05bbacSSakari Ailus 
4799e05bbacSSakari Ailus 		print_pll(dev, pll);
4809e05bbacSSakari Ailus 		return 0;
4819e05bbacSSakari Ailus 	}
4829e05bbacSSakari Ailus 
4839e05bbacSSakari Ailus 	dev_dbg(dev, "unable to compute pre_pll divisor\n");
4849e05bbacSSakari Ailus 
4859e05bbacSSakari Ailus 	return rval;
4869e05bbacSSakari Ailus }
4879e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate);
4889e05bbacSSakari Ailus 
4897389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
4909e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator");
491b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2");
492