[PATCH RESEND v2 04/12] media: qcom: camss: Add streams API support for CSIPHY
From: Gjorgji Rosikopulos
Date: Wed Nov 25 2026 - 13:23:58 EST
Implement the V4L2 subdev streams API (enable_streams/disable_streams,
set_routing) on CSIPHY, selected per SoC via the new streams_enable
field on struct camss_resources.
The shared D-PHY lanes are enabled once, on the transition to an
active state, and torn down once no sink stream is referenced by any
source pad any more. CSIPHY has no per-VC demux, so every route must
pass a sink stream straight through as the same source stream; a
multi-VC sensor is supported by userspace adding further routes via
.set_routing.
CSIPHY can be wired through either the legacy s_power/s_stream path
or the generic PHY framework (camss->legacy_phy); the streams-API
pad ops are shared between both, with enable/disable dispatching to
the matching backend.
Co-developed-by: Hitesh Patel <hitesh@xxxxxxxxxxxxxx>
Signed-off-by: Hitesh Patel <hitesh@xxxxxxxxxxxxxx>
Signed-off-by: Gjorgji Rosikopulos <gjorgji.rosikopulos@xxxxxxxxxxxxxxxx>
---
.../media/platform/qcom/camss/camss-csiphy.c | 249 +++++++++++++++++-
.../media/platform/qcom/camss/camss-csiphy.h | 1 +
2 files changed, 244 insertions(+), 6 deletions(-)
diff --git a/drivers/media/platform/qcom/camss/camss-csiphy.c b/drivers/media/platform/qcom/camss/camss-csiphy.c
index f99eea2ef228..41df8f1dad83 100644
--- a/drivers/media/platform/qcom/camss/camss-csiphy.c
+++ b/drivers/media/platform/qcom/camss/camss-csiphy.c
@@ -421,6 +421,129 @@ static int csiphy_set_stream(struct v4l2_subdev *sd, int enable)
return ret;
}
+/*
+ * csiphy_streams_set_stream - Enable/disable streaming on CSIPHY module
+ * @sd: CSIPHY V4L2 subdevice
+ * @enable: Requested streaming state
+ *
+ * Dispatches to the legacy or generic PHY depending on how this
+ * CSIPHY instance is wired, mirroring msm_csiphy_register_entity().
+ *
+ * Return 0 on success or a negative error code otherwise
+ */
+static int csiphy_streams_set_stream(struct v4l2_subdev *sd, int enable)
+{
+ struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
+
+ if (csiphy->camss->legacy_phy)
+ return csiphy_set_stream_legacy(sd, enable);
+
+ return csiphy_set_stream(sd, enable);
+}
+
+/*
+ * csiphy_pad_enable_streams - Enable one or more streams on the source pad
+ * @sd: CSIPHY V4L2 subdevice
+ * @state: V4L2 subdevice state
+ * @pad: Pad number
+ * @streams_mask: Bitmask of streams to enable
+ *
+ * The shared PHY lanes are enabled once, on the transition to an active
+ * state. The sink stream(s) are propagated upstream to the sensor only for
+ * streams which are inactive, so a stream that's already running is never
+ * redundantly re-propagated.
+ *
+ * CSIPHY is only ever linked to a single entity on its source pad, and that
+ * entity is responsible for only enabling a stream on this pad while it
+ * itself still needs it, so no cross-consumer refcounting is needed here.
+ *
+ * Return 0 on success or a negative error code otherwise
+ */
+static int csiphy_pad_enable_streams(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *state,
+ u32 pad, u64 streams_mask)
+{
+ struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
+ struct media_pad *remote_pad =
+ media_pad_remote_pad_first(&csiphy->pads[MSM_CSIPHY_PAD_SINK]);
+ u64 sink_streams, propagate_mask;
+ int ret;
+
+ if (!csiphy->enabled_streams) {
+ ret = csiphy_streams_set_stream(sd, 1);
+ if (ret)
+ return ret;
+ }
+
+ sink_streams = v4l2_subdev_state_xlate_streams(state, pad, MSM_CSIPHY_PAD_SINK,
+ &streams_mask);
+
+ propagate_mask = sink_streams & ~csiphy->enabled_streams;
+ if (propagate_mask) {
+ struct v4l2_subdev *remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
+
+ ret = v4l2_subdev_enable_streams(remote_sd, remote_pad->index, propagate_mask);
+ if (ret) {
+ if (!csiphy->enabled_streams)
+ csiphy_streams_set_stream(sd, 0);
+
+ return ret;
+ }
+ }
+ csiphy->enabled_streams |= sink_streams;
+
+ return 0;
+}
+
+/*
+ * csiphy_pad_disable_streams - Disable one or more streams on the source pad
+ * @sd: CSIPHY V4L2 subdevice
+ * @state: V4L2 subdevice state
+ * @pad: Pad number
+ * @streams_mask: Bitmask of streams to disable
+ *
+ * The shared PHY lanes, and the propagation to the sensor, are only torn
+ * down once no sink stream is referenced by any source pad any more.
+ *
+ * CSIPHY is only ever linked to a single entity on its source pad, and that
+ * entity is responsible for only disabling a stream on this pad once it no
+ * longer needs it, so no cross-consumer refcounting is needed here.
+ * If error occurs propagate the error but continue to disable the streams
+ * on best effort.
+ *
+ * Return 0 on success or a negative error code otherwise
+ */
+static int csiphy_pad_disable_streams(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *state,
+ u32 pad, u64 streams_mask)
+{
+ struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
+ struct media_pad *remote_pad =
+ media_pad_remote_pad_first(&csiphy->pads[MSM_CSIPHY_PAD_SINK]);
+ u64 sink_streams;
+ int ret = 0;
+
+ sink_streams = v4l2_subdev_state_xlate_streams(state, pad, MSM_CSIPHY_PAD_SINK,
+ &streams_mask);
+
+ csiphy->enabled_streams &= ~sink_streams;
+
+ if (!csiphy->enabled_streams)
+ ret = csiphy_streams_set_stream(sd, 0);
+
+ if (sink_streams) {
+ struct v4l2_subdev *remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
+ int remote_ret;
+
+ remote_ret = v4l2_subdev_disable_streams(remote_sd, remote_pad->index,
+ sink_streams);
+ if (!ret)
+ ret = remote_ret;
+ }
+
+ return ret;
+}
+
/*
* __csiphy_get_format - Get pointer to format structure
* @csiphy: CSIPHY device
@@ -881,6 +1004,71 @@ static int csiphy_link_setup(struct media_entity *entity,
return 0;
}
+static int csiphy_init_state(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *state)
+{
+ struct v4l2_subdev_route routes[] = {
+ {
+ .sink_pad = MSM_CSIPHY_PAD_SINK,
+ .sink_stream = 0,
+ .source_pad = MSM_CSIPHY_PAD_SRC,
+ .source_stream = 0,
+ .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
+ },
+ };
+ struct v4l2_subdev_krouting routing = {
+ .num_routes = ARRAY_SIZE(routes),
+ .routes = routes,
+ };
+
+ /*
+ * CSIPHY has no per-VC demux, so every sink stream (VC) a
+ * multi-stream sensor may drive must pass straight through as the
+ * same source stream, or downstream link validation (e.g. against
+ * CSID's multi-pad sink) will flag it as dangling. A multi-VC sensor
+ * is supported by userspace adding further routes via .set_routing;
+ * this default covers the common single-VC case.
+ */
+ return v4l2_subdev_set_routing(sd, state, &routing);
+}
+
+/*
+ * csiphy_set_routing - Set routing for the CSIPHY subdev
+ * @sd: CSIPHY V4L2 subdevice
+ * @state: V4L2 subdevice state
+ * @which: Type of format state (V4L2_SUBDEV_FORMAT_ACTIVE or TRY)
+ * @routing: Routing table to set
+ *
+ * CSIPHY has no per-VC demux, so every route must pass a sink stream
+ * straight through as the same source stream.
+ *
+ * Return 0 on success or a negative error code otherwise
+ */
+static int csiphy_set_routing(struct v4l2_subdev *sd,
+ struct v4l2_subdev_state *state,
+ enum v4l2_subdev_format_whence which,
+ struct v4l2_subdev_krouting *routing)
+{
+ struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
+ unsigned int i;
+ int ret;
+
+ if (which == V4L2_SUBDEV_FORMAT_ACTIVE && csiphy->enabled_streams)
+ return -EBUSY;
+
+ for (i = 0; i < routing->num_routes; i++)
+ if (routing->routes[i].sink_stream != routing->routes[i].source_stream)
+ return -EINVAL;
+
+ ret = v4l2_subdev_routing_validate(sd, routing,
+ V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
+ V4L2_SUBDEV_ROUTING_NO_N_TO_1);
+ if (ret)
+ return ret;
+
+ return v4l2_subdev_set_routing(sd, state, routing);
+}
+
static const struct v4l2_subdev_core_ops csiphy_core_ops_legacy = {
.s_power = csiphy_set_power_legacy,
};
@@ -911,10 +1099,40 @@ static const struct v4l2_subdev_ops csiphy_v4l2_ops = {
.pad = &csiphy_pad_ops,
};
+static const struct v4l2_subdev_pad_ops csiphy_streams_pad_ops = {
+ .enum_mbus_code = csiphy_enum_mbus_code,
+ .enum_frame_size = csiphy_enum_frame_size,
+ .get_fmt = csiphy_get_format,
+ .set_fmt = csiphy_set_format,
+ .get_frame_desc = v4l2_subdev_get_frame_desc_passthrough,
+ .set_routing = csiphy_set_routing,
+ .enable_streams = csiphy_pad_enable_streams,
+ .disable_streams = csiphy_pad_disable_streams,
+};
+
+static const struct v4l2_subdev_video_ops csiphy_streams_video_ops = {
+ .s_stream = v4l2_subdev_s_stream_helper,
+};
+
+static const struct v4l2_subdev_ops csiphy_streams_v4l2_ops_legacy = {
+ .core = &csiphy_core_ops_legacy,
+ .pad = &csiphy_streams_pad_ops,
+ .video = &csiphy_streams_video_ops,
+};
+
+static const struct v4l2_subdev_ops csiphy_streams_v4l2_ops = {
+ .pad = &csiphy_streams_pad_ops,
+ .video = &csiphy_streams_video_ops,
+};
+
static const struct v4l2_subdev_internal_ops csiphy_v4l2_internal_ops = {
.open = csiphy_init_formats,
};
+static const struct v4l2_subdev_internal_ops csiphy_streams_internal_ops = {
+ .init_state = csiphy_init_state,
+};
+
static const struct media_entity_operations csiphy_media_ops = {
.link_setup = csiphy_link_setup,
.link_validate = v4l2_subdev_link_validate,
@@ -934,15 +1152,21 @@ int msm_csiphy_register_entity(struct csiphy_device *csiphy,
struct media_pad *pads = csiphy->pads;
struct camss *camss = csiphy->camss;
struct device *dev = camss->dev;
+ bool streams_api = camss->res->streams_enable;
int ret;
- if (camss->legacy_phy)
- v4l2_subdev_init(sd, &csiphy_v4l2_ops_legacy);
+ if (streams_api)
+ v4l2_subdev_init(sd, camss->legacy_phy ? &csiphy_streams_v4l2_ops_legacy
+ : &csiphy_streams_v4l2_ops);
else
- v4l2_subdev_init(sd, &csiphy_v4l2_ops);
+ v4l2_subdev_init(sd, camss->legacy_phy ? &csiphy_v4l2_ops_legacy
+ : &csiphy_v4l2_ops);
- sd->internal_ops = &csiphy_v4l2_internal_ops;
+ sd->internal_ops = streams_api ? &csiphy_streams_internal_ops
+ : &csiphy_v4l2_internal_ops;
sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+ if (streams_api)
+ sd->flags |= V4L2_SUBDEV_FL_STREAMS;
snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
MSM_CSIPHY_NAME, csiphy->id);
sd->grp_id = CSIPHY_GRP_ID;
@@ -965,10 +1189,18 @@ int msm_csiphy_register_entity(struct csiphy_device *csiphy,
return ret;
}
+ if (streams_api) {
+ ret = v4l2_subdev_init_finalize(sd);
+ if (ret) {
+ dev_err(dev, "Failed to finalize subdev: %d\n", ret);
+ goto media_entity_err;
+ }
+ }
+
ret = v4l2_device_register_subdev(v4l2_dev, sd);
if (ret < 0) {
dev_err(dev, "Failed to register subdev: %d\n", ret);
- goto media_entity_err;
+ goto subdev_cleanup_err;
}
if (!IS_ERR(csiphy->phy)) {
@@ -976,12 +1208,15 @@ int msm_csiphy_register_entity(struct csiphy_device *csiphy,
if (ret) {
dev_err(dev, "%s init fail %d\n", csiphy->name, ret);
v4l2_device_unregister_subdev(sd);
- goto media_entity_err;
+ goto subdev_cleanup_err;
}
}
return 0;
+subdev_cleanup_err:
+ if (streams_api)
+ v4l2_subdev_cleanup(sd);
media_entity_err:
media_entity_cleanup(&sd->entity);
return ret;
@@ -996,5 +1231,7 @@ void msm_csiphy_unregister_entity(struct csiphy_device *csiphy)
if (!IS_ERR(csiphy->phy))
phy_exit(csiphy->phy);
v4l2_device_unregister_subdev(&csiphy->subdev);
+ if (csiphy->camss->res->streams_enable)
+ v4l2_subdev_cleanup(&csiphy->subdev);
media_entity_cleanup(&csiphy->subdev.entity);
}
diff --git a/drivers/media/platform/qcom/camss/camss-csiphy.h b/drivers/media/platform/qcom/camss/camss-csiphy.h
index 8c3319f2ca1c..cd15369d132e 100644
--- a/drivers/media/platform/qcom/camss/camss-csiphy.h
+++ b/drivers/media/platform/qcom/camss/camss-csiphy.h
@@ -118,6 +118,7 @@ struct csiphy_device {
struct v4l2_mbus_framefmt fmt[MSM_CSIPHY_PADS_NUM];
const struct csiphy_subdev_resources *res;
struct csiphy_device_regs *regs;
+ u64 enabled_streams;
};
struct camss_subdev_resources;
--
2.34.1