Skip to contentSkip to conversation

CCS PLL fixes and improvements

PATCH Superseded Sakari Ailus 1 participant 8 messages Feb 26, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

cover CCS PLL fixes and improvements

Hi folks,

Here are a few fixes and improvements to the CCS PLL calculator.

Sakari Ailus (7):
media: ccs-pll: Start OP pre-PLL multiplier search from correct value
media: ccs-pll: Start VT pre-PLL multiplier search from correct value
media: ccs-pll: Check for too high VT PLL multiplier in dual PLL case
media: ccs-pll: Correctly the upper limit of maximum
op_pre_pll_clk_div
media: ccs-pll: Print a debug message on too high VT PLL OP clock
media: ccs-pll: Add a flag for even PLL multipliers
media: ccs-pll: Better validate VT PLL branch

 drivers/media/i2c/ccs-pll.c | 43 +++++++++++++++++++++++++++++--------
 drivers/media/i2c/ccs-pll.h |  1 +
 2 files changed, 35 insertions(+), 9 deletions(-)
Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH 1/7 media: ccs-pll: Start OP pre-PLL multiplier search from correct value

The ccs_pll_calculate() function does a search over possible PLL configurations to find the "best" one. If the sensor did not support odd pre-PLL divisors and the minimum value (with constraints) wasn't 1, other even values could have errorneously searched (and selected) for the pre-PLL divisor. Fix this.

Fixes 415ddd993978 ("media: ccs-pll: Split limits and PLL configuration into front and back parts")
---
 drivers/media/i2c/ccs-pll.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/i2c/ccs-pll.c +2 -0
index cf8858cb13d4..6f99cb8be02a 100644
@@ -815,6 +815,8 @@ int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
 			      one_or_more(
 				      DIV_ROUND_UP(op_lim_fr->max_pll_op_clk_freq_hz,
 						   pll->ext_clk_freq_hz))));
+	if (!(pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER))
+		min_op_pre_pll_clk_div = clk_div_even(min_op_pre_pll_clk_div);
 	dev_dbg(dev, "pll_op check: min / max op_pre_pll_clk_div: %u / %u\n",
 		min_op_pre_pll_clk_div, max_op_pre_pll_clk_div);
 
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH 2/7 media: ccs-pll: Start VT pre-PLL multiplier search from correct value

In reply to Sakari Ailus

The ccs_pll_calculate_vt_tree() function does a search over possible VT PLL configurations to find the "best" one. If the sensor did not support odd pre-PLL divisors and the minimum value (with constraints) wasn't 1, other even values could have errorneously searched (and selected) for the pre-PLL divisor. Fix this.

Fixes 415ddd993978 ("media: ccs-pll: Split limits and PLL configuration into front and back parts")
---
 drivers/media/i2c/ccs-pll.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/i2c/ccs-pll.c +2 -0
index 6f99cb8be02a..98ffbdf565f3 100644
@@ -397,6 +397,8 @@ static int ccs_pll_calculate_vt_tree(struct device *dev,
 	min_pre_pll_clk_div = max_t(u16, min_pre_pll_clk_div,
 				    pll->ext_clk_freq_hz /
 				    lim_fr->max_pll_ip_clk_freq_hz);
+	if (!(pll->flags & CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER))
+		min_pre_pll_clk_div = clk_div_even(min_pre_pll_clk_div);
 
 	dev_dbg(dev, "vt min/max_pre_pll_clk_div: %u,%u\n",
 		min_pre_pll_clk_div, max_pre_pll_clk_div);
Signature · 1 line
2.39.5
author maintainer
#3 lore raw

PATCH 3/7 media: ccs-pll: Check for too high VT PLL multiplier in dual PLL case

In reply to Sakari Ailus

The check for VT PLL upper limit in dual PLL case was missing. Add it now.

Fixes 6c7469e46b60 ("media: ccs-pll: Add trivial dual PLL support")
---
 drivers/media/i2c/ccs-pll.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/media/i2c/ccs-pll.c +5 -0
index 98ffbdf565f3..9041c020e34b 100644
@@ -312,6 +312,11 @@ __ccs_pll_calculate_vt_tree(struct device *dev,
 	dev_dbg(dev, "more_mul2: %u\n", more_mul);
 
 	pll_fr->pll_multiplier = mul * more_mul;
+	if (pll_fr->pll_multiplier > lim_fr->max_pll_multiplier) {
+		dev_dbg(dev, "pll multiplier %u too high\n",
+			pll_fr->pll_multiplier);
+		return -EINVAL;
+	}
 
 	if (pll_fr->pll_multiplier * pll_fr->pll_ip_clk_freq_hz >
 	    lim_fr->max_pll_op_clk_freq_hz)
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

PATCH 4/7 media: ccs-pll: Correctly the upper limit of maximum op_pre_pll_clk_div

In reply to Sakari Ailus

The PLL calculator does a search of the PLL configuration space for all valid OP pre-PLL clock dividers. The maximum did not take into account CCS PLL flag CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER in which case also odd PLL dividers (other than 1) are valid. Do that now.

Fixes 4e1e8d240dff ("media: ccs-pll: Add support for extended input PLL clock divider")
---
 drivers/media/i2c/ccs-pll.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/ccs-pll.c +1 -1
index 9041c020e34b..611c9823be85 100644
@@ -799,7 +799,7 @@ int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
 		op_lim_fr->min_pre_pll_clk_div, op_lim_fr->max_pre_pll_clk_div);
 	max_op_pre_pll_clk_div =
 		min_t(u16, op_lim_fr->max_pre_pll_clk_div,
-		      clk_div_even(pll->ext_clk_freq_hz /
+		      DIV_ROUND_UP(pll->ext_clk_freq_hz,
 				   op_lim_fr->min_pll_ip_clk_freq_hz));
 	min_op_pre_pll_clk_div =
 		max_t(u16, op_lim_fr->min_pre_pll_clk_div,
Signature · 1 line
2.39.5
author maintainer
#5 lore raw

PATCH 5/7 media: ccs-pll: Print a debug message on too high VT PLL OP clock

In reply to Sakari Ailus

In general the CCS PLL calculator prints debugging information on the process to ease debugging. This case was not annotated, do that now.

Remove an extra multiplication while at it.

---
 drivers/media/i2c/ccs-pll.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/media/i2c/ccs-pll.c +5 -4
index 611c9823be85..66281f676707 100644
@@ -318,12 +318,13 @@ __ccs_pll_calculate_vt_tree(struct device *dev,
 		return -EINVAL;
 	}
 
-	if (pll_fr->pll_multiplier * pll_fr->pll_ip_clk_freq_hz >
-	    lim_fr->max_pll_op_clk_freq_hz)
-		return -EINVAL;
-
 	pll_fr->pll_op_clk_freq_hz =
 		pll_fr->pll_ip_clk_freq_hz * pll_fr->pll_multiplier;
+	if (pll_fr->pll_op_clk_freq_hz > lim_fr->max_pll_op_clk_freq_hz) {
+		dev_dbg(dev, "too high OP clock %u\n",
+			pll_fr->pll_op_clk_freq_hz);
+		return -EINVAL;
+	}
 
 	vt_div = div * more_mul;
 
Signature · 1 line
2.39.5
author maintainer
#6 lore raw

PATCH 6/7 media: ccs-pll: Add a flag for even PLL multipliers

In reply to Sakari Ailus

Some devices (not entirely CCS compliant) only support even PLL multipliers. Add support for this through a PLL flag.

---
 drivers/media/i2c/ccs-pll.c | 13 +++++++++++--
 drivers/media/i2c/ccs-pll.h |  1 +
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/media/i2c/ccs-pll.c +11 -2
index 66281f676707..fa3c577fbc0c 100644
@@ -124,7 +124,7 @@ static void print_pll(struct device *dev, struct ccs_pll *pll)
 	dev_dbg(dev, "pixel rate on CSI-2 bus:\t%u\n",
 		pll->pixel_rate_csi);
 
-	dev_dbg(dev, "flags%s%s%s%s%s%s%s%s%s\n",
+	dev_dbg(dev, "flags%s%s%s%s%s%s%s%s%s%s\n",
 		pll->flags & PLL_FL(LANE_SPEED_MODEL) ? " lane-speed" : "",
 		pll->flags & PLL_FL(LINK_DECOUPLED) ? " link-decoupled" : "",
 		pll->flags & PLL_FL(EXT_IP_PLL_DIVIDER) ?
@@ -135,7 +135,8 @@ static void print_pll(struct device *dev, struct ccs_pll *pll)
 		pll->flags & PLL_FL(FIFO_OVERRATING) ? " fifo-overrating" : "",
 		pll->flags & PLL_FL(DUAL_PLL) ? " dual-pll" : "",
 		pll->flags & PLL_FL(OP_SYS_DDR) ? " op-sys-ddr" : "",
-		pll->flags & PLL_FL(OP_PIX_DDR) ? " op-pix-ddr" : "");
+		pll->flags & PLL_FL(OP_PIX_DDR) ? " op-pix-ddr" : "",
+		pll->flags & PLL_FL(EVEN_PLL_MULTIPLIER) ? " even-pll-multiplier" : "");
 }
 
 static u32 op_sys_ddr(u32 flags)
@@ -311,6 +312,10 @@ __ccs_pll_calculate_vt_tree(struct device *dev,
 	more_mul *= DIV_ROUND_UP(lim_fr->min_pll_multiplier, mul * more_mul);
 	dev_dbg(dev, "more_mul2: %u\n", more_mul);
 
+	if (pll->flags & CCS_PLL_FLAG_EVEN_PLL_MULTIPLIER &&
+	    mul & 1 && more_mul & 1)
+		more_mul <<= 1;
+
 	pll_fr->pll_multiplier = mul * more_mul;
 	if (pll_fr->pll_multiplier > lim_fr->max_pll_multiplier) {
 		dev_dbg(dev, "pll multiplier %u too high\n",
@@ -667,6 +672,10 @@ ccs_pll_calculate_op(struct device *dev, const struct ccs_pll_limits *lim,
 	if (!is_one_or_even(i))
 		i <<= 1;
 
+	if (pll->flags & CCS_PLL_FLAG_EVEN_PLL_MULTIPLIER &&
+	    mul & 1 && i & 1)
+		i <<= 1;
+
 	dev_dbg(dev, "final more_mul: %u\n", i);
 	if (i > more_mul_max) {
 		dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max);
drivers/media/i2c/ccs-pll.h +1 -0
index 6eb1b1c68e1e..fce2bbf84c99 100644
@@ -32,6 +32,7 @@
 #define CCS_PLL_FLAG_DUAL_PLL					BIT(8)
 #define CCS_PLL_FLAG_OP_SYS_DDR					BIT(9)
 #define CCS_PLL_FLAG_OP_PIX_DDR					BIT(10)
+#define CCS_PLL_FLAG_EVEN_PLL_MULTIPLIER			BIT(11)
 
 /**
  * struct ccs_pll_branch_fr - CCS PLL configuration (front)
Signature · 1 line
2.39.5
author maintainer
#7 lore raw

PATCH 7/7 media: ccs-pll: Better validate VT PLL branch

In reply to Sakari Ailus

Check that the VT PLL dividers are actually found, don't trust they always are even though they should be.

---
 drivers/media/i2c/ccs-pll.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/media/i2c/ccs-pll.c +9 -3
index fa3c577fbc0c..49cc4491ebdd 100644
@@ -448,7 +448,7 @@ static int ccs_pll_calculate_vt_tree(struct device *dev,
 	return -EINVAL;
 }
 
-static void
+static int
 ccs_pll_calculate_vt(struct device *dev, const struct ccs_pll_limits *lim,
 		     const struct ccs_pll_branch_limits_bk *op_lim_bk,
 		     struct ccs_pll *pll, struct ccs_pll_branch_fr *pll_fr,
@@ -571,6 +571,8 @@ ccs_pll_calculate_vt(struct device *dev, const struct ccs_pll_limits *lim,
 		if (best_pix_div < SHRT_MAX >> 1)
 			break;
 	}
+	if (best_pix_div == SHRT_MAX >> 1)
+		return -EINVAL;
 
 	pll->vt_bk.sys_clk_div = DIV_ROUND_UP(vt_div, best_pix_div);
 	pll->vt_bk.pix_clk_div = best_pix_div;
@@ -583,6 +585,8 @@ ccs_pll_calculate_vt(struct device *dev, const struct ccs_pll_limits *lim,
 out_calc_pixel_rate:
 	pll->pixel_rate_pixel_array =
 		pll->vt_bk.pix_clk_freq_hz * pll->vt_lanes;
+
+	return 0;
 }
 
 /*
@@ -862,8 +866,10 @@ int ccs_pll_calculate(struct device *dev, const struct ccs_pll_limits *lim,
 		if (pll->flags & CCS_PLL_FLAG_DUAL_PLL)
 			break;
 
-		ccs_pll_calculate_vt(dev, lim, op_lim_bk, pll, op_pll_fr,
-				     op_pll_bk, cphy, phy_const);
+		rval = ccs_pll_calculate_vt(dev, lim, op_lim_bk, pll, op_pll_fr,
+					    op_pll_bk, cphy, phy_const);
+		if (rval)
+			continue;
 
 		rval = check_bk_bounds(dev, lim, pll, PLL_VT);
 		if (rval)
Signature · 1 line
2.39.5