Skip to contentSkip to conversation

Add support for the Sony IMX681 camera sensor

PATCH Superseded Sergey Lebedev 2 participants 6 messages Nov 6

Subsystems (3)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover Add support for the Sony IMX681 camera sensor

The Sony IMX681 is the user-facing camera on the Microsoft Surface Pro 11 for Business (Intel Lunar Lake, IPU7), ACPI device SONY0681. Without a driver the camera does not appear at all.

  1/3  dt-bindings: media: Add Sony IMX681        (mine)
  2/3  media: i2c: Add Sony IMX681 sensor driver  (Andre's, co-developed)
  3/3  media: ipu-bridge: Add Sony IMX681         (mine)

The driver is Andre's, reverse-engineered from I2C traces taken under Windows.

Changes in v9
=============

Rebased on v7.4-rc1, where v8 no longer applied: imx576 went in next to 2/3's Makefile line. No functional change; imx681.c is byte for byte v8's.
- 2/3: the Makefile line follows imx678.
- 3/3: the bridge entry follows the IMX471 ones, in the table's order there. This also keeps it apart from the VD55G0 entry in [1], so the two series apply in either order.

All tags are kept. On v7.4-rc1, 2/3 and 3/3 build with W=1 and sparse clean, alone and together with [1]; the tests below are v8's.

While this sensor streams, IPU7's CSI-2 receiver logs "Received packet is too long" once per frame, and the frames are complete. Andre sees the same with his downstream driver, so it does not come from this series.

Tested with v8 on the Surface Pro 11 for Business, on media next 2dcdfb625c3 with KASAN and lockdep:
- the clock is INT3472:02-clk at 19.2 MHz, with the sensor as consumer;
- v4l2-compliance 46 of 46, no warnings;
- 3844x2640 at 30.01 fps;
- the four flip states give the four Bayer orders, and EBUSY while streaming;
- runtime suspend after streaming, and capture after unbind and bind;
- no KASAN or lockdep report from the driver;
- W=1 and sparse clean, dt_binding_check clean.

[1] https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/

Andre Gilerson (1):
media: i2c: Add Sony IMX681 sensor driver

Sergey Lebedev (2):
dt-bindings: media: Add Sony IMX681
media: ipu-bridge: Add Sony IMX681

 .../bindings/media/i2c/sony,imx681.yaml       |  108 ++
 MAINTAINERS                                   |    8 +
 drivers/media/i2c/Kconfig                     |   10 +
 drivers/media/i2c/Makefile                    |    1 +
 drivers/media/i2c/imx681.c                    | 1032 +++++++++++++++++
 drivers/media/pci/intel/ipu-bridge.c          |    2 +
 6 files changed, 1161 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml
 create mode 100644 drivers/media/i2c/imx681.c

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.54.0
#1 lore raw

PATCH v9 1/3 dt-bindings: media: Add Sony IMX681

The Sony IMX681 is a CMOS image sensor used as the user-facing camera on several Microsoft Surface devices. It operates from analog 2.8 V, digital 1.05 V and interface 1.8 V supplies, is programmable over I2C, and outputs 10-bit Bayer data over a two-lane MIPI CSI-2 D-PHY interface.

---
 .../bindings/media/i2c/sony,imx681.yaml       | 108 ++++++++++++++++++
 1 file changed, 108 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml
Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml new file +108 -0
new file mode 100644
index 00000000000..24c55760547
@@ -0,0 +1,108 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/sony,imx681.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sony IMX681 CMOS Image Sensor
+
+maintainers:
+  - Andre Gilerson <[email protected]>
+  - Sergey Lebedev <[email protected]>
+
+description:
+  The Sony IMX681 is a CMOS active pixel image sensor with a square pixel
+  array, used as the user-facing camera on several Microsoft Surface devices.
+  It operates from analog 2.8 V, digital 1.05 V and interface 1.8 V supplies,
+  is programmable over I2C, and outputs 10-bit Bayer data over a two-lane
+  MIPI CSI-2 D-PHY interface.
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: sony,imx681
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    description: Input clock (19.2 MHz)
+    maxItems: 1
+
+  avdd-supply:
+    description: Analog power supply (2.8 V)
+
+  dvdd-supply:
+    description: Digital power supply (1.05 V)
+
+  dovdd-supply:
+    description: Interface power supply (1.8 V)
+
+  reset-gpios:
+    description: Sensor reset (XCLR) GPIO, active low
+    maxItems: 1
+
+  port:
+    $ref: /schemas/graph.yaml#/$defs/port-base
+    unevaluatedProperties: false
+
+    properties:
+      endpoint:
+        $ref: /schemas/media/video-interfaces.yaml#
+        unevaluatedProperties: false
+
+        properties:
+          data-lanes:
+            items:
+              - const: 1
+              - const: 2
+
+        required:
+          - data-lanes
+          - link-frequencies
+
+    required:
+      - endpoint
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - avdd-supply
+  - dvdd-supply
+  - dovdd-supply
+  - port
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        camera-sensor@10 {
+            compatible = "sony,imx681";
+            reg = <0x10>;
+            clocks = <&clock_cam>;
+            avdd-supply = <&vcc2v8_cam>;
+            dvdd-supply = <&vcc1v05_cam>;
+            dovdd-supply = <&vcc1v8_cam>;
+            reset-gpios = <&gpio 4 GPIO_ACTIVE_LOW>;
+            orientation = <0>;
+            rotation = <0>;
+
+            port {
+                imx681_ep: endpoint {
+                    data-lanes = <1 2>;
+                    link-frequencies = /bits/ 64 <969600000>;
+                    remote-endpoint = <&csi_in>;
+                };
+            };
+        };
+    };
+...
Signature · 1 line
2.54.0
#2 lore raw

PATCH v9 2/3 media: i2c: Add Sony IMX681 sensor driver

In reply to Sergey Lebedev

From: Andre Gilerson <[email protected]>

Add a V4L2 sub-device driver for the Sony IMX681, a 10-bit Bayer sensor on a two-lane CSI-2 bus. It offers a single 3844x2640 mode at 30 fps and controls for exposure, analogue and digital gain, vertical blanking, both flips and the sensor's test patterns. The part is enumerated as ACPI device SONY0681 on Intel platforms and by the sony,imx681 compatible elsewhere.

There is no public documentation for this sensor. The initialisation sequence was recovered from I2C traces taken under Windows, so imx681_init_regs[] keeps the writes whose meaning is not known; the registers that are CCS are written by name instead. The pixel rate is measured on the part rather than computed from the CSI-2 link, because the pixel array is clocked from a second PLL - the comment above IMX681_PIXEL_RATE records the measurement.

---
 MAINTAINERS                |    8 +
 drivers/media/i2c/Kconfig  |   10 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/imx681.c | 1032 ++++++++++++++++++++++++++++++++++++
 4 files changed, 1051 insertions(+)
 create mode 100644 drivers/media/i2c/imx681.c
MAINTAINERS +8 -0
index 93abbb31506..12b378fe272 100644
@@ -26194,6 +26194,14 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/media/i2c/sony,imx678.yaml
 F:	drivers/media/i2c/imx678.c
 
+SONY IMX681 SENSOR DRIVER
+M:	Andre Gilerson <[email protected]>
+M:	Sergey Lebedev <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml
+F:	drivers/media/i2c/imx681.c
+
 SONY MEMORYSTICK SUBSYSTEM
 M:	Maxim Levitsky <[email protected]>
 M:	Alex Dubov <[email protected]>
drivers/media/i2c/Kconfig +10 -0
index 11c30252540..ef18c38215b 100644
@@ -332,6 +332,16 @@ config VIDEO_IMX678
 	  To compile this driver as a module, choose M here: the
 	  module will be called imx678.
 
+config VIDEO_IMX681
+	tristate "Sony IMX681 sensor support"
+	select V4L2_CCI_I2C
+	help
+	  This is a Video4Linux2 sensor driver for the Sony
+	  IMX681 camera.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called imx681.
+
 config VIDEO_MAX9271_LIB
 	tristate
 
drivers/media/i2c/Makefile +1 -0
index 59e31af457c..4abc1e96296 100644
@@ -65,6 +65,7 @@ obj-$(CONFIG_VIDEO_IMX415) += imx415.o
 obj-$(CONFIG_VIDEO_IMX471) += imx471.o
 obj-$(CONFIG_VIDEO_IMX576) += imx576.o
 obj-$(CONFIG_VIDEO_IMX678) += imx678.o
+obj-$(CONFIG_VIDEO_IMX681) += imx681.o
 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
 obj-$(CONFIG_VIDEO_IT6625) += it6625.o
drivers/media/i2c/imx681.c new file +1032 -0

1,032 lines not on this page · raw message

Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH v9 2/3] media: i2c: Add Sony IMX681 sensor driver

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] media: i2c: imx681: double imx681_power_off() on imx681_probe() error path --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: i2c: imx681: double imx681_power_off() on imx681_probe() error path --

commit c9e6568c02771cf63bede017166c475cf41cb773
Author: Andre Gilerson <[email protected]>

media: i2c: Add Sony IMX681 sensor driver

This commit adds a V4L2 sub-device driver for the Sony IMX681 10-bit Bayer sensor supporting 3844x2640 at 30 fps over a two-lane CSI-2 bus, with controls for exposure, gains, vertical blanking, flips, and test patterns.

Quoted 5 lines
drivers/media/i2c/imx681.c new file +0 -0
new file mode 100644
index 0000000000000..06fdf1856aecc

[ ... ]

Quoted 5 lines
drivers/media/i2c/imx681.c new file +5 -0
+static int imx681_probe(struct i2c_client *client)
+{
+	struct imx681 *imx681;
+	unsigned long xclk_freq;
+	int ret;

[ ... ]

Quoted 58 lines
drivers/media/i2c/imx681.c new file +58 -0
+	/* Power on and verify chip ID */
+	ret = imx681_power_on(imx681->dev);
+	if (ret)
+		return dev_err_probe(imx681->dev, ret, "power on failed\n");
+
+	ret = imx681_identify_module(imx681);
+	if (ret)
+		goto error_power_off;
+
+	/* Enable runtime PM */
+	pm_runtime_set_active(imx681->dev);
+	pm_runtime_enable(imx681->dev);
+	pm_runtime_set_autosuspend_delay(imx681->dev, 1000);
+	pm_runtime_use_autosuspend(imx681->dev);
+
+	/* Init V4L2 controls */
+	ret = imx681_init_controls(imx681);
+	if (ret)
+		goto error_pm;
+
+	/* Setup subdev */
+	imx681->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	imx681->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+	imx681->sd.internal_ops = &imx681_internal_ops;
+
+	/* Init media entity */
+	imx681->pad.flags = MEDIA_PAD_FL_SOURCE;
+	ret = media_entity_pads_init(&imx681->sd.entity, 1, &imx681->pad);
+	if (ret) {
+		ret = dev_err_probe(imx681->dev, ret,
+				    "media entity init failed\n");
+		goto error_handler_free;
+	}
+
+	imx681->sd.state_lock = imx681->ctrl_handler.lock;
+	ret = v4l2_subdev_init_finalize(&imx681->sd);
+	if (ret < 0) {
+		ret = dev_err_probe(imx681->dev, ret,
+				    "subdev init finalize failed\n");
+		goto error_media_entity;
+	}
+
+	ret = v4l2_async_register_subdev_sensor(&imx681->sd);
+	if (ret < 0) {
+		ret = dev_err_probe(imx681->dev, ret,
+				    "async register subdev failed\n");
+		goto error_subdev_cleanup;
+	}
+
+	pm_runtime_idle(imx681->dev);
+
+	return 0;
+
+error_subdev_cleanup:
+	v4l2_subdev_cleanup(&imx681->sd);
+error_media_entity:
+	media_entity_cleanup(&imx681->sd.entity);
+error_handler_free:

drivers/media/i2c/imx681.c line 981

Quoted 10 lines
drivers/media/i2c/imx681.c new file +10 -0
+	v4l2_ctrl_handler_free(imx681->sd.ctrl_handler);
+error_pm:
+	pm_runtime_dont_use_autosuspend(imx681->dev);
+	pm_runtime_disable(imx681->dev);
+	pm_runtime_set_suspended(imx681->dev);
+error_power_off:
+	imx681_power_off(imx681->dev);
+
+	return ret;
+}

[Severity: Medium]
Can imx681_probe() call imx681_power_off() twice on the error path?

When pm_runtime_set_autosuspend_delay() is called after pm_runtime_enable() with a usage_count of 0 (and use_autosuspend not yet set), update_autosuspend() invokes:

pm_runtime_set_autosuspend_delay()

    -> update_autosuspend()
      -> rpm_idle(dev, RPM_AUTO)
        -> rpm_suspend(dev, RPM_AUTO)
          -> imx681_power_off()

This synchronously suspends the device and disables xclk and the supplies during probe. Similarly, at the error_pm label, calling pm_runtime_dont_use_autosuspend() before pm_runtime_disable() also runs update_autosuspend() -> rpm_idle() -> rpm_suspend() -> imx681_power_off() if the device is still runtime-active.

If imx681_init_controls(), media_entity_pads_init(), v4l2_subdev_init_finalize(), or v4l2_async_register_subdev_sensor() fails, execution falls through error_pm to error_power_off and calls imx681_power_off(imx681->dev) a second time, underflowing the enable counts on imx681->xclk and imx681->supplies.

Would holding a runtime PM reference across probe (via pm_runtime_get_noresume() before pm_runtime_enable(), paired with pm_runtime_put_noidle() on error or pm_runtime_put_autosuspend() at the end of probe) or checking !pm_runtime_status_suspended() after pm_runtime_disable() as done in imx681_remove() avoid the double power-off?

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

PATCH v9 3/3 media: ipu-bridge: Add Sony IMX681

In reply to Sergey Lebedev

The Microsoft Surface Pro 11 for Business (Intel Lunar Lake, IPU7) carries a Sony IMX681 as its user-facing sensor, enumerated as ACPI device SONY0681. Add it so ipu_bridge_connect_sensors() builds its half of the graph; without the entry the sensor is never connected, whatever driver is present.

One link frequency, 969.6 MHz, on two CSI-2 lanes.

Assisted-by Claude:claude-opus-5-5
---
 drivers/media/pci/intel/ipu-bridge.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/pci/intel/ipu-bridge.c +2 -0
index 3739c4a2a8c..2759c776cfc 100644
@@ -124,6 +124,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	IPU_SENSOR_CONFIG("SONY471A", 1, 200000000),
 	/* Sony IMX471 (found on Lenovo X1 Carbon G14) */
 	IPU_SENSOR_CONFIG("TBE20A0", 1, 200000000),
+	/* Sony IMX681 */
+	IPU_SENSOR_CONFIG("SONY0681", 1, 969600000),
 	/* STMicroelectronics VD55G1 (found on Lenovo X1 Carbon G14) */
 	IPU_SENSOR_CONFIG("TBE20A1", 1, 402000000),
 	/* Toshiba T4KA3 */
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH v9 3/3] media: ipu-bridge: Add Sony IMX681

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] media: i2c: imx681: unbalanced power off on imx681_probe() error path --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: i2c: imx681: unbalanced power off on imx681_probe() error path --

commit 948d595796e70ce27a350282b1ce33a466c43a6f
Author: Sergey Lebedev <[email protected]>

media: ipu-bridge: Add Sony IMX681

This commit adds the SONY0681 ACPI sensor configuration to ipu_supported_sensors so ipu_bridge_connect_sensors() connects the Sony IMX681 sensor endpoint on the Microsoft Surface Pro 11 for Business.

drivers/media/pci/intel/ipu-bridge.c static const struct ipu_sensor_config ipu_supported_sensors[] = { · line 128

Quoted 10 lines
drivers/media/pci/intel/ipu-bridge.c +2 -0
index 3739c4a2a8c43..2759c776cfcae 100644
@@ -124,6 +124,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	IPU_SENSOR_CONFIG("SONY471A", 1, 200000000),
 	/* Sony IMX471 (found on Lenovo X1 Carbon G14) */
 	IPU_SENSOR_CONFIG("TBE20A0", 1, 200000000),
+	/* Sony IMX681 */
+	IPU_SENSOR_CONFIG("SONY0681", 1, 969600000),

[Severity: Medium]
With SONY0681 added to ipu_supported_sensors[], ipu_bridge_connect_sensors() populates the sensor endpoint so imx681_probe() in drivers/media/i2c/imx681.c proceeds past imx681_parse_endpoint(). Can this trigger an unbalanced imx681_power_off() call if a later step in imx681_probe() fails when CONFIG_PM is enabled?

In drivers/media/i2c/imx681.c, imx681_probe() powers on the sensor and enables runtime PM without taking a usage reference via pm_runtime_get_noresume():

drivers/media/i2c/imx681.c:imx681_probe() {
	...
	ret = imx681_power_on(imx681->dev);
	if (ret)
		return dev_err_probe(imx681->dev, ret, "power on failed\n");

	ret = imx681_identify_module(imx681);
	if (ret)
		goto error_power_off;

	/* Enable runtime PM */
	pm_runtime_set_active(imx681->dev);
	pm_runtime_enable(imx681->dev);
	pm_runtime_set_autosuspend_delay(imx681->dev, 1000);
	pm_runtime_use_autosuspend(imx681->dev);
	...
}

Because dev->power.usage_count is 0 and use_autosuspend is not yet set when pm_runtime_set_autosuspend_delay() runs, the call chain:

pm_runtime_set_autosuspend_delay()

    -> update_autosuspend()
      -> rpm_idle()
        -> rpm_suspend()
          -> imx681_power_off()

synchronously suspends the device and disables imx681->xclk and imx681->supplies.

If imx681_init_controls(), media_entity_pads_init(), v4l2_subdev_init_finalize(), or v4l2_async_register_subdev_sensor() subsequently fails, the error path jumps to error_pm and falls through to error_power_off:

drivers/media/i2c/imx681.c:imx681_probe() {
	...
error_pm:
	pm_runtime_dont_use_autosuspend(imx681->dev);
	pm_runtime_disable(imx681->dev);
	pm_runtime_set_suspended(imx681->dev);
error_power_off:
	imx681_power_off(imx681->dev);

	return ret;
}

Calling imx681_power_off() unconditionally a second time will call clk_disable_unprepare() and regulator_bulk_disable() on already-disabled resources. Should imx681_probe() hold a runtime PM reference across probe (via pm_runtime_get_noresume() before pm_runtime_enable(), paired with pm_runtime_put_autosuspend() on success and pm_runtime_put_noidle() in error_pm), or guard imx681_power_off() with !pm_runtime_status_suspended(imx681->dev) as in imx681_remove()?

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