[PATCH RESEND v2 07/12] media: qcom: camss: Add streams API support in tpg subdev

From: Gjorgji Rosikopulos

Date: Wed Nov 25 2026 - 13:24:06 EST


Add support for the TPG subdevice to generate multiple concurrent
virtual channels under the streams API. Replace the single fmt field
with a per-stream array sized MSM_TPG_MAX_STREAMS and track the set
of active streams in a new enabled_streams bitmask.

Add tpg_pad_enable_streams()/tpg_pad_disable_streams() pad ops that
update enabled_streams and (re)configure the hardware, a
tpg_get_frame_desc() implementation that reports one CSI-2 frame
descriptor entry per active route with VC equal to the stream index,
and a tpg_init_state() internal op that sets up one default route per
stream.

Signed-off-by: Gjorgji Rosikopulos <gjorgji.rosikopulos@xxxxxxxxxxxxxxxx>
---
.../platform/qcom/camss/camss-tpg-gen1.c | 63 +++---
drivers/media/platform/qcom/camss/camss-tpg.c | 179 +++++++++++++++++-
drivers/media/platform/qcom/camss/camss-tpg.h | 6 +-
3 files changed, 205 insertions(+), 43 deletions(-)

diff --git a/drivers/media/platform/qcom/camss/camss-tpg-gen1.c b/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
index d29de5f93c18..22fdeef8e76f 100644
--- a/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
+++ b/drivers/media/platform/qcom/camss/camss-tpg-gen1.c
@@ -5,6 +5,7 @@
*
* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
*/
+#include <linux/bitops.h>
#include <linux/io.h>
#include <linux/kernel.h>

@@ -106,13 +107,16 @@ static int tpg_stream_on(struct tpg_device *tpg)
u8 payload_mode = (tg->mode > TPG_PAYLOAD_MODE_DISABLED) ?
tg->mode - 1 : 0;
u8 lane_cnt = tpg->res->lane_cnt;
- u8 vc, dt, last_vc = 0;
+ u8 vc, last_vc = fls64(tpg->enabled_streams) - 1;
u32 val;

- for (vc = 0; vc <= MSM_TPG_ACTIVE_VC; vc++) {
- last_vc = vc;
-
- input_format = &tpg->fmt;
+ /*
+ * The hardware generates traffic for VC 0 up to last_vc as a
+ * contiguous block, so every VC in that range must be configured
+ * with a valid format, not just the ones actually streaming.
+ */
+ for (vc = 0; vc <= last_vc; vc++) {
+ input_format = &tpg->fmt[vc];
format = tpg_get_fmt_entry(tpg->res->formats->formats,
tpg->res->formats->nformats,
input_format->code);
@@ -120,7 +124,8 @@ static int tpg_stream_on(struct tpg_device *tpg)
return -EINVAL;

/* VC configuration */
- val = FIELD_PREP(TPG_VC_n_CFG0_NUM_ACTIVE_DT, MSM_TPG_ACTIVE_DT) |
+ val = FIELD_PREP(TPG_VC_n_CFG0_VC_NUM, vc) |
+ FIELD_PREP(TPG_VC_n_CFG0_NUM_ACTIVE_DT, 0) |
FIELD_PREP(TPG_VC_n_CFG0_NUM_FRAMES, 0);
writel(val, tpg->base + TPG_VC_n_CFG0(vc));

@@ -136,31 +141,29 @@ static int tpg_stream_on(struct tpg_device *tpg)
writel(TPG_COLOR_BARS_CFG_STANDARD, tpg->base + TPG_VC_n_COLOR_BARS_CFG(vc));

/* DT configuration */
- for (dt = 0; dt <= MSM_TPG_ACTIVE_DT; dt++) {
- val = FIELD_PREP(TPG_VC_m_DT_n_CFG_0_FRAME_HEIGHT,
- input_format->height & 0xffff) |
- FIELD_PREP(TPG_VC_m_DT_n_CFG_0_FRAME_WIDTH,
- input_format->width & 0xffff);
- writel(val, tpg->base + TPG_VC_m_DT_n_CFG_0(vc, dt));
-
- val = FIELD_PREP(TPG_VC_m_DT_n_CFG_1_DATA_TYPE, format->data_type);
- writel(val, tpg->base + TPG_VC_m_DT_n_CFG_1(vc, dt));
-
- if (tpg->hw_version == TPG_HW_VER_2_0_0) {
- val = FIELD_PREP(TPG_VC_m_DT_n_CFG_2_PAYLOAD_MODE, payload_mode) |
- FIELD_PREP(TPG_V2_0_0_VC_m_DT_n_CFG_2_USER_SPECIFIED_PAYLOAD,
- TPG_USER_SPECIFIED_PAYLOAD_DEFAULT) |
- FIELD_PREP(TPG_V2_0_0_VC_m_DT_n_CFG_2_ENCODE_FORMAT,
- format->encode_format);
- } else if (tpg->hw_version >= TPG_HW_VER_2_1_0) {
- val = FIELD_PREP(TPG_VC_m_DT_n_CFG_2_PAYLOAD_MODE, payload_mode) |
- FIELD_PREP(TPG_V2_1_0_VC_m_DT_n_CFG_2_USER_SPECIFIED_PAYLOAD,
- TPG_USER_SPECIFIED_PAYLOAD_DEFAULT) |
- FIELD_PREP(TPG_V2_1_0_VC_m_DT_n_CFG_2_ENCODE_FORMAT,
- format->encode_format);
- }
- writel(val, tpg->base + TPG_VC_m_DT_n_CFG_2(vc, dt));
+ val = FIELD_PREP(TPG_VC_m_DT_n_CFG_0_FRAME_HEIGHT,
+ input_format->height & 0xffff) |
+ FIELD_PREP(TPG_VC_m_DT_n_CFG_0_FRAME_WIDTH,
+ input_format->width & 0xffff);
+ writel(val, tpg->base + TPG_VC_m_DT_n_CFG_0(vc, 0));
+
+ val = FIELD_PREP(TPG_VC_m_DT_n_CFG_1_DATA_TYPE, format->data_type);
+ writel(val, tpg->base + TPG_VC_m_DT_n_CFG_1(vc, 0));
+
+ if (tpg->hw_version == TPG_HW_VER_2_0_0) {
+ val = FIELD_PREP(TPG_VC_m_DT_n_CFG_2_PAYLOAD_MODE, payload_mode) |
+ FIELD_PREP(TPG_V2_0_0_VC_m_DT_n_CFG_2_USER_SPECIFIED_PAYLOAD,
+ TPG_USER_SPECIFIED_PAYLOAD_DEFAULT) |
+ FIELD_PREP(TPG_V2_0_0_VC_m_DT_n_CFG_2_ENCODE_FORMAT,
+ format->encode_format);
+ } else if (tpg->hw_version >= TPG_HW_VER_2_1_0) {
+ val = FIELD_PREP(TPG_VC_m_DT_n_CFG_2_PAYLOAD_MODE, payload_mode) |
+ FIELD_PREP(TPG_V2_1_0_VC_m_DT_n_CFG_2_USER_SPECIFIED_PAYLOAD,
+ TPG_USER_SPECIFIED_PAYLOAD_DEFAULT) |
+ FIELD_PREP(TPG_V2_1_0_VC_m_DT_n_CFG_2_ENCODE_FORMAT,
+ format->encode_format);
}
+ writel(val, tpg->base + TPG_VC_m_DT_n_CFG_2(vc, 0));
}

/* Global TPG control */
diff --git a/drivers/media/platform/qcom/camss/camss-tpg.c b/drivers/media/platform/qcom/camss/camss-tpg.c
index 5764a60fa34c..a7ef6d2bb4e8 100644
--- a/drivers/media/platform/qcom/camss/camss-tpg.c
+++ b/drivers/media/platform/qcom/camss/camss-tpg.c
@@ -200,22 +200,78 @@ static int tpg_set_stream(struct v4l2_subdev *sd, int enable)
"could not sync v4l2 controls: %d\n", ret);
return ret;
}
+
+ tpg->enabled_streams = BIT_ULL(0);
+ } else {
+ tpg->enabled_streams = 0;
}

return tpg->res->hw_ops->configure_stream(tpg, enable);
}

+static int tpg_pad_enable_streams(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *state,
+ u32 pad, u64 streams_mask)
+{
+ struct tpg_device *tpg = v4l2_get_subdevdata(sd);
+ bool start = !tpg->enabled_streams;
+ int ret;
+
+ if (pad != MSM_TPG_PAD_SRC)
+ return -EINVAL;
+
+ tpg->enabled_streams |= streams_mask;
+ if (!start)
+ return tpg->res->hw_ops->configure_stream(tpg, 1);
+
+ ret = v4l2_ctrl_handler_setup(&tpg->ctrls);
+ if (ret < 0) {
+ tpg->enabled_streams &= ~streams_mask;
+ return ret;
+ }
+
+ ret = tpg->res->hw_ops->configure_stream(tpg, 1);
+ if (ret)
+ tpg->enabled_streams &= ~streams_mask;
+
+ return ret;
+}
+
+static int tpg_pad_disable_streams(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *state,
+ u32 pad, u64 streams_mask)
+{
+ struct tpg_device *tpg = v4l2_get_subdevdata(sd);
+
+ if (pad != MSM_TPG_PAD_SRC)
+ return -EINVAL;
+
+ tpg->enabled_streams &= ~streams_mask;
+
+ /*
+ * Growing the active VC range live is safe, but shrinking it while
+ * the generator is still running for the remaining VCs corrupts its
+ * state machine. Only touch the hardware when the last VC disables.
+ */
+ if (tpg->enabled_streams)
+ return 0;
+
+ return tpg->res->hw_ops->configure_stream(tpg, 0);
+}
+
static struct v4l2_mbus_framefmt *
__tpg_get_format(struct tpg_device *tpg,
struct v4l2_subdev_state *sd_state,
- unsigned int pad,
+ unsigned int pad, u32 stream,
enum v4l2_subdev_format_whence which)
{
+ if (pad != MSM_TPG_PAD_SRC || stream >= MSM_TPG_MAX_STREAMS)
+ return NULL;
+
if (which == V4L2_SUBDEV_FORMAT_TRY)
- return v4l2_subdev_state_get_format(sd_state,
- pad);
+ return v4l2_subdev_state_get_format(sd_state, pad, stream);

- return &tpg->fmt;
+ return &tpg->fmt[stream];
}

static void tpg_try_format(struct tpg_device *tpg,
@@ -282,7 +338,7 @@ static int tpg_get_format(struct v4l2_subdev *sd,
struct tpg_device *tpg = v4l2_get_subdevdata(sd);
struct v4l2_mbus_framefmt *format;

- format = __tpg_get_format(tpg, sd_state, fmt->pad, fmt->which);
+ format = __tpg_get_format(tpg, sd_state, fmt->pad, fmt->stream, fmt->which);
if (!format)
return -EINVAL;

@@ -299,10 +355,13 @@ static int tpg_set_format(struct v4l2_subdev *sd,
struct tpg_device *tpg = v4l2_get_subdevdata(sd);
struct v4l2_mbus_framefmt *format;

- format = __tpg_get_format(tpg, sd_state, fmt->pad, fmt->which);
+ format = __tpg_get_format(tpg, sd_state, fmt->pad, fmt->stream, fmt->which);
if (!format)
return -EINVAL;

+ if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE && tpg->enabled_streams)
+ return -EBUSY;
+
tpg_try_format(tpg, &fmt->format);
*format = fmt->format;

@@ -326,6 +385,69 @@ static int tpg_init_formats(struct v4l2_subdev *sd,
return tpg_set_format(sd, NULL, fh ? fh->state : NULL, &format);
}

+static int tpg_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+ struct v4l2_mbus_frame_desc *fd)
+{
+ struct tpg_device *tpg = v4l2_get_subdevdata(sd);
+ struct v4l2_subdev_state *state;
+ struct v4l2_subdev_route *route;
+ unsigned int i = 0;
+
+ if (pad != MSM_TPG_PAD_SRC)
+ return -EINVAL;
+
+ state = v4l2_subdev_lock_and_get_active_state(sd);
+ fd->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
+
+ for_each_active_route(&state->routing, route) {
+ const struct tpg_format_info *format;
+
+ format = tpg_get_fmt_entry(tpg->res->formats->formats,
+ tpg->res->formats->nformats,
+ tpg->fmt[route->source_stream].code);
+ if (IS_ERR(format))
+ continue;
+
+ fd->entry[i].stream = route->source_stream;
+ fd->entry[i].flags = 0;
+ fd->entry[i].bus.csi2.vc = route->source_stream;
+ fd->entry[i].bus.csi2.dt = format->data_type;
+ i++;
+ }
+
+ fd->num_entries = i;
+ v4l2_subdev_unlock_state(state);
+
+ return 0;
+}
+
+static int tpg_init_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state)
+{
+ struct tpg_device *tpg = v4l2_get_subdevdata(sd);
+ struct v4l2_subdev_route routes[MSM_TPG_MAX_STREAMS];
+ struct v4l2_subdev_krouting routing = {
+ .num_routes = ARRAY_SIZE(routes),
+ .routes = routes,
+ };
+ struct v4l2_mbus_framefmt fmt = {
+ .code = MEDIA_BUS_FMT_SBGGR8_1X8,
+ .width = 1920,
+ .height = 1080,
+ };
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(routes); i++) {
+ routes[i].sink_pad = MSM_TPG_PAD_SRC;
+ routes[i].sink_stream = i;
+ routes[i].source_pad = MSM_TPG_PAD_SRC;
+ routes[i].source_stream = i;
+ routes[i].flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE;
+ tpg->fmt[i] = fmt;
+ }
+
+ return v4l2_subdev_set_routing_with_fmt(sd, state, &routing, &fmt);
+}
+
static int tpg_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct tpg_device *tpg = container_of(ctrl->handler,
@@ -433,16 +555,36 @@ static const struct v4l2_subdev_pad_ops tpg_pad_ops = {
.set_fmt = tpg_set_format,
};

+static const struct v4l2_subdev_pad_ops tpg_streams_pad_ops = {
+ .enum_mbus_code = tpg_enum_mbus_code,
+ .enum_frame_size = tpg_enum_frame_size,
+ .get_fmt = tpg_get_format,
+ .set_fmt = tpg_set_format,
+ .get_frame_desc = tpg_get_frame_desc,
+ .enable_streams = tpg_pad_enable_streams,
+ .disable_streams = tpg_pad_disable_streams,
+};
+
static const struct v4l2_subdev_ops tpg_v4l2_ops = {
.core = &tpg_core_ops,
.video = &tpg_video_ops,
.pad = &tpg_pad_ops,
};

+static const struct v4l2_subdev_ops tpg_streams_v4l2_ops = {
+ .core = &tpg_core_ops,
+ .video = &tpg_video_ops,
+ .pad = &tpg_streams_pad_ops,
+};
+
static const struct v4l2_subdev_internal_ops tpg_v4l2_internal_ops = {
.open = tpg_init_formats,
};

+static const struct v4l2_subdev_internal_ops tpg_streams_internal_ops = {
+ .init_state = tpg_init_state,
+};
+
static const struct media_entity_operations tpg_media_ops = {
.link_setup = tpg_link_setup,
.link_validate = v4l2_subdev_link_validate,
@@ -453,12 +595,15 @@ int msm_tpg_register_entity(struct tpg_device *tpg,
{
struct v4l2_subdev *sd = &tpg->subdev;
struct device *dev = tpg->camss->dev;
+ bool streams_api = tpg->camss->res->streams_enable;
int ret;

- v4l2_subdev_init(sd, &tpg_v4l2_ops);
- sd->internal_ops = &tpg_v4l2_internal_ops;
+ v4l2_subdev_init(sd, streams_api ? &tpg_streams_v4l2_ops : &tpg_v4l2_ops);
+ sd->internal_ops = streams_api ? &tpg_streams_internal_ops : &tpg_v4l2_internal_ops;
sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
V4L2_SUBDEV_FL_HAS_EVENTS;
+ if (streams_api)
+ sd->flags |= V4L2_SUBDEV_FL_STREAMS;
snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
"msm_tpg", tpg->id);
sd->grp_id = TPG_GRP_ID;
@@ -497,15 +642,27 @@ int msm_tpg_register_entity(struct tpg_device *tpg,
goto free_ctrl;
}

+ if (streams_api) {
+ ret = v4l2_subdev_init_finalize(sd);
+ if (ret) {
+ dev_err(dev, "Failed to finalize subdev: %d\n", ret);
+ goto media_cleanup;
+ }
+ }
+
ret = v4l2_device_register_subdev(v4l2_dev, sd);
if (ret < 0) {
dev_err(dev, "Failed to register subdev: %d\n", ret);
- media_entity_cleanup(&sd->entity);
- goto free_ctrl;
+ goto subdev_cleanup;
}

return 0;

+subdev_cleanup:
+ if (streams_api)
+ v4l2_subdev_cleanup(sd);
+media_cleanup:
+ media_entity_cleanup(&sd->entity);
free_ctrl:
v4l2_ctrl_handler_free(&tpg->ctrls);

@@ -515,6 +672,8 @@ int msm_tpg_register_entity(struct tpg_device *tpg,
void msm_tpg_unregister_entity(struct tpg_device *tpg)
{
v4l2_device_unregister_subdev(&tpg->subdev);
+ if (tpg->camss->res->streams_enable)
+ v4l2_subdev_cleanup(&tpg->subdev);
media_entity_cleanup(&tpg->subdev.entity);
v4l2_ctrl_handler_free(&tpg->ctrls);
}
diff --git a/drivers/media/platform/qcom/camss/camss-tpg.h b/drivers/media/platform/qcom/camss/camss-tpg.h
index 7fb35a97dd06..5a6e9e726716 100644
--- a/drivers/media/platform/qcom/camss/camss-tpg.h
+++ b/drivers/media/platform/qcom/camss/camss-tpg.h
@@ -26,8 +26,7 @@
#define ENCODE_FORMAT_UNCOMPRESSED_24_BIT 0x7

#define MSM_TPG_PAD_SRC 0
-#define MSM_TPG_ACTIVE_VC 0
-#define MSM_TPG_ACTIVE_DT 0
+#define MSM_TPG_MAX_STREAMS 4

#define TPG_MIN_WIDTH 1
#define TPG_MIN_HEIGHT 1
@@ -89,11 +88,12 @@ struct tpg_device {
struct camss_clock *clock;
int nclocks;
struct tpg_testgen_config testgen;
- struct v4l2_mbus_framefmt fmt;
+ struct v4l2_mbus_framefmt fmt[MSM_TPG_MAX_STREAMS];
struct v4l2_ctrl_handler ctrls;
struct v4l2_ctrl *testgen_mode;
const struct tpg_subdev_resources *res;
u32 hw_version;
+ u64 enabled_streams;
};

struct camss_subdev_resources;
--
2.34.1