From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=68.232.153.233 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796195797; b=U+oWQc71rhWaFcKZ3PFFldlIoSglfr+basoprce/jx47We98Qm6E1oRJnZJFSPwRzl/x UAJjTUy/T8DmX1tMM3MiBjM02EvZ4dVt417b7ybAZ6FA9jKz8nldGOYHog6wwPbE7oP9y jghurllOOHfEILz3c/tSzRFQuQJw/OEpyyoig6xdCK5B2AJ6SASjqHOb8ZP5J5jGpfH83 gXyih6BQpu5R+VtaTYwssihkW+0Jzs8Aew58V2/WzeF4NQa5EUVvm08m1XFggLcvpwhgs pRcSDxhvsJQ4JOkMqOk+1Xro8deKMK+N/IeMyarZQdQ+aA6M2r/XjexnE7GA0+YKdig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796195797; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-Amp-Result:Received:Received:Received:From:Date: Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-ID:References:In-Reply-To:To:CC:X-Mailer; bh=a9Rmi7HLIz80zcZjVjwlfjn6U1iLK4cGGeXSJ9e58b8=; b=KRN0MiWyPmZV05y8ZewyKwQrDNMaPCZ+a+i4MQuumzb3LvKEjOpA0dH+zjl7LFJaIF9K VtteXFuWY1BAnXfdbFVv9tXfJfPyjrD5LnDhz970QzToxfn+XIIy0J+EtZie3PPFj288m TgNDZl+5pGNCVVoJo4IESK8db0O86XR5eD6NeUnPiu7ubyAHS/5RamSoVv4FT1OdlM1GY kTmG1TD9p8c93hvfZkPr29wiq3g+8Xx0rNrOdNKddMYFX1eZqOI/6XX7pDLOAy9nMqLpz 6fc7qv7dv/wTvXaczQRr+NB8+lXr9G+sivv/lwHy3i5xGBtx05MVcEC/DO4AZW8Cf4g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=microchip.com; spf=pass smtp.mailfrom=microchip.com; dkim=pass header.d=microchip.com header.i=@microchip.com header.a=rsa-sha256 header.s=mchp header.b=jHhPdeh+; arc=none smtp.remote-ip=68.232.153.233 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8B52606A335 for ; Wed, 2 Dec 2026 07:16:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E8B52606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=microchip.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.a=rsa-sha256 header.s=mchp header.b=jHhPdeh+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1796195797; x=1827731797; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=ZYhQbc+/GGHw4C2J4AhWGTGGZjeJicaBkdK+FvAkshM=; b=jHhPdeh+vXwA/49I0aH69A48fE1YB/5PNExlyQjdCxxTbWagf3Zu1Tb7 CHyMeBlRn4ADM0tG7C2zmYbB6BoXSo0gl9NSes+hDCXEy5D0MMfxZRh9H 1CWHGtqP+rhiEP1wR5E9kooF434zzxw61ed2jHF59DtqF74IckWTujPXW 9yo3k7ycdTEl6YN8ZTkdAoURW2/swsvKa+oN6EgWGBbjTjfSU1SOhzf+p B1+POUbcMzam/MZLxk/v1Fy1BCzUqvDvUITaB4vkBdrUEAmEnjQXCZE+F whCcR/+C3zk6ts3ZNkvUokPx+wo0jNh860Af10d0tTm0YwMYhHC8oV/PV g==; X-CSE-ConnectionGUID: I2dYQ1G7QrGEFTiYu1jMSQ== X-CSE-MsgGUID: QBJmrsruSQqzLbxZ6nKaJg== X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="298747278" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa5.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 00:16:37 -0700 Received: from chn-vm-ex01.mchp-main.com (10.10.87.71) by chn-vm-ex1.mchp-main.com (10.10.87.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.57; Wed, 2 Dec 2026 00:16:36 -0700 Received: from [127.0.0.1] (10.10.85.11) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server id 15.1.2507.58 via Frontend Transport; Wed, 2 Dec 2026 00:16:34 -0700 From: Balakrishnan Sambath Date: Wed, 2 Dec 2026 12:46:19 +0530 Subject: [PATCH 13/15] media: microchip-isc: smooth AWB gains with an EMA filter X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <20261202-isc-series-v1-v1-13-4e2f17c0271c@microchip.com> References: <20261202-isc-series-v1-v1-0-4e2f17c0271c@microchip.com> In-Reply-To: <20261202-isc-series-v1-v1-0-4e2f17c0271c@microchip.com> To: CC: , Balakrishnan Sambath X-Mailer: b4 0.14.3 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 --- .../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