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 956c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL || 966c7469e4SSakari 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 1226c7469e4SSakari 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" : "", 1306c7469e4SSakari Ailus pll->flags & PLL_FL(FIFO_OVERRATING) ? " fifo-overrating" : "", 1316c7469e4SSakari 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 2726c7469e4SSakari Ailus static inline int 2736c7469e4SSakari Ailus __ccs_pll_calculate_vt_tree(struct device *dev, 2746c7469e4SSakari Ailus const struct ccs_pll_limits *lim, 2756c7469e4SSakari Ailus struct ccs_pll *pll, uint32_t mul, uint32_t div) 2766c7469e4SSakari Ailus { 2776c7469e4SSakari Ailus const struct ccs_pll_branch_limits_fr *lim_fr = &lim->vt_fr; 2786c7469e4SSakari Ailus const struct ccs_pll_branch_limits_bk *lim_bk = &lim->vt_bk; 2796c7469e4SSakari Ailus struct ccs_pll_branch_fr *pll_fr = &pll->vt_fr; 2806c7469e4SSakari Ailus struct ccs_pll_branch_bk *pll_bk = &pll->vt_bk; 2816c7469e4SSakari Ailus uint32_t more_mul; 2826c7469e4SSakari Ailus uint16_t best_pix_div = SHRT_MAX >> 1, best_div; 2836c7469e4SSakari Ailus uint16_t vt_div, min_sys_div, max_sys_div, sys_div; 2846c7469e4SSakari Ailus 2856c7469e4SSakari Ailus pll_fr->pll_ip_clk_freq_hz = 2866c7469e4SSakari Ailus pll->ext_clk_freq_hz / pll_fr->pre_pll_clk_div; 2876c7469e4SSakari Ailus 2886c7469e4SSakari Ailus dev_dbg(dev, "vt_pll_ip_clk_freq_hz %u\n", pll_fr->pll_ip_clk_freq_hz); 2896c7469e4SSakari Ailus 2906c7469e4SSakari Ailus more_mul = one_or_more(DIV_ROUND_UP(lim_fr->min_pll_op_clk_freq_hz, 2916c7469e4SSakari Ailus pll_fr->pll_ip_clk_freq_hz * mul)); 2926c7469e4SSakari Ailus 2936c7469e4SSakari Ailus dev_dbg(dev, "more_mul: %u\n", more_mul); 2946c7469e4SSakari Ailus more_mul *= DIV_ROUND_UP(lim_fr->min_pll_multiplier, mul * more_mul); 2956c7469e4SSakari Ailus dev_dbg(dev, "more_mul2: %u\n", more_mul); 2966c7469e4SSakari Ailus 2976c7469e4SSakari Ailus pll_fr->pll_multiplier = mul * more_mul; 2986c7469e4SSakari Ailus 2996c7469e4SSakari Ailus if (pll_fr->pll_multiplier * pll_fr->pll_ip_clk_freq_hz > 3006c7469e4SSakari Ailus lim_fr->max_pll_op_clk_freq_hz) 3016c7469e4SSakari Ailus return -EINVAL; 3026c7469e4SSakari Ailus 3036c7469e4SSakari Ailus pll_fr->pll_op_clk_freq_hz = 3046c7469e4SSakari Ailus pll_fr->pll_ip_clk_freq_hz * pll_fr->pll_multiplier; 3056c7469e4SSakari Ailus 3066c7469e4SSakari Ailus vt_div = div * more_mul; 3076c7469e4SSakari Ailus 3086c7469e4SSakari Ailus ccs_pll_find_vt_sys_div(dev, lim, pll, pll_fr, vt_div, vt_div, 3096c7469e4SSakari Ailus &min_sys_div, &max_sys_div); 3106c7469e4SSakari Ailus 3116c7469e4SSakari Ailus max_sys_div = (vt_div & 1) ? 1 : max_sys_div; 3126c7469e4SSakari Ailus 3136c7469e4SSakari Ailus dev_dbg(dev, "vt min/max_sys_div: %u,%u\n", min_sys_div, max_sys_div); 3146c7469e4SSakari Ailus 3156c7469e4SSakari Ailus for (sys_div = min_sys_div; sys_div <= max_sys_div; 3166c7469e4SSakari Ailus sys_div += 2 - (sys_div & 1)) { 3176c7469e4SSakari Ailus uint16_t pix_div; 3186c7469e4SSakari Ailus 3196c7469e4SSakari Ailus if (vt_div % sys_div) 3206c7469e4SSakari Ailus continue; 3216c7469e4SSakari Ailus 3226c7469e4SSakari Ailus pix_div = vt_div / sys_div; 3236c7469e4SSakari Ailus 3246c7469e4SSakari Ailus if (pix_div < lim_bk->min_pix_clk_div || 3256c7469e4SSakari Ailus pix_div > lim_bk->max_pix_clk_div) { 3266c7469e4SSakari Ailus dev_dbg(dev, 3276c7469e4SSakari Ailus "pix_div %u too small or too big (%u--%u)\n", 3286c7469e4SSakari Ailus pix_div, 3296c7469e4SSakari Ailus lim_bk->min_pix_clk_div, 3306c7469e4SSakari Ailus lim_bk->max_pix_clk_div); 3316c7469e4SSakari Ailus continue; 3326c7469e4SSakari Ailus } 3336c7469e4SSakari Ailus 334*b41f2708SSakari Ailus dev_dbg(dev, "sys/pix/best_pix: %u,%u,%u\n", sys_div, pix_div, 335*b41f2708SSakari Ailus best_pix_div); 336*b41f2708SSakari Ailus 337*b41f2708SSakari Ailus if (pix_div * sys_div <= best_pix_div) { 3386c7469e4SSakari Ailus best_pix_div = pix_div; 3396c7469e4SSakari Ailus best_div = pix_div * sys_div; 3406c7469e4SSakari Ailus } 3416c7469e4SSakari Ailus } 3426c7469e4SSakari Ailus if (best_pix_div == SHRT_MAX >> 1) 3436c7469e4SSakari Ailus return -EINVAL; 3446c7469e4SSakari Ailus 3456c7469e4SSakari Ailus pll_bk->sys_clk_div = best_div / best_pix_div; 3466c7469e4SSakari Ailus pll_bk->pix_clk_div = best_pix_div; 3476c7469e4SSakari Ailus 3486c7469e4SSakari Ailus pll_bk->sys_clk_freq_hz = 3496c7469e4SSakari Ailus pll_fr->pll_op_clk_freq_hz / pll_bk->sys_clk_div; 3506c7469e4SSakari Ailus pll_bk->pix_clk_freq_hz = 3516c7469e4SSakari Ailus pll_bk->sys_clk_freq_hz / pll_bk->pix_clk_div; 3526c7469e4SSakari Ailus 3536c7469e4SSakari Ailus pll->pixel_rate_pixel_array = 3546c7469e4SSakari Ailus pll_bk->pix_clk_freq_hz * pll->vt_lanes; 3556c7469e4SSakari Ailus 3566c7469e4SSakari Ailus return 0; 3576c7469e4SSakari Ailus } 3586c7469e4SSakari Ailus 3596c7469e4SSakari Ailus static int ccs_pll_calculate_vt_tree(struct device *dev, 3606c7469e4SSakari Ailus const struct ccs_pll_limits *lim, 3616c7469e4SSakari Ailus struct ccs_pll *pll) 3626c7469e4SSakari Ailus { 3636c7469e4SSakari Ailus const struct ccs_pll_branch_limits_fr *lim_fr = &lim->vt_fr; 3646c7469e4SSakari Ailus struct ccs_pll_branch_fr *pll_fr = &pll->vt_fr; 3656c7469e4SSakari Ailus uint16_t min_pre_pll_clk_div = lim_fr->min_pre_pll_clk_div; 3666c7469e4SSakari Ailus uint16_t max_pre_pll_clk_div = lim_fr->max_pre_pll_clk_div; 3676c7469e4SSakari Ailus uint32_t pre_mul, pre_div; 3686c7469e4SSakari Ailus 3696c7469e4SSakari Ailus pre_div = gcd(pll->pixel_rate_csi, 3706c7469e4SSakari Ailus pll->ext_clk_freq_hz * pll->vt_lanes); 3716c7469e4SSakari Ailus pre_mul = pll->pixel_rate_csi / pre_div; 3726c7469e4SSakari Ailus pre_div = pll->ext_clk_freq_hz * pll->vt_lanes / pre_div; 3736c7469e4SSakari Ailus 3746c7469e4SSakari Ailus /* Make sure PLL input frequency is within limits */ 3756c7469e4SSakari Ailus max_pre_pll_clk_div = 3766c7469e4SSakari Ailus min_t(uint16_t, max_pre_pll_clk_div, 3776c7469e4SSakari Ailus DIV_ROUND_UP(pll->ext_clk_freq_hz, 3786c7469e4SSakari Ailus lim_fr->min_pll_ip_clk_freq_hz)); 3796c7469e4SSakari Ailus 3806c7469e4SSakari Ailus min_pre_pll_clk_div = max_t(uint16_t, min_pre_pll_clk_div, 3816c7469e4SSakari Ailus pll->ext_clk_freq_hz / 3826c7469e4SSakari Ailus lim_fr->max_pll_ip_clk_freq_hz); 3836c7469e4SSakari Ailus 3846c7469e4SSakari Ailus dev_dbg(dev, "vt min/max_pre_pll_clk_div: %u,%u\n", 3856c7469e4SSakari Ailus min_pre_pll_clk_div, max_pre_pll_clk_div); 3866c7469e4SSakari Ailus 3876c7469e4SSakari Ailus for (pll_fr->pre_pll_clk_div = min_pre_pll_clk_div; 3886c7469e4SSakari Ailus pll_fr->pre_pll_clk_div <= max_pre_pll_clk_div; 3896c7469e4SSakari Ailus pll_fr->pre_pll_clk_div += 3906c7469e4SSakari Ailus (pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER) ? 1 : 3916c7469e4SSakari Ailus 2 - (pll_fr->pre_pll_clk_div & 1)) { 3926c7469e4SSakari Ailus uint32_t mul, div; 3936c7469e4SSakari Ailus int rval; 3946c7469e4SSakari Ailus 3956c7469e4SSakari Ailus div = gcd(pre_mul * pll_fr->pre_pll_clk_div, pre_div); 3966c7469e4SSakari Ailus mul = pre_mul * pll_fr->pre_pll_clk_div / div; 3976c7469e4SSakari Ailus div = pre_div / div; 3986c7469e4SSakari Ailus 3996c7469e4SSakari Ailus dev_dbg(dev, "vt pre-div/mul/div: %u,%u,%u\n", 4006c7469e4SSakari Ailus pll_fr->pre_pll_clk_div, mul, div); 4016c7469e4SSakari Ailus 4026c7469e4SSakari Ailus rval = __ccs_pll_calculate_vt_tree(dev, lim, pll, 4036c7469e4SSakari Ailus mul, div); 4046c7469e4SSakari Ailus if (rval) 4056c7469e4SSakari Ailus continue; 4066c7469e4SSakari Ailus 4076c7469e4SSakari Ailus rval = check_fr_bounds(dev, lim, pll, PLL_VT); 4086c7469e4SSakari Ailus if (rval) 4096c7469e4SSakari Ailus continue; 4106c7469e4SSakari Ailus 4116c7469e4SSakari Ailus rval = check_bk_bounds(dev, lim, pll, PLL_VT); 4126c7469e4SSakari Ailus if (rval) 4136c7469e4SSakari Ailus continue; 4146c7469e4SSakari Ailus 4156c7469e4SSakari Ailus return 0; 4166c7469e4SSakari Ailus } 4176c7469e4SSakari Ailus 4186c7469e4SSakari Ailus return -EINVAL; 4196c7469e4SSakari Ailus } 4206c7469e4SSakari Ailus 4213e2db036SSakari Ailus static void 422a38836b2SSakari Ailus ccs_pll_calculate_vt(struct device *dev, const struct ccs_pll_limits *lim, 4233e2db036SSakari Ailus const struct ccs_pll_branch_limits_bk *op_lim_bk, 4243e2db036SSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch_fr *pll_fr, 4253e2db036SSakari Ailus struct ccs_pll_branch_bk *op_pll_bk, bool cphy, 4263e2db036SSakari Ailus uint32_t phy_const) 4273e2db036SSakari Ailus { 428594f1e93SSakari Ailus uint16_t sys_div; 429594f1e93SSakari Ailus uint16_t best_pix_div = SHRT_MAX >> 1; 430594f1e93SSakari Ailus uint16_t vt_op_binning_div; 431594f1e93SSakari Ailus uint16_t min_vt_div, max_vt_div, vt_div; 432594f1e93SSakari Ailus uint16_t min_sys_div, max_sys_div; 4333e2db036SSakari Ailus 434a38836b2SSakari Ailus if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) 435a38836b2SSakari Ailus goto out_calc_pixel_rate; 436a38836b2SSakari Ailus 4373e2db036SSakari Ailus /* 43838c94eb8SSakari Ailus * Find out whether a sensor supports derating. If it does not, VT and 43938c94eb8SSakari Ailus * OP domains are required to run at the same pixel rate. 44038c94eb8SSakari Ailus */ 44138c94eb8SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_FIFO_DERATING)) { 44238c94eb8SSakari Ailus min_vt_div = 44338c94eb8SSakari Ailus op_pll_bk->sys_clk_div * op_pll_bk->pix_clk_div 44438c94eb8SSakari Ailus * pll->vt_lanes * phy_const 44538c94eb8SSakari Ailus / pll->op_lanes / PHY_CONST_DIV; 44638c94eb8SSakari Ailus } else { 44738c94eb8SSakari Ailus /* 4483e2db036SSakari Ailus * Some sensors perform analogue binning and some do this 4493e2db036SSakari Ailus * digitally. The ones doing this digitally can be roughly be 4503e2db036SSakari Ailus * found out using this formula. The ones doing this digitally 4513e2db036SSakari Ailus * should run at higher clock rate, so smaller divisor is used 4523e2db036SSakari Ailus * on video timing side. 4533e2db036SSakari Ailus */ 4543e2db036SSakari Ailus if (lim->min_line_length_pck_bin > lim->min_line_length_pck 4553e2db036SSakari Ailus / pll->binning_horizontal) 4563e2db036SSakari Ailus vt_op_binning_div = pll->binning_horizontal; 4573e2db036SSakari Ailus else 4583e2db036SSakari Ailus vt_op_binning_div = 1; 4593e2db036SSakari Ailus dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div); 4603e2db036SSakari Ailus 4613e2db036SSakari Ailus /* 4623e2db036SSakari Ailus * Profile 2 supports vt_pix_clk_div E [4, 10] 4633e2db036SSakari Ailus * 4643e2db036SSakari Ailus * Horizontal binning can be used as a base for difference in 4653e2db036SSakari Ailus * divisors. One must make sure that horizontal blanking is 4663e2db036SSakari Ailus * enough to accommodate the CSI-2 sync codes. 4673e2db036SSakari Ailus * 4683e2db036SSakari Ailus * Take scaling factor and number of VT lanes into account as well. 4693e2db036SSakari Ailus * 4703e2db036SSakari Ailus * Find absolute limits for the factor of vt divider. 4713e2db036SSakari Ailus */ 4723e2db036SSakari Ailus dev_dbg(dev, "scale_m: %u\n", pll->scale_m); 47338c94eb8SSakari Ailus min_vt_div = 47438c94eb8SSakari Ailus DIV_ROUND_UP(pll->bits_per_pixel 47538c94eb8SSakari Ailus * op_pll_bk->sys_clk_div * pll->scale_n 47638c94eb8SSakari Ailus * pll->vt_lanes * phy_const, 47738c94eb8SSakari Ailus (pll->flags & 47838c94eb8SSakari Ailus CCS_PLL_FLAG_LANE_SPEED_MODEL ? 4793e2db036SSakari Ailus pll->csi2.lanes : 1) 4803e2db036SSakari Ailus * vt_op_binning_div * pll->scale_m 4813e2db036SSakari Ailus * PHY_CONST_DIV); 48238c94eb8SSakari Ailus } 4833e2db036SSakari Ailus 4843e2db036SSakari Ailus /* Find smallest and biggest allowed vt divisor. */ 4853e2db036SSakari Ailus dev_dbg(dev, "min_vt_div: %u\n", min_vt_div); 486594f1e93SSakari Ailus min_vt_div = max_t(uint16_t, min_vt_div, 4873e2db036SSakari Ailus DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz, 4883e2db036SSakari Ailus lim->vt_bk.max_pix_clk_freq_hz)); 4893e2db036SSakari Ailus dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n", 4903e2db036SSakari Ailus min_vt_div); 491594f1e93SSakari Ailus min_vt_div = max_t(uint16_t, min_vt_div, lim->vt_bk.min_pix_clk_div 4923e2db036SSakari Ailus * lim->vt_bk.min_sys_clk_div); 4933e2db036SSakari Ailus dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div); 4943e2db036SSakari Ailus 4953e2db036SSakari Ailus max_vt_div = lim->vt_bk.max_sys_clk_div * lim->vt_bk.max_pix_clk_div; 4963e2db036SSakari Ailus dev_dbg(dev, "max_vt_div: %u\n", max_vt_div); 497594f1e93SSakari Ailus max_vt_div = min_t(uint16_t, max_vt_div, 4983e2db036SSakari Ailus DIV_ROUND_UP(pll_fr->pll_op_clk_freq_hz, 4993e2db036SSakari Ailus lim->vt_bk.min_pix_clk_freq_hz)); 5003e2db036SSakari Ailus dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n", 5013e2db036SSakari Ailus max_vt_div); 5023e2db036SSakari Ailus 5039ec6e5b1SSakari Ailus ccs_pll_find_vt_sys_div(dev, lim, pll, pll_fr, min_vt_div, 5049ec6e5b1SSakari Ailus max_vt_div, &min_sys_div, &max_sys_div); 5053e2db036SSakari Ailus 5063e2db036SSakari Ailus /* 5073e2db036SSakari Ailus * Find pix_div such that a legal pix_div * sys_div results 5083e2db036SSakari Ailus * into a value which is not smaller than div, the desired 5093e2db036SSakari Ailus * divisor. 5103e2db036SSakari Ailus */ 51136154b68SSakari Ailus for (vt_div = min_vt_div; vt_div <= max_vt_div; vt_div++) { 51236154b68SSakari Ailus uint16_t __max_sys_div = vt_div & 1 ? 1 : max_sys_div; 51336154b68SSakari Ailus 51436154b68SSakari Ailus for (sys_div = min_sys_div; sys_div <= __max_sys_div; 5153e2db036SSakari Ailus sys_div += 2 - (sys_div & 1)) { 51636154b68SSakari Ailus uint16_t pix_div; 5173e2db036SSakari Ailus uint16_t rounded_div; 5183e2db036SSakari Ailus 51936154b68SSakari Ailus pix_div = DIV_ROUND_UP(vt_div, sys_div); 52036154b68SSakari Ailus 5213e2db036SSakari Ailus if (pix_div < lim->vt_bk.min_pix_clk_div 5223e2db036SSakari Ailus || pix_div > lim->vt_bk.max_pix_clk_div) { 5233e2db036SSakari Ailus dev_dbg(dev, 5243e2db036SSakari Ailus "pix_div %u too small or too big (%u--%u)\n", 5253e2db036SSakari Ailus pix_div, 5263e2db036SSakari Ailus lim->vt_bk.min_pix_clk_div, 5273e2db036SSakari Ailus lim->vt_bk.max_pix_clk_div); 5283e2db036SSakari Ailus continue; 5293e2db036SSakari Ailus } 5303e2db036SSakari Ailus 5313e2db036SSakari Ailus rounded_div = roundup(vt_div, best_pix_div); 5323e2db036SSakari Ailus 5333e2db036SSakari Ailus /* Check if this one is better. */ 5343e2db036SSakari Ailus if (pix_div * sys_div <= rounded_div) 5353e2db036SSakari Ailus best_pix_div = pix_div; 5363e2db036SSakari Ailus 5373e2db036SSakari Ailus /* Bail out if we've already found the best value. */ 5383e2db036SSakari Ailus if (vt_div == rounded_div) 5393e2db036SSakari Ailus break; 5403e2db036SSakari Ailus } 541594f1e93SSakari Ailus if (best_pix_div < SHRT_MAX >> 1) 5423e2db036SSakari Ailus break; 5433e2db036SSakari Ailus } 5443e2db036SSakari Ailus 5453e2db036SSakari Ailus pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div); 5463e2db036SSakari Ailus pll->vt_bk.pix_clk_div = best_pix_div; 5473e2db036SSakari Ailus 5483e2db036SSakari Ailus pll->vt_bk.sys_clk_freq_hz = 5493e2db036SSakari Ailus pll_fr->pll_op_clk_freq_hz / pll->vt_bk.sys_clk_div; 5503e2db036SSakari Ailus pll->vt_bk.pix_clk_freq_hz = 5513e2db036SSakari Ailus pll->vt_bk.sys_clk_freq_hz / pll->vt_bk.pix_clk_div; 552a38836b2SSakari Ailus 553a38836b2SSakari Ailus out_calc_pixel_rate: 554a38836b2SSakari Ailus pll->pixel_rate_pixel_array = 555a38836b2SSakari Ailus pll->vt_bk.pix_clk_freq_hz * pll->vt_lanes; 5563e2db036SSakari Ailus } 5573e2db036SSakari Ailus 5589e05bbacSSakari Ailus /* 5599e05bbacSSakari Ailus * Heuristically guess the PLL tree for a given common multiplier and 5609e05bbacSSakari Ailus * divisor. Begin with the operational timing and continue to video 5619e05bbacSSakari Ailus * timing once operational timing has been verified. 5629e05bbacSSakari Ailus * 5639e05bbacSSakari Ailus * @mul is the PLL multiplier and @div is the common divisor 5649e05bbacSSakari Ailus * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL 5659e05bbacSSakari Ailus * multiplier will be a multiple of @mul. 5669e05bbacSSakari Ailus * 5679e05bbacSSakari Ailus * @return Zero on success, error code on error. 5689e05bbacSSakari Ailus */ 5699e05bbacSSakari Ailus static int 570a38836b2SSakari Ailus ccs_pll_calculate_op(struct device *dev, const struct ccs_pll_limits *lim, 571415ddd99SSakari Ailus const struct ccs_pll_branch_limits_fr *op_lim_fr, 572415ddd99SSakari Ailus const struct ccs_pll_branch_limits_bk *op_lim_bk, 573415ddd99SSakari Ailus struct ccs_pll *pll, struct ccs_pll_branch_fr *op_pll_fr, 574415ddd99SSakari Ailus struct ccs_pll_branch_bk *op_pll_bk, uint32_t mul, 5758030aa4fSSakari Ailus uint32_t div, uint32_t l, bool cphy, uint32_t phy_const) 5769e05bbacSSakari Ailus { 5779e05bbacSSakari Ailus /* 5789e05bbacSSakari Ailus * Higher multipliers (and divisors) are often required than 5799e05bbacSSakari Ailus * necessitated by the external clock and the output clocks. 5809e05bbacSSakari Ailus * There are limits for all values in the clock tree. These 5819e05bbacSSakari Ailus * are the minimum and maximum multiplier for mul. 5829e05bbacSSakari Ailus */ 5839e05bbacSSakari Ailus uint32_t more_mul_min, more_mul_max; 5849e05bbacSSakari Ailus uint32_t more_mul_factor; 585e583e654SSakari Ailus uint32_t i; 5869e05bbacSSakari Ailus 5879e05bbacSSakari Ailus /* 5889e05bbacSSakari Ailus * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be 5899e05bbacSSakari Ailus * too high. 5909e05bbacSSakari Ailus */ 591415ddd99SSakari Ailus dev_dbg(dev, "op_pre_pll_clk_div %u\n", op_pll_fr->pre_pll_clk_div); 5929e05bbacSSakari Ailus 5939e05bbacSSakari Ailus /* Don't go above max pll multiplier. */ 594415ddd99SSakari Ailus more_mul_max = op_lim_fr->max_pll_multiplier / mul; 595415ddd99SSakari Ailus dev_dbg(dev, "more_mul_max: max_op_pll_multiplier check: %u\n", 5969e05bbacSSakari Ailus more_mul_max); 5979e05bbacSSakari Ailus /* Don't go above max pll op frequency. */ 5989e05bbacSSakari Ailus more_mul_max = 5999e05bbacSSakari Ailus min_t(uint32_t, 6009e05bbacSSakari Ailus more_mul_max, 601415ddd99SSakari Ailus op_lim_fr->max_pll_op_clk_freq_hz 602ae502e08SSakari Ailus / (pll->ext_clk_freq_hz / 603ae502e08SSakari Ailus op_pll_fr->pre_pll_clk_div * mul)); 604415ddd99SSakari Ailus dev_dbg(dev, "more_mul_max: max_pll_op_clk_freq_hz check: %u\n", 6059e05bbacSSakari Ailus more_mul_max); 6069e05bbacSSakari Ailus /* Don't go above the division capability of op sys clock divider. */ 6079e05bbacSSakari Ailus more_mul_max = min(more_mul_max, 608415ddd99SSakari Ailus op_lim_bk->max_sys_clk_div * op_pll_fr->pre_pll_clk_div 6099e05bbacSSakari Ailus / div); 6109e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n", 6119e05bbacSSakari Ailus more_mul_max); 612c64cf71dSSakari Ailus /* Ensure we won't go above max_pll_multiplier. */ 61382ab97c8SSakari Ailus more_mul_max = min(more_mul_max, op_lim_fr->max_pll_multiplier / mul); 6149e05bbacSSakari Ailus dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n", 6159e05bbacSSakari Ailus more_mul_max); 6169e05bbacSSakari Ailus 617415ddd99SSakari Ailus /* Ensure we won't go below min_pll_op_clk_freq_hz. */ 618415ddd99SSakari Ailus more_mul_min = DIV_ROUND_UP(op_lim_fr->min_pll_op_clk_freq_hz, 619415ddd99SSakari Ailus pll->ext_clk_freq_hz / 620415ddd99SSakari Ailus op_pll_fr->pre_pll_clk_div * mul); 621415ddd99SSakari Ailus dev_dbg(dev, "more_mul_min: min_op_pll_op_clk_freq_hz check: %u\n", 6229e05bbacSSakari Ailus more_mul_min); 6239e05bbacSSakari Ailus /* Ensure we won't go below min_pll_multiplier. */ 6249e05bbacSSakari Ailus more_mul_min = max(more_mul_min, 625415ddd99SSakari Ailus DIV_ROUND_UP(op_lim_fr->min_pll_multiplier, mul)); 626415ddd99SSakari Ailus dev_dbg(dev, "more_mul_min: min_op_pll_multiplier check: %u\n", 6279e05bbacSSakari Ailus more_mul_min); 6289e05bbacSSakari Ailus 6299e05bbacSSakari Ailus if (more_mul_min > more_mul_max) { 6309e05bbacSSakari Ailus dev_dbg(dev, 6319e05bbacSSakari Ailus "unable to compute more_mul_min and more_mul_max\n"); 6329e05bbacSSakari Ailus return -EINVAL; 6339e05bbacSSakari Ailus } 6349e05bbacSSakari Ailus 635415ddd99SSakari Ailus more_mul_factor = lcm(div, op_pll_fr->pre_pll_clk_div) / div; 6369e05bbacSSakari Ailus dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor); 637415ddd99SSakari Ailus more_mul_factor = lcm(more_mul_factor, op_lim_bk->min_sys_clk_div); 6389e05bbacSSakari Ailus dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n", 6399e05bbacSSakari Ailus more_mul_factor); 6409e05bbacSSakari Ailus i = roundup(more_mul_min, more_mul_factor); 6419e05bbacSSakari Ailus if (!is_one_or_even(i)) 6429e05bbacSSakari Ailus i <<= 1; 6439e05bbacSSakari Ailus 6449e05bbacSSakari Ailus dev_dbg(dev, "final more_mul: %u\n", i); 6459e05bbacSSakari Ailus if (i > more_mul_max) { 6469e05bbacSSakari Ailus dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max); 6479e05bbacSSakari Ailus return -EINVAL; 6489e05bbacSSakari Ailus } 6499e05bbacSSakari Ailus 650415ddd99SSakari Ailus op_pll_fr->pll_multiplier = mul * i; 651415ddd99SSakari Ailus op_pll_bk->sys_clk_div = div * i / op_pll_fr->pre_pll_clk_div; 652415ddd99SSakari Ailus dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll_bk->sys_clk_div); 6539e05bbacSSakari Ailus 654415ddd99SSakari Ailus op_pll_fr->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz 655415ddd99SSakari Ailus / op_pll_fr->pre_pll_clk_div; 6569e05bbacSSakari Ailus 657415ddd99SSakari Ailus op_pll_fr->pll_op_clk_freq_hz = op_pll_fr->pll_ip_clk_freq_hz 658415ddd99SSakari Ailus * op_pll_fr->pll_multiplier; 6599e05bbacSSakari Ailus 660c4c0b222SSakari Ailus if (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL) 661cac8f5d2SSakari Ailus op_pll_bk->pix_clk_div = pll->bits_per_pixel 6628030aa4fSSakari Ailus * pll->op_lanes * phy_const 6638030aa4fSSakari Ailus / PHY_CONST_DIV / pll->csi2.lanes / l; 664c4c0b222SSakari Ailus else 6658030aa4fSSakari Ailus op_pll_bk->pix_clk_div = 6668030aa4fSSakari Ailus pll->bits_per_pixel * phy_const / PHY_CONST_DIV / l; 667c4c0b222SSakari Ailus 668415ddd99SSakari Ailus op_pll_bk->pix_clk_freq_hz = 669415ddd99SSakari Ailus op_pll_bk->sys_clk_freq_hz / op_pll_bk->pix_clk_div; 670c4c0b222SSakari Ailus 671cac8f5d2SSakari Ailus dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll_bk->pix_clk_div); 672cac8f5d2SSakari Ailus 673a38836b2SSakari Ailus return 0; 6749e05bbacSSakari Ailus } 6759e05bbacSSakari Ailus 676415ddd99SSakari Ailus int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim, 6779e05bbacSSakari Ailus struct ccs_pll *pll) 6789e05bbacSSakari Ailus { 6796c7469e4SSakari Ailus const struct ccs_pll_branch_limits_fr *op_lim_fr; 6806c7469e4SSakari Ailus const struct ccs_pll_branch_limits_bk *op_lim_bk; 6816c7469e4SSakari Ailus struct ccs_pll_branch_fr *op_pll_fr; 6826c7469e4SSakari Ailus struct ccs_pll_branch_bk *op_pll_bk; 6838030aa4fSSakari Ailus bool cphy = pll->bus_type == CCS_PLL_BUS_TYPE_CSI2_CPHY; 6848030aa4fSSakari Ailus uint32_t phy_const = cphy ? CPHY_CONST : DPHY_CONST; 685415ddd99SSakari Ailus uint16_t min_op_pre_pll_clk_div; 686415ddd99SSakari Ailus uint16_t max_op_pre_pll_clk_div; 6879e05bbacSSakari Ailus uint32_t mul, div; 688c4c0b222SSakari Ailus uint32_t l = (!pll->op_bits_per_lane || 689c4c0b222SSakari Ailus pll->op_bits_per_lane >= pll->bits_per_pixel) ? 1 : 2; 690e583e654SSakari Ailus uint32_t i; 6919e05bbacSSakari Ailus int rval = -EINVAL; 6929e05bbacSSakari Ailus 693cac8f5d2SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL)) { 694cac8f5d2SSakari Ailus pll->op_lanes = 1; 695cac8f5d2SSakari Ailus pll->vt_lanes = 1; 696cac8f5d2SSakari Ailus } 6979490a227SSakari Ailus 6986c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL) { 6996c7469e4SSakari Ailus op_lim_fr = &lim->op_fr; 7006c7469e4SSakari Ailus op_lim_bk = &lim->op_bk; 7016c7469e4SSakari Ailus op_pll_fr = &pll->op_fr; 7026c7469e4SSakari Ailus op_pll_bk = &pll->op_bk; 7036c7469e4SSakari Ailus } else if (pll->flags & CCS_PLL_FLAG_NO_OP_CLOCKS) { 7046c7469e4SSakari Ailus /* 7056c7469e4SSakari Ailus * If there's no OP PLL at all, use the VT values 7066c7469e4SSakari Ailus * instead. The OP values are ignored for the rest of 7076c7469e4SSakari Ailus * the PLL calculation. 7086c7469e4SSakari Ailus */ 7096c7469e4SSakari Ailus op_lim_fr = &lim->vt_fr; 7106c7469e4SSakari Ailus op_lim_bk = &lim->vt_bk; 7116c7469e4SSakari Ailus op_pll_fr = &pll->vt_fr; 7126c7469e4SSakari Ailus op_pll_bk = &pll->vt_bk; 7136c7469e4SSakari Ailus } else { 7146c7469e4SSakari Ailus op_lim_fr = &lim->vt_fr; 7156c7469e4SSakari Ailus op_lim_bk = &lim->op_bk; 7166c7469e4SSakari Ailus op_pll_fr = &pll->vt_fr; 7176c7469e4SSakari Ailus op_pll_bk = &pll->op_bk; 7186c7469e4SSakari Ailus } 7196c7469e4SSakari Ailus 720d7172c0eSSakari Ailus if (!pll->op_lanes || !pll->vt_lanes || !pll->bits_per_pixel || 721d7172c0eSSakari Ailus !pll->ext_clk_freq_hz || !pll->link_freq || !pll->scale_m || 722d7172c0eSSakari Ailus !op_lim_fr->min_pll_ip_clk_freq_hz || 723d7172c0eSSakari Ailus !op_lim_fr->max_pll_ip_clk_freq_hz || 724d7172c0eSSakari Ailus !op_lim_fr->min_pll_op_clk_freq_hz || 725d7172c0eSSakari Ailus !op_lim_fr->max_pll_op_clk_freq_hz || 726d7172c0eSSakari Ailus !op_lim_bk->max_sys_clk_div || !op_lim_fr->max_pll_multiplier) 727d7172c0eSSakari Ailus return -EINVAL; 728d7172c0eSSakari Ailus 7299490a227SSakari Ailus /* 7309490a227SSakari Ailus * Make sure op_pix_clk_div will be integer --- unless flexible 7319490a227SSakari Ailus * op_pix_clk_div is supported 7329490a227SSakari Ailus */ 7339490a227SSakari Ailus if (!(pll->flags & CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV) && 7349490a227SSakari Ailus (pll->bits_per_pixel * pll->op_lanes) % (pll->csi2.lanes * l)) { 7359490a227SSakari Ailus dev_dbg(dev, "op_pix_clk_div not an integer (bpp %u, op lanes %u, lanes %u, l %u)\n", 7369490a227SSakari Ailus pll->bits_per_pixel, pll->op_lanes, pll->csi2.lanes, l); 7379490a227SSakari Ailus return -EINVAL; 7389490a227SSakari Ailus } 7399490a227SSakari Ailus 740cac8f5d2SSakari Ailus dev_dbg(dev, "vt_lanes: %u\n", pll->vt_lanes); 741cac8f5d2SSakari Ailus dev_dbg(dev, "op_lanes: %u\n", pll->op_lanes); 742cac8f5d2SSakari Ailus 7439e05bbacSSakari Ailus dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal, 7449e05bbacSSakari Ailus pll->binning_vertical); 7459e05bbacSSakari Ailus 7469e05bbacSSakari Ailus switch (pll->bus_type) { 74747b6eaf3SSakari Ailus case CCS_PLL_BUS_TYPE_CSI2_DPHY: 7489e05bbacSSakari Ailus /* CSI transfers 2 bits per clock per lane; thus times 2 */ 749cab27256SSakari Ailus op_pll_bk->sys_clk_freq_hz = pll->link_freq * 2 750cac8f5d2SSakari Ailus * (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 751ae502e08SSakari Ailus 1 : pll->csi2.lanes); 7529e05bbacSSakari Ailus break; 7538030aa4fSSakari Ailus case CCS_PLL_BUS_TYPE_CSI2_CPHY: 7548030aa4fSSakari Ailus op_pll_bk->sys_clk_freq_hz = 7558030aa4fSSakari Ailus pll->link_freq 7568030aa4fSSakari Ailus * (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 7578030aa4fSSakari Ailus 1 : pll->csi2.lanes); 7588030aa4fSSakari Ailus break; 7599e05bbacSSakari Ailus default: 7609e05bbacSSakari Ailus return -EINVAL; 7619e05bbacSSakari Ailus } 7629e05bbacSSakari Ailus 763cac8f5d2SSakari Ailus pll->pixel_rate_csi = 7648030aa4fSSakari Ailus div_u64((uint64_t)op_pll_bk->sys_clk_freq_hz 765cac8f5d2SSakari Ailus * (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 7668030aa4fSSakari Ailus pll->csi2.lanes : 1) * PHY_CONST_DIV, 7678030aa4fSSakari Ailus phy_const * pll->bits_per_pixel * l); 768cac8f5d2SSakari Ailus 769415ddd99SSakari Ailus /* Figure out limits for OP pre-pll divider based on extclk */ 770415ddd99SSakari Ailus dev_dbg(dev, "min / max op_pre_pll_clk_div: %u / %u\n", 771415ddd99SSakari Ailus op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div); 772415ddd99SSakari Ailus max_op_pre_pll_clk_div = 773415ddd99SSakari Ailus min_t(uint16_t, op_lim_fr->max_pre_pll_clk_div, 7749e05bbacSSakari Ailus clk_div_even(pll->ext_clk_freq_hz / 775415ddd99SSakari Ailus op_lim_fr->min_pll_ip_clk_freq_hz)); 776415ddd99SSakari Ailus min_op_pre_pll_clk_div = 777415ddd99SSakari Ailus max_t(uint16_t, op_lim_fr->min_pre_pll_clk_div, 7789e05bbacSSakari Ailus clk_div_even_up( 7799e05bbacSSakari Ailus DIV_ROUND_UP(pll->ext_clk_freq_hz, 780415ddd99SSakari Ailus op_lim_fr->max_pll_ip_clk_freq_hz))); 781415ddd99SSakari Ailus dev_dbg(dev, "pre-pll check: min / max op_pre_pll_clk_div: %u / %u\n", 782415ddd99SSakari Ailus min_op_pre_pll_clk_div, max_op_pre_pll_clk_div); 7839e05bbacSSakari Ailus 784cab27256SSakari Ailus i = gcd(op_pll_bk->sys_clk_freq_hz, pll->ext_clk_freq_hz); 785cab27256SSakari Ailus mul = op_pll_bk->sys_clk_freq_hz / i; 7869e05bbacSSakari Ailus div = pll->ext_clk_freq_hz / i; 7879e05bbacSSakari Ailus dev_dbg(dev, "mul %u / div %u\n", mul, div); 7889e05bbacSSakari Ailus 789415ddd99SSakari Ailus min_op_pre_pll_clk_div = 790415ddd99SSakari Ailus max_t(uint16_t, min_op_pre_pll_clk_div, 7919e05bbacSSakari Ailus clk_div_even_up( 792482e75e7SSakari Ailus mul / 793482e75e7SSakari Ailus one_or_more( 794482e75e7SSakari Ailus DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz, 795482e75e7SSakari Ailus pll->ext_clk_freq_hz)))); 796415ddd99SSakari Ailus dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n", 797415ddd99SSakari Ailus min_op_pre_pll_clk_div, max_op_pre_pll_clk_div); 7989e05bbacSSakari Ailus 799415ddd99SSakari Ailus for (op_pll_fr->pre_pll_clk_div = min_op_pre_pll_clk_div; 800415ddd99SSakari Ailus op_pll_fr->pre_pll_clk_div <= max_op_pre_pll_clk_div; 8014e1e8d24SSakari Ailus op_pll_fr->pre_pll_clk_div += 8024e1e8d24SSakari Ailus (pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER) ? 1 : 8034e1e8d24SSakari Ailus 2 - (op_pll_fr->pre_pll_clk_div & 1)) { 804a38836b2SSakari Ailus rval = ccs_pll_calculate_op(dev, lim, op_lim_fr, op_lim_bk, pll, 8058030aa4fSSakari Ailus op_pll_fr, op_pll_bk, mul, div, l, 8068030aa4fSSakari Ailus cphy, phy_const); 8079e05bbacSSakari Ailus if (rval) 8089e05bbacSSakari Ailus continue; 8099e05bbacSSakari Ailus 810*b41f2708SSakari Ailus rval = check_fr_bounds(dev, lim, pll, 811*b41f2708SSakari Ailus pll->flags & CCS_PLL_FLAG_DUAL_PLL ? 812*b41f2708SSakari Ailus PLL_OP : PLL_VT); 813f25d3962SSakari Ailus if (rval) 814f25d3962SSakari Ailus continue; 815f25d3962SSakari Ailus 816f25d3962SSakari Ailus rval = check_bk_bounds(dev, lim, pll, PLL_OP); 817f25d3962SSakari Ailus if (rval) 818f25d3962SSakari Ailus continue; 819f25d3962SSakari Ailus 8206c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL) 8216c7469e4SSakari Ailus break; 8226c7469e4SSakari Ailus 823a38836b2SSakari Ailus ccs_pll_calculate_vt(dev, lim, op_lim_bk, pll, op_pll_fr, 824a38836b2SSakari Ailus op_pll_bk, cphy, phy_const); 825a38836b2SSakari Ailus 826f25d3962SSakari Ailus rval = check_bk_bounds(dev, lim, pll, PLL_VT); 827f25d3962SSakari Ailus if (rval) 828f25d3962SSakari Ailus continue; 829f25d3962SSakari Ailus rval = check_ext_bounds(dev, pll); 830a38836b2SSakari Ailus if (rval) 831a38836b2SSakari Ailus continue; 832a38836b2SSakari Ailus 8336c7469e4SSakari Ailus break; 8349e05bbacSSakari Ailus } 8359e05bbacSSakari Ailus 8366c7469e4SSakari Ailus if (rval) { 8379e05bbacSSakari Ailus dev_dbg(dev, "unable to compute pre_pll divisor\n"); 8389e05bbacSSakari Ailus 8399e05bbacSSakari Ailus return rval; 8409e05bbacSSakari Ailus } 8416c7469e4SSakari Ailus 8426c7469e4SSakari Ailus if (pll->flags & CCS_PLL_FLAG_DUAL_PLL) { 8436c7469e4SSakari Ailus rval = ccs_pll_calculate_vt_tree(dev, lim, pll); 8446c7469e4SSakari Ailus 8456c7469e4SSakari Ailus if (rval) 8466c7469e4SSakari Ailus return rval; 8476c7469e4SSakari Ailus } 8486c7469e4SSakari Ailus 8496c7469e4SSakari Ailus print_pll(dev, pll); 8506c7469e4SSakari Ailus 8516c7469e4SSakari Ailus return 0; 8526c7469e4SSakari Ailus } 8539e05bbacSSakari Ailus EXPORT_SYMBOL_GPL(ccs_pll_calculate); 8549e05bbacSSakari Ailus 8557389d01cSSakari Ailus MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); 8569e05bbacSSakari Ailus MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ PLL calculator"); 857b3c0115eSSakari Ailus MODULE_LICENSE("GPL v2"); 858