diff mbox series

media: mt9p031: Move open subdev op init code into init_cfg

Message ID 20220618222151.478111-1-marex@denx.de
State Accepted
Commit 69681cd04164812e4089f3f57e450e9b7c27c1c2
Headers show
Series media: mt9p031: Move open subdev op init code into init_cfg | expand

Commit Message

Marek Vasut June 18, 2022, 10:21 p.m. UTC
The .open subdev op is not always called soon enough to initialize
the default crop settings. Move all this initialization into .init_cfg
op instead.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: Stefan Riedmueller <s.riedmueller@phytec.de>
---
 drivers/media/i2c/mt9p031.c | 51 +++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 22 deletions(-)

Comments

Laurent Pinchart June 18, 2022, 11:12 p.m. UTC | #1
Hi Marek,

Thank you for the patch.

On Sun, Jun 19, 2022 at 12:21:51AM +0200, Marek Vasut wrote:
> The .open subdev op is not always called soon enough to initialize
> the default crop settings. Move all this initialization into .init_cfg
> op instead.
> 
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
> Cc: Stefan Riedmueller <s.riedmueller@phytec.de>
> ---
>  drivers/media/i2c/mt9p031.c | 51 +++++++++++++++++++++----------------
>  1 file changed, 29 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
> index e73e814c60207..e0f0d87d4cfe9 100644
> --- a/drivers/media/i2c/mt9p031.c
> +++ b/drivers/media/i2c/mt9p031.c
> @@ -505,6 +505,34 @@ static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
>  	return mt9p031_pll_enable(mt9p031);
>  }
>  
> +static int mt9p031_init_cfg(struct v4l2_subdev *subdev,
> +			    struct v4l2_subdev_state *sd_state)
> +{
> +	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
> +	struct v4l2_mbus_framefmt *format;
> +	struct v4l2_rect *crop;
> +
> +	crop = v4l2_subdev_get_try_crop(subdev, sd_state, 0);
> +	crop->left = MT9P031_COLUMN_START_DEF;
> +	crop->top = MT9P031_ROW_START_DEF;
> +	crop->width = MT9P031_WINDOW_WIDTH_DEF;
> +	crop->height = MT9P031_WINDOW_HEIGHT_DEF;
> +
> +	format = v4l2_subdev_get_try_format(subdev, sd_state, 0);
> +
> +	if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
> +		format->code = MEDIA_BUS_FMT_Y12_1X12;
> +	else
> +		format->code = MEDIA_BUS_FMT_SGRBG12_1X12;
> +
> +	format->width = MT9P031_WINDOW_WIDTH_DEF;
> +	format->height = MT9P031_WINDOW_HEIGHT_DEF;
> +	format->field = V4L2_FIELD_NONE;
> +	format->colorspace = V4L2_COLORSPACE_SRGB;
> +
> +	return 0;
> +}

This looks good, but could I ask you to take it a small step further to
also replace the active format and crop rectangle initialization in the
probe function with a call to mt9p031_init_cfg() ? The probe function
will call mt9p031_init_cfg() with NULL for the second argument, and this
function would then call __mt9p031_get_pad_format() and
__mt9p031_get_pad_crop() instead of v4l2_subdev_get_try_format() and
v4l2_subdev_get_try_crop(), with the which argument set to ACTIVE or TRY
based on whether the sd_state is NULL or not.

> +
>  static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
>  				  struct v4l2_subdev_state *sd_state,
>  				  struct v4l2_subdev_mbus_code_enum *code)
> @@ -988,28 +1016,6 @@ static int mt9p031_registered(struct v4l2_subdev *subdev)
>  
>  static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
>  {
> -	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
> -	struct v4l2_mbus_framefmt *format;
> -	struct v4l2_rect *crop;
> -
> -	crop = v4l2_subdev_get_try_crop(subdev, fh->state, 0);
> -	crop->left = MT9P031_COLUMN_START_DEF;
> -	crop->top = MT9P031_ROW_START_DEF;
> -	crop->width = MT9P031_WINDOW_WIDTH_DEF;
> -	crop->height = MT9P031_WINDOW_HEIGHT_DEF;
> -
> -	format = v4l2_subdev_get_try_format(subdev, fh->state, 0);
> -
> -	if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
> -		format->code = MEDIA_BUS_FMT_Y12_1X12;
> -	else
> -		format->code = MEDIA_BUS_FMT_SGRBG12_1X12;
> -
> -	format->width = MT9P031_WINDOW_WIDTH_DEF;
> -	format->height = MT9P031_WINDOW_HEIGHT_DEF;
> -	format->field = V4L2_FIELD_NONE;
> -	format->colorspace = V4L2_COLORSPACE_SRGB;
> -
>  	return mt9p031_set_power(subdev, 1);
>  }
>  
> @@ -1027,6 +1033,7 @@ static const struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
>  };
>  
>  static const struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
> +	.init_cfg = mt9p031_init_cfg,
>  	.enum_mbus_code = mt9p031_enum_mbus_code,
>  	.enum_frame_size = mt9p031_enum_frame_size,
>  	.get_fmt = mt9p031_get_format,
diff mbox series

Patch

diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
index e73e814c60207..e0f0d87d4cfe9 100644
--- a/drivers/media/i2c/mt9p031.c
+++ b/drivers/media/i2c/mt9p031.c
@@ -505,6 +505,34 @@  static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
 	return mt9p031_pll_enable(mt9p031);
 }
 
+static int mt9p031_init_cfg(struct v4l2_subdev *subdev,
+			    struct v4l2_subdev_state *sd_state)
+{
+	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
+	struct v4l2_mbus_framefmt *format;
+	struct v4l2_rect *crop;
+
+	crop = v4l2_subdev_get_try_crop(subdev, sd_state, 0);
+	crop->left = MT9P031_COLUMN_START_DEF;
+	crop->top = MT9P031_ROW_START_DEF;
+	crop->width = MT9P031_WINDOW_WIDTH_DEF;
+	crop->height = MT9P031_WINDOW_HEIGHT_DEF;
+
+	format = v4l2_subdev_get_try_format(subdev, sd_state, 0);
+
+	if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
+		format->code = MEDIA_BUS_FMT_Y12_1X12;
+	else
+		format->code = MEDIA_BUS_FMT_SGRBG12_1X12;
+
+	format->width = MT9P031_WINDOW_WIDTH_DEF;
+	format->height = MT9P031_WINDOW_HEIGHT_DEF;
+	format->field = V4L2_FIELD_NONE;
+	format->colorspace = V4L2_COLORSPACE_SRGB;
+
+	return 0;
+}
+
 static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
 				  struct v4l2_subdev_state *sd_state,
 				  struct v4l2_subdev_mbus_code_enum *code)
@@ -988,28 +1016,6 @@  static int mt9p031_registered(struct v4l2_subdev *subdev)
 
 static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
 {
-	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
-	struct v4l2_mbus_framefmt *format;
-	struct v4l2_rect *crop;
-
-	crop = v4l2_subdev_get_try_crop(subdev, fh->state, 0);
-	crop->left = MT9P031_COLUMN_START_DEF;
-	crop->top = MT9P031_ROW_START_DEF;
-	crop->width = MT9P031_WINDOW_WIDTH_DEF;
-	crop->height = MT9P031_WINDOW_HEIGHT_DEF;
-
-	format = v4l2_subdev_get_try_format(subdev, fh->state, 0);
-
-	if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
-		format->code = MEDIA_BUS_FMT_Y12_1X12;
-	else
-		format->code = MEDIA_BUS_FMT_SGRBG12_1X12;
-
-	format->width = MT9P031_WINDOW_WIDTH_DEF;
-	format->height = MT9P031_WINDOW_HEIGHT_DEF;
-	format->field = V4L2_FIELD_NONE;
-	format->colorspace = V4L2_COLORSPACE_SRGB;
-
 	return mt9p031_set_power(subdev, 1);
 }
 
@@ -1027,6 +1033,7 @@  static const struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
 };
 
 static const struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
+	.init_cfg = mt9p031_init_cfg,
 	.enum_mbus_code = mt9p031_enum_mbus_code,
 	.enum_frame_size = mt9p031_enum_frame_size,
 	.get_fmt = mt9p031_get_format,