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 99e05bbacSSakari Ailus * Contact: Sakari Ailus <sakari.ailus@iki.fi> 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 { 569e05bbacSSakari Ailus dev_dbg(dev, "pre_pll_clk_div\t%u\n", pll->pre_pll_clk_div); 579e05bbacSSakari Ailus dev_dbg(dev, "pll_multiplier \t%u\n", pll->pll_multiplier); 589e05bbacSSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS)) { 599e05bbacSSakari Ailus dev_dbg(dev, "op_sys_clk_div \t%u\n", pll->op.sys_clk_div); 609e05bbacSSakari Ailus dev_dbg(dev, "op_pix_clk_div \t%u\n", pll->op.pix_clk_div); 619e05bbacSSakari Ailus } 629e05bbacSSakari Ailus dev_dbg(dev, "vt_sys_clk_div \t%u\n", pll->vt.sys_clk_div); 639e05bbacSSakari Ailus dev_dbg(dev, "vt_pix_clk_div \t%u\n", pll->vt.pix_clk_div); 649e05bbacSSakari Ailus 659e05bbacSSakari Ailus dev_dbg(dev, "ext_clk_freq_hz \t%u\n", pll->ext_clk_freq_hz); 669e05bbacSSakari Ailus dev_dbg(dev, "pll_ip_clk_freq_hz \t%u\n", pll->pll_ip_clk_freq_hz); 679e05bbacSSakari Ailus dev_dbg(dev, "pll_op_clk_freq_hz \t%u\n", pll->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", 709e05bbacSSakari Ailus pll->op.sys_clk_freq_hz); 719e05bbacSSakari Ailus dev_dbg(dev, "op_pix_clk_freq_hz \t%u\n", 729e05bbacSSakari Ailus pll->op.pix_clk_freq_hz); 739e05bbacSSakari Ailus } 749e05bbacSSakari Ailus dev_dbg(dev, "vt_sys_clk_freq_hz \t%u\n", pll->vt.sys_clk_freq_hz); 759e05bbacSSakari Ailus dev_dbg(dev, "vt_pix_clk_freq_hz \t%u\n", pll->vt.pix_clk_freq_hz); 769e05bbacSSakari Ailus } 779e05bbacSSakari Ailus 789e05bbacSSakari Ailus static int check_all_bounds(struct device *dev, 799e05bbacSSakari Ailus const struct ccs_pll_limits *limits, 809e05bbacSSakari Ailus const struct ccs_pll_branch_limits *op_limits, 819e05bbacSSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch *op_pll) 829e05bbacSSakari Ailus { 839e05bbacSSakari Ailus int rval; 849e05bbacSSakari Ailus 859e05bbacSSakari Ailus rval = bounds_check(dev, pll->pll_ip_clk_freq_hz, 869e05bbacSSakari Ailus limits->min_pll_ip_freq_hz, 879e05bbacSSakari Ailus limits->max_pll_ip_freq_hz, 889e05bbacSSakari Ailus "pll_ip_clk_freq_hz"); 899e05bbacSSakari Ailus if (!rval) 909e05bbacSSakari Ailus rval = bounds_check( 919e05bbacSSakari Ailus dev, pll->pll_multiplier, 929e05bbacSSakari Ailus limits->min_pll_multiplier, limits->max_pll_multiplier, 939e05bbacSSakari Ailus "pll_multiplier"); 949e05bbacSSakari Ailus if (!rval) 959e05bbacSSakari Ailus rval = bounds_check( 969e05bbacSSakari Ailus dev, pll->pll_op_clk_freq_hz, 979e05bbacSSakari Ailus limits->min_pll_op_freq_hz, limits->max_pll_op_freq_hz, 989e05bbacSSakari Ailus "pll_op_clk_freq_hz"); 999e05bbacSSakari Ailus if (!rval) 1009e05bbacSSakari Ailus rval = bounds_check( 1019e05bbacSSakari Ailus dev, op_pll->sys_clk_div, 1029e05bbacSSakari Ailus op_limits->min_sys_clk_div, op_limits->max_sys_clk_div, 1039e05bbacSSakari Ailus "op_sys_clk_div"); 1049e05bbacSSakari Ailus if (!rval) 1059e05bbacSSakari Ailus rval = bounds_check( 1069e05bbacSSakari Ailus dev, op_pll->sys_clk_freq_hz, 1079e05bbacSSakari Ailus op_limits->min_sys_clk_freq_hz, 1089e05bbacSSakari Ailus op_limits->max_sys_clk_freq_hz, 1099e05bbacSSakari Ailus "op_sys_clk_freq_hz"); 1109e05bbacSSakari Ailus if (!rval) 1119e05bbacSSakari Ailus rval = bounds_check( 1129e05bbacSSakari Ailus dev, op_pll->pix_clk_freq_hz, 1139e05bbacSSakari Ailus op_limits->min_pix_clk_freq_hz, 1149e05bbacSSakari Ailus op_limits->max_pix_clk_freq_hz, 1159e05bbacSSakari Ailus "op_pix_clk_freq_hz"); 1169e05bbacSSakari Ailus 1179e05bbacSSakari Ailus /* 1189e05bbacSSakari Ailus * If there are no OP clocks, the VT clocks are contained in 1199e05bbacSSakari Ailus * the OP clock struct. 1209e05bbacSSakari Ailus */ 1219e05bbacSSakari Ailus if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) 1229e05bbacSSakari Ailus return rval; 1239e05bbacSSakari Ailus 1249e05bbacSSakari Ailus if (!rval) 1259e05bbacSSakari Ailus rval = bounds_check( 1269e05bbacSSakari Ailus dev, pll->vt.sys_clk_freq_hz, 1279e05bbacSSakari Ailus limits->vt.min_sys_clk_freq_hz, 1289e05bbacSSakari Ailus limits->vt.max_sys_clk_freq_hz, 1299e05bbacSSakari Ailus "vt_sys_clk_freq_hz"); 1309e05bbacSSakari Ailus if (!rval) 1319e05bbacSSakari Ailus rval = bounds_check( 1329e05bbacSSakari Ailus dev, pll->vt.pix_clk_freq_hz, 1339e05bbacSSakari Ailus limits->vt.min_pix_clk_freq_hz, 1349e05bbacSSakari Ailus limits->vt.max_pix_clk_freq_hz, 1359e05bbacSSakari Ailus "vt_pix_clk_freq_hz"); 1369e05bbacSSakari Ailus 1379e05bbacSSakari Ailus return rval; 1389e05bbacSSakari Ailus } 1399e05bbacSSakari Ailus 1409e05bbacSSakari Ailus /* 1419e05bbacSSakari Ailus * Heuristically guess the PLL tree for a given common multiplier and 1429e05bbacSSakari Ailus * divisor. Begin with the operational timing and continue to video 1439e05bbacSSakari Ailus * timing once operational timing has been verified. 1449e05bbacSSakari Ailus * 1459e05bbacSSakari Ailus * @mul is the PLL multiplier and @div is the common divisor 1469e05bbacSSakari Ailus * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL 1479e05bbacSSakari Ailus * multiplier will be a multiple of @mul. 1489e05bbacSSakari Ailus * 1499e05bbacSSakari Ailus * @return Zero on success, error code on error. 1509e05bbacSSakari Ailus */ 1519e05bbacSSakari Ailus static int 1529e05bbacSSakari Ailus __ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *limits, 1539e05bbacSSakari Ailus const struct ccs_pll_branch_limits *op_limits, 1549e05bbacSSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch *op_pll, 1559e05bbacSSakari Ailus uint32_t mul, uint32_t div, uint32_t lane_op_clock_ratio) 1569e05bbacSSakari Ailus { 1579e05bbacSSakari Ailus uint32_t sys_div; 1589e05bbacSSakari Ailus uint32_t best_pix_div = INT_MAX >> 1; 1599e05bbacSSakari Ailus uint32_t vt_op_binning_div; 1609e05bbacSSakari Ailus /* 1619e05bbacSSakari Ailus * Higher multipliers (and divisors) are often required than 1629e05bbacSSakari Ailus * necessitated by the external clock and the output clocks. 1639e05bbacSSakari Ailus * There are limits for all values in the clock tree. These 1649e05bbacSSakari Ailus * are the minimum and maximum multiplier for mul. 1659e05bbacSSakari Ailus */ 1669e05bbacSSakari Ailus uint32_t more_mul_min, more_mul_max; 1679e05bbacSSakari Ailus uint32_t more_mul_factor; 1689e05bbacSSakari Ailus uint32_t min_vt_div, max_vt_div, vt_div; 1699e05bbacSSakari Ailus uint32_t min_sys_div, max_sys_div; 1709e05bbacSSakari Ailus unsigned int i; 1719e05bbacSSakari Ailus 1729e05bbacSSakari Ailus /* 1739e05bbacSSakari Ailus * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be 1749e05bbacSSakari Ailus * too high. 1759e05bbacSSakari Ailus */ 1769e05bbacSSakari Ailus dev_dbg(dev, "pre_pll_clk_div %u\n", pll->pre_pll_clk_div); 1779e05bbacSSakari Ailus 1789e05bbacSSakari Ailus /* Don't go above max pll multiplier. */ 1799e05bbacSSakari Ailus more_mul_max = limits->max_pll_multiplier / mul; 1809e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: max_pll_multiplier check: %u\n", 1819e05bbacSSakari Ailus more_mul_max); 1829e05bbacSSakari Ailus /* Don't go above max pll op frequency. */ 1839e05bbacSSakari Ailus more_mul_max = 1849e05bbacSSakari Ailus min_t(uint32_t, 1859e05bbacSSakari Ailus more_mul_max, 1869e05bbacSSakari Ailus limits->max_pll_op_freq_hz 1879e05bbacSSakari Ailus / (pll->ext_clk_freq_hz / pll->pre_pll_clk_div * mul)); 1889e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: max_pll_op_freq_hz check: %u\n", 1899e05bbacSSakari Ailus more_mul_max); 1909e05bbacSSakari Ailus /* Don't go above the division capability of op sys clock divider. */ 1919e05bbacSSakari Ailus more_mul_max = min(more_mul_max, 1929e05bbacSSakari Ailus op_limits->max_sys_clk_div * pll->pre_pll_clk_div 1939e05bbacSSakari Ailus / div); 1949e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n", 1959e05bbacSSakari Ailus more_mul_max); 1969e05bbacSSakari Ailus /* Ensure we won't go above min_pll_multiplier. */ 1979e05bbacSSakari Ailus more_mul_max = min(more_mul_max, 1989e05bbacSSakari Ailus DIV_ROUND_UP(limits->max_pll_multiplier, mul)); 1999e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n", 2009e05bbacSSakari Ailus more_mul_max); 2019e05bbacSSakari Ailus 2029e05bbacSSakari Ailus /* Ensure we won't go below min_pll_op_freq_hz. */ 2039e05bbacSSakari Ailus more_mul_min = DIV_ROUND_UP(limits->min_pll_op_freq_hz, 2049e05bbacSSakari Ailus pll->ext_clk_freq_hz / pll->pre_pll_clk_div 2059e05bbacSSakari Ailus * mul); 2069e05bbacSSakari Ailus dev_dbg(dev, "more_mul_min: min_pll_op_freq_hz check: %u\n", 2079e05bbacSSakari Ailus more_mul_min); 2089e05bbacSSakari Ailus /* Ensure we won't go below min_pll_multiplier. */ 2099e05bbacSSakari Ailus more_mul_min = max(more_mul_min, 2109e05bbacSSakari Ailus DIV_ROUND_UP(limits->min_pll_multiplier, mul)); 2119e05bbacSSakari Ailus dev_dbg(dev, "more_mul_min: min_pll_multiplier check: %u\n", 2129e05bbacSSakari Ailus more_mul_min); 2139e05bbacSSakari Ailus 2149e05bbacSSakari Ailus if (more_mul_min > more_mul_max) { 2159e05bbacSSakari Ailus dev_dbg(dev, 2169e05bbacSSakari Ailus "unable to compute more_mul_min and more_mul_max\n"); 2179e05bbacSSakari Ailus return -EINVAL; 2189e05bbacSSakari Ailus } 2199e05bbacSSakari Ailus 2209e05bbacSSakari Ailus more_mul_factor = lcm(div, pll->pre_pll_clk_div) / div; 2219e05bbacSSakari Ailus dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor); 2229e05bbacSSakari Ailus more_mul_factor = lcm(more_mul_factor, op_limits->min_sys_clk_div); 2239e05bbacSSakari Ailus dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n", 2249e05bbacSSakari Ailus more_mul_factor); 2259e05bbacSSakari Ailus i = roundup(more_mul_min, more_mul_factor); 2269e05bbacSSakari Ailus if (!is_one_or_even(i)) 2279e05bbacSSakari Ailus i <<= 1; 2289e05bbacSSakari Ailus 2299e05bbacSSakari Ailus dev_dbg(dev, "final more_mul: %u\n", i); 2309e05bbacSSakari Ailus if (i > more_mul_max) { 2319e05bbacSSakari Ailus dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max); 2329e05bbacSSakari Ailus return -EINVAL; 2339e05bbacSSakari Ailus } 2349e05bbacSSakari Ailus 2359e05bbacSSakari Ailus pll->pll_multiplier = mul * i; 2369e05bbacSSakari Ailus op_pll->sys_clk_div = div * i / pll->pre_pll_clk_div; 2379e05bbacSSakari Ailus dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll->sys_clk_div); 2389e05bbacSSakari Ailus 2399e05bbacSSakari Ailus pll->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz 2409e05bbacSSakari Ailus / pll->pre_pll_clk_div; 2419e05bbacSSakari Ailus 2429e05bbacSSakari Ailus pll->pll_op_clk_freq_hz = pll->pll_ip_clk_freq_hz 2439e05bbacSSakari Ailus * pll->pll_multiplier; 2449e05bbacSSakari Ailus 2459e05bbacSSakari Ailus /* Derive pll_op_clk_freq_hz. */ 2469e05bbacSSakari Ailus op_pll->sys_clk_freq_hz = 2479e05bbacSSakari Ailus pll->pll_op_clk_freq_hz / op_pll->sys_clk_div; 2489e05bbacSSakari Ailus 2499e05bbacSSakari Ailus op_pll->pix_clk_div = pll->bits_per_pixel; 2509e05bbacSSakari Ailus dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll->pix_clk_div); 2519e05bbacSSakari Ailus 2529e05bbacSSakari Ailus op_pll->pix_clk_freq_hz = 2539e05bbacSSakari Ailus op_pll->sys_clk_freq_hz / op_pll->pix_clk_div; 2549e05bbacSSakari Ailus 2559e05bbacSSakari Ailus if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) { 2569e05bbacSSakari Ailus /* No OP clocks --- VT clocks are used instead. */ 2579e05bbacSSakari Ailus goto out_skip_vt_calc; 2589e05bbacSSakari Ailus } 2599e05bbacSSakari Ailus 2609e05bbacSSakari Ailus /* 2619e05bbacSSakari Ailus * Some sensors perform analogue binning and some do this 2629e05bbacSSakari Ailus * digitally. The ones doing this digitally can be roughly be 2639e05bbacSSakari Ailus * found out using this formula. The ones doing this digitally 2649e05bbacSSakari Ailus * should run at higher clock rate, so smaller divisor is used 2659e05bbacSSakari Ailus * on video timing side. 2669e05bbacSSakari Ailus */ 2679e05bbacSSakari Ailus if (limits->min_line_length_pck_bin > limits->min_line_length_pck 2689e05bbacSSakari Ailus / pll->binning_horizontal) 2699e05bbacSSakari Ailus vt_op_binning_div = pll->binning_horizontal; 2709e05bbacSSakari Ailus else 2719e05bbacSSakari Ailus vt_op_binning_div = 1; 2729e05bbacSSakari Ailus dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div); 2739e05bbacSSakari Ailus 2749e05bbacSSakari Ailus /* 2759e05bbacSSakari Ailus * Profile 2 supports vt_pix_clk_div E [4, 10] 2769e05bbacSSakari Ailus * 2779e05bbacSSakari Ailus * Horizontal binning can be used as a base for difference in 2789e05bbacSSakari Ailus * divisors. One must make sure that horizontal blanking is 2799e05bbacSSakari Ailus * enough to accommodate the CSI-2 sync codes. 2809e05bbacSSakari Ailus * 2819e05bbacSSakari Ailus * Take scaling factor into account as well. 2829e05bbacSSakari Ailus * 2839e05bbacSSakari Ailus * Find absolute limits for the factor of vt divider. 2849e05bbacSSakari Ailus */ 2859e05bbacSSakari Ailus dev_dbg(dev, "scale_m: %u\n", pll->scale_m); 2869e05bbacSSakari Ailus min_vt_div = DIV_ROUND_UP(op_pll->pix_clk_div * op_pll->sys_clk_div 2879e05bbacSSakari Ailus * pll->scale_n, 2889e05bbacSSakari Ailus lane_op_clock_ratio * vt_op_binning_div 2899e05bbacSSakari Ailus * pll->scale_m); 2909e05bbacSSakari Ailus 2919e05bbacSSakari Ailus /* Find smallest and biggest allowed vt divisor. */ 2929e05bbacSSakari Ailus dev_dbg(dev, "min_vt_div: %u\n", min_vt_div); 2939e05bbacSSakari Ailus min_vt_div = max(min_vt_div, 2949e05bbacSSakari Ailus DIV_ROUND_UP(pll->pll_op_clk_freq_hz, 2959e05bbacSSakari Ailus limits->vt.max_pix_clk_freq_hz)); 2969e05bbacSSakari Ailus dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n", 2979e05bbacSSakari Ailus min_vt_div); 2989e05bbacSSakari Ailus min_vt_div = max_t(uint32_t, min_vt_div, 2999e05bbacSSakari Ailus limits->vt.min_pix_clk_div 3009e05bbacSSakari Ailus * limits->vt.min_sys_clk_div); 3019e05bbacSSakari Ailus dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div); 3029e05bbacSSakari Ailus 3039e05bbacSSakari Ailus max_vt_div = limits->vt.max_sys_clk_div * limits->vt.max_pix_clk_div; 3049e05bbacSSakari Ailus dev_dbg(dev, "max_vt_div: %u\n", max_vt_div); 3059e05bbacSSakari Ailus max_vt_div = min(max_vt_div, 3069e05bbacSSakari Ailus DIV_ROUND_UP(pll->pll_op_clk_freq_hz, 3079e05bbacSSakari Ailus limits->vt.min_pix_clk_freq_hz)); 3089e05bbacSSakari Ailus dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n", 3099e05bbacSSakari Ailus max_vt_div); 3109e05bbacSSakari Ailus 3119e05bbacSSakari Ailus /* 3129e05bbacSSakari Ailus * Find limitsits for sys_clk_div. Not all values are possible 3139e05bbacSSakari Ailus * with all values of pix_clk_div. 3149e05bbacSSakari Ailus */ 3159e05bbacSSakari Ailus min_sys_div = limits->vt.min_sys_clk_div; 3169e05bbacSSakari Ailus dev_dbg(dev, "min_sys_div: %u\n", min_sys_div); 3179e05bbacSSakari Ailus min_sys_div = max(min_sys_div, 3189e05bbacSSakari Ailus DIV_ROUND_UP(min_vt_div, 3199e05bbacSSakari Ailus limits->vt.max_pix_clk_div)); 3209e05bbacSSakari Ailus dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", min_sys_div); 3219e05bbacSSakari Ailus min_sys_div = max(min_sys_div, 3229e05bbacSSakari Ailus pll->pll_op_clk_freq_hz 3239e05bbacSSakari Ailus / limits->vt.max_sys_clk_freq_hz); 3249e05bbacSSakari Ailus dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", min_sys_div); 3259e05bbacSSakari Ailus min_sys_div = clk_div_even_up(min_sys_div); 3269e05bbacSSakari Ailus dev_dbg(dev, "min_sys_div: one or even: %u\n", min_sys_div); 3279e05bbacSSakari Ailus 3289e05bbacSSakari Ailus max_sys_div = limits->vt.max_sys_clk_div; 3299e05bbacSSakari Ailus dev_dbg(dev, "max_sys_div: %u\n", max_sys_div); 3309e05bbacSSakari Ailus max_sys_div = min(max_sys_div, 3319e05bbacSSakari Ailus DIV_ROUND_UP(max_vt_div, 3329e05bbacSSakari Ailus limits->vt.min_pix_clk_div)); 3339e05bbacSSakari Ailus dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", max_sys_div); 3349e05bbacSSakari Ailus max_sys_div = min(max_sys_div, 3359e05bbacSSakari Ailus DIV_ROUND_UP(pll->pll_op_clk_freq_hz, 3369e05bbacSSakari Ailus limits->vt.min_pix_clk_freq_hz)); 3379e05bbacSSakari Ailus dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", max_sys_div); 3389e05bbacSSakari Ailus 3399e05bbacSSakari Ailus /* 3409e05bbacSSakari Ailus * Find pix_div such that a legal pix_div * sys_div results 3419e05bbacSSakari Ailus * into a value which is not smaller than div, the desired 3429e05bbacSSakari Ailus * divisor. 3439e05bbacSSakari Ailus */ 3449e05bbacSSakari Ailus for (vt_div = min_vt_div; vt_div <= max_vt_div; 3459e05bbacSSakari Ailus vt_div += 2 - (vt_div & 1)) { 3469e05bbacSSakari Ailus for (sys_div = min_sys_div; 3479e05bbacSSakari Ailus sys_div <= max_sys_div; 3489e05bbacSSakari Ailus sys_div += 2 - (sys_div & 1)) { 3499e05bbacSSakari Ailus uint16_t pix_div = DIV_ROUND_UP(vt_div, sys_div); 3509e05bbacSSakari Ailus 3519e05bbacSSakari Ailus if (pix_div < limits->vt.min_pix_clk_div 3529e05bbacSSakari Ailus || pix_div > limits->vt.max_pix_clk_div) { 3539e05bbacSSakari Ailus dev_dbg(dev, 3549e05bbacSSakari Ailus "pix_div %u too small or too big (%u--%u)\n", 3559e05bbacSSakari Ailus pix_div, 3569e05bbacSSakari Ailus limits->vt.min_pix_clk_div, 3579e05bbacSSakari Ailus limits->vt.max_pix_clk_div); 3589e05bbacSSakari Ailus continue; 3599e05bbacSSakari Ailus } 3609e05bbacSSakari Ailus 3619e05bbacSSakari Ailus /* Check if this one is better. */ 3629e05bbacSSakari Ailus if (pix_div * sys_div 3639e05bbacSSakari Ailus <= roundup(min_vt_div, best_pix_div)) 3649e05bbacSSakari Ailus best_pix_div = pix_div; 3659e05bbacSSakari Ailus } 3669e05bbacSSakari Ailus if (best_pix_div < INT_MAX >> 1) 3679e05bbacSSakari Ailus break; 3689e05bbacSSakari Ailus } 3699e05bbacSSakari Ailus 3709e05bbacSSakari Ailus pll->vt.sys_clk_div = DIV_ROUND_UP(min_vt_div, best_pix_div); 3719e05bbacSSakari Ailus pll->vt.pix_clk_div = best_pix_div; 3729e05bbacSSakari Ailus 3739e05bbacSSakari Ailus pll->vt.sys_clk_freq_hz = 3749e05bbacSSakari Ailus pll->pll_op_clk_freq_hz / pll->vt.sys_clk_div; 3759e05bbacSSakari Ailus pll->vt.pix_clk_freq_hz = 3769e05bbacSSakari Ailus pll->vt.sys_clk_freq_hz / pll->vt.pix_clk_div; 3779e05bbacSSakari Ailus 3789e05bbacSSakari Ailus out_skip_vt_calc: 3799e05bbacSSakari Ailus pll->pixel_rate_csi = 3809e05bbacSSakari Ailus op_pll->pix_clk_freq_hz * lane_op_clock_ratio; 3819e05bbacSSakari Ailus pll->pixel_rate_pixel_array = pll->vt.pix_clk_freq_hz; 3829e05bbacSSakari Ailus 3839e05bbacSSakari Ailus return check_all_bounds(dev, limits, op_limits, pll, op_pll); 3849e05bbacSSakari Ailus } 3859e05bbacSSakari Ailus 3869e05bbacSSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *limits, 3879e05bbacSSakari Ailus struct ccs_pll *pll) 3889e05bbacSSakari Ailus { 3899e05bbacSSakari Ailus const struct ccs_pll_branch_limits *op_limits = &limits->op; 3909e05bbacSSakari Ailus struct ccs_pll_branch *op_pll = &pll->op; 3919e05bbacSSakari Ailus uint16_t min_pre_pll_clk_div; 3929e05bbacSSakari Ailus uint16_t max_pre_pll_clk_div; 3939e05bbacSSakari Ailus uint32_t lane_op_clock_ratio; 3949e05bbacSSakari Ailus uint32_t mul, div; 3959e05bbacSSakari Ailus unsigned int i; 3969e05bbacSSakari Ailus int rval = -EINVAL; 3979e05bbacSSakari Ailus 3989e05bbacSSakari Ailus if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) { 3999e05bbacSSakari Ailus /* 4009e05bbacSSakari Ailus * If there's no OP PLL at all, use the VT values 4019e05bbacSSakari Ailus * instead. The OP values are ignored for the rest of 4029e05bbacSSakari Ailus * the PLL calculation. 4039e05bbacSSakari Ailus */ 4049e05bbacSSakari Ailus op_limits = &limits->vt; 4059e05bbacSSakari Ailus op_pll = &pll->vt; 4069e05bbacSSakari Ailus } 4079e05bbacSSakari Ailus 4089e05bbacSSakari Ailus if (pll->flags & CCS_PLL_FLAG_OP_PIX_CLOCK_PER_LANE) 4099e05bbacSSakari Ailus lane_op_clock_ratio = pll->csi2.lanes; 4109e05bbacSSakari Ailus else 4119e05bbacSSakari Ailus lane_op_clock_ratio = 1; 4129e05bbacSSakari Ailus dev_dbg(dev, "lane_op_clock_ratio: %u\n", lane_op_clock_ratio); 4139e05bbacSSakari Ailus 4149e05bbacSSakari Ailus dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal, 4159e05bbacSSakari Ailus pll->binning_vertical); 4169e05bbacSSakari Ailus 4179e05bbacSSakari Ailus switch (pll->bus_type) { 4189e05bbacSSakari Ailus case CCS_PLL_BUS_TYPE_CSI2: 4199e05bbacSSakari Ailus /* CSI transfers 2 bits per clock per lane; thus times 2 */ 4209e05bbacSSakari Ailus pll->pll_op_clk_freq_hz = pll->link_freq * 2 4219e05bbacSSakari Ailus * (pll->csi2.lanes / lane_op_clock_ratio); 4229e05bbacSSakari Ailus break; 4239e05bbacSSakari Ailus case CCS_PLL_BUS_TYPE_PARALLEL: 4249e05bbacSSakari Ailus pll->pll_op_clk_freq_hz = pll->link_freq * pll->bits_per_pixel 4259e05bbacSSakari Ailus / DIV_ROUND_UP(pll->bits_per_pixel, 4269e05bbacSSakari Ailus pll->parallel.bus_width); 4279e05bbacSSakari Ailus break; 4289e05bbacSSakari Ailus default: 4299e05bbacSSakari Ailus return -EINVAL; 4309e05bbacSSakari Ailus } 4319e05bbacSSakari Ailus 4329e05bbacSSakari Ailus /* Figure out limits for pre-pll divider based on extclk */ 4339e05bbacSSakari Ailus dev_dbg(dev, "min / max pre_pll_clk_div: %u / %u\n", 4349e05bbacSSakari Ailus limits->min_pre_pll_clk_div, limits->max_pre_pll_clk_div); 4359e05bbacSSakari Ailus max_pre_pll_clk_div = 4369e05bbacSSakari Ailus min_t(uint16_t, limits->max_pre_pll_clk_div, 4379e05bbacSSakari Ailus clk_div_even(pll->ext_clk_freq_hz / 4389e05bbacSSakari Ailus limits->min_pll_ip_freq_hz)); 4399e05bbacSSakari Ailus min_pre_pll_clk_div = 4409e05bbacSSakari Ailus max_t(uint16_t, limits->min_pre_pll_clk_div, 4419e05bbacSSakari Ailus clk_div_even_up( 4429e05bbacSSakari Ailus DIV_ROUND_UP(pll->ext_clk_freq_hz, 4439e05bbacSSakari Ailus limits->max_pll_ip_freq_hz))); 4449e05bbacSSakari Ailus dev_dbg(dev, "pre-pll check: min / max pre_pll_clk_div: %u / %u\n", 4459e05bbacSSakari Ailus min_pre_pll_clk_div, max_pre_pll_clk_div); 4469e05bbacSSakari Ailus 4479e05bbacSSakari Ailus i = gcd(pll->pll_op_clk_freq_hz, pll->ext_clk_freq_hz); 4489e05bbacSSakari Ailus mul = div_u64(pll->pll_op_clk_freq_hz, i); 4499e05bbacSSakari Ailus div = pll->ext_clk_freq_hz / i; 4509e05bbacSSakari Ailus dev_dbg(dev, "mul %u / div %u\n", mul, div); 4519e05bbacSSakari Ailus 4529e05bbacSSakari Ailus min_pre_pll_clk_div = 4539e05bbacSSakari Ailus max_t(uint16_t, min_pre_pll_clk_div, 4549e05bbacSSakari Ailus clk_div_even_up( 4559e05bbacSSakari Ailus DIV_ROUND_UP(mul * pll->ext_clk_freq_hz, 4569e05bbacSSakari Ailus limits->max_pll_op_freq_hz))); 4579e05bbacSSakari Ailus dev_dbg(dev, "pll_op check: min / max pre_pll_clk_div: %u / %u\n", 4589e05bbacSSakari Ailus min_pre_pll_clk_div, max_pre_pll_clk_div); 4599e05bbacSSakari Ailus 4609e05bbacSSakari Ailus for (pll->pre_pll_clk_div = min_pre_pll_clk_div; 4619e05bbacSSakari Ailus pll->pre_pll_clk_div <= max_pre_pll_clk_div; 4629e05bbacSSakari Ailus pll->pre_pll_clk_div += 2 - (pll->pre_pll_clk_div & 1)) { 4639e05bbacSSakari Ailus rval = __ccs_pll_calculate(dev, limits, op_limits, pll, op_pll, 4649e05bbacSSakari Ailus mul, div, lane_op_clock_ratio); 4659e05bbacSSakari Ailus if (rval) 4669e05bbacSSakari Ailus continue; 4679e05bbacSSakari Ailus 4689e05bbacSSakari Ailus print_pll(dev, pll); 4699e05bbacSSakari Ailus return 0; 4709e05bbacSSakari Ailus } 4719e05bbacSSakari Ailus 4729e05bbacSSakari Ailus dev_dbg(dev, "unable to compute pre_pll divisor\n"); 4739e05bbacSSakari Ailus 4749e05bbacSSakari Ailus return rval; 4759e05bbacSSakari Ailus } 4769e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate); 4779e05bbacSSakari Ailus 4789e05bbacSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>"); 4799e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator"); 480*b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2"); 481