mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Balakrishnan Sambath <balakrishnan.s@microchip.com>
To: <ehristev@kernel.org>
Cc: <linux-kernel@vger.kernel.org>,
	Balakrishnan Sambath <balakrishnan.s@microchip.com>
Subject: [PATCH 13/15] media: microchip-isc: smooth AWB gains with an EMA filter
Date: Wed, 2 Dec 2026 12:46:19 +0530	[thread overview]
Message-ID: <20261202-isc-series-v1-v1-13-4e2f17c0271c@microchip.com> (raw)
In-Reply-To: <20261202-isc-series-v1-v1-0-4e2f17c0271c@microchip.com>

The grey world gains are recomputed from every histogram and follow
sensor noise from frame to frame. Filter them with an exponential moving
average, gain_smooth = (3 * gain_smooth + gain + 2) / 4 (alpha = 0.25),
and write the result to ctrls->gain[]. Only ISC_WB_AUTO filters. One-time
white balance and a channel held neutral load gain_smooth[] directly.

Manual gains are written to ctrls->gain[] only, so seed gain_smooth[]
from them when AWB is switched on. Otherwise the filter would start from
the last automatic result. Take awb_mutex before the mode and the manual
gains are written, so the AWB work cannot interleave with the seed.

Signed-off-by: Balakrishnan Sambath <balakrishnan.s@microchip.com>
---
 .../media/platform/microchip/microchip-isc-base.c  | 37 ++++++++++++++++++----
 drivers/media/platform/microchip/microchip-isc.h   |  1 +
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/drivers/media/platform/microchip/microchip-isc-base.c b/drivers/media/platform/microchip/microchip-isc-base.c
index a767dfc719f3..f40088246651 100644
--- a/drivers/media/platform/microchip/microchip-isc-base.c
+++ b/drivers/media/platform/microchip/microchip-isc-base.c
@@ -86,6 +86,7 @@ static inline void isc_reset_awb_ctrls(struct isc_device *isc)
 	for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
 		/* gains have a fixed point at 9 decimals */
 		isc->ctrls.gain[c] = 1 << 9;
+		isc->ctrls.gain_smooth[c] = 1 << 9;
 		/* offsets are in 2's complements */
 		isc->ctrls.offset[c] = 0;
 	}
@@ -1342,6 +1343,7 @@ static void isc_wb_update(struct isc_ctrls *ctrls)
 	u32 *channel_avg = &ctrls->channel_avg[0];
 	u32 c, offset[4];
 	u32 min_pixels;
+	u32 gain;
 	u64 avg = 0;
 	/* We compute two gains, stretch gain and grey world gain */
 	u32 s_gain[4], gw_gain[4];
@@ -1376,6 +1378,7 @@ static void isc_wb_update(struct isc_ctrls *ctrls)
 		if (ctrls->total_pixels[c] < min_pixels) {
 			ctrls->offset[c] = 0;
 			ctrls->gain[c] = 1 << 9;
+			ctrls->gain_smooth[c] = 1 << 9;
 			continue;
 		}
 
@@ -1428,12 +1431,22 @@ static void isc_wb_update(struct isc_ctrls *ctrls)
 			"isc wb: component %d, s_gain %u, gw_gain %u\n",
 			c, s_gain[c], gw_gain[c]);
 		/* multiply both gains and adjust for decimals */
-		/* the product of two Q9 gains can exceed 32 bits */
-		ctrls->gain[c] = min_t(u64, ((u64)s_gain[c] * gw_gain[c]) >> 9,
-				       GENMASK(12, 0));
+		/* Clamp before smoothing so one outlier cannot saturate it */
+		gain = min_t(u64, ((u64)s_gain[c] * gw_gain[c]) >> 9,
+			     GENMASK(12, 0));
 
-		dev_dbg(isc->dev, "isc wb: component %d, final gain %u\n",
-			c, ctrls->gain[c]);
+		/* 1/4 EMA, +2 rounds to nearest. Only auto AWB filters */
+		if (ctrls->awb != ISC_WB_AUTO)
+			ctrls->gain_smooth[c] = gain;
+		else
+			ctrls->gain_smooth[c] =
+				(3 * ctrls->gain_smooth[c] + gain + 2) / 4;
+
+		ctrls->gain[c] = ctrls->gain_smooth[c];
+
+		dev_dbg(isc->dev,
+			"isc wb: component %d, gain %u, smooth %u\n",
+			c, gain, ctrls->gain_smooth[c]);
 	}
 }
 
@@ -1463,7 +1476,10 @@ static void isc_awb_work(struct work_struct *w)
 	if (hist_id != ISC_HIS_CFG_MODE_B) {
 		hist_id++;
 	} else {
+		/* gain_smooth is also seeded from the control path */
+		mutex_lock(&isc->awb_mutex);
 		isc_wb_update(ctrls);
+		mutex_unlock(&isc->awb_mutex);
 		hist_id = ISC_HIS_CFG_MODE_GR;
 	}
 
@@ -1571,12 +1587,17 @@ static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
 	struct isc_device *isc = container_of(ctrl->handler,
 					     struct isc_device, ctrls.handler);
 	struct isc_ctrls *ctrls = &isc->ctrls;
+	bool was_auto;
 
 	if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
 		return 0;
 
 	switch (ctrl->id) {
 	case V4L2_CID_AUTO_WHITE_BALANCE:
+		/* The AWB work reads the mode and the gains under this mutex */
+		mutex_lock(&isc->awb_mutex);
+		was_auto = ctrls->awb == ISC_WB_AUTO;
+
 		if (ctrl->val == 1)
 			ctrls->awb = ISC_WB_AUTO;
 		else
@@ -1601,7 +1622,11 @@ static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
 		if (ctrl->cluster[ISC_CTRL_GB_OFF]->is_new)
 			ctrls->offset[ISC_HIS_CFG_MODE_GB] = isc->gb_off_ctrl->val;
 
-		mutex_lock(&isc->awb_mutex);
+		/* Start the EMA from the gains in use, not the last AWB run */
+		if (!was_auto && ctrls->awb == ISC_WB_AUTO)
+			memcpy(ctrls->gain_smooth, ctrls->gain,
+			       sizeof(ctrls->gain_smooth));
+
 		if (vb2_is_streaming(&isc->vb2_vidq) && !isc->stop) {
 			unsigned long flags;
 
diff --git a/drivers/media/platform/microchip/microchip-isc.h b/drivers/media/platform/microchip/microchip-isc.h
index 2c3efd2fc56e..b17a216ca109 100644
--- a/drivers/media/platform/microchip/microchip-isc.h
+++ b/drivers/media/platform/microchip/microchip-isc.h
@@ -151,6 +151,7 @@ struct isc_ctrls {
 
 	/* one for each component : GR, R, GB, B */
 	u32 gain[HIST_BAYER];
+	u32 gain_smooth[HIST_BAYER];
 	s32 offset[HIST_BAYER];
 
 	u32 hist_entry[HIST_ENTRIES];

-- 
2.34.1


  parent reply	other threads:[~2026-12-02  7:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  7:16 [PATCH 00/15] media: microchip-isc: fix SAMA7G5 image pipeline and AWB Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 01/15] dt-bindings: media: microchip,xisc: add SAM9X7 compatible Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 02/15] media: microchip-isc: set SAM9X7 maximum resolution per SoC Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 03/15] media: microchip-isc: fix DPC black level scale Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 04/15] media: microchip-isc: enable black level correction on SAMA7G5 Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 05/15] media: microchip-isc: bypass SAMA7G5 pipeline for Bayer output Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 06/15] media: microchip-isc: add gamma 1.8 and 2.4 curves for SAMA7G5 Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 07/15] media: microchip-isc: fix CBHS register names Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 08/15] media: microchip-isc: add SAMA7G5 hue and saturation controls Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 09/15] media: microchip-isc: fix histogram bin index for AWB Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 10/15] media: microchip-isc: trim outliers in the AWB average Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 11/15] media: microchip-isc: skip AWB on near-empty channels Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 12/15] media: microchip-isc: do the AWB gain product in 64 bits Balakrishnan Sambath
2026-12-02  7:16 ` Balakrishnan Sambath [this message]
2026-12-02  7:16 ` [PATCH 14/15] media: microchip-isc: use an unsigned contrast range Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 15/15] media: microchip-isc: add driver documentation Balakrishnan Sambath

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202-isc-series-v1-v1-13-4e2f17c0271c@microchip.com \
    --to=balakrishnan.s@microchip.com \
    --cc=ehristev@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®