Re: [PATCH 2/6] media: i2c: mt9p031: Rewrite a bitwise mask

Ricardo Ribalda
2026-04-28T13:37:50Z

Hi Laurent

On Tue, 28 Apr 2026 at 15:25, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> On Tue, Apr 28, 2026 at 12:41:08PM +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;
>
> This is less readable than it was :-/ Is there a way to avoid the
> warning while keeping the ~7 ? Maybe by assigning

I guess it is a matter of taste. I find my version more readable.
Also yours do not seem to convince smatch :P

drivers/media/i2c/mt9p031.c:800 mt9p031_s_ctrl() warn: '(data << 8) |
(1 << 6) | 32' 16775264 can't fit into 65535 'data'

ribalda@ribalda:~/work/linux$ git diff
diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
index 5c9dff030b4d..f9f51d5060f6 100644
--- a/drivers/media/i2c/mt9p031.c
+++ b/drivers/media/i2c/mt9p031.c
@@ -795,8 +795,9 @@ static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
ctrl->val &= ~1;
data = (1 << 6) | (ctrl->val >> 1);
} else {
- ctrl->val -= ctrl->val % 8;
- data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
+ ctrl->val &= ~7;
+ data = (ctrl->val - 64) >> 3;
+ data = (data << 8) | (1 << 6) | 32;

>
> data = (ctrl->val - 64) >> 3;
> data = (data << 8) | (1 << 6) | 32;
>
> > data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
> > }
> >
>
> --
> Regards,
>
> Laurent Pinchart

--
Ricardo Ribalda