Skip to contentSkip to conversation

Add support for the Sony IMX681 camera sensor

PATCH Superseded Sergey Lebedev 2 participants 5 messages Sep 23

Subsystems (3)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v6 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 Gilerson's)
  3/3  media: ipu-bridge: Add Sony IMX681             (mine)

The driver is Andre's work, reverse-engineered from I2C traces taken under Windows. I am carrying the submission, not the code.

Changes in v6
=============

Sakari Ailus's review of v5 2/3, all of it bar two points I have answered in that thread instead of applying.

The pixel rate is measured now, not computed. He saw that the part runs two PLLs, so a rate taken from the CSI-2 link describes the wrong clock: the old value implied 16.16 fps against a cover claiming thirty. The sensor times at 29.95 fps, which at LINE_LENGTH_PCK x FRAME_LENGTH_LINES is 720,000,000. Deriving it from the video-timing PLL instead does not work either: its dividers are at the CCS addresses without the CCS meaning, and the formula gives 180 MHz for a part running at thirty frames a second. So v6 carries the measurement, and a comment saying why there is no formula.

The CCS registers are named for what ccs-regs.h calls them, which turned up two more partial writes: 0x0307 is the low byte of the 16-bit PLL_MULTIPLIER and 0x030d the low byte of OP_PRE_PLL_CLK_DIV, each right only while its high byte held the reset value - the same fault the crop registers had in v5. Both are written whole now, after reading them back to confirm nothing moves. imx681_init_regs[] keeps only what is the vendor's.

Blanking, orientation and the first exposure come from controls rather than that list: HBLANK and VBLANK give line and frame length, both flips drive IMAGE_ORIENTATION and the media bus code. Group hold is gone - it wrapped single-register writes and bought nothing. The exposure maximum is a margin below the frame length, so a dynamic frame length works. The rest follows his
list: the runtime-PM shape in probe and remove, endpoint parsing by id, the debug prints, the error paths.

2/3's commit message was rewritten to describe the patch; what it used to carry is here.

Two changes are not from that review. Both are things the driver told userspace that were not true, and both were found by asking the part.

imx681_get_selection() answered all four targets with the same rectangle at the origin, while the init sequence puts the window at (100, 256) - which became obvious once the CCS registers were named. The part settles it:
X_ADDR_MIN, X_ADDR_MAX, Y_ADDR_MIN and Y_ADDR_MAX read 8, 4039, 64 and 3087, so 4032x3024 of readable array at (8, 64) with the 3844x2640 window inside it. CROP and CROP_DEFAULT now report the window, CROP_BOUNDS the readable area, and NATIVE_SIZE the smallest rectangle containing that, since what lies beyond is undocumented. libcamera stops defaulting PixelArraySize and PixelArrayActiveAreas as a result.

And the test-pattern menu offered two modes that cannot work. "Solid Colour" shows TEST_DATA_RED and its three neighbours, which reset to zero and which nothing ever wrote, so the mode could only ever produce a black frame; the four V4L2_CID_TEST_PATTERN_* controls are added, defaulting to white as imx219 does, and the mode now shows what it is asked for. PN9 produced a black frame where modes 2 and 3 produced their patterns, and TEST_PATTERN_CAPABILITY reads zero on this part so it cannot be asked, so that entry is dropped rather than advertised.

Checked
=======

Built into a kernel from this series and booted on the machine. 2/3's imx681.c and the file that built the running module hash to the same bytes. That kernel also carries the vd55g0 and ov13858 work in flight on this list, so the numbers below come from a build with all of it in - and imx681.c in that build is byte for byte what 2/3 adds.

v4l2-compliance 1.32.0 on the sensor subdev:
Total for device /dev/v4l-subdev5: 46, Succeeded: 46, Failed: 0, Warnings: 0

400 frames at 29.97 fps, and the sensor's own eight-bar test pattern reads mean 124.1 of 255 with eleven distinct values across a row. A live frame takes longer than that to arrive: from a cold boot the exposure starts at the minimum, and libcamera's generic AGC needs about four hundred frames to open up - at three hundred it is still at exposure 136 of 3173 and the frame is black. After that it renders as the room. Runtime PM reads suspended with usage 0 after probe, active with usage 1 while streaming, and back after.

checkpatch --strict, sparse and W=1 are clean on 2/3 and 3/3; 1/3's only warning asks whether MAINTAINERS needs updating, which 2/3 does.

Also built with KASAN, PROVE_LOCKING, DEBUG_ATOMIC_SLEEP and DEBUG_OBJECTS and exercised on that kernel: compliance again 46 of 46, then streaming, start and stop four times over, every control including the new ones, unbind and rebind, and an s2idle suspend and resume with a capture after it. No splat from this driver in any of it. The one the boot does produce is a circular locking dependency in iwlmld's thermal zone, which is not ours.

The three control laws came off the Windows traces and had never been checked against the part, so they were measured on it - raw V4L2 off the ISYS node with the IPA out of the way, since libcamera's AGC otherwise overwrites exposure and gain. Analogue gain follows 1024/(1024-code) to within 0.5% over eleven codes, and code 960, the cap, is 16.008x. Digital gain follows value/256 to within 0.5% from 1x to 16x. Exposure fits 0.043814 x lines + 0.045, an intercept worth 0.10% of the signal at a thousand lines, with residuals inside 0.14% from 400 lines upward.

Limitations: one machine, one sensor sample, one mode (3844x2640, SRGGB10 by default and following the flips, 969.6 MHz per lane), dummy dvdd and dovdd from INT3472 so the fatal regulator path is reasoned rather than exercised, and no IVSC HID present.

Still open: Andre Gilerson's ack on the binding, which Krzysztof asked for and which is properly his. He is back from 28 September and is on Cc.

Based on media/next at aedd77ea8168 ("media: qcom: camss: use fwnode_graph_for_each_endpoint_scoped() to simplify code"), still the tip of that branch today.

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       |  107 ++
 MAINTAINERS                                   |    7 +
 drivers/media/i2c/Kconfig                     |   10 +
 drivers/media/i2c/Makefile                    |    1 +
 drivers/media/i2c/imx681.c                    | 1003 +++++++++++++++++
 drivers/media/pci/intel/ipu-bridge.c          |    2 +
 6 files changed, 1130 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml
 create mode 100644 drivers/media/i2c/imx681.c
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH v6 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       | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml
Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml new file +107 -0
new file mode 100644
index 00000000000..6d2e4ddcee9
@@ -0,0 +1,107 @@
+# 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]>
+
+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 (Apple Git-157)
#2 lore raw

PATCH v6 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                |    7 +
 drivers/media/i2c/Kconfig  |   10 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/imx681.c | 1003 ++++++++++++++++++++++++++++++++++++
 4 files changed, 1021 insertions(+)
 create mode 100644 drivers/media/i2c/imx681.c
MAINTAINERS +7 -0
index 4cc4a2dc6d3..4479f96d0d1 100644
@@ -25609,6 +25609,13 @@ 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]>
+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 4d994647916..c759a2d2398 100644
@@ -321,6 +321,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 fd1cb25718c..98bcecf0c43 100644
@@ -64,6 +64,7 @@ obj-$(CONFIG_VIDEO_IMX412) += imx412.o
 obj-$(CONFIG_VIDEO_IMX415) += imx415.o
 obj-$(CONFIG_VIDEO_IMX678) += imx678.o
 obj-$(CONFIG_VIDEO_IMX471) += imx471.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 +1003 -0

1,003 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] media: i2c: imx681: missing clock rate validation - [Medium] media: i2c: imx681: flip controls…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] media: i2c: imx681: missing clock rate validation
- [Medium] media: i2c: imx681: flip controls silently ignored during streaming --

commit 9302e1005f9a8ea647ef15f49b763c01aec8d79f
Author: Andre Gilerson <[email protected]>

media: i2c: Add Sony IMX681 sensor driver

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.

Quoted 6 lines
drivers/media/i2c/imx681.c new file +0 -0
new file mode 100644
index 0000000000000..3be96965c53ec
@@ -0,0 +1,1003 @@

[ ... ]

Quoted 4 lines
drivers/media/i2c/imx681.c new file +4 -0
+static int imx681_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct imx681 *imx681 = container_of(ctrl->handler, struct imx681,
+					     ctrl_handler);

[ ... ]

Quoted 1 line
drivers/media/i2c/imx681.c new file +1 -0
+	switch (ctrl->id) {

[ ... ]

drivers/media/i2c/imx681.c line 324

Quoted 4 lines
drivers/media/i2c/imx681.c new file +4 -0
+	case V4L2_CID_HFLIP:
+	case V4L2_CID_VFLIP:
+		/* Read from imx681_start_streaming(); the layout changes. */
+		break;

[Severity: Medium]
Will this silently ignore flip control changes while the sensor is streaming?

When userspace modifies V4L2_CID_HFLIP or V4L2_CID_VFLIP via VIDIOC_S_CTRL during streaming, returning 0 here without updating the hardware tells the V4L2 core the change succeeded. This can cause the V4L2 control state to diverge from the actual hardware state, breaking pipeline format validation because the reported Bayer format no longer matches the sensor's actual output.

Should the driver call v4l2_ctrl_grab(..., true) when streaming starts to prevent these modifications?

[ ... ]

Quoted 8 lines
drivers/media/i2c/imx681.c new file +8 -0
+static int imx681_probe(struct i2c_client *client)
+{
+	struct imx681 *imx681;
+	int ret;
+
+	imx681 = devm_kzalloc(&client->dev, sizeof(*imx681), GFP_KERNEL);
+	if (!imx681)
+		return -ENOMEM;

[ ... ]

drivers/media/i2c/imx681.c line 857

Quoted 8 lines
drivers/media/i2c/imx681.c new file +8 -0
+	/* Initialise CCI regmap for 16-bit register addresses */
+	imx681->cci = devm_cci_regmap_init_i2c(client, 16);
+	if (IS_ERR(imx681->cci))
+		return dev_err_probe(imx681->dev, PTR_ERR(imx681->cci),
+				     "failed to init CCI\n");
+
+	/* Get clock (optional - INT3472 provides it on Surface devices) */
+	imx681->xclk = devm_clk_get_optional(imx681->dev, NULL);

[Severity: Medium]
Does this clock need rate validation?

The sensor's PLL and CSI-2 link timings appear to be configured assuming an exact 19.2 MHz input clock. If a different clock rate is provided (e.g., 24 MHz from a device tree or ACPI configuration), this probe will silently succeed, but the sensor might output garbage or fail to link.

Should we check the rate using clk_get_rate() here?

Quoted 3 lines
drivers/media/i2c/imx681.c new file +3 -0
+	if (IS_ERR(imx681->xclk))
+		return dev_err_probe(imx681->dev, PTR_ERR(imx681->xclk),
+				     "failed to get clock\n");
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH v6 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.

---
 drivers/media/pci/intel/ipu-bridge.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/pci/intel/ipu-bridge.c +2 -0
index 77257311d1d..a1a4023f813 100644
@@ -116,6 +116,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	IPU_SENSOR_CONFIG("OVTI5693", 1, 419200000),
 	/* Omnivision OV8856 */
 	IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
+	/* Sony IMX681 */
+	IPU_SENSOR_CONFIG("SONY0681", 1, 969600000),
 	/* Sony IMX471 */
 	IPU_SENSOR_CONFIG("SONY471A", 1, 200000000),
 	/* Sony IMX471 (found on Lenovo X1 Carbon G14) */
Signature · 1 line
2.54.0 (Apple Git-157)