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, 49f25d3962SSakari Ailus uint32_t min, uint32_t max, const char *prefix, 50f25d3962SSakari Ailus char *str) 519e05bbacSSakari Ailus { 529e05bbacSSakari Ailus if (val >= min && val <= max) 539e05bbacSSakari Ailus return 0; 549e05bbacSSakari Ailus 55f25d3962SSakari Ailus dev_dbg(dev, "%s_%s out of bounds: %d (%d--%d)\n", prefix, 56f25d3962SSakari Ailus str, val, min, max); 579e05bbacSSakari Ailus 589e05bbacSSakari Ailus return -EINVAL; 599e05bbacSSakari Ailus } 609e05bbacSSakari Ailus 61fadfe884SSakari Ailus #define PLL_OP 1 62fadfe884SSakari Ailus #define PLL_VT 2 63fadfe884SSakari Ailus 64fadfe884SSakari Ailus static const char *pll_string(unsigned int which) 65fadfe884SSakari Ailus { 66fadfe884SSakari Ailus switch (which) { 67fadfe884SSakari Ailus case PLL_OP: 68fadfe884SSakari Ailus return "op"; 69fadfe884SSakari Ailus case PLL_VT: 70fadfe884SSakari Ailus return "vt"; 71fadfe884SSakari Ailus } 72fadfe884SSakari Ailus 73fadfe884SSakari Ailus return NULL; 74fadfe884SSakari Ailus } 75fadfe884SSakari Ailus 76fadfe884SSakari Ailus #define PLL_FL(f) CCS_PLL_FLAG_##f 77fadfe884SSakari Ailus 789e05bbacSSakari Ailus static void print_pll(struct device *dev, struct ccs_pll *pll) 799e05bbacSSakari Ailus { 80fadfe884SSakari Ailus const struct { 81fadfe884SSakari Ailus struct ccs_pll_branch_fr *fr; 82fadfe884SSakari Ailus struct ccs_pll_branch_bk *bk; 83fadfe884SSakari Ailus unsigned int which; 84fadfe884SSakari Ailus } branches[] = { 85fadfe884SSakari Ailus { &pll->vt_fr, &pll->vt_bk, PLL_VT }, 86f25d3962SSakari Ailus { &pll->op_fr, &pll->op_bk, PLL_OP } 87fadfe884SSakari Ailus }, *br; 88fadfe884SSakari Ailus unsigned int i; 899e05bbacSSakari Ailus 90fadfe884SSakari Ailus dev_dbg(dev, "ext_clk_freq_hz\t\t%u\n", pll->ext_clk_freq_hz); 91fadfe884SSakari Ailus 92fadfe884SSakari Ailus for (i = 0, br = branches; i < ARRAY_SIZE(branches); i++, br++) { 93fadfe884SSakari Ailus const char *s = pll_string(br->which); 94fadfe884SSakari Ailus 95*6c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL || 96*6c7469e4SSakari Ailus br->which == PLL_VT) { 97fadfe884SSakari Ailus dev_dbg(dev, "%s_pre_pll_clk_div\t\t%u\n", s, 98fadfe884SSakari Ailus br->fr->pre_pll_clk_div); 99fadfe884SSakari Ailus dev_dbg(dev, "%s_pll_multiplier\t\t%u\n", s, 100fadfe884SSakari Ailus br->fr->pll_multiplier); 101fadfe884SSakari Ailus 102fadfe884SSakari Ailus dev_dbg(dev, "%s_pll_ip_clk_freq_hz\t%u\n", s, 103fadfe884SSakari Ailus br->fr->pll_ip_clk_freq_hz); 104fadfe884SSakari Ailus dev_dbg(dev, "%s_pll_op_clk_freq_hz\t%u\n", s, 105fadfe884SSakari Ailus br->fr->pll_op_clk_freq_hz); 1069e05bbacSSakari Ailus } 107fadfe884SSakari Ailus 108fadfe884SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) || 109fadfe884SSakari Ailus br->which == PLL_VT) { 110fadfe884SSakari Ailus dev_dbg(dev, "%s_sys_clk_div\t\t%u\n", s, 111fadfe884SSakari Ailus br->bk->sys_clk_div); 112fadfe884SSakari Ailus dev_dbg(dev, "%s_pix_clk_div\t\t%u\n", s, 113fadfe884SSakari Ailus br->bk->pix_clk_div); 114fadfe884SSakari Ailus 115fadfe884SSakari Ailus dev_dbg(dev, "%s_sys_clk_freq_hz\t%u\n", s, 116fadfe884SSakari Ailus br->bk->sys_clk_freq_hz); 117fadfe884SSakari Ailus dev_dbg(dev, "%s_pix_clk_freq_hz\t%u\n", s, 118fadfe884SSakari Ailus br->bk->pix_clk_freq_hz); 119fadfe884SSakari Ailus } 120fadfe884SSakari Ailus } 121fadfe884SSakari Ailus 122*6c7469e4SSakari Ailus dev_dbg(dev, "flags%s%s%s%s%s%s%s\n", 123fadfe884SSakari Ailus pll->flags & PLL_FL(LANE_SPEED_MODEL) ? " lane-speed" : "", 124fadfe884SSakari Ailus pll->flags & PLL_FL(LINK_DECOUPLED) ? " link-decoupled" : "", 125fadfe884SSakari Ailus pll->flags & PLL_FL(EXT_IP_PLL_DIVIDER) ? 126fadfe884SSakari Ailus " ext-ip-pll-divider" : "", 127fadfe884SSakari Ailus pll->flags & PLL_FL(FLEXIBLE_OP_PIX_CLK_DIV) ? 128fadfe884SSakari Ailus " flexible-op-pix-div" : "", 129fadfe884SSakari Ailus pll->flags & PLL_FL(FIFO_DERATING) ? " fifo-derating" : "", 130*6c7469e4SSakari Ailus pll->flags & PLL_FL(FIFO_OVERRATING) ? " fifo-overrating" : "", 131*6c7469e4SSakari Ailus pll->flags & PLL_FL(DUAL_PLL) ? " dual-pll" : ""); 1329e05bbacSSakari Ailus } 1339e05bbacSSakari Ailus 134f25d3962SSakari Ailus static int check_fr_bounds(struct device *dev, 135415ddd99SSakari Ailus const struct ccs_pll_limits *lim, 136f25d3962SSakari Ailus struct ccs_pll *pll, unsigned int which) 1379e05bbacSSakari Ailus { 138f25d3962SSakari Ailus const struct ccs_pll_branch_limits_fr *lim_fr; 139f25d3962SSakari Ailus struct ccs_pll_branch_fr *pll_fr; 140f25d3962SSakari Ailus const char *s = pll_string(which); 1419e05bbacSSakari Ailus int rval; 1429e05bbacSSakari Ailus 143f25d3962SSakari Ailus if (which == PLL_OP) { 144f25d3962SSakari Ailus lim_fr = &lim->op_fr; 145f25d3962SSakari Ailus pll_fr = &pll->op_fr; 146f25d3962SSakari Ailus } else { 147f25d3962SSakari Ailus lim_fr = &lim->vt_fr; 148f25d3962SSakari Ailus pll_fr = &pll->vt_fr; 149f25d3962SSakari Ailus } 1509e05bbacSSakari Ailus 151f25d3962SSakari Ailus rval = bounds_check(dev, pll_fr->pre_pll_clk_div, 152f25d3962SSakari Ailus lim_fr->min_pre_pll_clk_div, 153f25d3962SSakari Ailus lim_fr->max_pre_pll_clk_div, s, "pre_pll_clk_div"); 154f25d3962SSakari Ailus 155f25d3962SSakari Ailus if (!rval) 156f25d3962SSakari Ailus rval = bounds_check(dev, pll_fr->pll_ip_clk_freq_hz, 157f25d3962SSakari Ailus lim_fr->min_pll_ip_clk_freq_hz, 158f25d3962SSakari Ailus lim_fr->max_pll_ip_clk_freq_hz, 159f25d3962SSakari Ailus s, "pll_ip_clk_freq_hz"); 160f25d3962SSakari Ailus if (!rval) 161f25d3962SSakari Ailus rval = bounds_check(dev, pll_fr->pll_multiplier, 162f25d3962SSakari Ailus lim_fr->min_pll_multiplier, 163f25d3962SSakari Ailus lim_fr->max_pll_multiplier, 164f25d3962SSakari Ailus s, "pll_multiplier"); 165f25d3962SSakari Ailus if (!rval) 166f25d3962SSakari Ailus rval = bounds_check(dev, pll_fr->pll_op_clk_freq_hz, 167f25d3962SSakari Ailus lim_fr->min_pll_op_clk_freq_hz, 168f25d3962SSakari Ailus lim_fr->max_pll_op_clk_freq_hz, 169f25d3962SSakari Ailus s, "pll_op_clk_freq_hz"); 170f25d3962SSakari Ailus 1719e05bbacSSakari Ailus return rval; 172f25d3962SSakari Ailus } 1739e05bbacSSakari Ailus 174f25d3962SSakari Ailus static int check_bk_bounds(struct device *dev, 175f25d3962SSakari Ailus const struct ccs_pll_limits *lim, 176f25d3962SSakari Ailus struct ccs_pll *pll, unsigned int which) 177f25d3962SSakari Ailus { 178f25d3962SSakari Ailus const struct ccs_pll_branch_limits_bk *lim_bk; 179f25d3962SSakari Ailus struct ccs_pll_branch_bk *pll_bk; 180f25d3962SSakari Ailus const char *s = pll_string(which); 181f25d3962SSakari Ailus int rval; 1829e05bbacSSakari Ailus 183f25d3962SSakari Ailus if (which == PLL_OP) { 184f25d3962SSakari Ailus if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) 185f25d3962SSakari Ailus return 0; 186f25d3962SSakari Ailus 187f25d3962SSakari Ailus lim_bk = &lim->op_bk; 188f25d3962SSakari Ailus pll_bk = &pll->op_bk; 189f25d3962SSakari Ailus } else { 190f25d3962SSakari Ailus lim_bk = &lim->vt_bk; 191f25d3962SSakari Ailus pll_bk = &pll->vt_bk; 192f25d3962SSakari Ailus } 193f25d3962SSakari Ailus 194f25d3962SSakari Ailus rval = bounds_check(dev, pll_bk->sys_clk_div, 195f25d3962SSakari Ailus lim_bk->min_sys_clk_div, 196f25d3962SSakari Ailus lim_bk->max_sys_clk_div, s, "op_sys_clk_div"); 197f25d3962SSakari Ailus if (!rval) 198f25d3962SSakari Ailus rval = bounds_check(dev, pll_bk->sys_clk_freq_hz, 199f25d3962SSakari Ailus lim_bk->min_sys_clk_freq_hz, 200f25d3962SSakari Ailus lim_bk->max_sys_clk_freq_hz, 201f25d3962SSakari Ailus s, "sys_clk_freq_hz"); 202f25d3962SSakari Ailus if (!rval) 203f25d3962SSakari Ailus rval = bounds_check(dev, pll_bk->sys_clk_div, 204f25d3962SSakari Ailus lim_bk->min_sys_clk_div, 205f25d3962SSakari Ailus lim_bk->max_sys_clk_div, 206f25d3962SSakari Ailus s, "sys_clk_div"); 207f25d3962SSakari Ailus if (!rval) 208f25d3962SSakari Ailus rval = bounds_check(dev, pll_bk->pix_clk_freq_hz, 209f25d3962SSakari Ailus lim_bk->min_pix_clk_freq_hz, 210f25d3962SSakari Ailus lim_bk->max_pix_clk_freq_hz, 211f25d3962SSakari Ailus s, "pix_clk_freq_hz"); 212f25d3962SSakari Ailus 213f25d3962SSakari Ailus return rval; 214f25d3962SSakari Ailus } 215f25d3962SSakari Ailus 216f25d3962SSakari Ailus static int check_ext_bounds(struct device *dev, struct ccs_pll *pll) 217f25d3962SSakari Ailus { 21838c94eb8SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_FIFO_DERATING) && 21938c94eb8SSakari Ailus pll->pixel_rate_pixel_array > pll->pixel_rate_csi) { 22038c94eb8SSakari Ailus dev_dbg(dev, "device does not support derating\n"); 22138c94eb8SSakari Ailus return -EINVAL; 22238c94eb8SSakari Ailus } 22338c94eb8SSakari Ailus 22438c94eb8SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_FIFO_OVERRATING) && 22538c94eb8SSakari Ailus pll->pixel_rate_pixel_array < pll->pixel_rate_csi) { 22638c94eb8SSakari Ailus dev_dbg(dev, "device does not support overrating\n"); 22738c94eb8SSakari Ailus return -EINVAL; 22838c94eb8SSakari Ailus } 22938c94eb8SSakari Ailus 230f25d3962SSakari Ailus return 0; 2319e05bbacSSakari Ailus } 2329e05bbacSSakari Ailus 2339ec6e5b1SSakari Ailus static void 2349ec6e5b1SSakari Ailus ccs_pll_find_vt_sys_div(struct device *dev, const struct ccs_pll_limits *lim, 2359ec6e5b1SSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch_fr *pll_fr, 2369ec6e5b1SSakari Ailus uint16_t min_vt_div, uint16_t max_vt_div, 2379ec6e5b1SSakari Ailus uint16_t *min_sys_div, uint16_t *max_sys_div) 2389ec6e5b1SSakari Ailus { 2399ec6e5b1SSakari Ailus /* 2409ec6e5b1SSakari Ailus * Find limits for sys_clk_div. Not all values are possible with all 2419ec6e5b1SSakari Ailus * values of pix_clk_div. 2429ec6e5b1SSakari Ailus */ 2439ec6e5b1SSakari Ailus *min_sys_div = lim->vt_bk.min_sys_clk_div; 2449ec6e5b1SSakari Ailus dev_dbg(dev, "min_sys_div: %u\n", *min_sys_div); 2459ec6e5b1SSakari Ailus *min_sys_div = max_t(uint16_t, *min_sys_div, 2469ec6e5b1SSakari Ailus DIV_ROUND_UP(min_vt_div, 2479ec6e5b1SSakari Ailus lim->vt_bk.max_pix_clk_div)); 2489ec6e5b1SSakari Ailus dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", *min_sys_div); 2499ec6e5b1SSakari Ailus *min_sys_div = max_t(uint16_t, *min_sys_div, 2509ec6e5b1SSakari Ailus pll_fr->pll_op_clk_freq_hz 2519ec6e5b1SSakari Ailus / lim->vt_bk.max_sys_clk_freq_hz); 2529ec6e5b1SSakari Ailus dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", *min_sys_div); 2539ec6e5b1SSakari Ailus *min_sys_div = clk_div_even_up(*min_sys_div); 2549ec6e5b1SSakari Ailus dev_dbg(dev, "min_sys_div: one or even: %u\n", *min_sys_div); 2559ec6e5b1SSakari Ailus 2569ec6e5b1SSakari Ailus *max_sys_div = lim->vt_bk.max_sys_clk_div; 2579ec6e5b1SSakari Ailus dev_dbg(dev, "max_sys_div: %u\n", *max_sys_div); 2589ec6e5b1SSakari Ailus *max_sys_div = min_t(uint16_t, *max_sys_div, 2599ec6e5b1SSakari Ailus DIV_ROUND_UP(max_vt_div, 2609ec6e5b1SSakari Ailus lim->vt_bk.min_pix_clk_div)); 2619ec6e5b1SSakari Ailus dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", *max_sys_div); 2629ec6e5b1SSakari Ailus *max_sys_div = min_t(uint16_t, *max_sys_div, 2639ec6e5b1SSakari Ailus DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz, 2649ec6e5b1SSakari Ailus lim->vt_bk.min_pix_clk_freq_hz)); 2659ec6e5b1SSakari Ailus dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", *max_sys_div); 2669ec6e5b1SSakari Ailus } 2679ec6e5b1SSakari Ailus 2688030aa4fSSakari Ailus #define CPHY_CONST 7 2698030aa4fSSakari Ailus #define DPHY_CONST 16 2708030aa4fSSakari Ailus #define PHY_CONST_DIV 16 2718030aa4fSSakari Ailus 272*6c7469e4SSakari Ailus static inline int 273*6c7469e4SSakari Ailus __ccs_pll_calculate_vt_tree(struct device *dev, 274*6c7469e4SSakari Ailus const struct ccs_pll_limits *lim, 275*6c7469e4SSakari Ailus struct ccs_pll *pll, uint32_t mul, uint32_t div) 276*6c7469e4SSakari Ailus { 277*6c7469e4SSakari Ailus const struct ccs_pll_branch_limits_fr *lim_fr = &lim->vt_fr; 278*6c7469e4SSakari Ailus const struct ccs_pll_branch_limits_bk *lim_bk = &lim->vt_bk; 279*6c7469e4SSakari Ailus struct ccs_pll_branch_fr *pll_fr = &pll->vt_fr; 280*6c7469e4SSakari Ailus struct ccs_pll_branch_bk *pll_bk = &pll->vt_bk; 281*6c7469e4SSakari Ailus uint32_t more_mul; 282*6c7469e4SSakari Ailus uint16_t best_pix_div = SHRT_MAX >> 1, best_div; 283*6c7469e4SSakari Ailus uint16_t vt_div, min_sys_div, max_sys_div, sys_div; 284*6c7469e4SSakari Ailus 285*6c7469e4SSakari Ailus pll_fr->pll_ip_clk_freq_hz = 286*6c7469e4SSakari Ailus pll->ext_clk_freq_hz / pll_fr->pre_pll_clk_div; 287*6c7469e4SSakari Ailus 288*6c7469e4SSakari Ailus dev_dbg(dev, "vt_pll_ip_clk_freq_hz %u\n", pll_fr->pll_ip_clk_freq_hz); 289*6c7469e4SSakari Ailus 290*6c7469e4SSakari Ailus more_mul = one_or_more(DIV_ROUND_UP(lim_fr->min_pll_op_clk_freq_hz, 291*6c7469e4SSakari Ailus pll_fr->pll_ip_clk_freq_hz * mul)); 292*6c7469e4SSakari Ailus 293*6c7469e4SSakari Ailus dev_dbg(dev, "more_mul: %u\n", more_mul); 294*6c7469e4SSakari Ailus more_mul *= DIV_ROUND_UP(lim_fr->min_pll_multiplier, mul * more_mul); 295*6c7469e4SSakari Ailus dev_dbg(dev, "more_mul2: %u\n", more_mul); 296*6c7469e4SSakari Ailus 297*6c7469e4SSakari Ailus pll_fr->pll_multiplier = mul * more_mul; 298*6c7469e4SSakari Ailus 299*6c7469e4SSakari Ailus if (pll_fr->pll_multiplier * pll_fr->pll_ip_clk_freq_hz > 300*6c7469e4SSakari Ailus lim_fr->max_pll_op_clk_freq_hz) 301*6c7469e4SSakari Ailus return -EINVAL; 302*6c7469e4SSakari Ailus 303*6c7469e4SSakari Ailus pll_fr->pll_op_clk_freq_hz = 304*6c7469e4SSakari Ailus pll_fr->pll_ip_clk_freq_hz * pll_fr->pll_multiplier; 305*6c7469e4SSakari Ailus 306*6c7469e4SSakari Ailus vt_div = div * more_mul; 307*6c7469e4SSakari Ailus 308*6c7469e4SSakari Ailus ccs_pll_find_vt_sys_div(dev, lim, pll, pll_fr, vt_div, vt_div, 309*6c7469e4SSakari Ailus &min_sys_div, &max_sys_div); 310*6c7469e4SSakari Ailus 311*6c7469e4SSakari Ailus max_sys_div = (vt_div & 1) ? 1 : max_sys_div; 312*6c7469e4SSakari Ailus 313*6c7469e4SSakari Ailus dev_dbg(dev, "vt min/max_sys_div: %u,%u\n", min_sys_div, max_sys_div); 314*6c7469e4SSakari Ailus 315*6c7469e4SSakari Ailus for (sys_div = min_sys_div; sys_div <= max_sys_div; 316*6c7469e4SSakari Ailus sys_div += 2 - (sys_div & 1)) { 317*6c7469e4SSakari Ailus uint16_t pix_div; 318*6c7469e4SSakari Ailus 319*6c7469e4SSakari Ailus if (vt_div % sys_div) 320*6c7469e4SSakari Ailus continue; 321*6c7469e4SSakari Ailus 322*6c7469e4SSakari Ailus pix_div = vt_div / sys_div; 323*6c7469e4SSakari Ailus 324*6c7469e4SSakari Ailus if (pix_div < lim_bk->min_pix_clk_div || 325*6c7469e4SSakari Ailus pix_div > lim_bk->max_pix_clk_div) { 326*6c7469e4SSakari Ailus dev_dbg(dev, 327*6c7469e4SSakari Ailus "pix_div %u too small or too big (%u--%u)\n", 328*6c7469e4SSakari Ailus pix_div, 329*6c7469e4SSakari Ailus lim_bk->min_pix_clk_div, 330*6c7469e4SSakari Ailus lim_bk->max_pix_clk_div); 331*6c7469e4SSakari Ailus continue; 332*6c7469e4SSakari Ailus } 333*6c7469e4SSakari Ailus 334*6c7469e4SSakari Ailus if (pix_div * sys_div <= best_div) { 335*6c7469e4SSakari Ailus best_pix_div = pix_div; 336*6c7469e4SSakari Ailus best_div = pix_div * sys_div; 337*6c7469e4SSakari Ailus } 338*6c7469e4SSakari Ailus } 339*6c7469e4SSakari Ailus if (best_pix_div == SHRT_MAX >> 1) 340*6c7469e4SSakari Ailus return -EINVAL; 341*6c7469e4SSakari Ailus 342*6c7469e4SSakari Ailus pll_bk->sys_clk_div = best_div / best_pix_div; 343*6c7469e4SSakari Ailus pll_bk->pix_clk_div = best_pix_div; 344*6c7469e4SSakari Ailus 345*6c7469e4SSakari Ailus pll_bk->sys_clk_freq_hz = 346*6c7469e4SSakari Ailus pll_fr->pll_op_clk_freq_hz / pll_bk->sys_clk_div; 347*6c7469e4SSakari Ailus pll_bk->pix_clk_freq_hz = 348*6c7469e4SSakari Ailus pll_bk->sys_clk_freq_hz / pll_bk->pix_clk_div; 349*6c7469e4SSakari Ailus 350*6c7469e4SSakari Ailus pll->pixel_rate_pixel_array = 351*6c7469e4SSakari Ailus pll_bk->pix_clk_freq_hz * pll->vt_lanes; 352*6c7469e4SSakari Ailus 353*6c7469e4SSakari Ailus return 0; 354*6c7469e4SSakari Ailus } 355*6c7469e4SSakari Ailus 356*6c7469e4SSakari Ailus static int ccs_pll_calculate_vt_tree(struct device *dev, 357*6c7469e4SSakari Ailus const struct ccs_pll_limits *lim, 358*6c7469e4SSakari Ailus struct ccs_pll *pll) 359*6c7469e4SSakari Ailus { 360*6c7469e4SSakari Ailus const struct ccs_pll_branch_limits_fr *lim_fr = &lim->vt_fr; 361*6c7469e4SSakari Ailus struct ccs_pll_branch_fr *pll_fr = &pll->vt_fr; 362*6c7469e4SSakari Ailus uint16_t min_pre_pll_clk_div = lim_fr->min_pre_pll_clk_div; 363*6c7469e4SSakari Ailus uint16_t max_pre_pll_clk_div = lim_fr->max_pre_pll_clk_div; 364*6c7469e4SSakari Ailus uint32_t pre_mul, pre_div; 365*6c7469e4SSakari Ailus 366*6c7469e4SSakari Ailus pre_div = gcd(pll->pixel_rate_csi, 367*6c7469e4SSakari Ailus pll->ext_clk_freq_hz * pll->vt_lanes); 368*6c7469e4SSakari Ailus pre_mul = pll->pixel_rate_csi / pre_div; 369*6c7469e4SSakari Ailus pre_div = pll->ext_clk_freq_hz * pll->vt_lanes / pre_div; 370*6c7469e4SSakari Ailus 371*6c7469e4SSakari Ailus /* Make sure PLL input frequency is within limits */ 372*6c7469e4SSakari Ailus max_pre_pll_clk_div = 373*6c7469e4SSakari Ailus min_t(uint16_t, max_pre_pll_clk_div, 374*6c7469e4SSakari Ailus DIV_ROUND_UP(pll->ext_clk_freq_hz, 375*6c7469e4SSakari Ailus lim_fr->min_pll_ip_clk_freq_hz)); 376*6c7469e4SSakari Ailus 377*6c7469e4SSakari Ailus min_pre_pll_clk_div = max_t(uint16_t, min_pre_pll_clk_div, 378*6c7469e4SSakari Ailus pll->ext_clk_freq_hz / 379*6c7469e4SSakari Ailus lim_fr->max_pll_ip_clk_freq_hz); 380*6c7469e4SSakari Ailus 381*6c7469e4SSakari Ailus dev_dbg(dev, "vt min/max_pre_pll_clk_div: %u,%u\n", 382*6c7469e4SSakari Ailus min_pre_pll_clk_div, max_pre_pll_clk_div); 383*6c7469e4SSakari Ailus 384*6c7469e4SSakari Ailus for (pll_fr->pre_pll_clk_div = min_pre_pll_clk_div; 385*6c7469e4SSakari Ailus pll_fr->pre_pll_clk_div <= max_pre_pll_clk_div; 386*6c7469e4SSakari Ailus pll_fr->pre_pll_clk_div += 387*6c7469e4SSakari Ailus (pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER) ? 1 : 388*6c7469e4SSakari Ailus 2 - (pll_fr->pre_pll_clk_div & 1)) { 389*6c7469e4SSakari Ailus uint32_t mul, div; 390*6c7469e4SSakari Ailus int rval; 391*6c7469e4SSakari Ailus 392*6c7469e4SSakari Ailus div = gcd(pre_mul * pll_fr->pre_pll_clk_div, pre_div); 393*6c7469e4SSakari Ailus mul = pre_mul * pll_fr->pre_pll_clk_div / div; 394*6c7469e4SSakari Ailus div = pre_div / div; 395*6c7469e4SSakari Ailus 396*6c7469e4SSakari Ailus dev_dbg(dev, "vt pre-div/mul/div: %u,%u,%u\n", 397*6c7469e4SSakari Ailus pll_fr->pre_pll_clk_div, mul, div); 398*6c7469e4SSakari Ailus 399*6c7469e4SSakari Ailus rval = __ccs_pll_calculate_vt_tree(dev, lim, pll, 400*6c7469e4SSakari Ailus mul, div); 401*6c7469e4SSakari Ailus if (rval) 402*6c7469e4SSakari Ailus continue; 403*6c7469e4SSakari Ailus 404*6c7469e4SSakari Ailus rval = check_fr_bounds(dev, lim, pll, PLL_VT); 405*6c7469e4SSakari Ailus if (rval) 406*6c7469e4SSakari Ailus continue; 407*6c7469e4SSakari Ailus 408*6c7469e4SSakari Ailus rval = check_bk_bounds(dev, lim, pll, PLL_VT); 409*6c7469e4SSakari Ailus if (rval) 410*6c7469e4SSakari Ailus continue; 411*6c7469e4SSakari Ailus 412*6c7469e4SSakari Ailus return 0; 413*6c7469e4SSakari Ailus } 414*6c7469e4SSakari Ailus 415*6c7469e4SSakari Ailus return -EINVAL; 416*6c7469e4SSakari Ailus } 417*6c7469e4SSakari Ailus 4183e2db036SSakari Ailus static void 419a38836b2SSakari Ailus ccs_pll_calculate_vt(struct device *dev, const struct ccs_pll_limits *lim, 4203e2db036SSakari Ailus const struct ccs_pll_branch_limits_bk *op_lim_bk, 4213e2db036SSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch_fr *pll_fr, 4223e2db036SSakari Ailus struct ccs_pll_branch_bk *op_pll_bk, bool cphy, 4233e2db036SSakari Ailus uint32_t phy_const) 4243e2db036SSakari Ailus { 425594f1e93SSakari Ailus uint16_t sys_div; 426594f1e93SSakari Ailus uint16_t best_pix_div = SHRT_MAX >> 1; 427594f1e93SSakari Ailus uint16_t vt_op_binning_div; 428594f1e93SSakari Ailus uint16_t min_vt_div, max_vt_div, vt_div; 429594f1e93SSakari Ailus uint16_t min_sys_div, max_sys_div; 4303e2db036SSakari Ailus 431a38836b2SSakari Ailus if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) 432a38836b2SSakari Ailus goto out_calc_pixel_rate; 433a38836b2SSakari Ailus 4343e2db036SSakari Ailus /* 43538c94eb8SSakari Ailus * Find out whether a sensor supports derating. If it does not, VT and 43638c94eb8SSakari Ailus * OP domains are required to run at the same pixel rate. 43738c94eb8SSakari Ailus */ 43838c94eb8SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_FIFO_DERATING)) { 43938c94eb8SSakari Ailus min_vt_div = 44038c94eb8SSakari Ailus op_pll_bk->sys_clk_div * op_pll_bk->pix_clk_div 44138c94eb8SSakari Ailus * pll->vt_lanes * phy_const 44238c94eb8SSakari Ailus / pll->op_lanes / PHY_CONST_DIV; 44338c94eb8SSakari Ailus } else { 44438c94eb8SSakari Ailus /* 4453e2db036SSakari Ailus * Some sensors perform analogue binning and some do this 4463e2db036SSakari Ailus * digitally. The ones doing this digitally can be roughly be 4473e2db036SSakari Ailus * found out using this formula. The ones doing this digitally 4483e2db036SSakari Ailus * should run at higher clock rate, so smaller divisor is used 4493e2db036SSakari Ailus * on video timing side. 4503e2db036SSakari Ailus */ 4513e2db036SSakari Ailus if (lim->min_line_length_pck_bin > lim->min_line_length_pck 4523e2db036SSakari Ailus / pll->binning_horizontal) 4533e2db036SSakari Ailus vt_op_binning_div = pll->binning_horizontal; 4543e2db036SSakari Ailus else 4553e2db036SSakari Ailus vt_op_binning_div = 1; 4563e2db036SSakari Ailus dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div); 4573e2db036SSakari Ailus 4583e2db036SSakari Ailus /* 4593e2db036SSakari Ailus * Profile 2 supports vt_pix_clk_div E [4, 10] 4603e2db036SSakari Ailus * 4613e2db036SSakari Ailus * Horizontal binning can be used as a base for difference in 4623e2db036SSakari Ailus * divisors. One must make sure that horizontal blanking is 4633e2db036SSakari Ailus * enough to accommodate the CSI-2 sync codes. 4643e2db036SSakari Ailus * 4653e2db036SSakari Ailus * Take scaling factor and number of VT lanes into account as well. 4663e2db036SSakari Ailus * 4673e2db036SSakari Ailus * Find absolute limits for the factor of vt divider. 4683e2db036SSakari Ailus */ 4693e2db036SSakari Ailus dev_dbg(dev, "scale_m: %u\n", pll->scale_m); 47038c94eb8SSakari Ailus min_vt_div = 47138c94eb8SSakari Ailus DIV_ROUND_UP(pll->bits_per_pixel 47238c94eb8SSakari Ailus * op_pll_bk->sys_clk_div * pll->scale_n 47338c94eb8SSakari Ailus * pll->vt_lanes * phy_const, 47438c94eb8SSakari Ailus (pll->flags & 47538c94eb8SSakari Ailus CCS_PLL_FLAG_LANE_SPEED_MODEL ? 4763e2db036SSakari Ailus pll->csi2.lanes : 1) 4773e2db036SSakari Ailus * vt_op_binning_div * pll->scale_m 4783e2db036SSakari Ailus * PHY_CONST_DIV); 47938c94eb8SSakari Ailus } 4803e2db036SSakari Ailus 4813e2db036SSakari Ailus /* Find smallest and biggest allowed vt divisor. */ 4823e2db036SSakari Ailus dev_dbg(dev, "min_vt_div: %u\n", min_vt_div); 483594f1e93SSakari Ailus min_vt_div = max_t(uint16_t, min_vt_div, 4843e2db036SSakari Ailus DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz, 4853e2db036SSakari Ailus lim->vt_bk.max_pix_clk_freq_hz)); 4863e2db036SSakari Ailus dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n", 4873e2db036SSakari Ailus min_vt_div); 488594f1e93SSakari Ailus min_vt_div = max_t(uint16_t, min_vt_div, lim->vt_bk.min_pix_clk_div 4893e2db036SSakari Ailus * lim->vt_bk.min_sys_clk_div); 4903e2db036SSakari Ailus dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div); 4913e2db036SSakari Ailus 4923e2db036SSakari Ailus max_vt_div = lim->vt_bk.max_sys_clk_div * lim->vt_bk.max_pix_clk_div; 4933e2db036SSakari Ailus dev_dbg(dev, "max_vt_div: %u\n", max_vt_div); 494594f1e93SSakari Ailus max_vt_div = min_t(uint16_t, max_vt_div, 4953e2db036SSakari Ailus DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz, 4963e2db036SSakari Ailus lim->vt_bk.min_pix_clk_freq_hz)); 4973e2db036SSakari Ailus dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n", 4983e2db036SSakari Ailus max_vt_div); 4993e2db036SSakari Ailus 5009ec6e5b1SSakari Ailus ccs_pll_find_vt_sys_div(dev, lim, pll, pll_fr, min_vt_div, 5019ec6e5b1SSakari Ailus max_vt_div, &min_sys_div, &max_sys_div); 5023e2db036SSakari Ailus 5033e2db036SSakari Ailus /* 5043e2db036SSakari Ailus * Find pix_div such that a legal pix_div * sys_div results 5053e2db036SSakari Ailus * into a value which is not smaller than div, the desired 5063e2db036SSakari Ailus * divisor. 5073e2db036SSakari Ailus */ 50836154b68SSakari Ailus for (vt_div = min_vt_div; vt_div <= max_vt_div; vt_div++) { 50936154b68SSakari Ailus uint16_t __max_sys_div = vt_div & 1 ? 1 : max_sys_div; 51036154b68SSakari Ailus 51136154b68SSakari Ailus for (sys_div = min_sys_div; sys_div <= __max_sys_div; 5123e2db036SSakari Ailus sys_div += 2 - (sys_div & 1)) { 51336154b68SSakari Ailus uint16_t pix_div; 5143e2db036SSakari Ailus uint16_t rounded_div; 5153e2db036SSakari Ailus 51636154b68SSakari Ailus pix_div = DIV_ROUND_UP(vt_div, sys_div); 51736154b68SSakari Ailus 5183e2db036SSakari Ailus if (pix_div < lim->vt_bk.min_pix_clk_div 5193e2db036SSakari Ailus || pix_div > lim->vt_bk.max_pix_clk_div) { 5203e2db036SSakari Ailus dev_dbg(dev, 5213e2db036SSakari Ailus "pix_div %u too small or too big (%u--%u)\n", 5223e2db036SSakari Ailus pix_div, 5233e2db036SSakari Ailus lim->vt_bk.min_pix_clk_div, 5243e2db036SSakari Ailus lim->vt_bk.max_pix_clk_div); 5253e2db036SSakari Ailus continue; 5263e2db036SSakari Ailus } 5273e2db036SSakari Ailus 5283e2db036SSakari Ailus rounded_div = roundup(vt_div, best_pix_div); 5293e2db036SSakari Ailus 5303e2db036SSakari Ailus /* Check if this one is better. */ 5313e2db036SSakari Ailus if (pix_div * sys_div <= rounded_div) 5323e2db036SSakari Ailus best_pix_div = pix_div; 5333e2db036SSakari Ailus 5343e2db036SSakari Ailus /* Bail out if we've already found the best value. */ 5353e2db036SSakari Ailus if (vt_div == rounded_div) 5363e2db036SSakari Ailus break; 5373e2db036SSakari Ailus } 538594f1e93SSakari Ailus if (best_pix_div < SHRT_MAX >> 1) 5393e2db036SSakari Ailus break; 5403e2db036SSakari Ailus } 5413e2db036SSakari Ailus 5423e2db036SSakari Ailus pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div); 5433e2db036SSakari Ailus pll->vt_bk.pix_clk_div = best_pix_div; 5443e2db036SSakari Ailus 5453e2db036SSakari Ailus pll->vt_bk.sys_clk_freq_hz = 5463e2db036SSakari Ailus pll_fr->pll_op_clk_freq_hz / pll->vt_bk.sys_clk_div; 5473e2db036SSakari Ailus pll->vt_bk.pix_clk_freq_hz = 5483e2db036SSakari Ailus pll->vt_bk.sys_clk_freq_hz / pll->vt_bk.pix_clk_div; 549a38836b2SSakari Ailus 550a38836b2SSakari Ailus out_calc_pixel_rate: 551a38836b2SSakari Ailus pll->pixel_rate_pixel_array = 552a38836b2SSakari Ailus pll->vt_bk.pix_clk_freq_hz * pll->vt_lanes; 5533e2db036SSakari Ailus } 5543e2db036SSakari Ailus 5559e05bbacSSakari Ailus /* 5569e05bbacSSakari Ailus * Heuristically guess the PLL tree for a given common multiplier and 5579e05bbacSSakari Ailus * divisor. Begin with the operational timing and continue to video 5589e05bbacSSakari Ailus * timing once operational timing has been verified. 5599e05bbacSSakari Ailus * 5609e05bbacSSakari Ailus * @mul is the PLL multiplier and @div is the common divisor 5619e05bbacSSakari Ailus * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL 5629e05bbacSSakari Ailus * multiplier will be a multiple of @mul. 5639e05bbacSSakari Ailus * 5649e05bbacSSakari Ailus * @return Zero on success, error code on error. 5659e05bbacSSakari Ailus */ 5669e05bbacSSakari Ailus static int 567a38836b2SSakari Ailus ccs_pll_calculate_op(struct device *dev, const struct ccs_pll_limits *lim, 568415ddd99SSakari Ailus const struct ccs_pll_branch_limits_fr *op_lim_fr, 569415ddd99SSakari Ailus const struct ccs_pll_branch_limits_bk *op_lim_bk, 570415ddd99SSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch_fr *op_pll_fr, 571415ddd99SSakari Ailus struct ccs_pll_branch_bk *op_pll_bk, uint32_t mul, 5728030aa4fSSakari Ailus uint32_t div, uint32_t l, bool cphy, uint32_t phy_const) 5739e05bbacSSakari Ailus { 5749e05bbacSSakari Ailus /* 5759e05bbacSSakari Ailus * Higher multipliers (and divisors) are often required than 5769e05bbacSSakari Ailus * necessitated by the external clock and the output clocks. 5779e05bbacSSakari Ailus * There are limits for all values in the clock tree. These 5789e05bbacSSakari Ailus * are the minimum and maximum multiplier for mul. 5799e05bbacSSakari Ailus */ 5809e05bbacSSakari Ailus uint32_t more_mul_min, more_mul_max; 5819e05bbacSSakari Ailus uint32_t more_mul_factor; 582e583e654SSakari Ailus uint32_t i; 5839e05bbacSSakari Ailus 5849e05bbacSSakari Ailus /* 5859e05bbacSSakari Ailus * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be 5869e05bbacSSakari Ailus * too high. 5879e05bbacSSakari Ailus */ 588415ddd99SSakari Ailus dev_dbg(dev, "op_pre_pll_clk_div %u\n", op_pll_fr->pre_pll_clk_div); 5899e05bbacSSakari Ailus 5909e05bbacSSakari Ailus /* Don't go above max pll multiplier. */ 591415ddd99SSakari Ailus more_mul_max = op_lim_fr->max_pll_multiplier / mul; 592415ddd99SSakari Ailus dev_dbg(dev, "more_mul_max: max_op_pll_multiplier check: %u\n", 5939e05bbacSSakari Ailus more_mul_max); 5949e05bbacSSakari Ailus /* Don't go above max pll op frequency. */ 5959e05bbacSSakari Ailus more_mul_max = 5969e05bbacSSakari Ailus min_t(uint32_t, 5979e05bbacSSakari Ailus more_mul_max, 598415ddd99SSakari Ailus op_lim_fr->max_pll_op_clk_freq_hz 599ae502e08SSakari Ailus / (pll->ext_clk_freq_hz / 600ae502e08SSakari Ailus op_pll_fr->pre_pll_clk_div * mul)); 601415ddd99SSakari Ailus dev_dbg(dev, "more_mul_max: max_pll_op_clk_freq_hz check: %u\n", 6029e05bbacSSakari Ailus more_mul_max); 6039e05bbacSSakari Ailus /* Don't go above the division capability of op sys clock divider. */ 6049e05bbacSSakari Ailus more_mul_max = min(more_mul_max, 605415ddd99SSakari Ailus op_lim_bk->max_sys_clk_div * op_pll_fr->pre_pll_clk_div 6069e05bbacSSakari Ailus / div); 6079e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n", 6089e05bbacSSakari Ailus more_mul_max); 609c64cf71dSSakari Ailus /* Ensure we won't go above max_pll_multiplier. */ 61082ab97c8SSakari Ailus more_mul_max = min(more_mul_max, op_lim_fr->max_pll_multiplier / mul); 6119e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n", 6129e05bbacSSakari Ailus more_mul_max); 6139e05bbacSSakari Ailus 614415ddd99SSakari Ailus /* Ensure we won't go below min_pll_op_clk_freq_hz. */ 615415ddd99SSakari Ailus more_mul_min = DIV_ROUND_UP(op_lim_fr->min_pll_op_clk_freq_hz, 616415ddd99SSakari Ailus pll->ext_clk_freq_hz / 617415ddd99SSakari Ailus op_pll_fr->pre_pll_clk_div * mul); 618415ddd99SSakari Ailus dev_dbg(dev, "more_mul_min: min_op_pll_op_clk_freq_hz check: %u\n", 6199e05bbacSSakari Ailus more_mul_min); 6209e05bbacSSakari Ailus /* Ensure we won't go below min_pll_multiplier. */ 6219e05bbacSSakari Ailus more_mul_min = max(more_mul_min, 622415ddd99SSakari Ailus DIV_ROUND_UP(op_lim_fr->min_pll_multiplier, mul)); 623415ddd99SSakari Ailus dev_dbg(dev, "more_mul_min: min_op_pll_multiplier check: %u\n", 6249e05bbacSSakari Ailus more_mul_min); 6259e05bbacSSakari Ailus 6269e05bbacSSakari Ailus if (more_mul_min > more_mul_max) { 6279e05bbacSSakari Ailus dev_dbg(dev, 6289e05bbacSSakari Ailus "unable to compute more_mul_min and more_mul_max\n"); 6299e05bbacSSakari Ailus return -EINVAL; 6309e05bbacSSakari Ailus } 6319e05bbacSSakari Ailus 632415ddd99SSakari Ailus more_mul_factor = lcm(div, op_pll_fr->pre_pll_clk_div) / div; 6339e05bbacSSakari Ailus dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor); 634415ddd99SSakari Ailus more_mul_factor = lcm(more_mul_factor, op_lim_bk->min_sys_clk_div); 6359e05bbacSSakari Ailus dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n", 6369e05bbacSSakari Ailus more_mul_factor); 6379e05bbacSSakari Ailus i = roundup(more_mul_min, more_mul_factor); 6389e05bbacSSakari Ailus if (!is_one_or_even(i)) 6399e05bbacSSakari Ailus i <<= 1; 6409e05bbacSSakari Ailus 6419e05bbacSSakari Ailus dev_dbg(dev, "final more_mul: %u\n", i); 6429e05bbacSSakari Ailus if (i > more_mul_max) { 6439e05bbacSSakari Ailus dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max); 6449e05bbacSSakari Ailus return -EINVAL; 6459e05bbacSSakari Ailus } 6469e05bbacSSakari Ailus 647415ddd99SSakari Ailus op_pll_fr->pll_multiplier = mul * i; 648415ddd99SSakari Ailus op_pll_bk->sys_clk_div = div * i / op_pll_fr->pre_pll_clk_div; 649415ddd99SSakari Ailus dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll_bk->sys_clk_div); 6509e05bbacSSakari Ailus 651415ddd99SSakari Ailus op_pll_fr->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz 652415ddd99SSakari Ailus / op_pll_fr->pre_pll_clk_div; 6539e05bbacSSakari Ailus 654415ddd99SSakari Ailus op_pll_fr->pll_op_clk_freq_hz = op_pll_fr->pll_ip_clk_freq_hz 655415ddd99SSakari Ailus * op_pll_fr->pll_multiplier; 6569e05bbacSSakari Ailus 657c4c0b222SSakari Ailus if (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL) 658cac8f5d2SSakari Ailus op_pll_bk->pix_clk_div = pll->bits_per_pixel 6598030aa4fSSakari Ailus * pll->op_lanes * phy_const 6608030aa4fSSakari Ailus / PHY_CONST_DIV / pll->csi2.lanes / l; 661c4c0b222SSakari Ailus else 6628030aa4fSSakari Ailus op_pll_bk->pix_clk_div = 6638030aa4fSSakari Ailus pll->bits_per_pixel * phy_const / PHY_CONST_DIV / l; 664c4c0b222SSakari Ailus 665415ddd99SSakari Ailus op_pll_bk->pix_clk_freq_hz = 666415ddd99SSakari Ailus op_pll_bk->sys_clk_freq_hz / op_pll_bk->pix_clk_div; 667c4c0b222SSakari Ailus 668cac8f5d2SSakari Ailus dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll_bk->pix_clk_div); 669cac8f5d2SSakari Ailus 670a38836b2SSakari Ailus return 0; 6719e05bbacSSakari Ailus } 6729e05bbacSSakari Ailus 673415ddd99SSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim, 6749e05bbacSSakari Ailus struct ccs_pll *pll) 6759e05bbacSSakari Ailus { 676*6c7469e4SSakari Ailus const struct ccs_pll_branch_limits_fr *op_lim_fr; 677*6c7469e4SSakari Ailus const struct ccs_pll_branch_limits_bk *op_lim_bk; 678*6c7469e4SSakari Ailus struct ccs_pll_branch_fr *op_pll_fr; 679*6c7469e4SSakari Ailus struct ccs_pll_branch_bk *op_pll_bk; 6808030aa4fSSakari Ailus bool cphy = pll->bus_type == CCS_PLL_BUS_TYPE_CSI2_CPHY; 6818030aa4fSSakari Ailus uint32_t phy_const = cphy ? CPHY_CONST : DPHY_CONST; 682415ddd99SSakari Ailus uint16_t min_op_pre_pll_clk_div; 683415ddd99SSakari Ailus uint16_t max_op_pre_pll_clk_div; 6849e05bbacSSakari Ailus uint32_t mul, div; 685c4c0b222SSakari Ailus uint32_t l = (!pll->op_bits_per_lane || 686c4c0b222SSakari Ailus pll->op_bits_per_lane >= pll->bits_per_pixel) ? 1 : 2; 687e583e654SSakari Ailus uint32_t i; 6889e05bbacSSakari Ailus int rval = -EINVAL; 6899e05bbacSSakari Ailus 690cac8f5d2SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL)) { 691cac8f5d2SSakari Ailus pll->op_lanes = 1; 692cac8f5d2SSakari Ailus pll->vt_lanes = 1; 693cac8f5d2SSakari Ailus } 6949490a227SSakari Ailus 695*6c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL) { 696*6c7469e4SSakari Ailus op_lim_fr = &lim->op_fr; 697*6c7469e4SSakari Ailus op_lim_bk = &lim->op_bk; 698*6c7469e4SSakari Ailus op_pll_fr = &pll->op_fr; 699*6c7469e4SSakari Ailus op_pll_bk = &pll->op_bk; 700*6c7469e4SSakari Ailus } else if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) { 701*6c7469e4SSakari Ailus /* 702*6c7469e4SSakari Ailus * If there's no OP PLL at all, use the VT values 703*6c7469e4SSakari Ailus * instead. The OP values are ignored for the rest of 704*6c7469e4SSakari Ailus * the PLL calculation. 705*6c7469e4SSakari Ailus */ 706*6c7469e4SSakari Ailus op_lim_fr = &lim->vt_fr; 707*6c7469e4SSakari Ailus op_lim_bk = &lim->vt_bk; 708*6c7469e4SSakari Ailus op_pll_fr = &pll->vt_fr; 709*6c7469e4SSakari Ailus op_pll_bk = &pll->vt_bk; 710*6c7469e4SSakari Ailus } else { 711*6c7469e4SSakari Ailus op_lim_fr = &lim->vt_fr; 712*6c7469e4SSakari Ailus op_lim_bk = &lim->op_bk; 713*6c7469e4SSakari Ailus op_pll_fr = &pll->vt_fr; 714*6c7469e4SSakari Ailus op_pll_bk = &pll->op_bk; 715*6c7469e4SSakari Ailus } 716*6c7469e4SSakari Ailus 717d7172c0eSSakari Ailus if (!pll->op_lanes || !pll->vt_lanes || !pll->bits_per_pixel || 718d7172c0eSSakari Ailus !pll->ext_clk_freq_hz || !pll->link_freq || !pll->scale_m || 719d7172c0eSSakari Ailus !op_lim_fr->min_pll_ip_clk_freq_hz || 720d7172c0eSSakari Ailus !op_lim_fr->max_pll_ip_clk_freq_hz || 721d7172c0eSSakari Ailus !op_lim_fr->min_pll_op_clk_freq_hz || 722d7172c0eSSakari Ailus !op_lim_fr->max_pll_op_clk_freq_hz || 723d7172c0eSSakari Ailus !op_lim_bk->max_sys_clk_div || !op_lim_fr->max_pll_multiplier) 724d7172c0eSSakari Ailus return -EINVAL; 725d7172c0eSSakari Ailus 7269490a227SSakari Ailus /* 7279490a227SSakari Ailus * Make sure op_pix_clk_div will be integer --- unless flexible 7289490a227SSakari Ailus * op_pix_clk_div is supported 7299490a227SSakari Ailus */ 7309490a227SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV) && 7319490a227SSakari Ailus (pll->bits_per_pixel * pll->op_lanes) % (pll->csi2.lanes * l)) { 7329490a227SSakari Ailus dev_dbg(dev, "op_pix_clk_div not an integer (bpp %u, op lanes %u, lanes %u, l %u)\n", 7339490a227SSakari Ailus pll->bits_per_pixel, pll->op_lanes, pll->csi2.lanes, l); 7349490a227SSakari Ailus return -EINVAL; 7359490a227SSakari Ailus } 7369490a227SSakari Ailus 737cac8f5d2SSakari Ailus dev_dbg(dev, "vt_lanes: %u\n", pll->vt_lanes); 738cac8f5d2SSakari Ailus dev_dbg(dev, "op_lanes: %u\n", pll->op_lanes); 739cac8f5d2SSakari Ailus 7409e05bbacSSakari Ailus dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal, 7419e05bbacSSakari Ailus pll->binning_vertical); 7429e05bbacSSakari Ailus 7439e05bbacSSakari Ailus switch (pll->bus_type) { 74447b6eaf3SSakari Ailus case CCS_PLL_BUS_TYPE_CSI2_DPHY: 7459e05bbacSSakari Ailus /* CSI transfers 2 bits per clock per lane; thus times 2 */ 746cab27256SSakari Ailus op_pll_bk->sys_clk_freq_hz = pll->link_freq * 2 747cac8f5d2SSakari Ailus * (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 748ae502e08SSakari Ailus 1 : pll->csi2.lanes); 7499e05bbacSSakari Ailus break; 7508030aa4fSSakari Ailus case CCS_PLL_BUS_TYPE_CSI2_CPHY: 7518030aa4fSSakari Ailus op_pll_bk->sys_clk_freq_hz = 7528030aa4fSSakari Ailus pll->link_freq 7538030aa4fSSakari Ailus * (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 7548030aa4fSSakari Ailus 1 : pll->csi2.lanes); 7558030aa4fSSakari Ailus break; 7569e05bbacSSakari Ailus default: 7579e05bbacSSakari Ailus return -EINVAL; 7589e05bbacSSakari Ailus } 7599e05bbacSSakari Ailus 760cac8f5d2SSakari Ailus pll->pixel_rate_csi = 7618030aa4fSSakari Ailus div_u64((uint64_t)op_pll_bk->sys_clk_freq_hz 762cac8f5d2SSakari Ailus * (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 7638030aa4fSSakari Ailus pll->csi2.lanes : 1) * PHY_CONST_DIV, 7648030aa4fSSakari Ailus phy_const * pll->bits_per_pixel * l); 765cac8f5d2SSakari Ailus 766415ddd99SSakari Ailus /* Figure out limits for OP pre-pll divider based on extclk */ 767415ddd99SSakari Ailus dev_dbg(dev, "min / max op_pre_pll_clk_div: %u / %u\n", 768415ddd99SSakari Ailus op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div); 769415ddd99SSakari Ailus max_op_pre_pll_clk_div = 770415ddd99SSakari Ailus min_t(uint16_t, op_lim_fr->max_pre_pll_clk_div, 7719e05bbacSSakari Ailus clk_div_even(pll->ext_clk_freq_hz / 772415ddd99SSakari Ailus op_lim_fr->min_pll_ip_clk_freq_hz)); 773415ddd99SSakari Ailus min_op_pre_pll_clk_div = 774415ddd99SSakari Ailus max_t(uint16_t, op_lim_fr->min_pre_pll_clk_div, 7759e05bbacSSakari Ailus clk_div_even_up( 7769e05bbacSSakari Ailus DIV_ROUND_UP(pll->ext_clk_freq_hz, 777415ddd99SSakari Ailus op_lim_fr->max_pll_ip_clk_freq_hz))); 778415ddd99SSakari Ailus dev_dbg(dev, "pre-pll check: min / max op_pre_pll_clk_div: %u / %u\n", 779415ddd99SSakari Ailus min_op_pre_pll_clk_div, max_op_pre_pll_clk_div); 7809e05bbacSSakari Ailus 781cab27256SSakari Ailus i = gcd(op_pll_bk->sys_clk_freq_hz, pll->ext_clk_freq_hz); 782cab27256SSakari Ailus mul = op_pll_bk->sys_clk_freq_hz / i; 7839e05bbacSSakari Ailus div = pll->ext_clk_freq_hz / i; 7849e05bbacSSakari Ailus dev_dbg(dev, "mul %u / div %u\n", mul, div); 7859e05bbacSSakari Ailus 786415ddd99SSakari Ailus min_op_pre_pll_clk_div = 787415ddd99SSakari Ailus max_t(uint16_t, min_op_pre_pll_clk_div, 7889e05bbacSSakari Ailus clk_div_even_up( 789482e75e7SSakari Ailus mul / 790482e75e7SSakari Ailus one_or_more( 791482e75e7SSakari Ailus DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz, 792482e75e7SSakari Ailus pll->ext_clk_freq_hz)))); 793415ddd99SSakari Ailus dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n", 794415ddd99SSakari Ailus min_op_pre_pll_clk_div, max_op_pre_pll_clk_div); 7959e05bbacSSakari Ailus 796415ddd99SSakari Ailus for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div; 797415ddd99SSakari Ailus op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div; 7984e1e8d24SSakari Ailus op_pll_fr->pre_pll_clk_div += 7994e1e8d24SSakari Ailus (pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER) ? 1 : 8004e1e8d24SSakari Ailus 2 - (op_pll_fr->pre_pll_clk_div & 1)) { 801a38836b2SSakari Ailus rval = ccs_pll_calculate_op(dev, lim, op_lim_fr, op_lim_bk, pll, 8028030aa4fSSakari Ailus op_pll_fr, op_pll_bk, mul, div, l, 8038030aa4fSSakari Ailus cphy, phy_const); 8049e05bbacSSakari Ailus if (rval) 8059e05bbacSSakari Ailus continue; 8069e05bbacSSakari Ailus 807f25d3962SSakari Ailus rval = check_fr_bounds(dev, lim, pll, PLL_VT); 808f25d3962SSakari Ailus if (rval) 809f25d3962SSakari Ailus continue; 810f25d3962SSakari Ailus 811f25d3962SSakari Ailus rval = check_bk_bounds(dev, lim, pll, PLL_OP); 812f25d3962SSakari Ailus if (rval) 813f25d3962SSakari Ailus continue; 814f25d3962SSakari Ailus 815*6c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL) 816*6c7469e4SSakari Ailus break; 817*6c7469e4SSakari Ailus 818a38836b2SSakari Ailus ccs_pll_calculate_vt(dev, lim, op_lim_bk, pll, op_pll_fr, 819a38836b2SSakari Ailus op_pll_bk, cphy, phy_const); 820a38836b2SSakari Ailus 821f25d3962SSakari Ailus rval = check_bk_bounds(dev, lim, pll, PLL_VT); 822f25d3962SSakari Ailus if (rval) 823f25d3962SSakari Ailus continue; 824f25d3962SSakari Ailus rval = check_ext_bounds(dev, pll); 825a38836b2SSakari Ailus if (rval) 826a38836b2SSakari Ailus continue; 827a38836b2SSakari Ailus 828*6c7469e4SSakari Ailus break; 8299e05bbacSSakari Ailus } 8309e05bbacSSakari Ailus 831*6c7469e4SSakari Ailus if (rval) { 8329e05bbacSSakari Ailus dev_dbg(dev, "unable to compute pre_pll divisor\n"); 8339e05bbacSSakari Ailus 8349e05bbacSSakari Ailus return rval; 8359e05bbacSSakari Ailus } 836*6c7469e4SSakari Ailus 837*6c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL) { 838*6c7469e4SSakari Ailus rval = ccs_pll_calculate_vt_tree(dev, lim, pll); 839*6c7469e4SSakari Ailus 840*6c7469e4SSakari Ailus if (rval) 841*6c7469e4SSakari Ailus return rval; 842*6c7469e4SSakari Ailus } 843*6c7469e4SSakari Ailus 844*6c7469e4SSakari Ailus print_pll(dev, pll); 845*6c7469e4SSakari Ailus 846*6c7469e4SSakari Ailus return 0; 847*6c7469e4SSakari Ailus } 8489e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate); 8499e05bbacSSakari Ailus 8507389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); 8519e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator"); 852b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2"); 853