[PATCH v5 3/5] media: hi846: Fix modes handling for different lane cases

From: Pengyu Luo

Date: Sun Jul 12 2026 - 07:53:55 EST


When using 4-lane, v4l2_find_nearest_size may return an unsupported
mode, 640x480 mode, use v4l2_find_nearest_size_conditional() to fix
it.

Reported-by: Sebastian Krzyszkowiak <sebastian.krzyszkowiak@xxxxxxx>
Closes: https://lore.kernel.org/linux-media/OmTXoHZJTSGePymL9I-1Cw@xxxxxxx
Fixes: e8c0882685f9 ("media: i2c: add driver for the SK Hynix Hi-846 8M pixel camera")
Signed-off-by: Pengyu Luo <mitltlatltl@xxxxxxxxx>
---
v5:
- Use v4l2_find_nearest_size_conditional() to filter for different lane cases. (Sakari)
---
drivers/media/i2c/hi846.c | 58 +++++++++++++++++++++++----------------
1 file changed, 34 insertions(+), 24 deletions(-)

diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c
index 6bcd862aee30..1223567641c4 100644
--- a/drivers/media/i2c/hi846.c
+++ b/drivers/media/i2c/hi846.c
@@ -1688,6 +1688,16 @@ static int __maybe_unused hi846_resume(struct device *dev)
return hi846_power_on(hi846);
}

+static bool filter_by_mipi_lanes(const void *array, size_t index,
+ const void *context)
+{
+ const struct hi846_mode *mode = array;
+ const struct hi846 *hi846 = context;
+
+ return (hi846->nr_lanes == 2 && mode->reg_list_2lane.num_of_regs) ||
+ (hi846->nr_lanes == 4 && mode->reg_list_4lane.num_of_regs);
+}
+
static int hi846_set_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *format)
@@ -1710,20 +1720,6 @@ static int hi846_set_format(struct v4l2_subdev *sd,
return 0;
}

- if (hi846->nr_lanes == 2) {
- if (!hi846->cur_mode->reg_list_2lane.num_of_regs) {
- dev_err(&client->dev,
- "this mode is not supported for 2 lanes\n");
- return -EINVAL;
- }
- } else {
- if (!hi846->cur_mode->reg_list_4lane.num_of_regs) {
- dev_err(&client->dev,
- "this mode is not supported for 4 lanes\n");
- return -EINVAL;
- }
- }
-
mutex_lock(&hi846->mutex);

if (hi846->streaming) {
@@ -1734,9 +1730,12 @@ static int hi846_set_format(struct v4l2_subdev *sd,
hi846->fmt = fmt;

hi846->cur_mode =
- v4l2_find_nearest_size(supported_modes,
- ARRAY_SIZE(supported_modes),
- width, height, mf->width, mf->height);
+ v4l2_find_nearest_size_conditional(supported_modes,
+ ARRAY_SIZE(supported_modes),
+ width, height,
+ mf->width, mf->height,
+ filter_by_mipi_lanes, hi846);
+
dev_dbg(&client->dev, "%s: found mode: %dx%d\n", __func__,
hi846->cur_mode->width, hi846->cur_mode->height);

@@ -1819,6 +1818,8 @@ static int hi846_enum_frame_size(struct v4l2_subdev *sd,
struct v4l2_subdev_frame_size_enum *fse)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
+ struct hi846 *hi846 = to_hi846(sd);
+ int i, count;

if (fse->pad || fse->index >= ARRAY_SIZE(supported_modes))
return -EINVAL;
@@ -1828,15 +1829,24 @@ static int hi846_enum_frame_size(struct v4l2_subdev *sd,
return -EINVAL;
}

- fse->min_width = supported_modes[fse->index].width;
- fse->max_width = supported_modes[fse->index].width;
- fse->min_height = supported_modes[fse->index].height;
- fse->max_height = supported_modes[fse->index].height;
+ for (count = i = 0; i < ARRAY_SIZE(supported_modes); i++) {
+ if (!filter_by_mipi_lanes(&supported_modes[i], i, hi846))
+ continue;
+
+ if (count == fse->index) {
+ fse->min_width = supported_modes[i].width;
+ fse->max_width = fse->min_width;
+ fse->min_height = supported_modes[i].height;
+ fse->max_height = fse->min_height;
+ dev_dbg(&client->dev, "%s: max width: %d max height: %d\n", __func__,
+ fse->max_width, fse->max_height);
+ return 0;
+ }

- dev_dbg(&client->dev, "%s: max width: %d max height: %d\n", __func__,
- fse->max_width, fse->max_height);
+ count++;
+ }

- return 0;
+ return -EINVAL;
}

static int hi846_get_selection(struct v4l2_subdev *sd,
--
2.54.0