From nobody Tue Nov 24 04:33:13 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.116 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793969137; b=E8bjtFnTXGmy3dexYchl1DROLSD7mx6pevM3FOdPvmbERRnUuW5rvkpiFXxV/GvwMAm3 Q+RekD/c0QopPNK/fNnIPNAeOYzKx45h/fSder6taGRlLqDrCjJu90XPeuZ/g49Zurqqt 7NjeQWqFWuUMpzp63AzgyrHo5tVe7fOTW69dyvefJDSkqzcolxkP7C9b4iuQ7SfvW5vUT grZSoXyxFGVKuMFjH1xoQVHfJomQYZPuqa0crMkMSu1M1xMR4KwGJhypvZxcjB7/kUtlk 8oBcVMYdnXOeagNYGgrJ9xkGemLdiZ02s/M/rB7EZI3Bcz3s1ZcLRaOh7vMar6o8RaA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793969137; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=mus20XM8D+qKMgV/iA9VhjQY13W0kUKsfKS6jbgQWf4=; b=kU13ToE7h12QUGTbq9941U26muh7M2G3ya7Mfrhb8LxmBfyOvwgHZ+4L0lpnorK4W5V/ MknaSfYvRLmpaxLeNsH3/p2gcVmOPBqFjTHgS31ku9zGvSZ0/lX7RoystHOwT8mYGCgwW pwyv6yGAXpHGjE2TIr6Zu+8lHhawolg/af9vgz0gNAITxX8Mlme/MXKnMUKLO01hn+AMv 6L+5/9DGL/3uMe4uz7EObg6erGCL/CZ1tsBtDJCXC6gQgFx8CMZmgvo1rlnKjkxrkXtu1 Q32JynsUXpFq7UuZ5iLQcKRs/l8xIlE89DYOrXFzvq4+rvLtiLpHLTdaBZSCQ0NIraw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass header.d=pm.me header.i=@pm.me header.a=rsa-sha256 header.s=protonmail3 header.b=EtNfSauM; arc=none smtp.remote-ip=109.224.244.116 Received: from mail-244116.protonmail.ch (mail-244116.protonmail.ch [109.224.244.116]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A446606A339 for ; Fri, 6 Nov 2026 12:45:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1A446606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=pm.me header.i=@pm.me header.a=rsa-sha256 header.s=protonmail3 header.b=EtNfSauM DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1793969134; x=1794228334; bh=mus20XM8D+qKMgV/iA9VhjQY13W0kUKsfKS6jbgQWf4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=EtNfSauMKUgWngbyZAjNYkBm7QiR7cEXAm0m9e4dRpAYUXuB537+thCepnfIbkjAQ 1Utlg728AGGPkVuvRNuV1bmK5Y0g+n+sfXvBMKKuHzuvBhoYAysO1nUT+PUeSTRgxJ qU6YiXNgbfFNLazinAAImD/hxltc6F5/wj5CNM9N+TOt4EYCfPeMnzRwAeJA+q/rfi 9wbKLFnUkfjuG6Lxp3yQQ+WhoihODC1CambnGyOlwxbvfKPTPdmN4VBI3IBS/4qtoa +wJ0IhV5QzDRNCpGK3KPm2CBGF18DKN2QX5A6+yzUwbQRB+K6J77+ixPInEvOdSJaw 8j3dCkxOf3VeQ== Date: Fri, 06 Nov 2026 12:45:29 +0000 To: Sakari Ailus , Mauro Carvalho Chehab , Andre Gilerson , Dan Scally From: Sergey Lebedev Cc: Hans de Goede , Rob Herring , Krzysztof Kozlowski , Conor Dooley , German Pablo Lindo , George Neacsu , sashiko-bot@kernel.org, linux-media@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 1/3] dt-bindings: media: Add Sony IMX681 Message-ID: <20261106124505.71429-2-lsa.uz@pm.me> In-Reply-To: <20261106124505.71429-1-lsa.uz@pm.me> References: <20261106124505.71429-1-lsa.uz@pm.me> Feedback-ID: 113843758:user:proton X-Pm-Message-ID: 871190daa2fc31ce0cacccd47fed5741b595f31c X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Sergey Lebedev Reviewed-by: Krzysztof Kozlowski Acked-by: Andre Gilerson --- .../bindings/media/i2c/sony,imx681.yaml | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/sony,imx681= .yaml diff --git a/Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml b= /Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml new file mode 100644 index 00000000000..24c55760547 --- /dev/null +++ b/Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml @@ -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 + - Sergey Lebedev + +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 devic= es. + It operates from analog 2.8 V, digital 1.05 V and interface 1.8 V suppli= es, + 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 + + i2c { + #address-cells =3D <1>; + #size-cells =3D <0>; + + camera-sensor@10 { + compatible =3D "sony,imx681"; + reg =3D <0x10>; + clocks =3D <&clock_cam>; + avdd-supply =3D <&vcc2v8_cam>; + dvdd-supply =3D <&vcc1v05_cam>; + dovdd-supply =3D <&vcc1v8_cam>; + reset-gpios =3D <&gpio 4 GPIO_ACTIVE_LOW>; + orientation =3D <0>; + rotation =3D <0>; + + port { + imx681_ep: endpoint { + data-lanes =3D <1 2>; + link-frequencies =3D /bits/ 64 <969600000>; + remote-endpoint =3D <&csi_in>; + }; + }; + }; + }; +... --=20 2.54.0 From nobody Tue Nov 24 04:33:13 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.70.43.102 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793969146; b=qs5WI5lS2KpwQ3R3AFX63/tEqM2KzRSJikiOZ5w8Hd7vn8xpnwxqGIDH83NA3Bg8i/vD Dz95uYFKcNw1jgguXQi5PX8VzGTryZe/BlafFShiAqh3Zgvj5Pc+CPoSnF3MULpwZtLaL 0Ih8f5E4MuYO4ZJ+KyMThho1KOHlDMyrKOZ/+uL1XxHZMIeXyq/N7avBtA0OdVciEFDWC aGK03Rt+MMpwxkJb972choaePaBU0S+6ZfkzhR3GWl2+acg5J6RCFc+fqjeF1bknpEMre RWqhYwGQ6mx+oJt3ScRoqQYyqZJIO86hDOUh8iTzu23/o2A4VV/7rPRsFKyW8WqgsHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793969146; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=TwkrIaP59pdsvbiRw1v6XGpnKdkb+yG7enUkE9bHQO8=; b=hvFeFbc4bdWqy2Mv/A0YTuRyuwwNMpRi1WeCMepnOOstTSQbBm6H8HPf8tJUFqZawEDn qX9hQna1nNGNeAiPw4KuTK6bQgS6OqkcqPCzzK1toWFK52KkE6cZt2hVvj4NH2TNHreBU 9OCsUGI+tezlMR4Do4FI/1nx4Jtqn/cFkr6hYJwGbORLmeuAyAewv+pT8d7TiZGXYuxby dajV3eTFxOdf9VmjBiazZlZ4BAiBoPfNJ7bblXBCz9qopgd4XfzSoaAHgTZiyt7x7SvRj HZHIKuEsv2VVPWvLpREIvQSEiHvgUTUaSCUrjI3N7Gz0EUmQCvTQa0UAYq982VaeIQg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass header.d=pm.me header.i=@pm.me header.a=rsa-sha256 header.s=protonmail3 header.b=RnNN2Wr4; arc=none smtp.remote-ip=185.70.43.102 Received: from mail-43102.protonmail.ch (mail-43102.protonmail.ch [185.70.43.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91F40606A326 for ; Fri, 6 Nov 2026 12:45:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 91F40606A326 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=pm.me header.i=@pm.me header.a=rsa-sha256 header.s=protonmail3 header.b=RnNN2Wr4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1793969142; x=1794228342; bh=TwkrIaP59pdsvbiRw1v6XGpnKdkb+yG7enUkE9bHQO8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=RnNN2Wr4m6C+zrFBoIrXZw3vCQwMQz5rwSDGvLVpYuqIMwiG3WJEamFMqo7T6N9MS LJR1lL+WTq5jeXLs95OW7cacTYhk5zcjzqVM7LpocPUl9BuYfOdSIcQIi6fzxov4wZ sBVN3M+iXGwNVCOEWm4VbvriMccGyxcfJYzMlvNQM7hPdKFahGypCaRvBSUEW9xKCH GRD8MDj+EK4lybzj4PPPZ8Py5HdCvI7ppt2qkXSKQ+bhX4Y1Zf+vAnomqkqrdEGVXY +qwAmxfjG7GjNXbeDxTuAUe/KflbWneT61jSuLWISlKo9p60vvsQ9+sSt+enf5h2IN gZAaREdbQsqGw== Date: Fri, 06 Nov 2026 12:45:38 +0000 To: Sakari Ailus , Mauro Carvalho Chehab , Andre Gilerson , Dan Scally From: Sergey Lebedev Cc: Hans de Goede , Rob Herring , Krzysztof Kozlowski , Conor Dooley , German Pablo Lindo , George Neacsu , sashiko-bot@kernel.org, linux-media@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 2/3] media: i2c: Add Sony IMX681 sensor driver Message-ID: <20261106124505.71429-3-lsa.uz@pm.me> In-Reply-To: <20261106124505.71429-1-lsa.uz@pm.me> References: <20261106124505.71429-1-lsa.uz@pm.me> Feedback-ID: 113843758:user:proton X-Pm-Message-ID: a3c4fadbff93497c568bb862b13dc0c77a741c60 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Andre Gilerson 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. Signed-off-by: Andre Gilerson Co-developed-by: Sergey Lebedev Signed-off-by: Sergey Lebedev Tested-by: German Pablo Lindo --- MAINTAINERS | 8 + drivers/media/i2c/Kconfig | 10 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/imx681.c | 1034 ++++++++++++++++++++++++++++++++++++ 4 files changed, 1053 insertions(+) create mode 100644 drivers/media/i2c/imx681.c diff --git a/MAINTAINERS b/MAINTAINERS index 93abbb31506..12b378fe272 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -26194,6 +26194,14 @@ S: Maintained F: Documentation/devicetree/bindings/media/i2c/sony,imx678.yaml F: drivers/media/i2c/imx678.c =20 +SONY IMX681 SENSOR DRIVER +M: Andre Gilerson +M: Sergey Lebedev +L: linux-media@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/media/i2c/sony,imx681.yaml +F: drivers/media/i2c/imx681.c + SONY MEMORYSTICK SUBSYSTEM M: Maxim Levitsky M: Alex Dubov diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index 11c30252540..ef18c38215b 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -332,6 +332,16 @@ config VIDEO_IMX678 To compile this driver as a module, choose M here: the module will be called imx678. =20 +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 =20 diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index 59e31af457c..4abc1e96296 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -65,6 +65,7 @@ obj-$(CONFIG_VIDEO_IMX415) +=3D imx415.o obj-$(CONFIG_VIDEO_IMX471) +=3D imx471.o obj-$(CONFIG_VIDEO_IMX576) +=3D imx576.o obj-$(CONFIG_VIDEO_IMX678) +=3D imx678.o +obj-$(CONFIG_VIDEO_IMX681) +=3D imx681.o obj-$(CONFIG_VIDEO_IR_I2C) +=3D ir-kbd-i2c.o obj-$(CONFIG_VIDEO_ISL7998X) +=3D isl7998x.o obj-$(CONFIG_VIDEO_IT6625) +=3D it6625.o diff --git a/drivers/media/i2c/imx681.c b/drivers/media/i2c/imx681.c new file mode 100644 index 00000000000..0aa80a66393 --- /dev/null +++ b/drivers/media/i2c/imx681.c @@ -0,0 +1,1034 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Sony IMX681 CMOS Image Sensor Driver + * + * Front camera on Surface Pro 11 Business (Intel/Lunar Lake). + * Register sequences reverse-engineered from Windows I2C traces. + * + * Copyright (C) 2025 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/* Chip ID register and expected value */ +#define IMX681_REG_CHIP_ID CCI_REG16(0x0016) +#define IMX681_CHIP_ID 0x0681 + +/* Mode select */ +#define IMX681_REG_MODE_SELECT CCI_REG8(0x0100) +#define IMX681_MODE_STANDBY 0x00 +#define IMX681_MODE_STREAMING 0x01 + +/* Exposure (coarse integration time, 24-bit) */ +#define IMX681_REG_EXPOSURE CCI_REG24(0x0229) +/* + * The external clock frequency, in MHz, as a Q8.8 fixed-point number: the + * sensor takes 19.2 MHz as 19 in the high byte and 0.2 * 256 in the low o= ne. + */ +#define IMX681_XCLK_FREQ 19200000 +#define IMX681_XCLK_FREQ_ENC ((IMX681_XCLK_FREQ / 1000000) << 8 | \ + (IMX681_XCLK_FREQ % 1000000) * 256 / 1000000) + +#define IMX681_EXPOSURE_MIN 4 +/* The exposure may not come within this many lines of the frame length. */ +#define IMX681_EXPOSURE_MARGIN 4 +#define IMX681_EXPOSURE_DEFAULT IMX681_EXPOSURE_MIN + +/* Analog gain */ +#define IMX681_REG_ANALOG_GAIN CCI_REG16(0x0204) +#define IMX681_ANA_GAIN_MIN 0 +#define IMX681_ANA_GAIN_MAX 960 /* 16x, where the analogue stage ends */ +#define IMX681_ANA_GAIN_DEFAULT 0 + +/* Digital gain */ +#define IMX681_REG_DIGITAL_GAIN CCI_REG16(0x020E) +#define IMX681_DIG_GAIN_MIN 0x0100 /* 1.0x */ +#define IMX681_DIG_GAIN_MAX 0x0FFF +#define IMX681_DIG_GAIN_DEFAULT 0x0100 + +/* Test pattern, and the colour the solid-colour mode shows */ +#define IMX681_REG_TEST_PATTERN CCI_REG16(0x0600) +#define IMX681_REG_TESTP_RED CCI_REG16(0x0602) +#define IMX681_REG_TESTP_GREENR CCI_REG16(0x0604) +#define IMX681_REG_TESTP_BLUE CCI_REG16(0x0606) +#define IMX681_REG_TESTP_GREENB CCI_REG16(0x0608) +#define IMX681_TESTP_COLOUR_MIN 0 +#define IMX681_TESTP_COLOUR_MAX 0x03ff +#define IMX681_TESTP_COLOUR_STEP 1 + +/* + * Frame length and exposure are 24-bit and at vendor addresses; everything + * else this driver touches is a CCS register at its CCS address, and is + * named for it here. + */ +#define IMX681_REG_FRAME_LENGTH CCI_REG24(0x033D) +#define IMX681_REG_LINE_LENGTH CCI_REG16(0x0342) +#define IMX681_REG_ORIENTATION CCI_REG8(0x0101) +#define IMX681_REG_CSI_SIGNALING_MODE CCI_REG8(0x0111) +#define IMX681_REG_EXTCLK_FREQ_MHZ CCI_REG16(0x0136) +#define IMX681_REG_PLL_MULTIPLIER CCI_REG16(0x0306) +#define IMX681_REG_OP_PRE_PLL_CLK_DIV CCI_REG16(0x030c) +#define IMX681_REG_OP_PLL_MULTIPLIER CCI_REG16(0x030e) +#define IMX681_REG_X_ADDR_START CCI_REG16(0x0344) +#define IMX681_REG_Y_ADDR_START CCI_REG16(0x0346) +#define IMX681_REG_X_ADDR_END CCI_REG16(0x0348) +#define IMX681_REG_Y_ADDR_END CCI_REG16(0x034a) +#define IMX681_REG_X_OUTPUT_SIZE CCI_REG16(0x034c) +#define IMX681_REG_Y_OUTPUT_SIZE CCI_REG16(0x034e) +#define IMX681_REG_DIGITAL_CROP_WIDTH CCI_REG16(0x040c) +#define IMX681_REG_DIGITAL_CROP_HEIGHT CCI_REG16(0x040e) + +/* PLL, as the Windows traces configure it and the part reads back. */ +#define IMX681_PLL_MULTIPLIER 225 +#define IMX681_OP_PRE_PLL_CLK_DIV 3 +#define IMX681_OP_PLL_MULTIPLIER 303 + +/* + * Where the read-out window sits in the pixel array, and how much array + * there is. The bounds are the part's own answer: X_ADDR_MIN, X_ADDR_MAX, + * Y_ADDR_MIN and Y_ADDR_MAX at the CCS addresses read 8, 4039, 64 and 308= 7, + * so 4032x3024 of readable pixels at (8, 64), and the window below sits + * inside that. What lies beyond the readable area is not documented, so t= he + * native size is the smallest rectangle that contains it. + */ +#define IMX681_CROP_X_START 100 +#define IMX681_CROP_Y_START 256 +#define IMX681_PIXEL_ARRAY_LEFT 8 +#define IMX681_PIXEL_ARRAY_TOP 64 +#define IMX681_PIXEL_ARRAY_WIDTH 4032 +#define IMX681_PIXEL_ARRAY_HEIGHT 3024 +#define IMX681_NATIVE_WIDTH 4040 +#define IMX681_NATIVE_HEIGHT 3088 + +/* Image dimensions =E2=80=94 native sensor output */ +#define IMX681_WIDTH 3844 +#define IMX681_HEIGHT 2640 +#define IMX681_LINE_LENGTH_PCK 7552 /* 0x1D80 */ +#define IMX681_FRAME_LENGTH_LINES 3177 /* 0x0C69 */ +#define IMX681_FRAME_LENGTH_MAX 0xFFFF /* 24-bit reg, limit to 16-bit */ + +/* MIPI lanes */ +#define IMX681_NUM_LANES 2 + +/* + * Link frequency derived from PLL settings in Windows trace: + * EXCK=3D19.2MHz, PLL2_MUL=3D303, PLL2_PRE_DIV=3D3 + * OP output =3D 19.2 * 303 / 3 =3D 1939.2 MHz (MIPI bit rate) + * Link freq =3D 1939.2 / 2 (DDR) =3D 969.6 MHz + */ +#define IMX681_LINK_FREQ 969600000LL + +/* + * The pixel rate cannot be derived from the CSI-2 link: this part runs two + * PLLs, and the pixel array is clocked from the video-timing one while the + * link is clocked from the other. It cannot be derived from the video-tim= ing + * PLL either, because these registers are at the CCS addresses without + * having the CCS meaning. Read back from the part at its operating point + * they are PRE_PLL_CLK_DIV 2 and PLL_MULTIPLIER 225, so a 2160 MHz VCO, t= hen + * VT_SYS_CLK_DIV 2 and VT_PIX_CLK_DIV 6 - by the CCS formula 180 MHz, whi= ch + * would be 7.5 fps. The sensor runs at 30. + * + * So it is measured: LINE_LENGTH_PCK x FRAME_LENGTH_LINES x the frame rate + * the part achieves, which is 29.95 fps over 300 frames and 720 MHz. + */ +#define IMX681_PIXEL_RATE 720000000LL + +/* Power-on delay after reset deassert */ +#define IMX681_RESET_DELAY_US 1000 +#define IMX681_RESET_DELAY_RANGE_US 1000 + +/* Post-standby-cancel stabilisation delays */ +#define IMX681_INIT_DELAY_US 10000 + +#define IMAGE_PAD 0 + +static const s64 imx681_link_frequencies[] =3D { + IMX681_LINK_FREQ, +}; + +/* + * Sensor init register sequence, captured from Windows I2C traces. + * This configures the sensor for 3844x2640 RAW10 output at ~30fps + * with 2-lane MIPI CSI-2, 19.2MHz input clock. + */ +static const struct cci_reg_sequence imx681_init_regs[] =3D { + { IMX681_REG_EXTCLK_FREQ_MHZ, IMX681_XCLK_FREQ_ENC }, + /* Vendor specific configuration */ + { CCI_REG16(0x002C), 0x0505 }, + /* 0x02 is D-PHY, which is the only mode this part offers. */ + { IMX681_REG_CSI_SIGNALING_MODE, 0x02 }, + /* Vendor access unlock sequence */ + { CCI_REG8(0x30EB), 0x05 }, + { CCI_REG8(0x30EB), 0x0C }, + /* Vendor specific */ + { CCI_REG16(0x300A), 0xFFFF }, + { CCI_REG16(0x3532), 0xFFFF }, + /* + * The analogue crop, its digital counterpart and the output size, at + * the CCS addresses. The traces wrote these as overlapping partial + * writes - 0x0345 and 0x0349 carry only the low byte of a 16-bit + * register, so the window was right only while the high bytes held + * their reset values. Written whole here, and the values read back + * from the part at the operating point agree: the window is + * 3943 - 100 + 1 by 2895 - 256 + 1, which is the output size. + */ + { IMX681_REG_X_ADDR_START, IMX681_CROP_X_START }, + { IMX681_REG_Y_ADDR_START, IMX681_CROP_Y_START }, + { IMX681_REG_X_ADDR_END, IMX681_CROP_X_START + IMX681_WIDTH - 1 }, + { IMX681_REG_Y_ADDR_END, IMX681_CROP_Y_START + IMX681_HEIGHT - 1 }, + { IMX681_REG_DIGITAL_CROP_WIDTH, IMX681_WIDTH }, + { IMX681_REG_DIGITAL_CROP_HEIGHT, IMX681_HEIGHT }, + { IMX681_REG_X_OUTPUT_SIZE, IMX681_WIDTH }, + { IMX681_REG_Y_OUTPUT_SIZE, IMX681_HEIGHT }, + /* + * Both of these were partial writes in the traces too: 0x0307 is the + * low byte of PLL_MULTIPLIER and 0x030d the low byte of + * OP_PRE_PLL_CLK_DIV, and each was right only while its high byte + * held the reset value. Written whole, and the part reads back + * 225, 3 and 303 at the operating point either way. + */ + { IMX681_REG_PLL_MULTIPLIER, IMX681_PLL_MULTIPLIER }, + { IMX681_REG_OP_PRE_PLL_CLK_DIV, IMX681_OP_PRE_PLL_CLK_DIV }, + { IMX681_REG_OP_PLL_MULTIPLIER, IMX681_OP_PLL_MULTIPLIER }, + /* Vendor specific registers */ + { CCI_REG8(0x7E9B), 0x02 }, + { CCI_REG8(0x0368), 0x00 }, + { CCI_REG8(0xD383), 0x01 }, +}; + +/* + * The CCS numbering, which this part follows: 1 solid colour, 2 colour ba= rs, + * 3 bars fading to grey. Mode 4, PN9, is not offered because it produces a + * black frame on this sensor where 2 and 3 produce their patterns, and + * TEST_PATTERN_CAPABILITY reads zero here so it cannot be asked. + */ +static const char * const imx681_test_pattern_menu[] =3D { + "Disabled", + "Solid Colour", + "Eight Vertical Colour Bars", + "Colour Bars With Fade to Grey", +}; + +/* + * The pixel array reads out RGGB. Mirroring moves the Bayer phase by one + * column and flipping by one row, so the code the sensor delivers follows + * the two controls: index =3D vflip << 1 | hflip. + */ +static const u32 imx681_mbus_codes[] =3D { + MEDIA_BUS_FMT_SRGGB10_1X10, + MEDIA_BUS_FMT_SGRBG10_1X10, + MEDIA_BUS_FMT_SGBRG10_1X10, + MEDIA_BUS_FMT_SBGGR10_1X10, +}; + +/* Regulator supplies */ +static const char * const imx681_supply_names[] =3D { + "avdd", /* Analog 2.8V */ + "dvdd", /* Digital 1.05V */ + "dovdd", /* I/O 1.8V */ +}; + +struct imx681 { + struct device *dev; + struct regmap *cci; + + struct v4l2_subdev sd; + struct media_pad pad; + + struct clk *xclk; + struct gpio_desc *reset_gpio; + struct regulator_bulk_data supplies[ARRAY_SIZE(imx681_supply_names)]; + + /* V4L2 Controls */ + struct v4l2_ctrl_handler ctrl_handler; + struct v4l2_ctrl *exposure; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *hflip; + struct v4l2_ctrl *vflip; + + unsigned long link_freq_bitmap; +}; + +static inline struct imx681 *to_imx681(struct v4l2_subdev *sd) +{ + return container_of_const(sd, struct imx681, sd); +} + +static int imx681_set_ctrl(struct v4l2_ctrl *ctrl) +{ + struct imx681 *imx681 =3D container_of(ctrl->handler, struct imx681, + ctrl_handler); + s64 exposure_max; + int pm_status; + int ret =3D 0; + + /* Update exposure max when VBLANK changes (even when not streaming) */ + if (ctrl->id =3D=3D V4L2_CID_VBLANK) { + exposure_max =3D IMX681_HEIGHT + ctrl->val - IMX681_EXPOSURE_MARGIN; + ret =3D __v4l2_ctrl_modify_range(imx681->exposure, + IMX681_EXPOSURE_MIN, exposure_max, + 1, IMX681_EXPOSURE_DEFAULT); + if (ret) + return ret; + } + + /* + * 1 with a reference taken, 0 if the device is not active, or -EINVAL + * if runtime PM is unavailable. Only the 0 means there is nothing to + * do: without runtime PM the sensor is powered from probe and never + * suspended, so the write still has to go out - but no reference was + * taken then, and none may be dropped. + */ + pm_status =3D pm_runtime_get_if_active(imx681->dev); + if (!pm_status) + return 0; + + switch (ctrl->id) { + case V4L2_CID_VBLANK: + ret =3D cci_write(imx681->cci, IMX681_REG_FRAME_LENGTH, + IMX681_HEIGHT + ctrl->val, NULL); + break; + + case V4L2_CID_EXPOSURE: + ret =3D cci_write(imx681->cci, IMX681_REG_EXPOSURE, ctrl->val, + NULL); + break; + + case V4L2_CID_ANALOGUE_GAIN: + /* Gain formula: gain =3D 1024/(1024-code); code 960 is 16x. */ + ret =3D cci_write(imx681->cci, IMX681_REG_ANALOG_GAIN, ctrl->val, + NULL); + break; + + case V4L2_CID_DIGITAL_GAIN: + ret =3D cci_write(imx681->cci, IMX681_REG_DIGITAL_GAIN, ctrl->val, + NULL); + break; + + case V4L2_CID_HFLIP: + case V4L2_CID_VFLIP: + /* + * Read from imx681_start_streaming(). These are grabbed while + * streaming, so this runs only when the part is idle and the + * value is applied at the next stream start. + */ + break; + + case V4L2_CID_TEST_PATTERN: + ret =3D cci_write(imx681->cci, IMX681_REG_TEST_PATTERN, + ctrl->val, NULL); + break; + + case V4L2_CID_TEST_PATTERN_RED: + ret =3D cci_write(imx681->cci, IMX681_REG_TESTP_RED, ctrl->val, + NULL); + break; + + case V4L2_CID_TEST_PATTERN_GREENR: + ret =3D cci_write(imx681->cci, IMX681_REG_TESTP_GREENR, + ctrl->val, NULL); + break; + + case V4L2_CID_TEST_PATTERN_BLUE: + ret =3D cci_write(imx681->cci, IMX681_REG_TESTP_BLUE, ctrl->val, + NULL); + break; + + case V4L2_CID_TEST_PATTERN_GREENB: + ret =3D cci_write(imx681->cci, IMX681_REG_TESTP_GREENB, + ctrl->val, NULL); + break; + + default: + break; + } + + if (pm_status > 0) + pm_runtime_put(imx681->dev); + + return ret; +} + +static const struct v4l2_ctrl_ops imx681_ctrl_ops =3D { + .s_ctrl =3D imx681_set_ctrl, +}; + +static u32 imx681_get_format_code(struct imx681 *imx681) +{ + return imx681_mbus_codes[(imx681->vflip->val ? 2 : 0) | + (imx681->hflip->val ? 1 : 0)]; +} + +static int imx681_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_mbus_code_enum *code) +{ + struct imx681 *imx681 =3D to_imx681(sd); + + if (code->index > 0) + return -EINVAL; + + code->code =3D imx681_get_format_code(imx681); + + return 0; +} + +static bool imx681_is_valid_mbus_code(struct imx681 *imx681, u32 code) +{ + return code =3D=3D imx681_get_format_code(imx681); +} + +static int imx681_enum_frame_size(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_frame_size_enum *fse) +{ + if (fse->index > 0) + return -EINVAL; + + if (!imx681_is_valid_mbus_code(to_imx681(sd), fse->code)) + return -EINVAL; + + fse->min_width =3D IMX681_WIDTH; + fse->max_width =3D IMX681_WIDTH; + fse->min_height =3D IMX681_HEIGHT; + fse->max_height =3D IMX681_HEIGHT; + + return 0; +} + +static int imx681_init_state(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state) +{ + struct v4l2_mbus_framefmt *format; + + format =3D v4l2_subdev_state_get_format(state, IMAGE_PAD); + format->width =3D IMX681_WIDTH; + format->height =3D IMX681_HEIGHT; + format->code =3D imx681_get_format_code(to_imx681(sd)); + format->field =3D V4L2_FIELD_NONE; + format->colorspace =3D V4L2_COLORSPACE_RAW; + format->ycbcr_enc =3D V4L2_YCBCR_ENC_601; + format->quantization =3D V4L2_QUANTIZATION_FULL_RANGE; + format->xfer_func =3D V4L2_XFER_FUNC_NONE; + + return 0; +} + +/* + * There is nothing to configure on the part here - the size is fixed and = the + * code follows the flips - but the stored format still has to be told whi= ch + * Bayer order the flips have produced, or the pipeline validates the state + * against a code the sensor is no longer delivering. + */ +static int imx681_set_pad_format(struct v4l2_subdev *sd, + const struct v4l2_subdev_client_info *ci, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *fmt) +{ + struct imx681 *imx681 =3D to_imx681(sd); + struct v4l2_mbus_framefmt *format; + + fmt->format.width =3D IMX681_WIDTH; + fmt->format.height =3D IMX681_HEIGHT; + fmt->format.code =3D imx681_get_format_code(imx681); + fmt->format.field =3D V4L2_FIELD_NONE; + fmt->format.colorspace =3D V4L2_COLORSPACE_RAW; + fmt->format.ycbcr_enc =3D V4L2_YCBCR_ENC_601; + fmt->format.quantization =3D V4L2_QUANTIZATION_FULL_RANGE; + fmt->format.xfer_func =3D V4L2_XFER_FUNC_NONE; + + format =3D v4l2_subdev_state_get_format(state, fmt->pad); + *format =3D fmt->format; + + return 0; +} + +static int imx681_get_selection(struct v4l2_subdev *sd, + const struct v4l2_subdev_client_info *ci, + struct v4l2_subdev_state *state, + struct v4l2_subdev_selection *sel) +{ + switch (sel->target) { + case V4L2_SEL_TGT_CROP: + case V4L2_SEL_TGT_CROP_DEFAULT: + sel->r.left =3D IMX681_CROP_X_START; + sel->r.top =3D IMX681_CROP_Y_START; + sel->r.width =3D IMX681_WIDTH; + sel->r.height =3D IMX681_HEIGHT; + return 0; + case V4L2_SEL_TGT_CROP_BOUNDS: + sel->r.left =3D IMX681_PIXEL_ARRAY_LEFT; + sel->r.top =3D IMX681_PIXEL_ARRAY_TOP; + sel->r.width =3D IMX681_PIXEL_ARRAY_WIDTH; + sel->r.height =3D IMX681_PIXEL_ARRAY_HEIGHT; + return 0; + case V4L2_SEL_TGT_NATIVE_SIZE: + sel->r.left =3D 0; + sel->r.top =3D 0; + sel->r.width =3D IMX681_NATIVE_WIDTH; + sel->r.height =3D IMX681_NATIVE_HEIGHT; + return 0; + default: + return -EINVAL; + } +} + +static int imx681_start_streaming(struct imx681 *imx681) +{ + int ret; + + /* + * The orientation goes in before the register list, because the crop + * window in it is interpreted in the orientation then in force. The + * downstream driver had this write inside the list, ahead of the crop + * registers, for the same reason. + */ + ret =3D cci_write(imx681->cci, IMX681_REG_ORIENTATION, + imx681->hflip->val | imx681->vflip->val << 1, NULL); + if (ret) + return ret; + + ret =3D cci_multi_reg_write(imx681->cci, imx681_init_regs, + ARRAY_SIZE(imx681_init_regs), NULL); + if (ret) { + dev_err(imx681->dev, "failed to write init regs: %d\n", ret); + return ret; + } + + /* Wait for sensor to stabilise after configuration */ + usleep_range(IMX681_INIT_DELAY_US, IMX681_INIT_DELAY_US + 1000); + + /* + * The blanking is the controls' to set, not the register list's: + * HBLANK gives the line length, and VBLANK the frame length through + * imx681_set_ctrl() below, along with the exposure and the gains. + */ + ret =3D cci_write(imx681->cci, IMX681_REG_LINE_LENGTH, + IMX681_WIDTH + imx681->hblank->val, NULL); + if (ret) + return ret; + + ret =3D __v4l2_ctrl_handler_setup(imx681->sd.ctrl_handler); + if (ret) { + dev_err(imx681->dev, "failed to apply controls: %d\n", ret); + return ret; + } + + /* Start streaming */ + ret =3D cci_write(imx681->cci, IMX681_REG_MODE_SELECT, + IMX681_MODE_STREAMING, NULL); + if (ret) { + dev_err(imx681->dev, "failed to start streaming: %d\n", ret); + return ret; + } + + return 0; +} + +static int imx681_stop_streaming(struct imx681 *imx681) +{ + int ret; + + ret =3D cci_write(imx681->cci, IMX681_REG_MODE_SELECT, + IMX681_MODE_STANDBY, NULL); + if (ret) + dev_err(imx681->dev, "failed to stop streaming: %d\n", ret); + + return ret; +} + +static int imx681_enable_streams(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + u32 pad, u64 streams_mask) +{ + struct imx681 *imx681 =3D to_imx681(sd); + int ret; + + ret =3D pm_runtime_resume_and_get(imx681->dev); + if (ret) + return ret; + + ret =3D imx681_start_streaming(imx681); + if (ret) { + pm_runtime_put_autosuspend(imx681->dev); + return ret; + } + + /* + * The flips are read once, in imx681_start_streaming(), and they + * choose the media bus code. Accepting a change now would report + * success, leave the part as it is and leave the stored format + * naming a code the sensor is no longer delivering. + */ + __v4l2_ctrl_grab(imx681->hflip, true); + __v4l2_ctrl_grab(imx681->vflip, true); + + return 0; +} + +static int imx681_disable_streams(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + u32 pad, u64 streams_mask) +{ + struct imx681 *imx681 =3D to_imx681(sd); + int ret; + + __v4l2_ctrl_grab(imx681->hflip, false); + __v4l2_ctrl_grab(imx681->vflip, false); + + ret =3D imx681_stop_streaming(imx681); + + /* The reference goes back whether or not the part acknowledged. */ + pm_runtime_put_autosuspend(imx681->dev); + + return ret; +} + +static const struct v4l2_subdev_video_ops imx681_video_ops =3D { + .s_stream =3D v4l2_subdev_s_stream_helper, +}; + +static const struct v4l2_subdev_pad_ops imx681_pad_ops =3D { + .enum_mbus_code =3D imx681_enum_mbus_code, + .get_fmt =3D v4l2_subdev_get_fmt, + .set_fmt =3D imx681_set_pad_format, + .get_selection =3D imx681_get_selection, + .enum_frame_size =3D imx681_enum_frame_size, + .enable_streams =3D imx681_enable_streams, + .disable_streams =3D imx681_disable_streams, +}; + +static const struct v4l2_subdev_ops imx681_subdev_ops =3D { + .video =3D &imx681_video_ops, + .pad =3D &imx681_pad_ops, +}; + +static const struct v4l2_subdev_internal_ops imx681_internal_ops =3D { + .init_state =3D imx681_init_state, +}; + +/* Power management */ +static int imx681_power_on(struct device *dev) +{ + struct v4l2_subdev *sd =3D dev_get_drvdata(dev); + struct imx681 *imx681 =3D to_imx681(sd); + int ret; + + ret =3D regulator_bulk_enable(ARRAY_SIZE(imx681_supply_names), imx681->su= pplies); + if (ret) { + dev_err(imx681->dev, "failed to enable regulators: %d\n", ret); + return ret; + } + + ret =3D clk_prepare_enable(imx681->xclk); + if (ret) { + dev_err(imx681->dev, "failed to enable clock: %d\n", ret); + goto err_reg_disable; + } + + /* Deassert reset (active low) */ + gpiod_set_value_cansleep(imx681->reset_gpio, 0); + + usleep_range(IMX681_RESET_DELAY_US, + IMX681_RESET_DELAY_US + IMX681_RESET_DELAY_RANGE_US); + + return 0; + +err_reg_disable: + regulator_bulk_disable(ARRAY_SIZE(imx681_supply_names), imx681->supplies); + + return ret; +} + +static int imx681_power_off(struct device *dev) +{ + struct v4l2_subdev *sd =3D dev_get_drvdata(dev); + struct imx681 *imx681 =3D to_imx681(sd); + + /* Assert reset */ + gpiod_set_value_cansleep(imx681->reset_gpio, 1); + clk_disable_unprepare(imx681->xclk); + regulator_bulk_disable(ARRAY_SIZE(imx681_supply_names), imx681->supplies); + + return 0; +} + +static int imx681_identify_module(struct imx681 *imx681) +{ + u64 val; + int ret; + + ret =3D cci_read(imx681->cci, IMX681_REG_CHIP_ID, &val, NULL); + if (ret) + return dev_err_probe(imx681->dev, ret, + "failed to read chip ID register 0x0016\n"); + + if (val !=3D IMX681_CHIP_ID) { + return dev_err_probe(imx681->dev, -EIO, + "chip ID mismatch: 0x%04llx !=3D 0x%04x\n", + val, IMX681_CHIP_ID); + } + + return 0; +} + +static int imx681_init_controls(struct imx681 *imx681) +{ + struct v4l2_ctrl_handler *ctrl_hdlr =3D &imx681->ctrl_handler; + struct v4l2_fwnode_device_properties props; + struct v4l2_ctrl *link_freq; + s64 hblank, vblank; + int ret; + + ret =3D v4l2_fwnode_device_parse(imx681->dev, &props); + if (ret) + return ret; + + ret =3D v4l2_ctrl_handler_init(ctrl_hdlr, 16); + if (ret) + return ret; + + /* Pixel rate (read-only) */ + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_PIXEL_RATE, IMX681_PIXEL_RATE, + IMX681_PIXEL_RATE, 1, IMX681_PIXEL_RATE); + + /* Link frequency (read-only) */ + link_freq =3D v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_LINK_FREQ, + __fls(imx681->link_freq_bitmap), + __ffs(imx681->link_freq_bitmap), + imx681_link_frequencies); + if (link_freq) + link_freq->flags |=3D V4L2_CTRL_FLAG_READ_ONLY; + + /* Horizontal blanking (read-only, fixed) */ + hblank =3D IMX681_LINE_LENGTH_PCK - IMX681_WIDTH; + imx681->hblank =3D v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_HBLANK, hblank, hblank, + 1, hblank); + if (imx681->hblank) + imx681->hblank->flags |=3D V4L2_CTRL_FLAG_READ_ONLY; + + /* Vertical blanking (writable to allow longer exposures) */ + vblank =3D IMX681_FRAME_LENGTH_LINES - IMX681_HEIGHT; + imx681->vblank =3D v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_VBLANK, vblank, + IMX681_FRAME_LENGTH_MAX - IMX681_HEIGHT, + 1, vblank); + + /* Exposure */ + imx681->exposure =3D v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_EXPOSURE, + IMX681_EXPOSURE_MIN, + IMX681_FRAME_LENGTH_LINES - + IMX681_EXPOSURE_MARGIN, 1, + IMX681_EXPOSURE_DEFAULT); + + /* + * Both flips move the Bayer phase, so the media bus code changes with + * them and the layout flag has to say so. + */ + imx681->hflip =3D v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_HFLIP, 0, 1, 1, 0); + imx681->vflip =3D v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_VFLIP, 0, 1, 1, 0); + if (imx681->hflip) + imx681->hflip->flags |=3D V4L2_CTRL_FLAG_MODIFY_LAYOUT; + if (imx681->vflip) + imx681->vflip->flags |=3D V4L2_CTRL_FLAG_MODIFY_LAYOUT; + + /* Analog gain */ + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, + IMX681_ANA_GAIN_MIN, IMX681_ANA_GAIN_MAX, 1, + IMX681_ANA_GAIN_DEFAULT); + + /* Digital gain */ + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, V4L2_CID_DIGITAL_GAIN, + IMX681_DIG_GAIN_MIN, IMX681_DIG_GAIN_MAX, 1, + IMX681_DIG_GAIN_DEFAULT); + + /* Test pattern */ + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_TEST_PATTERN, + ARRAY_SIZE(imx681_test_pattern_menu) - 1, + 0, 0, imx681_test_pattern_menu); + + /* + * The solid-colour mode shows whatever is in these, and they reset to + * zero - so without them the mode this driver advertises can only + * ever produce a black frame. Default to white, as imx219 does. + */ + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_TEST_PATTERN_RED, IMX681_TESTP_COLOUR_MIN, + IMX681_TESTP_COLOUR_MAX, IMX681_TESTP_COLOUR_STEP, + IMX681_TESTP_COLOUR_MAX); + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_TEST_PATTERN_GREENR, IMX681_TESTP_COLOUR_MIN, + IMX681_TESTP_COLOUR_MAX, IMX681_TESTP_COLOUR_STEP, + IMX681_TESTP_COLOUR_MAX); + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_TEST_PATTERN_BLUE, IMX681_TESTP_COLOUR_MIN, + IMX681_TESTP_COLOUR_MAX, IMX681_TESTP_COLOUR_STEP, + IMX681_TESTP_COLOUR_MAX); + v4l2_ctrl_new_std(ctrl_hdlr, &imx681_ctrl_ops, + V4L2_CID_TEST_PATTERN_GREENB, IMX681_TESTP_COLOUR_MIN, + IMX681_TESTP_COLOUR_MAX, IMX681_TESTP_COLOUR_STEP, + IMX681_TESTP_COLOUR_MAX); + + if (ctrl_hdlr->error) { + ret =3D dev_err_probe(imx681->dev, ctrl_hdlr->error, + "control init failed\n"); + goto error; + } + + ret =3D v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx681_ctrl_ops, + &props); + if (ret) + goto error; + + imx681->sd.ctrl_handler =3D ctrl_hdlr; + + return 0; + +error: + v4l2_ctrl_handler_free(ctrl_hdlr); + + return ret; +} + +static int imx681_parse_endpoint(struct imx681 *imx681) +{ + struct fwnode_handle *fwnode =3D dev_fwnode(imx681->dev); + struct v4l2_fwnode_endpoint bus_cfg =3D { + .bus_type =3D V4L2_MBUS_CSI2_DPHY, + }; + struct fwnode_handle *ep; + int ret; + + ep =3D fwnode_graph_get_endpoint_by_id(fwnode, 0, 0, 0); + + ret =3D v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); + fwnode_handle_put(ep); + if (ret) + return dev_err_probe(imx681->dev, ret, + "failed to parse endpoint\n"); + + if (bus_cfg.bus.mipi_csi2.num_data_lanes !=3D IMX681_NUM_LANES) { + ret =3D dev_err_probe(imx681->dev, -EINVAL, + "expected %d data lanes, got %d\n", + IMX681_NUM_LANES, + bus_cfg.bus.mipi_csi2.num_data_lanes); + goto done; + } + + ret =3D v4l2_link_freq_to_bitmap(imx681->dev, + bus_cfg.link_frequencies, + bus_cfg.nr_of_link_frequencies, + imx681_link_frequencies, + ARRAY_SIZE(imx681_link_frequencies), + &imx681->link_freq_bitmap); + if (ret) + ret =3D dev_err_probe(imx681->dev, ret, + "link frequency mismatch\n"); + +done: + v4l2_fwnode_endpoint_free(&bus_cfg); + + return ret; +} + +static int imx681_probe(struct i2c_client *client) +{ + struct imx681 *imx681; + unsigned long xclk_freq; + int ret; + + imx681 =3D devm_kzalloc(&client->dev, sizeof(*imx681), GFP_KERNEL); + if (!imx681) + return -ENOMEM; + + imx681->dev =3D &client->dev; + + /* Initialise V4L2 subdev */ + v4l2_i2c_subdev_init(&imx681->sd, client, &imx681_subdev_ops); + + /* Initialise CCI regmap for 16-bit register addresses */ + imx681->cci =3D 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"); + + imx681->xclk =3D devm_v4l2_sensor_clk_get(imx681->dev, NULL); + if (IS_ERR(imx681->xclk)) + return dev_err_probe(imx681->dev, PTR_ERR(imx681->xclk), + "failed to get clock\n"); + + /* + * Both PLLs in imx681_init_regs[] and the value written to + * EXTCLK_FREQUENCY_MHZ assume this rate. + */ + xclk_freq =3D clk_get_rate(imx681->xclk); + if (xclk_freq !=3D IMX681_XCLK_FREQ) + return dev_err_probe(imx681->dev, -EINVAL, + "external clock %lu Hz not supported, expected %u Hz\n", + xclk_freq, IMX681_XCLK_FREQ); + + /* Get regulators */ + for (unsigned int i =3D 0; i < ARRAY_SIZE(imx681_supply_names); i++) + imx681->supplies[i].supply =3D imx681_supply_names[i]; + + ret =3D devm_regulator_bulk_get(imx681->dev, ARRAY_SIZE(imx681_supply_nam= es), + imx681->supplies); + if (ret) + return dev_err_probe(imx681->dev, ret, + "failed to get regulators\n"); + + /* Get reset GPIO (optional) */ + imx681->reset_gpio =3D devm_gpiod_get_optional(imx681->dev, "reset", + GPIOD_OUT_HIGH); + if (IS_ERR(imx681->reset_gpio)) + return dev_err_probe(imx681->dev, + PTR_ERR(imx681->reset_gpio), + "failed to get reset GPIO\n"); + + /* Parse CSI-2 endpoint */ + ret =3D imx681_parse_endpoint(imx681); + if (ret) + return dev_err_probe(imx681->dev, ret, + "endpoint parse failed\n"); + + /* Power on and verify chip ID */ + ret =3D imx681_power_on(imx681->dev); + if (ret) + return dev_err_probe(imx681->dev, ret, "power on failed\n"); + + ret =3D imx681_identify_module(imx681); + if (ret) + goto error_power_off; + + /* Enable runtime PM, held so the sensor stays on until registered */ + pm_runtime_set_active(imx681->dev); + pm_runtime_get_noresume(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 =3D imx681_init_controls(imx681); + if (ret) + goto error_pm; + + /* Setup subdev */ + imx681->sd.flags |=3D V4L2_SUBDEV_FL_HAS_DEVNODE; + imx681->sd.entity.function =3D MEDIA_ENT_F_CAM_SENSOR; + imx681->sd.internal_ops =3D &imx681_internal_ops; + + /* Init media entity */ + imx681->pad.flags =3D MEDIA_PAD_FL_SOURCE; + ret =3D media_entity_pads_init(&imx681->sd.entity, 1, &imx681->pad); + if (ret) { + ret =3D dev_err_probe(imx681->dev, ret, + "media entity init failed\n"); + goto error_handler_free; + } + + imx681->sd.state_lock =3D imx681->ctrl_handler.lock; + ret =3D v4l2_subdev_init_finalize(&imx681->sd); + if (ret < 0) { + ret =3D dev_err_probe(imx681->dev, ret, + "subdev init finalize failed\n"); + goto error_media_entity; + } + + ret =3D v4l2_async_register_subdev_sensor(&imx681->sd); + if (ret < 0) { + ret =3D dev_err_probe(imx681->dev, ret, + "async register subdev failed\n"); + goto error_subdev_cleanup; + } + + pm_runtime_put_autosuspend(imx681->dev); + + return 0; + +error_subdev_cleanup: + v4l2_subdev_cleanup(&imx681->sd); +error_media_entity: + media_entity_cleanup(&imx681->sd.entity); +error_handler_free: + v4l2_ctrl_handler_free(imx681->sd.ctrl_handler); +error_pm: + pm_runtime_disable(imx681->dev); + pm_runtime_set_suspended(imx681->dev); + pm_runtime_put_noidle(imx681->dev); + pm_runtime_dont_use_autosuspend(imx681->dev); +error_power_off: + imx681_power_off(imx681->dev); + + return ret; +} + +static void imx681_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd =3D i2c_get_clientdata(client); + struct imx681 *imx681 =3D to_imx681(sd); + + v4l2_async_unregister_subdev(sd); + v4l2_subdev_cleanup(&imx681->sd); + media_entity_cleanup(&sd->entity); + v4l2_ctrl_handler_free(imx681->sd.ctrl_handler); + + pm_runtime_disable(imx681->dev); + if (!pm_runtime_status_suspended(imx681->dev)) { + imx681_power_off(imx681->dev); + pm_runtime_set_suspended(imx681->dev); + } + pm_runtime_dont_use_autosuspend(imx681->dev); +} + +static DEFINE_RUNTIME_DEV_PM_OPS(imx681_pm_ops, imx681_power_off, + imx681_power_on, NULL); + +#ifdef CONFIG_ACPI +static const struct acpi_device_id imx681_acpi_ids[] =3D { + { "SONY0681" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(acpi, imx681_acpi_ids); +#endif + +static const struct of_device_id imx681_dt_ids[] =3D { + { .compatible =3D "sony,imx681" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, imx681_dt_ids); + +static struct i2c_driver imx681_i2c_driver =3D { + .driver =3D { + .name =3D "imx681", + .pm =3D pm_ptr(&imx681_pm_ops), + .acpi_match_table =3D ACPI_PTR(imx681_acpi_ids), + .of_match_table =3D imx681_dt_ids, + }, + .probe =3D imx681_probe, + .remove =3D imx681_remove, +}; +module_i2c_driver(imx681_i2c_driver); + +MODULE_DESCRIPTION("Sony IMX681 CMOS Image Sensor Driver"); +MODULE_AUTHOR("Andre Gilerson "); +MODULE_LICENSE("GPL"); --=20 2.54.0 From nobody Tue Nov 24 04:33:13 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.70.43.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793969151; b=4oo5f+Ha53RhueNwb71El+zhG7Qg/eg5QSIO8v+rOQEwCq5K96VuZRWBKf5/0fuoXfK8 Eo4+U4NH1VqW1XE8q2ExoRgpxuVkP+XOvPhO9jSCIN1Feg6GIE8gI1fPkv9smAT2T0yOt z9cz0OFnSj4thYBV3pKHaZUsfFa59Iuj+GVT7EEoDKx+OIDCCRlFRxLNdHmppqHxCgqY9 a9eukNgut+vT7LeNjon9OjZ7Z8nUoIX6T2gWEdfIFPWjU6oFGwGpVaWG7CHAC/b1cgTzA GSGZ96Hhbi5Mf0uRuLHsFUJDImz/QOs3MVLb6zs/lGt/O97f+P5K/18XrnnZdZga1eQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793969151; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=SvCOCf3YteA9iimmP/e0lnCKLJTugesY45Fw5285iVw=; b=blhNbJF1EfMe2jBWQqiW2H3io2RI0TnLd9Ice5XeBObFIReGlAR+n2nt9QalHWuAIN0B RUC9vJXFTtltXm/+9lH0vUtb/DuOQ1T2r0HKso5Opd6S24ljWBfYiZ+Tju8VA/eAxGO6O KF0eUdDG/QU9c6jn25x00OIoAye3Wvb/e0ZvLosNnNBw4suvxHC4DWFLFigUbRXmMAx+P +/g0tsT525L8+v3GMwdxe5xXUEDqicN6V78uLnEXnKvEGF1gJaF6PduBAB6gYhuwjx2YA TT4gXE8wQZgHfcEuk0PAZjnDiyZrjGb7IIZcGmC/vHj7b/9GBVC9yAFwuFHBs+XCt6A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass header.d=pm.me header.i=@pm.me header.a=rsa-sha256 header.s=protonmail3 header.b=QSZCZtXd; arc=none smtp.remote-ip=185.70.43.16 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF947606A326 for ; Fri, 6 Nov 2026 12:45:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BF947606A326 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=pm.me header.i=@pm.me header.a=rsa-sha256 header.s=protonmail3 header.b=QSZCZtXd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1793969149; x=1794228349; bh=SvCOCf3YteA9iimmP/e0lnCKLJTugesY45Fw5285iVw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=QSZCZtXdqU+7S8oGK81m5dtWVyyK2pLUfp8XDB2j7lZTm7xXDVZDDRdB+JSUCJHHw RfuauBmStZCLCwBZXsu1md1ZMO73h0xEipezjF60ti5kmGV+67RhZlp29tWcMN8I4o xO351TK50c+kADkedAxJEUqu+N5oz86eOaTfbXw2ZtNzKzYG9JRo7kKdb+9u7WheIv iLxa39b/4C9OkB0AU21smxHGblE65tk6Cpvql1iqI4/WF6O9EohX/CfMPJrUDaI8gA 3FLEVwB7tMq/ylovZUl2VXqjTEfZCTGJ6HviX9b+uxvXQQbasqGsRTLipbhuI9gDir 9NuPKuULVnuQw== Date: Fri, 06 Nov 2026 12:45:44 +0000 To: Sakari Ailus , Mauro Carvalho Chehab , Andre Gilerson , Dan Scally From: Sergey Lebedev Cc: Hans de Goede , Rob Herring , Krzysztof Kozlowski , Conor Dooley , German Pablo Lindo , George Neacsu , sashiko-bot@kernel.org, linux-media@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 3/3] media: ipu-bridge: Add Sony IMX681 Message-ID: <20261106124505.71429-4-lsa.uz@pm.me> In-Reply-To: <20261106124505.71429-1-lsa.uz@pm.me> References: <20261106124505.71429-1-lsa.uz@pm.me> Feedback-ID: 113843758:user:proton X-Pm-Message-ID: 83f751e65593d37a7ae0d24e615f62f4cf494344 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Sergey Lebedev --- drivers/media/pci/intel/ipu-bridge.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/media/pci/intel/ipu-bridge.c b/drivers/media/pci/intel= /ipu-bridge.c index 3739c4a2a8c..2759c776cfc 100644 --- a/drivers/media/pci/intel/ipu-bridge.c +++ b/drivers/media/pci/intel/ipu-bridge.c @@ -124,6 +124,8 @@ static const struct ipu_sensor_config ipu_supported_sen= sors[] =3D { 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 */ --=20 2.54.0