Skip to contentSkip to conversation

media: nxp: Add CSI Pixel Formatter support

PATCH Superseded Guoniu Zhou 3 participants 6 messages May 11 – May 12

Subsystems (3)

v6 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v6 cover media: nxp: Add CSI Pixel Formatter support

CSI Pixel Formatter is a module found on i.MX95. It could unpack the pixels received by the formatter and reformat them to meet the pixel link format requirement.

This patch series adds a new V4L2 driver for CSI Pixel Formatter.

v4l2-compliance 1.28.1-5233, 64 bits, 64-bit time_t v4l2-compliance SHA: fc15e229d9d3 2024-07-23 19:22:15

Compliance test for device /dev/v4l-subdev9:

Driver Info:

	Driver version   : 7.1.0
	Capabilities     : 0x00000002
		Streams Support
	Client Capabilities: 0x0000000000000003

streams interval-uses-which
Required ioctls:
test VIDIOC_SUDBEV_QUERYCAP: OK
test invalid ioctls: OK

Allow for multiple opens:
test second /dev/v4l-subdev9 open: OK
test VIDIOC_SUBDEV_QUERYCAP: OK
test for unlimited opens: OK

Debug ioctls:
test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported) test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)

Sub-Device routing ioctls:
test Try VIDIOC_SUBDEV_G_ROUTING/VIDIOC_SUBDEV_S_ROUTING: OK test Active VIDIOC_SUBDEV_G_ROUTING/VIDIOC_SUBDEV_S_ROUTING: OK

Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK (Not Supported) test VIDIOC_QUERYCTRL: OK (Not Supported)
test VIDIOC_G/S_CTRL: OK (Not Supported)
test VIDIOC_G/S/TRY_EXT_CTRLS: OK (Not Supported) test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK (Not Supported) test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 0 Private Controls: 0

Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK (Not Supported) test VIDIOC_G/S_PARM: OK (Not Supported)
test VIDIOC_G_FBUF: OK (Not Supported)
test VIDIOC_G_FMT: OK (Not Supported)
test VIDIOC_TRY_FMT: OK (Not Supported)
test VIDIOC_S_FMT: OK (Not Supported)
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)

Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK (Not Supported) test CREATE_BUFS maximum buffers: OK
test VIDIOC_REMOVE_BUFS: OK
test VIDIOC_EXPBUF: OK (Not Supported)
test Requests: OK (Not Supported)

Total for device /dev/v4l-subdev9: 47, Succeeded: 47, Failed: 0, Warnings: 0

---
Changes in v6:
- Rebase to latest media/next
- Update v4l2-compliace test
- Remove unused header includes
- Unify macro naming: VCx/VCX -> VC and parameter x -> vc
- Remove unused format field from csi_formatter struct
- Use compact initialization for formats array
- Make find_csi_format() return NULL instead of default format
- Use unsigned int for array index in find_csi_format()
- Add err_ prefix to error handling labels
- Add v4l2_subdev_cleanup() and reorder cleanup sequence
- Update enable_streams debug output format
- Rename VC_MAX to VC_NUM and fix boundary check
- Update CSI formatter Kconfig description
- Use v4l2_subdev_get_frame_desc_passthrough() helper
- Fix error paths in async registration and probe
- Add mutex to protect enabled_streams
- Switch to devm_pm_runtime_enable()
- Remove redundant num_routes check in set_routing
- Optimize get_index_by_dt() and add warning for unsupported type
- csi_formatter_start/stop_stream: Process all streams in mask
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Remove CSI_FORMATTER_DRV_NAME macro since only use once.
- Remove sd->owner = THIS_MODULE;
- Simplify code by using DEFINE_RUNTIME_DEV_PM_OPS macro.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Rebase to latest media/next.
- Add comments to describe the index field in formatter_dt_to_index_map array.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rename nxp,imx9-csi-formatter.yaml to fsl,imx9-csi-formatter.yaml.
- Drop clock-names property.
- Drop macro IMX95_PD_CAMERA definition and use a constant directly. [PATCH 1/2] media: dt-bindings: Add CSI Pixel Formatter DT bindings
- Remove the assignment driver.owner = THIS_MODULE.
- Assign struct fwnode_handle *ep __free(fwnode_handle) when definition.
- Update yaml file name for csi formatter in MAINTAINERS.
[PATCH 2/2] media: nxp: Add i.MX9 CSI pixel formatter v4l2 driver
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Delete "|" for description key.
- Add empty line between child node and property.
- Delete labels for endpoint of child nodes.
[PATCH 1/2] media: dt-bindings: Add CSI Pixel Formatter DT bindings

- Update commit message.
- Use the value defined by bellow macros directly since they are used only once.
#define CSI_FORMATTER_DEF_MBUS_CODE MEDIA_BUS_FMT_UYVY8_1X16
#define CSI_FORMATTER_DEF_PIX_WIDTH 1920U
#define CSI_FORMATTER_DEF_PIX_HEIGHT 1080U
#define CSI_FORMATTER_MAX_PIX_WIDTH 0xffff
#define CSI_FORMATTER_MAX_PIX_HEIGHT 0xffff
- Use macro pm_ptr() to fix build warning when CONFIG_PM is disabled.
- Finish route loop by break statement, instead of goto.
- Return dev_err_probe() when meet errors in probe() function instead of dev_err().
- Remove MODULE_ALIAS().
- Refine .enable(.dsable)_stream callback implementation, include bellow changes:
Add stream checking.
Fix potential pm runtime count unbalance issue.
Add stop stream error handling when enabling remote subdev stream.
- Use __free(fwnode_handle) to drop reference to a device node automatically. [PATCH 2/2] media: nxp: Add i.MX9 CSI pixel formatter v4l2 driver

- Link to v1: https://lore.kernel.org/r/[email protected]

---
Guoniu Zhou (2):
media: dt-bindings: Add CSI Pixel Formatter DT bindings
media: nxp: Add i.MX9 CSI pixel formatter v4l2 driver

 .../bindings/media/fsl,imx9-csi-formatter.yaml     |  87 +++
 MAINTAINERS                                        |   8 +
 drivers/media/platform/nxp/Kconfig                 |  14 +
 drivers/media/platform/nxp/Makefile                |   1 +
 drivers/media/platform/nxp/imx9-csi-formatter.c    | 776 +++++++++++++++++++++
 5 files changed, 886 insertions(+)

---
base-commit: bc1ba628e37c93cf2abeb2c79716f49087f8a024
change-id: 20251125-csi_formatter-e6d29316dce6

Best regards,

Signature · 1 line
Guoniu Zhou <[email protected]>
#1 lore raw

PATCH v6 1/2 media: dt-bindings: Add CSI Pixel Formatter DT bindings

From: Guoniu Zhou <[email protected]>

The i.MX9 CSI pixel formatting module uses packet info, pixel and non-pixel data from the CSI-2 host controller and reformat them to match Pixel Link(PL) definition.

---
 .../bindings/media/fsl,imx9-csi-formatter.yaml     | 87 ++++++++++++++++++++++
 1 file changed, 87 insertions(+)
Documentation/devicetree/bindings/media/fsl,imx9-csi-formatter.yaml new file +87 -0
new file mode 100644
index 000000000000..774d37d2b987
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/fsl,imx9-csi-formatter.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: i.MX9 CSI Pixel Formatter
+
+maintainers:
+  - Guoniu Zhou <[email protected]>
+
+description:
+  The CSI pixel formatting module uses packet info, pixel and non-pixel
+  data from the CSI-2 host controller and reformat them to match Pixel
+  Link(PL) definition.
+
+properties:
+  compatible:
+    const: fsl,imx9-csi-formatter
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  power-domains:
+    maxItems: 1
+
+  ports:
+    $ref: /schemas/graph.yaml#/properties/ports
+
+    properties:
+      port@0:
+        $ref: /schemas/graph.yaml#/$defs/port-base
+        unevaluatedProperties: false
+        description: MIPI CSI-2 RX IDI interface
+
+        properties:
+          endpoint:
+            $ref: video-interfaces.yaml#
+            unevaluatedProperties: false
+
+      port@1:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Pixel Link Interface
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - power-domains
+  - ports
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/nxp,imx95-clock.h>
+
+    formatter@20 {
+        compatible = "fsl,imx9-csi-formatter";
+        reg = <0x20 0x100>;
+        clocks = <&cameramix_csr IMX95_CLK_CAMBLK_CSI2_FOR0>;
+        power-domains = <&scmi_devpd 3>;
+
+        ports {
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            port@0 {
+                reg = <0>;
+
+                endpoint {
+                    remote-endpoint = <&mipi_csi_0_out>;
+                };
+            };
+
+            port@1 {
+                reg = <1>;
+
+                endpoint {
+                    remote-endpoint = <&isi_in_2>;
+                };
+            };
+        };
+    };
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v6 1/2] media: dt-bindings: Add CSI Pixel Formatter DT bindings

Quoted 5 lines · Guoniu Zhou

From: Guoniu Zhou <[email protected]>

The i.MX9 CSI pixel formatting module uses packet info, pixel and non-pixel data from the CSI-2 host controller and reformat them to match Pixel Link(PL) definition.

Sorry for chiming in very late, but can you please provide on which i.MX9 devices this formatting module is present? I've checked the i.MX93 reference manual and found no info instead I found a CAMERA_MUX register which does something similiar but is not the same. Please provide a more specific compatible if this IP is only be present on i.MX95 devices.

Regards,
Marco

Quoted 106 lines
---
 .../bindings/media/fsl,imx9-csi-formatter.yaml     | 87 ++++++++++++++++++++++
 1 file changed, 87 insertions(+)
Documentation/devicetree/bindings/media/fsl,imx9-csi-formatter.yaml new file +87 -0
new file mode 100644
index 000000000000..774d37d2b987
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/fsl,imx9-csi-formatter.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: i.MX9 CSI Pixel Formatter
+
+maintainers:
+  - Guoniu Zhou <[email protected]>
+
+description:
+  The CSI pixel formatting module uses packet info, pixel and non-pixel
+  data from the CSI-2 host controller and reformat them to match Pixel
+  Link(PL) definition.
+
+properties:
+  compatible:
+    const: fsl,imx9-csi-formatter
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  power-domains:
+    maxItems: 1
+
+  ports:
+    $ref: /schemas/graph.yaml#/properties/ports
+
+    properties:
+      port@0:
+        $ref: /schemas/graph.yaml#/$defs/port-base
+        unevaluatedProperties: false
+        description: MIPI CSI-2 RX IDI interface
+
+        properties:
+          endpoint:
+            $ref: video-interfaces.yaml#
+            unevaluatedProperties: false
+
+      port@1:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Pixel Link Interface
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - power-domains
+  - ports
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/nxp,imx95-clock.h>
+
+    formatter@20 {
+        compatible = "fsl,imx9-csi-formatter";
+        reg = <0x20 0x100>;
+        clocks = <&cameramix_csr IMX95_CLK_CAMBLK_CSI2_FOR0>;
+        power-domains = <&scmi_devpd 3>;
+
+        ports {
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            port@0 {
+                reg = <0>;
+
+                endpoint {
+                    remote-endpoint = <&mipi_csi_0_out>;
+                };
+            };
+
+            port@1 {
+                reg = <1>;
+
+                endpoint {
+                    remote-endpoint = <&isi_in_2>;
+                };
+            };
+        };
+    };
Signature · 2 lines
-- 
2.34.1
Signature · 7 lines
#gernperDu 
#CallMeByMyFirstName

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-9    |
#3 lore raw

RE: [PATCH v6 1/2] media: dt-bindings: Add CSI Pixel Formatter DT bindings

Hi Macro,

Thank you for the review and the valid point!

Quoted 27 lines

-----Original Message-----
From: Marco Felsch <[email protected]>
Sent: Monday, May 11, 2026 7:46 PM
To: G.N. Zhou (OSS) <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>; Rob Herring <[email protected]>; Krzysztof Kozlowski <[email protected]>; Conor Dooley <[email protected]>; Shawn Guo <[email protected]>; Sascha Hauer <[email protected]>; Pengutronix Kernel Team <[email protected]>; Fabio Estevam <[email protected]>; Laurent Pinchart <[email protected]>; Frank Li <[email protected]>; [email protected]; Krzysztof Kozlowski <[email protected]>; [email protected]; linux- [email protected]; G.N. Zhou <[email protected]>; linux-arm- [email protected]; [email protected]
Subject: Re: [PATCH v6 1/2] media: dt-bindings: Add CSI Pixel Formatter DT bindings

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 5 lines · Guoniu Zhou

From: Guoniu Zhou <[email protected]>

The i.MX9 CSI pixel formatting module uses packet info, pixel and non-pixel data from the CSI-2 host controller and reformat them to match Pixel Link(PL) definition.

Quoted 5 lines

Sorry for chiming in very late, but can you please provide on which i.MX9 devices this formatting module is present? I've checked the i.MX93 reference manual and found no info instead I found a CAMERA_MUX register which does something similiar but is not the same. Please provide a more specific compatible if this IP is only be present on i.MX95 devices.

You're right. I initially used "imx9" because this CSI formatter IP is present on both i.MX95 and i.MX952 (not on i.MX93 as you correctly noted). However, I agree this naming is confusing and could mislead people into thinking it's available across all i.MX9 series devices.

I'll change the compatible to "fsl,imx95-csi-formatter" as the current driver only supports i.MX95. When i.MX952 support is added in the future, we can extend the compatible string accordingly (e.g., using "fsl,imx952-csi-formatter" with "fsl,imx95-csi-formatter" as fallback if they're compatible).

Thanks again for catching this!

Best Regards
G.N Zhou

Quoted 125 lines

Regards,
Marco

Quoted 6 lines
Reviewed-by Krzysztof Kozlowski

<[email protected]>

---
 .../bindings/media/fsl,imx9-csi-formatter.yaml     | 87

++++++++++++++++++++++

Quoted 101 lines
 1 file changed, 87 insertions(+)

diff --git a/Documentation/devicetree/bindings/media/fsl,imx9-csi-formatter.yaml b/Documentation/devicetree/bindings/media/fsl,imx9-csi-formatter.yaml new file mode 100644
index 000000000000..774d37d2b987

Documentation/devicetree/bindings/media/fsl,imx9-csi-formatter.y new file +89 -1
+++ aml
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) %YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/fsl,imx9-csi-formatter.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: i.MX9 CSI Pixel Formatter
+
+maintainers:
+  - Guoniu Zhou <[email protected]>
+
+description:
+  The CSI pixel formatting module uses packet info, pixel and
+non-pixel
+  data from the CSI-2 host controller and reformat them to match
+Pixel
+  Link(PL) definition.
+
+properties:
+  compatible:
+    const: fsl,imx9-csi-formatter
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  power-domains:
+    maxItems: 1
+
+  ports:
+    $ref: /schemas/graph.yaml#/properties/ports
+
+    properties:
+      port@0:
+        $ref: /schemas/graph.yaml#/$defs/port-base
+        unevaluatedProperties: false
+        description: MIPI CSI-2 RX IDI interface
+
+        properties:
+          endpoint:
+            $ref: video-interfaces.yaml#
+            unevaluatedProperties: false
+
+      port@1:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Pixel Link Interface
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - power-domains
+  - ports
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/nxp,imx95-clock.h>
+
+    formatter@20 {
+        compatible = "fsl,imx9-csi-formatter";
+        reg = <0x20 0x100>;
+        clocks = <&cameramix_csr IMX95_CLK_CAMBLK_CSI2_FOR0>;
+        power-domains = <&scmi_devpd 3>;
+
+        ports {
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            port@0 {
+                reg = <0>;
+
+                endpoint {
+                    remote-endpoint = <&mipi_csi_0_out>;
+                };
+            };
+
+            port@1 {
+                reg = <1>;
+
+                endpoint {
+                    remote-endpoint = <&isi_in_2>;
+                };
+            };
+        };
+    };
 
--
Quoted 4 lines

2.34.1

--
#gernperDu
#CallMeByMyFirstName

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-9    |
#4 lore raw

PATCH v6 2/2 media: nxp: Add i.MX9 CSI pixel formatter v4l2 driver

In reply to Guoniu Zhou

From: Guoniu Zhou <[email protected]>

The CSI pixel formatter is a module found on i.MX95 used to reformat packet info, pixel and non-pixel data from CSI-2 host controller to match Pixel Link(PL) definition.

Add data formatting support.

---
Changes in v6:
- Remove unused header includes
- Unify macro naming: VCx/VCX -> VC and parameter x -> vc
- Remove unused format field from csi_formatter struct
- Use compact initialization for formats array
- Make find_csi_format() return NULL instead of default format
- Use unsigned int for array index in find_csi_format()
- Add err_ prefix to error handling labels
- Add v4l2_subdev_cleanup() and reorder cleanup sequence
- Update enable_streams debug output format
- Rename VC_MAX to VC_NUM and fix boundary check
- Update CSI formatter Kconfig description
- Use v4l2_subdev_get_frame_desc_passthrough() helper
- Fix error paths in async registration and probe
- Add mutex to protect enabled_streams
- Switch to devm_pm_runtime_enable()
- Remove redundant num_routes check in set_routing
- Optimize get_index_by_dt() and add warning for unsupported type
- csi_formatter_start/stop_stream: Process all streams in mask

---
 MAINTAINERS                                     |   8 +
 drivers/media/platform/nxp/Kconfig              |  14 +
 drivers/media/platform/nxp/Makefile             |   1 +
 drivers/media/platform/nxp/imx9-csi-formatter.c | 776 ++++++++++++++++++++++++
 4 files changed, 799 insertions(+)
MAINTAINERS +8 -0
index 81d53481d3f7..096361682b23 100644
@@ -19265,6 +19265,14 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/media/nxp,imx8-jpeg.yaml
 F:	drivers/media/platform/nxp/imx-jpeg
 
+NXP i.MX 9 CSI PIXEL FORMATTER V4L2 DRIVER
+M:	Guoniu Zhou <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/media/fsl,imx9-csi-formatter.yaml
+F:	drivers/media/platform/nxp/imx9-csi-formatter.c
+
 NXP i.MX CLOCK DRIVERS
 M:	Abel Vesa <[email protected]>
 R:	Peng Fan <[email protected]>
drivers/media/platform/nxp/Kconfig +14 -0
index 40e3436669e2..952deb3b81c1 100644
@@ -28,6 +28,20 @@ config VIDEO_IMX8MQ_MIPI_CSI2
 	  Video4Linux2 driver for the MIPI CSI-2 receiver found on the i.MX8MQ
 	  SoC.
 
+config VIDEO_IMX9_CSI_FORMATTER
+	tristate "NXP i.MX9 CSI Pixel Formatter driver"
+	depends on ARCH_MXC || COMPILE_TEST
+	depends on VIDEO_DEV
+	select MEDIA_CONTROLLER
+	select V4L2_FWNODE
+	select VIDEO_V4L2_SUBDEV_API
+	help
+	  This driver provides support for the CSI Pixel Formatter found on
+	  i.MX9 series SoCs. This module unpacks the pixels received from the
+	  CSI-2 interface and reformats them to meet pixel link requirements.
+
+	  Say Y here to enable CSI Pixel Formater module for i.MX9 SoC.
+
 config VIDEO_IMX_MIPI_CSIS
 	tristate "NXP MIPI CSI-2 CSIS receiver found on i.MX7 and i.MX8 models"
 	depends on ARCH_MXC || COMPILE_TEST
drivers/media/platform/nxp/Makefile +1 -0
index 4d90eb713652..39ba5660ba92 100644
@@ -6,6 +6,7 @@ obj-y += imx8-isi/
 
 obj-$(CONFIG_VIDEO_IMX7_CSI) += imx7-media-csi.o
 obj-$(CONFIG_VIDEO_IMX8MQ_MIPI_CSI2) += imx8mq-mipi-csi2.o
+obj-$(CONFIG_VIDEO_IMX9_CSI_FORMATTER) += imx9-csi-formatter.o
 obj-$(CONFIG_VIDEO_IMX_MIPI_CSIS) += imx-mipi-csis.o
 obj-$(CONFIG_VIDEO_IMX_PXP) += imx-pxp.o
 obj-$(CONFIG_VIDEO_MX2_EMMAPRP) += mx2_emmaprp.o
drivers/media/platform/nxp/imx9-csi-formatter.c new file +776 -0

776 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v6 2/2] media: nxp: Add i.MX9 CSI pixel formatter v4l2 driver

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [Critical] Missing .unbind callback in async notifier leaves a dangling pointer, causing a…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [Critical] Missing .unbind callback in async notifier leaves a dangling pointer, causing a Use-After-Free.
- [High] Missing NULL pointer check after calling v4l2_subdev_state_get_format() causes a kernel panic.
- [High] Missing NULL check for formatter->csi_sd in formatter_subdev_disable_streams() leads to kernel panic.
- [High] Global caching of V4L2 format breaks hardware configuration for multiplexed streams.
- [High] State desynchronization in formatter_subdev_disable_streams() leads to PM reference counter underflow.
- [High] Subdevice is registered asynchronously before runtime PM and drvdata are initialized.
- [High] Uninitialized stack variable source_fd in get_vc() passed to downstream subdevice, risking OOB stack read.
- [Medium] Mixing goto-based error handling with scope-based cleanup helpers (__free and guard).
- [Medium] Conflation of V4L2_FRAME_DESC_ENTRY_MAX with stream IDs silently drops configurations for streams >= 8. --

commit 0a5796f1d64d741325c60f3d9fb1c35507045c6e
Author: Guoniu Zhou <[email protected]>

media: nxp: Add i.MX9 CSI pixel formatter v4l2 driver

The CSI pixel formatter is a module found on i.MX95 used to reformat packet info, pixel and non-pixel data from CSI-2 host controller to match Pixel Link(PL) definition. Add data formatting support.

Quoted 6 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +0 -0
new file mode 100644
index 0000000000000..aca04de8e0ed8
@@ -0,0 +1,776 @@

[ ... ]

drivers/media/platform/nxp/imx9-csi-formatter.c line 230

Quoted 13 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +13 -0
+static int formatter_subdev_enum_mbus_code(struct v4l2_subdev *sd,
+					   struct v4l2_subdev_state *sd_state,
+					   struct v4l2_subdev_mbus_code_enum *code)
+{
+	if (code->pad == CSI_FORMATTER_PAD_SOURCE) {
+		struct v4l2_mbus_framefmt *fmt;
+
+		if (code->index > 0)
+			return -EINVAL;
+
+		fmt = v4l2_subdev_state_get_format(sd_state, code->pad,
+						   code->stream);
+		code->code = fmt->code;

v4l2_subdev_state_get_format() can return NULL if the requested stream ID is not in the routing table. Can this cause a NULL pointer dereference when accessing fmt->code?

[ ... ]

Quoted 7 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +7 -0
+static int formatter_subdev_set_fmt(struct v4l2_subdev *sd,
+				    struct v4l2_subdev_state *sd_state,
+				    struct v4l2_subdev_format *sdformat)
+{
+	struct csi_formatter *formatter = sd_to_formatter(sd);
+	struct formatter_pix_format const *format;
+	struct v4l2_mbus_framefmt *fmt;

[ ... ]

drivers/media/platform/nxp/imx9-csi-formatter.c line 269

Quoted 3 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +3 -0
+	fmt = v4l2_subdev_state_get_format(sd_state, sdformat->pad,
+					   sdformat->stream);
+	*fmt = sdformat->format;

Is it possible for v4l2_subdev_state_get_format() to return NULL here as well if userspace provides an invalid stream ID?

Quoted 8 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +8 -0
+
+	/* Set default code if user set an invalid value */
+	fmt->code = format->code;
+
+	/* Propagate the format from sink stream to source stream */
+	fmt = v4l2_subdev_state_get_opposite_stream_format(sd_state, sdformat->pad,
+							   sdformat->stream);
+	if (!fmt)

drivers/media/platform/nxp/imx9-csi-formatter.c line 287

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+		return -EINVAL;
+
+	*fmt = sdformat->format;
+
+	/* Store the CSIS format descriptor for active formats. */
+	if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
+		formatter->fmt = format;
+
+	return 0;
+}

Does storing a single format globally in formatter->fmt break hardware configuration when multiplexing multiple streams?

If userspace configures different formats for different streams, formatter->fmt retains only the last configured format. Later, csi_formatter_start_stream() applies formatter->fmt->data_type identically to all enabled streams.

[ ... ]

Quoted 8 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +8 -0
+static int get_vc(struct csi_formatter *formatter, unsigned int stream)
+{
+	struct v4l2_mbus_frame_desc source_fd;
+	struct v4l2_mbus_frame_desc_entry *entry = NULL;
+	unsigned int i;
+	int vc;
+	int ret;
+

drivers/media/platform/nxp/imx9-csi-formatter.c line 342

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+	/*
+	 * Return virtual channel 0 as default value when remote subdev
+	 * don't implement .get_frame_desc subdev callback
+	 */
+	ret = v4l2_subdev_call(formatter->csi_sd, pad, get_frame_desc,
+			       formatter->remote_pad, &source_fd);
+	if (ret < 0)
+		return (ret == -ENOIOCTLCMD) ? 0 : ret;
+
+	for (i = 0; i < source_fd.num_entries; ++i) {

Should source_fd be zero-initialized before passing it to the downstream subdevice?

If the remote driver succeeds but fails to populate the struct completely, could source_fd.num_entries contain uninitialized stack data, potentially causing an out-of-bounds read in this loop?

[ ... ]

drivers/media/platform/nxp/imx9-csi-formatter.c line 374

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+static void csi_formatter_stop_stream(struct csi_formatter *formatter,
+				     u64 stream_mask)
+{
+	unsigned int i;
+	int ret;
+	int vc;
+
+	for (i = 0; i < V4L2_FRAME_DESC_ENTRY_MAX; ++i) {
+		if (!(stream_mask & BIT(i)))
+			continue;

Is V4L2_FRAME_DESC_ENTRY_MAX (which is 8) the correct upper bound when iterating through a 64-bit stream_mask?

If userspace enables a stream with an ID of 8 or higher, will this loop silently skip it?

[ ... ]

drivers/media/platform/nxp/imx9-csi-formatter.c line 399

Quoted 11 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +11 -0
+static int csi_formatter_start_stream(struct csi_formatter *formatter,
+				      u64 stream_mask)
+{
+	const struct formatter_pix_format *fmt = formatter->fmt;
+	u64 configured_streams = 0;
+	unsigned int i;
+	u32 val;
+	int ret;
+	int vc;
+
+	for (i = 0; i < V4L2_FRAME_DESC_ENTRY_MAX; ++i) {

Similarly, does this loop also drop configurations for stream IDs >= 8?

[ ... ]

Quoted 8 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +8 -0
+static int formatter_subdev_enable_streams(struct v4l2_subdev *sd,
+					   struct v4l2_subdev_state *state,
+					   u32 pad, u64 streams_mask)
+{
+	struct csi_formatter *formatter = sd_to_formatter(sd);
+	struct device *dev = formatter->dev;
+	u64 sink_streams;
+	int ret;

[ ... ]

Quoted 26 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +26 -0
+	if (!formatter->csi_sd) {
+		dev_err(dev, "CSI controller not linked with formatter\n");
+		return -EPIPE;
+	}
+
+	guard(mutex)(&formatter->lock);
+
+	if (!formatter->enabled_streams) {
+		ret = pm_runtime_resume_and_get(formatter->dev);
+		if (ret < 0) {
+			dev_err(dev, "Failed to resume runtime PM: %d\n", ret);
+			return ret;
+		}
+	}
+
+	ret = csi_formatter_start_stream(formatter, streams_mask);
+	if (ret)
+		goto err_runtime_put;
+
+	ret = v4l2_subdev_enable_streams(formatter->csi_sd,
+					 formatter->remote_pad,
+					 sink_streams);
+	if (ret)
+		goto err_stop_stream;
+
+	formatter->enabled_streams |= streams_mask;

drivers/media/platform/nxp/imx9-csi-formatter.c line 476

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+
+	return 0;
+
+err_stop_stream:
+	csi_formatter_stop_stream(formatter, streams_mask);
+err_runtime_put:
+	if (!formatter->enabled_streams)
+		pm_runtime_put(formatter->dev);
+	return ret;
+}

Does this function mix scope-based cleanup (guard()) with goto error labels?

According to the cleanup subsystem guidelines, mixing goto-based error handling with scope variables in the same function can create confusing ownership semantics and is typically avoided.

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+
+static int formatter_subdev_disable_streams(struct v4l2_subdev *sd,
+					    struct v4l2_subdev_state *state,
+					    u32 pad, u64 streams_mask)
+{
+	struct csi_formatter *formatter = sd_to_formatter(sd);
+	u64 sink_streams;
+	int ret;
+
+	sink_streams = v4l2_subdev_state_xlate_streams(state,

drivers/media/platform/nxp/imx9-csi-formatter.c line 496

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+						       CSI_FORMATTER_PAD_SOURCE,
+						       CSI_FORMATTER_PAD_SINK,
+						       &streams_mask);
+	if (!sink_streams || !streams_mask)
+		return -EINVAL;
+
+	guard(mutex)(&formatter->lock);
+
+	ret = v4l2_subdev_disable_streams(formatter->csi_sd, formatter->remote_pad,
+					  sink_streams);

While formatter_subdev_enable_streams() explicitly checks if (!formatter->csi_sd) before proceeding, this function does not.

If the pipeline is unbound, could formatter->csi_sd be NULL and lead to a panic when v4l2_subdev_disable_streams() dereferences it?

drivers/media/platform/nxp/imx9-csi-formatter.c line 508

Quoted 12 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +12 -0
+	if (ret)
+		dev_err(formatter->dev, "Failed to disable streams: %d\n", ret);
+
+	csi_formatter_stop_stream(formatter, streams_mask);
+
+	formatter->enabled_streams &= ~streams_mask;
+
+	if (!formatter->enabled_streams)
+		pm_runtime_put(formatter->dev);
+
+	return ret;
+}

If v4l2_subdev_disable_streams() returns an error, the driver still clears formatter->enabled_streams and calls pm_runtime_put().

The V4L2 core will see the returned error and keep the streams active in its state. If userspace then retries disabling the streams, this function could be called again, redundantly clearing the state and calling pm_runtime_put().

Could this cause a PM reference counter underflow?

[ ... ]

Quoted 23 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +23 -0
+static int csi_formatter_notify_bound(struct v4l2_async_notifier *notifier,
+				      struct v4l2_subdev *sd,
+				      struct v4l2_async_connection *asc)
+{
+	const unsigned int link_flags = MEDIA_LNK_FL_IMMUTABLE
+				      | MEDIA_LNK_FL_ENABLED;
+	struct csi_formatter *formatter = notifier_to_formatter(notifier);
+	struct v4l2_subdev *sdev = &formatter->sd;
+	struct media_pad *sink = &sdev->entity.pads[CSI_FORMATTER_PAD_SINK];
+	struct media_pad *remote_pad;
+	int ret;
+
+	formatter->csi_sd = sd;
+
+	dev_dbg(formatter->dev, "Bound subdev: %s pad\n", sd->name);
+
+	ret = v4l2_create_fwnode_links_to_pad(sd, sink, link_flags);
+	if (ret < 0)
+		return ret;
+
+	remote_pad = media_pad_remote_pad_first(sink);
+	if (!remote_pad) {
+		dev_err(formatter->dev, "Pipe not setup correctly\n");

drivers/media/platform/nxp/imx9-csi-formatter.c line 609

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+		return -EPIPE;
+	}
+	formatter->remote_pad = remote_pad->index;
+
+	return 0;
+}
+
+static const struct v4l2_async_notifier_operations formatter_notify_ops = {
+	.bound = csi_formatter_notify_bound,
+};

The driver saves the sd pointer to formatter->csi_sd, but formatter_notify_ops does not implement an .unbind callback to clear it.

If the remote subdevice is unloaded, will formatter->csi_sd become a dangling pointer and risk a use-after-free during later operations?

Quoted 30 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +30 -0
+
+static int csi_formatter_async_register(struct csi_formatter *formatter)
+{
+	struct device *dev = formatter->dev;
+	struct v4l2_async_connection *asc;
+	int ret;
+
+	struct fwnode_handle *ep __free(fwnode_handle) =
+		fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 0,
+						FWNODE_GRAPH_ENDPOINT_NEXT);
+	if (!ep)
+		return -ENOTCONN;
+
+	v4l2_async_subdev_nf_init(&formatter->notifier, &formatter->sd);
+
+	asc = v4l2_async_nf_add_fwnode_remote(&formatter->notifier, ep,
+					      struct v4l2_async_connection);
+	if (IS_ERR(asc)) {
+		ret = PTR_ERR(asc);
+		goto err_cleanup_notifier;
+	}
+
+	formatter->notifier.ops = &formatter_notify_ops;
+
+	ret = v4l2_async_nf_register(&formatter->notifier);
+	if (ret)
+		goto err_cleanup_notifier;
+
+	ret = v4l2_async_register_subdev(&formatter->sd);
+	if (ret)

drivers/media/platform/nxp/imx9-csi-formatter.c line 649

Quoted 10 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +10 -0
+		goto err_unregister_notifier;
+
+	return 0;
+
+err_unregister_notifier:
+	v4l2_async_nf_unregister(&formatter->notifier);
+err_cleanup_notifier:
+	v4l2_async_nf_cleanup(&formatter->notifier);
+	return ret;
+}

Does this function also mix goto-based error handling with a scope-based cleanup helper (__free(fwnode_handle))?

[ ... ]

Quoted 2 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +2 -0
+static int csi_formatter_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/media/platform/nxp/imx9-csi-formatter.c line 735

Quoted 12 lines
drivers/media/platform/nxp/imx9-csi-formatter.c new file +12 -0
+	ret = csi_formatter_async_register(formatter);
+	if (ret < 0)
+		goto err_cleanup_subdev;
+
+	platform_set_drvdata(pdev, &formatter->sd);
+
+	/* Enable runtime PM. */
+	ret = devm_pm_runtime_enable(dev);
+	if (ret)
+		goto err_async_unregister;
+
+	return 0;

The driver registers the async subdevice before calling platform_set_drvdata() and devm_pm_runtime_enable().

If userspace opens the node immediately and issues an IOCTL, could it fail to acquire PM (since it's disabled) or hit a NULL drvdata pointer if the device attempts to suspend/resume? Usually, asynchronous registration is the final step in probe().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2