All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Jacopo Mondi <jacopo@jmondi.org>
Cc: "Dave Stevenson" <dave.stevenson@raspberrypi.com>,
	"Krzysztof Hałasa" <khalasa@piap.pl>,
	"Mauro Carvalho Chehab" <mchehab@kernel.org>,
	"Sakari Ailus" <sakari.ailus@iki.fi>,
	linux-media@vger.kernel.org
Subject: Re: [PATCH 02/10] media: ar0521: Add V4L2_CID_ANALOG_GAIN
Date: Thu, 6 Oct 2022 18:05:52 +0300	[thread overview]
Message-ID: <Yz7u0LT2kwhyhc8x@pendragon.ideasonboard.com> (raw)
In-Reply-To: <20221006150015.7p7qlab4dbaldnsg@uno.localdomain>

Hello,

On Thu, Oct 06, 2022 at 05:00:15PM +0200, Jacopo Mondi wrote:
> On Thu, Oct 06, 2022 at 03:44:54PM +0100, Dave Stevenson wrote:
> > On Wed, 5 Oct 2022 at 20:07, Jacopo Mondi wrote:
> > >
> > > Add support for V4L2_CID_ANALOG_GAIN. The control programs the global
> > > gain register which applies to all color channels.
> > >
> > > As both the global digital and analog gains are controlled through a
> > > single register, in order not to overwrite the configured digital gain
> > > we need to read the current register value before modifying it.
> > >
> > > Implement a function to read register values and use it before applying
> > > the new analog gain.
> > >
> > > Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> > > ---
> > >  drivers/media/i2c/ar0521.c | 64 ++++++++++++++++++++++++++++++++++++++
> > >  1 file changed, 64 insertions(+)
> > >
> > > diff --git a/drivers/media/i2c/ar0521.c b/drivers/media/i2c/ar0521.c
> > > index 89f3c01f18ce..581f5e42994d 100644
> > > --- a/drivers/media/i2c/ar0521.c
> > > +++ b/drivers/media/i2c/ar0521.c
> > > @@ -5,6 +5,8 @@
> > >   * Written by Krzysztof Hałasa
> > >   */
> > >
> > > +#include <asm/unaligned.h>
> > > +
> > >  #include <linux/clk.h>
> > >  #include <linux/delay.h>
> > >  #include <linux/pm_runtime.h>
> > > @@ -35,6 +37,11 @@
> > >  #define AR0521_HEIGHT_BLANKING_MIN     38u /* must be even */
> > >  #define AR0521_TOTAL_WIDTH_MIN      2968u
> > >
> > > +#define AR0521_ANA_GAIN_MIN            0x00
> > > +#define AR0521_ANA_GAIN_MAX            0x3f
> > > +#define AR0521_ANA_GAIN_STEP           0x01
> > > +#define AR0521_ANA_GAIN_DEFAULT                0x00
> > > +
> > >  /* AR0521 registers */
> > >  #define AR0521_REG_VT_PIX_CLK_DIV              0x0300
> > >  #define AR0521_REG_FRAME_LENGTH_LINES          0x0340
> > > @@ -55,6 +62,7 @@
> > >  #define AR0521_REG_RED_GAIN                    0x305A
> > >  #define AR0521_REG_GREEN2_GAIN                 0x305C
> > >  #define AR0521_REG_GLOBAL_GAIN                 0x305E
> > > +#define AR0521_REG_GLOBAL_GAIN_ANA_MASK                0x3f
> > >
> > >  #define AR0521_REG_HISPI_TEST_MODE             0x3066
> > >  #define AR0521_REG_HISPI_TEST_MODE_LP11                  0x0004
> > > @@ -77,6 +85,7 @@ static const char * const ar0521_supply_names[] = {
> > >
> > >  struct ar0521_ctrls {
> > >         struct v4l2_ctrl_handler handler;
> > > +       struct v4l2_ctrl *ana_gain;
> > >         struct {
> > >                 struct v4l2_ctrl *gain;
> > >                 struct v4l2_ctrl *red_balance;
> > > @@ -167,6 +176,36 @@ static int ar0521_write_reg(struct ar0521_dev *sensor, u16 reg, u16 val)
> > >         return ar0521_write_regs(sensor, buf, 2);
> > >  }
> > >
> > > +static int ar0521_read_reg(struct ar0521_dev *sensor, u16 reg, u16 *val)
> > > +{
> > > +       struct i2c_client *client = sensor->i2c_client;
> > > +       unsigned char buf[2];
> > > +       struct i2c_msg msg;
> > > +       int ret;
> > > +
> > > +       msg.addr = client->addr;
> > > +       msg.flags = client->flags;
> > > +       msg.len = sizeof(u16);
> > > +       msg.buf = buf;
> > > +       put_unaligned_be16(reg, buf);
> > > +
> > > +       ret = i2c_transfer(client->adapter, &msg, 1);
> > > +       if (ret < 0)
> > > +               return ret;
> > > +
> > > +       msg.len = sizeof(u16);
> > > +       msg.flags = client->flags | I2C_M_RD;
> > > +       msg.buf = buf;
> > > +
> > > +       ret = i2c_transfer(client->adapter, &msg, 1);
> > > +       if (ret < 0)
> > > +               return ret;
> > > +
> > > +       *val = get_unaligned_be16(buf);
> > > +
> > > +       return 0;
> > > +}
> > > +
> > >  static int ar0521_set_geometry(struct ar0521_dev *sensor)
> > >  {
> > >         /* All dimensions are unsigned 12-bit integers */
> > > @@ -187,6 +226,21 @@ static int ar0521_set_geometry(struct ar0521_dev *sensor)
> > >         return ar0521_write_regs(sensor, regs, ARRAY_SIZE(regs));
> > >  }
> > >
> > > +static int ar0521_set_analog_gain(struct ar0521_dev *sensor)
> > > +{
> > > +       u16 global_gain;
> > > +       int ret;
> > > +
> > > +       ret = ar0521_read_reg(sensor, AR0521_REG_GLOBAL_GAIN, &global_gain);
> > > +       if (ret)
> > > +               return ret;
> > > +
> > > +       global_gain &= ~AR0521_REG_GLOBAL_GAIN_ANA_MASK;
> > > +       global_gain |= sensor->ctrls.ana_gain->val & AR0521_REG_GLOBAL_GAIN_ANA_MASK;
> > > +
> > > +       return ar0521_write_reg(sensor, AR0521_REG_GLOBAL_GAIN, global_gain);
> >
> > Does this work without nasty interactions?
> 
> It seems so :)
> 
> > The register reference I have says that the bits 6:0 of 0x3056,
> > 0x3058, 0x305a, 0x305c, and 0x305e are all aliased to register 0x3056.
> > That means that the writes from ar0521_set_gains for GAIN,
> > RED_BALANCE, and BLUE_BALANCE will stomp over your ANALOGUE_GAIN.
> 
> but you're right the interactions between the registers are not 100%
> clear to me yet.
> 
> The fact is that libcamera only manipulates ANALOGUE_GAIN while the
> other gains are not changed. I guess if one wants to manipulate the
> single gains individually this is possible, but when setting the
> global gain they will be overwritten ? This seems to be confirmed from
> my experiments where changing the BLUE/RED gains has no visible
> effects on the image as libcamera constantly adjusts the global gain

I'm tempted to drop support for the colour gains really, and turn the
V4L2_CID_GAIN into V4L2_CID_DIGITAL_GAIN. Digital colour gains can still
be useful on platforms that have no ISP, but I think we need an array of
gains in that case, not abusing V4L2_CID_RED_BALANCE and
V4L2_CID_BLUE_BALANCE. Any objection ?

> > I also don't see a call to __v4l2_ctrl_handler_setup from
> > ar0521_set_stream, so whilst there is an explicit call to
> > ar0521_set_gains, ANALOGUE_GAIN won't be set.
>
> See [PATCH 08/10] media: ar0521: Setup controls at s_stream time
> later in the series :)
> 
> > [1] https://github.com/torvalds/linux/blob/master/drivers/media/i2c/ar0521.c#L190
> >
> > > +}
> > > +
> > >  static int ar0521_set_gains(struct ar0521_dev *sensor)
> > >  {
> > >         int green = sensor->ctrls.gain->val;
> > > @@ -456,6 +510,9 @@ static int ar0521_s_ctrl(struct v4l2_ctrl *ctrl)
> > >         case V4L2_CID_VBLANK:
> > >                 ret = ar0521_set_geometry(sensor);
> > >                 break;
> > > +       case V4L2_CID_ANALOGUE_GAIN:
> > > +               ret = ar0521_set_analog_gain(sensor);
> > > +               break;
> > >         case V4L2_CID_GAIN:
> > >         case V4L2_CID_RED_BALANCE:
> > >         case V4L2_CID_BLUE_BALANCE:
> > > @@ -499,6 +556,13 @@ static int ar0521_init_controls(struct ar0521_dev *sensor)
> > >         /* We can use our own mutex for the ctrl lock */
> > >         hdl->lock = &sensor->lock;
> > >
> > > +       /* Analog gain */
> > > +       ctrls->ana_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
> > > +                                           AR0521_ANA_GAIN_MIN,
> > > +                                           AR0521_ANA_GAIN_MAX,
> > > +                                           AR0521_ANA_GAIN_STEP,
> > > +                                           AR0521_ANA_GAIN_DEFAULT);
> > > +
> > >         /* Manual gain */
> > >         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 511, 1, 0);
> > >         ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2022-10-06 15:06 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-05 19:06 [PATCH 00/10] media: ar0521: Add analog gain, rework clock tree Jacopo Mondi
2022-10-05 19:06 ` [PATCH 01/10] media: ar0521: Implement enum_frame_sizes Jacopo Mondi
2022-10-06 14:37   ` Dave Stevenson
2022-10-06 16:33   ` Laurent Pinchart
2022-10-07  4:57     ` Krzysztof Hałasa
2022-10-07  8:08       ` Laurent Pinchart
2022-10-07  7:29     ` Jacopo Mondi
2022-10-07  8:11       ` Laurent Pinchart
2022-10-07 10:32         ` Dave Stevenson
2022-10-07 12:05           ` Sakari Ailus
2022-10-07 12:12             ` Laurent Pinchart
2022-10-05 19:06 ` [PATCH 02/10] media: ar0521: Add V4L2_CID_ANALOG_GAIN Jacopo Mondi
2022-10-06 14:44   ` Dave Stevenson
2022-10-06 15:00     ` Jacopo Mondi
2022-10-06 15:05       ` Laurent Pinchart [this message]
2022-10-07  5:28         ` Krzysztof Hałasa
2022-10-07  8:20           ` Laurent Pinchart
2022-10-12 18:54             ` Sakari Ailus
2022-10-13  9:30               ` Laurent Pinchart
2022-10-07  5:20   ` Krzysztof Hałasa
2022-10-07  7:17     ` Jacopo Mondi
2022-10-07  8:30       ` Laurent Pinchart
2022-10-07 12:01         ` Krzysztof Hałasa
2022-10-07 12:07           ` Laurent Pinchart
2022-10-07 14:02             ` Krzysztof Hałasa
2022-10-17 15:10         ` Jacopo Mondi
2022-10-17 15:57           ` Sakari Ailus
2022-10-17 16:31             ` Jacopo Mondi
2022-10-17 16:37               ` Sakari Ailus
2022-10-17 16:42               ` Dave Stevenson
2022-10-07 11:56       ` Krzysztof Hałasa
2022-10-07 12:11         ` Laurent Pinchart
2022-10-07 14:00           ` Krzysztof Hałasa
2022-10-05 19:06 ` [PATCH 03/10] media: ar0521: Set maximum resolution to 2592x1944 Jacopo Mondi
2022-10-06 14:57   ` Dave Stevenson
2022-10-07 13:06     ` Laurent Pinchart
2022-10-20 11:23       ` Jacopo Mondi
2022-10-07  5:33   ` Krzysztof Hałasa
2022-10-07 12:42     ` Jacopo Mondi
2022-10-07 14:07       ` Krzysztof Hałasa
2022-10-05 19:06 ` [PATCH 04/10] media: ar0521: Rework PLL computation Jacopo Mondi
2022-10-07 13:56   ` Laurent Pinchart
2022-10-12 19:02     ` Sakari Ailus
2022-10-13  9:31       ` Laurent Pinchart
2022-10-05 19:06 ` [PATCH 05/10] media: ar0521: Add LINK_FREQ control Jacopo Mondi
2022-10-06 15:10   ` Dave Stevenson
2022-10-07 14:01     ` Laurent Pinchart
2022-10-07 14:26       ` Dave Stevenson
2022-10-16  1:53         ` Laurent Pinchart
2022-10-17 11:21           ` Dave Stevenson
2022-10-17  9:24         ` Jacopo Mondi
2022-10-17 11:00           ` Dave Stevenson
2022-10-17 12:00             ` Jacopo Mondi
2022-10-05 19:06 ` [PATCH 06/10] media: ar0521: Configure pixel rate using LINK_FREQ Jacopo Mondi
2022-10-06  5:51   ` kernel test robot
2022-10-06 15:42   ` Sakari Ailus
2022-10-07  7:25     ` Jacopo Mondi
2022-10-07  5:52   ` Krzysztof Hałasa
2022-10-05 19:06 ` [PATCH 07/10] media: ar0521: Adjust exposure and blankings limits Jacopo Mondi
2022-10-06  2:08   ` kernel test robot
2022-10-06  4:17   ` kernel test robot
2022-10-06 15:41   ` Dave Stevenson
2022-10-05 19:06 ` [PATCH 08/10] media: ar0521: Setup controls at s_stream time Jacopo Mondi
2022-10-06 15:43   ` Dave Stevenson
2022-10-07  7:23     ` Jacopo Mondi
2022-10-05 19:06 ` [PATCH 09/10] media: ar0521: Rework startup sequence Jacopo Mondi
2022-10-06 15:45   ` Dave Stevenson
2022-10-05 19:06 ` [PATCH 10/10] media: ar0521: Tab-align definitions Jacopo Mondi
2022-10-06 15:48   ` Dave Stevenson
2022-10-06 16:11   ` Laurent Pinchart
2022-10-07  5:42   ` Krzysztof Hałasa

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=Yz7u0LT2kwhyhc8x@pendragon.ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=dave.stevenson@raspberrypi.com \
    --cc=jacopo@jmondi.org \
    --cc=khalasa@piap.pl \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=sakari.ailus@iki.fi \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.