Linux Media Controller development
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: Tarang Raval <tarang.raval@siliconsignals.io>
Cc: Kate Hsuan <hpa@redhat.com>,
	"linux-media@vger.kernel.org" <linux-media@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Hans Verkuil <hverkuil+cisco@kernel.org>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Hans de Goede <johannes.goede@oss.qualcomm.com>,
	Serin Yeh <serin.yeh@intel.com>
Subject: Re: [PATCH v3 2/3] media: i2c: imx471: Add Sony IMX471 image sensor driver
Date: Sat, 23 May 2026 01:38:17 +0300	[thread overview]
Message-ID: <ahDa2Vj56-S7R6JH@kekkonen.localdomain> (raw)
In-Reply-To: <PN3P287MB182919EBD2C36CBBC0A8D8598B0F2@PN3P287MB1829.INDP287.PROD.OUTLOOK.COM>

Hi Tarang, Kate,

On Fri, May 22, 2026 at 11:12:53AM +0000, Tarang Raval wrote:
> > +/* Exposure control */
> > +#define IMX471_REG_EXPOSURE                    CCI_REG16(0x0202)
> > +#define IMX471_EXPOSURE_MIN                    1
> > +#define IMX471_EXPOSURE_STEP                   1
> > +#define IMX471_EXPOSURE_DEFAULT                        0x04f6
> 
> Better to use a decimal value here.

I'd rather just initialise this to the maximum instead of a fixed value.


...

> > +static int imx471_set_ctrl(struct v4l2_ctrl *ctrl)
> > +{
> > +       struct imx471 *sensor = container_of(ctrl->handler,
> > +                                                 struct imx471,
> > +                                                 ctrl_handler);
> 
> Use container_of_const.
> 
> > +       struct v4l2_subdev_state *state =
> > +                       v4l2_subdev_get_locked_active_state(&sensor->sd);
> > +       const struct v4l2_mbus_framefmt *format =
> > +                       v4l2_subdev_state_get_format(state, 0);
> > +       s64 exposure_max;
> > +       int ret;
> 
> ret = 0;

Or assign the return value to ret below. Either works.

> 
> > +
> > +       /* Propagate change of current control to all related controls */
> > +       if (ctrl->id == V4L2_CID_VBLANK) {
> > +               /* Update max exposure while meeting expected vblanking */
> > +               exposure_max =
> > +                       format->height + ctrl->val - IMX471_EXPOSURE_MARGIN;
> > +               __v4l2_ctrl_modify_range(sensor->exposure,
> > +                                        sensor->exposure->minimum,
> > +                                        exposure_max,
> > +                                        sensor->exposure->step,
> > +                                        exposure_max);
> 
> This control operation can fail. Please check the return value.
> 
> > +       }
> > +
> > +       /* V4L2 controls values will be applied only when power is already up */
> > +       if (!pm_runtime_get_if_in_use(sensor->dev))
> > +               return 0;
> > +
> > +       switch (ctrl->id) {
> > +       case V4L2_CID_ANALOGUE_GAIN:
> > +               cci_write(sensor->regmap, IMX471_REG_ANALOG_GAIN,
> > +                         ctrl->val, &ret);
> 
> You are using ret for the first time here, Please initialize ret with 0 when
> declaring it.
> 
> cci_write() uses the value pointed by &ret to determine whether a previous
> error has already occurred, and an uninitialized ret may contain a garbage
> value, causing the write operation to fail unexpectedly.
> 
> > +               break;
> > +       case V4L2_CID_DIGITAL_GAIN:
> > +               cci_write(sensor->regmap, IMX471_REG_DIG_GAIN_GLOBAL,
> > +                         ctrl->val, &ret);
> > +               break;
> > +       case V4L2_CID_EXPOSURE:
> > +               cci_write(sensor->regmap, IMX471_REG_EXPOSURE,
> > +                         ctrl->val, &ret);
> > +               break;
> > +       case V4L2_CID_VBLANK:
> > +               /* Update FLL that meets expected vertical blanking */
> > +               cci_write(sensor->regmap, IMX471_REG_FLL,
> > +                         format->height + ctrl->val, &ret);
> > +               break;
> > +       case V4L2_CID_TEST_PATTERN:
> > +               cci_write(sensor->regmap, IMX471_REG_TEST_PATTERN,
> > +                         ctrl->val, &ret);
> > +               break;
> > +       case V4L2_CID_HFLIP:
> > +       case V4L2_CID_VFLIP:
> > +               cci_write(sensor->regmap, IMX471_REG_ORIENTATION,
> > +                         sensor->hflip->val | sensor->vflip->val << 1, &ret);
> > +               break;
> > +       default:
> > +               ret = -EINVAL;
> > +               dev_info(sensor->dev, "ctrl(id:0x%x,val:0x%x) is not handled",
> > +                        ctrl->id, ctrl->val);
> > +               break;
> > +       }
> > +
> > +       pm_runtime_put(sensor->dev);
> > +
> > +       return ret;
> > +}

...

> > +static int imx471_set_pad_format(struct v4l2_subdev *sd,
> > +                                struct v4l2_subdev_state *sd_state,
> > +                                struct v4l2_subdev_format *fmt)
> > +{
> > +       struct imx471 *sensor = to_imx471(sd);
> > +       const struct imx471_mode *mode;
> > +       int h_blank;
> > +       u64 pixel_rate;
> > +
> > +       mode = v4l2_find_nearest_size(imx471_modes,
> > +                                     ARRAY_SIZE(imx471_modes),
> > +                                     width, height,
> > +                                     fmt->format.width, fmt->format.height);
> > +
> > +       imx471_update_pad_format(sensor, mode, fmt);
> > +
> > +       *v4l2_subdev_state_get_format(sd_state, fmt->pad) = fmt->format;
> > +
> > +       if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
> > +               return 0;
> > +
> > +       if (media_entity_is_streaming(&sensor->sd.entity))
> > +               return -EBUSY;
> > +
> > +       pixel_rate = IMX471_LINK_FREQ_DEFAULT * 2 * 4;
> > +       div_u64(pixel_rate, 10);
> 
> You need to store the return value, as the above operation does not update
> pixel_rate.
> 
> Please use:
> pixel_rate = div_u64(IMX471_LINK_FREQ_DEFAULT * 2 * 4, 10);
> 
> > +       __v4l2_ctrl_modify_range(sensor->pixel_rate,
> > +                                V4L2_CID_PIXEL_RATE,
> > +                                pixel_rate, 1, pixel_rate);
> > +
> > +       __v4l2_ctrl_modify_range(sensor->vblank,
> > +                                mode->fll_min - mode->height,
> > +                                IMX471_FLL_MAX - mode->height,
> > +                                1,
> > +                                mode->fll_def - mode->height);
> > +
> > +       h_blank = mode->llp - mode->width;
> > +       /*
> > +        * Currently hblank is not changeable.
> > +        * So FPS control is done only by vblank.
> > +        */
> > +       __v4l2_ctrl_modify_range(sensor->hblank, h_blank,
> > +                                h_blank, 1, h_blank);
> 
> All the above control operations can fail. Please add proper error checks for them.
> 
> > +
> > +       return 0;
> > +}
> > +
> > +static int imx471_get_selection(struct v4l2_subdev *sd,
> > +                               struct v4l2_subdev_state *sd_state,
> > +                               struct v4l2_subdev_selection *sel)
> > +{
> > +       switch (sel->target) {
> > +       case V4L2_SEL_TGT_CROP:
> > +               sel->r = *v4l2_subdev_state_get_crop(sd_state, sel->pad);
> > +               break;
> > +
> > +       case V4L2_SEL_TGT_NATIVE_SIZE:
> > +               sel->r.top = 0;
> > +               sel->r.left = 0;
> > +               sel->r.width = IMX471_NATIVE_WIDTH;
> > +               sel->r.height = IMX471_NATIVE_HEIGHT;
> > +               return 0;
> > +
> > +       case V4L2_SEL_TGT_CROP_DEFAULT:
> > +       case V4L2_SEL_TGT_CROP_BOUNDS:
> > +               sel->r.top = IMX471_PIXEL_ARRAY_TOP;
> > +               sel->r.left = IMX471_PIXEL_ARRAY_LEFT;
> > +               sel->r.width = IMX471_PIXEL_ARRAY_WIDTH;
> > +               sel->r.height = IMX471_PIXEL_ARRAY_HEIGHT;
> > +               return 0;
> > +       }
> > +
> > +       return -EINVAL;
> > +}
> > +
> > +static int imx471_init_state(struct v4l2_subdev *sd,
> > +                            struct v4l2_subdev_state *sd_state)
> > +{
> > +       struct v4l2_subdev_format fmt = {
> > +               .which = V4L2_SUBDEV_FORMAT_ACTIVE,

You shouldn't be setting the active format here.

> > +               .format = {
> > +                       .code = MEDIA_BUS_FMT_SRGGB10_1X10,
> > +                       .width = imx471_modes[0].width,
> > +                       .height = imx471_modes[0].height,
> > +               },
> > +       };
> > +
> > +       imx471_set_pad_format(sd, sd_state, &fmt);
> > +
> > +       return 0;

...

> > +static int imx471_check_hwcfg(struct imx471 *sensor)
> > +{
> > +       struct v4l2_fwnode_endpoint bus_cfg = {
> > +               .bus_type = V4L2_MBUS_CSI2_DPHY,
> > +       };
> > +       struct fwnode_handle *ep, *fwnode = dev_fwnode(sensor->dev);
> > +       struct clk *clk;
> > +       unsigned long link_freq_bitmap;
> > +       int ret;
> 
> If you want, you can sort the variable declarations throughout the code, where
> appropriate, by length to make them more readable.
> 
> > +
> > +       clk = devm_v4l2_sensor_clk_get(sensor->dev, NULL);
> > +       if (IS_ERR(clk))
> > +               return dev_err_probe(sensor->dev, PTR_ERR(clk),
> > +                                    "can't get clock frequency\n");
> > +
> > +       if (clk_get_rate(clk) != IMX471_EXT_CLK)
> > +               return dev_err_probe(sensor->dev, -EINVAL,
> > +                                    "external clock %lu is not supported\n",
> > +                                    clk_get_rate(clk));
> > +
> > +       ep = fwnode_graph_get_endpoint_by_id(fwnode, 0, 0, 0);
> > +       ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
> > +       fwnode_handle_put(ep);
> > +       if (ret)
> > +               return dev_err_probe(sensor->dev, ret,
> > +                                    "parsing endpoint failed");
> > +
> > +       ret = v4l2_link_freq_to_bitmap(sensor->dev, bus_cfg.link_frequencies,
> > +                                      bus_cfg.nr_of_link_frequencies,
> > +                                      link_freq_menu_items,
> > +                                      ARRAY_SIZE(link_freq_menu_items),
> > +                                      &link_freq_bitmap);
> 
> This can fail silently. Please add an error message before returning the failure.

v4l2_link_freq_to_bitmap() does print errors already, don't do it here.

> 
> > +
> > +       v4l2_fwnode_endpoint_free(&bus_cfg);
> > +
> > +       return ret;
> > +}

-- 
Regards,

Sakari Ailus

  reply	other threads:[~2026-05-22 22:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22  3:11 [PATCH v3 0/3] Add Sony IMX471 camera sensor driver Kate Hsuan
2026-05-22  3:11 ` [PATCH v3 1/3] media: ipu-bridge: Add DMI information of Lenovo X9 to the image upside-down list Kate Hsuan
2026-05-29 22:33   ` Damjan Georgievski
2026-06-02  9:35     ` Kate Hsuan
2026-06-03  5:03       ` Mark Pearson
2026-06-04  3:19         ` Kate Hsuan
2026-05-22  3:11 ` [PATCH v3 2/3] media: i2c: imx471: Add Sony IMX471 image sensor driver Kate Hsuan
2026-05-22 11:12   ` Tarang Raval
2026-05-22 22:38     ` Sakari Ailus [this message]
2026-05-27  6:24       ` Kate Hsuan
2026-05-27  6:01     ` Kate Hsuan
2026-05-22 22:48   ` Sakari Ailus
2026-05-27  6:14     ` Kate Hsuan
2026-05-27 11:42       ` Sakari Ailus
2026-06-05  8:43         ` Kate Hsuan
2026-06-05  9:32           ` Tarang Raval
2026-05-22  3:11 ` [PATCH v3 3/3] media: i2c: imx471: Naming the register Kate Hsuan
2026-05-22 22:49   ` Sakari Ailus

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=ahDa2Vj56-S7R6JH@kekkonen.localdomain \
    --to=sakari.ailus@linux.intel.com \
    --cc=hpa@redhat.com \
    --cc=hverkuil+cisco@kernel.org \
    --cc=johannes.goede@oss.qualcomm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=serin.yeh@intel.com \
    --cc=tarang.raval@siliconsignals.io \
    /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