xref: /openbmc/linux/drivers/media/i2c/ccs-pll.c (revision 482e75e7b3eba6730cbfaa1911916d13887c9606)
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 
43*482e75e7SSakari Ailus static inline uint32_t one_or_more(uint32_t a)
44*482e75e7SSakari Ailus {
45*482e75e7SSakari Ailus 	return a ?: 1;
46*482e75e7SSakari Ailus }
47*482e75e7SSakari Ailus 
489e05bbacSSakari Ailus static int bounds_check(struct device *dev, uint32_t val,
499e05bbacSSakari Ailus 			uint32_t min, uint32_t max, char *str)
509e05bbacSSakari Ailus {
519e05bbacSSakari Ailus 	if (val >= min && val <= max)
529e05bbacSSakari Ailus 		return 0;
539e05bbacSSakari Ailus 
549e05bbacSSakari Ailus 	dev_dbg(dev, "%s out of bounds: %d (%d--%d)\n", str, val, min, max);
559e05bbacSSakari Ailus 
569e05bbacSSakari Ailus 	return -EINVAL;
579e05bbacSSakari Ailus }
589e05bbacSSakari Ailus 
599e05bbacSSakari Ailus static void print_pll(struct device *dev, struct ccs_pll *pll)
609e05bbacSSakari Ailus {
61415ddd99SSakari Ailus 	dev_dbg(dev, "pre_pll_clk_div\t%u\n",  pll->vt_fr.pre_pll_clk_div);
62415ddd99SSakari Ailus 	dev_dbg(dev, "pll_multiplier \t%u\n",  pll->vt_fr.pll_multiplier);
639e05bbacSSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)) {
64415ddd99SSakari Ailus 		dev_dbg(dev, "op_sys_clk_div \t%u\n", pll->op_bk.sys_clk_div);
65415ddd99SSakari Ailus 		dev_dbg(dev, "op_pix_clk_div \t%u\n", pll->op_bk.pix_clk_div);
669e05bbacSSakari Ailus 	}
67415ddd99SSakari Ailus 	dev_dbg(dev, "vt_sys_clk_div \t%u\n",  pll->vt_bk.sys_clk_div);
68415ddd99SSakari Ailus 	dev_dbg(dev, "vt_pix_clk_div \t%u\n",  pll->vt_bk.pix_clk_div);
699e05bbacSSakari Ailus 
709e05bbacSSakari Ailus 	dev_dbg(dev, "ext_clk_freq_hz \t%u\n", pll->ext_clk_freq_hz);
71415ddd99SSakari Ailus 	dev_dbg(dev, "pll_ip_clk_freq_hz \t%u\n", pll->vt_fr.pll_ip_clk_freq_hz);
72415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op_clk_freq_hz \t%u\n", pll->vt_fr.pll_op_clk_freq_hz);
739e05bbacSSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)) {
749e05bbacSSakari Ailus 		dev_dbg(dev, "op_sys_clk_freq_hz \t%u\n",
75415ddd99SSakari Ailus 			pll->op_bk.sys_clk_freq_hz);
769e05bbacSSakari Ailus 		dev_dbg(dev, "op_pix_clk_freq_hz \t%u\n",
77415ddd99SSakari Ailus 			pll->op_bk.pix_clk_freq_hz);
789e05bbacSSakari Ailus 	}
79415ddd99SSakari Ailus 	dev_dbg(dev, "vt_sys_clk_freq_hz \t%u\n", pll->vt_bk.sys_clk_freq_hz);
80415ddd99SSakari Ailus 	dev_dbg(dev, "vt_pix_clk_freq_hz \t%u\n", pll->vt_bk.pix_clk_freq_hz);
819e05bbacSSakari Ailus }
829e05bbacSSakari Ailus 
839e05bbacSSakari Ailus static int check_all_bounds(struct device *dev,
84415ddd99SSakari Ailus 			    const struct ccs_pll_limits *lim,
85415ddd99SSakari Ailus 			    const struct ccs_pll_branch_limits_fr *op_lim_fr,
86415ddd99SSakari Ailus 			    const struct ccs_pll_branch_limits_bk *op_lim_bk,
87415ddd99SSakari Ailus 			    struct ccs_pll *pll,
88415ddd99SSakari Ailus 			    struct ccs_pll_branch_fr *op_pll_fr,
89415ddd99SSakari Ailus 			    struct ccs_pll_branch_bk *op_pll_bk)
909e05bbacSSakari Ailus {
919e05bbacSSakari Ailus 	int rval;
929e05bbacSSakari Ailus 
93415ddd99SSakari Ailus 	rval = bounds_check(dev, op_pll_fr->pll_ip_clk_freq_hz,
94415ddd99SSakari Ailus 			    op_lim_fr->min_pll_ip_clk_freq_hz,
95415ddd99SSakari Ailus 			    op_lim_fr->max_pll_ip_clk_freq_hz,
969e05bbacSSakari Ailus 			    "pll_ip_clk_freq_hz");
979e05bbacSSakari Ailus 	if (!rval)
989e05bbacSSakari Ailus 		rval = bounds_check(
99415ddd99SSakari Ailus 			dev, op_pll_fr->pll_multiplier,
100415ddd99SSakari Ailus 			op_lim_fr->min_pll_multiplier,
101415ddd99SSakari Ailus 			op_lim_fr->max_pll_multiplier, "pll_multiplier");
1029e05bbacSSakari Ailus 	if (!rval)
1039e05bbacSSakari Ailus 		rval = bounds_check(
104415ddd99SSakari Ailus 			dev, op_pll_fr->pll_op_clk_freq_hz,
105415ddd99SSakari Ailus 			op_lim_fr->min_pll_op_clk_freq_hz,
106415ddd99SSakari Ailus 			op_lim_fr->max_pll_op_clk_freq_hz, "pll_op_clk_freq_hz");
1079e05bbacSSakari Ailus 	if (!rval)
1089e05bbacSSakari Ailus 		rval = bounds_check(
109415ddd99SSakari Ailus 			dev, op_pll_bk->sys_clk_div,
110415ddd99SSakari Ailus 			op_lim_bk->min_sys_clk_div, op_lim_bk->max_sys_clk_div,
1119e05bbacSSakari Ailus 			"op_sys_clk_div");
1129e05bbacSSakari Ailus 	if (!rval)
1139e05bbacSSakari Ailus 		rval = bounds_check(
114415ddd99SSakari Ailus 			dev, op_pll_bk->sys_clk_freq_hz,
115415ddd99SSakari Ailus 			op_lim_bk->min_sys_clk_freq_hz,
116415ddd99SSakari Ailus 			op_lim_bk->max_sys_clk_freq_hz,
1179e05bbacSSakari Ailus 			"op_sys_clk_freq_hz");
1189e05bbacSSakari Ailus 	if (!rval)
1199e05bbacSSakari Ailus 		rval = bounds_check(
120415ddd99SSakari Ailus 			dev, op_pll_bk->pix_clk_freq_hz,
121415ddd99SSakari Ailus 			op_lim_bk->min_pix_clk_freq_hz,
122415ddd99SSakari Ailus 			op_lim_bk->max_pix_clk_freq_hz,
1239e05bbacSSakari Ailus 			"op_pix_clk_freq_hz");
1249e05bbacSSakari Ailus 
1259e05bbacSSakari Ailus 	/*
1269e05bbacSSakari Ailus 	 * If there are no OP clocks, the VT clocks are contained in
1279e05bbacSSakari Ailus 	 * the OP clock struct.
1289e05bbacSSakari Ailus 	 */
1299e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)
1309e05bbacSSakari Ailus 		return rval;
1319e05bbacSSakari Ailus 
1329e05bbacSSakari Ailus 	if (!rval)
1339e05bbacSSakari Ailus 		rval = bounds_check(
134415ddd99SSakari Ailus 			dev, pll->vt_bk.sys_clk_freq_hz,
135415ddd99SSakari Ailus 			lim->vt_bk.min_sys_clk_freq_hz,
136415ddd99SSakari Ailus 			lim->vt_bk.max_sys_clk_freq_hz,
1379e05bbacSSakari Ailus 			"vt_sys_clk_freq_hz");
1389e05bbacSSakari Ailus 	if (!rval)
1399e05bbacSSakari Ailus 		rval = bounds_check(
140415ddd99SSakari Ailus 			dev, pll->vt_bk.pix_clk_freq_hz,
141415ddd99SSakari Ailus 			lim->vt_bk.min_pix_clk_freq_hz,
142415ddd99SSakari Ailus 			lim->vt_bk.max_pix_clk_freq_hz,
1439e05bbacSSakari Ailus 			"vt_pix_clk_freq_hz");
1449e05bbacSSakari Ailus 
1459e05bbacSSakari Ailus 	return rval;
1469e05bbacSSakari Ailus }
1479e05bbacSSakari Ailus 
1489e05bbacSSakari Ailus /*
1499e05bbacSSakari Ailus  * Heuristically guess the PLL tree for a given common multiplier and
1509e05bbacSSakari Ailus  * divisor. Begin with the operational timing and continue to video
1519e05bbacSSakari Ailus  * timing once operational timing has been verified.
1529e05bbacSSakari Ailus  *
1539e05bbacSSakari Ailus  * @mul is the PLL multiplier and @div is the common divisor
1549e05bbacSSakari Ailus  * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL
1559e05bbacSSakari Ailus  * multiplier will be a multiple of @mul.
1569e05bbacSSakari Ailus  *
1579e05bbacSSakari Ailus  * @return Zero on success, error code on error.
1589e05bbacSSakari Ailus  */
1599e05bbacSSakari Ailus static int
160415ddd99SSakari Ailus __ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
161415ddd99SSakari Ailus 		    const struct ccs_pll_branch_limits_fr *op_lim_fr,
162415ddd99SSakari Ailus 		    const struct ccs_pll_branch_limits_bk *op_lim_bk,
163415ddd99SSakari Ailus 		    struct ccs_pll *pll, struct ccs_pll_branch_fr *op_pll_fr,
164415ddd99SSakari Ailus 		    struct ccs_pll_branch_bk *op_pll_bk, uint32_t mul,
165415ddd99SSakari Ailus 		    uint32_t div, uint32_t lane_op_clock_ratio)
1669e05bbacSSakari Ailus {
1679e05bbacSSakari Ailus 	uint32_t sys_div;
1689e05bbacSSakari Ailus 	uint32_t best_pix_div = INT_MAX >> 1;
1699e05bbacSSakari Ailus 	uint32_t vt_op_binning_div;
1709e05bbacSSakari Ailus 	/*
1719e05bbacSSakari Ailus 	 * Higher multipliers (and divisors) are often required than
1729e05bbacSSakari Ailus 	 * necessitated by the external clock and the output clocks.
1739e05bbacSSakari Ailus 	 * There are limits for all values in the clock tree. These
1749e05bbacSSakari Ailus 	 * are the minimum and maximum multiplier for mul.
1759e05bbacSSakari Ailus 	 */
1769e05bbacSSakari Ailus 	uint32_t more_mul_min, more_mul_max;
1779e05bbacSSakari Ailus 	uint32_t more_mul_factor;
1789e05bbacSSakari Ailus 	uint32_t min_vt_div, max_vt_div, vt_div;
1799e05bbacSSakari Ailus 	uint32_t min_sys_div, max_sys_div;
1809e05bbacSSakari Ailus 	unsigned int i;
1819e05bbacSSakari Ailus 
1829e05bbacSSakari Ailus 	/*
1839e05bbacSSakari Ailus 	 * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be
1849e05bbacSSakari Ailus 	 * too high.
1859e05bbacSSakari Ailus 	 */
186415ddd99SSakari Ailus 	dev_dbg(dev, "op_pre_pll_clk_div %u\n", op_pll_fr->pre_pll_clk_div);
1879e05bbacSSakari Ailus 
1889e05bbacSSakari Ailus 	/* Don't go above max pll multiplier. */
189415ddd99SSakari Ailus 	more_mul_max = op_lim_fr->max_pll_multiplier / mul;
190415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_pll_multiplier check: %u\n",
1919e05bbacSSakari Ailus 		more_mul_max);
1929e05bbacSSakari Ailus 	/* Don't go above max pll op frequency. */
1939e05bbacSSakari Ailus 	more_mul_max =
1949e05bbacSSakari Ailus 		min_t(uint32_t,
1959e05bbacSSakari Ailus 		      more_mul_max,
196415ddd99SSakari Ailus 		      op_lim_fr->max_pll_op_clk_freq_hz
197415ddd99SSakari Ailus 		      / (pll->ext_clk_freq_hz / op_pll_fr->pre_pll_clk_div * mul));
198415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_pll_op_clk_freq_hz check: %u\n",
1999e05bbacSSakari Ailus 		more_mul_max);
2009e05bbacSSakari Ailus 	/* Don't go above the division capability of op sys clock divider. */
2019e05bbacSSakari Ailus 	more_mul_max = min(more_mul_max,
202415ddd99SSakari Ailus 			   op_lim_bk->max_sys_clk_div * op_pll_fr->pre_pll_clk_div
2039e05bbacSSakari Ailus 			   / div);
2049e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n",
2059e05bbacSSakari Ailus 		more_mul_max);
2069e05bbacSSakari Ailus 	/* Ensure we won't go above min_pll_multiplier. */
2079e05bbacSSakari Ailus 	more_mul_max = min(more_mul_max,
208415ddd99SSakari Ailus 			   DIV_ROUND_UP(op_lim_fr->max_pll_multiplier, mul));
2099e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n",
2109e05bbacSSakari Ailus 		more_mul_max);
2119e05bbacSSakari Ailus 
212415ddd99SSakari Ailus 	/* Ensure we won't go below min_pll_op_clk_freq_hz. */
213415ddd99SSakari Ailus 	more_mul_min = DIV_ROUND_UP(op_lim_fr->min_pll_op_clk_freq_hz,
214415ddd99SSakari Ailus 				    pll->ext_clk_freq_hz /
215415ddd99SSakari Ailus 				    op_pll_fr->pre_pll_clk_div * mul);
216415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_op_clk_freq_hz check: %u\n",
2179e05bbacSSakari Ailus 		more_mul_min);
2189e05bbacSSakari Ailus 	/* Ensure we won't go below min_pll_multiplier. */
2199e05bbacSSakari Ailus 	more_mul_min = max(more_mul_min,
220415ddd99SSakari Ailus 			   DIV_ROUND_UP(op_lim_fr->min_pll_multiplier, mul));
221415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_multiplier check: %u\n",
2229e05bbacSSakari Ailus 		more_mul_min);
2239e05bbacSSakari Ailus 
2249e05bbacSSakari Ailus 	if (more_mul_min > more_mul_max) {
2259e05bbacSSakari Ailus 		dev_dbg(dev,
2269e05bbacSSakari Ailus 			"unable to compute more_mul_min and more_mul_max\n");
2279e05bbacSSakari Ailus 		return -EINVAL;
2289e05bbacSSakari Ailus 	}
2299e05bbacSSakari Ailus 
230415ddd99SSakari Ailus 	more_mul_factor = lcm(div, op_pll_fr->pre_pll_clk_div) / div;
2319e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor);
232415ddd99SSakari Ailus 	more_mul_factor = lcm(more_mul_factor, op_lim_bk->min_sys_clk_div);
2339e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n",
2349e05bbacSSakari Ailus 		more_mul_factor);
2359e05bbacSSakari Ailus 	i = roundup(more_mul_min, more_mul_factor);
2369e05bbacSSakari Ailus 	if (!is_one_or_even(i))
2379e05bbacSSakari Ailus 		i <<= 1;
2389e05bbacSSakari Ailus 
2399e05bbacSSakari Ailus 	dev_dbg(dev, "final more_mul: %u\n", i);
2409e05bbacSSakari Ailus 	if (i > more_mul_max) {
2419e05bbacSSakari Ailus 		dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max);
2429e05bbacSSakari Ailus 		return -EINVAL;
2439e05bbacSSakari Ailus 	}
2449e05bbacSSakari Ailus 
245415ddd99SSakari Ailus 	op_pll_fr->pll_multiplier = mul * i;
246415ddd99SSakari Ailus 	op_pll_bk->sys_clk_div = div * i / op_pll_fr->pre_pll_clk_div;
247415ddd99SSakari Ailus 	dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll_bk->sys_clk_div);
2489e05bbacSSakari Ailus 
249415ddd99SSakari Ailus 	op_pll_fr->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz
250415ddd99SSakari Ailus 		/ op_pll_fr->pre_pll_clk_div;
2519e05bbacSSakari Ailus 
252415ddd99SSakari Ailus 	op_pll_fr->pll_op_clk_freq_hz = op_pll_fr->pll_ip_clk_freq_hz
253415ddd99SSakari Ailus 		* op_pll_fr->pll_multiplier;
2549e05bbacSSakari Ailus 
255415ddd99SSakari Ailus 	op_pll_bk->pix_clk_div = pll->bits_per_pixel;
256415ddd99SSakari Ailus 	dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll_bk->pix_clk_div);
2579e05bbacSSakari Ailus 
258415ddd99SSakari Ailus 	op_pll_bk->pix_clk_freq_hz =
259415ddd99SSakari Ailus 		op_pll_bk->sys_clk_freq_hz / op_pll_bk->pix_clk_div;
2609e05bbacSSakari Ailus 
2619e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
2629e05bbacSSakari Ailus 		/* No OP clocks --- VT clocks are used instead. */
2639e05bbacSSakari Ailus 		goto out_skip_vt_calc;
2649e05bbacSSakari Ailus 	}
2659e05bbacSSakari Ailus 
2669e05bbacSSakari Ailus 	/*
2679e05bbacSSakari Ailus 	 * Some sensors perform analogue binning and some do this
2689e05bbacSSakari Ailus 	 * digitally. The ones doing this digitally can be roughly be
2699e05bbacSSakari Ailus 	 * found out using this formula. The ones doing this digitally
2709e05bbacSSakari Ailus 	 * should run at higher clock rate, so smaller divisor is used
2719e05bbacSSakari Ailus 	 * on video timing side.
2729e05bbacSSakari Ailus 	 */
273415ddd99SSakari Ailus 	if (lim->min_line_length_pck_bin > lim->min_line_length_pck
2749e05bbacSSakari Ailus 	    / pll->binning_horizontal)
2759e05bbacSSakari Ailus 		vt_op_binning_div = pll->binning_horizontal;
2769e05bbacSSakari Ailus 	else
2779e05bbacSSakari Ailus 		vt_op_binning_div = 1;
2789e05bbacSSakari Ailus 	dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div);
2799e05bbacSSakari Ailus 
2809e05bbacSSakari Ailus 	/*
2819e05bbacSSakari Ailus 	 * Profile 2 supports vt_pix_clk_div E [4, 10]
2829e05bbacSSakari Ailus 	 *
2839e05bbacSSakari Ailus 	 * Horizontal binning can be used as a base for difference in
2849e05bbacSSakari Ailus 	 * divisors. One must make sure that horizontal blanking is
2859e05bbacSSakari Ailus 	 * enough to accommodate the CSI-2 sync codes.
2869e05bbacSSakari Ailus 	 *
2879e05bbacSSakari Ailus 	 * Take scaling factor into account as well.
2889e05bbacSSakari Ailus 	 *
2899e05bbacSSakari Ailus 	 * Find absolute limits for the factor of vt divider.
2909e05bbacSSakari Ailus 	 */
2919e05bbacSSakari Ailus 	dev_dbg(dev, "scale_m: %u\n", pll->scale_m);
292415ddd99SSakari Ailus 	min_vt_div = DIV_ROUND_UP(op_pll_bk->pix_clk_div
293415ddd99SSakari Ailus 				  * op_pll_bk->sys_clk_div * pll->scale_n,
2949e05bbacSSakari Ailus 				  lane_op_clock_ratio * vt_op_binning_div
2959e05bbacSSakari Ailus 				  * pll->scale_m);
2969e05bbacSSakari Ailus 
2979e05bbacSSakari Ailus 	/* Find smallest and biggest allowed vt divisor. */
2989e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: %u\n", min_vt_div);
2999e05bbacSSakari Ailus 	min_vt_div = max(min_vt_div,
300415ddd99SSakari Ailus 			 DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
301415ddd99SSakari Ailus 				      lim->vt_bk.max_pix_clk_freq_hz));
3029e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n",
3039e05bbacSSakari Ailus 		min_vt_div);
3049e05bbacSSakari Ailus 	min_vt_div = max_t(uint32_t, min_vt_div,
305415ddd99SSakari Ailus 			   lim->vt_bk.min_pix_clk_div
306415ddd99SSakari Ailus 			   * lim->vt_bk.min_sys_clk_div);
3079e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div);
3089e05bbacSSakari Ailus 
309415ddd99SSakari Ailus 	max_vt_div = lim->vt_bk.max_sys_clk_div * lim->vt_bk.max_pix_clk_div;
3109e05bbacSSakari Ailus 	dev_dbg(dev, "max_vt_div: %u\n", max_vt_div);
3119e05bbacSSakari Ailus 	max_vt_div = min(max_vt_div,
312415ddd99SSakari Ailus 			 DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
313415ddd99SSakari Ailus 				      lim->vt_bk.min_pix_clk_freq_hz));
3149e05bbacSSakari Ailus 	dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n",
3159e05bbacSSakari Ailus 		max_vt_div);
3169e05bbacSSakari Ailus 
3179e05bbacSSakari Ailus 	/*
3189e05bbacSSakari Ailus 	 * Find limitsits for sys_clk_div. Not all values are possible
3199e05bbacSSakari Ailus 	 * with all values of pix_clk_div.
3209e05bbacSSakari Ailus 	 */
321415ddd99SSakari Ailus 	min_sys_div = lim->vt_bk.min_sys_clk_div;
3229e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: %u\n", min_sys_div);
3239e05bbacSSakari Ailus 	min_sys_div = max(min_sys_div,
3249e05bbacSSakari Ailus 			  DIV_ROUND_UP(min_vt_div,
325415ddd99SSakari Ailus 				       lim->vt_bk.max_pix_clk_div));
3269e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", min_sys_div);
3279e05bbacSSakari Ailus 	min_sys_div = max(min_sys_div,
328415ddd99SSakari Ailus 			  op_pll_fr->pll_op_clk_freq_hz
329415ddd99SSakari Ailus 			  / lim->vt_bk.max_sys_clk_freq_hz);
3309e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", min_sys_div);
3319e05bbacSSakari Ailus 	min_sys_div = clk_div_even_up(min_sys_div);
3329e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: one or even: %u\n", min_sys_div);
3339e05bbacSSakari Ailus 
334415ddd99SSakari Ailus 	max_sys_div = lim->vt_bk.max_sys_clk_div;
3359e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: %u\n", max_sys_div);
3369e05bbacSSakari Ailus 	max_sys_div = min(max_sys_div,
3379e05bbacSSakari Ailus 			  DIV_ROUND_UP(max_vt_div,
338415ddd99SSakari Ailus 				       lim->vt_bk.min_pix_clk_div));
3399e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", max_sys_div);
3409e05bbacSSakari Ailus 	max_sys_div = min(max_sys_div,
341415ddd99SSakari Ailus 			  DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
342415ddd99SSakari Ailus 				       lim->vt_bk.min_pix_clk_freq_hz));
3439e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", max_sys_div);
3449e05bbacSSakari Ailus 
3459e05bbacSSakari Ailus 	/*
3469e05bbacSSakari Ailus 	 * Find pix_div such that a legal pix_div * sys_div results
3479e05bbacSSakari Ailus 	 * into a value which is not smaller than div, the desired
3489e05bbacSSakari Ailus 	 * divisor.
3499e05bbacSSakari Ailus 	 */
3509e05bbacSSakari Ailus 	for (vt_div = min_vt_div; vt_div <= max_vt_div;
3519e05bbacSSakari Ailus 	     vt_div += 2 - (vt_div & 1)) {
3529e05bbacSSakari Ailus 		for (sys_div = min_sys_div;
3539e05bbacSSakari Ailus 		     sys_div <= max_sys_div;
3549e05bbacSSakari Ailus 		     sys_div += 2 - (sys_div & 1)) {
3559e05bbacSSakari Ailus 			uint16_t pix_div = DIV_ROUND_UP(vt_div, sys_div);
3569c1a0d9eSSakari Ailus 			uint16_t rounded_div;
3579e05bbacSSakari Ailus 
358415ddd99SSakari Ailus 			if (pix_div < lim->vt_bk.min_pix_clk_div
359415ddd99SSakari Ailus 			    || pix_div > lim->vt_bk.max_pix_clk_div) {
3609e05bbacSSakari Ailus 				dev_dbg(dev,
3619e05bbacSSakari Ailus 					"pix_div %u too small or too big (%u--%u)\n",
3629e05bbacSSakari Ailus 					pix_div,
363415ddd99SSakari Ailus 					lim->vt_bk.min_pix_clk_div,
364415ddd99SSakari Ailus 					lim->vt_bk.max_pix_clk_div);
3659e05bbacSSakari Ailus 				continue;
3669e05bbacSSakari Ailus 			}
3679e05bbacSSakari Ailus 
3689c1a0d9eSSakari Ailus 			rounded_div = roundup(vt_div, best_pix_div);
3699c1a0d9eSSakari Ailus 
3709e05bbacSSakari Ailus 			/* Check if this one is better. */
3719c1a0d9eSSakari Ailus 			if (pix_div * sys_div <= rounded_div)
3729e05bbacSSakari Ailus 				best_pix_div = pix_div;
3739c1a0d9eSSakari Ailus 
3749c1a0d9eSSakari Ailus 			/* Bail out if we've already found the best value. */
3759c1a0d9eSSakari Ailus 			if (vt_div == rounded_div)
3769c1a0d9eSSakari Ailus 				break;
3779e05bbacSSakari Ailus 		}
3789e05bbacSSakari Ailus 		if (best_pix_div < INT_MAX >> 1)
3799e05bbacSSakari Ailus 			break;
3809e05bbacSSakari Ailus 	}
3819e05bbacSSakari Ailus 
3829454432aSSakari Ailus 	pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div);
383415ddd99SSakari Ailus 	pll->vt_bk.pix_clk_div = best_pix_div;
3849e05bbacSSakari Ailus 
385415ddd99SSakari Ailus 	pll->vt_bk.sys_clk_freq_hz =
386415ddd99SSakari Ailus 		op_pll_fr->pll_op_clk_freq_hz / pll->vt_bk.sys_clk_div;
387415ddd99SSakari Ailus 	pll->vt_bk.pix_clk_freq_hz =
388415ddd99SSakari Ailus 		pll->vt_bk.sys_clk_freq_hz / pll->vt_bk.pix_clk_div;
3899e05bbacSSakari Ailus 
3909e05bbacSSakari Ailus out_skip_vt_calc:
3919e05bbacSSakari Ailus 	pll->pixel_rate_csi =
392415ddd99SSakari Ailus 		op_pll_bk->pix_clk_freq_hz * lane_op_clock_ratio;
393415ddd99SSakari Ailus 	pll->pixel_rate_pixel_array = pll->vt_bk.pix_clk_freq_hz;
3949e05bbacSSakari Ailus 
395415ddd99SSakari Ailus 	return check_all_bounds(dev, lim, op_lim_fr, op_lim_bk, pll, op_pll_fr,
396415ddd99SSakari Ailus 				op_pll_bk);
3979e05bbacSSakari Ailus }
3989e05bbacSSakari Ailus 
399415ddd99SSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
4009e05bbacSSakari Ailus 		      struct ccs_pll *pll)
4019e05bbacSSakari Ailus {
402415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_fr *op_lim_fr = &lim->vt_fr;
403415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_bk *op_lim_bk = &lim->op_bk;
404415ddd99SSakari Ailus 	struct ccs_pll_branch_fr *op_pll_fr = &pll->vt_fr;
405415ddd99SSakari Ailus 	struct ccs_pll_branch_bk *op_pll_bk = &pll->op_bk;
406415ddd99SSakari Ailus 	uint16_t min_op_pre_pll_clk_div;
407415ddd99SSakari Ailus 	uint16_t max_op_pre_pll_clk_div;
4089e05bbacSSakari Ailus 	uint32_t lane_op_clock_ratio;
4099e05bbacSSakari Ailus 	uint32_t mul, div;
4109e05bbacSSakari Ailus 	unsigned int i;
4119e05bbacSSakari Ailus 	int rval = -EINVAL;
4129e05bbacSSakari Ailus 
4139e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
4149e05bbacSSakari Ailus 		/*
4159e05bbacSSakari Ailus 		 * If there's no OP PLL at all, use the VT values
4169e05bbacSSakari Ailus 		 * instead. The OP values are ignored for the rest of
4179e05bbacSSakari Ailus 		 * the PLL calculation.
4189e05bbacSSakari Ailus 		 */
419415ddd99SSakari Ailus 		op_lim_fr = &lim->vt_fr;
420415ddd99SSakari Ailus 		op_lim_bk = &lim->vt_bk;
421415ddd99SSakari Ailus 		op_pll_bk = &pll->vt_bk;
4229e05bbacSSakari Ailus 	}
4239e05bbacSSakari Ailus 
4249e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_OP_PIX_CLOCK_PER_LANE)
4259e05bbacSSakari Ailus 		lane_op_clock_ratio = pll->csi2.lanes;
4269e05bbacSSakari Ailus 	else
4279e05bbacSSakari Ailus 		lane_op_clock_ratio = 1;
4289e05bbacSSakari Ailus 	dev_dbg(dev, "lane_op_clock_ratio: %u\n", lane_op_clock_ratio);
4299e05bbacSSakari Ailus 
4309e05bbacSSakari Ailus 	dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal,
4319e05bbacSSakari Ailus 		pll->binning_vertical);
4329e05bbacSSakari Ailus 
4339e05bbacSSakari Ailus 	switch (pll->bus_type) {
43447b6eaf3SSakari Ailus 	case CCS_PLL_BUS_TYPE_CSI2_DPHY:
4359e05bbacSSakari Ailus 		/* CSI transfers 2 bits per clock per lane; thus times 2 */
436cab27256SSakari Ailus 		op_pll_bk->sys_clk_freq_hz = pll->link_freq * 2
4379e05bbacSSakari Ailus 			* (pll->csi2.lanes / lane_op_clock_ratio);
4389e05bbacSSakari Ailus 		break;
4399e05bbacSSakari Ailus 	default:
4409e05bbacSSakari Ailus 		return -EINVAL;
4419e05bbacSSakari Ailus 	}
4429e05bbacSSakari Ailus 
443415ddd99SSakari Ailus 	/* Figure out limits for OP pre-pll divider based on extclk */
444415ddd99SSakari Ailus 	dev_dbg(dev, "min / max op_pre_pll_clk_div: %u / %u\n",
445415ddd99SSakari Ailus 		op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div);
446415ddd99SSakari Ailus 	max_op_pre_pll_clk_div =
447415ddd99SSakari Ailus 		min_t(uint16_t, op_lim_fr->max_pre_pll_clk_div,
4489e05bbacSSakari Ailus 		      clk_div_even(pll->ext_clk_freq_hz /
449415ddd99SSakari Ailus 				   op_lim_fr->min_pll_ip_clk_freq_hz));
450415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
451415ddd99SSakari Ailus 		max_t(uint16_t, op_lim_fr->min_pre_pll_clk_div,
4529e05bbacSSakari Ailus 		      clk_div_even_up(
4539e05bbacSSakari Ailus 			      DIV_ROUND_UP(pll->ext_clk_freq_hz,
454415ddd99SSakari Ailus 					   op_lim_fr->max_pll_ip_clk_freq_hz)));
455415ddd99SSakari Ailus 	dev_dbg(dev, "pre-pll check: min / max op_pre_pll_clk_div: %u / %u\n",
456415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4579e05bbacSSakari Ailus 
458cab27256SSakari Ailus 	i = gcd(op_pll_bk->sys_clk_freq_hz, pll->ext_clk_freq_hz);
459cab27256SSakari Ailus 	mul = op_pll_bk->sys_clk_freq_hz / i;
4609e05bbacSSakari Ailus 	div = pll->ext_clk_freq_hz / i;
4619e05bbacSSakari Ailus 	dev_dbg(dev, "mul %u / div %u\n", mul, div);
4629e05bbacSSakari Ailus 
463415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
464415ddd99SSakari Ailus 		max_t(uint16_t, min_op_pre_pll_clk_div,
4659e05bbacSSakari Ailus 		      clk_div_even_up(
466*482e75e7SSakari Ailus 			      mul /
467*482e75e7SSakari Ailus 			      one_or_more(
468*482e75e7SSakari Ailus 				      DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz,
469*482e75e7SSakari Ailus 						   pll->ext_clk_freq_hz))));
470415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n",
471415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4729e05bbacSSakari Ailus 
473415ddd99SSakari Ailus 	for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div;
474415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div;
475415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div += 2 - (op_pll_fr->pre_pll_clk_div & 1)) {
476415ddd99SSakari Ailus 		rval = __ccs_pll_calculate(dev, lim, op_lim_fr, op_lim_bk, pll,
477415ddd99SSakari Ailus 					   op_pll_fr, op_pll_bk, mul, div,
478415ddd99SSakari Ailus 					   lane_op_clock_ratio);
4799e05bbacSSakari Ailus 		if (rval)
4809e05bbacSSakari Ailus 			continue;
4819e05bbacSSakari Ailus 
4829e05bbacSSakari Ailus 		print_pll(dev, pll);
4839e05bbacSSakari Ailus 		return 0;
4849e05bbacSSakari Ailus 	}
4859e05bbacSSakari Ailus 
4869e05bbacSSakari Ailus 	dev_dbg(dev, "unable to compute pre_pll divisor\n");
4879e05bbacSSakari Ailus 
4889e05bbacSSakari Ailus 	return rval;
4899e05bbacSSakari Ailus }
4909e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate);
4919e05bbacSSakari Ailus 
4927389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
4939e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator");
494b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2");
495