Skip to contentSkip to conversation

media: i2c: Add support for alvium camera

PATCH Superseded Tommaso Merciai 4 participants 19 messages Jun 8, 2023 – Jun 20, 2023

Subsystems (3)

v5 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Tommaso Merciai

author maintainer
#0 lore raw

v5 cover media: i2c: Add support for alvium camera

Hello All,

This series add support for Allied Vision Alvium camera. The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Working on top of Ideas on Board (branch: ideasonboard/v6.2/isi) I'm able to test the driver on imx8mp-evk.
I collect also some patches to enable HDMI on imx8mp-evk from Pengutronix (branch: pengutronix-imx8mp-hdmi)

I collect the patchset required to enable ISI + HDMI on imx8mp-evk into the following branch from Avnet Silica Software & Services EMEA [1].

Some documentation on testing ISP and ISI of imx8mp-evk here [2].

Thanks all for the great work!

Tommaso Merciai (3):
dt-bindings: vendor-prefixes: Add prefix alliedvision
media: dt-bindings: alvium: add document YAML binding
media: i2c: Add support for alvium camera

 .../media/i2c/alliedvision,alvium-csi2.yaml   |   97 +
 .../devicetree/bindings/vendor-prefixes.yaml  |    2 +
 drivers/media/i2c/Kconfig                     |   10 +
 drivers/media/i2c/Makefile                    |    1 +
 drivers/media/i2c/alvium-csi2.c               | 3479 +++++++++++++++++
 drivers/media/i2c/alvium-csi2.h               |  485 +++
 6 files changed, 4074 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v5 1/3 dt-bindings: vendor-prefixes: Add prefix alliedvision

Add a vendor prefix entry for Allied Vision Inc.
(https://www.alliedvision.com)

---
Changes since v3:
- Collected tags from LPinchart, CDooley, KKozlowski

 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 82d39ab0231b..7bd678956b3d 100644
@@ -77,6 +77,8 @@ patternProperties:
     description: ALFA Network Inc.
   "^allegro,.*":
     description: Allegro DVT
+  "^alliedvision,.*":
+    description: Allied Vision Inc.
   "^allo,.*":
     description: Allo.com
   "^allwinner,.*":
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v5 2/3 media: dt-bindings: alvium: add document YAML binding

In reply to Tommaso Merciai

Add documentation of device tree in YAML schema for the ALVIUM Camera from Allied Vision Inc.

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v1:
- Fixed build error as suggested by RHerring bot

Changes since v2:
- Fixed License as suggested by KKozlowski/CDooley
- Removed rotation property as suggested by CDooley/LPinchart
- Fixed example node name as suggested by CDooley
- Fixed title as suggested by LPinchart
- Fixed compatible name as suggested by LPinchart
- Removed clock as suggested by LPinchart
- Removed gpios not as suggested by LPinchart
- Renamed property name streamon-delay into alliedvision,lp2hs-delay-us
- Fixed vendor prefix, unit append as suggested by KKozlowski
- Fixed data-lanes
- Fixed blank space + example indentation (from 6 -> 4 space) as suggested by KKozlowski
- Dropped status into example as suggested by KKozlowski
- Added vcc-ext-in supply as suggested by LPinchart
- Dropped pinctrl into example as suggested by LPinchart

Changes since v3:
- Fixed vcc-ext-in-supply description as suggested by LPinchart
- Fixed alliedvision,lp2hs-delay-us description as suggested by LPinchart
- Added maximum to alliedvision,lp2hs-delay-us as suggested by LPinchart
- Collected Reviewed-by tag from LPinchart

Changes since v4:
- Fixed id as reported by RHerring bot and LPinchart
- Add minimum into alliedvision,lp2hs-delay-us as suggested by CDooley
- Tested using make dt_binding_check DT_SCHEMA_FILES=alliedvision,alvium-csi2.yaml

 .../media/i2c/alliedvision,alvium-csi2.yaml   | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +97 -0
new file mode 100644
index 000000000000..c771e5039641
@@ -0,0 +1,97 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/alliedvision,alvium-csi2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Allied Vision Alvium Camera
+
+maintainers:
+  - Tommaso Merciai <[email protected]>
+  - Martin Hecht <[email protected]>
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: alliedvision,alvium-csi2
+
+  reg:
+    maxItems: 1
+
+  vcc-ext-in-supply:
+    description: |
+      The regulator that supplies power to the VCC_EXT_IN pins.
+
+  alliedvision,lp2hs-delay-us:
+    minimum: 1
+    maximum: 150000
+    description: |
+      Low power to high speed delay time.
+
+      If the value is larger than 0, the camera forces a reset of all
+      D-PHY lanes for the duration specified by this property. All lanes
+      will transition to the low-power state and back to the high-speed
+      state after the delay. Otherwise the lanes will transition to and
+      remain in the high-speed state immediately after power on.
+
+      This is meant to help CSI-2 receivers synchronizing their D-PHY
+      RX.
+
+  port:
+    description: Digital Output Port
+    $ref: /schemas/graph.yaml#/$defs/port-base
+    additionalProperties: false
+
+    properties:
+      endpoint:
+        $ref: /schemas/media/video-interfaces.yaml#
+        unevaluatedProperties: false
+
+        properties:
+          link-frequencies: true
+
+          data-lanes:
+            minItems: 1
+            items:
+              - const: 1
+              - const: 2
+              - const: 3
+              - const: 4
+
+        required:
+          - data-lanes
+          - link-frequencies
+
+required:
+  - compatible
+  - reg
+  - vcc-ext-in-supply
+  - port
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        alvium: camera@3c {
+            compatible = "alliedvision,alvium-csi2";
+            reg = <0x3c>;
+            vcc-ext-in-supply = <&reg_vcc_ext_in>;
+            alliedvision,lp2hs-delay-us = <20>;
+
+            port {
+                alvium_out: endpoint {
+                    remote-endpoint = <&mipi_csi_0_in>;
+                    data-lanes = <1 2 3 4>;
+                    link-frequencies = /bits/ 64 <681250000>;
+                };
+            };
+        };
+    };
+
+...
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v5 2/3] media: dt-bindings: alvium: add document YAML binding

Hi Tommaso,

Quoted 79 lines · Tommaso Merciai

Add documentation of device tree in YAML schema for the ALVIUM Camera from Allied Vision Inc.

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v1:
- Fixed build error as suggested by RHerring bot

Changes since v2:
- Fixed License as suggested by KKozlowski/CDooley
- Removed rotation property as suggested by CDooley/LPinchart
- Fixed example node name as suggested by CDooley
- Fixed title as suggested by LPinchart
- Fixed compatible name as suggested by LPinchart
- Removed clock as suggested by LPinchart
- Removed gpios not as suggested by LPinchart
- Renamed property name streamon-delay into alliedvision,lp2hs-delay-us
- Fixed vendor prefix, unit append as suggested by KKozlowski
- Fixed data-lanes
- Fixed blank space + example indentation (from 6 -> 4 space) as suggested by KKozlowski
- Dropped status into example as suggested by KKozlowski
- Added vcc-ext-in supply as suggested by LPinchart
- Dropped pinctrl into example as suggested by LPinchart

Changes since v3:
- Fixed vcc-ext-in-supply description as suggested by LPinchart
- Fixed alliedvision,lp2hs-delay-us description as suggested by LPinchart
- Added maximum to alliedvision,lp2hs-delay-us as suggested by LPinchart
- Collected Reviewed-by tag from LPinchart

Changes since v4:
- Fixed id as reported by RHerring bot and LPinchart
- Add minimum into alliedvision,lp2hs-delay-us as suggested by CDooley
- Tested using make dt_binding_check DT_SCHEMA_FILES=alliedvision,alvium-csi2.yaml

 .../media/i2c/alliedvision,alvium-csi2.yaml   | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +30 -0
new file mode 100644
index 000000000000..c771e5039641
@@ -0,0 +1,97 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/alliedvision,alvium-csi2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Allied Vision Alvium Camera
+
+maintainers:
+  - Tommaso Merciai <[email protected]>
+  - Martin Hecht <[email protected]>
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: alliedvision,alvium-csi2
+
+  reg:
+    maxItems: 1
+
+  vcc-ext-in-supply:
+    description: |
+      The regulator that supplies power to the VCC_EXT_IN pins.
+
+  alliedvision,lp2hs-delay-us:
+    minimum: 1
+    maximum: 150000
+    description: |

Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml line 40

Quoted 10 lines
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +10 -0
+      Low power to high speed delay time.
+
+      If the value is larger than 0, the camera forces a reset of all
+      D-PHY lanes for the duration specified by this property. All lanes
+      will transition to the low-power state and back to the high-speed
+      state after the delay. Otherwise the lanes will transition to and
+      remain in the high-speed state immediately after power on.
+
+      This is meant to help CSI-2 receivers synchronizing their D-PHY
+      RX.

Why do you need this? D-PHY TX is obviously controlled by the driver. Explicit control of PHY initialisation is of course best option when combined with some receivers, done via driver's pre_streamon and s_stream callbacks. Can't your hardware do that?

Quoted 57 lines
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +57 -0
+
+  port:
+    description: Digital Output Port
+    $ref: /schemas/graph.yaml#/$defs/port-base
+    additionalProperties: false
+
+    properties:
+      endpoint:
+        $ref: /schemas/media/video-interfaces.yaml#
+        unevaluatedProperties: false
+
+        properties:
+          link-frequencies: true
+
+          data-lanes:
+            minItems: 1
+            items:
+              - const: 1
+              - const: 2
+              - const: 3
+              - const: 4
+
+        required:
+          - data-lanes
+          - link-frequencies
+
+required:
+  - compatible
+  - reg
+  - vcc-ext-in-supply
+  - port
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        alvium: camera@3c {
+            compatible = "alliedvision,alvium-csi2";
+            reg = <0x3c>;
+            vcc-ext-in-supply = <&reg_vcc_ext_in>;
+            alliedvision,lp2hs-delay-us = <20>;
+
+            port {
+                alvium_out: endpoint {
+                    remote-endpoint = <&mipi_csi_0_in>;
+                    data-lanes = <1 2 3 4>;
+                    link-frequencies = /bits/ 64 <681250000>;
+                };
+            };
+        };
+    };
+
+...
Signature · 3 lines
Regards,

Sakari Ailus
author maintainer
#4 lore raw

Re: [PATCH v5 2/3] media: dt-bindings: alvium: add document YAML binding

Hi Sakari,
Thanks for your feedback.

Quoted 89 lines · Sakari Ailus

Hi Tommaso,

Quoted 86 lines · Tommaso Merciai

Add documentation of device tree in YAML schema for the ALVIUM Camera from Allied Vision Inc.

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v1:
- Fixed build error as suggested by RHerring bot

Changes since v2:
- Fixed License as suggested by KKozlowski/CDooley
- Removed rotation property as suggested by CDooley/LPinchart
- Fixed example node name as suggested by CDooley
- Fixed title as suggested by LPinchart
- Fixed compatible name as suggested by LPinchart
- Removed clock as suggested by LPinchart
- Removed gpios not as suggested by LPinchart
- Renamed property name streamon-delay into alliedvision,lp2hs-delay-us
- Fixed vendor prefix, unit append as suggested by KKozlowski
- Fixed data-lanes
- Fixed blank space + example indentation (from 6 -> 4 space) as suggested by KKozlowski
- Dropped status into example as suggested by KKozlowski
- Added vcc-ext-in supply as suggested by LPinchart
- Dropped pinctrl into example as suggested by LPinchart

Changes since v3:
- Fixed vcc-ext-in-supply description as suggested by LPinchart
- Fixed alliedvision,lp2hs-delay-us description as suggested by LPinchart
- Added maximum to alliedvision,lp2hs-delay-us as suggested by LPinchart
- Collected Reviewed-by tag from LPinchart

Changes since v4:
- Fixed id as reported by RHerring bot and LPinchart
- Add minimum into alliedvision,lp2hs-delay-us as suggested by CDooley
- Tested using make dt_binding_check DT_SCHEMA_FILES=alliedvision,alvium-csi2.yaml

 .../media/i2c/alliedvision,alvium-csi2.yaml   | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +37 -0
new file mode 100644
index 000000000000..c771e5039641
@@ -0,0 +1,97 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/alliedvision,alvium-csi2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Allied Vision Alvium Camera
+
+maintainers:
+  - Tommaso Merciai <[email protected]>
+  - Martin Hecht <[email protected]>
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: alliedvision,alvium-csi2
+
+  reg:
+    maxItems: 1
+
+  vcc-ext-in-supply:
+    description: |
+      The regulator that supplies power to the VCC_EXT_IN pins.
+
+  alliedvision,lp2hs-delay-us:
+    minimum: 1
+    maximum: 150000
+    description: |
+      Low power to high speed delay time.
+
+      If the value is larger than 0, the camera forces a reset of all
+      D-PHY lanes for the duration specified by this property. All lanes
+      will transition to the low-power state and back to the high-speed
+      state after the delay. Otherwise the lanes will transition to and
+      remain in the high-speed state immediately after power on.
Quoted 8 lines

Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml line 40

Quoted 3 lines
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +3 -0
+
+      This is meant to help CSI-2 receivers synchronizing their D-PHY
+      RX.

Why do you need this? D-PHY TX is obviously controlled by the driver. Explicit control of PHY initialisation is of course best option when combined with some receivers, done via driver's pre_streamon and s_stream callbacks. Can't your hardware do that?

You mean calculate this time in some dynamical way and program this during pre_streamon or s_stream?

If yes, unfortunately hw can't do this.

Regards,
Tommaso

Quoted 63 lines
Quoted 58 lines
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +57 -0
+
+  port:
+    description: Digital Output Port
+    $ref: /schemas/graph.yaml#/$defs/port-base
+    additionalProperties: false
+
+    properties:
+      endpoint:
+        $ref: /schemas/media/video-interfaces.yaml#
+        unevaluatedProperties: false
+
+        properties:
+          link-frequencies: true
+
+          data-lanes:
+            minItems: 1
+            items:
+              - const: 1
+              - const: 2
+              - const: 3
+              - const: 4
+
+        required:
+          - data-lanes
+          - link-frequencies
+
+required:
+  - compatible
+  - reg
+  - vcc-ext-in-supply
+  - port
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        alvium: camera@3c {
+            compatible = "alliedvision,alvium-csi2";
+            reg = <0x3c>;
+            vcc-ext-in-supply = <&reg_vcc_ext_in>;
+            alliedvision,lp2hs-delay-us = <20>;
+
+            port {
+                alvium_out: endpoint {
+                    remote-endpoint = <&mipi_csi_0_in>;
+                    data-lanes = <1 2 3 4>;
+                    link-frequencies = /bits/ 64 <681250000>;
+                };
+            };
+        };
+    };
+
+...
Signature · 3 lines
Regards,

Sakari Ailus
#5 lore raw

Re: [PATCH v5 2/3] media: dt-bindings: alvium: add document YAML binding

Hi Tommaso,

Quoted 102 lines · Tommaso Merciai

Hi Sakari,
Thanks for your feedback.

Quoted 89 lines · Sakari Ailus

Hi Tommaso,

Quoted 86 lines · Tommaso Merciai

Add documentation of device tree in YAML schema for the ALVIUM Camera from Allied Vision Inc.

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v1:
- Fixed build error as suggested by RHerring bot

Changes since v2:
- Fixed License as suggested by KKozlowski/CDooley
- Removed rotation property as suggested by CDooley/LPinchart
- Fixed example node name as suggested by CDooley
- Fixed title as suggested by LPinchart
- Fixed compatible name as suggested by LPinchart
- Removed clock as suggested by LPinchart
- Removed gpios not as suggested by LPinchart
- Renamed property name streamon-delay into alliedvision,lp2hs-delay-us
- Fixed vendor prefix, unit append as suggested by KKozlowski
- Fixed data-lanes
- Fixed blank space + example indentation (from 6 -> 4 space) as suggested by KKozlowski
- Dropped status into example as suggested by KKozlowski
- Added vcc-ext-in supply as suggested by LPinchart
- Dropped pinctrl into example as suggested by LPinchart

Changes since v3:
- Fixed vcc-ext-in-supply description as suggested by LPinchart
- Fixed alliedvision,lp2hs-delay-us description as suggested by LPinchart
- Added maximum to alliedvision,lp2hs-delay-us as suggested by LPinchart
- Collected Reviewed-by tag from LPinchart

Changes since v4:
- Fixed id as reported by RHerring bot and LPinchart
- Add minimum into alliedvision,lp2hs-delay-us as suggested by CDooley
- Tested using make dt_binding_check DT_SCHEMA_FILES=alliedvision,alvium-csi2.yaml

 .../media/i2c/alliedvision,alvium-csi2.yaml   | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +37 -0
new file mode 100644
index 000000000000..c771e5039641
@@ -0,0 +1,97 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/alliedvision,alvium-csi2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Allied Vision Alvium Camera
+
+maintainers:
+  - Tommaso Merciai <[email protected]>
+  - Martin Hecht <[email protected]>
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: alliedvision,alvium-csi2
+
+  reg:
+    maxItems: 1
+
+  vcc-ext-in-supply:
+    description: |
+      The regulator that supplies power to the VCC_EXT_IN pins.
+
+  alliedvision,lp2hs-delay-us:
+    minimum: 1
+    maximum: 150000
+    description: |
+      Low power to high speed delay time.
+
+      If the value is larger than 0, the camera forces a reset of all
+      D-PHY lanes for the duration specified by this property. All lanes
+      will transition to the low-power state and back to the high-speed
+      state after the delay. Otherwise the lanes will transition to and
+      remain in the high-speed state immediately after power on.
Quoted 8 lines

Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml line 40

Quoted 3 lines
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +3 -0
+
+      This is meant to help CSI-2 receivers synchronizing their D-PHY
+      RX.

Why do you need this? D-PHY TX is obviously controlled by the driver. Explicit control of PHY initialisation is of course best option when combined with some receivers, done via driver's pre_streamon and s_stream callbacks. Can't your hardware do that?

Quoted 4 lines

You mean calculate this time in some dynamical way and program this during pre_streamon or s_stream?

If yes, unfortunately hw can't do this.

This shouldn't be based on timing but rather progress of the bus initialisation sequence. That's how it works on most sensors nowadays.

The description is also lacking details on what delay this exactly is. What is the bus state during that delay?

Signature · 3 lines
Regards,

Sakari Ailus
author maintainer
#6 lore raw

Re: [PATCH v5 2/3] media: dt-bindings: alvium: add document YAML binding

Hi Sakari,

Quoted 105 lines · Sakari Ailus

Hi Tommaso,

Quoted 102 lines · Tommaso Merciai

Hi Sakari,
Thanks for your feedback.

Quoted 97 lines · Sakari Ailus

Hi Tommaso,

Quoted 79 lines · Tommaso Merciai

Add documentation of device tree in YAML schema for the ALVIUM Camera from Allied Vision Inc.

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v1:
- Fixed build error as suggested by RHerring bot

Changes since v2:
- Fixed License as suggested by KKozlowski/CDooley
- Removed rotation property as suggested by CDooley/LPinchart
- Fixed example node name as suggested by CDooley
- Fixed title as suggested by LPinchart
- Fixed compatible name as suggested by LPinchart
- Removed clock as suggested by LPinchart
- Removed gpios not as suggested by LPinchart
- Renamed property name streamon-delay into alliedvision,lp2hs-delay-us
- Fixed vendor prefix, unit append as suggested by KKozlowski
- Fixed data-lanes
- Fixed blank space + example indentation (from 6 -> 4 space) as suggested by KKozlowski
- Dropped status into example as suggested by KKozlowski
- Added vcc-ext-in supply as suggested by LPinchart
- Dropped pinctrl into example as suggested by LPinchart

Changes since v3:
- Fixed vcc-ext-in-supply description as suggested by LPinchart
- Fixed alliedvision,lp2hs-delay-us description as suggested by LPinchart
- Added maximum to alliedvision,lp2hs-delay-us as suggested by LPinchart
- Collected Reviewed-by tag from LPinchart

Changes since v4:
- Fixed id as reported by RHerring bot and LPinchart
- Add minimum into alliedvision,lp2hs-delay-us as suggested by CDooley
- Tested using make dt_binding_check DT_SCHEMA_FILES=alliedvision,alvium-csi2.yaml

 .../media/i2c/alliedvision,alvium-csi2.yaml   | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +30 -0
new file mode 100644
index 000000000000..c771e5039641
@@ -0,0 +1,97 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/alliedvision,alvium-csi2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Allied Vision Alvium Camera
+
+maintainers:
+  - Tommaso Merciai <[email protected]>
+  - Martin Hecht <[email protected]>
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: alliedvision,alvium-csi2
+
+  reg:
+    maxItems: 1
+
+  vcc-ext-in-supply:
+    description: |
+      The regulator that supplies power to the VCC_EXT_IN pins.
+
+  alliedvision,lp2hs-delay-us:
+    minimum: 1
+    maximum: 150000
+    description: |

Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml line 40

Quoted 10 lines
Documentation/devicetree/bindings/media/i2c/alliedvision,alvium-csi2.yaml new file +10 -0
+      Low power to high speed delay time.
+
+      If the value is larger than 0, the camera forces a reset of all
+      D-PHY lanes for the duration specified by this property. All lanes
+      will transition to the low-power state and back to the high-speed
+      state after the delay. Otherwise the lanes will transition to and
+      remain in the high-speed state immediately after power on.
+
+      This is meant to help CSI-2 receivers synchronizing their D-PHY
+      RX.

Why do you need this? D-PHY TX is obviously controlled by the driver. Explicit control of PHY initialisation is of course best option when combined with some receivers, done via driver's pre_streamon and s_stream callbacks. Can't your hardware do that?

Quoted 7 lines
Quoted 4 lines

You mean calculate this time in some dynamical way and program this during pre_streamon or s_stream?

If yes, unfortunately hw can't do this.

This shouldn't be based on timing but rather progress of the bus initialisation sequence. That's how it works on most sensors nowadays.

Just to clarify, this is not a sensor:

"The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2."

Quoted 3 lines

The description is also lacking details on what delay this exactly is. What is the bus state during that delay?

Previously we have a brief thread with Laurent about the behaviour of this reg/property, I repost here:

"Thanks to Martin Hecht I'm able to provide you a clearer description:

Alvium normally delivers a continous clock even when it is not streaming. The purpose of this reg is force a DPhy reset for the period described by the us on the reg before it starts streaming. To be clear, with that value bigger than 0 the Alvium forces a dphy-reset on all lanes for that period. That means all lanes go up into low power state.
This may help a csi2 rx ip to reset if that IP can't deal with a continous clock."

Hope these infos can clarify.
Let me know.

Thanks,
Tommaso

Quoted 5 lines
Signature · 4 lines
-- 
Regards,

Sakari Ailus
author maintainer
#7 lore raw

PATCH v5 3/3 media: i2c: Add support for alvium camera

In reply to Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h
drivers/media/i2c/Kconfig +10 -0
index 256d55bb2b1d..a9fdf10ffc15 100644
@@ -34,6 +34,16 @@ config VIDEO_APTINA_PLL
 config VIDEO_CCS_PLL
 	tristate
 
+config VIDEO_ALVIUM_CSI2
+	tristate "Allied Vision ALVIUM MIPI CSI-2 camera support"
+	depends on I2C
+	select MEDIA_CONTROLLER
+	select VIDEO_V4L2_SUBDEV_API
+	select V4L2_FWNODE
+	help
+	  This is a Video4Linux2 sensor driver for the Allied Vision
+	  ALVIUM camera connected via MIPI CSI-2 interface.
+
 config VIDEO_AR0521
 	tristate "ON Semiconductor AR0521 sensor support"
 	depends on I2C && VIDEO_DEV
drivers/media/i2c/Makefile +1 -0
index b44dacf935f4..be4dddd6fd98 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_VIDEO_ADV7604) += adv7604.o
 obj-$(CONFIG_VIDEO_ADV7842) += adv7842.o
 obj-$(CONFIG_VIDEO_AK7375) += ak7375.o
 obj-$(CONFIG_VIDEO_AK881X) += ak881x.o
+obj-$(CONFIG_VIDEO_ALVIUM_CSI2) += alvium-csi2.o
 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
 obj-$(CONFIG_VIDEO_AR0521) += ar0521.o
 obj-$(CONFIG_VIDEO_BT819) += bt819.o
drivers/media/i2c/alvium-csi2.c new file +3479 -0

2,977 lines not on this page · raw message

drivers/media/i2c/alvium-csi2.h new file +485 -0

485 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#8 lore raw
kernel test robot noticed the following build warnings:

Hi Tommaso,

kernel test robot noticed the following build warnings:

[auto build test WARNING on media-tree/master]
[also build test WARNING on robh/for-next linus/master v6.4-rc5 next-20230608] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tommaso-Merciai/dt-bindings-vendor-prefixes-Add-prefix-alliedvision/20230608-163423
base:   git://linuxtv.org/media_tree.git master
patch link:    https://lore.kernel.org/r/20230608083127.545750-4-tomm.merciai%40gmail.com
patch subject: [PATCH v5 3/3] media: i2c: Add support for alvium camera
config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20230609/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 12.3.0

reproduce (this is a W=1 build):
mkdir -p ~/bin
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
git remote add media-tree git://linuxtv.org/media_tree.git
git fetch media-tree master
git checkout media-tree/master
b4 shazam https://lore.kernel.org/r/[email protected]
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.3.0 ~/bin/make.cross W=1 O=build_dir ARCH=alpha olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.3.0 ~/bin/make.cross W=1 O=build_dir ARCH=alpha SHELL=/bin/bash drivers/media/i2c/

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/media/i2c/alvium-csi2.c: In function 'alvium_get_dt_data':

>> drivers/media/i2c/alvium-csi2.c:3245:13: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
    3245 |         int ret = 0;
         |             ^~~

vim +/ret +3245 drivers/media/i2c/alvium-csi2.c

3239
3240 static int alvium_get_dt_data(struct alvium_dev *alvium)
3241 {
3242 struct device *dev = &alvium->i2c_client->dev;
3243 struct device_node *node = dev->of_node;
3244 struct fwnode_handle *endpoint;

> 3245		int ret = 0;

3246
3247 if (!node)
3248 return -EINVAL;
3249
3250 ret = fwnode_property_read_u32(dev_fwnode(dev),
3251 "alliedvision,lp2hs-delay-us",
3252 &alvium->lp2hs_delay);
3253
3254 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
3255 if (!endpoint) {
3256 dev_err(dev, "endpoint node not found\n");
3257 return -EINVAL;
3258 }
3259
3260 if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &alvium->ep)) {
3261 dev_err(dev, "could not parse endpoint\n");
3262 return 0;
3263 }
3264
3265 if (alvium->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
3266 dev_err(dev, "unsupported bus type\n");
3267 return -EINVAL;
3268 }
3269
3270 if (!alvium->ep.nr_of_link_frequencies) {
3271 dev_err(dev, "no link frequencies defined");
3272 return -EINVAL;
3273 }
3274
3275 dev_info(dev, "freq: %llu\n",
3276 alvium->ep.link_frequencies[0]);
3277 dev_info(dev, "lanes: %d\n",
3278 alvium->ep.bus.mipi_csi2.num_data_lanes);
3279
3280 return 0;
3281 }
3282

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw
In reply to Tommaso Merciai

Hi Tommaso,

Thanks for the patch.

Quoted 95 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h
drivers/media/i2c/Kconfig +10 -0
index 256d55bb2b1d..a9fdf10ffc15 100644
@@ -34,6 +34,16 @@ config VIDEO_APTINA_PLL
 config VIDEO_CCS_PLL
 	tristate
 
+config VIDEO_ALVIUM_CSI2
+	tristate "Allied Vision ALVIUM MIPI CSI-2 camera support"
+	depends on I2C
+	select MEDIA_CONTROLLER
+	select VIDEO_V4L2_SUBDEV_API
+	select V4L2_FWNODE
+	help
+	  This is a Video4Linux2 sensor driver for the Allied Vision
+	  ALVIUM camera connected via MIPI CSI-2 interface.
+
 config VIDEO_AR0521
 	tristate "ON Semiconductor AR0521 sensor support"
 	depends on I2C && VIDEO_DEV
drivers/media/i2c/Makefile +1 -0
index b44dacf935f4..be4dddd6fd98 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_VIDEO_ADV7604) += adv7604.o
 obj-$(CONFIG_VIDEO_ADV7842) += adv7842.o
 obj-$(CONFIG_VIDEO_AK7375) += ak7375.o
 obj-$(CONFIG_VIDEO_AK881X) += ak881x.o
+obj-$(CONFIG_VIDEO_ALVIUM_CSI2) += alvium-csi2.o
 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
 obj-$(CONFIG_VIDEO_AR0521) += ar0521.o
 obj-$(CONFIG_VIDEO_BT819) += bt819.o
drivers/media/i2c/alvium-csi2.c new file +9 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Allied Vision Inc. Alvium camera driver
+ *
+ * Copyright (C) 2023 Tommaso Merciai
+ * Copyright (C) 2023 Martin Hecht
+ * Copyright (C) 2023 Avnet EMG GmbH
+ */
+

drivers/media/i2c/alvium-csi2.c line 19

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+#include <asm/unaligned.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/clkdev.h>
+#include <linux/ctype.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/init.h>

Do you need this?

drivers/media/i2c/alvium-csi2.c line 29

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of_graph.h>
+#include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/mipi-csi2.h>

Please order alphabetically.

Quoted 17 lines
drivers/media/i2c/alvium-csi2.c new file +17 -0
+#include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#include "alvium-csi2.h"
+
+static const struct v4l2_mbus_framefmt alvium_csi2_default_fmt = {
+	.code = MEDIA_BUS_FMT_UYVY8_1X16,
+	.width = 640,
+	.height = 480,
+	.colorspace = V4L2_COLORSPACE_SRGB,
+	.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
+	.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.field = V4L2_FIELD_NONE,
+};

drivers/media/i2c/alvium-csi2.c line 56

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+
+static const struct alvium_pixfmt alvium_csi2_fmts[] = {
+	{
+		/* UYVY8_2X8 */
+		.id = ALVIUM_FMT_UYVY8_2X8,
+		.code = MEDIA_BUS_FMT_UYVY8_2X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,

Could you use MIPI_CSI2_DT_ definitions in mipi-csi2.h?

Quoted 268 lines
drivers/media/i2c/alvium-csi2.c new file +268 -0
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* UYVY8_1X16 */
+		.id = ALVIUM_FMT_UYVY8_1X16,
+		.code = MEDIA_BUS_FMT_UYVY8_1X16,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* YUYV8_1X16 */
+		.id = ALVIUM_FMT_YUYV8_1X16,
+		.code = MEDIA_BUS_FMT_YUYV8_1X16,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* YUYV8_2X8 */
+		.id = ALVIUM_FMT_YUYV8_2X8,
+		.code = MEDIA_BUS_FMT_YUYV8_2X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* YUYV10_1X20 */
+		.id = ALVIUM_FMT_YUYV10_1X20,
+		.code = MEDIA_BUS_FMT_YUYV10_1X20,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_10,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1F,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* RGB888_1X24 */
+		.id = ALVIUM_FMT_RGB888_1X24,
+		.code = MEDIA_BUS_FMT_RGB888_1X24,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* RBG888_1X24 */
+		.id = ALVIUM_FMT_RBG888_1X24,
+		.code = MEDIA_BUS_FMT_RBG888_1X24,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* BGR888_1X24 */
+		.id = ALVIUM_FMT_BGR888_1X24,
+		.code = MEDIA_BUS_FMT_BGR888_1X24,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* RGB888_3X8 */
+		.id = ALVIUM_FMT_RGB888_3X8,
+		.code = MEDIA_BUS_FMT_RGB888_3X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* Y8_1X8 */
+		.id = ALVIUM_FMT_Y8_1X8,
+		.code = MEDIA_BUS_FMT_Y8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_MONO,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x00,
+		.is_raw = 1,
+	}, {
+		/* SGRBG8_1X8 */
+		.id = ALVIUM_FMT_SGRBG8_1X8,
+		.code = MEDIA_BUS_FMT_SGRBG8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SRGGB8_1X8 */
+		.id = ALVIUM_FMT_SRGGB8_1X8,
+		.code = MEDIA_BUS_FMT_SRGGB8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SGBRG8_1X8 */
+		.id = ALVIUM_FMT_SGBRG8_1X8,
+		.code = MEDIA_BUS_FMT_SGBRG8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SBGGR8_1X8 */
+		.id = ALVIUM_FMT_SBGGR8_1X8,
+		.code = MEDIA_BUS_FMT_SBGGR8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	}, {
+		/* Y10_1X10 */
+		.id = ALVIUM_FMT_Y10_1X10,
+		.code = MEDIA_BUS_FMT_Y10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_MONO,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x00,
+		.is_raw = 1,
+	}, {
+		/* SGRBG10_1X10 */
+		.id = ALVIUM_FMT_SGRBG10_1X10,
+		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SRGGB10_1X10 */
+		.id = ALVIUM_FMT_SRGGB10_1X10,
+		.code = MEDIA_BUS_FMT_SRGGB10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SGBRG10_1X10 */
+		.id = ALVIUM_FMT_SGBRG10_1X10,
+		.code = MEDIA_BUS_FMT_SGBRG10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SBGGR10_1X10 */
+		.id = ALVIUM_FMT_SBGGR10_1X10,
+		.code = MEDIA_BUS_FMT_SBGGR10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	}, {
+		/* Y12_1X12 */
+		.id = ALVIUM_FMT_Y12_1X12,
+		.code = MEDIA_BUS_FMT_Y12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_MONO,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x00,
+		.is_raw = 1,
+	}, {
+		/* SGRBG12_1X12 */
+		.id = ALVIUM_FMT_SGRBG12_1X12,
+		.code = MEDIA_BUS_FMT_SGRBG12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SRGGB12_1X12 */
+		.id = ALVIUM_FMT_SRGGB12_1X12,
+		.code = MEDIA_BUS_FMT_SRGGB12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SGBRG12_1X12 */
+		.id = ALVIUM_FMT_SGBRG12_1X12,
+		.code = MEDIA_BUS_FMT_SGBRG12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SBGGR12_1X12 */
+		.id = ALVIUM_FMT_SBGGR12_1X12,
+		.code = MEDIA_BUS_FMT_SBGGR12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	}, {
+		/* SBGGR14_1X14 */
+		.id = ALVIUM_FMT_SBGGR14_1X14,
+		.code = MEDIA_BUS_FMT_SBGGR14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SGBRG14_1X14 */
+		.id = ALVIUM_FMT_SGBRG14_1X14,
+		.code = MEDIA_BUS_FMT_SGBRG14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SRGGB14_1X14 */
+		.id = ALVIUM_FMT_SRGGB14_1X14,
+		.code = MEDIA_BUS_FMT_SRGGB14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SGRBG14_1X14 */
+		.id = ALVIUM_FMT_SGRBG14_1X14,
+		.code = MEDIA_BUS_FMT_SGRBG14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,

drivers/media/i2c/alvium-csi2.c line 334

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	},
+	{ /* sentinel */ }
+};
+
+static int alvium_read(struct alvium_dev *alvium,
+				  u32 reg, u64 *val)

Fits on a single line.

Quoted 22 lines
drivers/media/i2c/alvium-csi2.c new file +22 -0
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	struct i2c_msg msgs[2] = {0};
+	u8 addr_buf[2] = {0};
+	u8 data_buf[8] = {0};
+	int ret;
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, addr_buf);
+
+	msgs[0].addr = client->addr;
+	msgs[0].flags = 0;
+	msgs[0].len = ARRAY_SIZE(addr_buf);
+	msgs[0].buf = addr_buf;
+

drivers/media/i2c/alvium-csi2.c line 366

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	msgs[1].addr = client->addr;
+	msgs[1].flags = I2C_M_RD;
+	msgs[1].len = len;
+	msgs[1].buf = &data_buf[8 - len];
+
+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+	if (ret != ARRAY_SIZE(msgs))
+		return -EIO;
+
+	*val = get_unaligned_be64(data_buf);

Could you use CCI wrapper Hans has been working on? It'll be merged fairly soon I presume:
<URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+
+	return 0;
+}
+
+static int alvium_write(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	u8 buf[10] = {0};
+

drivers/media/i2c/alvium-csi2.c line 388

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, buf);
+	put_unaligned_be64(val << (8 * (8 - len)), buf + 2);
+	if (i2c_master_send(client, buf, len + 2) != len + 2)
+		return -EIO;

Same here.

Quoted 93 lines
drivers/media/i2c/alvium-csi2.c new file +93 -0
+
+	return 0;
+}
+
+static int alvium_write_hshake(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 hshake_regval;
+	u8 hshake_bit;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/* reset handshake bit */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* write alvium reg*/
+	ret = alvium_write(alvium, reg, val);
+	if (ret) {
+		dev_err(dev, "Fail to write reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 1*/
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (!hshake_bit);
+
+	/* reset handshake bit, write 0 to bit0 */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 0 */
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (hshake_bit);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_vers(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_bcrm_vers *v;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	v = (struct alvium_bcrm_vers *) &val;
+
+	v->minor = cpu_to_le16(v->minor);
+	v->major = cpu_to_le16(v->major);
+
+	dev_info(dev, "bcrm version: %u.%u\n", v->minor,
+			  v->major);

drivers/media/i2c/alvium-csi2.c line 491

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+
+	return 0;
+}
+
+static int alvium_get_fw_version(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_fw_vers *fw_v;

Please order temporary, loop etc. variables as last. Same for the other functions. Reverse Christmas tree order is often the same.

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_DEVICE_FIRMWARE_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read fw version reg\n");

drivers/media/i2c/alvium-csi2.c line 507

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+		return ret;
+	}
+
+	fw_v = (struct alvium_fw_vers *) &val;
+
+	fw_v->minor = cpu_to_le16(fw_v->minor);
+	fw_v->patch = cpu_to_le32(fw_v->patch);
+
+	dev_info(dev, "fw version: %u.%u.%u.%u\n",
+			  fw_v->special,

Please align arguments to just right of the opening parenthesis. Also, more fits on the same line (up to 80 chars). This applies to the entire patch.

Quoted 47 lines
drivers/media/i2c/alvium-csi2.c new file +47 -0
+			  fw_v->major,
+			  fw_v->minor,
+			  fw_v->patch);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_addr(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_REG_ADDR_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm addr reg\n");
+		return ret;
+	}
+
+	alvium->bcrm_addr = (u16)val;
+	dev_dbg(dev, "bcrm addr: %d\n", alvium->bcrm_addr);
+
+	return 0;
+}
+
+static bool alvium_is_alive(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val = 0;
+	u8 hbeat_val = 0;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HEARTBEAT_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read heartbeat reg\n");

drivers/media/i2c/alvium-csi2.c line 564

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+		return false;
+	}
+
+	hbeat_val = (u8)val;
+	dev_dbg(dev, "hearthbeat = %d", hbeat_val);
+
+	if (hbeat_val)
+		return true;
+	else
+		return false;

return hbeat_val;

Quoted 135 lines
drivers/media/i2c/alvium-csi2.c new file +135 -0
+}
+
+static void alvium_print_avail_mipi_fmt(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_leg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_LEG]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_10: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_10]);
+	dev_dbg(dev, "avail mipi_fmt rgb888: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB888]);
+	dev_dbg(dev, "avail mipi_fmt rgb666: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB666]);
+	dev_dbg(dev, "avail mipi_fmt rgb565: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB565]);
+	dev_dbg(dev, "avail mipi_fmt rgb555: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB555]);
+	dev_dbg(dev, "avail mipi_fmt rgb444: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB444]);
+	dev_dbg(dev, "avail mipi_fmt raw6: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW6]);
+	dev_dbg(dev, "avail mipi_fmt raw7: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW7]);
+	dev_dbg(dev, "avail mipi_fmt raw8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW8]);
+	dev_dbg(dev, "avail mipi_fmt raw10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW10]);
+	dev_dbg(dev, "avail mipi_fmt raw12: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW12]);
+	dev_dbg(dev, "avail mipi_fmt raw14: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW14]);
+	dev_dbg(dev, "avail mipi_fmt jpeg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_JPEG]);
+}
+
+static void alvium_print_avail_feat(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "feature rev_x: %u\n",
+		  alvium->avail_ft.rev_x);
+	dev_dbg(dev, "feature rev_y: %u\n",
+		  alvium->avail_ft.rev_y);
+	dev_dbg(dev, "feature int_autop: %u\n",
+		  alvium->avail_ft.int_autop);
+	dev_dbg(dev, "feature black_lvl: %u\n",
+		  alvium->avail_ft.black_lvl);
+	dev_dbg(dev, "feature gain: %u\n",
+		  alvium->avail_ft.gain);
+	dev_dbg(dev, "feature gamma: %u\n",
+		  alvium->avail_ft.gamma);
+	dev_dbg(dev, "feature contrast: %u\n",
+		  alvium->avail_ft.contrast);
+	dev_dbg(dev, "feature sat: %u\n",
+		  alvium->avail_ft.sat);
+	dev_dbg(dev, "feature hue: %u\n",
+		  alvium->avail_ft.hue);
+	dev_dbg(dev, "feature whiteb: %u\n",
+		  alvium->avail_ft.whiteb);
+	dev_dbg(dev, "feature sharp: %u\n",
+		  alvium->avail_ft.sharp);
+	dev_dbg(dev, "feature auto_exp: %u\n",
+		  alvium->avail_ft.auto_exp);
+	dev_dbg(dev, "feature auto_gain: %u\n",
+		  alvium->avail_ft.auto_gain);
+	dev_dbg(dev, "feature auto_whiteb: %u\n",
+		  alvium->avail_ft.auto_whiteb);
+	dev_dbg(dev, "feature dev_temp: %u\n",
+		  alvium->avail_ft.dev_temp);
+	dev_dbg(dev, "feature acq_abort: %u\n",
+		  alvium->avail_ft.acq_abort);
+	dev_dbg(dev, "feature acq_fr: %u\n",
+		  alvium->avail_ft.acq_fr);
+	dev_dbg(dev, "feature fr_trigger: %u\n",
+		  alvium->avail_ft.fr_trigger);
+	dev_dbg(dev, "feature exp_acq_line: %u\n",
+		  alvium->avail_ft.exp_acq_line);
+}
+
+static void alvium_print_avail_bayer(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail bayer mono: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_MONO]);
+	dev_dbg(dev, "avail bayer gr: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GR]);
+	dev_dbg(dev, "avail bayer rg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_RG]);
+	dev_dbg(dev, "avail bayer gb: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GB]);
+	dev_dbg(dev, "avail bayer bg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_BG]);
+}
+
+static int alvium_get_feat_inq(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_feat *f;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_FEATURE_INQUIRY_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to features inquiry reg\n");
+		return ret;
+	}
+
+	f = (struct alvium_avail_feat *) &val;
+
+	alvium->avail_ft.rev_x = f->rev_x;
+	alvium->avail_ft.rev_y = f->rev_y;
+	alvium->avail_ft.int_autop = f->int_autop;
+	alvium->avail_ft.black_lvl = f->black_lvl;
+	alvium->avail_ft.gain = f->gain;
+	alvium->avail_ft.gamma = f->gamma;
+	alvium->avail_ft.contrast = f->contrast;
+	alvium->avail_ft.sat = f->sat;
+	alvium->avail_ft.hue = f->rev_y;

drivers/media/i2c/alvium-csi2.c line 709

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	alvium->avail_ft.whiteb = f->whiteb;
+	alvium->avail_ft.sharp = f->sharp;
+	alvium->avail_ft.auto_exp = f->auto_exp;
+	alvium->avail_ft.auto_gain = f->auto_gain;
+	alvium->avail_ft.auto_whiteb = f->auto_whiteb;
+	alvium->avail_ft.dev_temp = f->dev_temp;
+	alvium->avail_ft.acq_abort = f->acq_abort;
+	alvium->avail_ft.acq_fr = f->acq_fr;
+	alvium->avail_ft.fr_trigger = f->fr_trigger;
+	alvium->avail_ft.exp_acq_line = f->exp_acq_line;

You could instead do:

	alvium->avail_ft = *f;

But is there an endianness issue here? I think you should use u64 also for the struct fields.

Quoted 163 lines
drivers/media/i2c/alvium-csi2.c new file +163 -0
+
+	alvium_print_avail_feat(alvium);
+
+	return 0;
+}
+
+static int alvium_get_host_supp_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_LANE_COUNT_RW,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->h_sup_csi_lanes = (u8)val;
+	dev_dbg(dev, "host supported csi2 lanes count: %d\n",
+			  alvium->h_sup_csi_lanes);
+
+	return 0;
+}
+
+static int alvium_set_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 num_lanes;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	num_lanes = alvium->ep.bus.mipi_csi2.num_data_lanes;
+
+	if (num_lanes <= alvium->h_sup_csi_lanes) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_LANE_COUNT_RW,
+				  num_lanes);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "Set n lanes = %llu", num_lanes);
+
+	return 0;
+}
+
+static int alvium_set_lp2hs_delay(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 lp2hs_delay;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/*
+	 * The purpose of this reg is force a DPhy reset
+	 * for the period described by the microseconds on
+	 * the reg, before it starts streaming.
+	 *
+	 * To be clear, with that value bigger than 0 the
+	 * Alvium forces a dphy-reset on all lanes for that period.
+	 * That means all lanes go up into low power state.
+	 *
+	 * This may help a csi2 rx ip to reset if that IP
+	 * can't deal with a continous clock.
+	 */
+	lp2hs_delay = (u64)alvium->lp2hs_delay;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_STREAM_ON_DELAY_RW,
+			  lp2hs_delay);
+	if (ret) {
+		dev_err(dev, "Fail to set streamon delay reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_csi_clk_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->min_csi_clk = (u32)val;
+	dev_dbg(dev, "Min host supported csi clock: %d\n",
+			  alvium->min_csi_clk);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->max_csi_clk = (u32)val;
+	dev_dbg(dev, "Max host supported csi clock: %d\n",
+			  alvium->max_csi_clk);
+
+	return 0;
+}
+
+static int alvium_set_csi_clk(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 csi_clk;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	csi_clk = (u32)alvium->ep.link_frequencies[0];
+
+	if (csi_clk > alvium->min_csi_clk ||
+		csi_clk < alvium->max_csi_clk) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_CLOCK_RW,
+				  csi_clk);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	alvium->link_freq = alvium->ep.link_frequencies[0];
+
+	return 0;
+}
+
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+

drivers/media/i2c/alvium-csi2.c line 882

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 1,733 lines
drivers/media/i2c/alvium-csi2.c new file +1733 -0
+
+	alvium->img_min_width = (u32)val;
+	dev_dbg(dev, "Min img width: %d\n",
+			  alvium->img_min_width);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img max width reg\n");
+		return ret;
+	}
+
+	alvium->img_max_width = (u32)val;
+	dev_dbg(dev, "Max img width: %d\n",
+			  alvium->img_max_width);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img inc width reg\n");
+		return ret;
+	}
+
+	alvium->img_inc_width = (u32)val;
+	dev_dbg(dev, "img width increment: %d px\n",
+			  alvium->img_inc_width);
+
+	return 0;
+}
+
+static int alvium_get_img_height_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_HEIGHT_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img min height reg\n");
+		return ret;
+	}
+
+	alvium->img_min_height = (u32)val;
+	dev_dbg(dev, "Min img height: %d\n",
+			  alvium->img_min_height);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_HEIGHT_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img max height reg\n");
+		return ret;
+	}
+
+	alvium->img_max_height = (u32)val;
+	dev_dbg(dev, "Max img height: %d\n",
+			  alvium->img_max_height);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_HEIGHT_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img inc height reg\n");
+		return ret;
+	}
+
+	alvium->img_inc_height = (u32)val;
+	dev_dbg(dev, "img height increment: %d\n",
+			  alvium->img_inc_height);
+
+	return 0;
+}
+
+static int alvium_set_img_width(struct alvium_dev *alvium, u32 width)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_WIDTH_RW,
+			  width);
+	if (ret) {
+		dev_err(dev, "Fail to set img width\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_img_height(struct alvium_dev *alvium, u32 height)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_HEIGHT_RW,
+			  height);
+	if (ret) {
+		dev_err(dev, "Fail to set img height\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_img_offx(struct alvium_dev *alvium, u32 offx)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_OFFSET_X_RW,
+			  offx);
+	if (ret) {
+		dev_err(dev, "Fail to set img offx\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_img_offy(struct alvium_dev *alvium, u32 offy)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_RW,
+			  offy);
+	if (ret) {
+		dev_err(dev, "Fail to set img offy\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_offx_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_X_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min offx reg\n");
+		return ret;
+	}
+
+	alvium->min_offx = (u32)val;
+	dev_dbg(dev, "Min img offx: %d\n",
+			  alvium->min_offx);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_X_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max offx reg\n");
+		return ret;
+	}
+
+	alvium->max_offx = (u32)val;
+	dev_dbg(dev, "Max img offx: %d\n",
+			  alvium->max_offx);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_X_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc offx reg\n");
+		return ret;
+	}
+
+	alvium->inc_offx = (u32)val;
+	dev_dbg(dev, "Inc img offx: %d\n",
+			  alvium->inc_offx);
+
+	return 0;
+}
+
+static int alvium_get_offy_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min offy reg\n");
+		return ret;
+	}
+
+	alvium->min_offy = (u32)val;
+	dev_dbg(dev, "Min img offy: %d\n",
+			  alvium->min_offy);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max offy reg\n");
+		return ret;
+	}
+
+	alvium->max_offy = (u32)val;
+	dev_dbg(dev, "Max img offy: %d\n",
+			  alvium->max_offy);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc offy reg\n");
+		return ret;
+	}
+
+	alvium->inc_offy = (u32)val;
+	dev_dbg(dev, "Inc img offy: %d\n",
+				  alvium->inc_offy);
+
+	return 0;
+}
+
+static int alvium_get_gain_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to min gain reg\n");
+		return ret;
+	}
+
+	alvium->min_gain = val;
+	dev_dbg(dev, "Min gain: %llu mB\n",
+			  alvium->min_gain);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to max gain reg\n");
+		return ret;
+	}
+
+	alvium->max_gain = val;
+	dev_dbg(dev, "Max gain: %llu mB\n",
+			  alvium->max_gain);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to inc gain reg\n");
+		return ret;
+	}
+
+	alvium->inc_gain = val;
+	dev_dbg(dev, "gain inc: %llu mB\n",
+			  alvium->inc_gain);
+
+	return 0;
+}
+
+static int alvium_get_exposure_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img min exp reg\n");
+		return ret;
+	}
+
+	alvium->min_exp = val;
+	dev_dbg(dev, "Min exp: %llu ns\n",
+			  alvium->min_exp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img max exp reg\n");
+		return ret;
+	}
+
+	alvium->max_exp = val;
+	dev_dbg(dev, "Max exp: %llu ns\n",
+			  alvium->max_exp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc exp reg\n");
+		return ret;
+	}
+
+	alvium->inc_exp = val;
+	dev_dbg(dev, "Inc exp: %llu ns\n",
+			  alvium->inc_exp);
+
+	return 0;
+}
+
+static int alvium_get_red_balance_ratio_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min rbalance reg\n");
+		return ret;
+	}
+
+	alvium->min_rbalance = val;
+	dev_dbg(dev, "Min rbalance: %llu\n",
+			  alvium->min_rbalance);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max rbalance reg\n");
+		return ret;
+	}
+
+	alvium->max_rbalance = val;
+	dev_dbg(dev, "Max rbalance: %llu\n",
+			  alvium->max_rbalance);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc rbalance reg\n");
+		return ret;
+	}
+
+	alvium->inc_rbalance = val;
+	dev_dbg(dev, "Inc rbalance: %llu\n",
+			  alvium->inc_rbalance);
+
+	return 0;
+}
+
+static int alvium_get_blue_balance_ratio_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLUE_BALANCE_RATIO_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min bbalance reg\n");
+		return ret;
+	}
+
+	alvium->min_bbalance = val;
+	dev_dbg(dev, "Min bbalance: %llu\n",
+				  alvium->min_bbalance);
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_BLUE_BALANCE_RATIO_MAX_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max bbalance reg\n");
+		return ret;
+	}
+
+	alvium->max_bbalance = val;
+	dev_dbg(dev, "Max bbalance: %llu\n",
+			  alvium->max_bbalance);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLUE_BALANCE_RATIO_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc bbalance reg\n");
+		return ret;
+	}
+
+	alvium->inc_bbalance = val;
+	dev_dbg(dev, "Inc bbalance: %llu\n",
+			  alvium->inc_bbalance);
+
+	return 0;
+}
+
+static int alvium_get_hue_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HUE_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min hue reg\n");
+		return ret;
+	}
+
+	alvium->min_hue = (s32)val;
+	dev_dbg(dev, "Min hue: %d\n",
+			  alvium->min_hue);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HUE_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max hue reg\n");
+		return ret;
+	}
+
+	alvium->max_hue = (s32)val;
+	dev_dbg(dev, "Max hue: %d\n",
+			  alvium->max_hue);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HUE_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc hue reg\n");
+		return ret;
+	}
+
+	alvium->inc_hue = (s32)val;
+	dev_dbg(dev, "Inc hue: %d\n",
+			  alvium->inc_hue);
+
+	return 0;
+}
+
+static int alvium_get_black_lvl_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min black lvl reg\n");
+		return ret;
+	}
+
+	alvium->min_black_lvl = (s32)val;
+	dev_dbg(dev, "Min black lvl: %d\n",
+			  alvium->min_hue);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max black lvl reg\n");
+		return ret;
+	}
+
+	alvium->max_black_lvl = (s32)val;
+	dev_dbg(dev, "Max black lvl: %d\n",
+			  alvium->max_black_lvl);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc black lvl reg\n");
+		return ret;
+	}
+
+	alvium->inc_black_lvl = (s32)val;
+	dev_dbg(dev, "Inc black lvl: %d\n",
+			  alvium->inc_black_lvl);
+
+	return 0;
+}
+
+static int alvium_get_gamma_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_GAMMA_MIN_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min gamma reg\n");
+		return ret;
+	}
+
+	alvium->min_gamma = val;
+	dev_dbg(dev, "Min gamma: %llu\n",
+			  alvium->min_gamma);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAMMA_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max bbalance reg\n");
+		return ret;
+	}
+
+	alvium->max_gamma = val;
+	dev_dbg(dev, "Max gamma: %llu\n",
+			  alvium->max_gamma);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAMMA_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc gamma reg\n");
+		return ret;
+	}
+
+	alvium->inc_gamma = val;
+	dev_dbg(dev, "Inc gamma: %llu\n",
+			  alvium->inc_gamma);
+
+	return 0;
+}
+
+static int alvium_get_sharpness_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_SHARPNESS_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min sharpness lvl reg\n");
+		return ret;
+	}
+
+	alvium->min_sharp = (s32)val;
+	dev_dbg(dev, "Min sharpness: %d\n",
+			  alvium->min_sharp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max sharpness reg\n");
+		return ret;
+	}
+
+	alvium->max_sharp = (s32)val;
+	dev_dbg(dev, "Max sharpness: %d\n",
+			  alvium->max_sharp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_SHARPNESS_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc sharpness reg\n");
+		return ret;
+	}
+
+	alvium->inc_sharp = (s32)val;
+	dev_dbg(dev, "Inc sharpness: %d\n",
+			  alvium->inc_sharp);
+
+	return 0;
+}
+
+static int alvium_get_contrast_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CONTRAST_VALUE_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min contrast reg\n");
+		return ret;
+	}
+
+	alvium->min_contrast = (u32)val;
+	dev_dbg(dev, "Min contrast: %d\n",
+			  alvium->min_contrast);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CONTRAST_VALUE_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max contrast reg\n");
+		return ret;
+	}
+
+	alvium->max_contrast = (u32)val;
+	dev_dbg(dev, "Max contrast: %d\n",
+			  alvium->max_contrast);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CONTRAST_VALUE_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc contrast reg\n");
+		return ret;
+	}
+
+	alvium->inc_contrast = (u32)val;
+	dev_dbg(dev, "Inc contrast: %d\n",
+			  alvium->inc_contrast);
+
+	return 0;
+}
+
+static int alvium_get_saturation_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_SATURATION_MIN_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min saturation reg\n");
+		return ret;
+	}
+
+	alvium->min_sat = (u32)val;
+	dev_dbg(dev, "Min saturation: %d\n",
+			  alvium->min_sat);
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_SATURATION_MAX_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max saturation reg\n");
+		return ret;
+	}
+
+	alvium->max_sat = (u32)val;
+	dev_dbg(dev, "Max saturation: %d\n",
+			  alvium->max_sat);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_SATURATION_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max saturation reg\n");
+		return ret;
+	}
+
+	alvium->inc_sat = (u32)val;
+	dev_dbg(dev, "Inc saturation: %d\n",
+			  alvium->inc_sat);
+
+	return 0;
+}
+
+static int alvium_set_bcm_mode(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write(alvium,
+			  REG_GENCP_CHANGEMODE_W,
+			  ALVIUM_BCM_MODE);
+	if (ret) {
+		dev_err(dev, "Fail to current mode reg\n");
+		return ret;
+	}
+
+	alvium->bcrm_mode = ALVIUM_BCM_MODE;
+	dev_dbg(dev, "Set BCM mode");
+
+	return 0;
+}
+
+static int alvium_get_mode(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 bcrm_mode;
+
+	ret = alvium_read(alvium,
+			  REG_GENCP_CURRENTMODE_R,
+			  &bcrm_mode);
+	if (ret) {
+		dev_err(dev, "Fail to read current mode reg\n");
+		return ret;
+	}
+
+	switch (bcrm_mode) {
+	case ALVIUM_BCM_MODE:
+		alvium->bcrm_mode = ALVIUM_BCM_MODE;
+		break;
+	case ALVIUM_GENCP_MODE:
+		alvium->bcrm_mode = ALVIUM_GENCP_MODE;
+		break;
+	}
+
+	dev_dbg(dev, "Current mode is: %d\n", alvium->bcrm_mode);
+	return 0;
+}
+
+static int alvium_get_avail_mipi_data_format(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_mipi_fmt *avail_fmt;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_AVAILABLE_MIPI_DATA_FORMATS_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "available mipi data format reg\n");
+		return ret;
+	}
+
+	avail_fmt = (struct alvium_avail_mipi_fmt *) &val;
+
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_LEG]
+				  = avail_fmt->yuv420_8_leg;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8]
+				  = avail_fmt->yuv420_8;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10]
+				  = avail_fmt->yuv420_10;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_CSPS]
+				  = avail_fmt->yuv420_8_csps;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10_CSPS]
+				  = avail_fmt->yuv420_10_csps;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_8]
+				  = avail_fmt->yuv422_8;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_10]
+				  = avail_fmt->yuv422_10;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB888]
+				  = avail_fmt->rgb888;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB666]
+				  = avail_fmt->rgb666;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB565]
+				  = avail_fmt->rgb565;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB555]
+				  = avail_fmt->rgb555;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB444]
+				  = avail_fmt->rgb444;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW6]
+				  = avail_fmt->raw6;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW7]
+				  = avail_fmt->raw7;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW8]
+				  = avail_fmt->raw8;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW10]
+				  = avail_fmt->raw10;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW12]
+				  = avail_fmt->raw12;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW14]
+				  = avail_fmt->raw14;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_JPEG]
+				  = avail_fmt->jpeg;
+
+	alvium_print_avail_mipi_fmt(alvium);
+
+	return 0;
+}
+
+static int alvium_setup_mipi_fmt(struct alvium_dev *alvium)
+{
+	int avail_fmt_cnt = 0;
+	int sz = 0;
+
+	alvium->alvium_csi2_fmt = NULL;
+
+	/* calculate fmt array size */
+	for (int fmt = 0; fmt < ALVIUM_NUM_SUPP_MIPI_DATA_FMT; fmt++) {
+		if (alvium->is_mipi_fmt_avail[alvium_csi2_fmts[fmt].fmt_av_bit])
+			if ((!alvium_csi2_fmts[fmt].is_raw) ||
+				  (alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit]))
+				sz++;
+	}
+
+	/* init alvium_csi2_fmt array */
+	alvium->alvium_csi2_fmt_n = sz;
+	alvium->alvium_csi2_fmt = kmalloc_array(sz,
+						     sizeof(struct alvium_pixfmt),
+						     GFP_KERNEL);
+
+	/* Create the alvium_csi2 fmt array from formats available */
+	for (int fmt = 0; fmt < ALVIUM_NUM_SUPP_MIPI_DATA_FMT; fmt++) {
+		if (alvium->is_mipi_fmt_avail[alvium_csi2_fmts[fmt].fmt_av_bit])
+			if ((!alvium_csi2_fmts[fmt].is_raw) ||
+				  (alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit])) {
+				alvium->alvium_csi2_fmt[avail_fmt_cnt] =
+						alvium_csi2_fmts[fmt];
+				avail_fmt_cnt++;
+			}
+	}
+
+	return 0;
+}
+
+static int alvium_set_mipi_fmt(struct alvium_dev *alvium,
+				  const struct alvium_pixfmt *pixfmt)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 reg_val;
+	int ret;
+
+	reg_val = pixfmt->mipi_fmt_regval;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_MIPI_DATA_FORMAT_RW,
+			  reg_val);
+	if (ret) {
+		dev_err(dev, "Fail to set mipi fmt\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_avail_bayer(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_bayer *avail_bay;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_BAYER_PATTERN_INQUIRY_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read available Bayer patterns reg\n");
+		return ret;
+	}
+
+	avail_bay = (struct alvium_avail_bayer *) &val;
+
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_MONO] = avail_bay->mono;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_GR] = avail_bay->gr;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_RG] = avail_bay->rg;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_GB] = avail_bay->gb;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_BG] = avail_bay->bg;
+
+	alvium_print_avail_bayer(alvium);
+
+	return 0;
+}
+
+static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
+				  const struct alvium_pixfmt *pixfmt)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 reg_val;
+	int ret;
+
+	reg_val = pixfmt->bay_fmt_regval;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_BAYER_PATTERN_RW,
+			  reg_val);
+	if (ret) {
+		dev_err(dev, "Fail to set bayer pattern\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_frame_interval(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min frame rate reg\n");
+		return ret;
+	}
+
+	alvium->min_fr = val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max frame rate reg\n");
+		return ret;
+	}
+
+	alvium->max_fr = val;
+
+	dev_dbg(dev, "framerate min: %llu µHz, max: %llu µHz\n",
+			  alvium->min_fr, alvium->max_fr);
+	return 0;
+}
+
+static int alvium_set_frame_rate(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 fr;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	fr = alvium->fr;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_ACQUISITION_FRAME_RATE_RW,
+			  fr);
+	if (ret) {
+		dev_err(dev, "Fail to set frame rate lanes reg\n");
+		return ret;
+	}
+
+	dev_dbg(dev, "set frame rate: %llu us\n", fr);
+
+	return 0;
+}
+
+static int alvium_set_stream_mipi(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_ACQUISITION_START_RW,
+			  0x01);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_ACQUISITION_STOP_RW,
+			  0x01);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail set_stream_mipi\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_gain(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 gain;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_gain)
+		return -EINVAL;
+
+	/* The unit is millibel (1 mB = 0.01 dB) */
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_RW,
+			  &gain);
+	if (ret) {
+		dev_err(dev, "Fail to read gain value reg\n");
+		return ret;
+	}
+
+	return gain;
+}
+
+static int alvium_set_gain(struct alvium_dev *alvium, int gain)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_gain)
+		return -EINVAL;
+
+	/* The unit is millibel (1 mB = 0.01 dB) */
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAIN_RW,
+			  (u64)gain);
+	if (ret) {
+		dev_err(dev, "Fail to set gain value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_autogain(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_gain)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAIN_AUTO_RW,
+			  0x02);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAIN_AUTO_RW,
+			  0x00);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail to set autogain reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_exposure(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 exp;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_exp)
+		return -EINVAL;
+
+	/* Exposure time in ns */
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_RW,
+			  &exp);
+	if (ret) {
+		dev_err(dev, "Fail to read exposure value reg\n");
+		return ret;
+	}
+
+	return exp;
+}
+
+static int alvium_set_autoexposure(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_exp)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_EXPOSURE_AUTO_RW,
+			  0x02);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_EXPOSURE_AUTO_RW,
+			  0x00);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail to set autoexposure reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_exposure(struct alvium_dev *alvium, int exposure_ns)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_EXPOSURE_TIME_RW,
+			  (u64)exposure_ns);
+	if (ret) {
+		dev_err(dev, "Fail to set exposure value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_gain(struct alvium_dev *alvium, bool auto_gain)
+{
+	struct alvium_ctrls *ctrls = &alvium->ctrls;
+	int ret = 0;
+
+	if (ctrls->auto_gain->is_new) {
+		ret = alvium_set_autogain(alvium, auto_gain);
+		if (ret)
+			return ret;
+	}
+
+	if (!auto_gain && ctrls->gain->is_new) {
+		u64 max_gain;
+		u64 min_gain;
+
+		ret = alvium_get_gain_params(alvium);
+		if (ret)
+			return ret;
+
+		max_gain = alvium->max_gain;
+		min_gain = alvium->min_gain;
+
+		if (ctrls->gain->val >= min_gain &&
+			  ctrls->gain->val <= max_gain) {
+			ret = alvium_set_gain(alvium, ctrls->gain->val);
+		} else {
+			return -EINVAL;
+		}
+	}
+
+	return ret;
+}
+
+static int alvium_set_blue_balance_ratio(struct alvium_dev *alvium,
+				    int blue)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_BLUE_BALANCE_RATIO_RW,
+			  (u64)blue);
+	if (ret) {
+		dev_err(dev, "Fail to set blue ratio value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_red_balance_ratio(struct alvium_dev *alvium,
+				    int red)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_RW,
+			  (u64)red);
+	if (ret) {
+		dev_err(dev, "Fail to set red ratio value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_awb(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_whiteb)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_WHITE_BALANCE_AUTO_RW,
+			  0x02);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_WHITE_BALANCE_AUTO_RW,
+			  0x00);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail to set awb reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_exposure(struct alvium_dev *alvium,
+				    enum v4l2_exposure_auto_type auto_exposure)
+{
+	struct alvium_ctrls *ctrls = &alvium->ctrls;
+	bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
+	int ret = 0;
+
+	if (ctrls->auto_exp->is_new) {
+		ret = alvium_set_autoexposure(alvium, auto_exp);
+		if (ret)
+			return ret;
+	}
+
+	if (!auto_exp && ctrls->exposure->is_new) {
+		u64 max_exp;
+		u64 min_exp;
+
+		ret = alvium_get_exposure_params(alvium);
+		if (ret)
+			return ret;
+
+		max_exp = alvium->max_exp;
+		min_exp = alvium->min_exp;
+
+		if (ctrls->exposure->val >= min_exp &&
+			  ctrls->exposure->val <= max_exp) {
+			ret = alvium_set_exposure(alvium, ctrls->exposure->val);
+			if (ret)
+				return ret;
+		} else {
+			return -EINVAL;
+		}
+	}
+
+	return ret;
+}
+
+static int alvium_set_ctrl_white_balance(struct alvium_dev *alvium,
+				    bool awb)
+{
+	struct alvium_ctrls *ctrls = &alvium->ctrls;
+	int ret = 0;
+
+	if (ctrls->auto_wb->is_new) {
+		ret = alvium_set_awb(alvium, awb);
+		if (ret)
+			return ret;
+	}
+
+	if (!awb && ctrls->auto_wb->is_new) {
+		u64 red = (u64)ctrls->red_balance->val;
+		u64 blue = (u64)ctrls->blue_balance->val;
+		u64 red_max;
+		u64 red_min;
+		u64 blue_max;
+		u64 blue_min;
+
+		ret = alvium_get_red_balance_ratio_params(alvium);
+		if (ret)
+			return ret;
+
+		ret = alvium_get_blue_balance_ratio_params(alvium);
+		if (ret)
+			return ret;
+
+		red_max = alvium->max_rbalance;
+		red_min = alvium->min_rbalance;
+		blue_max = alvium->max_bbalance;
+		blue_min = alvium->min_rbalance;
+
+		if (red >= red_min &&
+			  red <= red_max) {
+			ret = alvium_set_red_balance_ratio(alvium, red);
+			if (ret)
+				return ret;
+		} else {
+			return -EINVAL;
+		}
+
+		if (blue >= blue_min &&
+			  blue <= blue_max) {
+			ret = alvium_set_blue_balance_ratio(alvium, blue);
+			if (ret)
+				return ret;
+		} else {
+			return -EINVAL;
+		}
+	}
+
+	return ret;
+}
+
+static int alvium_set_ctrl_hue(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_HUE_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set hue value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_contrast(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_CONTRAST_VALUE_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set contrast value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_saturation(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_SATURATION_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set contrast value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_gamma(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAMMA_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set gamma value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_sharpness(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_SHARPNESS_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set sharpness value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_hflip(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_REVERSE_X_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set reverse_x value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_vflip(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_REVERSE_Y_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set reverse_y value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_hw_features_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_get_csi_clk_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read min/max csi clock regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_img_width_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read img width regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_img_height_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read img heigth regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_offx_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read offx regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_offy_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read offy regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_gain_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read gain regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_exposure_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read min/max exp regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_red_balance_ratio_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read red balance ratio regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_blue_balance_ratio_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read blue balance ratio regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_hue_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read hue regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_contrast_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read contrast regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_saturation_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read saturation regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_black_lvl_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read black lvl regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_gamma_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read gamma regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_sharpness_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read sharpness regs\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_hw_info(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_get_bcrm_vers(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_bcrm_addr(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to bcrm address reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_fw_version(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read fw version reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_host_supp_csi_lanes(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi lanes reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_feat_inq(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm feature inquiry reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_hw_features_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read features params regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_avail_mipi_data_format(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read available mipi data formats reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_avail_bayer(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read available Bayer patterns reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_mode(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to get current mode reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_hw_init(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	/* Set Alvium BCM mode*/
+	ret = alvium_set_bcm_mode(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set BCM mode\n");
+		return ret;
+	}
+
+	ret = alvium_set_csi_lanes(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set csi lanes\n");
+		return ret;
+	}
+
+	ret = alvium_set_csi_clk(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set csi lanes\n");
+		return ret;
+	}
+
+	ret = alvium_set_lp2hs_delay(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set streamon reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/* --------------- Subdev Operations --------------- */
+
+static int alvium_g_frame_interval(struct v4l2_subdev *sd,
+				   struct v4l2_subdev_frame_interval *fi)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+
+	fi->interval = alvium->frame_interval;
+
+	return 0;
+}
+
+static int alvium_set_frame_interval(struct alvium_dev *alvium,
+					struct v4l2_subdev_frame_interval *fi)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 req_fr, min_fr, max_fr;
+	int ret;
+
+	if (fi->interval.denominator == 0)
+		return -EINVAL;
+
+	ret = alvium_get_frame_interval(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to get frame interval\n");
+		return ret;
+	}
+
+	min_fr = alvium->min_fr;
+	max_fr = alvium->max_fr;
+
+	dev_dbg(dev, "fi->interval.numerator = %d\n",
+		  fi->interval.numerator);
+	dev_dbg(dev, "fi->interval.denominator = %d\n",
+		  fi->interval.denominator);
+
+	req_fr = (fi->interval.denominator * USEC_PER_SEC) /
+				  fi->interval.numerator;
+
+	if (req_fr >= max_fr &&
+		  req_fr <= min_fr)
+		return -EINVAL;
+
+	alvium->fr = req_fr;
+	alvium->frame_interval.numerator = fi->interval.numerator;
+	alvium->frame_interval.denominator = fi->interval.denominator;
+
+	return 0;

drivers/media/i2c/alvium-csi2.c line 2625

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+}
+
+static int alvium_s_frame_interval(struct v4l2_subdev *sd,
+				   struct v4l2_subdev_frame_interval *fi)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int ret;
+
+	if (fi->pad != 0)
+		return -EINVAL;

The caller will have checked for this already.

Quoted 33 lines
drivers/media/i2c/alvium-csi2.c new file +33 -0
+
+	if (alvium->streaming)
+		return -EBUSY;
+
+	ret = alvium_set_frame_interval(alvium, fi);
+	if (!ret) {
+		ret = alvium_set_frame_rate(alvium);
+		if (ret)
+			return -EIO;
+	}
+
+	return ret;
+}
+
+static int alvium_enum_mbus_code(struct v4l2_subdev *sd,
+				 struct v4l2_subdev_state *sd_state,
+				 struct v4l2_subdev_mbus_code_enum *code)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	const struct alvium_pixfmt *formats;
+	unsigned int num_formats;
+
+	formats = alvium->alvium_csi2_fmt;
+	num_formats = alvium->alvium_csi2_fmt_n;
+
+	if (code->index >= num_formats)
+		return -EINVAL;
+
+	code->code = formats[code->index].code;
+
+	return 0;
+}
+

drivers/media/i2c/alvium-csi2.c line 2668

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+static const struct alvium_pixfmt *
+alvium_code_to_pixfmt(struct alvium_dev *alvium, u32 code)
+{
+	const struct alvium_pixfmt *formats = alvium->alvium_csi2_fmt;
+	unsigned int i;
+
+	for (i = 0; formats[i].code; ++i) {
+		if (formats[i].code == code)
+			return &formats[i];
+	}

No need for braces here.

Quoted 309 lines
drivers/media/i2c/alvium-csi2.c new file +309 -0
+
+	return &formats[0];
+}
+
+static int alvium_set_mode(struct alvium_dev *alvium,
+				 struct v4l2_subdev_state *state)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct v4l2_subdev *sd = &alvium->sd;
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+	int ret;
+
+	crop = v4l2_subdev_get_pad_crop(sd, state, 0);
+	fmt = v4l2_subdev_get_pad_format(sd, state, 0);
+
+	if (fmt->width < alvium->img_max_width &&
+		   fmt->width > alvium->img_min_width) {
+		ret = alvium_set_img_width(alvium, fmt->width);
+		if (ret)
+			return ret;
+	} else {
+		dev_err(dev, "Unsupported width");
+		return -EINVAL;
+	}
+
+	if (fmt->height < alvium->img_max_height &&
+		   fmt->height > alvium->img_min_height) {
+		ret = alvium_set_img_height(alvium, fmt->height);
+		if (ret)
+			return ret;
+	} else {
+		dev_err(dev, "Unsupported height");
+		return -EINVAL;
+	}
+
+	ret = alvium_set_img_offx(alvium, crop->left);
+	if (ret)
+		return ret;
+
+	ret = alvium_set_img_offy(alvium, crop->top);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int alvium_set_framefmt(struct alvium_dev *alvium,
+			       struct v4l2_mbus_framefmt *format)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	const struct alvium_pixfmt *alvium_csi2_fmt;
+	int ret = 0;
+
+	alvium_csi2_fmt = alvium_code_to_pixfmt(alvium, format->code);
+
+	ret = alvium_set_mipi_fmt(alvium, alvium_csi2_fmt);
+	if (ret)
+		return ret;
+
+	if (alvium_csi2_fmt->is_raw) {
+		ret = alvium_set_bayer_pattern(alvium, alvium_csi2_fmt);
+		if (ret)
+			return ret;
+	}
+
+	dev_dbg(dev, "start: %s, mipi_fmt_regval regval = 0x%llx",
+	  __func__, alvium_csi2_fmt->mipi_fmt_regval);
+
+	return ret;
+}
+
+static int alvium_s_stream(struct v4l2_subdev *sd, int enable)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_subdev_state *state;
+	int ret = 0;
+
+	state = v4l2_subdev_lock_and_get_active_state(sd);
+
+	if (enable) {
+		ret = __v4l2_ctrl_handler_setup(&alvium->ctrls.handler);
+		if (ret)
+			goto out;
+
+		ret = alvium_set_mode(alvium, state);
+		if (ret)
+			goto out;
+
+		fmt = v4l2_subdev_get_pad_format(sd, state, 0);
+		ret = alvium_set_framefmt(alvium, fmt);
+		if (ret)
+			goto out;
+
+		ret = alvium_set_stream_mipi(alvium, enable);
+		if (ret)
+			goto out;
+
+	} else {
+		ret = alvium_set_stream_mipi(alvium, enable);
+		if (ret)
+			goto out;
+	}
+
+	v4l2_subdev_unlock_state(state);
+	alvium->streaming = !!enable;
+
+	return 0;
+
+out:
+	v4l2_subdev_unlock_state(state);
+	return ret;
+}
+
+static int alvium_init_cfg(struct v4l2_subdev *sd,
+			   struct v4l2_subdev_state *state)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	struct alvium_mode *mode = &alvium->mode;
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+
+	struct v4l2_subdev_format sd_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_TRY,
+		.format = alvium_csi2_default_fmt,
+	};
+
+	struct v4l2_subdev_crop sd_crop = {
+		.which = V4L2_SUBDEV_FORMAT_TRY,
+		.rect = {
+			.left = mode->crop.left,
+			.top = mode->crop.top,
+			.width = mode->crop.width,
+			.height = mode->crop.height,
+		},
+	};
+
+	crop = v4l2_subdev_get_pad_crop(sd, state, 0);
+	fmt = v4l2_subdev_get_pad_format(sd, state, 0);
+
+	*crop = sd_crop.rect;
+	*fmt = sd_fmt.format;
+
+	return 0;
+}
+
+static int alvium_set_fmt(struct v4l2_subdev *sd,
+			   struct v4l2_subdev_state *sd_state,
+			   struct v4l2_subdev_format *format)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	const struct alvium_pixfmt *alvium_csi2_fmt;
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+
+	fmt = v4l2_subdev_get_pad_format(sd, sd_state, 0);
+	crop = v4l2_subdev_get_pad_crop(sd, sd_state, 0);
+
+	if (format->format.width <= alvium->img_max_width &&
+		  format->format.width >= alvium->img_min_width) {
+		fmt->width = format->format.width;
+	} else {
+		return -EINVAL;
+	}
+
+	if (format->format.height <= alvium->img_max_height &&
+		  format->format.height >= alvium->img_min_height) {
+		fmt->height = format->format.height;
+	} else {
+		return -EINVAL;
+	}
+
+	alvium_csi2_fmt = alvium_code_to_pixfmt(alvium, format->format.code);
+	fmt->code = alvium_csi2_fmt->code;
+
+	/* Set also the crop width and height when set a new fmt */
+	crop->width = fmt->width;
+	crop->height = fmt->height;
+
+	*fmt = format->format;
+
+	return 0;
+}
+
+static int alvium_set_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+
+	crop = v4l2_subdev_get_pad_crop(sd, sd_state, 0);
+	fmt = v4l2_subdev_get_pad_format(sd, sd_state, 0);
+
+	/*
+	 * Alvium can only shift the origin of the img
+	 * then we accept only value with the same value of the actual fmt
+	 */
+	if (sel->r.width != fmt->width ||
+		  sel->r.height != fmt->height)
+		return -EINVAL;
+
+	if (sel->target != V4L2_SEL_TGT_CROP)
+		return -EINVAL;
+
+	if ((sel->r.left + sel->r.width) <= alvium->img_max_width)
+		crop->left = sel->r.left;
+	else
+		return -EINVAL;
+
+	if ((sel->r.top + sel->r.height) <= alvium->img_max_height)
+		crop->top = sel->r.top;
+	else
+		return -EINVAL;
+
+	crop->width = sel->r.width;
+	crop->height = sel->r.height;
+
+	sel->r = *crop;
+
+	return 0;
+}
+
+static int alvium_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+
+	switch (sel->target) {
+	/* Current cropping area */
+	case V4L2_SEL_TGT_CROP:
+		sel->r = *v4l2_subdev_get_pad_crop(sd, sd_state, 0);
+		break;
+	/* Cropping bounds */
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = alvium->img_max_width;
+		sel->r.height = alvium->img_max_height;
+		break;
+	/* Default cropping area */
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = alvium->min_offy;
+		sel->r.left = alvium->min_offx;
+		sel->r.width = alvium->img_max_width;
+		sel->r.height = alvium->img_max_height;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int alvium_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int val;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUTOGAIN:
+		val = alvium_get_gain(alvium);
+		if (val < 0)
+			return val;
+		alvium->ctrls.gain->val = val;
+		break;
+	case V4L2_CID_EXPOSURE_AUTO:
+		val = alvium_get_exposure(alvium);
+		if (val < 0)
+			return val;
+		alvium->ctrls.exposure->val = val;
+		break;
+	}
+
+	return 0;
+}
+
+static int alvium_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int ret;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUTOGAIN:
+		ret = alvium_set_ctrl_gain(alvium, ctrl->val);
+		break;
+	case V4L2_CID_EXPOSURE_AUTO:
+		ret = alvium_set_ctrl_exposure(alvium, ctrl->val);
+		break;
+	case V4L2_CID_AUTO_WHITE_BALANCE:
+		ret = alvium_set_ctrl_white_balance(alvium, ctrl->val);
+		break;
+	case V4L2_CID_HUE:
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

drivers/media/i2c/alvium-csi2.c line 2977

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+		ret = alvium_set_ctrl_hue(alvium, ctrl->val);
+		break;
+	case V4L2_CID_CONTRAST:
+		ret = alvium_set_ctrl_contrast(alvium, ctrl->val);
+		break;
+	case V4L2_CID_SATURATION:
+		ret = alvium_set_ctrl_saturation(alvium, ctrl->val);
+		break;
+	case V4L2_CID_GAMMA:
+		ret = alvium_set_ctrl_gamma(alvium, ctrl->val);
[… more lines in the raw message]

No hsync or vsync controls?

drivers/media/i2c/alvium-csi2.c

Quoted 256 lines
drivers/media/i2c/alvium-csi2.c new file +256 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

It's not wrong to use OF nodes in general, but all APIs you're using require an fwnode instead. So please use fwnodes here.

	struct fwnode_handle *fwnode = dev_fwnode(dev);

This makes also of_* headers unnecessary, but please include linux/property.h.

drivers/media/i2c/alvium-csi2.c

Quoted 2 lines
drivers/media/i2c/alvium-csi2.c new file +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

No need to initialise ret.

drivers/media/i2c/alvium-csi2.c

Quoted 14 lines
drivers/media/i2c/alvium-csi2.c new file +14 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

As you have D-PHY, please assign bus type to D-PHY before parsing the endpoint.

drivers/media/i2c/alvium-csi2.c

Quoted 19 lines
drivers/media/i2c/alvium-csi2.c new file +19 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

The V4L2 fwnode framework prints the number of lanes already (with dynamic debug).

If you want to leave frequency print, then make it dev_dbg().

drivers/media/i2c/alvium-csi2.c

Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This seems useless and wrong: the regulator may be also used by another driver.

drivers/media/i2c/alvium-csi2.c

Quoted 7 lines
drivers/media/i2c/alvium-csi2.c new file +7 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Wow. That's the longest I've ever seen, by a large margin.

drivers/media/i2c/alvium-csi2.c

Quoted 15 lines
drivers/media/i2c/alvium-csi2.c new file +15 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Remove perhaps?

drivers/media/i2c/alvium-csi2.c

Quoted 95 lines
drivers/media/i2c/alvium-csi2.c new file +95 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Autosuspend is disabled by default.

What's the purpose of this? Leave the device powered on after probe?

drivers/media/i2c/alvium-csi2.c

Quoted 13 lines
drivers/media/i2c/alvium-csi2.c new file +13 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This looks like development time leftover, please remove.

drivers/media/i2c/alvium-csi2.h

Quoted 267 lines
drivers/media/i2c/alvium-csi2.c new file +59 -0

Lines of this file are not shown. View the raw message.

drivers/media/i2c/alvium-csi2.h new file +202 -0
new file mode 100644
index 000000000000..30b70e2ddb16

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Please use tabs instead.

drivers/media/i2c/alvium-csi2.h

Quoted 4 lines
drivers/media/i2c/alvium-csi2.h new file +4 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

And align the value to the same column.

drivers/media/i2c/alvium-csi2.h

Quoted 132 lines
drivers/media/i2c/alvium-csi2.h new file +132 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

I don't think C defines a particular location for the bit fields if their size is less than that of the type. Could you add padding to ensure that? Same for other structs.

drivers/media/i2c/alvium-csi2.h

Quoted 26 lines
drivers/media/i2c/alvium-csi2.h new file +26 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This would require less memory if you moved all 8-bit fields to the end of the struct.

Quoted 121 lines
drivers/media/i2c/alvium-csi2.h new file +121 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 3 lines
Kind regards,

Sakari Ailus

Part of the diff is not shown here. View the raw message.

author maintainer
#10 lore raw

Hi Sakar,

Thanks for your review.

Quoted 104 lines · Sakari Ailus

Hi Tommaso,

Thanks for the patch.

Quoted 99 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h
drivers/media/i2c/Kconfig +10 -0
index 256d55bb2b1d..a9fdf10ffc15 100644
@@ -34,6 +34,16 @@ config VIDEO_APTINA_PLL
 config VIDEO_CCS_PLL
 	tristate
 
+config VIDEO_ALVIUM_CSI2
+	tristate "Allied Vision ALVIUM MIPI CSI-2 camera support"
+	depends on I2C
+	select MEDIA_CONTROLLER
+	select VIDEO_V4L2_SUBDEV_API
+	select V4L2_FWNODE
+	help
+	  This is a Video4Linux2 sensor driver for the Allied Vision
+	  ALVIUM camera connected via MIPI CSI-2 interface.
+
 config VIDEO_AR0521
 	tristate "ON Semiconductor AR0521 sensor support"
 	depends on I2C && VIDEO_DEV
drivers/media/i2c/Makefile +1 -0
index b44dacf935f4..be4dddd6fd98 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_VIDEO_ADV7604) += adv7604.o
 obj-$(CONFIG_VIDEO_ADV7842) += adv7842.o
 obj-$(CONFIG_VIDEO_AK7375) += ak7375.o
 obj-$(CONFIG_VIDEO_AK881X) += ak881x.o
+obj-$(CONFIG_VIDEO_ALVIUM_CSI2) += alvium-csi2.o
 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
 obj-$(CONFIG_VIDEO_AR0521) += ar0521.o
 obj-$(CONFIG_VIDEO_BT819) += bt819.o
drivers/media/i2c/alvium-csi2.c new file +13 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Allied Vision Inc. Alvium camera driver
+ *
+ * Copyright (C) 2023 Tommaso Merciai
+ * Copyright (C) 2023 Martin Hecht
+ * Copyright (C) 2023 Avnet EMG GmbH
+ */
+
+#include <asm/unaligned.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/clkdev.h>
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 19

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+#include <linux/ctype.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/init.h>

Do you need this?

No, Thanks :)

Quoted 13 lines

drivers/media/i2c/alvium-csi2.c line 29

Quoted 11 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of_graph.h>
+#include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/mipi-csi2.h>

Please order alphabetically.

Oooks, ty

Quoted 22 lines
Quoted 22 lines
drivers/media/i2c/alvium-csi2.c new file +21 -0
+#include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#include "alvium-csi2.h"
+
+static const struct v4l2_mbus_framefmt alvium_csi2_default_fmt = {
+	.code = MEDIA_BUS_FMT_UYVY8_1X16,
+	.width = 640,
+	.height = 480,
+	.colorspace = V4L2_COLORSPACE_SRGB,
+	.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
+	.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.field = V4L2_FIELD_NONE,
+};
+
+static const struct alvium_pixfmt alvium_csi2_fmts[] = {
+	{
+		/* UYVY8_2X8 */
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 56

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+		.id = ALVIUM_FMT_UYVY8_2X8,
+		.code = MEDIA_BUS_FMT_UYVY8_2X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,

Could you use MIPI_CSI2_DT_ definitions in mipi-csi2.h?

ofc ty

Quoted 273 lines
Quoted 273 lines
drivers/media/i2c/alvium-csi2.c new file +272 -0
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* UYVY8_1X16 */
+		.id = ALVIUM_FMT_UYVY8_1X16,
+		.code = MEDIA_BUS_FMT_UYVY8_1X16,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* YUYV8_1X16 */
+		.id = ALVIUM_FMT_YUYV8_1X16,
+		.code = MEDIA_BUS_FMT_YUYV8_1X16,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* YUYV8_2X8 */
+		.id = ALVIUM_FMT_YUYV8_2X8,
+		.code = MEDIA_BUS_FMT_YUYV8_2X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* YUYV10_1X20 */
+		.id = ALVIUM_FMT_YUYV10_1X20,
+		.code = MEDIA_BUS_FMT_YUYV10_1X20,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_10,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1F,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* RGB888_1X24 */
+		.id = ALVIUM_FMT_RGB888_1X24,
+		.code = MEDIA_BUS_FMT_RGB888_1X24,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* RBG888_1X24 */
+		.id = ALVIUM_FMT_RBG888_1X24,
+		.code = MEDIA_BUS_FMT_RBG888_1X24,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* BGR888_1X24 */
+		.id = ALVIUM_FMT_BGR888_1X24,
+		.code = MEDIA_BUS_FMT_BGR888_1X24,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* RGB888_3X8 */
+		.id = ALVIUM_FMT_RGB888_3X8,
+		.code = MEDIA_BUS_FMT_RGB888_3X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_RGB888,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x24,
+		.bay_fmt_regval = -1,
+		.is_raw = 0,
+	}, {
+		/* Y8_1X8 */
+		.id = ALVIUM_FMT_Y8_1X8,
+		.code = MEDIA_BUS_FMT_Y8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_MONO,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x00,
+		.is_raw = 1,
+	}, {
+		/* SGRBG8_1X8 */
+		.id = ALVIUM_FMT_SGRBG8_1X8,
+		.code = MEDIA_BUS_FMT_SGRBG8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SRGGB8_1X8 */
+		.id = ALVIUM_FMT_SRGGB8_1X8,
+		.code = MEDIA_BUS_FMT_SRGGB8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SGBRG8_1X8 */
+		.id = ALVIUM_FMT_SGBRG8_1X8,
+		.code = MEDIA_BUS_FMT_SGBRG8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SBGGR8_1X8 */
+		.id = ALVIUM_FMT_SBGGR8_1X8,
+		.code = MEDIA_BUS_FMT_SBGGR8_1X8,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW8,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2A,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	}, {
+		/* Y10_1X10 */
+		.id = ALVIUM_FMT_Y10_1X10,
+		.code = MEDIA_BUS_FMT_Y10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_MONO,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x00,
+		.is_raw = 1,
+	}, {
+		/* SGRBG10_1X10 */
+		.id = ALVIUM_FMT_SGRBG10_1X10,
+		.code = MEDIA_BUS_FMT_SGRBG10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SRGGB10_1X10 */
+		.id = ALVIUM_FMT_SRGGB10_1X10,
+		.code = MEDIA_BUS_FMT_SRGGB10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SGBRG10_1X10 */
+		.id = ALVIUM_FMT_SGBRG10_1X10,
+		.code = MEDIA_BUS_FMT_SGBRG10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SBGGR10_1X10 */
+		.id = ALVIUM_FMT_SBGGR10_1X10,
+		.code = MEDIA_BUS_FMT_SBGGR10_1X10,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW10,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2B,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	}, {
+		/* Y12_1X12 */
+		.id = ALVIUM_FMT_Y12_1X12,
+		.code = MEDIA_BUS_FMT_Y12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_MONO,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x00,
+		.is_raw = 1,
+	}, {
+		/* SGRBG12_1X12 */
+		.id = ALVIUM_FMT_SGRBG12_1X12,
+		.code = MEDIA_BUS_FMT_SGRBG12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SRGGB12_1X12 */
+		.id = ALVIUM_FMT_SRGGB12_1X12,
+		.code = MEDIA_BUS_FMT_SRGGB12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SGBRG12_1X12 */
+		.id = ALVIUM_FMT_SGBRG12_1X12,
+		.code = MEDIA_BUS_FMT_SGBRG12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SBGGR12_1X12 */
+		.id = ALVIUM_FMT_SBGGR12_1X12,
+		.code = MEDIA_BUS_FMT_SBGGR12_1X12,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW12,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2C,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
+	}, {
+		/* SBGGR14_1X14 */
+		.id = ALVIUM_FMT_SBGGR14_1X14,
+		.code = MEDIA_BUS_FMT_SBGGR14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_GR,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x01,
+		.is_raw = 1,
+	}, {
+		/* SGBRG14_1X14 */
+		.id = ALVIUM_FMT_SGBRG14_1X14,
+		.code = MEDIA_BUS_FMT_SGBRG14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_RG,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x02,
+		.is_raw = 1,
+	}, {
+		/* SRGGB14_1X14 */
+		.id = ALVIUM_FMT_SRGGB14_1X14,
+		.code = MEDIA_BUS_FMT_SRGGB14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_GB,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x03,
+		.is_raw = 1,
+	}, {
+		/* SGRBG14_1X14 */
+		.id = ALVIUM_FMT_SGRBG14_1X14,
+		.code = MEDIA_BUS_FMT_SGRBG14_1X14,
+		.colorspace = V4L2_COLORSPACE_RAW,
+		.fmt_av_bit = ALVIUM_BIT_RAW14,
+		.bay_av_bit = ALVIUM_BIT_BAY_BG,
+		.mipi_fmt_regval = 0x2D,
+		.bay_fmt_regval = 0x04,
+		.is_raw = 1,
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 334

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+	},
+	{ /* sentinel */ }
+};
+
+static int alvium_read(struct alvium_dev *alvium,
+				  u32 reg, u64 *val)

Fits on a single line.

Ook

Quoted 29 lines
Quoted 29 lines
drivers/media/i2c/alvium-csi2.c new file +28 -0
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	struct i2c_msg msgs[2] = {0};
+	u8 addr_buf[2] = {0};
+	u8 data_buf[8] = {0};
+	int ret;
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, addr_buf);
+
+	msgs[0].addr = client->addr;
+	msgs[0].flags = 0;
+	msgs[0].len = ARRAY_SIZE(addr_buf);
+	msgs[0].buf = addr_buf;
+
+	msgs[1].addr = client->addr;
+	msgs[1].flags = I2C_M_RD;
+	msgs[1].len = len;
+	msgs[1].buf = &data_buf[8 - len];
+
+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 366

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret != ARRAY_SIZE(msgs))
+		return -EIO;
+
+	*val = get_unaligned_be64(data_buf);

Could you use CCI wrapper Hans has been working on? It'll be merged fairly soon I presume:
<URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

Thanks for sharing.

Actually I'm working/testing the driver on 6.4.0-rc2 I don't see this new functionality, sry.

I can try.
But this is only for CCI regs read/write right?

Or also for v4l2 regs?
Let me know, thanks! :)

Quoted 17 lines
Quoted 17 lines
drivers/media/i2c/alvium-csi2.c new file +16 -0
+
+	return 0;
+}
+
+static int alvium_write(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	u8 buf[10] = {0};
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 388

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, buf);
+	put_unaligned_be64(val << (8 * (8 - len)), buf + 2);
+	if (i2c_master_send(client, buf, len + 2) != len + 2)
+		return -EIO;

Same here.

^Same question.

Quoted 99 lines
Quoted 99 lines
drivers/media/i2c/alvium-csi2.c new file +98 -0
+
+	return 0;
+}
+
+static int alvium_write_hshake(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 hshake_regval;
+	u8 hshake_bit;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/* reset handshake bit */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* write alvium reg*/
+	ret = alvium_write(alvium, reg, val);
+	if (ret) {
+		dev_err(dev, "Fail to write reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 1*/
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (!hshake_bit);
+
+	/* reset handshake bit, write 0 to bit0 */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 0 */
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (hshake_bit);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_vers(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_bcrm_vers *v;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	v = (struct alvium_bcrm_vers *) &val;
+
+	v->minor = cpu_to_le16(v->minor);
+	v->major = cpu_to_le16(v->major);
+
+	dev_info(dev, "bcrm version: %u.%u\n", v->minor,
+			  v->major);
+
+	return 0;
+}
+
+static int alvium_get_fw_version(struct alvium_dev *alvium)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 491

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_fw_vers *fw_v;

Please order temporary, loop etc. variables as last. Same for the other functions. Reverse Christmas tree order is often the same.

Can you explain me better pls?
Your plan is the following order:

struct device *dev = &alvium->i2c_client->dev;
struct alvium_fw_vers *fw_v;
u64 val;
int ret;

or I'm wrong? Thanks.

Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +11 -0
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_DEVICE_FIRMWARE_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read fw version reg\n");
+		return ret;
+	}
+
+	fw_v = (struct alvium_fw_vers *) &val;
+
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 507

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	fw_v->minor = cpu_to_le16(fw_v->minor);
+	fw_v->patch = cpu_to_le32(fw_v->patch);
+
+	dev_info(dev, "fw version: %u.%u.%u.%u\n",
+			  fw_v->special,

Please align arguments to just right of the opening parenthesis. Also, more fits on the same line (up to 80 chars). This applies to the entire patch.

dev_info(dev, "fw version: %u.%u.%u.%u\n", fw_v->special, fw_v->major,

	 fw_v->minor, fw_v->patch);

Like this?

Quoted 52 lines
Quoted 52 lines
drivers/media/i2c/alvium-csi2.c new file +51 -0
+			  fw_v->major,
+			  fw_v->minor,
+			  fw_v->patch);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_addr(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_REG_ADDR_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm addr reg\n");
+		return ret;
+	}
+
+	alvium->bcrm_addr = (u16)val;
+	dev_dbg(dev, "bcrm addr: %d\n", alvium->bcrm_addr);
+
+	return 0;
+}
+
+static bool alvium_is_alive(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val = 0;
+	u8 hbeat_val = 0;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HEARTBEAT_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read heartbeat reg\n");
+		return false;
+	}
+
+	hbeat_val = (u8)val;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 564

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+	dev_dbg(dev, "hearthbeat = %d", hbeat_val);
+
+	if (hbeat_val)
+		return true;
+	else
+		return false;

return hbeat_val;

Oks.

Quoted 149 lines
Quoted 136 lines
drivers/media/i2c/alvium-csi2.c new file +135 -0
+}
+
+static void alvium_print_avail_mipi_fmt(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_leg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_LEG]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_10: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_10]);
+	dev_dbg(dev, "avail mipi_fmt rgb888: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB888]);
+	dev_dbg(dev, "avail mipi_fmt rgb666: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB666]);
+	dev_dbg(dev, "avail mipi_fmt rgb565: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB565]);
+	dev_dbg(dev, "avail mipi_fmt rgb555: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB555]);
+	dev_dbg(dev, "avail mipi_fmt rgb444: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB444]);
+	dev_dbg(dev, "avail mipi_fmt raw6: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW6]);
+	dev_dbg(dev, "avail mipi_fmt raw7: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW7]);
+	dev_dbg(dev, "avail mipi_fmt raw8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW8]);
+	dev_dbg(dev, "avail mipi_fmt raw10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW10]);
+	dev_dbg(dev, "avail mipi_fmt raw12: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW12]);
+	dev_dbg(dev, "avail mipi_fmt raw14: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW14]);
+	dev_dbg(dev, "avail mipi_fmt jpeg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_JPEG]);
+}
+
+static void alvium_print_avail_feat(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "feature rev_x: %u\n",
+		  alvium->avail_ft.rev_x);
+	dev_dbg(dev, "feature rev_y: %u\n",
+		  alvium->avail_ft.rev_y);
+	dev_dbg(dev, "feature int_autop: %u\n",
+		  alvium->avail_ft.int_autop);
+	dev_dbg(dev, "feature black_lvl: %u\n",
+		  alvium->avail_ft.black_lvl);
+	dev_dbg(dev, "feature gain: %u\n",
+		  alvium->avail_ft.gain);
+	dev_dbg(dev, "feature gamma: %u\n",
+		  alvium->avail_ft.gamma);
+	dev_dbg(dev, "feature contrast: %u\n",
+		  alvium->avail_ft.contrast);
+	dev_dbg(dev, "feature sat: %u\n",
+		  alvium->avail_ft.sat);
+	dev_dbg(dev, "feature hue: %u\n",
+		  alvium->avail_ft.hue);
+	dev_dbg(dev, "feature whiteb: %u\n",
+		  alvium->avail_ft.whiteb);
+	dev_dbg(dev, "feature sharp: %u\n",
+		  alvium->avail_ft.sharp);
+	dev_dbg(dev, "feature auto_exp: %u\n",
+		  alvium->avail_ft.auto_exp);
+	dev_dbg(dev, "feature auto_gain: %u\n",
+		  alvium->avail_ft.auto_gain);
+	dev_dbg(dev, "feature auto_whiteb: %u\n",
+		  alvium->avail_ft.auto_whiteb);
+	dev_dbg(dev, "feature dev_temp: %u\n",
+		  alvium->avail_ft.dev_temp);
+	dev_dbg(dev, "feature acq_abort: %u\n",
+		  alvium->avail_ft.acq_abort);
+	dev_dbg(dev, "feature acq_fr: %u\n",
+		  alvium->avail_ft.acq_fr);
+	dev_dbg(dev, "feature fr_trigger: %u\n",
+		  alvium->avail_ft.fr_trigger);
+	dev_dbg(dev, "feature exp_acq_line: %u\n",
+		  alvium->avail_ft.exp_acq_line);
+}
+
+static void alvium_print_avail_bayer(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail bayer mono: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_MONO]);
+	dev_dbg(dev, "avail bayer gr: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GR]);
+	dev_dbg(dev, "avail bayer rg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_RG]);
+	dev_dbg(dev, "avail bayer gb: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GB]);
+	dev_dbg(dev, "avail bayer bg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_BG]);
+}
+
+static int alvium_get_feat_inq(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_feat *f;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_FEATURE_INQUIRY_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to features inquiry reg\n");
+		return ret;
+	}
+
+	f = (struct alvium_avail_feat *) &val;
+
+	alvium->avail_ft.rev_x = f->rev_x;
+	alvium->avail_ft.rev_y = f->rev_y;
+	alvium->avail_ft.int_autop = f->int_autop;
+	alvium->avail_ft.black_lvl = f->black_lvl;
+	alvium->avail_ft.gain = f->gain;
+	alvium->avail_ft.gamma = f->gamma;
+	alvium->avail_ft.contrast = f->contrast;
+	alvium->avail_ft.sat = f->sat;
+	alvium->avail_ft.hue = f->rev_y;

drivers/media/i2c/alvium-csi2.c line 709

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	alvium->avail_ft.whiteb = f->whiteb;
+	alvium->avail_ft.sharp = f->sharp;
+	alvium->avail_ft.auto_exp = f->auto_exp;
+	alvium->avail_ft.auto_gain = f->auto_gain;
+	alvium->avail_ft.auto_whiteb = f->auto_whiteb;
+	alvium->avail_ft.dev_temp = f->dev_temp;
+	alvium->avail_ft.acq_abort = f->acq_abort;
+	alvium->avail_ft.acq_fr = f->acq_fr;
+	alvium->avail_ft.fr_trigger = f->fr_trigger;
+	alvium->avail_ft.exp_acq_line = f->exp_acq_line;

You could instead do:

Quoted 4 lines
	alvium->avail_ft = *f;

But is there an endianness issue here? I think you should use u64 also for the struct fields.

Mmmm.. I'm not sure are all u8 this is not safe (on endianess pesrpective)? Or I'm completely wrong?

Quoted 170 lines
Quoted 170 lines
drivers/media/i2c/alvium-csi2.c new file +169 -0
+
+	alvium_print_avail_feat(alvium);
+
+	return 0;
+}
+
+static int alvium_get_host_supp_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_LANE_COUNT_RW,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->h_sup_csi_lanes = (u8)val;
+	dev_dbg(dev, "host supported csi2 lanes count: %d\n",
+			  alvium->h_sup_csi_lanes);
+
+	return 0;
+}
+
+static int alvium_set_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 num_lanes;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	num_lanes = alvium->ep.bus.mipi_csi2.num_data_lanes;
+
+	if (num_lanes <= alvium->h_sup_csi_lanes) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_LANE_COUNT_RW,
+				  num_lanes);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "Set n lanes = %llu", num_lanes);
+
+	return 0;
+}
+
+static int alvium_set_lp2hs_delay(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 lp2hs_delay;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/*
+	 * The purpose of this reg is force a DPhy reset
+	 * for the period described by the microseconds on
+	 * the reg, before it starts streaming.
+	 *
+	 * To be clear, with that value bigger than 0 the
+	 * Alvium forces a dphy-reset on all lanes for that period.
+	 * That means all lanes go up into low power state.
+	 *
+	 * This may help a csi2 rx ip to reset if that IP
+	 * can't deal with a continous clock.
+	 */
+	lp2hs_delay = (u64)alvium->lp2hs_delay;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_STREAM_ON_DELAY_RW,
+			  lp2hs_delay);
+	if (ret) {
+		dev_err(dev, "Fail to set streamon delay reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_csi_clk_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->min_csi_clk = (u32)val;
+	dev_dbg(dev, "Min host supported csi clock: %d\n",
+			  alvium->min_csi_clk);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->max_csi_clk = (u32)val;
+	dev_dbg(dev, "Max host supported csi clock: %d\n",
+			  alvium->max_csi_clk);
+
+	return 0;
+}
+
+static int alvium_set_csi_clk(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 csi_clk;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	csi_clk = (u32)alvium->ep.link_frequencies[0];
+
+	if (csi_clk > alvium->min_csi_clk ||
+		csi_clk < alvium->max_csi_clk) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_CLOCK_RW,
+				  csi_clk);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	alvium->link_freq = alvium->ep.link_frequencies[0];
+
+	return 0;
+}
+
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 882

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

I don't get this comment.
Can you explain me better your plan please.

Thanks!

Quoted 1,738 lines
Quoted 1,738 lines
drivers/media/i2c/alvium-csi2.c new file +1737 -0
+
+	alvium->img_min_width = (u32)val;
+	dev_dbg(dev, "Min img width: %d\n",
+			  alvium->img_min_width);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img max width reg\n");
+		return ret;
+	}
+
+	alvium->img_max_width = (u32)val;
+	dev_dbg(dev, "Max img width: %d\n",
+			  alvium->img_max_width);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img inc width reg\n");
+		return ret;
+	}
+
+	alvium->img_inc_width = (u32)val;
+	dev_dbg(dev, "img width increment: %d px\n",
+			  alvium->img_inc_width);
+
+	return 0;
+}
+
+static int alvium_get_img_height_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_HEIGHT_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img min height reg\n");
+		return ret;
+	}
+
+	alvium->img_min_height = (u32)val;
+	dev_dbg(dev, "Min img height: %d\n",
+			  alvium->img_min_height);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_HEIGHT_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img max height reg\n");
+		return ret;
+	}
+
+	alvium->img_max_height = (u32)val;
+	dev_dbg(dev, "Max img height: %d\n",
+			  alvium->img_max_height);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_HEIGHT_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img inc height reg\n");
+		return ret;
+	}
+
+	alvium->img_inc_height = (u32)val;
+	dev_dbg(dev, "img height increment: %d\n",
+			  alvium->img_inc_height);
+
+	return 0;
+}
+
+static int alvium_set_img_width(struct alvium_dev *alvium, u32 width)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_WIDTH_RW,
+			  width);
+	if (ret) {
+		dev_err(dev, "Fail to set img width\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_img_height(struct alvium_dev *alvium, u32 height)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_HEIGHT_RW,
+			  height);
+	if (ret) {
+		dev_err(dev, "Fail to set img height\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_img_offx(struct alvium_dev *alvium, u32 offx)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_OFFSET_X_RW,
+			  offx);
+	if (ret) {
+		dev_err(dev, "Fail to set img offx\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_img_offy(struct alvium_dev *alvium, u32 offy)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_RW,
+			  offy);
+	if (ret) {
+		dev_err(dev, "Fail to set img offy\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_offx_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_X_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min offx reg\n");
+		return ret;
+	}
+
+	alvium->min_offx = (u32)val;
+	dev_dbg(dev, "Min img offx: %d\n",
+			  alvium->min_offx);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_X_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max offx reg\n");
+		return ret;
+	}
+
+	alvium->max_offx = (u32)val;
+	dev_dbg(dev, "Max img offx: %d\n",
+			  alvium->max_offx);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_X_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc offx reg\n");
+		return ret;
+	}
+
+	alvium->inc_offx = (u32)val;
+	dev_dbg(dev, "Inc img offx: %d\n",
+			  alvium->inc_offx);
+
+	return 0;
+}
+
+static int alvium_get_offy_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min offy reg\n");
+		return ret;
+	}
+
+	alvium->min_offy = (u32)val;
+	dev_dbg(dev, "Min img offy: %d\n",
+			  alvium->min_offy);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max offy reg\n");
+		return ret;
+	}
+
+	alvium->max_offy = (u32)val;
+	dev_dbg(dev, "Max img offy: %d\n",
+			  alvium->max_offy);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_OFFSET_Y_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc offy reg\n");
+		return ret;
+	}
+
+	alvium->inc_offy = (u32)val;
+	dev_dbg(dev, "Inc img offy: %d\n",
+				  alvium->inc_offy);
+
+	return 0;
+}
+
+static int alvium_get_gain_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to min gain reg\n");
+		return ret;
+	}
+
+	alvium->min_gain = val;
+	dev_dbg(dev, "Min gain: %llu mB\n",
+			  alvium->min_gain);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to max gain reg\n");
+		return ret;
+	}
+
+	alvium->max_gain = val;
+	dev_dbg(dev, "Max gain: %llu mB\n",
+			  alvium->max_gain);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to inc gain reg\n");
+		return ret;
+	}
+
+	alvium->inc_gain = val;
+	dev_dbg(dev, "gain inc: %llu mB\n",
+			  alvium->inc_gain);
+
+	return 0;
+}
+
+static int alvium_get_exposure_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img min exp reg\n");
+		return ret;
+	}
+
+	alvium->min_exp = val;
+	dev_dbg(dev, "Min exp: %llu ns\n",
+			  alvium->min_exp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img max exp reg\n");
+		return ret;
+	}
+
+	alvium->max_exp = val;
+	dev_dbg(dev, "Max exp: %llu ns\n",
+			  alvium->max_exp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc exp reg\n");
+		return ret;
+	}
+
+	alvium->inc_exp = val;
+	dev_dbg(dev, "Inc exp: %llu ns\n",
+			  alvium->inc_exp);
+
+	return 0;
+}
+
+static int alvium_get_red_balance_ratio_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min rbalance reg\n");
+		return ret;
+	}
+
+	alvium->min_rbalance = val;
+	dev_dbg(dev, "Min rbalance: %llu\n",
+			  alvium->min_rbalance);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max rbalance reg\n");
+		return ret;
+	}
+
+	alvium->max_rbalance = val;
+	dev_dbg(dev, "Max rbalance: %llu\n",
+			  alvium->max_rbalance);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc rbalance reg\n");
+		return ret;
+	}
+
+	alvium->inc_rbalance = val;
+	dev_dbg(dev, "Inc rbalance: %llu\n",
+			  alvium->inc_rbalance);
+
+	return 0;
+}
+
+static int alvium_get_blue_balance_ratio_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLUE_BALANCE_RATIO_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min bbalance reg\n");
+		return ret;
+	}
+
+	alvium->min_bbalance = val;
+	dev_dbg(dev, "Min bbalance: %llu\n",
+				  alvium->min_bbalance);
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_BLUE_BALANCE_RATIO_MAX_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max bbalance reg\n");
+		return ret;
+	}
+
+	alvium->max_bbalance = val;
+	dev_dbg(dev, "Max bbalance: %llu\n",
+			  alvium->max_bbalance);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLUE_BALANCE_RATIO_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc bbalance reg\n");
+		return ret;
+	}
+
+	alvium->inc_bbalance = val;
+	dev_dbg(dev, "Inc bbalance: %llu\n",
+			  alvium->inc_bbalance);
+
+	return 0;
+}
+
+static int alvium_get_hue_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HUE_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min hue reg\n");
+		return ret;
+	}
+
+	alvium->min_hue = (s32)val;
+	dev_dbg(dev, "Min hue: %d\n",
+			  alvium->min_hue);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HUE_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max hue reg\n");
+		return ret;
+	}
+
+	alvium->max_hue = (s32)val;
+	dev_dbg(dev, "Max hue: %d\n",
+			  alvium->max_hue);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HUE_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc hue reg\n");
+		return ret;
+	}
+
+	alvium->inc_hue = (s32)val;
+	dev_dbg(dev, "Inc hue: %d\n",
+			  alvium->inc_hue);
+
+	return 0;
+}
+
+static int alvium_get_black_lvl_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min black lvl reg\n");
+		return ret;
+	}
+
+	alvium->min_black_lvl = (s32)val;
+	dev_dbg(dev, "Min black lvl: %d\n",
+			  alvium->min_hue);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max black lvl reg\n");
+		return ret;
+	}
+
+	alvium->max_black_lvl = (s32)val;
+	dev_dbg(dev, "Max black lvl: %d\n",
+			  alvium->max_black_lvl);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc black lvl reg\n");
+		return ret;
+	}
+
+	alvium->inc_black_lvl = (s32)val;
+	dev_dbg(dev, "Inc black lvl: %d\n",
+			  alvium->inc_black_lvl);
+
+	return 0;
+}
+
+static int alvium_get_gamma_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_GAMMA_MIN_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min gamma reg\n");
+		return ret;
+	}
+
+	alvium->min_gamma = val;
+	dev_dbg(dev, "Min gamma: %llu\n",
+			  alvium->min_gamma);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAMMA_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max bbalance reg\n");
+		return ret;
+	}
+
+	alvium->max_gamma = val;
+	dev_dbg(dev, "Max gamma: %llu\n",
+			  alvium->max_gamma);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAMMA_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc gamma reg\n");
+		return ret;
+	}
+
+	alvium->inc_gamma = val;
+	dev_dbg(dev, "Inc gamma: %llu\n",
+			  alvium->inc_gamma);
+
+	return 0;
+}
+
+static int alvium_get_sharpness_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_SHARPNESS_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min sharpness lvl reg\n");
+		return ret;
+	}
+
+	alvium->min_sharp = (s32)val;
+	dev_dbg(dev, "Min sharpness: %d\n",
+			  alvium->min_sharp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_BLACK_LEVEL_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max sharpness reg\n");
+		return ret;
+	}
+
+	alvium->max_sharp = (s32)val;
+	dev_dbg(dev, "Max sharpness: %d\n",
+			  alvium->max_sharp);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_SHARPNESS_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc sharpness reg\n");
+		return ret;
+	}
+
+	alvium->inc_sharp = (s32)val;
+	dev_dbg(dev, "Inc sharpness: %d\n",
+			  alvium->inc_sharp);
+
+	return 0;
+}
+
+static int alvium_get_contrast_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CONTRAST_VALUE_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min contrast reg\n");
+		return ret;
+	}
+
+	alvium->min_contrast = (u32)val;
+	dev_dbg(dev, "Min contrast: %d\n",
+			  alvium->min_contrast);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CONTRAST_VALUE_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max contrast reg\n");
+		return ret;
+	}
+
+	alvium->max_contrast = (u32)val;
+	dev_dbg(dev, "Max contrast: %d\n",
+			  alvium->max_contrast);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CONTRAST_VALUE_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read inc contrast reg\n");
+		return ret;
+	}
+
+	alvium->inc_contrast = (u32)val;
+	dev_dbg(dev, "Inc contrast: %d\n",
+			  alvium->inc_contrast);
+
+	return 0;
+}
+
+static int alvium_get_saturation_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_SATURATION_MIN_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min saturation reg\n");
+		return ret;
+	}
+
+	alvium->min_sat = (u32)val;
+	dev_dbg(dev, "Min saturation: %d\n",
+			  alvium->min_sat);
+
+	ret = alvium_read(alvium,
+				  REG_BCRM_SATURATION_MAX_R,
+				  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max saturation reg\n");
+		return ret;
+	}
+
+	alvium->max_sat = (u32)val;
+	dev_dbg(dev, "Max saturation: %d\n",
+			  alvium->max_sat);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_SATURATION_INC_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max saturation reg\n");
+		return ret;
+	}
+
+	alvium->inc_sat = (u32)val;
+	dev_dbg(dev, "Inc saturation: %d\n",
+			  alvium->inc_sat);
+
+	return 0;
+}
+
+static int alvium_set_bcm_mode(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_write(alvium,
+			  REG_GENCP_CHANGEMODE_W,
+			  ALVIUM_BCM_MODE);
+	if (ret) {
+		dev_err(dev, "Fail to current mode reg\n");
+		return ret;
+	}
+
+	alvium->bcrm_mode = ALVIUM_BCM_MODE;
+	dev_dbg(dev, "Set BCM mode");
+
+	return 0;
+}
+
+static int alvium_get_mode(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 bcrm_mode;
+
+	ret = alvium_read(alvium,
+			  REG_GENCP_CURRENTMODE_R,
+			  &bcrm_mode);
+	if (ret) {
+		dev_err(dev, "Fail to read current mode reg\n");
+		return ret;
+	}
+
+	switch (bcrm_mode) {
+	case ALVIUM_BCM_MODE:
+		alvium->bcrm_mode = ALVIUM_BCM_MODE;
+		break;
+	case ALVIUM_GENCP_MODE:
+		alvium->bcrm_mode = ALVIUM_GENCP_MODE;
+		break;
+	}
+
+	dev_dbg(dev, "Current mode is: %d\n", alvium->bcrm_mode);
+	return 0;
+}
+
+static int alvium_get_avail_mipi_data_format(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_mipi_fmt *avail_fmt;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_AVAILABLE_MIPI_DATA_FORMATS_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "available mipi data format reg\n");
+		return ret;
+	}
+
+	avail_fmt = (struct alvium_avail_mipi_fmt *) &val;
+
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_LEG]
+				  = avail_fmt->yuv420_8_leg;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8]
+				  = avail_fmt->yuv420_8;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10]
+				  = avail_fmt->yuv420_10;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_CSPS]
+				  = avail_fmt->yuv420_8_csps;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10_CSPS]
+				  = avail_fmt->yuv420_10_csps;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_8]
+				  = avail_fmt->yuv422_8;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_10]
+				  = avail_fmt->yuv422_10;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB888]
+				  = avail_fmt->rgb888;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB666]
+				  = avail_fmt->rgb666;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB565]
+				  = avail_fmt->rgb565;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB555]
+				  = avail_fmt->rgb555;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB444]
+				  = avail_fmt->rgb444;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW6]
+				  = avail_fmt->raw6;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW7]
+				  = avail_fmt->raw7;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW8]
+				  = avail_fmt->raw8;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW10]
+				  = avail_fmt->raw10;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW12]
+				  = avail_fmt->raw12;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW14]
+				  = avail_fmt->raw14;
+	alvium->is_mipi_fmt_avail[ALVIUM_BIT_JPEG]
+				  = avail_fmt->jpeg;
+
+	alvium_print_avail_mipi_fmt(alvium);
+
+	return 0;
+}
+
+static int alvium_setup_mipi_fmt(struct alvium_dev *alvium)
+{
+	int avail_fmt_cnt = 0;
+	int sz = 0;
+
+	alvium->alvium_csi2_fmt = NULL;
+
+	/* calculate fmt array size */
+	for (int fmt = 0; fmt < ALVIUM_NUM_SUPP_MIPI_DATA_FMT; fmt++) {
+		if (alvium->is_mipi_fmt_avail[alvium_csi2_fmts[fmt].fmt_av_bit])
+			if ((!alvium_csi2_fmts[fmt].is_raw) ||
+				  (alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit]))
+				sz++;
+	}
+
+	/* init alvium_csi2_fmt array */
+	alvium->alvium_csi2_fmt_n = sz;
+	alvium->alvium_csi2_fmt = kmalloc_array(sz,
+						     sizeof(struct alvium_pixfmt),
+						     GFP_KERNEL);
+
+	/* Create the alvium_csi2 fmt array from formats available */
+	for (int fmt = 0; fmt < ALVIUM_NUM_SUPP_MIPI_DATA_FMT; fmt++) {
+		if (alvium->is_mipi_fmt_avail[alvium_csi2_fmts[fmt].fmt_av_bit])
+			if ((!alvium_csi2_fmts[fmt].is_raw) ||
+				  (alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit])) {
+				alvium->alvium_csi2_fmt[avail_fmt_cnt] =
+						alvium_csi2_fmts[fmt];
+				avail_fmt_cnt++;
+			}
+	}
+
+	return 0;
+}
+
+static int alvium_set_mipi_fmt(struct alvium_dev *alvium,
+				  const struct alvium_pixfmt *pixfmt)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 reg_val;
+	int ret;
+
+	reg_val = pixfmt->mipi_fmt_regval;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_MIPI_DATA_FORMAT_RW,
+			  reg_val);
+	if (ret) {
+		dev_err(dev, "Fail to set mipi fmt\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_avail_bayer(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_bayer *avail_bay;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_BAYER_PATTERN_INQUIRY_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read available Bayer patterns reg\n");
+		return ret;
+	}
+
+	avail_bay = (struct alvium_avail_bayer *) &val;
+
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_MONO] = avail_bay->mono;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_GR] = avail_bay->gr;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_RG] = avail_bay->rg;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_GB] = avail_bay->gb;
+	alvium->is_bay_avail[ALVIUM_BIT_BAY_BG] = avail_bay->bg;
+
+	alvium_print_avail_bayer(alvium);
+
+	return 0;
+}
+
+static int alvium_set_bayer_pattern(struct alvium_dev *alvium,
+				  const struct alvium_pixfmt *pixfmt)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 reg_val;
+	int ret;
+
+	reg_val = pixfmt->bay_fmt_regval;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_BAYER_PATTERN_RW,
+			  reg_val);
+	if (ret) {
+		dev_err(dev, "Fail to set bayer pattern\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_frame_interval(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read min frame rate reg\n");
+		return ret;
+	}
+
+	alvium->min_fr = val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read max frame rate reg\n");
+		return ret;
+	}
+
+	alvium->max_fr = val;
+
+	dev_dbg(dev, "framerate min: %llu µHz, max: %llu µHz\n",
+			  alvium->min_fr, alvium->max_fr);
+	return 0;
+}
+
+static int alvium_set_frame_rate(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 fr;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	fr = alvium->fr;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_ACQUISITION_FRAME_RATE_RW,
+			  fr);
+	if (ret) {
+		dev_err(dev, "Fail to set frame rate lanes reg\n");
+		return ret;
+	}
+
+	dev_dbg(dev, "set frame rate: %llu us\n", fr);
+
+	return 0;
+}
+
+static int alvium_set_stream_mipi(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_ACQUISITION_START_RW,
+			  0x01);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_ACQUISITION_STOP_RW,
+			  0x01);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail set_stream_mipi\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_gain(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 gain;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_gain)
+		return -EINVAL;
+
+	/* The unit is millibel (1 mB = 0.01 dB) */
+	ret = alvium_read(alvium,
+			  REG_BCRM_GAIN_RW,
+			  &gain);
+	if (ret) {
+		dev_err(dev, "Fail to read gain value reg\n");
+		return ret;
+	}
+
+	return gain;
+}
+
+static int alvium_set_gain(struct alvium_dev *alvium, int gain)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_gain)
+		return -EINVAL;
+
+	/* The unit is millibel (1 mB = 0.01 dB) */
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAIN_RW,
+			  (u64)gain);
+	if (ret) {
+		dev_err(dev, "Fail to set gain value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_autogain(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_gain)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAIN_AUTO_RW,
+			  0x02);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAIN_AUTO_RW,
+			  0x00);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail to set autogain reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_exposure(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 exp;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_exp)
+		return -EINVAL;
+
+	/* Exposure time in ns */
+	ret = alvium_read(alvium,
+			  REG_BCRM_EXPOSURE_TIME_RW,
+			  &exp);
+	if (ret) {
+		dev_err(dev, "Fail to read exposure value reg\n");
+		return ret;
+	}
+
+	return exp;
+}
+
+static int alvium_set_autoexposure(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_exp)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_EXPOSURE_AUTO_RW,
+			  0x02);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_EXPOSURE_AUTO_RW,
+			  0x00);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail to set autoexposure reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_exposure(struct alvium_dev *alvium, int exposure_ns)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_EXPOSURE_TIME_RW,
+			  (u64)exposure_ns);
+	if (ret) {
+		dev_err(dev, "Fail to set exposure value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_gain(struct alvium_dev *alvium, bool auto_gain)
+{
+	struct alvium_ctrls *ctrls = &alvium->ctrls;
+	int ret = 0;
+
+	if (ctrls->auto_gain->is_new) {
+		ret = alvium_set_autogain(alvium, auto_gain);
+		if (ret)
+			return ret;
+	}
+
+	if (!auto_gain && ctrls->gain->is_new) {
+		u64 max_gain;
+		u64 min_gain;
+
+		ret = alvium_get_gain_params(alvium);
+		if (ret)
+			return ret;
+
+		max_gain = alvium->max_gain;
+		min_gain = alvium->min_gain;
+
+		if (ctrls->gain->val >= min_gain &&
+			  ctrls->gain->val <= max_gain) {
+			ret = alvium_set_gain(alvium, ctrls->gain->val);
+		} else {
+			return -EINVAL;
+		}
+	}
+
+	return ret;
+}
+
+static int alvium_set_blue_balance_ratio(struct alvium_dev *alvium,
+				    int blue)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_BLUE_BALANCE_RATIO_RW,
+			  (u64)blue);
+	if (ret) {
+		dev_err(dev, "Fail to set blue ratio value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_red_balance_ratio(struct alvium_dev *alvium,
+				    int red)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_RED_BALANCE_RATIO_RW,
+			  (u64)red);
+	if (ret) {
+		dev_err(dev, "Fail to set red ratio value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_awb(struct alvium_dev *alvium, bool on)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	if (!alvium->avail_ft.auto_whiteb)
+		return -EINVAL;
+
+	if (on) {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_WHITE_BALANCE_AUTO_RW,
+			  0x02);
+	} else {
+		ret = alvium_write_hshake(alvium,
+			  REG_BCRM_WHITE_BALANCE_AUTO_RW,
+			  0x00);
+	}
+
+	if (ret) {
+		dev_err(dev, "Fail to set awb reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_exposure(struct alvium_dev *alvium,
+				    enum v4l2_exposure_auto_type auto_exposure)
+{
+	struct alvium_ctrls *ctrls = &alvium->ctrls;
+	bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
+	int ret = 0;
+
+	if (ctrls->auto_exp->is_new) {
+		ret = alvium_set_autoexposure(alvium, auto_exp);
+		if (ret)
+			return ret;
+	}
+
+	if (!auto_exp && ctrls->exposure->is_new) {
+		u64 max_exp;
+		u64 min_exp;
+
+		ret = alvium_get_exposure_params(alvium);
+		if (ret)
+			return ret;
+
+		max_exp = alvium->max_exp;
+		min_exp = alvium->min_exp;
+
+		if (ctrls->exposure->val >= min_exp &&
+			  ctrls->exposure->val <= max_exp) {
+			ret = alvium_set_exposure(alvium, ctrls->exposure->val);
+			if (ret)
+				return ret;
+		} else {
+			return -EINVAL;
+		}
+	}
+
+	return ret;
+}
+
+static int alvium_set_ctrl_white_balance(struct alvium_dev *alvium,
+				    bool awb)
+{
+	struct alvium_ctrls *ctrls = &alvium->ctrls;
+	int ret = 0;
+
+	if (ctrls->auto_wb->is_new) {
+		ret = alvium_set_awb(alvium, awb);
+		if (ret)
+			return ret;
+	}
+
+	if (!awb && ctrls->auto_wb->is_new) {
+		u64 red = (u64)ctrls->red_balance->val;
+		u64 blue = (u64)ctrls->blue_balance->val;
+		u64 red_max;
+		u64 red_min;
+		u64 blue_max;
+		u64 blue_min;
+
+		ret = alvium_get_red_balance_ratio_params(alvium);
+		if (ret)
+			return ret;
+
+		ret = alvium_get_blue_balance_ratio_params(alvium);
+		if (ret)
+			return ret;
+
+		red_max = alvium->max_rbalance;
+		red_min = alvium->min_rbalance;
+		blue_max = alvium->max_bbalance;
+		blue_min = alvium->min_rbalance;
+
+		if (red >= red_min &&
+			  red <= red_max) {
+			ret = alvium_set_red_balance_ratio(alvium, red);
+			if (ret)
+				return ret;
+		} else {
+			return -EINVAL;
+		}
+
+		if (blue >= blue_min &&
+			  blue <= blue_max) {
+			ret = alvium_set_blue_balance_ratio(alvium, blue);
+			if (ret)
+				return ret;
+		} else {
+			return -EINVAL;
+		}
+	}
+
+	return ret;
+}
+
+static int alvium_set_ctrl_hue(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_HUE_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set hue value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_contrast(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_CONTRAST_VALUE_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set contrast value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_saturation(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_SATURATION_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set contrast value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_gamma(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_GAMMA_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set gamma value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_sharpness(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_SHARPNESS_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set sharpness value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_hflip(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_REVERSE_X_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set reverse_x value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_set_ctrl_vflip(struct alvium_dev *alvium, int val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_IMG_REVERSE_Y_RW,
+			  (u64)val);
+	if (ret) {
+		dev_err(dev, "Fail to set reverse_y value reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_hw_features_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_get_csi_clk_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read min/max csi clock regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_img_width_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read img width regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_img_height_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read img heigth regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_offx_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read offx regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_offy_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read offy regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_gain_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read gain regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_exposure_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read min/max exp regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_red_balance_ratio_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read red balance ratio regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_blue_balance_ratio_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read blue balance ratio regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_hue_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read hue regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_contrast_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read contrast regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_saturation_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read saturation regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_black_lvl_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read black lvl regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_gamma_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read gamma regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_sharpness_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read sharpness regs\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_hw_info(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	ret = alvium_get_bcrm_vers(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_bcrm_addr(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to bcrm address reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_fw_version(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read fw version reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_host_supp_csi_lanes(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi lanes reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_feat_inq(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm feature inquiry reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_hw_features_params(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read features params regs\n");
+		return ret;
+	}
+
+	ret = alvium_get_avail_mipi_data_format(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read available mipi data formats reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_avail_bayer(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to read available Bayer patterns reg\n");
+		return ret;
+	}
+
+	ret = alvium_get_mode(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to get current mode reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_hw_init(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+
+	/* Set Alvium BCM mode*/
+	ret = alvium_set_bcm_mode(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set BCM mode\n");
+		return ret;
+	}
+
+	ret = alvium_set_csi_lanes(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set csi lanes\n");
+		return ret;
+	}
+
+	ret = alvium_set_csi_clk(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set csi lanes\n");
+		return ret;
+	}
+
+	ret = alvium_set_lp2hs_delay(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to set streamon reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/* --------------- Subdev Operations --------------- */
+
+static int alvium_g_frame_interval(struct v4l2_subdev *sd,
+				   struct v4l2_subdev_frame_interval *fi)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+
+	fi->interval = alvium->frame_interval;
+
+	return 0;
+}
+
+static int alvium_set_frame_interval(struct alvium_dev *alvium,
+					struct v4l2_subdev_frame_interval *fi)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 req_fr, min_fr, max_fr;
+	int ret;
+
+	if (fi->interval.denominator == 0)
+		return -EINVAL;
+
+	ret = alvium_get_frame_interval(alvium);
+	if (ret) {
+		dev_err(dev, "Fail to get frame interval\n");
+		return ret;
+	}
+
+	min_fr = alvium->min_fr;
+	max_fr = alvium->max_fr;
+
+	dev_dbg(dev, "fi->interval.numerator = %d\n",
+		  fi->interval.numerator);
+	dev_dbg(dev, "fi->interval.denominator = %d\n",
+		  fi->interval.denominator);
+
+	req_fr = (fi->interval.denominator * USEC_PER_SEC) /
+				  fi->interval.numerator;
+
+	if (req_fr >= max_fr &&
+		  req_fr <= min_fr)
+		return -EINVAL;
+
+	alvium->fr = req_fr;
+	alvium->frame_interval.numerator = fi->interval.numerator;
+	alvium->frame_interval.denominator = fi->interval.denominator;
+
+	return 0;
+}
+
+static int alvium_s_frame_interval(struct v4l2_subdev *sd,
+				   struct v4l2_subdev_frame_interval *fi)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 2625

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int ret;
+
+	if (fi->pad != 0)
+		return -EINVAL;

The caller will have checked for this already.

Oks, I will drop this check in v6 if for you is ok. Thanks.

Quoted 38 lines
Quoted 38 lines
drivers/media/i2c/alvium-csi2.c new file +37 -0
+
+	if (alvium->streaming)
+		return -EBUSY;
+
+	ret = alvium_set_frame_interval(alvium, fi);
+	if (!ret) {
+		ret = alvium_set_frame_rate(alvium);
+		if (ret)
+			return -EIO;
+	}
+
+	return ret;
+}
+
+static int alvium_enum_mbus_code(struct v4l2_subdev *sd,
+				 struct v4l2_subdev_state *sd_state,
+				 struct v4l2_subdev_mbus_code_enum *code)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	const struct alvium_pixfmt *formats;
+	unsigned int num_formats;
+
+	formats = alvium->alvium_csi2_fmt;
+	num_formats = alvium->alvium_csi2_fmt_n;
+
+	if (code->index >= num_formats)
+		return -EINVAL;
+
+	code->code = formats[code->index].code;
+
+	return 0;
+}
+
+static const struct alvium_pixfmt *
+alvium_code_to_pixfmt(struct alvium_dev *alvium, u32 code)
+{
+	const struct alvium_pixfmt *formats = alvium->alvium_csi2_fmt;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 2668

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+	unsigned int i;
+
+	for (i = 0; formats[i].code; ++i) {
+		if (formats[i].code == code)
+			return &formats[i];
+	}

No need for braces here.

Ty

Quoted 314 lines
Quoted 314 lines
drivers/media/i2c/alvium-csi2.c new file +313 -0
+
+	return &formats[0];
+}
+
+static int alvium_set_mode(struct alvium_dev *alvium,
+				 struct v4l2_subdev_state *state)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct v4l2_subdev *sd = &alvium->sd;
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+	int ret;
+
+	crop = v4l2_subdev_get_pad_crop(sd, state, 0);
+	fmt = v4l2_subdev_get_pad_format(sd, state, 0);
+
+	if (fmt->width < alvium->img_max_width &&
+		   fmt->width > alvium->img_min_width) {
+		ret = alvium_set_img_width(alvium, fmt->width);
+		if (ret)
+			return ret;
+	} else {
+		dev_err(dev, "Unsupported width");
+		return -EINVAL;
+	}
+
+	if (fmt->height < alvium->img_max_height &&
+		   fmt->height > alvium->img_min_height) {
+		ret = alvium_set_img_height(alvium, fmt->height);
+		if (ret)
+			return ret;
+	} else {
+		dev_err(dev, "Unsupported height");
+		return -EINVAL;
+	}
+
+	ret = alvium_set_img_offx(alvium, crop->left);
+	if (ret)
+		return ret;
+
+	ret = alvium_set_img_offy(alvium, crop->top);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int alvium_set_framefmt(struct alvium_dev *alvium,
+			       struct v4l2_mbus_framefmt *format)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	const struct alvium_pixfmt *alvium_csi2_fmt;
+	int ret = 0;
+
+	alvium_csi2_fmt = alvium_code_to_pixfmt(alvium, format->code);
+
+	ret = alvium_set_mipi_fmt(alvium, alvium_csi2_fmt);
+	if (ret)
+		return ret;
+
+	if (alvium_csi2_fmt->is_raw) {
+		ret = alvium_set_bayer_pattern(alvium, alvium_csi2_fmt);
+		if (ret)
+			return ret;
+	}
+
+	dev_dbg(dev, "start: %s, mipi_fmt_regval regval = 0x%llx",
+	  __func__, alvium_csi2_fmt->mipi_fmt_regval);
+
+	return ret;
+}
+
+static int alvium_s_stream(struct v4l2_subdev *sd, int enable)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_subdev_state *state;
+	int ret = 0;
+
+	state = v4l2_subdev_lock_and_get_active_state(sd);
+
+	if (enable) {
+		ret = __v4l2_ctrl_handler_setup(&alvium->ctrls.handler);
+		if (ret)
+			goto out;
+
+		ret = alvium_set_mode(alvium, state);
+		if (ret)
+			goto out;
+
+		fmt = v4l2_subdev_get_pad_format(sd, state, 0);
+		ret = alvium_set_framefmt(alvium, fmt);
+		if (ret)
+			goto out;
+
+		ret = alvium_set_stream_mipi(alvium, enable);
+		if (ret)
+			goto out;
+
+	} else {
+		ret = alvium_set_stream_mipi(alvium, enable);
+		if (ret)
+			goto out;
+	}
+
+	v4l2_subdev_unlock_state(state);
+	alvium->streaming = !!enable;
+
+	return 0;
+
+out:
+	v4l2_subdev_unlock_state(state);
+	return ret;
+}
+
+static int alvium_init_cfg(struct v4l2_subdev *sd,
+			   struct v4l2_subdev_state *state)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	struct alvium_mode *mode = &alvium->mode;
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+
+	struct v4l2_subdev_format sd_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_TRY,
+		.format = alvium_csi2_default_fmt,
+	};
+
+	struct v4l2_subdev_crop sd_crop = {
+		.which = V4L2_SUBDEV_FORMAT_TRY,
+		.rect = {
+			.left = mode->crop.left,
+			.top = mode->crop.top,
+			.width = mode->crop.width,
+			.height = mode->crop.height,
+		},
+	};
+
+	crop = v4l2_subdev_get_pad_crop(sd, state, 0);
+	fmt = v4l2_subdev_get_pad_format(sd, state, 0);
+
+	*crop = sd_crop.rect;
+	*fmt = sd_fmt.format;
+
+	return 0;
+}
+
+static int alvium_set_fmt(struct v4l2_subdev *sd,
+			   struct v4l2_subdev_state *sd_state,
+			   struct v4l2_subdev_format *format)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	const struct alvium_pixfmt *alvium_csi2_fmt;
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+
+	fmt = v4l2_subdev_get_pad_format(sd, sd_state, 0);
+	crop = v4l2_subdev_get_pad_crop(sd, sd_state, 0);
+
+	if (format->format.width <= alvium->img_max_width &&
+		  format->format.width >= alvium->img_min_width) {
+		fmt->width = format->format.width;
+	} else {
+		return -EINVAL;
+	}
+
+	if (format->format.height <= alvium->img_max_height &&
+		  format->format.height >= alvium->img_min_height) {
+		fmt->height = format->format.height;
+	} else {
+		return -EINVAL;
+	}
+
+	alvium_csi2_fmt = alvium_code_to_pixfmt(alvium, format->format.code);
+	fmt->code = alvium_csi2_fmt->code;
+
+	/* Set also the crop width and height when set a new fmt */
+	crop->width = fmt->width;
+	crop->height = fmt->height;
+
+	*fmt = format->format;
+
+	return 0;
+}
+
+static int alvium_set_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	struct v4l2_mbus_framefmt *fmt;
+	struct v4l2_rect *crop;
+
+	crop = v4l2_subdev_get_pad_crop(sd, sd_state, 0);
+	fmt = v4l2_subdev_get_pad_format(sd, sd_state, 0);
+
+	/*
+	 * Alvium can only shift the origin of the img
+	 * then we accept only value with the same value of the actual fmt
+	 */
+	if (sel->r.width != fmt->width ||
+		  sel->r.height != fmt->height)
+		return -EINVAL;
+
+	if (sel->target != V4L2_SEL_TGT_CROP)
+		return -EINVAL;
+
+	if ((sel->r.left + sel->r.width) <= alvium->img_max_width)
+		crop->left = sel->r.left;
+	else
+		return -EINVAL;
+
+	if ((sel->r.top + sel->r.height) <= alvium->img_max_height)
+		crop->top = sel->r.top;
+	else
+		return -EINVAL;
+
+	crop->width = sel->r.width;
+	crop->height = sel->r.height;
+
+	sel->r = *crop;
+
+	return 0;
+}
+
+static int alvium_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+
+	switch (sel->target) {
+	/* Current cropping area */
+	case V4L2_SEL_TGT_CROP:
+		sel->r = *v4l2_subdev_get_pad_crop(sd, sd_state, 0);
+		break;
+	/* Cropping bounds */
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = alvium->img_max_width;
+		sel->r.height = alvium->img_max_height;
+		break;
+	/* Default cropping area */
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = alvium->min_offy;
+		sel->r.left = alvium->min_offx;
+		sel->r.width = alvium->img_max_width;
+		sel->r.height = alvium->img_max_height;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int alvium_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int val;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUTOGAIN:
+		val = alvium_get_gain(alvium);
+		if (val < 0)
+			return val;
+		alvium->ctrls.gain->val = val;
+		break;
+	case V4L2_CID_EXPOSURE_AUTO:
+		val = alvium_get_exposure(alvium);
+		if (val < 0)
+			return val;
+		alvium->ctrls.exposure->val = val;
+		break;
+	}
+
+	return 0;
+}
+
+static int alvium_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int ret;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUTOGAIN:
+		ret = alvium_set_ctrl_gain(alvium, ctrl->val);
+		break;
+	case V4L2_CID_EXPOSURE_AUTO:
+		ret = alvium_set_ctrl_exposure(alvium, ctrl->val);
+		break;
+	case V4L2_CID_AUTO_WHITE_BALANCE:
+		ret = alvium_set_ctrl_white_balance(alvium, ctrl->val);
+		break;
+	case V4L2_CID_HUE:
+		ret = alvium_set_ctrl_hue(alvium, ctrl->val);
+		break;
+	case V4L2_CID_CONTRAST:
+		ret = alvium_set_ctrl_contrast(alvium, ctrl->val);
+		break;
+	case V4L2_CID_SATURATION:
+		ret = alvium_set_ctrl_saturation(alvium, ctrl->val);
+		break;
+	case V4L2_CID_GAMMA:
+		ret = alvium_set_ctrl_gamma(alvium, ctrl->val);
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

No hsync or vsync controls?

This is not a sensor Alvium ISP/Vision core
is responsible for timings checking of the sensor mounted.

Quoted 263 lines

drivers/media/i2c/alvium-csi2.c

Quoted 257 lines
drivers/media/i2c/alvium-csi2.c new file +256 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

It's not wrong to use OF nodes in general, but all APIs you're using require an fwnode instead. So please use fwnodes here.

	struct fwnode_handle *fwnode = dev_fwnode(dev);
Quoted 7 lines

This makes also of_* headers unnecessary, but please include linux/property.h.

drivers/media/i2c/alvium-csi2.c

Quoted 2 lines
drivers/media/i2c/alvium-csi2.c new file +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

No need to initialise ret.

Oks. Thanks.

Quoted 10 lines
Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +9 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

As you have D-PHY, please assign bus type to D-PHY before parsing the endpoint.

I don't completely got this.
You mean, check:

if (alvium->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
		dev_err(dev, "unsupported bus type\n");
		return -EINVAL;
	}

before parsing or I'm wrong?
Thanks

Quoted 24 lines

drivers/media/i2c/alvium-csi2.c

Quoted 20 lines
drivers/media/i2c/alvium-csi2.c new file +19 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

The V4L2 fwnode framework prints the number of lanes already (with dynamic debug).

Quoted 1 line

If you want to leave frequency print, then make it dev_dbg().

I will replace this with dev_dbg as you suggest.
Thanks.

Quoted 17 lines

drivers/media/i2c/alvium-csi2.c

Quoted 13 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This seems useless and wrong: the regulator may be also used by another driver.

Quoted 9 lines

drivers/media/i2c/alvium-csi2.c

Quoted 7 lines
drivers/media/i2c/alvium-csi2.c new file +7 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Wow. That's the longest I've ever seen, by a large margin.

Why wrong?

The camera has around 7s boot time. :'(
Pretty huge time.

This is one of way that I found to handle
this long time.

I have a short discussion with Laurent here:
http://www.linux-fancy.com/project/linux-fancy/patch/[email protected]/
Pls check.

Do you have any hints on your side?
Let me know. Thanks in advance.

Quoted 10 lines
Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +9 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Remove perhaps?

Yep

Quoted 99 lines

drivers/media/i2c/alvium-csi2.c

Quoted 96 lines
drivers/media/i2c/alvium-csi2.c new file +95 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Autosuspend is disabled by default.

Quoted 1 line

What's the purpose of this? Leave the device powered on after probe?

Due of the long boot time as we discuss in the previous mails we think that is safe to keep the device powered on.

If not we have a 7s delay before every stream_on :'(

http://www.linux-fancy.com/project/linux-fancy/patch/[email protected]/

Quoted 16 lines

drivers/media/i2c/alvium-csi2.c

Quoted 14 lines
drivers/media/i2c/alvium-csi2.c new file +13 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This looks like development time leftover, please remove.

Yes, thanks.
I'll drop this.

Quoted 262 lines
Quoted 262 lines
drivers/media/i2c/alvium-csi2.c new file +59 -0

Lines of this file are not shown. View the raw message.

drivers/media/i2c/alvium-csi2.h new file +196 -0
new file mode 100644
index 000000000000..30b70e2ddb16

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 8 lines

drivers/media/i2c/alvium-csi2.h

Quoted 6 lines
drivers/media/i2c/alvium-csi2.h new file +6 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Please use tabs instead.

K

Quoted 7 lines

drivers/media/i2c/alvium-csi2.h

Quoted 5 lines
drivers/media/i2c/alvium-csi2.h new file +4 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

And align the value to the same column.

K

Quoted 129 lines
Quoted 129 lines
drivers/media/i2c/alvium-csi2.h new file +128 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 8 lines

drivers/media/i2c/alvium-csi2.h

Quoted 4 lines
drivers/media/i2c/alvium-csi2.h new file +4 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

I don't think C defines a particular location for the bit fields if their size is less than that of the type. Could you add padding to ensure that? Same for other structs.

I take "struct s5k5baf" of drivers/media/i2c/s5k5baf.c as reference. I'm missing something? Can you explain me better?

Thanks!

Quoted 22 lines
Quoted 22 lines
drivers/media/i2c/alvium-csi2.h new file +21 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 8 lines

drivers/media/i2c/alvium-csi2.h

Quoted 5 lines
drivers/media/i2c/alvium-csi2.h new file +5 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This would require less memory if you moved all 8-bit fields to the end of the struct.

Also Laurent and Cristophe point me about this, thanks.

If is possible I prefer to keep this order this is more clear for me.

But If not fit your plan never mind I pick your order. Thanks to all! :)

Quoted 114 lines
Quoted 114 lines
drivers/media/i2c/alvium-csi2.h new file +113 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Quoted 13 lines
Quoted 8 lines
drivers/media/i2c/alvium-csi2.h new file +8 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 3 lines
Kind regards,

Sakari Ailus

Regards,
Tommaso

Part of the diff is not shown here. View the raw message.

#11 lore raw

Hi Tommaso,

Quoted 166 lines · Tommaso Merciai

Hi Sakar,

Thanks for your review.

Quoted 104 lines · Sakari Ailus

Hi Tommaso,

Thanks for the patch.

Quoted 99 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h
drivers/media/i2c/Kconfig +10 -0
index 256d55bb2b1d..a9fdf10ffc15 100644
@@ -34,6 +34,16 @@ config VIDEO_APTINA_PLL
 config VIDEO_CCS_PLL
 	tristate
 
+config VIDEO_ALVIUM_CSI2
+	tristate "Allied Vision ALVIUM MIPI CSI-2 camera support"
+	depends on I2C
+	select MEDIA_CONTROLLER
+	select VIDEO_V4L2_SUBDEV_API
+	select V4L2_FWNODE
+	help
+	  This is a Video4Linux2 sensor driver for the Allied Vision
+	  ALVIUM camera connected via MIPI CSI-2 interface.
+
 config VIDEO_AR0521
 	tristate "ON Semiconductor AR0521 sensor support"
 	depends on I2C && VIDEO_DEV
drivers/media/i2c/Makefile +1 -0
index b44dacf935f4..be4dddd6fd98 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_VIDEO_ADV7604) += adv7604.o
 obj-$(CONFIG_VIDEO_ADV7842) += adv7842.o
 obj-$(CONFIG_VIDEO_AK7375) += ak7375.o
 obj-$(CONFIG_VIDEO_AK881X) += ak881x.o
+obj-$(CONFIG_VIDEO_ALVIUM_CSI2) += alvium-csi2.o
 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
 obj-$(CONFIG_VIDEO_AR0521) += ar0521.o
 obj-$(CONFIG_VIDEO_BT819) += bt819.o
drivers/media/i2c/alvium-csi2.c new file +13 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Allied Vision Inc. Alvium camera driver
+ *
+ * Copyright (C) 2023 Tommaso Merciai
+ * Copyright (C) 2023 Martin Hecht
+ * Copyright (C) 2023 Avnet EMG GmbH
+ */
+
+#include <asm/unaligned.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/clkdev.h>
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 19

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+#include <linux/ctype.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/init.h>

Do you need this?

No, Thanks :)

Quoted 13 lines

drivers/media/i2c/alvium-csi2.c line 29

Quoted 11 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of_graph.h>
+#include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/mipi-csi2.h>

Please order alphabetically.

Oooks, ty

Quoted 22 lines
Quoted 22 lines
drivers/media/i2c/alvium-csi2.c new file +21 -0
+#include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#include "alvium-csi2.h"
+
+static const struct v4l2_mbus_framefmt alvium_csi2_default_fmt = {
+	.code = MEDIA_BUS_FMT_UYVY8_1X16,
+	.width = 640,
+	.height = 480,
+	.colorspace = V4L2_COLORSPACE_SRGB,
+	.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
+	.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.field = V4L2_FIELD_NONE,
+};
+
+static const struct alvium_pixfmt alvium_csi2_fmts[] = {
+	{
+		/* UYVY8_2X8 */
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 56

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+		.id = ALVIUM_FMT_UYVY8_2X8,
+		.code = MEDIA_BUS_FMT_UYVY8_2X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,

Could you use MIPI_CSI2_DT_ definitions in mipi-csi2.h?

Quoted 1 line

ofc ty

I'm not sure I understand all your comments.

...

Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
drivers/media/i2c/alvium-csi2.c new file +28 -0
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	struct i2c_msg msgs[2] = {0};
+	u8 addr_buf[2] = {0};
+	u8 data_buf[8] = {0};
+	int ret;
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, addr_buf);
+
+	msgs[0].addr = client->addr;
+	msgs[0].flags = 0;
+	msgs[0].len = ARRAY_SIZE(addr_buf);
+	msgs[0].buf = addr_buf;
+
+	msgs[1].addr = client->addr;
+	msgs[1].flags = I2C_M_RD;
+	msgs[1].len = len;
+	msgs[1].buf = &data_buf[8 - len];
+
+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 366

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret != ARRAY_SIZE(msgs))
+		return -EIO;
+
+	*val = get_unaligned_be64(data_buf);

Could you use CCI wrapper Hans has been working on? It'll be merged fairly soon I presume:
<URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

Quoted 4 lines

Thanks for sharing.

Actually I'm working/testing the driver on 6.4.0-rc2 I don't see this new functionality, sry.

They're not there yet. I presume we'll be merging them soonish though.

Quoted 143 lines

I can try.
But this is only for CCI regs read/write right?

Or also for v4l2 regs?
Let me know, thanks! :)

Quoted 17 lines
Quoted 17 lines
drivers/media/i2c/alvium-csi2.c new file +16 -0
+
+	return 0;
+}
+
+static int alvium_write(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	u8 buf[10] = {0};
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 388

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, buf);
+	put_unaligned_be64(val << (8 * (8 - len)), buf + 2);
+	if (i2c_master_send(client, buf, len + 2) != len + 2)
+		return -EIO;

Same here.

^Same question.

Quoted 99 lines
Quoted 99 lines
drivers/media/i2c/alvium-csi2.c new file +98 -0
+
+	return 0;
+}
+
+static int alvium_write_hshake(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 hshake_regval;
+	u8 hshake_bit;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/* reset handshake bit */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* write alvium reg*/
+	ret = alvium_write(alvium, reg, val);
+	if (ret) {
+		dev_err(dev, "Fail to write reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 1*/
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (!hshake_bit);
+
+	/* reset handshake bit, write 0 to bit0 */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 0 */
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (hshake_bit);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_vers(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_bcrm_vers *v;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	v = (struct alvium_bcrm_vers *) &val;
+
+	v->minor = cpu_to_le16(v->minor);
+	v->major = cpu_to_le16(v->major);
+
+	dev_info(dev, "bcrm version: %u.%u\n", v->minor,
+			  v->major);
+
+	return 0;
+}
+
+static int alvium_get_fw_version(struct alvium_dev *alvium)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 491

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_fw_vers *fw_v;

Please order temporary, loop etc. variables as last. Same for the other functions. Reverse Christmas tree order is often the same.

Quoted 7 lines

Can you explain me better pls?
Your plan is the following order:

struct device *dev = &alvium->i2c_client->dev;
struct alvium_fw_vers *fw_v;
u64 val;
int ret;

This is what I intended, yes.

Quoted 25 lines

or I'm wrong? Thanks.

Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +11 -0
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_DEVICE_FIRMWARE_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read fw version reg\n");
+		return ret;
+	}
+
+	fw_v = (struct alvium_fw_vers *) &val;
+
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 507

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	fw_v->minor = cpu_to_le16(fw_v->minor);
+	fw_v->patch = cpu_to_le32(fw_v->patch);
+
+	dev_info(dev, "fw version: %u.%u.%u.%u\n",
+			  fw_v->special,

Please align arguments to just right of the opening parenthesis. Also, more fits on the same line (up to 80 chars). This applies to the entire patch.

Quoted 4 lines

dev_info(dev, "fw version: %u.%u.%u.%u\n", fw_v->special, fw_v->major,

	 fw_v->minor, fw_v->patch);

Like this?

Yes.

Quoted 213 lines
Quoted 53 lines
Quoted 53 lines
drivers/media/i2c/alvium-csi2.c new file +51 -0
+			  fw_v->major,
+			  fw_v->minor,
+			  fw_v->patch);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_addr(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_REG_ADDR_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm addr reg\n");
+		return ret;
+	}
+
+	alvium->bcrm_addr = (u16)val;
+	dev_dbg(dev, "bcrm addr: %d\n", alvium->bcrm_addr);
+
+	return 0;
+}
+
+static bool alvium_is_alive(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val = 0;
+	u8 hbeat_val = 0;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HEARTBEAT_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read heartbeat reg\n");
+		return false;
+	}
+
+	hbeat_val = (u8)val;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 564

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+	dev_dbg(dev, "hearthbeat = %d", hbeat_val);
+
+	if (hbeat_val)
+		return true;
+	else
+		return false;

return hbeat_val;

Oks.

Quoted 149 lines
Quoted 136 lines
drivers/media/i2c/alvium-csi2.c new file +135 -0
+}
+
+static void alvium_print_avail_mipi_fmt(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_leg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_LEG]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_10: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_10]);
+	dev_dbg(dev, "avail mipi_fmt rgb888: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB888]);
+	dev_dbg(dev, "avail mipi_fmt rgb666: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB666]);
+	dev_dbg(dev, "avail mipi_fmt rgb565: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB565]);
+	dev_dbg(dev, "avail mipi_fmt rgb555: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB555]);
+	dev_dbg(dev, "avail mipi_fmt rgb444: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB444]);
+	dev_dbg(dev, "avail mipi_fmt raw6: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW6]);
+	dev_dbg(dev, "avail mipi_fmt raw7: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW7]);
+	dev_dbg(dev, "avail mipi_fmt raw8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW8]);
+	dev_dbg(dev, "avail mipi_fmt raw10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW10]);
+	dev_dbg(dev, "avail mipi_fmt raw12: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW12]);
+	dev_dbg(dev, "avail mipi_fmt raw14: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW14]);
+	dev_dbg(dev, "avail mipi_fmt jpeg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_JPEG]);
+}
+
+static void alvium_print_avail_feat(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "feature rev_x: %u\n",
+		  alvium->avail_ft.rev_x);
+	dev_dbg(dev, "feature rev_y: %u\n",
+		  alvium->avail_ft.rev_y);
+	dev_dbg(dev, "feature int_autop: %u\n",
+		  alvium->avail_ft.int_autop);
+	dev_dbg(dev, "feature black_lvl: %u\n",
+		  alvium->avail_ft.black_lvl);
+	dev_dbg(dev, "feature gain: %u\n",
+		  alvium->avail_ft.gain);
+	dev_dbg(dev, "feature gamma: %u\n",
+		  alvium->avail_ft.gamma);
+	dev_dbg(dev, "feature contrast: %u\n",
+		  alvium->avail_ft.contrast);
+	dev_dbg(dev, "feature sat: %u\n",
+		  alvium->avail_ft.sat);
+	dev_dbg(dev, "feature hue: %u\n",
+		  alvium->avail_ft.hue);
+	dev_dbg(dev, "feature whiteb: %u\n",
+		  alvium->avail_ft.whiteb);
+	dev_dbg(dev, "feature sharp: %u\n",
+		  alvium->avail_ft.sharp);
+	dev_dbg(dev, "feature auto_exp: %u\n",
+		  alvium->avail_ft.auto_exp);
+	dev_dbg(dev, "feature auto_gain: %u\n",
+		  alvium->avail_ft.auto_gain);
+	dev_dbg(dev, "feature auto_whiteb: %u\n",
+		  alvium->avail_ft.auto_whiteb);
+	dev_dbg(dev, "feature dev_temp: %u\n",
+		  alvium->avail_ft.dev_temp);
+	dev_dbg(dev, "feature acq_abort: %u\n",
+		  alvium->avail_ft.acq_abort);
+	dev_dbg(dev, "feature acq_fr: %u\n",
+		  alvium->avail_ft.acq_fr);
+	dev_dbg(dev, "feature fr_trigger: %u\n",
+		  alvium->avail_ft.fr_trigger);
+	dev_dbg(dev, "feature exp_acq_line: %u\n",
+		  alvium->avail_ft.exp_acq_line);
+}
+
+static void alvium_print_avail_bayer(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail bayer mono: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_MONO]);
+	dev_dbg(dev, "avail bayer gr: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GR]);
+	dev_dbg(dev, "avail bayer rg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_RG]);
+	dev_dbg(dev, "avail bayer gb: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GB]);
+	dev_dbg(dev, "avail bayer bg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_BG]);
+}
+
+static int alvium_get_feat_inq(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_feat *f;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_FEATURE_INQUIRY_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to features inquiry reg\n");
+		return ret;
+	}
+
+	f = (struct alvium_avail_feat *) &val;
+
+	alvium->avail_ft.rev_x = f->rev_x;
+	alvium->avail_ft.rev_y = f->rev_y;
+	alvium->avail_ft.int_autop = f->int_autop;
+	alvium->avail_ft.black_lvl = f->black_lvl;
+	alvium->avail_ft.gain = f->gain;
+	alvium->avail_ft.gamma = f->gamma;
+	alvium->avail_ft.contrast = f->contrast;
+	alvium->avail_ft.sat = f->sat;
+	alvium->avail_ft.hue = f->rev_y;

drivers/media/i2c/alvium-csi2.c line 709

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	alvium->avail_ft.whiteb = f->whiteb;
+	alvium->avail_ft.sharp = f->sharp;
+	alvium->avail_ft.auto_exp = f->auto_exp;
+	alvium->avail_ft.auto_gain = f->auto_gain;
+	alvium->avail_ft.auto_whiteb = f->auto_whiteb;
+	alvium->avail_ft.dev_temp = f->dev_temp;
+	alvium->avail_ft.acq_abort = f->acq_abort;
+	alvium->avail_ft.acq_fr = f->acq_fr;
+	alvium->avail_ft.fr_trigger = f->fr_trigger;
+	alvium->avail_ft.exp_acq_line = f->exp_acq_line;

You could instead do:

Quoted 7 lines
Quoted 4 lines
	alvium->avail_ft = *f;

But is there an endianness issue here? I think you should use u64 also for the struct fields.

Mmmm.. I'm not sure are all u8 this is not safe (on endianess pesrpective)? Or I'm completely wrong?

The u8 fields in the struct are fine but the u64 value is not. Instead you should read a u8 array.

Quoted 180 lines
Quoted 171 lines
Quoted 171 lines
drivers/media/i2c/alvium-csi2.c new file +169 -0
+
+	alvium_print_avail_feat(alvium);
+
+	return 0;
+}
+
+static int alvium_get_host_supp_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_LANE_COUNT_RW,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->h_sup_csi_lanes = (u8)val;
+	dev_dbg(dev, "host supported csi2 lanes count: %d\n",
+			  alvium->h_sup_csi_lanes);
+
+	return 0;
+}
+
+static int alvium_set_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 num_lanes;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	num_lanes = alvium->ep.bus.mipi_csi2.num_data_lanes;
+
+	if (num_lanes <= alvium->h_sup_csi_lanes) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_LANE_COUNT_RW,
+				  num_lanes);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "Set n lanes = %llu", num_lanes);
+
+	return 0;
+}
+
+static int alvium_set_lp2hs_delay(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 lp2hs_delay;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/*
+	 * The purpose of this reg is force a DPhy reset
+	 * for the period described by the microseconds on
+	 * the reg, before it starts streaming.
+	 *
+	 * To be clear, with that value bigger than 0 the
+	 * Alvium forces a dphy-reset on all lanes for that period.
+	 * That means all lanes go up into low power state.
+	 *
+	 * This may help a csi2 rx ip to reset if that IP
+	 * can't deal with a continous clock.
+	 */
+	lp2hs_delay = (u64)alvium->lp2hs_delay;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_STREAM_ON_DELAY_RW,
+			  lp2hs_delay);
+	if (ret) {
+		dev_err(dev, "Fail to set streamon delay reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_csi_clk_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->min_csi_clk = (u32)val;
+	dev_dbg(dev, "Min host supported csi clock: %d\n",
+			  alvium->min_csi_clk);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->max_csi_clk = (u32)val;
+	dev_dbg(dev, "Max host supported csi clock: %d\n",
+			  alvium->max_csi_clk);
+
+	return 0;
+}
+
+static int alvium_set_csi_clk(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 csi_clk;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	csi_clk = (u32)alvium->ep.link_frequencies[0];
+
+	if (csi_clk > alvium->min_csi_clk ||
+		csi_clk < alvium->max_csi_clk) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_CLOCK_RW,
+				  csi_clk);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	alvium->link_freq = alvium->ep.link_frequencies[0];
+
+	return 0;
+}
+
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 882

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

...

Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
drivers/media/i2c/alvium-csi2.c new file +31 -0
+static int alvium_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int ret;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUTOGAIN:
+		ret = alvium_set_ctrl_gain(alvium, ctrl->val);
+		break;
+	case V4L2_CID_EXPOSURE_AUTO:
+		ret = alvium_set_ctrl_exposure(alvium, ctrl->val);
+		break;
+	case V4L2_CID_AUTO_WHITE_BALANCE:
+		ret = alvium_set_ctrl_white_balance(alvium, ctrl->val);
+		break;
+	case V4L2_CID_HUE:
+		ret = alvium_set_ctrl_hue(alvium, ctrl->val);
+		break;
+	case V4L2_CID_CONTRAST:
+		ret = alvium_set_ctrl_contrast(alvium, ctrl->val);
+		break;
+	case V4L2_CID_SATURATION:
+		ret = alvium_set_ctrl_saturation(alvium, ctrl->val);
+		break;
+	case V4L2_CID_GAMMA:
+		ret = alvium_set_ctrl_gamma(alvium, ctrl->val);
+		break;
+	case V4L2_CID_SHARPNESS:
+		ret = alvium_set_ctrl_sharpness(alvium, ctrl->val);
+		break;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 2987

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+	case V4L2_CID_HFLIP:
+		ret = alvium_set_ctrl_hflip(alvium, ctrl->val);
+		break;
+	case V4L2_CID_VFLIP:
+		ret = alvium_set_ctrl_vflip(alvium, ctrl->val);
+		break;

No hsync or vsync controls?

Quoted 2 lines

This is not a sensor Alvium ISP/Vision core
is responsible for timings checking of the sensor mounted.

And those values are unknown then?

Generally raw cameras are controlled configuring the blanking. Of course if these aren't available, we can't expose them to user space either.

...

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	ret = fwnode_property_read_u32(dev_fwnode(dev),
+			  "alliedvision,lp2hs-delay-us",
+			  &alvium->lp2hs_delay);
+
+	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 3259

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	if (!endpoint) {
+		dev_err(dev, "endpoint node not found\n");
+		return -EINVAL;
+	}
+

As you have D-PHY, please assign bus type to D-PHY before parsing the endpoint.

I don't completely got this.
You mean, check:

Quoted 6 lines
if (alvium->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
		dev_err(dev, "unsupported bus type\n");
		return -EINVAL;
	}

before parsing or I'm wrong?

Please read v4l2_fwnode_endpoint_alloc_parse() documentation in include/media/v4l2-fwnode.h .

...

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
drivers/media/i2c/alvium-csi2.c new file +16 -0
+struct alvium_avail_mipi_fmt {
+	u8 yuv420_8_leg:1;
+	u8 yuv420_8:1;
+	u8 yuv420_10:1;
+	u8 yuv420_8_csps:1;
+	u8 yuv420_10_csps:1;
+	u8 yuv422_8:1;
+	u8 yuv422_10:1;
+	u8 rgb888:1;
+	u8 rgb666:1;
+	u8 rgb565:1;
+	u8 rgb555:1;
+	u8 rgb444:1;
+	u8 raw6:1;
+	u8 raw7:1;
+	u8 raw8:1;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 671

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	u8 raw10:1;
+	u8 raw12:1;
+	u8 raw14:1;
+	u8 jpeg:1;

I don't think C defines a particular location for the bit fields if their size is less than that of the type. Could you add padding to ensure that? Same for other structs.

Quoted 2 lines

I take "struct s5k5baf" of drivers/media/i2c/s5k5baf.c as reference. I'm missing something? Can you explain me better?

How is this related to the comment, other than that it uses bit fields?

Quoted 36 lines

Thanks!

Quoted 22 lines
Quoted 22 lines
drivers/media/i2c/alvium-csi2.c new file +21 -0
+};
+
+struct alvium_avail_bayer {
+	u8 mono:1;
+	u8 gr:1;
+	u8 rg:1;
+	u8 gb:1;
+	u8 bg:1;
+};
+
+struct alvium_mode {
+	struct v4l2_rect crop;
+	struct v4l2_mbus_framefmt fmt;
+	u32 width;
+	u32 height;
+};
+
+struct alvium_pixfmt {
+	u8 id;
+	u32 code;
+	u32 colorspace;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 697

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	u8 fmt_av_bit;
+	u8 bay_av_bit;
+	u64 mipi_fmt_regval;
+	u64 bay_fmt_regval;
+	u8 is_raw;

This would require less memory if you moved all 8-bit fields to the end of the struct.

Quoted 7 lines

Also Laurent and Cristophe point me about this, thanks.

If is possible I prefer to keep this order this is more clear for me.

But If not fit your plan never mind I pick your order. Thanks to all! :)

If you've received the same comment from three reviewers, then please do it. :-)

Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#12 lore raw

Hi Sakari,

Quoted 169 lines · Sakari Ailus

Hi Tommaso,

Quoted 166 lines · Tommaso Merciai

Hi Sakar,

Thanks for your review.

Quoted 104 lines · Sakari Ailus

Hi Tommaso,

Thanks for the patch.

Quoted 99 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h
drivers/media/i2c/Kconfig +10 -0
index 256d55bb2b1d..a9fdf10ffc15 100644
@@ -34,6 +34,16 @@ config VIDEO_APTINA_PLL
 config VIDEO_CCS_PLL
 	tristate
 
+config VIDEO_ALVIUM_CSI2
+	tristate "Allied Vision ALVIUM MIPI CSI-2 camera support"
+	depends on I2C
+	select MEDIA_CONTROLLER
+	select VIDEO_V4L2_SUBDEV_API
+	select V4L2_FWNODE
+	help
+	  This is a Video4Linux2 sensor driver for the Allied Vision
+	  ALVIUM camera connected via MIPI CSI-2 interface.
+
 config VIDEO_AR0521
 	tristate "ON Semiconductor AR0521 sensor support"
 	depends on I2C && VIDEO_DEV
drivers/media/i2c/Makefile +1 -0
index b44dacf935f4..be4dddd6fd98 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_VIDEO_ADV7604) += adv7604.o
 obj-$(CONFIG_VIDEO_ADV7842) += adv7842.o
 obj-$(CONFIG_VIDEO_AK7375) += ak7375.o
 obj-$(CONFIG_VIDEO_AK881X) += ak881x.o
+obj-$(CONFIG_VIDEO_ALVIUM_CSI2) += alvium-csi2.o
 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
 obj-$(CONFIG_VIDEO_AR0521) += ar0521.o
 obj-$(CONFIG_VIDEO_BT819) += bt819.o
drivers/media/i2c/alvium-csi2.c new file +13 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Allied Vision Inc. Alvium camera driver
+ *
+ * Copyright (C) 2023 Tommaso Merciai
+ * Copyright (C) 2023 Martin Hecht
+ * Copyright (C) 2023 Avnet EMG GmbH
+ */
+
+#include <asm/unaligned.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/clkdev.h>
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 19

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+#include <linux/ctype.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/init.h>

Do you need this?

No, Thanks :)

Quoted 13 lines

drivers/media/i2c/alvium-csi2.c line 29

Quoted 11 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of_graph.h>
+#include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <media/v4l2-async.h>
+#include <media/v4l2-ctrls.h>
+#include <media/mipi-csi2.h>

Please order alphabetically.

Oooks, ty

Quoted 30 lines
Quoted 18 lines
drivers/media/i2c/alvium-csi2.c new file +17 -0
+#include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-fwnode.h>
+#include <media/v4l2-subdev.h>
+
+#include "alvium-csi2.h"
+
+static const struct v4l2_mbus_framefmt alvium_csi2_default_fmt = {
+	.code = MEDIA_BUS_FMT_UYVY8_1X16,
+	.width = 640,
+	.height = 480,
+	.colorspace = V4L2_COLORSPACE_SRGB,
+	.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
+	.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
+	.field = V4L2_FIELD_NONE,
+};

drivers/media/i2c/alvium-csi2.c line 56

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+
+static const struct alvium_pixfmt alvium_csi2_fmts[] = {
+	{
+		/* UYVY8_2X8 */
+		.id = ALVIUM_FMT_UYVY8_2X8,
+		.code = MEDIA_BUS_FMT_UYVY8_2X8,
+		.colorspace = V4L2_COLORSPACE_SRGB,
+		.fmt_av_bit = ALVIUM_BIT_YUV422_8,
+		.bay_av_bit = ALVIUM_BIT_BAY_NONE,
+		.mipi_fmt_regval = 0x1E,

Could you use MIPI_CSI2_DT_ definitions in mipi-csi2.h?

Quoted 3 lines
Quoted 1 line

ofc ty

I'm not sure I understand all your comments.

Sorry.. :)

Quoted 40 lines

...

Quoted 37 lines
Quoted 36 lines
Quoted 22 lines
drivers/media/i2c/alvium-csi2.c new file +22 -0
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	struct i2c_msg msgs[2] = {0};
+	u8 addr_buf[2] = {0};
+	u8 data_buf[8] = {0};
+	int ret;
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, addr_buf);
+
+	msgs[0].addr = client->addr;
+	msgs[0].flags = 0;
+	msgs[0].len = ARRAY_SIZE(addr_buf);
+	msgs[0].buf = addr_buf;
+

drivers/media/i2c/alvium-csi2.c line 366

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	msgs[1].addr = client->addr;
+	msgs[1].flags = I2C_M_RD;
+	msgs[1].len = len;
+	msgs[1].buf = &data_buf[8 - len];
+
+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+	if (ret != ARRAY_SIZE(msgs))
+		return -EIO;
+
+	*val = get_unaligned_be64(data_buf);

Could you use CCI wrapper Hans has been working on? It'll be merged fairly soon I presume:
<URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

Quoted 6 lines
Quoted 4 lines

Thanks for sharing.

Actually I'm working/testing the driver on 6.4.0-rc2 I don't see this new functionality, sry.

They're not there yet. I presume we'll be merging them soonish though.

Please Check v6.
In this version I'm using new cci API from Hans.

Quoted 147 lines
Quoted 147 lines

I can try.
But this is only for CCI regs read/write right?

Or also for v4l2 regs?
Let me know, thanks! :)

Quoted 17 lines
Quoted 17 lines
drivers/media/i2c/alvium-csi2.c new file +16 -0
+
+	return 0;
+}
+
+static int alvium_write(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct i2c_client *client = alvium->i2c_client;
+	u16 addr = REG_BCRM_ADDR(reg);
+	unsigned int len = REG_BCRM_SIZE(reg);
+	u8 buf[10] = {0};
+
+	if (WARN_ON(len > 8))
+		return -EINVAL;
+
+	if (reg & REG_BCRM_V4L2)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 388

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+		addr += alvium->bcrm_addr;
+
+	put_unaligned_be16(addr, buf);
+	put_unaligned_be64(val << (8 * (8 - len)), buf + 2);
+	if (i2c_master_send(client, buf, len + 2) != len + 2)
+		return -EIO;

Same here.

^Same question.

Quoted 99 lines
Quoted 99 lines
drivers/media/i2c/alvium-csi2.c new file +98 -0
+
+	return 0;
+}
+
+static int alvium_write_hshake(struct alvium_dev *alvium,
+				  u32 reg, u64 val)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	u64 hshake_regval;
+	u8 hshake_bit;
+	int ret;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/* reset handshake bit */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* write alvium reg*/
+	ret = alvium_write(alvium, reg, val);
+	if (ret) {
+		dev_err(dev, "Fail to write reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 1*/
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (!hshake_bit);
+
+	/* reset handshake bit, write 0 to bit0 */
+	ret = alvium_write(alvium,
+			  REG_BCRM_WRITE_HANDSHAKE_RW,
+			  BCRM_HANDSHAKE_W_RST);
+	if (ret) {
+		dev_err(dev, "Fail to reset hshake reg\n");
+		return ret;
+	}
+
+	/* poll handshake bit since bit0 = 0 */
+	do {
+		ret = alvium_read(alvium,
+				  REG_BCRM_WRITE_HANDSHAKE_RW,
+				  &hshake_regval);
+		if (ret) {
+			dev_err(dev, "Fail to read hshake reg\n");
+			return ret;
+		}
+
+		hshake_bit = (hshake_regval & BCRM_HANDSHAKE_W_DONE_EN_BIT);
+
+	} while (hshake_bit);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_vers(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_bcrm_vers *v;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	v = (struct alvium_bcrm_vers *) &val;
+
+	v->minor = cpu_to_le16(v->minor);
+	v->major = cpu_to_le16(v->major);
+
+	dev_info(dev, "bcrm version: %u.%u\n", v->minor,
+			  v->major);
+
+	return 0;
+}
+
+static int alvium_get_fw_version(struct alvium_dev *alvium)
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 491

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+	struct alvium_fw_vers *fw_v;

Please order temporary, loop etc. variables as last. Same for the other functions. Reverse Christmas tree order is often the same.

Can you explain me better pls?
Your plan is the following order:

Quoted 6 lines
Quoted 4 lines

struct device *dev = &alvium->i2c_client->dev;
struct alvium_fw_vers *fw_v;
u64 val;
int ret;

This is what I intended, yes.

Fixed in v6.

Quoted 26 lines
Quoted 26 lines

or I'm wrong? Thanks.

Quoted 20 lines
Quoted 7 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_DEVICE_FIRMWARE_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read fw version reg\n");

drivers/media/i2c/alvium-csi2.c line 507

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+		return ret;
+	}
+
+	fw_v = (struct alvium_fw_vers *) &val;
+
+	fw_v->minor = cpu_to_le16(fw_v->minor);
+	fw_v->patch = cpu_to_le32(fw_v->patch);
+
+	dev_info(dev, "fw version: %u.%u.%u.%u\n",
+			  fw_v->special,

Please align arguments to just right of the opening parenthesis. Also, more fits on the same line (up to 80 chars). This applies to the entire patch.

Quoted 6 lines
Quoted 4 lines

dev_info(dev, "fw version: %u.%u.%u.%u\n", fw_v->special, fw_v->major,

	 fw_v->minor, fw_v->patch);

Like this?

Yes.

Oooks

Quoted 216 lines
Quoted 216 lines
Quoted 54 lines
Quoted 54 lines
drivers/media/i2c/alvium-csi2.c new file +51 -0
+			  fw_v->major,
+			  fw_v->minor,
+			  fw_v->patch);
+
+	return 0;
+}
+
+static int alvium_get_bcrm_addr(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_REG_ADDR_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm addr reg\n");
+		return ret;
+	}
+
+	alvium->bcrm_addr = (u16)val;
+	dev_dbg(dev, "bcrm addr: %d\n", alvium->bcrm_addr);
+
+	return 0;
+}
+
+static bool alvium_is_alive(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val = 0;
+	u8 hbeat_val = 0;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_VERSION_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read bcrm version reg\n");
+		return ret;
+	}
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_HEARTBEAT_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read heartbeat reg\n");
+		return false;
+	}
+
+	hbeat_val = (u8)val;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 564

Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +6 -0
+	dev_dbg(dev, "hearthbeat = %d", hbeat_val);
+
+	if (hbeat_val)
+		return true;
+	else
+		return false;

return hbeat_val;

Oks.

Quoted 151 lines
Quoted 136 lines
drivers/media/i2c/alvium-csi2.c new file +135 -0
+}
+
+static void alvium_print_avail_mipi_fmt(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_leg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_LEG]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_8_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_8_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv420_10_csps: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV420_10_CSPS]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_8]);
+	dev_dbg(dev, "avail mipi_fmt yuv422_10: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_YUV422_10]);
+	dev_dbg(dev, "avail mipi_fmt rgb888: %u\n",
+		   alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB888]);
+	dev_dbg(dev, "avail mipi_fmt rgb666: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB666]);
+	dev_dbg(dev, "avail mipi_fmt rgb565: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB565]);
+	dev_dbg(dev, "avail mipi_fmt rgb555: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB555]);
+	dev_dbg(dev, "avail mipi_fmt rgb444: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RGB444]);
+	dev_dbg(dev, "avail mipi_fmt raw6: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW6]);
+	dev_dbg(dev, "avail mipi_fmt raw7: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW7]);
+	dev_dbg(dev, "avail mipi_fmt raw8: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW8]);
+	dev_dbg(dev, "avail mipi_fmt raw10: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW10]);
+	dev_dbg(dev, "avail mipi_fmt raw12: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW12]);
+	dev_dbg(dev, "avail mipi_fmt raw14: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_RAW14]);
+	dev_dbg(dev, "avail mipi_fmt jpeg: %u\n",
+		  alvium->is_mipi_fmt_avail[ALVIUM_BIT_JPEG]);
+}
+
+static void alvium_print_avail_feat(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "feature rev_x: %u\n",
+		  alvium->avail_ft.rev_x);
+	dev_dbg(dev, "feature rev_y: %u\n",
+		  alvium->avail_ft.rev_y);
+	dev_dbg(dev, "feature int_autop: %u\n",
+		  alvium->avail_ft.int_autop);
+	dev_dbg(dev, "feature black_lvl: %u\n",
+		  alvium->avail_ft.black_lvl);
+	dev_dbg(dev, "feature gain: %u\n",
+		  alvium->avail_ft.gain);
+	dev_dbg(dev, "feature gamma: %u\n",
+		  alvium->avail_ft.gamma);
+	dev_dbg(dev, "feature contrast: %u\n",
+		  alvium->avail_ft.contrast);
+	dev_dbg(dev, "feature sat: %u\n",
+		  alvium->avail_ft.sat);
+	dev_dbg(dev, "feature hue: %u\n",
+		  alvium->avail_ft.hue);
+	dev_dbg(dev, "feature whiteb: %u\n",
+		  alvium->avail_ft.whiteb);
+	dev_dbg(dev, "feature sharp: %u\n",
+		  alvium->avail_ft.sharp);
+	dev_dbg(dev, "feature auto_exp: %u\n",
+		  alvium->avail_ft.auto_exp);
+	dev_dbg(dev, "feature auto_gain: %u\n",
+		  alvium->avail_ft.auto_gain);
+	dev_dbg(dev, "feature auto_whiteb: %u\n",
+		  alvium->avail_ft.auto_whiteb);
+	dev_dbg(dev, "feature dev_temp: %u\n",
+		  alvium->avail_ft.dev_temp);
+	dev_dbg(dev, "feature acq_abort: %u\n",
+		  alvium->avail_ft.acq_abort);
+	dev_dbg(dev, "feature acq_fr: %u\n",
+		  alvium->avail_ft.acq_fr);
+	dev_dbg(dev, "feature fr_trigger: %u\n",
+		  alvium->avail_ft.fr_trigger);
+	dev_dbg(dev, "feature exp_acq_line: %u\n",
+		  alvium->avail_ft.exp_acq_line);
+}
+
+static void alvium_print_avail_bayer(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+
+	dev_dbg(dev, "avail bayer mono: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_MONO]);
+	dev_dbg(dev, "avail bayer gr: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GR]);
+	dev_dbg(dev, "avail bayer rg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_RG]);
+	dev_dbg(dev, "avail bayer gb: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_GB]);
+	dev_dbg(dev, "avail bayer bg: %u\n",
+		  alvium->is_bay_avail[ALVIUM_BIT_BAY_BG]);
+}
+
+static int alvium_get_feat_inq(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	struct alvium_avail_feat *f;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_FEATURE_INQUIRY_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to features inquiry reg\n");
+		return ret;
+	}
+
+	f = (struct alvium_avail_feat *) &val;
+
+	alvium->avail_ft.rev_x = f->rev_x;
+	alvium->avail_ft.rev_y = f->rev_y;
+	alvium->avail_ft.int_autop = f->int_autop;
+	alvium->avail_ft.black_lvl = f->black_lvl;
+	alvium->avail_ft.gain = f->gain;
+	alvium->avail_ft.gamma = f->gamma;
+	alvium->avail_ft.contrast = f->contrast;
+	alvium->avail_ft.sat = f->sat;
+	alvium->avail_ft.hue = f->rev_y;

drivers/media/i2c/alvium-csi2.c line 709

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	alvium->avail_ft.whiteb = f->whiteb;
+	alvium->avail_ft.sharp = f->sharp;
+	alvium->avail_ft.auto_exp = f->auto_exp;
+	alvium->avail_ft.auto_gain = f->auto_gain;
+	alvium->avail_ft.auto_whiteb = f->auto_whiteb;
+	alvium->avail_ft.dev_temp = f->dev_temp;
+	alvium->avail_ft.acq_abort = f->acq_abort;
+	alvium->avail_ft.acq_fr = f->acq_fr;
+	alvium->avail_ft.fr_trigger = f->fr_trigger;
+	alvium->avail_ft.exp_acq_line = f->exp_acq_line;

You could instead do:

	alvium->avail_ft = *f;
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

But is there an endianness issue here? I think you should use u64 also for the struct fields.

Mmmm.. I'm not sure are all u8 this is not safe (on endianess pesrpective)? Or I'm completely wrong?

The u8 fields in the struct are fine but the u64 value is not. Instead you should read a u8 array.

This is fixed with new CCI Api right??
If yes, this is fixed in v6.

Quoted 181 lines
Quoted 181 lines
Quoted 180 lines
Quoted 166 lines
drivers/media/i2c/alvium-csi2.c new file +163 -0
+
+	alvium_print_avail_feat(alvium);
+
+	return 0;
+}
+
+static int alvium_get_host_supp_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_LANE_COUNT_RW,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->h_sup_csi_lanes = (u8)val;
+	dev_dbg(dev, "host supported csi2 lanes count: %d\n",
+			  alvium->h_sup_csi_lanes);
+
+	return 0;
+}
+
+static int alvium_set_csi_lanes(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 num_lanes;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	num_lanes = alvium->ep.bus.mipi_csi2.num_data_lanes;
+
+	if (num_lanes <= alvium->h_sup_csi_lanes) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_LANE_COUNT_RW,
+				  num_lanes);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "Set n lanes = %llu", num_lanes);
+
+	return 0;
+}
+
+static int alvium_set_lp2hs_delay(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 lp2hs_delay;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	/*
+	 * The purpose of this reg is force a DPhy reset
+	 * for the period described by the microseconds on
+	 * the reg, before it starts streaming.
+	 *
+	 * To be clear, with that value bigger than 0 the
+	 * Alvium forces a dphy-reset on all lanes for that period.
+	 * That means all lanes go up into low power state.
+	 *
+	 * This may help a csi2 rx ip to reset if that IP
+	 * can't deal with a continous clock.
+	 */
+	lp2hs_delay = (u64)alvium->lp2hs_delay;
+
+	ret = alvium_write_hshake(alvium,
+			  REG_BCRM_STREAM_ON_DELAY_RW,
+			  lp2hs_delay);
+	if (ret) {
+		dev_err(dev, "Fail to set streamon delay reg\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int alvium_get_csi_clk_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->min_csi_clk = (u32)val;
+	dev_dbg(dev, "Min host supported csi clock: %d\n",
+			  alvium->min_csi_clk);
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_CSI2_CLOCK_MAX_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read host supported csi2 lanes reg\n");
+		return ret;
+	}
+
+	alvium->max_csi_clk = (u32)val;
+	dev_dbg(dev, "Max host supported csi clock: %d\n",
+			  alvium->max_csi_clk);
+
+	return 0;
+}
+
+static int alvium_set_csi_clk(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 csi_clk;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	csi_clk = (u32)alvium->ep.link_frequencies[0];
+
+	if (csi_clk > alvium->min_csi_clk ||
+		csi_clk < alvium->max_csi_clk) {
+		ret = alvium_write_hshake(alvium,
+				  REG_BCRM_CSI2_CLOCK_RW,
+				  csi_clk);
+		if (ret) {
+			dev_err(dev, "Fail to set csi lanes reg\n");
+			return ret;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	alvium->link_freq = alvium->ep.link_frequencies[0];
+
+	return 0;
+}
+
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+

drivers/media/i2c/alvium-csi2.c line 882

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 7 lines
Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

Mmmm... I'll give a try on this in v7

Quoted 43 lines

...

Quoted 40 lines
Quoted 39 lines
Quoted 27 lines
drivers/media/i2c/alvium-csi2.c new file +27 -0
+static int alvium_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
+	struct alvium_dev *alvium = sd_to_alvium(sd);
+	int ret;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUTOGAIN:
+		ret = alvium_set_ctrl_gain(alvium, ctrl->val);
+		break;
+	case V4L2_CID_EXPOSURE_AUTO:
+		ret = alvium_set_ctrl_exposure(alvium, ctrl->val);
+		break;
+	case V4L2_CID_AUTO_WHITE_BALANCE:
+		ret = alvium_set_ctrl_white_balance(alvium, ctrl->val);
+		break;
+	case V4L2_CID_HUE:
+		ret = alvium_set_ctrl_hue(alvium, ctrl->val);
+		break;
+	case V4L2_CID_CONTRAST:
+		ret = alvium_set_ctrl_contrast(alvium, ctrl->val);
+		break;
+	case V4L2_CID_SATURATION:
+		ret = alvium_set_ctrl_saturation(alvium, ctrl->val);
+		break;
+	case V4L2_CID_GAMMA:
+		ret = alvium_set_ctrl_gamma(alvium, ctrl->val);

drivers/media/i2c/alvium-csi2.c line 2987

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+		break;
+	case V4L2_CID_SHARPNESS:
+		ret = alvium_set_ctrl_sharpness(alvium, ctrl->val);
+		break;
+	case V4L2_CID_HFLIP:
+		ret = alvium_set_ctrl_hflip(alvium, ctrl->val);
+		break;
+	case V4L2_CID_VFLIP:
+		ret = alvium_set_ctrl_vflip(alvium, ctrl->val);
+		break;

No hsync or vsync controls?

Quoted 7 lines
Quoted 2 lines

This is not a sensor Alvium ISP/Vision core
is responsible for timings checking of the sensor mounted.

And those values are unknown then?

Generally raw cameras are controlled configuring the blanking. Of course if these aren't available, we can't expose them to user space either.

Right, these are not available.

Quoted 25 lines

...

Quoted 22 lines
Quoted 5 lines
Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	ret = fwnode_property_read_u32(dev_fwnode(dev),
+			  "alliedvision,lp2hs-delay-us",
+			  &alvium->lp2hs_delay);
+
+	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 3259

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	if (!endpoint) {
+		dev_err(dev, "endpoint node not found\n");
+		return -EINVAL;
+	}
+

As you have D-PHY, please assign bus type to D-PHY before parsing the endpoint.

I don't completely got this.
You mean, check:

if (alvium->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
		dev_err(dev, "unsupported bus type\n");
		return -EINVAL;
	}
Quoted 4 lines
Quoted 1 line

before parsing or I'm wrong?

Please read v4l2_fwnode_endpoint_alloc_parse() documentation in include/media/v4l2-fwnode.h .

I Think this is fixed in v6. Pls check.

Quoted 28 lines

...

Quoted 25 lines
Quoted 24 lines
Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+struct alvium_avail_mipi_fmt {
+	u8 yuv420_8_leg:1;
+	u8 yuv420_8:1;
+	u8 yuv420_10:1;
+	u8 yuv420_8_csps:1;
+	u8 yuv420_10_csps:1;
+	u8 yuv422_8:1;
+	u8 yuv422_10:1;
+	u8 rgb888:1;
+	u8 rgb666:1;

drivers/media/i2c/alvium-csi2.c

Quoted 10 lines
drivers/media/i2c/alvium-csi2.c new file +10 -0
+	u8 rgb565:1;
+	u8 rgb555:1;
+	u8 rgb444:1;
+	u8 raw6:1;
+	u8 raw7:1;
+	u8 raw8:1;
+	u8 raw10:1;
+	u8 raw12:1;
+	u8 raw14:1;
+	u8 jpeg:1;

I don't think C defines a particular location for the bit fields if their size is less than that of the type. Could you add padding to ensure that? Same for other structs.

Quoted 4 lines
Quoted 2 lines

I take "struct s5k5baf" of drivers/media/i2c/s5k5baf.c as reference. I'm missing something? Can you explain me better?

How is this related to the comment, other than that it uses bit fields?

Yes.

Quoted 39 lines
Quoted 39 lines

Thanks!

Quoted 22 lines
Quoted 22 lines
drivers/media/i2c/alvium-csi2.c new file +21 -0
+};
+
+struct alvium_avail_bayer {
+	u8 mono:1;
+	u8 gr:1;
+	u8 rg:1;
+	u8 gb:1;
+	u8 bg:1;
+};
+
+struct alvium_mode {
+	struct v4l2_rect crop;
+	struct v4l2_mbus_framefmt fmt;
+	u32 width;
+	u32 height;
+};
+
+struct alvium_pixfmt {
+	u8 id;
+	u32 code;
+	u32 colorspace;
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 5 lines
drivers/media/i2c/alvium-csi2.c new file +5 -0
+	u8 fmt_av_bit;
+	u8 bay_av_bit;
+	u64 mipi_fmt_regval;
+	u64 bay_fmt_regval;
+	u8 is_raw;

This would require less memory if you moved all 8-bit fields to the end of the struct.

Also Laurent and Cristophe point me about this, thanks.

Quoted 8 lines
Quoted 5 lines

If is possible I prefer to keep this order this is more clear for me.

But If not fit your plan never mind I pick your order. Thanks to all! :)

If you've received the same comment from three reviewers, then please do it. :-)

Pls check v6.

Resume:
I'm going to send v7 with some hints from Laurent, and some suggestion from you and Hans. Also I have to fix .h indentation.

Please meanwhile take your time to check v6. Thanks again for your review! :)

Regards,
Tommaso

Quoted 5 lines
Signature · 4 lines
-- 
Kind regards,

Sakari Ailus
#13 lore raw
In reply to Sakari Ailus
Quoted 49 lines · Sakari Ailus
Quoted 48 lines · Tommaso Merciai
Quoted 47 lines · Sakari Ailus
Quoted 46 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +0 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@

[snip]

Quoted 24 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 882

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

Quoted 6 lines

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

...

The best option is to print an error message in alvium_read() and drop all error messages from the callers.

Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#14 lore raw

Hi Laurent, Sakari,

Quoted 91 lines · Laurent Pinchart
Quoted 49 lines · Sakari Ailus
Quoted 48 lines · Tommaso Merciai
Quoted 47 lines · Sakari Ailus
Quoted 46 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +0 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@

[snip]

Quoted 29 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 882

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

Quoted 4 lines
Quoted 1 line

...

The best option is to print an error message in alvium_read() and drop all error messages from the callers.

What about don't print anything? We already have prints that comes from CCI API if some errors occurs. Laurent suggest me this into some previous comments. Let me know.

Thanks & Regards,
Tommaso

Quoted 5 lines
Signature · 4 lines
-- 
Regards,

Laurent Pinchart
#15 lore raw
Quoted 92 lines · Tommaso Merciai
Quoted 91 lines · Laurent Pinchart
Quoted 49 lines · Sakari Ailus
Quoted 48 lines · Tommaso Merciai
Quoted 47 lines · Sakari Ailus
Quoted 46 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +0 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@

[snip]

Quoted 29 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c line 882

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

...

The best option is to print an error message in alvium_read() and drop all error messages from the callers.

What about don't print anything? We already have prints that comes from CCI API if some errors occurs. Laurent suggest me this into some previous comments. Let me know.

We need to print something somewhere as silent failures are bad. The messages printed by the CCI helpers are good enough, so no need to print anything specific in the alvium driver.

Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#16 lore raw

Hi Laurent, Sakari,
Thanks for your comment.

Quoted 98 lines · Laurent Pinchart
Quoted 97 lines · Tommaso Merciai
Quoted 95 lines · Laurent Pinchart
Quoted 49 lines · Sakari Ailus
Quoted 48 lines · Tommaso Merciai
Quoted 47 lines · Sakari Ailus
Quoted 46 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +0 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@

[snip]

Quoted 24 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

Quoted 6 lines

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

...

The best option is to print an error message in alvium_read() and drop all error messages from the callers.

Quoted 7 lines
Quoted 3 lines

What about don't print anything? We already have prints that comes from CCI API if some errors occurs. Laurent suggest me this into some previous comments. Let me know.

We need to print something somewhere as silent failures are bad. The messages printed by the CCI helpers are good enough, so no need to print anything specific in the alvium driver.

Oooks I'll follow your way on v7, thanks Laurent. My plan is to switch to the following implementation:

static int alvium_get_img_width_params(struct alvium_dev *alvium)

{
	struct device *dev = &alvium->i2c_client->dev;
	u64 val;
	int ret = 0;

	if (!alvium->bcrm_addr)
		return -EINVAL;

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_MIN_R, &val, &ret);
	alvium->img_min_width = val;
	dev_dbg(dev, "Min img width: %d\n", alvium->img_min_width);

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_MAX_R, &val, NULL);
	alvium->img_max_width = val;
	dev_dbg(dev, "Max img width: %d\n", alvium->img_max_width);

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_INC_R, &val, &ret);
	alvium->img_inc_width = val;
	dev_dbg(dev, "img width increment: %d px\n", alvium->img_inc_width);

	return ret;
}

Like you suggest. :)
What do you think about?

Thanks again,
Tommaso

Quoted 5 lines
Signature · 4 lines
-- 
Regards,

Laurent Pinchart
#17 lore raw

Hi Tommaso,

Quoted 112 lines · Tommaso Merciai
Quoted 98 lines · Laurent Pinchart
Quoted 97 lines · Tommaso Merciai
Quoted 95 lines · Laurent Pinchart
Quoted 49 lines · Sakari Ailus
Quoted 48 lines · Tommaso Merciai
Quoted 47 lines · Sakari Ailus
Quoted 46 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +0 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@

[snip]

Quoted 24 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

Quoted 6 lines

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

...

The best option is to print an error message in alvium_read() and drop all error messages from the callers.

Quoted 7 lines
Quoted 3 lines

What about don't print anything? We already have prints that comes from CCI API if some errors occurs. Laurent suggest me this into some previous comments. Let me know.

We need to print something somewhere as silent failures are bad. The messages printed by the CCI helpers are good enough, so no need to print anything specific in the alvium driver.

Oooks I'll follow your way on v7, thanks Laurent. My plan is to switch to the following implementation:

static int alvium_get_img_width_params(struct alvium_dev *alvium)

Quoted 13 lines
{
	struct device *dev = &alvium->i2c_client->dev;
	u64 val;
	int ret = 0;

	if (!alvium->bcrm_addr)
		return -EINVAL;

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_MIN_R, &val, &ret);
	alvium->img_min_width = val;
	dev_dbg(dev, "Min img width: %d\n", alvium->img_min_width);

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_MAX_R, &val, NULL);

I assume you mean &ret and not NULL here.

Quoted 10 lines
	alvium->img_max_width = val;
	dev_dbg(dev, "Max img width: %d\n", alvium->img_max_width);

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_INC_R, &val, &ret);
	alvium->img_inc_width = val;
	dev_dbg(dev, "img width increment: %d px\n", alvium->img_inc_width);

	return ret;
}
Quoted 2 lines

Like you suggest. :)
What do you think about?

I would probably drop the debug messages, or at least group them all in a single message at the end of the function to print all three values. You should also not print the values if an error occurs during the reads, as they will be undefined.

In general, while debug messages are useful, they should be used with parcimony. Printing every single parameter passed by userspace, or adding a debug message at the beginning of every function, would make the kernel log very noisy and doesn't bring that much value.

Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#18 lore raw

Hi Laurent,

Quoted 123 lines · Laurent Pinchart

Hi Tommaso,

Quoted 120 lines · Tommaso Merciai
Quoted 98 lines · Laurent Pinchart
Quoted 97 lines · Tommaso Merciai
Quoted 95 lines · Laurent Pinchart
Quoted 49 lines · Sakari Ailus
Quoted 48 lines · Tommaso Merciai
Quoted 47 lines · Sakari Ailus
Quoted 46 lines · Tommaso Merciai

The Alvium camera is shipped with sensor + isp in the same housing. The camera can be equipped with one out of various sensor and abstract the user from this. Camera is connected via MIPI CSI-2.

Most of the camera module features are supported, with the main exception being fw update.

The driver provides all mandatory, optional and recommended V4L2 controls for maximum compatibility with libcamera

References:
- https://www.alliedvision.com/en/products/embedded-vision-solutions

---
Changes since v2:
- Removed gpios/clock handling as suggested by LPinchart
- Added vcc-ext-in supply support as suggested by LPinchart
- Fixed alvium_setup_mipi_fmt funct as suggested by CJAILLET
- Removed upside_down/hshake_bit priv data as suggested by CJAILLET
- Fixed commit body as suggested by LPinchart
- Mv alvium_set_streamon_delay to yalvium_set_lp2hs_delay
- Fixed comment on lp2hs prop as suggested by LPinchart
- Added pm resume/suspend functs as suggested by LPinchart
- Dropped alvium_link_setup/alvium_s_power as suggested by LPinchart
- Fixed regs defines as suggested by LPinchart
- Fixed typedef as suggested by LPinchart
- Dropped bcrm_v/fw_v from priv data as suggested by LPinchart
- Now driver use the subdev active state to store the active format and crop as suggested by LPinchart
- Dropped alvium_is_csi2/i2c_to_alvium as suggested by LPinchart

Changes since v3:
- Fixed warnings Reported-by: kernel test robot <[email protected]>

Changes since v4:
- Removed print into alvium_get_dt_data for alliedvision,lp2hs-delay-us as suggested by CDooley

 drivers/media/i2c/Kconfig       |   10 +
 drivers/media/i2c/Makefile      |    1 +
 drivers/media/i2c/alvium-csi2.c | 3479 +++++++++++++++++++++++++++++++
 drivers/media/i2c/alvium-csi2.h |  485 +++++
 4 files changed, 3975 insertions(+)
 create mode 100644 drivers/media/i2c/alvium-csi2.c
 create mode 100644 drivers/media/i2c/alvium-csi2.h

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/alvium-csi2.c new file +0 -0
new file mode 100644
index 000000000000..52c9263075cf
@@ -0,0 +1,3479 @@

[snip]

Quoted 24 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/media/i2c/alvium-csi2.c new file +12 -0
+static int alvium_get_img_width_params(struct alvium_dev *alvium)
+{
+	struct device *dev = &alvium->i2c_client->dev;
+	int ret;
+	u64 val;
+
+	if (!alvium->bcrm_addr)
+		return -EINVAL;
+
+	ret = alvium_read(alvium,
+			  REG_BCRM_IMG_WIDTH_MIN_R,
+			  &val);
Quoted 8 lines

drivers/media/i2c/alvium-csi2.c

Quoted 4 lines
drivers/media/i2c/alvium-csi2.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Fail to read img min width reg\n");
+		return ret;
+	}

Could you add a macro that assigns the value to the variable (or a struct field in this case) when the read is successful? Add the print if you think you need it.

Quoted 2 lines

I don't get this comment.
Can you explain me better your plan please.

Quoted 6 lines

You have exactly the same pattern repeated over and over in a number of functions. I'd like you to add a macro (or a function) that takes what varies as arguments, and call that function here. It would reduce a lot of the repeated lines code here.

...

The best option is to print an error message in alvium_read() and drop all error messages from the callers.

Quoted 7 lines
Quoted 3 lines

What about don't print anything? We already have prints that comes from CCI API if some errors occurs. Laurent suggest me this into some previous comments. Let me know.

We need to print something somewhere as silent failures are bad. The messages printed by the CCI helpers are good enough, so no need to print anything specific in the alvium driver.

Oooks I'll follow your way on v7, thanks Laurent. My plan is to switch to the following implementation:

static int alvium_get_img_width_params(struct alvium_dev *alvium)

{
	struct device *dev = &alvium->i2c_client->dev;
	u64 val;
	int ret = 0;

	if (!alvium->bcrm_addr)
		return -EINVAL;
Quoted 7 lines
Quoted 5 lines
	alvium_read(alvium, REG_BCRM_IMG_WIDTH_MIN_R, &val, &ret);
	alvium->img_min_width = val;
	dev_dbg(dev, "Min img width: %d\n", alvium->img_min_width);

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_MAX_R, &val, NULL);

I assume you mean &ret and not NULL here.

Yes, sorry.
Wrong paste.

Quoted 19 lines
Quoted 11 lines
	alvium->img_max_width = val;
	dev_dbg(dev, "Max img width: %d\n", alvium->img_max_width);

	alvium_read(alvium, REG_BCRM_IMG_WIDTH_INC_R, &val, &ret);
	alvium->img_inc_width = val;
	dev_dbg(dev, "img width increment: %d px\n", alvium->img_inc_width);

	return ret;
}
Quoted 2 lines

Like you suggest. :)
What do you think about?

I would probably drop the debug messages, or at least group them all in a single message at the end of the function to print all three values. You should also not print the values if an error occurs during the reads, as they will be undefined.

Quoted 4 lines

In general, while debug messages are useful, they should be used with parcimony. Printing every single parameter passed by userspace, or adding a debug message at the beginning of every function, would make the kernel log very noisy and doesn't bring that much value.

Ok, I'll try to collect your hints in v7.

Thanks,
Tommaso

Quoted 5 lines
Signature · 4 lines
-- 
Regards,

Laurent Pinchart