From 5e3c0bd3f864fec9f895bd42c172f56ac4cdeb63 Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Tue, 30 Dec 2025 18:03:06 +0100 Subject: [PATCH] media: mt9m114: Adjust IFP selections and source format when source format changes to/from RAW10 Changing the IFP source pad format to RAW10 means disabling the scaler, which means that the crop and compose rectangles must be reset to match the sink format size with no border. And when changing the source pad format back from RAW10 to another format which require demosaicing the crop and compose rectangles must be reset to the sink format size minus a 4 pixels border all around it. Also when changing the source pad format back from RAW10 to another format the colorspace, ycbcr_enc and quantization need to be updated too. Add a new mt9m114_ifp_update_sel_and_src_fmt() helper which resets all these taking the bordersize for the new source format into account and call this helper whenever the source pad format changes to/from RAW10. Reviewed-by: Laurent Pinchart Signed-off-by: Hans de Goede Signed-off-by: Sakari Ailus Signed-off-by: Hans Verkuil --- drivers/media/i2c/mt9m114.c | 53 +++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/drivers/media/i2c/mt9m114.c b/drivers/media/i2c/mt9m114.c index 0933a5236e3d3..6dd8e1cbe99e6 100644 --- a/drivers/media/i2c/mt9m114.c +++ b/drivers/media/i2c/mt9m114.c @@ -1840,6 +1840,41 @@ static int mt9m114_ifp_enum_frameintervals(struct v4l2_subdev *sd, return 0; } +/* + * Helper function to update IFP crop, compose rectangles and source format + * when the pixel border size changes, which requires resetting these. + */ +static void mt9m114_ifp_update_sel_and_src_fmt(struct v4l2_subdev_state *state) +{ + struct v4l2_mbus_framefmt *src_format, *sink_format; + struct v4l2_rect *crop; + unsigned int border; + + sink_format = v4l2_subdev_state_get_format(state, 0); + src_format = v4l2_subdev_state_get_format(state, 1); + crop = v4l2_subdev_state_get_crop(state, 0); + border = mt9m114_ifp_get_border(state); + + crop->left = border; + crop->top = border; + crop->width = sink_format->width - 2 * border; + crop->height = sink_format->height - 2 * border; + *v4l2_subdev_state_get_compose(state, 0) = *crop; + + src_format->width = crop->width; + src_format->height = crop->height; + + if (src_format->code == MEDIA_BUS_FMT_SGRBG10_1X10) { + src_format->colorspace = V4L2_COLORSPACE_RAW; + src_format->ycbcr_enc = V4L2_YCBCR_ENC_601; + src_format->quantization = V4L2_QUANTIZATION_FULL_RANGE; + } else { + src_format->colorspace = V4L2_COLORSPACE_SRGB; + src_format->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; + src_format->quantization = V4L2_QUANTIZATION_DEFAULT; + } +} + static int mt9m114_ifp_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state, struct v4l2_subdev_format *fmt) @@ -1863,11 +1898,19 @@ static int mt9m114_ifp_set_fmt(struct v4l2_subdev *sd, /* Only the media bus code can be changed on the source pad. */ info = mt9m114_format_info(sensor, 1, fmt->format.code); - format->code = info->code; - - /* If the output format is RAW10, bypass the scaler. */ - if (format->code == MEDIA_BUS_FMT_SGRBG10_1X10) - *format = *v4l2_subdev_state_get_format(state, 0); + /* + * If the output format changes from/to RAW10 then the crop + * rectangle needs to be adjusted to add / remove the 4 pixel + * border used for demosaicing. And these changes then need to + * be propagated to the compose rectangle and source format. + */ + if ((format->code == MEDIA_BUS_FMT_SGRBG10_1X10) != + (info->code == MEDIA_BUS_FMT_SGRBG10_1X10)) { + format->code = info->code; + mt9m114_ifp_update_sel_and_src_fmt(state); + } else { + format->code = info->code; + } } fmt->format = *format; -- 2.47.3