[PATCH v1 12/13] media: tegra-video: csi: Add pad and media operations for own CSI channels

From: Svyatoslav Ryhel

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


Implement pad format enumeration, format negotiation clamped to SoC
limits via vi_fmt_align(), subdevice state initialization across sink and
source pads, and v4l2_subdev_link_validate() for CSI bridge channels
when TPG mode is disabled.

Signed-off-by: Svyatoslav Ryhel <clamor95@xxxxxxxxx>
---
.../media/platform/nvidia/tegra-video/csi.c | 154 +++++++++++++++++-
1 file changed, 152 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/nvidia/tegra-video/csi.c b/drivers/media/platform/nvidia/tegra-video/csi.c
index fb6608e0f632d..aaab884d99f54 100644
--- a/drivers/media/platform/nvidia/tegra-video/csi.c
+++ b/drivers/media/platform/nvidia/tegra-video/csi.c
@@ -322,6 +322,138 @@ void tegra_csi_calc_settle_time(struct tegra_csi_channel *csi_chan,
/ (2 * pix_clk_mhz) - 5000) / 1000;
}

+/*
+ * Proper CSI configuration
+ */
+
+enum tegra_csi_bridge_pad {
+ TEGRA_CSI_PAD_SINK = 0,
+ TEGRA_CSI_PAD_SOURCE = 1,
+ TEGRA_CSI_NUM_PADS,
+};
+
+static const u32 tegra_csi_mbus_formats[] = {
+ /* RAW 8 */
+ MEDIA_BUS_FMT_SGBRG8_1X8,
+ MEDIA_BUS_FMT_SBGGR8_1X8,
+ MEDIA_BUS_FMT_SRGGB8_1X8,
+ MEDIA_BUS_FMT_SGRBG8_1X8,
+ /* RAW 10 */
+ MEDIA_BUS_FMT_SGBRG10_1X10,
+ MEDIA_BUS_FMT_SBGGR10_1X10,
+ MEDIA_BUS_FMT_SRGGB10_1X10,
+ MEDIA_BUS_FMT_SGRBG10_1X10,
+ /* RAW 12 */
+ MEDIA_BUS_FMT_SGBRG12_1X12,
+ MEDIA_BUS_FMT_SBGGR12_1X12,
+ MEDIA_BUS_FMT_SRGGB12_1X12,
+ MEDIA_BUS_FMT_SGRBG12_1X12,
+ /* RGB888 */
+ MEDIA_BUS_FMT_RGB888_1X24,
+ MEDIA_BUS_FMT_RGB888_1X32_PADHI,
+ /* YUV422 */
+ MEDIA_BUS_FMT_UYVY8_1X16,
+ MEDIA_BUS_FMT_VYUY8_1X16,
+ MEDIA_BUS_FMT_YUYV8_1X16,
+ MEDIA_BUS_FMT_YVYU8_1X16,
+};
+
+static u32 tegra_csi_get_format_code(u32 code)
+{
+ u32 i;
+
+ for (i = 0; i < ARRAY_SIZE(tegra_csi_mbus_formats); i++)
+ if (tegra_csi_mbus_formats[i] == code)
+ break;
+
+ if (i >= ARRAY_SIZE(tegra_csi_mbus_formats))
+ return MEDIA_BUS_FMT_SRGGB10_1X10;
+
+ return tegra_csi_mbus_formats[i];
+}
+
+/*
+ * V4L2 Subdevice Operations
+ */
+static int tegra_csi_enum_bus_code(struct v4l2_subdev *subdev,
+ struct v4l2_subdev_state *state,
+ struct v4l2_subdev_mbus_code_enum *code)
+{
+ if (code->pad == TEGRA_CSI_PAD_SOURCE) {
+ const struct v4l2_mbus_framefmt *sink_fmt;
+
+ if (code->index)
+ return -EINVAL;
+
+ sink_fmt = v4l2_subdev_state_get_format(state,
+ TEGRA_CSI_PAD_SINK);
+ code->code = sink_fmt->code;
+
+ return 0;
+ }
+
+ code->code = tegra_csi_get_format_code(code->index);
+
+ return 0;
+}
+
+static int tegra_csi_set_format(struct v4l2_subdev *subdev,
+ const struct v4l2_subdev_client_info *ci,
+ struct v4l2_subdev_state *state,
+ struct v4l2_subdev_format *format)
+{
+ struct tegra_vi_channel *chan = v4l2_get_subdev_hostdata(subdev);
+ struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
+ struct v4l2_mbus_framefmt *sink_fmt, *src_fmt;
+ struct v4l2_frmsize_stepwise stepwise = { };
+ struct tegra_vi *vi = chan->vi;
+
+ /* The format on the source pad always matches the sink pad. */
+ if (format->pad == TEGRA_CSI_PAD_SOURCE)
+ return v4l2_subdev_get_fmt(subdev, state, format);
+
+ sink_fmt = v4l2_subdev_state_get_format(state, TEGRA_CSI_PAD_SINK);
+ src_fmt = v4l2_subdev_state_get_format(state, TEGRA_CSI_PAD_SOURCE);
+ if (!sink_fmt || !src_fmt)
+ return -EINVAL;
+
+ mbus_fmt->code = tegra_csi_get_format_code(mbus_fmt->code);
+ mbus_fmt->field = V4L2_FIELD_NONE;
+
+ /*
+ * CSI and VI have same size restrictions but they are SoC specific.
+ * Use VI operation to get them.
+ */
+ vi->ops->vi_fmt_align(NULL, &stepwise, 0);
+
+ mbus_fmt->width = clamp_t(u32, mbus_fmt->width, stepwise.min_width,
+ stepwise.max_width);
+ mbus_fmt->height = clamp_t(u32, mbus_fmt->height, stepwise.min_height,
+ stepwise.max_height);
+
+ *sink_fmt = *mbus_fmt;
+ *src_fmt = *mbus_fmt;
+
+ return 0;
+}
+
+static int tegra_csi_init_state(struct v4l2_subdev *subdev,
+ struct v4l2_subdev_state *state)
+{
+ struct tegra_csi_channel *chan = to_csi_chan(subdev);
+ struct v4l2_mbus_framefmt *fmt[TEGRA_CSI_NUM_PADS];
+
+ for (int i = 0; i < chan->numpads; i++) {
+ fmt[i] = v4l2_subdev_state_get_format(state, i);
+ if (!fmt[i])
+ continue;
+
+ *fmt[i] = chan->format;
+ }
+
+ return 0;
+}
+
static int tegra_csi_enable_stream(struct v4l2_subdev *subdev)
{
struct tegra_vi_channel *chan = v4l2_get_subdev_hostdata(subdev);
@@ -483,6 +615,12 @@ static const struct v4l2_subdev_pad_ops tegra_csi_tpg_pad_ops = {
.set_frame_interval = tegra_csi_tpg_set_frame_interval,
};

+static const struct v4l2_subdev_pad_ops tegra_csi_pad_ops = {
+ .enum_mbus_code = tegra_csi_enum_bus_code,
+ .get_fmt = v4l2_subdev_get_fmt,
+ .set_fmt = tegra_csi_set_format,
+};
+
static const struct v4l2_subdev_ops tegra_csi_tpg_ops = {
.video = &tegra_csi_video_ops,
.pad = &tegra_csi_tpg_pad_ops,
@@ -490,12 +628,21 @@ static const struct v4l2_subdev_ops tegra_csi_tpg_ops = {

static const struct v4l2_subdev_ops tegra_csi_ops = {
.video = &tegra_csi_video_ops,
+ .pad = &tegra_csi_pad_ops,
};

static const struct v4l2_subdev_internal_ops tegra_csi_tpg_internal_ops = {
.init_state = tegra_csi_tpg_init_state,
};

+static const struct v4l2_subdev_internal_ops tegra_csi_internal_ops = {
+ .init_state = tegra_csi_init_state,
+};
+
+static const struct media_entity_operations tegra_csi_media_ops = {
+ .link_validate = v4l2_subdev_link_validate,
+};
+
struct v4l2_subdev *tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
{
struct media_pad *pad;
@@ -544,8 +691,8 @@ static int tegra_csi_channel_alloc(struct tegra_csi *csi,
chan->of_node = of_node_get(node);
chan->numpads = num_pads;
if (num_pads & 0x2) {
- chan->pads[0].flags = MEDIA_PAD_FL_SINK;
- chan->pads[1].flags = MEDIA_PAD_FL_SOURCE;
+ chan->pads[TEGRA_CSI_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
+ chan->pads[TEGRA_CSI_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
} else {
chan->pads[0].flags = MEDIA_PAD_FL_SOURCE;
}
@@ -676,6 +823,8 @@ static int tegra_csi_channel_init(struct tegra_csi_channel *chan)
snprintf(subdev->name, sizeof(subdev->name), "%s-%d", "tpg",
chan->csi_port_nums[0]);
} else {
+ v4l2_subdev_init(subdev, &tegra_csi_ops);
+ subdev->internal_ops = &tegra_csi_internal_ops;
snprintf(subdev->name, sizeof(subdev->name), "%s",
kbasename(chan->of_node->full_name));
}
@@ -684,6 +833,7 @@ static int tegra_csi_channel_init(struct tegra_csi_channel *chan)
v4l2_set_subdevdata(subdev, chan);
subdev->fwnode = of_fwnode_handle(chan->of_node);
subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
+ subdev->entity.ops = &tegra_csi_media_ops;
subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;

/* initialize media entity pads */
--
2.53.0