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=1796195791; b=kI8IgfpMAj1q+tppAsCBJZePeYoBugb+7lDqKVoz/HbZKTcZaeq0ZhCUOAZ6MNUQOMBb 9d1slhhQKa+eHxsr2bjmPU0Y8CA1DZQKCmsMhkpzY4XOBPeSXR08qLXFJDxFassy4KuwB yh+Tye+rWVdRWS4JAeHxr+uPDQjdQkgAUijBjGOJmaLKsIjlouii5+nSB/GO2gmMENeNr zSaRuu7KBoWnN+tgnUp9w5+XX71/Xl+XsLh62rK27CYxIwLV5AV1KJTnFAKTodBWUbqhv z6/Gwqzqw/fJk7CP4iXcweOGIAGYRRJyHdAlstfOm0aeAovvvXz07Zv8KDNdJ0mmaQA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796195791; 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=c1JoRNgMjCqYo1qvurAtNz5RKc1ItvkooZTCeWKQnWE=; b=G6WEi1SiKJLvQo4O5yG6BfT1wWjQd2Vn5gOFHJdwiBCvcAm5nseMg/eiUTh8k/+EBBrK /iYOohCxwdOsxL0yr3UYpwxr5yhtP1PMicyUr4RNNjSHRAMh0EvHaXfqJX4CQtCv5l31L Ej9AwsUXmGlxDhIw6IX9+XbIBN241EA3zpKcaH/0uI48h8wc6HDuRj//Ble21QMmxuHK2 M+e2cyQ49gEuHF/P+OYWN+zhg9M+FY0rpmdWI4GqMNVvHk1Jk67UuurcmUFpBPdkPj1cs KvVP/wU3W/LS/p629HbrttRJDdD6Va0+F/9OGyyBQJ68BLBCFcyMhtK3VytmhGwYmvA== 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=VjLwiLdF; 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 7B9FF606A68E for ; Wed, 2 Dec 2026 07:16:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7B9FF606A68E 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=VjLwiLdF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1796195792; x=1827731792; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=k7sRqFD7emGJAEXkmCZt/nhOJLkmbZv+NRvcwaLK5qk=; b=VjLwiLdFW8oex0Zy6kArNuKkXciCBLurGtL504L2akKTiuRaygBgko2M NfTgUKdicPxRhFGDa2MfHdHl+8VbmidVD/kH6L8BbPouVyQAXBhcGraG/ TNOsVZsInAL13hw77zsbh33JUvyXi2XgZ4ZivA+mtKLGkf1scCJ1TmHma KDEpCfHwHydP6z2XF6bQB4wa6vAssxl3N8zBfp4VQxiTaQn+GA5VYHPGJ ukERRvuSZQQEzI8XenTzNpRa2Lx5B6wJ+GPT1s4MfZsIxuWTd7lPlj2s2 9tl8pmPsA8JsQBiR8OjuunpEqrruWNZHcCZVyiRqOKWPMQccMcBRCMMYt g==; X-CSE-ConnectionGUID: 6x1aN4LbQkuNz8jqKYFBzw== X-CSE-MsgGUID: 05s6/YKxRTGTlYTuhGep2A== X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="298747273" 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-AES128-GCM-SHA256; 02 Dec 2026 00:16:31 -0700 Received: from chn-vm-ex01.mchp-main.com (10.10.85.143) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.58; Wed, 2 Dec 2026 00:16:30 -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:29 -0700 From: Balakrishnan Sambath Date: Wed, 2 Dec 2026 12:46:16 +0530 Subject: [PATCH 10/15] media: microchip-isc: trim outliers in the AWB average 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-10-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 Skip the darkest and brightest 2% of each channel histogram (ISC_AWB_OUTLIER_DIV) when picking min and max, so a few hot or dark pixels no longer set the stretch range. Keep the mean over the remaining bins in ctrls->channel_avg[] in place of the raw sum, hist_count[]. The pixel count left after the trim differs per channel, so the raw sums no longer compare across channels. An empty histogram reports a zero mean, so the null green check in isc_wb_update() still returns early and the gains stay as they are. The trim counts bins 0 to 510 only, the last bin stays out as before, so the outlier budget is taken from the same count. When the bins above min fit in the budget, max settles on min and the mean is that bin. Signed-off-by: Balakrishnan Sambath --- .../media/platform/microchip/microchip-isc-base.c | 78 +++++++++++++++------- drivers/media/platform/microchip/microchip-isc.h | 2 +- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/drivers/media/platform/microchip/microchip-isc-base.c b/drivers/media/platform/microchip/microchip-isc-base.c index b2c19879e1c6..2787de606b4a 100644 --- a/drivers/media/platform/microchip/microchip-isc-base.c +++ b/drivers/media/platform/microchip/microchip-isc-base.c @@ -40,6 +40,9 @@ (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \ (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8))) +/* Skip the darkest and brightest 2% of the histogram as outliers */ +#define ISC_AWB_OUTLIER_DIV 50 + static inline void isc_update_v4l2_ctrls(struct isc_device *isc) { struct isc_ctrls *ctrls = &isc->ctrls; @@ -1275,41 +1278,66 @@ static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max) { struct regmap *regmap = isc->regmap; struct isc_ctrls *ctrls = &isc->ctrls; - u32 *hist_count = &ctrls->hist_count[ctrls->hist_id]; u32 *hist_entry = &ctrls->hist_entry[0]; - bool seen = false; + u64 weighted_sum = 0; + u32 cumulative, outlier; + u32 total_pixels = 0; + u32 pixels = 0; u32 i; - *min = 0; - *max = HIST_ENTRIES; - regmap_bulk_read(regmap, ISC_HIS_ENTRY + isc->offsets.his_entry, hist_entry, HIST_ENTRIES); - *hist_count = 0; - /* - * we deliberately ignore the end of the histogram, - * the most white pixels - */ + /* The last bin, the most white pixels, is deliberately left out */ + for (i = 0; i < HIST_ENTRIES - 1; i++) + total_pixels += hist_entry[i]; + + /* No pixels, a zero average leaves the gains alone */ + if (!total_pixels) { + ctrls->channel_avg[ctrls->hist_id] = 0; + *min = 0; + *max = HIST_ENTRIES - 2; + return; + } + + outlier = total_pixels / ISC_AWB_OUTLIER_DIV; + + /* The effective minimum skips the dark outliers */ + cumulative = 0; for (i = 0; i < HIST_ENTRIES - 1; i++) { - if (!hist_entry[i]) - continue; - if (!seen) { - *min = i; - seen = true; - } - *max = i; - *hist_count += i * hist_entry[i]; + cumulative += hist_entry[i]; + if (cumulative > outlier) + break; + } + *min = i; + + /* The maximum skips the bright ones and stops at the minimum */ + cumulative = 0; + for (i = HIST_ENTRIES - 2; i > *min; i--) { + cumulative += hist_entry[i]; + if (cumulative > outlier) + break; } + *max = i; - dev_dbg(isc->dev, "isc wb: hist_id %u, hist_count %u", - ctrls->hist_id, *hist_count); + for (i = *min; i <= *max; i++) { + pixels += hist_entry[i]; + weighted_sum += (u64)i * hist_entry[i]; + } + + /* The minimum bin is occupied, so pixels is never zero */ + ctrls->channel_avg[ctrls->hist_id] = div_u64(weighted_sum, pixels); + + dev_dbg(isc->dev, + "isc wb: hist_id %u, avg %u, range [%u,%u], total %u\n", + ctrls->hist_id, ctrls->channel_avg[ctrls->hist_id], + *min, *max, total_pixels); } static void isc_wb_update(struct isc_ctrls *ctrls) { struct isc_device *isc = container_of(ctrls, struct isc_device, ctrls); - u32 *hist_count = &ctrls->hist_count[0]; + u32 *channel_avg = &ctrls->channel_avg[0]; u32 c, offset[4]; u64 avg = 0; /* We compute two gains, stretch gain and grey world gain */ @@ -1321,8 +1349,8 @@ static void isc_wb_update(struct isc_ctrls *ctrls) * Thus we want to keep Green as fixed and adjust only Red/Blue * Compute the average of the both green channels first */ - avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] + - (u64)hist_count[ISC_HIS_CFG_MODE_GB]; + avg = (u64)channel_avg[ISC_HIS_CFG_MODE_GR] + + (u64)channel_avg[ISC_HIS_CFG_MODE_GB]; avg >>= 1; dev_dbg(isc->dev, "isc wb: green components average %llu\n", avg); @@ -1372,8 +1400,8 @@ static void isc_wb_update(struct isc_ctrls *ctrls) * If it happens that the component is zero, use the * fixed point value : 1.0 gain. */ - if (hist_count[c]) - gw_gain[c] = div_u64(avg << 9, hist_count[c]); + if (channel_avg[c]) + gw_gain[c] = div_u64(avg << 9, channel_avg[c]); else gw_gain[c] = 1 << 9; diff --git a/drivers/media/platform/microchip/microchip-isc.h b/drivers/media/platform/microchip/microchip-isc.h index ccb566e5e627..1784c0089296 100644 --- a/drivers/media/platform/microchip/microchip-isc.h +++ b/drivers/media/platform/microchip/microchip-isc.h @@ -154,12 +154,12 @@ struct isc_ctrls { s32 offset[HIST_BAYER]; u32 hist_entry[HIST_ENTRIES]; - u32 hist_count[HIST_BAYER]; u8 hist_id; u8 hist_stat; #define HIST_MIN_INDEX 0 #define HIST_MAX_INDEX 1 u32 hist_minmax[HIST_BAYER][2]; + u32 channel_avg[HIST_BAYER]; }; #define ISC_PIPE_LINE_NODE_NUM 15 -- 2.34.1