Linux kernel staging patches
 help / color / mirror / Atom feed
From: Maurizio Casciano <mauriziocasciano7@gmail.com>
To: linux-media@vger.kernel.org
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Bingbu Cao <bingbu.cao@amd.com>,
	Jacopo Mondi <jacopo.mondi@ideasonboard.com>,
	Nicholas Roth <nicholas@rothemail.net>,
	Andy Shevchenko <andy@kernel.org>,
	Hans de Goede <hansg@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jose Maria Martin <jmmartinf@hotmail.com>,
	linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org,
	Maurizio Casciano <mauriziocasciano7@gmail.com>
Subject: [PATCH 6/8] media: ov2740: add manual white balance controls
Date: Wed, 26 Aug 2026 15:22:54 +0200	[thread overview]
Message-ID: <20260826132256.3343451-7-mauriziocasciano7@gmail.com> (raw)
In-Reply-To: <20260826132256.3343451-1-mauriziocasciano7@gmail.com>

The sensor has separate red, green and blue manual white-balance gain
registers, but the driver currently writes the same digital-gain value
to all three channels. This prevents userspace from correcting the
strong color cast of raw Bayer capture.

Expose red- and blue-balance controls relative to the digital gain,
update all three channels under group hold, and always release and
launch the group even when a channel write fails.

Tested on the Yoga Book OV2740 with live gain changes and continuous raw
capture.

Signed-off-by: Maurizio Casciano <mauriziocasciano7@gmail.com>
Assisted-by: Codex:gpt-5.6-sol sparse
---
 drivers/media/i2c/ov2740.c | 71 +++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 21 deletions(-)

diff --git a/drivers/media/i2c/ov2740.c b/drivers/media/i2c/ov2740.c
index b760d4dc0e68..7651443b28c1 100644
--- a/drivers/media/i2c/ov2740.c
+++ b/drivers/media/i2c/ov2740.c
@@ -607,6 +607,9 @@ struct ov2740 {
 	struct v4l2_ctrl *vblank;
 	struct v4l2_ctrl *hblank;
 	struct v4l2_ctrl *exposure;
+	struct v4l2_ctrl *digital_gain;
+	struct v4l2_ctrl *red_balance;
+	struct v4l2_ctrl *blue_balance;
 
 	/* GPIOs, clocks, regulators */
 	struct gpio_desc *reset_gpio;
@@ -736,35 +739,46 @@ static int ov2740_identify_module(struct ov2740 *ov2740)
 	return 0;
 }
 
-static int ov2740_update_digital_gain(struct ov2740 *ov2740, u32 d_gain)
+static int ov2740_update_mwb_gains(struct ov2740 *ov2740)
 {
-	int ret;
+	u32 green_gain = ov2740->digital_gain->val;
+	u32 red_gain, blue_gain;
+	int end_ret, launch_ret, ret;
+
+	/* Balance controls use 1024 as unity relative to the digital gain. */
+	red_gain = min_t(u64,
+			 DIV_ROUND_CLOSEST_ULL((u64)green_gain *
+					       ov2740->red_balance->val,
+					       OV2740_DGTL_GAIN_DEFAULT),
+			 OV2740_DGTL_GAIN_MAX);
+	blue_gain = min_t(u64,
+			  DIV_ROUND_CLOSEST_ULL((u64)green_gain *
+						ov2740->blue_balance->val,
+						OV2740_DGTL_GAIN_DEFAULT),
+			  OV2740_DGTL_GAIN_MAX);
 
 	ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
 			       OV2740_GROUP_HOLD_START);
 	if (ret)
 		return ret;
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_R_GAIN, 2, d_gain);
+	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_R_GAIN, 2, red_gain);
 	if (ret)
-		return ret;
+		goto release_group;
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_G_GAIN, 2, d_gain);
+	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_G_GAIN, 2, green_gain);
 	if (ret)
-		return ret;
+		goto release_group;
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_B_GAIN, 2, d_gain);
-	if (ret)
-		return ret;
+	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_B_GAIN, 2, blue_gain);
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
-			       OV2740_GROUP_HOLD_END);
-	if (ret)
-		return ret;
+release_group:
+	end_ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
+				   OV2740_GROUP_HOLD_END);
+	launch_ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
+				      OV2740_GROUP_HOLD_LAUNCH);
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
-			       OV2740_GROUP_HOLD_LAUNCH);
-	return ret;
+	return ret ?: end_ret ?: launch_ret;
 }
 
 static int ov2740_test_pattern(struct ov2740 *ov2740, u32 pattern)
@@ -805,7 +819,9 @@ static int ov2740_set_ctrl(struct v4l2_ctrl *ctrl)
 		break;
 
 	case V4L2_CID_DIGITAL_GAIN:
-		ret = ov2740_update_digital_gain(ov2740, ctrl->val);
+	case V4L2_CID_RED_BALANCE:
+	case V4L2_CID_BLUE_BALANCE:
+		ret = ov2740_update_mwb_gains(ov2740);
 		break;
 
 	case V4L2_CID_EXPOSURE:
@@ -846,7 +862,7 @@ static int ov2740_init_controls(struct ov2740 *ov2740)
 	int ret;
 
 	ctrl_hdlr = &ov2740->ctrl_handler;
-	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
+	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
 	if (ret)
 		return ret;
 
@@ -881,9 +897,22 @@ static int ov2740_init_controls(struct ov2740 *ov2740)
 	v4l2_ctrl_new_std(ctrl_hdlr, &ov2740_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
 			  OV2740_ANAL_GAIN_MIN, OV2740_ANAL_GAIN_MAX,
 			  OV2740_ANAL_GAIN_STEP, OV2740_ANAL_GAIN_MIN);
-	v4l2_ctrl_new_std(ctrl_hdlr, &ov2740_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
-			  OV2740_DGTL_GAIN_MIN, OV2740_DGTL_GAIN_MAX,
-			  OV2740_DGTL_GAIN_STEP, OV2740_DGTL_GAIN_DEFAULT);
+	ov2740->digital_gain =
+		v4l2_ctrl_new_std(ctrl_hdlr, &ov2740_ctrl_ops,
+				  V4L2_CID_DIGITAL_GAIN,
+				  OV2740_DGTL_GAIN_MIN, OV2740_DGTL_GAIN_MAX,
+				  OV2740_DGTL_GAIN_STEP,
+				  OV2740_DGTL_GAIN_DEFAULT);
+	ov2740->red_balance =
+		v4l2_ctrl_new_std(ctrl_hdlr, &ov2740_ctrl_ops,
+				  V4L2_CID_RED_BALANCE,
+				  1, OV2740_DGTL_GAIN_MAX, 1,
+				  OV2740_DGTL_GAIN_DEFAULT);
+	ov2740->blue_balance =
+		v4l2_ctrl_new_std(ctrl_hdlr, &ov2740_ctrl_ops,
+				  V4L2_CID_BLUE_BALANCE,
+				  1, OV2740_DGTL_GAIN_MAX, 1,
+				  OV2740_DGTL_GAIN_DEFAULT);
 	exposure_max = ov2740->cur_mode->vts_def - OV2740_EXPOSURE_MAX_MARGIN;
 	ov2740->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov2740_ctrl_ops,
 					     V4L2_CID_EXPOSURE,
-- 
2.53.0


  parent reply	other threads:[~2026-08-26 13:23 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-26 13:22 [PATCH 0/8] media: Add Lenovo Yoga Book YB1-X91 camera support Maurizio Casciano
2026-08-26 13:22 ` [PATCH 1/8] media: ov8858: support 19.2 MHz clock and CHT gain setup Maurizio Casciano
2026-08-27  9:40   ` Andy Shevchenko
2026-08-27 11:59     ` Sakari Ailus
2026-08-27 18:19     ` Maurizio Casciano
2026-08-26 13:22 ` [PATCH 2/8] media: i2c: Add Yoga Book camera ACPI IDs Maurizio Casciano
2026-08-27  9:58   ` Andy Shevchenko
2026-08-27 12:14     ` Sakari Ailus
2026-08-27 12:46       ` Andy Shevchenko
2026-08-27 18:19     ` Maurizio Casciano
2026-08-27 23:18     ` Maurizio Casciano
2026-08-27 12:13   ` Sakari Ailus
2026-08-26 13:22 ` [PATCH 3/8] media: intel: ipu-bridge: Add Yoga Book camera sensors Maurizio Casciano
2026-08-26 13:22 ` [PATCH 4/8] media: atomisp: Add Yoga Book camera configuration Maurizio Casciano
2026-08-26 13:22 ` [PATCH 5/8] media: atomisp: support the Yoga Book OV2740 link Maurizio Casciano
2026-08-27 14:26   ` Andy Shevchenko
2026-08-27 23:18     ` Maurizio Casciano
2026-08-26 13:22 ` Maurizio Casciano [this message]
2026-08-27  3:13   ` [PATCH 6/8] media: ov2740: add manual white balance controls Cao, Bingbu
2026-08-27 18:19     ` Maurizio Casciano
2026-08-27 14:32   ` Andy Shevchenko
2026-08-26 13:22 ` [PATCH 7/8] media: atomisp: allow opt-in raw Bayer capture Maurizio Casciano
2026-08-27 14:43   ` Andy Shevchenko
2026-08-26 13:22 ` [PATCH 8/8] media: i2c: Add WV517S lens actuator driver Maurizio Casciano
2026-08-27 12:30   ` Sakari Ailus
2026-08-27 18:19     ` Maurizio Casciano
2026-08-27 18:17 ` [PATCH v2 00/11] media: Add Lenovo Yoga Book YB1-X91 camera support Maurizio Casciano
2026-08-27 18:17   ` [PATCH v2 01/11] media: ov8858: Extract digital gain programming Maurizio Casciano
2026-08-27 18:17   ` [PATCH v2 02/11] media: ov8858: support 19.2 MHz clock and CHT gain setup Maurizio Casciano
2026-08-27 19:46     ` Andy Shevchenko
2026-08-27 23:18       ` Maurizio Casciano
2026-08-28  7:28       ` Sakari Ailus
2026-08-28  7:48         ` Andy Shevchenko
2026-08-27 18:17   ` [PATCH v2 03/11] media: ov2740: Use C99 initializers for ACPI IDs Maurizio Casciano
2026-08-27 19:47     ` Andy Shevchenko
2026-08-27 18:17   ` [PATCH v2 04/11] media: ov2740: Add OVTI2740 ACPI ID Maurizio Casciano
2026-08-27 18:17   ` [PATCH v2 05/11] media: ov8858: Add INT3477 " Maurizio Casciano
2026-08-28 11:21     ` Sakari Ailus
2026-08-28 13:24       ` Andy Shevchenko
2026-08-28 13:31         ` Andy Shevchenko
2026-08-27 18:17   ` [PATCH v2 06/11] media: intel: ipu-bridge: Add Yoga Book camera sensors Maurizio Casciano
2026-08-27 18:17   ` [PATCH v2 07/11] media: atomisp: Add Yoga Book camera configuration Maurizio Casciano
2026-08-27 18:17   ` [PATCH v2 08/11] media: atomisp: support the Yoga Book OV2740 link Maurizio Casciano
2026-08-27 19:57     ` Andy Shevchenko
2026-08-28 11:37     ` Sakari Ailus
2026-08-28 11:42     ` Sakari Ailus
2026-08-27 18:17   ` [PATCH v2 09/11] media: ov2740: add manual white balance controls Maurizio Casciano
2026-08-27 20:03     ` Andy Shevchenko
2026-08-27 18:17   ` [PATCH v2 10/11] media: atomisp: allow raw Bayer capture Maurizio Casciano
2026-08-27 20:24     ` Andy Shevchenko
2026-08-27 18:17   ` [PATCH v2 11/11] media: i2c: Add WV517S lens actuator driver Maurizio Casciano
2026-08-27 20:31     ` Andy Shevchenko
2026-08-27 18:58   ` [PATCH v2 00/11] media: Add Lenovo Yoga Book YB1-X91 camera support Andy Shevchenko

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=20260826132256.3343451-7-mauriziocasciano7@gmail.com \
    --to=mauriziocasciano7@gmail.com \
    --cc=andy@kernel.org \
    --cc=bingbu.cao@amd.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hansg@kernel.org \
    --cc=jacopo.mondi@ideasonboard.com \
    --cc=jmmartinf@hotmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=mchehab@kernel.org \
    --cc=nicholas@rothemail.net \
    --cc=sakari.ailus@linux.intel.com \
    /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