xref: /openbmc/linux/drivers/media/i2c/ccs-pll.c (revision e583e654)
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,
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;
180*e583e654SSakari Ailus 	uint32_t 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);
206c64cf71dSSakari Ailus 	/* Ensure we won't go above max_pll_multiplier. */
20782ab97c8SSakari Ailus 	more_mul_max = min(more_mul_max, op_lim_fr->max_pll_multiplier / mul);
2089e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n",
2099e05bbacSSakari Ailus 		more_mul_max);
2109e05bbacSSakari Ailus 
211415ddd99SSakari Ailus 	/* Ensure we won't go below min_pll_op_clk_freq_hz. */
212415ddd99SSakari Ailus 	more_mul_min = DIV_ROUND_UP(op_lim_fr->min_pll_op_clk_freq_hz,
213415ddd99SSakari Ailus 				    pll->ext_clk_freq_hz /
214415ddd99SSakari Ailus 				    op_pll_fr->pre_pll_clk_div * mul);
215415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_op_clk_freq_hz check: %u\n",
2169e05bbacSSakari Ailus 		more_mul_min);
2179e05bbacSSakari Ailus 	/* Ensure we won't go below min_pll_multiplier. */
2189e05bbacSSakari Ailus 	more_mul_min = max(more_mul_min,
219415ddd99SSakari Ailus 			   DIV_ROUND_UP(op_lim_fr->min_pll_multiplier, mul));
220415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_min: min_op_pll_multiplier check: %u\n",
2219e05bbacSSakari Ailus 		more_mul_min);
2229e05bbacSSakari Ailus 
2239e05bbacSSakari Ailus 	if (more_mul_min > more_mul_max) {
2249e05bbacSSakari Ailus 		dev_dbg(dev,
2259e05bbacSSakari Ailus 			"unable to compute more_mul_min and more_mul_max\n");
2269e05bbacSSakari Ailus 		return -EINVAL;
2279e05bbacSSakari Ailus 	}
2289e05bbacSSakari Ailus 
229415ddd99SSakari Ailus 	more_mul_factor = lcm(div, op_pll_fr->pre_pll_clk_div) / div;
2309e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor);
231415ddd99SSakari Ailus 	more_mul_factor = lcm(more_mul_factor, op_lim_bk->min_sys_clk_div);
2329e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n",
2339e05bbacSSakari Ailus 		more_mul_factor);
2349e05bbacSSakari Ailus 	i = roundup(more_mul_min, more_mul_factor);
2359e05bbacSSakari Ailus 	if (!is_one_or_even(i))
2369e05bbacSSakari Ailus 		i <<= 1;
2379e05bbacSSakari Ailus 
2389e05bbacSSakari Ailus 	dev_dbg(dev, "final more_mul: %u\n", i);
2399e05bbacSSakari Ailus 	if (i > more_mul_max) {
2409e05bbacSSakari Ailus 		dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max);
2419e05bbacSSakari Ailus 		return -EINVAL;
2429e05bbacSSakari Ailus 	}
2439e05bbacSSakari Ailus 
244415ddd99SSakari Ailus 	op_pll_fr->pll_multiplier = mul * i;
245415ddd99SSakari Ailus 	op_pll_bk->sys_clk_div = div * i / op_pll_fr->pre_pll_clk_div;
246415ddd99SSakari Ailus 	dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll_bk->sys_clk_div);
2479e05bbacSSakari Ailus 
248415ddd99SSakari Ailus 	op_pll_fr->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz
249415ddd99SSakari Ailus 		/ op_pll_fr->pre_pll_clk_div;
2509e05bbacSSakari Ailus 
251415ddd99SSakari Ailus 	op_pll_fr->pll_op_clk_freq_hz = op_pll_fr->pll_ip_clk_freq_hz
252415ddd99SSakari Ailus 		* op_pll_fr->pll_multiplier;
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;
409*e583e654SSakari Ailus 	uint32_t 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) {
43347b6eaf3SSakari Ailus 	case CCS_PLL_BUS_TYPE_CSI2_DPHY:
4349e05bbacSSakari Ailus 		/* CSI transfers 2 bits per clock per lane; thus times 2 */
435cab27256SSakari Ailus 		op_pll_bk->sys_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 
457cab27256SSakari Ailus 	i = gcd(op_pll_bk->sys_clk_freq_hz, pll->ext_clk_freq_hz);
458cab27256SSakari Ailus 	mul = op_pll_bk->sys_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(
465482e75e7SSakari Ailus 			      mul /
466482e75e7SSakari Ailus 			      one_or_more(
467482e75e7SSakari Ailus 				      DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz,
468482e75e7SSakari Ailus 						   pll->ext_clk_freq_hz))));
469415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n",
470415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4719e05bbacSSakari Ailus 
472415ddd99SSakari Ailus 	for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div;
473415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div;
474415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div += 2 - (op_pll_fr->pre_pll_clk_div & 1)) {
475415ddd99SSakari Ailus 		rval = __ccs_pll_calculate(dev, lim, op_lim_fr, op_lim_bk, pll,
476415ddd99SSakari Ailus 					   op_pll_fr, op_pll_bk, mul, div,
477415ddd99SSakari Ailus 					   lane_op_clock_ratio);
4789e05bbacSSakari Ailus 		if (rval)
4799e05bbacSSakari Ailus 			continue;
4809e05bbacSSakari Ailus 
4819e05bbacSSakari Ailus 		print_pll(dev, pll);
4829e05bbacSSakari Ailus 		return 0;
4839e05bbacSSakari Ailus 	}
4849e05bbacSSakari Ailus 
4859e05bbacSSakari Ailus 	dev_dbg(dev, "unable to compute pre_pll divisor\n");
4869e05bbacSSakari Ailus 
4879e05bbacSSakari Ailus 	return rval;
4889e05bbacSSakari Ailus }
4899e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate);
4909e05bbacSSakari Ailus 
4917389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
4929e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator");
493b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2");
494