From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Ricardo Ribalda <ribalda@chromium.org>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
Sakari Ailus <sakari.ailus@linux.intel.com>,
Hans Verkuil <hverkuil@kernel.org>,
Nas Chung <nas.chung@chipsnmedia.com>,
Jackson Lee <jackson.lee@chipsnmedia.com>,
Bingbu Cao <bingbu.cao@intel.com>,
Tianshu Qiu <tian.shu.qiu@intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Keke Li <keke.li@amlogic.com>, Yong Zhi <yong.zhi@intel.com>,
Jacopo Mondi <jacopo.mondi@ideasonboard.com>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-staging@lists.linux.dev,
Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Subject: Re: [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask
Date: Sat, 2 May 2026 19:54:55 +0300 [thread overview]
Message-ID: <20260502165455.GB488660@killaraus.ideasonboard.com> (raw)
In-Reply-To: <CANiDSCtf9ThohCR8XT7LNQUsbYeTdB=gGkmnXKc1cPjxUsVHdA@mail.gmail.com>
On Sat, May 02, 2026 at 09:56:19AM +0200, Ricardo Ribalda wrote:
> On Fri, 1 May 2026 at 22:19, Laurent Pinchart wrote:
> > On Fri, May 01, 2026 at 11:32:47AM +0000, Ricardo Ribalda wrote:
> > > The current code makes smatch a bit uncomfortable:
> > > drivers/media/i2c/mt9p031.c:799 mt9p031_s_ctrl() warn: assigning (-1952) to unsigned variable 'data'
> > >
> > > Probably because smatch is not clever enough (yet). Do a simple rewrite
> > > to make sure that smatch understands what we are doing here.
> > >
> > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > ---
> > > drivers/media/i2c/mt9p031.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
> > > index ea5d43d925ff..5c9dff030b4d 100644
> > > --- a/drivers/media/i2c/mt9p031.c
> > > +++ b/drivers/media/i2c/mt9p031.c
> > > @@ -795,7 +795,7 @@ static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
> > > ctrl->val &= ~1;
> > > data = (1 << 6) | (ctrl->val >> 1);
> > > } else {
> > > - ctrl->val &= ~7;
> > > + ctrl->val -= ctrl->val % 8;
> > > data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
> >
> > I'd still like to keep the ~7 (and, while at it, making the register
> > computation easier to read). I previously proposed
> >
> > ctrl->val &= ~7;
> > data = (ctrl->val - 64) >> 3;
> > data = (data << 8) | (1 << 6) | 32;
> >
> > which didn't quite appease smatch. We could use an explicit mask:
> >
> > ctrl->val &= ~7;
> > data = ((ctrl->val - 64) >> 3) & 0xff;
>
> Why 0xff and not 0x7f?
>
> If I understand it correctly the max is 1024 and (1024-64) >> 3 is < 127.
You're right. This won't matter in practice given that the control value
is bound by the minimum and maximum gains specified when creating the
control.
> Anyway... following the mask idea what about:
>
> ctrl->val &= ~7;
> data = (((ctrl->val - 64) & 0x3ff) << 5) | (3 << 5);
>
> ?
The digital gain is a 7-bit value stored in bits [14:8]. As the value
has to be divided by 8, the driver shifts left by 5. This is correct,
but I find it confusing for the reader (including myself, I had to pause
when reading this patch to understand the code). Hence the proposal to
improve readability.
Separating the (1 << 6) and 32 is also meant to make the code readable.
Bit 6 is the 1-bit second analog gain stage, and bits [5:0] store the
first stage analog gain.
> > data = (data << 8) | (1 << 6) | 32;
> >
> > > }
> > >
> > >
--
Regards,
Laurent Pinchart
next prev parent reply other threads:[~2026-05-02 16:55 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-01 11:32 [PATCH v2 0/6] media: Fix new smatch warnings Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 1/6] media: v4l2-dev: Add range check for vdev->minor Ricardo Ribalda
2026-05-04 8:12 ` Sakari Ailus
2026-05-01 11:32 ` [PATCH v2 2/6] media: i2c: mt9p031: Rewrite a bitwise mask Ricardo Ribalda
2026-05-01 20:19 ` Laurent Pinchart
2026-05-02 7:56 ` Ricardo Ribalda
2026-05-02 16:54 ` Laurent Pinchart [this message]
2026-05-01 11:32 ` [PATCH v2 3/6] media: i2c: adv7604: Add range checks for chip info Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 4/6] media: chips-media: wave5: Add range checks for dec_output_info Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 5/6] media: staging: ipu3-imgu: Add range check for imgu_css_cfg_acc_stripe Ricardo Ribalda
2026-05-04 8:22 ` Sakari Ailus
2026-05-04 8:25 ` Ricardo Ribalda
2026-05-01 11:32 ` [PATCH v2 6/6] media: amlogic-c3: Add validations for ae and awb config Ricardo Ribalda
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=20260502165455.GB488660@killaraus.ideasonboard.com \
--to=laurent.pinchart@ideasonboard.com \
--cc=bingbu.cao@intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=hverkuil@kernel.org \
--cc=jackson.lee@chipsnmedia.com \
--cc=jacopo.mondi@ideasonboard.com \
--cc=keke.li@amlogic.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=mchehab+samsung@kernel.org \
--cc=mchehab@kernel.org \
--cc=nas.chung@chipsnmedia.com \
--cc=ribalda@chromium.org \
--cc=sakari.ailus@linux.intel.com \
--cc=tian.shu.qiu@intel.com \
--cc=yong.zhi@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