[PATCH v1 08/13] media: tegra-video: vi: Enumerate supported formats instead of building bitmap of compatible formats

From: Svyatoslav Ryhel

Date: Wed Nov 25 2026 - 06:09:15 EST


Enumerate pixel formats directly from SoC video formats (filtering by
f->mbus_code when provided) in tegra_channel_enum_format(), and remove the
subdevice/TPG format bitmaps, MAX_FORMAT_NUM, and
tegra_channel_set_subdev_active_fmt() so channel format enumeration no
longer depends on subdevice media bus enumeration during graph completion.

Signed-off-by: Svyatoslav Ryhel <clamor95@xxxxxxxxx>
---
.../media/platform/nvidia/tegra-video/vi.c | 161 ++----------------
.../media/platform/nvidia/tegra-video/vi.h | 6 -
2 files changed, 16 insertions(+), 151 deletions(-)

diff --git a/drivers/media/platform/nvidia/tegra-video/vi.c b/drivers/media/platform/nvidia/tegra-video/vi.c
index ed3e1e91baf12..4a0742b514a2b 100644
--- a/drivers/media/platform/nvidia/tegra-video/vi.c
+++ b/drivers/media/platform/nvidia/tegra-video/vi.c
@@ -64,29 +64,6 @@ to_tegra_vi_graph_entity(struct v4l2_async_connection *asd)
return container_of(asd, struct tegra_vi_graph_entity, asd);
}

-static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
- unsigned int code,
- unsigned int offset)
-{
- unsigned int i;
-
- for (i = offset; i < vi->soc->nformats; ++i) {
- if (vi->soc->video_formats[i].code == code)
- return i;
- }
-
- return -1;
-}
-
-static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
- unsigned int index)
-{
- if (WARN_ON_ONCE(index >= vi->soc->nformats))
- return vi->soc->video_formats[0].fourcc;
-
- return vi->soc->video_formats[index].fourcc;
-}
-
static const struct tegra_video_format *
tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
{
@@ -327,19 +304,26 @@ static int tegra_channel_enum_format(struct file *file, void *fh,
struct v4l2_fmtdesc *f)
{
struct tegra_vi_channel *chan = video_drvdata(file);
- unsigned int index;
- unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
-
- if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
- fmts_bitmap = chan->fmts_bitmap;
+ const struct tegra_video_format *vfmt = NULL;
+ struct tegra_vi *vi = chan->vi;
+ unsigned int index = 0;

- index = find_nth_bit(fmts_bitmap, MAX_FORMAT_NUM, f->index);
- if (index >= MAX_FORMAT_NUM)
+ if (f->index >= vi->soc->nformats)
return -EINVAL;

- f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index);
+ for (unsigned int i = 0; i < vi->soc->nformats; i++) {
+ vfmt = &vi->soc->video_formats[i];

- return 0;
+ if (f->mbus_code && vfmt->code != f->mbus_code)
+ continue;
+
+ if (index++ == f->index) {
+ f->pixelformat = vfmt->fourcc;
+ return 0;
+ }
+ }
+
+ return -EINVAL;
}

static int tegra_channel_get_format(struct file *file, void *fh,
@@ -489,39 +473,6 @@ static int tegra_channel_set_format(struct file *file, void *fh,
return 0;
}

-static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
-{
- int ret, index;
- struct v4l2_subdev *subdev;
- struct v4l2_subdev_format fmt = {
- .which = V4L2_SUBDEV_FORMAT_ACTIVE,
- };
-
- /*
- * Initialize channel format to the sub-device active format if there
- * is corresponding match in the Tegra supported video formats.
- */
- subdev = tegra_channel_get_remote_source_subdev(chan);
- ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
- if (ret)
- return ret;
-
- index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
- if (index < 0)
- return -EINVAL;
-
- chan->fmtinfo = &chan->vi->soc->video_formats[index];
- v4l2_fill_pix_format(&chan->format, &fmt.format);
- chan->format.pixelformat = chan->fmtinfo->fourcc;
- chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
- chan->format.sizeimage = chan->format.bytesperline *
- chan->format.height;
- chan->vi->ops->vi_fmt_align(&chan->format, NULL, chan->fmtinfo->bpp);
- tegra_channel_update_gangports(chan);
-
- return 0;
-}
-
static int
tegra_channel_subscribe_event(struct v4l2_fh *fh,
const struct v4l2_event_subscription *sub)
@@ -713,78 +664,6 @@ static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
return 0;
}

-/* VI only support 2 formats in TPG mode */
-static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
-{
- int index;
-
- bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
-
- index = tegra_get_format_idx_by_code(chan->vi,
- MEDIA_BUS_FMT_SRGGB10_1X10, 0);
- bitmap_set(chan->tpg_fmts_bitmap, index, 1);
-
- index = tegra_get_format_idx_by_code(chan->vi,
- MEDIA_BUS_FMT_RGB888_1X32_PADHI,
- 0);
- bitmap_set(chan->tpg_fmts_bitmap, index, 1);
-}
-
-static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
-{
- int index, ret, match_code = 0;
- struct v4l2_subdev *subdev;
- struct v4l2_subdev_mbus_code_enum code = {
- .which = V4L2_SUBDEV_FORMAT_ACTIVE,
- };
-
- bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
-
- /*
- * Set the bitmap bits based on all the matched formats between the
- * available media bus formats of sub-device and the pre-defined Tegra
- * supported video formats.
- */
- subdev = tegra_channel_get_remote_source_subdev(chan);
- while (1) {
- ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
- NULL, &code);
- if (ret < 0)
- break;
-
- index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
- while (index >= 0) {
- bitmap_set(chan->fmts_bitmap, index, 1);
- if (!match_code)
- match_code = code.code;
- /* look for other formats with same mbus code */
- index = tegra_get_format_idx_by_code(chan->vi,
- code.code,
- index + 1);
- }
-
- code.index++;
- }
-
- /*
- * Set the bitmap bit corresponding to default tegra video format if
- * there are no matched formats.
- */
- if (!match_code) {
- match_code = chan->vi->soc->default_video_format->code;
- index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
- if (WARN_ON(index < 0))
- return -EINVAL;
-
- bitmap_set(chan->fmts_bitmap, index, 1);
- }
-
- /* initialize channel format to the sub-device active format */
- tegra_channel_set_subdev_active_fmt(chan);
-
- return 0;
-}
-
static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
{
v4l2_ctrl_handler_free(&chan->ctrl_handler);
@@ -1115,7 +994,6 @@ int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
goto cleanup;

v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
- vi_tpg_fmts_bitmap_init(vi_chan);
csi_chan = list_next_entry(csi_chan, list);
}

@@ -1319,13 +1197,6 @@ static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
goto unregister_video;
}

- ret = vi_fmts_bitmap_init(chan);
- if (ret < 0) {
- dev_err(vi->dev,
- "failed to initialize formats bitmap: %d\n", ret);
- goto unregister_video;
- }
-
subdev = tegra_channel_get_remote_bridge_subdev(chan);
if (!subdev) {
ret = -ENODEV;
diff --git a/drivers/media/platform/nvidia/tegra-video/vi.h b/drivers/media/platform/nvidia/tegra-video/vi.h
index ac86f11588f95..fb377d428d510 100644
--- a/drivers/media/platform/nvidia/tegra-video/vi.h
+++ b/drivers/media/platform/nvidia/tegra-video/vi.h
@@ -29,8 +29,6 @@
#define TEGRA_DEF_HEIGHT 1080
#define TEGRA_IMAGE_FORMAT_DEF 32

-#define MAX_FORMAT_NUM 64
-
enum tegra_vi_pg_mode {
TEGRA_VI_PG_DISABLED = 0,
TEGRA_VI_PG_DIRECT,
@@ -172,8 +170,6 @@ struct tegra_vi {
*
* @ctrl_handler: V4L2 control handler of this video channel
* @syncpt_timeout_retry: syncpt timeout retry count for the capture
- * @fmts_bitmap: a bitmap for supported formats matching v4l2 subdev formats
- * @tpg_fmts_bitmap: a bitmap for supported TPG formats
* @pg_mode: test pattern generator mode (disabled/direct/patch)
* @notifier: V4L2 asynchronous subdevs notifier
*
@@ -223,8 +219,6 @@ struct tegra_vi_channel {

struct v4l2_ctrl_handler ctrl_handler;
unsigned int syncpt_timeout_retry;
- DECLARE_BITMAP(fmts_bitmap, MAX_FORMAT_NUM);
- DECLARE_BITMAP(tpg_fmts_bitmap, MAX_FORMAT_NUM);
enum tegra_vi_pg_mode pg_mode;

struct v4l2_async_notifier notifier;
--
2.53.0