xref: /openbmc/linux/drivers/media/i2c/ccs-pll.c (revision c4c0b222720d413cc866275a0200019eb3c58f33)
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,
165*c4c0b222SSakari Ailus 		    uint32_t div, uint32_t l)
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;
180e583e654SSakari 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
197ae502e08SSakari Ailus 		      / (pll->ext_clk_freq_hz /
198ae502e08SSakari Ailus 			 op_pll_fr->pre_pll_clk_div * mul));
199415ddd99SSakari Ailus 	dev_dbg(dev, "more_mul_max: max_pll_op_clk_freq_hz check: %u\n",
2009e05bbacSSakari Ailus 		more_mul_max);
2019e05bbacSSakari Ailus 	/* Don't go above the division capability of op sys clock divider. */
2029e05bbacSSakari Ailus 	more_mul_max = min(more_mul_max,
203415ddd99SSakari Ailus 			   op_lim_bk->max_sys_clk_div * op_pll_fr->pre_pll_clk_div
2049e05bbacSSakari Ailus 			   / div);
2059e05bbacSSakari Ailus 	dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n",
2069e05bbacSSakari Ailus 		more_mul_max);
207c64cf71dSSakari Ailus 	/* Ensure we won't go above max_pll_multiplier. */
20882ab97c8SSakari Ailus 	more_mul_max = min(more_mul_max, 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 
255*c4c0b222SSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL)
256cac8f5d2SSakari Ailus 		op_pll_bk->pix_clk_div = pll->bits_per_pixel
257*c4c0b222SSakari Ailus 			* pll->op_lanes / pll->csi2.lanes / l;
258*c4c0b222SSakari Ailus 	else
259*c4c0b222SSakari Ailus 		op_pll_bk->pix_clk_div = pll->bits_per_pixel / l;
260*c4c0b222SSakari Ailus 
261415ddd99SSakari Ailus 	op_pll_bk->pix_clk_freq_hz =
262415ddd99SSakari Ailus 		op_pll_bk->sys_clk_freq_hz / op_pll_bk->pix_clk_div;
263*c4c0b222SSakari Ailus 
264cac8f5d2SSakari Ailus 	dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll_bk->pix_clk_div);
265cac8f5d2SSakari Ailus 
2669e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
2679e05bbacSSakari Ailus 		/* No OP clocks --- VT clocks are used instead. */
2689e05bbacSSakari Ailus 		goto out_skip_vt_calc;
2699e05bbacSSakari Ailus 	}
2709e05bbacSSakari Ailus 
2719e05bbacSSakari Ailus 	/*
2729e05bbacSSakari Ailus 	 * Some sensors perform analogue binning and some do this
2739e05bbacSSakari Ailus 	 * digitally. The ones doing this digitally can be roughly be
2749e05bbacSSakari Ailus 	 * found out using this formula. The ones doing this digitally
2759e05bbacSSakari Ailus 	 * should run at higher clock rate, so smaller divisor is used
2769e05bbacSSakari Ailus 	 * on video timing side.
2779e05bbacSSakari Ailus 	 */
278415ddd99SSakari Ailus 	if (lim->min_line_length_pck_bin > lim->min_line_length_pck
2799e05bbacSSakari Ailus 	    / pll->binning_horizontal)
2809e05bbacSSakari Ailus 		vt_op_binning_div = pll->binning_horizontal;
2819e05bbacSSakari Ailus 	else
2829e05bbacSSakari Ailus 		vt_op_binning_div = 1;
2839e05bbacSSakari Ailus 	dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div);
2849e05bbacSSakari Ailus 
2859e05bbacSSakari Ailus 	/*
2869e05bbacSSakari Ailus 	 * Profile 2 supports vt_pix_clk_div E [4, 10]
2879e05bbacSSakari Ailus 	 *
2889e05bbacSSakari Ailus 	 * Horizontal binning can be used as a base for difference in
2899e05bbacSSakari Ailus 	 * divisors. One must make sure that horizontal blanking is
2909e05bbacSSakari Ailus 	 * enough to accommodate the CSI-2 sync codes.
2919e05bbacSSakari Ailus 	 *
292cac8f5d2SSakari Ailus 	 * Take scaling factor and number of VT lanes into account as well.
2939e05bbacSSakari Ailus 	 *
2949e05bbacSSakari Ailus 	 * Find absolute limits for the factor of vt divider.
2959e05bbacSSakari Ailus 	 */
2969e05bbacSSakari Ailus 	dev_dbg(dev, "scale_m: %u\n", pll->scale_m);
297415ddd99SSakari Ailus 	min_vt_div = DIV_ROUND_UP(op_pll_bk->pix_clk_div
298cac8f5d2SSakari Ailus 				  * op_pll_bk->sys_clk_div * pll->scale_n
299*c4c0b222SSakari Ailus 				  * pll->vt_lanes * l,
300cac8f5d2SSakari Ailus 				  pll->op_lanes * vt_op_binning_div
301ae502e08SSakari Ailus 				  * pll->scale_m);
3029e05bbacSSakari Ailus 
3039e05bbacSSakari Ailus 	/* Find smallest and biggest allowed vt divisor. */
3049e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: %u\n", min_vt_div);
3059e05bbacSSakari Ailus 	min_vt_div = max(min_vt_div,
306415ddd99SSakari Ailus 			 DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
307415ddd99SSakari Ailus 				      lim->vt_bk.max_pix_clk_freq_hz));
3089e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n",
3099e05bbacSSakari Ailus 		min_vt_div);
3109e05bbacSSakari Ailus 	min_vt_div = max_t(uint32_t, min_vt_div,
311415ddd99SSakari Ailus 			   lim->vt_bk.min_pix_clk_div
312415ddd99SSakari Ailus 			   * lim->vt_bk.min_sys_clk_div);
3139e05bbacSSakari Ailus 	dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div);
3149e05bbacSSakari Ailus 
315415ddd99SSakari Ailus 	max_vt_div = lim->vt_bk.max_sys_clk_div * lim->vt_bk.max_pix_clk_div;
3169e05bbacSSakari Ailus 	dev_dbg(dev, "max_vt_div: %u\n", max_vt_div);
3179e05bbacSSakari Ailus 	max_vt_div = min(max_vt_div,
318415ddd99SSakari Ailus 			 DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
319415ddd99SSakari Ailus 				      lim->vt_bk.min_pix_clk_freq_hz));
3209e05bbacSSakari Ailus 	dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n",
3219e05bbacSSakari Ailus 		max_vt_div);
3229e05bbacSSakari Ailus 
3239e05bbacSSakari Ailus 	/*
3249e05bbacSSakari Ailus 	 * Find limitsits for sys_clk_div. Not all values are possible
3259e05bbacSSakari Ailus 	 * with all values of pix_clk_div.
3269e05bbacSSakari Ailus 	 */
327415ddd99SSakari Ailus 	min_sys_div = lim->vt_bk.min_sys_clk_div;
3289e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: %u\n", min_sys_div);
3299e05bbacSSakari Ailus 	min_sys_div = max(min_sys_div,
3309e05bbacSSakari Ailus 			  DIV_ROUND_UP(min_vt_div,
331415ddd99SSakari Ailus 				       lim->vt_bk.max_pix_clk_div));
3329e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", min_sys_div);
3339e05bbacSSakari Ailus 	min_sys_div = max(min_sys_div,
334415ddd99SSakari Ailus 			  op_pll_fr->pll_op_clk_freq_hz
335415ddd99SSakari Ailus 			  / lim->vt_bk.max_sys_clk_freq_hz);
3369e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", min_sys_div);
3379e05bbacSSakari Ailus 	min_sys_div = clk_div_even_up(min_sys_div);
3389e05bbacSSakari Ailus 	dev_dbg(dev, "min_sys_div: one or even: %u\n", min_sys_div);
3399e05bbacSSakari Ailus 
340415ddd99SSakari Ailus 	max_sys_div = lim->vt_bk.max_sys_clk_div;
3419e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: %u\n", max_sys_div);
3429e05bbacSSakari Ailus 	max_sys_div = min(max_sys_div,
3439e05bbacSSakari Ailus 			  DIV_ROUND_UP(max_vt_div,
344415ddd99SSakari Ailus 				       lim->vt_bk.min_pix_clk_div));
3459e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", max_sys_div);
3469e05bbacSSakari Ailus 	max_sys_div = min(max_sys_div,
347415ddd99SSakari Ailus 			  DIV_ROUND_UP(op_pll_fr->pll_op_clk_freq_hz,
348415ddd99SSakari Ailus 				       lim->vt_bk.min_pix_clk_freq_hz));
3499e05bbacSSakari Ailus 	dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", max_sys_div);
3509e05bbacSSakari Ailus 
3519e05bbacSSakari Ailus 	/*
3529e05bbacSSakari Ailus 	 * Find pix_div such that a legal pix_div * sys_div results
3539e05bbacSSakari Ailus 	 * into a value which is not smaller than div, the desired
3549e05bbacSSakari Ailus 	 * divisor.
3559e05bbacSSakari Ailus 	 */
3569e05bbacSSakari Ailus 	for (vt_div = min_vt_div; vt_div <= max_vt_div;
3579e05bbacSSakari Ailus 	     vt_div += 2 - (vt_div & 1)) {
3589e05bbacSSakari Ailus 		for (sys_div = min_sys_div;
3599e05bbacSSakari Ailus 		     sys_div <= max_sys_div;
3609e05bbacSSakari Ailus 		     sys_div += 2 - (sys_div & 1)) {
3619e05bbacSSakari Ailus 			uint16_t pix_div = DIV_ROUND_UP(vt_div, sys_div);
3629c1a0d9eSSakari Ailus 			uint16_t rounded_div;
3639e05bbacSSakari Ailus 
364415ddd99SSakari Ailus 			if (pix_div < lim->vt_bk.min_pix_clk_div
365415ddd99SSakari Ailus 			    || pix_div > lim->vt_bk.max_pix_clk_div) {
3669e05bbacSSakari Ailus 				dev_dbg(dev,
3679e05bbacSSakari Ailus 					"pix_div %u too small or too big (%u--%u)\n",
3689e05bbacSSakari Ailus 					pix_div,
369415ddd99SSakari Ailus 					lim->vt_bk.min_pix_clk_div,
370415ddd99SSakari Ailus 					lim->vt_bk.max_pix_clk_div);
3719e05bbacSSakari Ailus 				continue;
3729e05bbacSSakari Ailus 			}
3739e05bbacSSakari Ailus 
3749c1a0d9eSSakari Ailus 			rounded_div = roundup(vt_div, best_pix_div);
3759c1a0d9eSSakari Ailus 
3769e05bbacSSakari Ailus 			/* Check if this one is better. */
3779c1a0d9eSSakari Ailus 			if (pix_div * sys_div <= rounded_div)
3789e05bbacSSakari Ailus 				best_pix_div = pix_div;
3799c1a0d9eSSakari Ailus 
3809c1a0d9eSSakari Ailus 			/* Bail out if we've already found the best value. */
3819c1a0d9eSSakari Ailus 			if (vt_div == rounded_div)
3829c1a0d9eSSakari Ailus 				break;
3839e05bbacSSakari Ailus 		}
3849e05bbacSSakari Ailus 		if (best_pix_div < INT_MAX >> 1)
3859e05bbacSSakari Ailus 			break;
3869e05bbacSSakari Ailus 	}
3879e05bbacSSakari Ailus 
3889454432aSSakari Ailus 	pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div);
389415ddd99SSakari Ailus 	pll->vt_bk.pix_clk_div = best_pix_div;
3909e05bbacSSakari Ailus 
391415ddd99SSakari Ailus 	pll->vt_bk.sys_clk_freq_hz =
392415ddd99SSakari Ailus 		op_pll_fr->pll_op_clk_freq_hz / pll->vt_bk.sys_clk_div;
393415ddd99SSakari Ailus 	pll->vt_bk.pix_clk_freq_hz =
394415ddd99SSakari Ailus 		pll->vt_bk.sys_clk_freq_hz / pll->vt_bk.pix_clk_div;
3959e05bbacSSakari Ailus 
3969e05bbacSSakari Ailus out_skip_vt_calc:
397cac8f5d2SSakari Ailus 	pll->pixel_rate_pixel_array =
398cac8f5d2SSakari Ailus 		pll->vt_bk.pix_clk_freq_hz * pll->vt_lanes;
3999e05bbacSSakari Ailus 
400415ddd99SSakari Ailus 	return check_all_bounds(dev, lim, op_lim_fr, op_lim_bk, pll, op_pll_fr,
401415ddd99SSakari Ailus 				op_pll_bk);
4029e05bbacSSakari Ailus }
4039e05bbacSSakari Ailus 
404415ddd99SSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
4059e05bbacSSakari Ailus 		      struct ccs_pll *pll)
4069e05bbacSSakari Ailus {
407415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_fr *op_lim_fr = &lim->vt_fr;
408415ddd99SSakari Ailus 	const struct ccs_pll_branch_limits_bk *op_lim_bk = &lim->op_bk;
409415ddd99SSakari Ailus 	struct ccs_pll_branch_fr *op_pll_fr = &pll->vt_fr;
410415ddd99SSakari Ailus 	struct ccs_pll_branch_bk *op_pll_bk = &pll->op_bk;
411415ddd99SSakari Ailus 	uint16_t min_op_pre_pll_clk_div;
412415ddd99SSakari Ailus 	uint16_t max_op_pre_pll_clk_div;
4139e05bbacSSakari Ailus 	uint32_t mul, div;
414*c4c0b222SSakari Ailus 	uint32_t l = (!pll->op_bits_per_lane ||
415*c4c0b222SSakari Ailus 		      pll->op_bits_per_lane >= pll->bits_per_pixel) ? 1 : 2;
416e583e654SSakari Ailus 	uint32_t i;
4179e05bbacSSakari Ailus 	int rval = -EINVAL;
4189e05bbacSSakari Ailus 
419cac8f5d2SSakari Ailus 	if (!(pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL)) {
420cac8f5d2SSakari Ailus 		pll->op_lanes = 1;
421cac8f5d2SSakari Ailus 		pll->vt_lanes = 1;
422cac8f5d2SSakari Ailus 	}
423cac8f5d2SSakari Ailus 	dev_dbg(dev, "vt_lanes: %u\n", pll->vt_lanes);
424cac8f5d2SSakari Ailus 	dev_dbg(dev, "op_lanes: %u\n", pll->op_lanes);
425cac8f5d2SSakari Ailus 
4269e05bbacSSakari Ailus 	if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) {
4279e05bbacSSakari Ailus 		/*
4289e05bbacSSakari Ailus 		 * If there's no OP PLL at all, use the VT values
4299e05bbacSSakari Ailus 		 * instead. The OP values are ignored for the rest of
4309e05bbacSSakari Ailus 		 * the PLL calculation.
4319e05bbacSSakari Ailus 		 */
432415ddd99SSakari Ailus 		op_lim_fr = &lim->vt_fr;
433415ddd99SSakari Ailus 		op_lim_bk = &lim->vt_bk;
434415ddd99SSakari Ailus 		op_pll_bk = &pll->vt_bk;
4359e05bbacSSakari Ailus 	}
4369e05bbacSSakari Ailus 
4379e05bbacSSakari Ailus 	dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal,
4389e05bbacSSakari Ailus 		pll->binning_vertical);
4399e05bbacSSakari Ailus 
4409e05bbacSSakari Ailus 	switch (pll->bus_type) {
44147b6eaf3SSakari Ailus 	case CCS_PLL_BUS_TYPE_CSI2_DPHY:
4429e05bbacSSakari Ailus 		/* CSI transfers 2 bits per clock per lane; thus times 2 */
443cab27256SSakari Ailus 		op_pll_bk->sys_clk_freq_hz = pll->link_freq * 2
444cac8f5d2SSakari Ailus 			* (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
445ae502e08SSakari Ailus 			   1 : pll->csi2.lanes);
4469e05bbacSSakari Ailus 		break;
4479e05bbacSSakari Ailus 	default:
4489e05bbacSSakari Ailus 		return -EINVAL;
4499e05bbacSSakari Ailus 	}
4509e05bbacSSakari Ailus 
451cac8f5d2SSakari Ailus 	pll->pixel_rate_csi =
452cac8f5d2SSakari Ailus 		op_pll_bk->pix_clk_freq_hz
453cac8f5d2SSakari Ailus 		* (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
454*c4c0b222SSakari Ailus 		   pll->csi2.lanes : 1) / l;
455cac8f5d2SSakari Ailus 
456415ddd99SSakari Ailus 	/* Figure out limits for OP pre-pll divider based on extclk */
457415ddd99SSakari Ailus 	dev_dbg(dev, "min / max op_pre_pll_clk_div: %u / %u\n",
458415ddd99SSakari Ailus 		op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div);
459415ddd99SSakari Ailus 	max_op_pre_pll_clk_div =
460415ddd99SSakari Ailus 		min_t(uint16_t, op_lim_fr->max_pre_pll_clk_div,
4619e05bbacSSakari Ailus 		      clk_div_even(pll->ext_clk_freq_hz /
462415ddd99SSakari Ailus 				   op_lim_fr->min_pll_ip_clk_freq_hz));
463415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
464415ddd99SSakari Ailus 		max_t(uint16_t, op_lim_fr->min_pre_pll_clk_div,
4659e05bbacSSakari Ailus 		      clk_div_even_up(
4669e05bbacSSakari Ailus 			      DIV_ROUND_UP(pll->ext_clk_freq_hz,
467415ddd99SSakari Ailus 					   op_lim_fr->max_pll_ip_clk_freq_hz)));
468415ddd99SSakari Ailus 	dev_dbg(dev, "pre-pll check: min / max op_pre_pll_clk_div: %u / %u\n",
469415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4709e05bbacSSakari Ailus 
471cab27256SSakari Ailus 	i = gcd(op_pll_bk->sys_clk_freq_hz, pll->ext_clk_freq_hz);
472cab27256SSakari Ailus 	mul = op_pll_bk->sys_clk_freq_hz / i;
4739e05bbacSSakari Ailus 	div = pll->ext_clk_freq_hz / i;
4749e05bbacSSakari Ailus 	dev_dbg(dev, "mul %u / div %u\n", mul, div);
4759e05bbacSSakari Ailus 
476415ddd99SSakari Ailus 	min_op_pre_pll_clk_div =
477415ddd99SSakari Ailus 		max_t(uint16_t, min_op_pre_pll_clk_div,
4789e05bbacSSakari Ailus 		      clk_div_even_up(
479482e75e7SSakari Ailus 			      mul /
480482e75e7SSakari Ailus 			      one_or_more(
481482e75e7SSakari Ailus 				      DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz,
482482e75e7SSakari Ailus 						   pll->ext_clk_freq_hz))));
483415ddd99SSakari Ailus 	dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n",
484415ddd99SSakari Ailus 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
4859e05bbacSSakari Ailus 
486415ddd99SSakari Ailus 	for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div;
487415ddd99SSakari Ailus 	     op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div;
4884e1e8d24SSakari Ailus 	     op_pll_fr->pre_pll_clk_div +=
4894e1e8d24SSakari Ailus 		     (pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER) ? 1 :
4904e1e8d24SSakari Ailus 		     2 - (op_pll_fr->pre_pll_clk_div & 1)) {
491415ddd99SSakari Ailus 		rval = __ccs_pll_calculate(dev, lim, op_lim_fr, op_lim_bk, pll,
492*c4c0b222SSakari Ailus 					   op_pll_fr, op_pll_bk, mul, div, l);
4939e05bbacSSakari Ailus 		if (rval)
4949e05bbacSSakari Ailus 			continue;
4959e05bbacSSakari Ailus 
4969e05bbacSSakari Ailus 		print_pll(dev, pll);
4979e05bbacSSakari Ailus 		return 0;
4989e05bbacSSakari Ailus 	}
4999e05bbacSSakari Ailus 
5009e05bbacSSakari Ailus 	dev_dbg(dev, "unable to compute pre_pll divisor\n");
5019e05bbacSSakari Ailus 
5029e05bbacSSakari Ailus 	return rval;
5039e05bbacSSakari Ailus }
5049e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate);
5059e05bbacSSakari Ailus 
5067389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
5079e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator");
508b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2");
509