mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] media: mira016 gain model updates
@ 2026-11-24 16:51 Jacopo Mondi
  2026-11-24 16:51 ` [PATCH 1/2] media: mira016: Update the gain model Jacopo Mondi
  2026-11-24 16:51 ` [PATCH 2/2] media: mira016: Update 8-bit gain init sequences Jacopo Mondi
  0 siblings, 2 replies; 4+ messages in thread
From: Jacopo Mondi @ 2026-11-24 16:51 UTC (permalink / raw)
  To: Philippe Baetens, Mauro Carvalho Chehab, Hans Verkuil, Sakari Ailus
  Cc: linux-media, linux-kernel, Jacopo Mondi

A few updates for the mira016 driver which just got merged in
media-committers/next at the beginning of the v7.5 cycle.

The bulk of the work is the reworking of the gain handling which was
previously modeled using lookup tables and is now calculated. Thanks
Philippe for the investigation and the effort. This makes the libcamera
integration easier as we can drop tables from the camera helpers there
as well.

I would really like this patch to be collected for 7.5 together with the
driver, so that we don't have one release with the previous index-based
analogue gain interface to maintain compatibility with.

Tested in 8 and 10 bits modes, by applying a manual gain and validating
the actual image brightness changes accordingly.

CI pipeline:
https://gitlab.freedesktop.org/linux-media/users/jmondi/-/pipelines/1802703

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
Philippe Baetens (2):
      media: mira016: Update the gain model
      media: mira016: Update 8-bit gain init sequences

 drivers/media/i2c/mira016.c | 147 ++++++++++++++++++++++++--------------------
 1 file changed, 81 insertions(+), 66 deletions(-)
---
base-commit: b7ac871daee981f1963f632586ff9b9d898ce5dd
change-id: 20261123-mira016-fixes-c2619f82c418

Best regards,
--  
Jacopo Mondi <jacopo.mondi@ideasonboard.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] media: mira016: Update the gain model
  2026-11-24 16:51 [PATCH 0/2] media: mira016 gain model updates Jacopo Mondi
@ 2026-11-24 16:51 ` Jacopo Mondi
  2026-12-06  5:54   ` Laurent Pinchart
  2026-11-24 16:51 ` [PATCH 2/2] media: mira016: Update 8-bit gain init sequences Jacopo Mondi
  1 sibling, 1 reply; 4+ messages in thread
From: Jacopo Mondi @ 2026-11-24 16:51 UTC (permalink / raw)
  To: Philippe Baetens, Mauro Carvalho Chehab, Hans Verkuil, Sakari Ailus
  Cc: linux-media, linux-kernel, Jacopo Mondi

From: Philippe Baetens <philippebaetens@gmail.com>

The analogue gain handling is currently modeled using tables that
contains the values of the three registers that control the gain
(ADC_GAIN, RG_MULT and GDIG_PREAMP).

The underlying gain model has been characterized as

    analogue_gain =  57 * 2^(2 - RG_MULT) / (ADC_GAIN + 21)

In 8-bits mode the output of the 10-bit ADC is scaled to 8-bit allowing
an additional 4x digital gain, for a total achievable 16x gain, compared
to the 4x available in 10-bits mode.

Implement the calculation of the [ADC_GAIN, RG_MULT, GDIG_PREAMP]
registers values in mira016_fine_gain() and remove the tables.

The expected gain code is the desired gain value multiplied by 1000 to
perform all calculations with integers arithmetic.

Signed-off-by: Philippe Baetens <philippebaetens@gmail.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 drivers/media/i2c/mira016.c | 143 ++++++++++++++++++++++++--------------------
 1 file changed, 79 insertions(+), 64 deletions(-)

diff --git a/drivers/media/i2c/mira016.c b/drivers/media/i2c/mira016.c
index 082442ffad28..cecef53d8af0 100644
--- a/drivers/media/i2c/mira016.c
+++ b/drivers/media/i2c/mira016.c
@@ -127,8 +127,15 @@
 /* Fine analogue gain */
 #define MIRA016_GDIG_PREAMP		CCI_REG8(0x0195)
 #define MIRA016_BIAS_RG_ADCGAIN		CCI_REG8(0x01f0)
+#define MIRA016_BIAS_RG_ADCGAIN_MAX	63
 #define MIRA016_BIAS_RG_MULT		CCI_REG8(0x01f3)
 
+#define MIRA016_GAIN_UNIT		1000
+#define MIRA016_GAIN_MAX_8B		(16 * MIRA016_GAIN_UNIT)
+#define MIRA016_GAIN_MAX_10B		(4 * MIRA016_GAIN_UNIT)
+#define MIRA016_ADC_GAIN_VAL(n)		(57000 << (n))
+#define MIRA016_FINE_GAIN_REF_MULT	2
+
 /* MIPI CSI-2 link frequency for 1.5Mbps per lane */
 #define MIRA016_LINK_FREQ_750M		750000000
 
@@ -956,55 +963,71 @@ static const struct cci_reg_sequence mira016_soft_reset_assert[] = {
 	{ CCI_REG8(0xe009), 0x0 },
 };
 
-struct mira016_gain_lut {
+struct mira016_fine_gain {
 	u8 gdig_preamp;
 	u8 rg_adcgain;
 	u8 rg_mult;
 };
 
-/* Table 29: Lookup table for fine analog gain in 10-bit mode */
-static const struct mira016_gain_lut mira016_gain_lut_10bit[] = {
-	{ 15, 36, 2 }, { 15, 35, 2 }, { 15, 33, 2 }, { 15, 32, 2 },
-	{ 15, 30, 2 }, { 15, 29, 2 }, { 15, 27, 2 }, { 15, 26, 2 },
-	{ 15, 24, 2 }, { 15, 23, 2 }, { 15, 22, 2 }, { 15, 62, 1 },
-	{ 15, 59, 1 }, { 15, 57, 1 }, { 15, 55, 1 }, { 15, 53, 1 },
-	{ 15, 51, 1 }, { 15, 48, 1 }, { 15, 46, 1 }, { 15, 45, 1 },
-	{ 15, 43, 1 }, { 15, 41, 1 }, { 15, 39, 1 }, { 15, 37, 1 },
-	{ 15, 36, 1 }, { 15, 34, 1 }, { 15, 32, 1 }, { 15, 31, 1 },
-	{ 15, 29, 1 }, { 15, 28, 1 }, { 15, 26, 1 }, { 15, 25, 1 },
-	{ 15, 24, 1 }, { 15, 22, 1 }, { 15, 63, 0 }, { 15, 61, 0 },
-	{ 15, 58, 0 }, { 15, 56, 0 }, { 15, 54, 0 }, { 15, 51, 0 },
-	{ 15, 49, 0 }, { 15, 47, 0 }, { 15, 45, 0 }, { 15, 42, 0 },
-	{ 15, 41, 0 }, { 15, 39, 0 }, { 15, 37, 0 }, { 15, 35, 0 },
-};
+/*
+ * Fine analogue gain of the Mira016, from the formula:
+ *
+ * analogue_gain = 57 * 2^(MIRA016_RG_REF - RG_MULT) / (ADC_GAIN + 21)
+ *
+ * with ADC_GAIN in the [0, 63] range and MIRA016_RG_REF fixed at 2 for Mira016.
+ *
+ * The analogue gain is realized by controlling the ADC ramp current which gives
+ * a gain multiplier up to 4x. In 8-bit mode the digital gain GDIG_PREAMP takes
+ * the 10-bit ADC result to 8 bit and gives an additional 4x.
+ */
+static void mira016_fine_gain(u32 code, u32 gain, struct mira016_fine_gain *fg)
+{
+	unsigned int bit_shift;
+	unsigned int n, k, j;
+	int adc;
 
-/* Table 26: Lookup table for fine analog gain in 8-bit mode */
-static const struct mira016_gain_lut mira016_gain_lut_8bit[] = {
-	{ 3, 36, 2 }, { 3, 35, 2 }, { 3, 33, 2 }, { 3, 32, 2 },
-	{ 3, 30, 2 }, { 3, 29, 2 }, { 3, 28, 2 }, { 3, 26, 2 },
-	{ 3, 27, 2 }, { 3, 25, 2 }, { 3, 23, 2 }, { 3, 22, 2 },
-	{ 3, 22, 2 }, { 3, 22, 2 }, { 3, 55, 1 }, { 3, 54, 1 },
-	{ 3, 52, 1 }, { 3, 51, 1 }, { 3, 49, 1 }, { 3, 47, 1 },
-	{ 3, 44, 1 }, { 3, 43, 1 }, { 3, 42, 1 }, { 3, 40, 1 },
-	{ 3, 39, 1 }, { 3, 37, 1 }, { 3, 35, 1 }, { 3, 32, 1 },
-	{ 3, 31, 1 }, { 3, 29, 1 }, { 3, 28, 1 }, { 3, 27, 1 },
-	{ 3, 26, 1 }, { 3, 24, 1 }, { 3, 23, 1 }, { 3, 22, 1 },
-	{ 3, 61, 0 }, { 3, 58, 0 }, { 3, 57, 0 }, { 3, 55, 0 },
-	{ 3, 53, 0 }, { 3, 49, 0 }, { 3, 47, 0 }, { 3, 45, 0 },
-	{ 3, 42, 0 }, { 3, 40, 0 }, { 7, 39, 1 }, { 7, 36, 1 },
-	{ 7, 35, 1 }, { 7, 33, 1 }, { 7, 32, 1 }, { 7, 30, 1 },
-	{ 7, 28, 1 }, { 7, 27, 1 }, { 7, 25, 1 }, { 7, 24, 1 },
-	{ 7, 22, 1 }, { 7, 60, 0 }, { 7, 58, 0 }, { 7, 55, 0 },
-	{ 7, 52, 0 }, { 7, 51, 0 }, { 7, 48, 0 }, { 7, 47, 0 },
-	{ 7, 45, 0 }, { 7, 43, 0 }, { 7, 41, 0 }, { 7, 40, 0 },
-	{ 15, 38, 1 }, { 15, 36, 1 }, { 15, 35, 1 }, { 15, 34, 1 },
-	{ 15, 32, 1 }, { 15, 31, 1 }, { 15, 30, 1 }, { 15, 29, 1 },
-	{ 15, 27, 1 }, { 15, 26, 1 }, { 15, 25, 1 }, { 15, 23, 1 },
-	{ 15, 21, 1 }, { 15, 61, 0 }, { 15, 59, 0 }, { 15, 57, 0 },
-	{ 15, 56, 0 }, { 15, 53, 0 }, { 15, 51, 0 }, { 15, 49, 0 },
-	{ 15, 47, 0 }, { 15, 45, 0 }, { 15, 44, 0 }, { 15, 42, 0 },
-	{ 15, 41, 0 }, { 15, 39, 0 }, { 15, 37, 0 },
-};
+	/*
+	 * In 8-bit mode the digital gain takes the 10-bit ADC result to 8 bit
+	 * and gives another 4x.
+	 */
+	switch (code) {
+	case MEDIA_BUS_FMT_Y8_1X8:
+		bit_shift = 2;
+		break;
+	case MEDIA_BUS_FMT_Y10_1X10:
+		bit_shift = 0;
+		break;
+	default:
+		/* 12 bit gain is fixed, we shouldn't be getting here. */
+		return;
+	}
+
+	gain = clamp_t(u32, gain, MIRA016_GAIN_UNIT,
+		       (4U * MIRA016_GAIN_UNIT) << bit_shift);
+
+	/*
+	 * Analogue octave: largest n with 57 * 2^n <= 84 * gain, or
+	 * floor(log2(84 * gain / 57)).
+	 *
+	 * 84 is the max adc_gain value of 63 + 21 from the formula.
+	 *
+	 * Limited to 2 for 10 bits, goes up to 4 for 8 bits. The remainder of
+	 * the gain that cannot be obtained through the ADC ramp current goes to
+	 * the digital part for the 8-bits mode.
+	 */
+	for (n = 0; n < 2 + bit_shift; n++) {
+		if (MIRA016_ADC_GAIN_VAL(n + 1) > 84U * gain)
+			break;
+	}
+
+	k = min(n, 2U);
+	j = n - k;
+	adc = (int)DIV_ROUND_CLOSEST(MIRA016_ADC_GAIN_VAL(n), gain) - 21;
+
+	fg->rg_adcgain = clamp(adc, 0, MIRA016_BIAS_RG_ADCGAIN_MAX);
+	fg->rg_mult = MIRA016_FINE_GAIN_REF_MULT - k;
+	fg->gdig_preamp = (16U << j) / BIT(bit_shift) - 1;
+}
 
 static const u32 mira016_mbus_formats[] = {
 	MEDIA_BUS_FMT_Y12_1X12,
@@ -1140,10 +1163,10 @@ static int mira016_set_pad_format(struct v4l2_subdev *sd,
 
 	switch (fmt->format.code) {
 	case MEDIA_BUS_FMT_Y8_1X8:
-		gain_max = ARRAY_SIZE(mira016_gain_lut_8bit);
+		gain_max = MIRA016_GAIN_MAX_8B;
 		break;
 	case MEDIA_BUS_FMT_Y10_1X10:
-		gain_max = ARRAY_SIZE(mira016_gain_lut_10bit);
+		gain_max = MIRA016_GAIN_MAX_10B;
 		break;
 	case MEDIA_BUS_FMT_Y12_1X12:
 	default:
@@ -1152,11 +1175,12 @@ static int mira016_set_pad_format(struct v4l2_subdev *sd,
 		 * changes the line timings while streaming. Only allow 1x
 		 * for the time being.
 		 */
-		gain_max = 1;
+		gain_max = MIRA016_GAIN_UNIT;
 		break;
 	}
 
-	ret = __v4l2_ctrl_modify_range(mira016->gain, 1, gain_max, 1, 1);
+	ret = __v4l2_ctrl_modify_range(mira016->gain, MIRA016_GAIN_UNIT,
+				       gain_max, 1, MIRA016_GAIN_UNIT);
 	if (ret)
 		return ret;
 
@@ -1692,23 +1716,17 @@ static int mira016_write_analogue_gain(struct mira016 *mira016,
 				       struct v4l2_subdev_state *state,
 				       u32 gain)
 {
-	const struct mira016_gain_lut *lut;
 	struct v4l2_mbus_framefmt *format;
+	struct mira016_fine_gain fg = {};
 	int ret = 0;
 
-	/*
-	 * Use 'gain - 1' as the gain control values are indexed from 1
-	 * while the gain luts are 0-indexed.
-	 */
 	format = v4l2_subdev_state_get_format(state, 0);
 	switch (format->code) {
 	case MEDIA_BUS_FMT_Y8_1X8:
-		lut = &mira016_gain_lut_8bit[gain - 1];
-		break;
+		fallthrough;
 	case MEDIA_BUS_FMT_Y10_1X10:
-		lut = &mira016_gain_lut_10bit[gain - 1];
+		mira016_fine_gain(format->code, gain, &fg);
 		break;
-	case MEDIA_BUS_FMT_Y12_1X12:
 	default:
 		/* 12 bit gain is fixed, nothing to do here. */
 		return 0;
@@ -1716,13 +1734,9 @@ static int mira016_write_analogue_gain(struct mira016 *mira016,
 
 	cci_write(mira016->regmap, MIRA016_BANK_SEL_REG, 0, &ret);
 
-	cci_write(mira016->regmap, MIRA016_BIAS_RG_ADCGAIN,
-		  lut->rg_adcgain, &ret);
-	cci_write(mira016->regmap, MIRA016_BIAS_RG_MULT,
-		  lut->rg_mult, &ret);
-	if (format->code == MEDIA_BUS_FMT_Y8_1X8)
-		cci_write(mira016->regmap, MIRA016_GDIG_PREAMP,
-			  lut->gdig_preamp, &ret);
+	cci_write(mira016->regmap, MIRA016_BIAS_RG_ADCGAIN, fg.rg_adcgain, &ret);
+	cci_write(mira016->regmap, MIRA016_BIAS_RG_MULT, fg.rg_mult, &ret);
+	cci_write(mira016->regmap, MIRA016_GDIG_PREAMP, fg.gdig_preamp, &ret);
 
 	return ret;
 }
@@ -1919,7 +1933,8 @@ static int mira016_init_controls(struct mira016 *mira016)
 
 	mira016->gain = v4l2_ctrl_new_std(ctrl_hdlr, &mira016_ctrl_ops,
 					  V4L2_CID_ANALOGUE_GAIN,
-					  1, 1, 1, 1);
+					  MIRA016_GAIN_UNIT, MIRA016_GAIN_UNIT,
+					  1, MIRA016_GAIN_UNIT);
 
 	/*
 	 * Changing VFLIP requires re-programming the top point, hence we

-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] media: mira016: Update 8-bit gain init sequences
  2026-11-24 16:51 [PATCH 0/2] media: mira016 gain model updates Jacopo Mondi
  2026-11-24 16:51 ` [PATCH 1/2] media: mira016: Update the gain model Jacopo Mondi
@ 2026-11-24 16:51 ` Jacopo Mondi
  1 sibling, 0 replies; 4+ messages in thread
From: Jacopo Mondi @ 2026-11-24 16:51 UTC (permalink / raw)
  To: Philippe Baetens, Mauro Carvalho Chehab, Hans Verkuil, Sakari Ailus
  Cc: linux-media, linux-kernel, Jacopo Mondi

From: Philippe Baetens <philippebaetens@gmail.com>

Slightly adjust the 8-bits mode gain init sequence.

Register 0xe024 controls the additional digital gain only available in
8-bits mode. Initialize it to the unit 1x value.

Also adjut the 0x005d register that controls the desired black level
pedestal to the 8-bits default value of 0x06.

Signed-off-by: Philippe Baetens <philippebaetens@gmail.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 drivers/media/i2c/mira016.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/i2c/mira016.c b/drivers/media/i2c/mira016.c
index cecef53d8af0..a3396b411746 100644
--- a/drivers/media/i2c/mira016.c
+++ b/drivers/media/i2c/mira016.c
@@ -323,10 +323,10 @@ static const struct cci_reg_sequence mira016_8b_fine_gain_init[] = {
 	{ CCI_REG8(0x01f0), 0x24 },
 	{ CCI_REG8(0x01f3), 0x2 },
 	{ CCI_REG8(0xe000), 0x1 },
-	{ CCI_REG8(0xe024), 0x3 },
+	{ CCI_REG8(0xe024), 0xf },
 	{ CCI_REG8(0xe000), 0x0 },
 	{ CCI_REG8(0x005c), 0x0 },
-	{ CCI_REG8(0x005d), 0x18 },
+	{ CCI_REG8(0x005d), 0x6 },
 };
 
 static const struct cci_reg_sequence mira016_10b_fine_gain_init[] = {

-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] media: mira016: Update the gain model
  2026-11-24 16:51 ` [PATCH 1/2] media: mira016: Update the gain model Jacopo Mondi
@ 2026-12-06  5:54   ` Laurent Pinchart
  0 siblings, 0 replies; 4+ messages in thread
From: Laurent Pinchart @ 2026-12-06  5:54 UTC (permalink / raw)
  To: Jacopo Mondi
  Cc: Philippe Baetens, Mauro Carvalho Chehab, Hans Verkuil,
	Sakari Ailus, linux-media, linux-kernel

Hi Jacopo, Philippe,

On Tue, Nov 24, 2026 at 05:51:11PM +0100, Jacopo Mondi wrote:
> From: Philippe Baetens <philippebaetens@gmail.com>
> 
> The analogue gain handling is currently modeled using tables that
> contains the values of the three registers that control the gain
> (ADC_GAIN, RG_MULT and GDIG_PREAMP).
> 
> The underlying gain model has been characterized as
> 
>     analogue_gain =  57 * 2^(2 - RG_MULT) / (ADC_GAIN + 21)
> 
> In 8-bits mode the output of the 10-bit ADC is scaled to 8-bit allowing
> an additional 4x digital gain, for a total achievable 16x gain, compared
> to the 4x available in 10-bits mode.

Why does scaling to 8 bits allow an additional digital gain ?

Given that the gain is exposed through the V4L2_CID_ANALOGUE_GAIN
control, I would have kept the digital part out, possibly exposed it
through V4L2_CID_DIGITAL_GAIN. Is there a reason why it has to be
combined with the analogue gain ?

> Implement the calculation of the [ADC_GAIN, RG_MULT, GDIG_PREAMP]
> registers values in mira016_fine_gain() and remove the tables.
> 
> The expected gain code is the desired gain value multiplied by 1000 to
> perform all calculations with integers arithmetic.
> 
> Signed-off-by: Philippe Baetens <philippebaetens@gmail.com>
> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> ---
>  drivers/media/i2c/mira016.c | 143 ++++++++++++++++++++++++--------------------
>  1 file changed, 79 insertions(+), 64 deletions(-)
> 
> diff --git a/drivers/media/i2c/mira016.c b/drivers/media/i2c/mira016.c
> index 082442ffad28..cecef53d8af0 100644
> --- a/drivers/media/i2c/mira016.c
> +++ b/drivers/media/i2c/mira016.c
> @@ -127,8 +127,15 @@
>  /* Fine analogue gain */
>  #define MIRA016_GDIG_PREAMP		CCI_REG8(0x0195)
>  #define MIRA016_BIAS_RG_ADCGAIN		CCI_REG8(0x01f0)
> +#define MIRA016_BIAS_RG_ADCGAIN_MAX	63
>  #define MIRA016_BIAS_RG_MULT		CCI_REG8(0x01f3)
>  
> +#define MIRA016_GAIN_UNIT		1000
> +#define MIRA016_GAIN_MAX_8B		(16 * MIRA016_GAIN_UNIT)
> +#define MIRA016_GAIN_MAX_10B		(4 * MIRA016_GAIN_UNIT)
> +#define MIRA016_ADC_GAIN_VAL(n)		(57000 << (n))
> +#define MIRA016_FINE_GAIN_REF_MULT	2
> +
>  /* MIPI CSI-2 link frequency for 1.5Mbps per lane */
>  #define MIRA016_LINK_FREQ_750M		750000000
>  
> @@ -956,55 +963,71 @@ static const struct cci_reg_sequence mira016_soft_reset_assert[] = {
>  	{ CCI_REG8(0xe009), 0x0 },
>  };
>  
> -struct mira016_gain_lut {
> +struct mira016_fine_gain {
>  	u8 gdig_preamp;
>  	u8 rg_adcgain;
>  	u8 rg_mult;
>  };
>  
> -/* Table 29: Lookup table for fine analog gain in 10-bit mode */
> -static const struct mira016_gain_lut mira016_gain_lut_10bit[] = {
> -	{ 15, 36, 2 }, { 15, 35, 2 }, { 15, 33, 2 }, { 15, 32, 2 },
> -	{ 15, 30, 2 }, { 15, 29, 2 }, { 15, 27, 2 }, { 15, 26, 2 },
> -	{ 15, 24, 2 }, { 15, 23, 2 }, { 15, 22, 2 }, { 15, 62, 1 },
> -	{ 15, 59, 1 }, { 15, 57, 1 }, { 15, 55, 1 }, { 15, 53, 1 },
> -	{ 15, 51, 1 }, { 15, 48, 1 }, { 15, 46, 1 }, { 15, 45, 1 },
> -	{ 15, 43, 1 }, { 15, 41, 1 }, { 15, 39, 1 }, { 15, 37, 1 },
> -	{ 15, 36, 1 }, { 15, 34, 1 }, { 15, 32, 1 }, { 15, 31, 1 },
> -	{ 15, 29, 1 }, { 15, 28, 1 }, { 15, 26, 1 }, { 15, 25, 1 },
> -	{ 15, 24, 1 }, { 15, 22, 1 }, { 15, 63, 0 }, { 15, 61, 0 },
> -	{ 15, 58, 0 }, { 15, 56, 0 }, { 15, 54, 0 }, { 15, 51, 0 },
> -	{ 15, 49, 0 }, { 15, 47, 0 }, { 15, 45, 0 }, { 15, 42, 0 },
> -	{ 15, 41, 0 }, { 15, 39, 0 }, { 15, 37, 0 }, { 15, 35, 0 },
> -};
> +/*
> + * Fine analogue gain of the Mira016, from the formula:
> + *
> + * analogue_gain = 57 * 2^(MIRA016_RG_REF - RG_MULT) / (ADC_GAIN + 21)
> + *
> + * with ADC_GAIN in the [0, 63] range and MIRA016_RG_REF fixed at 2 for Mira016.
> + *
> + * The analogue gain is realized by controlling the ADC ramp current which gives
> + * a gain multiplier up to 4x. In 8-bit mode the digital gain GDIG_PREAMP takes
> + * the 10-bit ADC result to 8 bit and gives an additional 4x.
> + */
> +static void mira016_fine_gain(u32 code, u32 gain, struct mira016_fine_gain *fg)
> +{
> +	unsigned int bit_shift;
> +	unsigned int n, k, j;
> +	int adc;
>  
> -/* Table 26: Lookup table for fine analog gain in 8-bit mode */
> -static const struct mira016_gain_lut mira016_gain_lut_8bit[] = {
> -	{ 3, 36, 2 }, { 3, 35, 2 }, { 3, 33, 2 }, { 3, 32, 2 },
> -	{ 3, 30, 2 }, { 3, 29, 2 }, { 3, 28, 2 }, { 3, 26, 2 },
> -	{ 3, 27, 2 }, { 3, 25, 2 }, { 3, 23, 2 }, { 3, 22, 2 },
> -	{ 3, 22, 2 }, { 3, 22, 2 }, { 3, 55, 1 }, { 3, 54, 1 },
> -	{ 3, 52, 1 }, { 3, 51, 1 }, { 3, 49, 1 }, { 3, 47, 1 },
> -	{ 3, 44, 1 }, { 3, 43, 1 }, { 3, 42, 1 }, { 3, 40, 1 },
> -	{ 3, 39, 1 }, { 3, 37, 1 }, { 3, 35, 1 }, { 3, 32, 1 },
> -	{ 3, 31, 1 }, { 3, 29, 1 }, { 3, 28, 1 }, { 3, 27, 1 },
> -	{ 3, 26, 1 }, { 3, 24, 1 }, { 3, 23, 1 }, { 3, 22, 1 },
> -	{ 3, 61, 0 }, { 3, 58, 0 }, { 3, 57, 0 }, { 3, 55, 0 },
> -	{ 3, 53, 0 }, { 3, 49, 0 }, { 3, 47, 0 }, { 3, 45, 0 },
> -	{ 3, 42, 0 }, { 3, 40, 0 }, { 7, 39, 1 }, { 7, 36, 1 },
> -	{ 7, 35, 1 }, { 7, 33, 1 }, { 7, 32, 1 }, { 7, 30, 1 },
> -	{ 7, 28, 1 }, { 7, 27, 1 }, { 7, 25, 1 }, { 7, 24, 1 },
> -	{ 7, 22, 1 }, { 7, 60, 0 }, { 7, 58, 0 }, { 7, 55, 0 },
> -	{ 7, 52, 0 }, { 7, 51, 0 }, { 7, 48, 0 }, { 7, 47, 0 },
> -	{ 7, 45, 0 }, { 7, 43, 0 }, { 7, 41, 0 }, { 7, 40, 0 },
> -	{ 15, 38, 1 }, { 15, 36, 1 }, { 15, 35, 1 }, { 15, 34, 1 },
> -	{ 15, 32, 1 }, { 15, 31, 1 }, { 15, 30, 1 }, { 15, 29, 1 },
> -	{ 15, 27, 1 }, { 15, 26, 1 }, { 15, 25, 1 }, { 15, 23, 1 },
> -	{ 15, 21, 1 }, { 15, 61, 0 }, { 15, 59, 0 }, { 15, 57, 0 },
> -	{ 15, 56, 0 }, { 15, 53, 0 }, { 15, 51, 0 }, { 15, 49, 0 },
> -	{ 15, 47, 0 }, { 15, 45, 0 }, { 15, 44, 0 }, { 15, 42, 0 },
> -	{ 15, 41, 0 }, { 15, 39, 0 }, { 15, 37, 0 },
> -};
> +	/*
> +	 * In 8-bit mode the digital gain takes the 10-bit ADC result to 8 bit
> +	 * and gives another 4x.
> +	 */
> +	switch (code) {
> +	case MEDIA_BUS_FMT_Y8_1X8:
> +		bit_shift = 2;
> +		break;
> +	case MEDIA_BUS_FMT_Y10_1X10:
> +		bit_shift = 0;
> +		break;
> +	default:
> +		/* 12 bit gain is fixed, we shouldn't be getting here. */
> +		return;
> +	}
> +
> +	gain = clamp_t(u32, gain, MIRA016_GAIN_UNIT,
> +		       (4U * MIRA016_GAIN_UNIT) << bit_shift);
> +
> +	/*
> +	 * Analogue octave: largest n with 57 * 2^n <= 84 * gain, or

This doesn't seem to match the code below.

> +	 * floor(log2(84 * gain / 57)).
> +	 *
> +	 * 84 is the max adc_gain value of 63 + 21 from the formula.
> +	 *
> +	 * Limited to 2 for 10 bits, goes up to 4 for 8 bits. The remainder of
> +	 * the gain that cannot be obtained through the ADC ramp current goes to
> +	 * the digital part for the 8-bits mode.
> +	 */
> +	for (n = 0; n < 2 + bit_shift; n++) {
> +		if (MIRA016_ADC_GAIN_VAL(n + 1) > 84U * gain)
> +			break;
> +	}
> +
> +	k = min(n, 2U);
> +	j = n - k;
> +	adc = (int)DIV_ROUND_CLOSEST(MIRA016_ADC_GAIN_VAL(n), gain) - 21;
> +
> +	fg->rg_adcgain = clamp(adc, 0, MIRA016_BIAS_RG_ADCGAIN_MAX);
> +	fg->rg_mult = MIRA016_FINE_GAIN_REF_MULT - k;
> +	fg->gdig_preamp = (16U << j) / BIT(bit_shift) - 1;
> +}
>  
>  static const u32 mira016_mbus_formats[] = {
>  	MEDIA_BUS_FMT_Y12_1X12,
> @@ -1140,10 +1163,10 @@ static int mira016_set_pad_format(struct v4l2_subdev *sd,
>  
>  	switch (fmt->format.code) {
>  	case MEDIA_BUS_FMT_Y8_1X8:
> -		gain_max = ARRAY_SIZE(mira016_gain_lut_8bit);
> +		gain_max = MIRA016_GAIN_MAX_8B;
>  		break;
>  	case MEDIA_BUS_FMT_Y10_1X10:
> -		gain_max = ARRAY_SIZE(mira016_gain_lut_10bit);
> +		gain_max = MIRA016_GAIN_MAX_10B;
>  		break;
>  	case MEDIA_BUS_FMT_Y12_1X12:
>  	default:
> @@ -1152,11 +1175,12 @@ static int mira016_set_pad_format(struct v4l2_subdev *sd,
>  		 * changes the line timings while streaming. Only allow 1x
>  		 * for the time being.
>  		 */
> -		gain_max = 1;
> +		gain_max = MIRA016_GAIN_UNIT;
>  		break;
>  	}
>  
> -	ret = __v4l2_ctrl_modify_range(mira016->gain, 1, gain_max, 1, 1);
> +	ret = __v4l2_ctrl_modify_range(mira016->gain, MIRA016_GAIN_UNIT,
> +				       gain_max, 1, MIRA016_GAIN_UNIT);
>  	if (ret)
>  		return ret;
>  
> @@ -1692,23 +1716,17 @@ static int mira016_write_analogue_gain(struct mira016 *mira016,
>  				       struct v4l2_subdev_state *state,
>  				       u32 gain)
>  {
> -	const struct mira016_gain_lut *lut;
>  	struct v4l2_mbus_framefmt *format;
> +	struct mira016_fine_gain fg = {};
>  	int ret = 0;
>  
> -	/*
> -	 * Use 'gain - 1' as the gain control values are indexed from 1
> -	 * while the gain luts are 0-indexed.
> -	 */
>  	format = v4l2_subdev_state_get_format(state, 0);
>  	switch (format->code) {
>  	case MEDIA_BUS_FMT_Y8_1X8:
> -		lut = &mira016_gain_lut_8bit[gain - 1];
> -		break;
> +		fallthrough;
>  	case MEDIA_BUS_FMT_Y10_1X10:
> -		lut = &mira016_gain_lut_10bit[gain - 1];
> +		mira016_fine_gain(format->code, gain, &fg);
>  		break;
> -	case MEDIA_BUS_FMT_Y12_1X12:
>  	default:
>  		/* 12 bit gain is fixed, nothing to do here. */
>  		return 0;
> @@ -1716,13 +1734,9 @@ static int mira016_write_analogue_gain(struct mira016 *mira016,
>  
>  	cci_write(mira016->regmap, MIRA016_BANK_SEL_REG, 0, &ret);
>  
> -	cci_write(mira016->regmap, MIRA016_BIAS_RG_ADCGAIN,
> -		  lut->rg_adcgain, &ret);
> -	cci_write(mira016->regmap, MIRA016_BIAS_RG_MULT,
> -		  lut->rg_mult, &ret);
> -	if (format->code == MEDIA_BUS_FMT_Y8_1X8)
> -		cci_write(mira016->regmap, MIRA016_GDIG_PREAMP,
> -			  lut->gdig_preamp, &ret);
> +	cci_write(mira016->regmap, MIRA016_BIAS_RG_ADCGAIN, fg.rg_adcgain, &ret);
> +	cci_write(mira016->regmap, MIRA016_BIAS_RG_MULT, fg.rg_mult, &ret);
> +	cci_write(mira016->regmap, MIRA016_GDIG_PREAMP, fg.gdig_preamp, &ret);
>  
>  	return ret;
>  }
> @@ -1919,7 +1933,8 @@ static int mira016_init_controls(struct mira016 *mira016)
>  
>  	mira016->gain = v4l2_ctrl_new_std(ctrl_hdlr, &mira016_ctrl_ops,
>  					  V4L2_CID_ANALOGUE_GAIN,
> -					  1, 1, 1, 1);
> +					  MIRA016_GAIN_UNIT, MIRA016_GAIN_UNIT,
> +					  1, MIRA016_GAIN_UNIT);
>  
>  	/*
>  	 * Changing VFLIP requires re-programming the top point, hence we

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-06  5:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 16:51 [PATCH 0/2] media: mira016 gain model updates Jacopo Mondi
2026-11-24 16:51 ` [PATCH 1/2] media: mira016: Update the gain model Jacopo Mondi
2026-12-06  5:54   ` Laurent Pinchart
2026-11-24 16:51 ` [PATCH 2/2] media: mira016: Update 8-bit gain init sequences Jacopo Mondi

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®