Skip to contentSkip to conversation

media: Add ISX031 sensor support

PATCH Superseded [email protected] 3 participants 6 messages Dec 2 – Dec 3 Media (V4L/DVB)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover media: Add ISX031 sensor support

From: "Sodhi, Vunny" <[email protected]>

This series adds INTC113C support to ipu-bridge and a V4L2 driver for the Sony ISX031 sensor. The driver is ACPI-only and does not add a Device Tree binding; it relies on ipu-bridge for endpoint properties.

Changes since v3:
- Add the driver to MAINTAINERS.
- Get and validate the 24-MHz sensor input clock.
- Reset and power up the sensor explicitly during probe, with cleanup on failure.
- Disable external frame-sync property handling.

Kris Pan (1):
media: ipu-bridge: add INTC113C for the ISX031 sensor

Yew, Chang Ching (1):
media: i2c: add isx031 sensor driver

 MAINTAINERS                          |    7 +
 drivers/media/i2c/Kconfig            |   11 +
 drivers/media/i2c/Makefile           |    1 +
 drivers/media/i2c/isx031.c           | 1052 ++++++++++++++++++++++++++
 drivers/media/pci/intel/ipu-bridge.c |    2 +
 5 files changed, 1073 insertions(+)
 create mode 100644 drivers/media/i2c/isx031.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/2 media: ipu-bridge: add INTC113C for the ISX031 sensor

From: Kris Pan <[email protected]>

The ISX031 sensor driver binds through the ACPI HID INTC113C and needs the IPU bridge to create its software node and endpoint. Add the HID to ipu_supported_sensors[] with a 288 MHz link frequency, matching the driver's V4L2_CID_LINK_FREQ menu.

---
 drivers/media/pci/intel/ipu-bridge.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/pci/intel/ipu-bridge.c +2 -0
index 3739c4a2a8c4..8020bcf00dc1 100644
@@ -90,6 +90,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
 	/* Lontium lt6911uxe */
 	IPU_SENSOR_CONFIG("INTC10C5", 0),
+	/* D3 Embedded ISX031 */
+	IPU_SENSOR_CONFIG("INTC113C", 1, 288000000),
 	/* Omnivision OV01A10 / OV01A1B / OV01A1S */
 	IPU_SENSOR_CONFIG("OVTI01A0", 1, 400000000),
 	IPU_SENSOR_CONFIG("OVTI01AB", 1, 400000000),
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 2/2 media: i2c: add isx031 sensor driver

In reply to [email protected]

From: "Yew, Chang Ching" <[email protected]>

Add a V4L2 sub-device driver for the Sony ISX031 YUV422 image sensor. The sensor is controlled over I2C with the CCI helpers and outputs MIPI CSI-2.

Supported modes:
- 1920x1536 at 30 fps
- 1920x1080 at 30 fps
- 1280x720 at 30 fps

The frame rate is selected according to the configured CSI-2 lane count. The driver supports runtime PM, auto/manual exposure controls and an optional external frame-sync input (fsin GPIO).

The driver is ACPI-only: it binds through the INTC113C HID and relies on the IPU bridge for the endpoint (lane count) and device properties.

---
 MAINTAINERS                |    7 +
 drivers/media/i2c/Kconfig  |   11 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/isx031.c | 1052 ++++++++++++++++++++++++++++++++++++
 4 files changed, 1071 insertions(+)
 create mode 100644 drivers/media/i2c/isx031.c
MAINTAINERS +7 -0
index ccbcdc0799b9..ccb3a1e077e9 100644
@@ -26266,6 +26266,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/media/i2c/sony,imx678.yaml
 F:	drivers/media/i2c/imx678.c
 
+SONY ISX031 SENSOR DRIVER
+M:	Yew, Chang Ching <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/media/i2c/isx031.c
+
 SONY MEMORYSTICK SUBSYSTEM
 M:	Maxim Levitsky <[email protected]>
 M:	Alex Dubov <[email protected]>
@@ -29374,6 +29380,7 @@ F:	drivers/media/i2c/ar*
 F:	drivers/media/i2c/gc*
 F:	drivers/media/i2c/hi*
 F:	drivers/media/i2c/imx*
+F:	drivers/media/i2c/isx*
 F:	drivers/media/i2c/mt*
 F:	drivers/media/i2c/og*
 F:	drivers/media/i2c/ov*
drivers/media/i2c/Kconfig +11 -0
index 11c30252540e..cdca5c8fcf53 100644
@@ -300,6 +300,17 @@ config VIDEO_IMX415
 	  To compile this driver as a module, choose M here: the
 	  module will be called imx415.
 
+config VIDEO_ISX031
+	tristate "ISX031 sensor support"
+	depends on ACPI || COMPILE_TEST
+	select V4L2_CCI_I2C
+	help
+	  This is a Video4Linux2 sensor-level driver for the
+	  Sony ISX031 camera.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called isx031.
+
 config VIDEO_IMX471
 	tristate "Sony IMX471 sensor support"
 	select V4L2_CCI_I2C
drivers/media/i2c/Makefile +1 -0
index 59e31af457cd..53449ed01969 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_VIDEO_IMX576) += imx576.o
 obj-$(CONFIG_VIDEO_IMX678) += imx678.o
 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
+obj-$(CONFIG_VIDEO_ISX031) += isx031.o
 obj-$(CONFIG_VIDEO_IT6625) += it6625.o
 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
 obj-$(CONFIG_VIDEO_LM3560) += lm3560.o
drivers/media/i2c/isx031.c new file +1052 -0

1,052 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v4 2/2] media: i2c: add isx031 sensor driver

Hi

I did a brief read through as I see this sensor is offered by a few companies with a GMSL serializer, so that opens up use cases.

I may have failed in my searching, but I couldn't find V1 or V2 of this series on linux-media, nor any note about what changed between versions. Those would have been useful to see what previous reviewers had said.

Quoted 219 lines · On Wed, 2 Dec 2026 at 09:36, <[email protected]> wrote:

From: "Yew, Chang Ching" <[email protected]>

Add a V4L2 sub-device driver for the Sony ISX031 YUV422 image sensor. The sensor is controlled over I2C with the CCI helpers and outputs MIPI CSI-2.

Supported modes:
- 1920x1536 at 30 fps
- 1920x1080 at 30 fps
- 1280x720 at 30 fps

The frame rate is selected according to the configured CSI-2 lane count. The driver supports runtime PM, auto/manual exposure controls and an optional external frame-sync input (fsin GPIO).

The driver is ACPI-only: it binds through the INTC113C HID and relies on the IPU bridge for the endpoint (lane count) and device properties.

---
 MAINTAINERS                |    7 +
 drivers/media/i2c/Kconfig  |   11 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/isx031.c | 1052 ++++++++++++++++++++++++++++++++++++
 4 files changed, 1071 insertions(+)
 create mode 100644 drivers/media/i2c/isx031.c
MAINTAINERS +7 -0
index ccbcdc0799b9..ccb3a1e077e9 100644
@@ -26266,6 +26266,12 @@ S:     Maintained
 F:     Documentation/devicetree/bindings/media/i2c/sony,imx678.yaml
 F:     drivers/media/i2c/imx678.c
 
+SONY ISX031 SENSOR DRIVER
+M:     Yew, Chang Ching <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     drivers/media/i2c/isx031.c
+
 SONY MEMORYSTICK SUBSYSTEM
 M:     Maxim Levitsky <[email protected]>
 M:     Alex Dubov <[email protected]>
@@ -29374,6 +29380,7 @@ F:      drivers/media/i2c/ar*
 F:     drivers/media/i2c/gc*
 F:     drivers/media/i2c/hi*
 F:     drivers/media/i2c/imx*
+F:     drivers/media/i2c/isx*
 F:     drivers/media/i2c/mt*
 F:     drivers/media/i2c/og*
 F:     drivers/media/i2c/ov*
drivers/media/i2c/Kconfig +11 -0
index 11c30252540e..cdca5c8fcf53 100644
@@ -300,6 +300,17 @@ config VIDEO_IMX415
          To compile this driver as a module, choose M here: the
          module will be called imx415.
 
+config VIDEO_ISX031
+       tristate "ISX031 sensor support"
+       depends on ACPI || COMPILE_TEST
+       select V4L2_CCI_I2C
+       help
+         This is a Video4Linux2 sensor-level driver for the
+         Sony ISX031 camera.
+
+         To compile this driver as a module, choose M here: the
+         module will be called isx031.
+
 config VIDEO_IMX471
        tristate "Sony IMX471 sensor support"
        select V4L2_CCI_I2C
drivers/media/i2c/Makefile +1 -0
index 59e31af457cd..53449ed01969 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_VIDEO_IMX576) += imx576.o
 obj-$(CONFIG_VIDEO_IMX678) += imx678.o
 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
+obj-$(CONFIG_VIDEO_ISX031) += isx031.o
 obj-$(CONFIG_VIDEO_IT6625) += it6625.o
 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
 obj-$(CONFIG_VIDEO_LM3560) += lm3560.o
drivers/media/i2c/isx031.c new file +118 -0
new file mode 100644
index 000000000000..22bfa016265e
@@ -0,0 +1,1052 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2022-2026 Intel Corporation.
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/units.h>
+
+#include <media/mipi-csi2.h>
+#include <media/v4l2-cci.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+
+#define to_isx031(_sd) container_of(_sd, struct isx031, sd)
+
+#define ISX031_OTP_TYPE_NAME           CCI_REG16_LE(0x7E8A)
+#define ISX031_OTP_TYPE_NAME_MASK      0x0FFF
+#define ISX031_OTP_MODULE_ID_L         0x031
+
+#define ISX031_REG_MODE_SET_F          CCI_REG8(0x8A01)
+#define ISX031_MODE_STANDBY            0x00
+#define ISX031_MODE_STREAMING          0x80
+
+#define ISX031_REG_SENSOR_STATE                CCI_REG8(0x6005)
+#define ISX031_STATE_STREAMING         0x05
+#define ISX031_STATE_STARTUP           0x02
+
+#define ISX031_REG_MODE_SET_F_LOCK     CCI_REG8(0xBEF0)
+#define ISX031_MODE_UNLOCK             0x53
+
+#define ISX031_REG_MODE_SELECT         CCI_REG8(0x8A00)
+#define ISX031_MODE_4LANES_30FPS       0x17
+
+#define ISX031_NUM_LANES               4
+
+#define ISX031_REG_AEMODE              CCI_REG8(0xABC0)
+#define ISX031_AEMODE_AUTO             0x00
+#define ISX031_AEMODE_FULL_ME          0x03
+#define ISX031_REG_FME_SHTVAL          CCI_REG32_LE(0xABEC)
+#define ISX031_REG_FME_SHTVAL_UNIT     CCI_REG8(0xABF0)
+#define ISX031_REG_FME_SHTVAL_S1       CCI_REG32_LE(0xABF4)
+#define ISX031_REG_FME_SHTVAL_S1_UNIT  CCI_REG8(0xABF8)
+#define ISX031_FME_SHTVAL_UNIT_USEC    0x03
+
+/* Exposure time applied by the sensor in either AE mode, in 100 us units */
+#define ISX031_REG_SHT_TIME_SP1                CCI_REG16_LE(0x61A2)
+#define ISX031_SHT_TIME_SP1_UNIT_USEC  100
+
+#define ISX031_INCK_FREQ               (24 * HZ_PER_MHZ)
+
+#define ISX031_READ_REG_RETRY_TIMEOUT  50
+#define ISX031_REG_SLEEP_50MS          50
+
+/*
+ * Exposure is in microseconds. Full manual exposure (Full ME, AEMODE=3) is
+ * limited to one frame period so it does not lower the frame rate.
+ */
+#define ISX031_EXPOSURE_MIN            1200
+#define ISX031_EXPOSURE_STEP           1
+#define ISX031_EXPOSURE_DEF            ISX031_EXPOSURE_MIN
+
+struct isx031_reg_list {
+       u32 num_of_regs;
+       const struct cci_reg_sequence *regs;
+};
+
+struct isx031_mode {
+       u32 width;      /* Frame width in pixels */
+       u32 height;     /* Frame height in pixels */
+       u32 code;       /* MEDIA_BUS_FMT */
+       u16 fps;        /* MODE_FPS */
+       u8 datatype;    /* CSI-2 data type ID */
+
+       /* Sensor register settings for a specific resolution */
+       const struct isx031_reg_list reg_list;
+};
+
+struct isx031 {
+       struct v4l2_subdev sd;
+       struct v4l2_ctrl_handler ctrls;
+       struct v4l2_ctrl *ctrl_exposure_auto;
+       struct v4l2_ctrl *ctrl_exposure;
+
+       struct device *dev;
+       struct regmap *regmap;
+
+       struct gpio_desc *reset_gpio;
+       struct gpio_desc *fsin_gpio;
+       struct media_pad pad;
+
+       bool frame_sync;        /* External frame sync over GMSL (opt-in) */
+};
+
+static const s64 isx031_link_frequencies[] = {
+       288 * HZ_PER_MHZ,       /* 576 Mbps/lane DDR */
+};
+
+static const struct cci_reg_sequence isx031_init_reg[] = {
+       {CCI_REG8(0xFFFF), 0x00}, /* Select mode */
+       {CCI_REG8(0x0171), 0x00}, /* Close F_EBD */
+       {CCI_REG8(0x0172), 0x00}, /* Close R_EBD */
+       {CCI_REG8(0xBF14), 0x00}, /* SG_MODE_APL */
+       {CCI_REG8(0x8AF0), 0x00}, /* Internal sync */
+};
+
+static const struct cci_reg_sequence isx031_framesync_stream_reg[] = {
+       {CCI_REG8(0x8AF0), 0x02}, /* External pulse-based sync */
+       {CCI_REG8(0x8AF1), 0x00},
+       {CCI_REG8(0x8AFE), 0x00},
+       {CCI_REG8(0x8AFF), 0x0C},
+       {CCI_REG8(0xBF14), 0x02}, /* SG_MODE_APL */

drivers/media/i2c/isx031.c line 128

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+};
+
+/*
+ * Crop settings are frame-rate independent; the frame rate is selected
+ * separately through ISX031_REG_MODE_SELECT.
+ */
+static const struct cci_reg_sequence isx031_1920_1536_reg[] = {
+       {CCI_REG8(0x8AA8), 0x01}, /* Crop enable */
+       {CCI_REG8(0x8AAA), 0x80}, /* H size = 1920 */
+       {CCI_REG8(0x8AAB), 0x07},

CCI_REG16_LE(0x8AAA), 0x0780) ?
Ditto for most of the other registers.

Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+       {CCI_REG8(0x8AAC), 0x00}, /* H croped 0 */
+       {CCI_REG8(0x8AAD), 0x00},
+       {CCI_REG8(0x8AAE), 0x00}, /* V size 1536 */
+       {CCI_REG8(0x8AAF), 0x06},
+       {CCI_REG8(0x8AB0), 0x00}, /* V cropped 0 */
+       {CCI_REG8(0x8AB1), 0x00},

drivers/media/i2c/isx031.c line 144

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       {CCI_REG8(0x8ADA), 0x03}, /* DCROP_DATA_SEL */
+       {CCI_REG8(0xBF04), 0x01},
+       {CCI_REG8(0xBF06), 0x80},
+       {CCI_REG8(0xBF07), 0x07},
+       {CCI_REG8(0xBF08), 0x00},
+       {CCI_REG8(0xBF09), 0x00},
+       {CCI_REG8(0xBF0A), 0x00},
+       {CCI_REG8(0xBF0B), 0x06},
+       {CCI_REG8(0xBF0C), 0x00},
+       {CCI_REG8(0xBF0D), 0x00},

All these registers appear to be reuse of values from the struct isx031_mode, or simple computation for offsets based on those values and the array size. Could they be set programmatically instead of tables of registers?

Quoted 70 lines
drivers/media/i2c/isx031.c new file +70 -0
+};
+
+static const struct cci_reg_sequence isx031_1920_1080_reg[] = {
+       {CCI_REG8(0x8AA8), 0x01}, /* Crop enable */
+       {CCI_REG8(0x8AAA), 0x80}, /* H size = 1920 */
+       {CCI_REG8(0x8AAB), 0x07},
+       {CCI_REG8(0x8AAC), 0x00}, /* H croped 0 */
+       {CCI_REG8(0x8AAD), 0x00},
+       {CCI_REG8(0x8AAE), 0x38}, /* V size 1080 */
+       {CCI_REG8(0x8AAF), 0x04},
+       {CCI_REG8(0x8AB0), 0xE4}, /* V cropped 228*2 */
+       {CCI_REG8(0x8AB1), 0x00},
+       {CCI_REG8(0x8ADA), 0x03}, /* DCROP_DATA_SEL */
+       {CCI_REG8(0xBF04), 0x01},
+       {CCI_REG8(0xBF06), 0x80},
+       {CCI_REG8(0xBF07), 0x07},
+       {CCI_REG8(0xBF08), 0x00},
+       {CCI_REG8(0xBF09), 0x00},
+       {CCI_REG8(0xBF0A), 0x38},
+       {CCI_REG8(0xBF0B), 0x04},
+       {CCI_REG8(0xBF0C), 0xE4},
+       {CCI_REG8(0xBF0D), 0x00},
+};
+
+static const struct cci_reg_sequence isx031_1280_720_30fps_reg[] = {
+       {CCI_REG8(0x8AA8), 0x01}, /* Crop enable */
+       {CCI_REG8(0x8AAA), 0x00}, /* H size = 1280 */
+       {CCI_REG8(0x8AAB), 0x05},
+       {CCI_REG8(0x8AAC), 0x40}, /* H croped 320*2 */
+       {CCI_REG8(0x8AAD), 0x01},
+       {CCI_REG8(0x8AAE), 0xD0}, /* V size 720 */
+       {CCI_REG8(0x8AAF), 0x02},
+       {CCI_REG8(0x8AB0), 0x98}, /* V cropped 408*2 */
+       {CCI_REG8(0x8AB1), 0x01},
+       {CCI_REG8(0x8ADA), 0x03}, /* DCROP_DATA_SEL */
+       {CCI_REG8(0xBF04), 0x01},
+       {CCI_REG8(0xBF06), 0x00},
+       {CCI_REG8(0xBF07), 0x05},
+       {CCI_REG8(0xBF08), 0x40},
+       {CCI_REG8(0xBF09), 0x01},
+       {CCI_REG8(0xBF0A), 0xD0},
+       {CCI_REG8(0xBF0B), 0x02},
+       {CCI_REG8(0xBF0C), 0x98},
+       {CCI_REG8(0xBF0D), 0x01},
+};
+
+static const struct isx031_reg_list isx031_init_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_init_reg),
+       .regs = isx031_init_reg,
+};
+
+static const struct isx031_reg_list isx031_framesync_stream_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_framesync_stream_reg),
+       .regs = isx031_framesync_stream_reg,
+};
+
+static const struct isx031_reg_list isx031_1920_1536_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_1920_1536_reg),
+       .regs = isx031_1920_1536_reg,
+};
+
+static const struct isx031_reg_list isx031_1920_1080_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_1920_1080_reg),
+       .regs = isx031_1920_1080_reg,
+};
+
+static const struct isx031_reg_list isx031_1280_720_30fps_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_1280_720_30fps_reg),
+       .regs = isx031_1280_720_30fps_reg,
+};

drivers/media/i2c/isx031.c line 224

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+static const struct isx031_mode supported_modes[] = {
+       {
+               .width          = 1920,
+               .height         = 1536,
+               .code           = MEDIA_BUS_FMT_UYVY8_1X16,
+               .datatype       = MIPI_CSI2_DT_YUV422_8B,
+               .fps            = 30,
+               .reg_list       = isx031_1920_1536_reg_list,
+       },

There's been a push for free-form cropping from sensors rather than tables of modes. Is there a reason that this sensor has to have a specific table of modes?

code and datatype don't change between modes and that's acknowledged in isx031_enum_mbus_code(), so they could be removed.

Quoted 81 lines
drivers/media/i2c/isx031.c new file +81 -0
+       {
+               .width          = 1920,
+               .height         = 1080,
+               .code           = MEDIA_BUS_FMT_UYVY8_1X16,
+               .datatype       = MIPI_CSI2_DT_YUV422_8B,
+               .fps            = 30,
+               .reg_list       = isx031_1920_1080_reg_list,
+       },
+       {
+               .width          = 1280,
+               .height         = 720,
+               .code           = MEDIA_BUS_FMT_UYVY8_1X16,
+               .datatype       = MIPI_CSI2_DT_YUV422_8B,
+               .fps            = 30,
+               .reg_list       = isx031_1280_720_30fps_reg_list,
+       },
+};
+
+static int isx031_wait_state(struct isx031 *isx031, u32 state)
+{
+       u64 val = 0;
+       int read_ret;
+       int ret;
+
+       /*
+        * read_poll_timeout() retries the read itself; the register value has
+        * to be checked here so that a refused transition is reported.
+        */
+       ret = read_poll_timeout(cci_read, read_ret,
+                               !read_ret && val == state,
+                               10 * USEC_PER_MSEC,
+                               ISX031_READ_REG_RETRY_TIMEOUT * 10 * USEC_PER_MSEC,
+                               false, isx031->regmap,
+                               ISX031_REG_SENSOR_STATE, &val, NULL);
+       /* Seen over GMSL: DEVSTS never reports the new state, yet streaming works */
+       if (ret)
+               dev_warn(isx031->dev,
+                        "Sensor state 0x%02llx, expected 0x%02x, continuing\n",
+                        val, state);
+
+       return 0;
+}
+
+static int isx031_mode_transit(struct isx031 *isx031, int state)
+{
+       struct device *dev = isx031->dev;
+       int ret;
+       u32 mode;
+
+       if (state == ISX031_STATE_STARTUP)
+               mode = ISX031_MODE_STANDBY;
+       else if (state == ISX031_STATE_STREAMING)
+               mode = ISX031_MODE_STREAMING;
+       else
+               return -EINVAL;
+
+       ret = cci_write(isx031->regmap, ISX031_REG_MODE_SELECT,
+                       ISX031_MODE_4LANES_30FPS, NULL);
+       if (ret) {
+               dev_err(dev, "Failed to set drive mode\n");
+               return ret;
+       }
+
+       ret = cci_write(isx031->regmap, ISX031_REG_MODE_SET_F_LOCK,
+                       ISX031_MODE_UNLOCK, NULL);
+       if (ret) {
+               dev_err(dev, "Failed to unlock mode\n");
+               return ret;
+       }
+
+       ret = cci_write(isx031->regmap, ISX031_REG_MODE_SET_F, mode, NULL);
+       if (ret) {
+               dev_err(dev, "Failed to set mode 0x%x\n", mode);
+               return ret;
+       }
+
+       return isx031_wait_state(isx031, state);
+}
+
+static int isx031_initialize_module(struct isx031 *isx031)
+{

drivers/media/i2c/isx031.c line 315

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       int ret;
+       u64 val;
+
+       ret = cci_read(isx031->regmap, ISX031_REG_SENSOR_STATE, &val, NULL);
+       if (ret) {
+               dev_err(isx031->dev, "Failed to read sensor state\n");
+               return ret;
+       }
+
+       /* If sensor is streaming, transition to startup before initialization */

isx031_initialize_module is only called from isx031_resume, which brings the sensor out of reset and therefore it is presumably in ISX031_STATE_STARTUP already.
Admittedly the reset_gpio is optional, but it would feel cleaner to put the sensor into ISX031_STATE_STARTUP in isx031_power_off before asserting reset to cover that, therefore guaranteeing the state.

Actually you do call isx031_mode_transit(isx031, ISX031_STATE_STARTUP); from disable_streams, which really makes me wonder how you got into this state in the first place.

Quoted 76 lines
drivers/media/i2c/isx031.c new file +76 -0
+       if (val == ISX031_STATE_STREAMING) {
+               ret = isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+               if (ret)
+                       return ret;
+       }
+
+       cci_multi_reg_write(isx031->regmap, isx031_init_reg_list.regs,
+                           isx031_init_reg_list.num_of_regs, &ret);
+
+       return ret;
+}
+
+static int isx031_identify_module(struct isx031 *isx031)
+{
+       struct device *dev = isx031->dev;
+       u64 name = 0;
+       int read_ret;
+       int ret;
+
+       /* After reset the sensor first NAKs, then reads 0xffff until OTP is loaded */
+       ret = read_poll_timeout(cci_read, read_ret,
+                               !read_ret && (name & ISX031_OTP_TYPE_NAME_MASK) ==
+                                            ISX031_OTP_MODULE_ID_L,
+                               10 * USEC_PER_MSEC,
+                               ISX031_READ_REG_RETRY_TIMEOUT * 10 * USEC_PER_MSEC,
+                               false, isx031->regmap, ISX031_OTP_TYPE_NAME,
+                               &name, NULL);
+       if (read_ret) {
+               dev_err(dev, "Failed to read OTP type name register\n");
+               return read_ret;
+       }
+       if (ret) {
+               dev_err(dev,
+                       "Invalid module ID: expected 0x%04x, got 0x%04llx\n",
+                       ISX031_OTP_MODULE_ID_L,
+                       name & ISX031_OTP_TYPE_NAME_MASK);
+               return -ENODEV;
+       }
+
+       return 0;
+}
+
+static void isx031_update_pad_format(const struct isx031_mode *mode,
+                                    struct v4l2_mbus_framefmt *fmt)
+{
+       fmt->width = mode->width;
+       fmt->height = mode->height;
+       fmt->code = mode->code;
+       fmt->field = V4L2_FIELD_NONE;
+       fmt->colorspace = V4L2_COLORSPACE_SRGB;
+       fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
+       fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
+       fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
+}
+
+static int isx031_parse_endpoint(struct device *dev)
+{
+       struct fwnode_handle *endpoint;
+       struct v4l2_fwnode_endpoint bus_cfg = {
+               .bus_type = V4L2_MBUS_CSI2_DPHY,
+       };
+       unsigned long link_freq_bitmap;
+       int ret;
+
+       endpoint =
+               fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
+                                               0, 0,
+                                               FWNODE_GRAPH_ENDPOINT_NEXT);
+       if (!endpoint)
+               return dev_err_probe(dev, -EPROBE_DEFER,
+                                    "waiting for fwnode graph endpoint\n");
+
+       ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
+       fwnode_handle_put(endpoint);
+       if (ret)
+               return dev_err_probe(dev, ret,

drivers/media/i2c/isx031.c line 401

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+                                    "Failed to parse endpoint node\n");
+
+       if (bus_cfg.bus.mipi_csi2.num_data_lanes != ISX031_NUM_LANES) {
+               dev_err(dev, "Only %u data lanes are supported\n",
+                       ISX031_NUM_LANES);
+               ret = -EINVAL;
+               goto out_err;
+       }
+
+       /* Endpoints created for the sensor behind a GMSL link carry no frequencies */

Doesn't the GMSL serializer need to be configured at any level to know what rate data is coming in, or have Sony really combined the serializer into the sensor such that there is no CSI bus?

The comment at the top of the file says "The sensor is controlled over I2C with the CCI helpers and outputs MIPI CSI-2.", so it feels a little odd that there are any references to GMSL in the driver at all.

Quoted 140 lines
drivers/media/i2c/isx031.c new file +140 -0
+       if (bus_cfg.nr_of_link_frequencies) {
+               ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies,
+                                              bus_cfg.nr_of_link_frequencies,
+                                              isx031_link_frequencies,
+                                              ARRAY_SIZE(isx031_link_frequencies),
+                                              &link_freq_bitmap);
+               if (ret)
+                       goto out_err;
+       }
+
+out_err:
+       v4l2_fwnode_endpoint_free(&bus_cfg);
+       return ret;
+}
+
+static const struct isx031_mode *
+isx031_state_mode(struct v4l2_subdev_state *state)
+{
+       const struct v4l2_mbus_framefmt *fmt;
+
+       fmt = v4l2_subdev_state_get_format(state, 0);
+
+       return v4l2_find_nearest_size(supported_modes,
+                                     ARRAY_SIZE(supported_modes),
+                                     width, height, fmt->width, fmt->height);
+}
+
+static int isx031_enable_streams(struct v4l2_subdev *subdev,
+                                struct v4l2_subdev_state *state,
+                                u32 pad, u64 streams_mask)
+{
+       struct isx031 *isx031 = to_isx031(subdev);
+       struct device *dev = isx031->dev;
+       const struct isx031_mode *mode = isx031_state_mode(state);
+       const struct isx031_reg_list *reg_list;
+       int ret;
+
+       ret = pm_runtime_resume_and_get(dev);
+       if (ret < 0)
+               return ret;
+
+       /* Mode registers must be reprogrammed in standby */
+       cci_write(isx031->regmap, ISX031_REG_MODE_SET_F_LOCK,
+                 ISX031_MODE_UNLOCK, &ret);
+       cci_write(isx031->regmap, ISX031_REG_MODE_SET_F,
+                 ISX031_MODE_STANDBY, &ret);
+       if (ret) {
+               dev_err(dev, "Failed to enter standby: %d\n", ret);
+               goto err_put;
+       }
+       msleep(ISX031_REG_SLEEP_50MS);
+
+       reg_list = &mode->reg_list;
+       cci_multi_reg_write(isx031->regmap, reg_list->regs,
+                           reg_list->num_of_regs, &ret);
+       if (isx031->frame_sync) {
+               reg_list = &isx031_framesync_stream_reg_list;
+               cci_multi_reg_write(isx031->regmap, reg_list->regs,
+                                   reg_list->num_of_regs, &ret);
+       }
+       if (ret) {
+               dev_err(dev, "Failed to set stream mode: %d\n", ret);
+               goto err_put;
+       }
+
+       ret = isx031_mode_transit(isx031, ISX031_STATE_STREAMING);
+       if (ret) {
+               dev_err(dev, "Failed to start streaming: %d\n", ret);
+               goto err_stop;
+       }
+
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL_UNIT,
+                 ISX031_FME_SHTVAL_UNIT_USEC, &ret);
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL_S1_UNIT,
+                 ISX031_FME_SHTVAL_UNIT_USEC, &ret);
+       if (ret) {
+               dev_err(dev, "Failed to set exposure unit: %d\n", ret);
+               goto err_stop;
+       }
+
+       /*
+        * The STREAMING transition resets the AE registers, so re-apply the
+        * cached control values once the sensor runs. This runs with the state
+        * lock held, which v4l2_subdev_enable_streams() also uses as the
+        * control handler lock, as required by __v4l2_ctrl_handler_setup().
+        */
+       ret = __v4l2_ctrl_handler_setup(&isx031->ctrls);
+       if (ret) {
+               dev_err(dev, "Failed to apply controls, stopping stream: %d\n",
+                       ret);
+               goto err_stop;
+       }
+
+       /* AEMODE must not be changed while streaming */
+       __v4l2_ctrl_grab(isx031->ctrl_exposure_auto, true);
+
+       return 0;
+
+err_stop:
+       isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+err_put:
+       pm_runtime_put(dev);
+
+       return ret;
+}
+
+static int isx031_disable_streams(struct v4l2_subdev *subdev,
+                                 struct v4l2_subdev_state *state,
+                                 u32 pad, u64 streams_mask)
+{
+       struct isx031 *isx031 = to_isx031(subdev);
+       int ret;
+
+       __v4l2_ctrl_grab(isx031->ctrl_exposure_auto, false);
+
+       ret = isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+       if (ret)
+               dev_err(isx031->dev, "Failed to stop streaming: %d\n", ret);
+
+       pm_runtime_put(isx031->dev);
+
+       return 0;
+}
+
+static int isx031_power_off(struct device *dev)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+       struct isx031 *isx031 = to_isx031(sd);
+
+       /* Active low gpio reset, set 1 to power off sensor */
+       gpiod_set_value_cansleep(isx031->reset_gpio, 1);
+
+       return 0;
+}
+
+static int isx031_resume(struct device *dev)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);

drivers/media/i2c/isx031.c line 551

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       struct isx031 *isx031 = to_isx031(sd);
+       int ret;
+
+       /* Active low; isx031_identify_module() polls until the sensor has booted */
+       gpiod_set_value_cansleep(isx031->reset_gpio, 0);
+
+       /* S4 clears the GPIO bias/config, so re-set fsin direction and level */
+       gpiod_direction_output(isx031->fsin_gpio, 0);
+
+       ret = isx031_identify_module(isx031);

Is there a need to identify the module on every resume?

Quoted 108 lines
drivers/media/i2c/isx031.c new file +108 -0
+       if (ret) {
+               dev_err(dev, "Failed to identify sensor module: %d\n", ret);
+               return ret;
+       }
+
+       return isx031_initialize_module(isx031);
+}
+
+static int isx031_get_frame_desc(struct v4l2_subdev *sd,
+                                unsigned int pad,
+                                struct v4l2_mbus_frame_desc *desc)
+{
+       struct v4l2_subdev_state *state;
+       const struct isx031_mode *mode;
+
+       state = v4l2_subdev_lock_and_get_active_state(sd);
+       mode = isx031_state_mode(state);
+       v4l2_subdev_unlock_state(state);
+
+       desc->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
+       desc->num_entries = 1;
+       desc->entry[0].pixelcode = mode->code;
+       desc->entry[0].stream = 0;
+       desc->entry[0].bus.csi2.vc = 0;
+       desc->entry[0].bus.csi2.dt = mode->datatype;
+       return 0;
+}
+
+static u32 isx031_exposure_max(u16 fps)
+{
+       return USEC_PER_SEC / fps;
+}
+
+/* Called with the control handler lock (sd->state_lock) held */
+static int isx031_update_exposure_range(struct isx031 *isx031,
+                                       const struct isx031_mode *mode)
+{
+       struct v4l2_ctrl *exposure = isx031->ctrl_exposure;
+       s64 max = isx031_exposure_max(mode->fps);
+
+       return __v4l2_ctrl_modify_range(exposure, exposure->minimum, max,
+                                       exposure->step,
+                                       min(exposure->default_value, max));
+}
+
+static int isx031_set_format(struct v4l2_subdev *sd,
+                            const struct v4l2_subdev_client_info *ci,
+                            struct v4l2_subdev_state *sd_state,
+                            struct v4l2_subdev_format *fmt)
+{
+       struct isx031 *isx031 = to_isx031(sd);
+       const struct isx031_mode *mode;
+       struct v4l2_fract *interval;
+
+       /* The sensor keeps the old mode until the next stream start. */
+       if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+           media_entity_is_streaming(&sd->entity))
+               return -EBUSY;
+
+       mode = v4l2_find_nearest_size(supported_modes,
+                                     ARRAY_SIZE(supported_modes),
+                                     width, height,
+                                     fmt->format.width, fmt->format.height);
+
+       isx031_update_pad_format(mode, &fmt->format);
+       *v4l2_subdev_state_get_format(sd_state, fmt->pad) = fmt->format;
+
+       interval = v4l2_subdev_state_get_interval(sd_state, fmt->pad);
+       interval->numerator = 1;
+       interval->denominator = mode->fps;
+
+       if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
+               return isx031_update_exposure_range(isx031, mode);
+
+       return 0;
+}
+
+static int isx031_init_state(struct v4l2_subdev *sd,
+                            struct v4l2_subdev_state *sd_state)
+{
+       struct v4l2_fract *interval = v4l2_subdev_state_get_interval(sd_state, 0);
+
+       isx031_update_pad_format(&supported_modes[0],
+                                v4l2_subdev_state_get_format(sd_state, 0));
+       interval->numerator = 1;
+       interval->denominator = supported_modes[0].fps;
+
+       return 0;
+}
+
+static int isx031_enum_mbus_code(struct v4l2_subdev *sd,
+                                struct v4l2_subdev_state *sd_state,
+                                struct v4l2_subdev_mbus_code_enum *code)
+{
+       /* All modes share a single media-bus code */
+       if (code->index > 0)
+               return -EINVAL;
+
+       code->code = supported_modes[0].code;
+
+       return 0;
+}
+
+static int isx031_enum_frame_size(struct v4l2_subdev *sd,
+                                 struct v4l2_subdev_state *sd_state,
+                                 struct v4l2_subdev_frame_size_enum *fse)
+{
+       unsigned int i, count = 0;

drivers/media/i2c/isx031.c line 669

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+       for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
+               if (supported_modes[i].code != fse->code)
+                       continue;
+
+               /* Skip fps variants that repeat an already-listed resolution */
+               if (i > 0 &&
+                   supported_modes[i].code == supported_modes[i - 1].code &&
+                   supported_modes[i].width == supported_modes[i - 1].width &&
+                   supported_modes[i].height == supported_modes[i - 1].height)

There aren't any modes that are repeats, and you've stated earlier that
* Crop settings are frame-rate independent; the frame rate is selected
* separately through ISX031_REG_MODE_SELECT."
so why are the mode tables (if they remain) including a frame rate in the first place?

Quoted 195 lines
drivers/media/i2c/isx031.c new file +195 -0
+                       continue;
+
+               if (count == fse->index) {
+                       fse->min_width = supported_modes[i].width;
+                       fse->max_width = supported_modes[i].width;
+                       fse->min_height = supported_modes[i].height;
+                       fse->max_height = supported_modes[i].height;
+                       return 0;
+               }
+               count++;
+       }
+
+       return -EINVAL;
+}
+
+static int isx031_enum_frame_interval(struct v4l2_subdev *sd,
+                                     struct v4l2_subdev_state *sd_state,
+                                     struct v4l2_subdev_frame_interval_enum *fie)
+{
+       unsigned int i, count = 0;
+
+       for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
+               if (supported_modes[i].code != fie->code ||
+                   supported_modes[i].width != fie->width ||
+                   supported_modes[i].height != fie->height)
+                       continue;
+
+               if (count == fie->index) {
+                       fie->interval.numerator = 1;
+                       fie->interval.denominator = supported_modes[i].fps;
+                       return 0;
+               }
+               count++;
+       }
+
+       return -EINVAL;
+}
+
+/* Each resolution has a single fixed frame rate, set along with the format */
+static int isx031_set_frame_interval(struct v4l2_subdev *sd,
+                                    struct v4l2_subdev_state *sd_state,
+                                    struct v4l2_subdev_frame_interval *fi)
+{
+       fi->interval = *v4l2_subdev_state_get_interval(sd_state, fi->pad);
+
+       return 0;
+}
+
+static const struct v4l2_subdev_video_ops isx031_video_ops = {
+       .s_stream = v4l2_subdev_s_stream_helper,
+};
+
+static const struct v4l2_subdev_pad_ops isx031_pad_ops = {
+       .set_fmt = isx031_set_format,
+       .get_fmt = v4l2_subdev_get_fmt,
+       .get_frame_desc = isx031_get_frame_desc,
+       .enable_streams = isx031_enable_streams,
+       .disable_streams = isx031_disable_streams,
+       .enum_mbus_code = isx031_enum_mbus_code,
+       .enum_frame_size = isx031_enum_frame_size,
+       .enum_frame_interval = isx031_enum_frame_interval,
+       .get_frame_interval = v4l2_subdev_get_frame_interval,
+       .set_frame_interval = isx031_set_frame_interval,
+};
+
+static const struct v4l2_subdev_ops isx031_subdev_ops = {
+       .video = &isx031_video_ops,
+       .pad = &isx031_pad_ops,
+};
+
+static const struct v4l2_subdev_internal_ops isx031_internal_ops = {
+       .init_state = isx031_init_state,
+};
+
+static int isx031_set_exposure_value(struct isx031 *isx031, u32 val)
+{
+       int ret = 0;
+
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL, val, &ret);
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL_S1, val, &ret);
+
+       return ret;
+}
+
+static int isx031_apply_ctrls(struct isx031 *isx031)
+{
+       struct device *dev = isx031->dev;
+       bool manual = isx031->ctrl_exposure_auto->val == V4L2_EXPOSURE_MANUAL;
+       u32 val;
+       int ret = 0;
+
+       val = manual ? ISX031_AEMODE_FULL_ME : ISX031_AEMODE_AUTO;
+
+       cci_write(isx031->regmap, ISX031_REG_AEMODE, val, &ret);
+       if (ret) {
+               dev_err(dev, "Failed to write AEMODE=0x%02x: %d\n", val, ret);
+               return ret;
+       }
+
+       if (manual)
+               return isx031_set_exposure_value(isx031,
+                                                isx031->ctrl_exposure->val);
+
+       return 0;
+}
+
+static int isx031_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct isx031 *isx031 = container_of(ctrl->handler,
+                                            struct isx031, ctrls);
+       struct device *dev = isx031->dev;
+       int ret;
+
+       /* Cached values are applied by __v4l2_ctrl_handler_setup() at stream start */
+       ret = pm_runtime_get_if_in_use(dev);
+       if (ret <= 0)
+               return 0;
+
+       ret = isx031_apply_ctrls(isx031);
+
+       pm_runtime_put(dev);
+
+       return ret;
+}
+
+static int isx031_get_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct isx031 *isx031 = container_of(ctrl->handler,
+                                            struct isx031, ctrls);
+       struct device *dev = isx031->dev;
+       struct v4l2_ctrl *exposure;
+       u64 val;
+       int ret;
+
+       /* If not powered up, the framework falls back to cur.val */
+       ret = pm_runtime_get_if_in_use(dev);
+       if (ret <= 0)
+               return 0;
+
+       /*
+        * FME_SHTVAL only applies in Full ME mode and keeps the last manual
+        * value under AE, so report the exposure the sensor actually applies.
+        * This is called on the cluster master, so update the exposure control.
+        */
+       ret = cci_read(isx031->regmap, ISX031_REG_SHT_TIME_SP1, &val, NULL);
+       if (ret) {
+               dev_err(dev, "Failed to read SHT_TIME_SP1: %d\n", ret);
+               goto out;
+       }
+       exposure = isx031->ctrl_exposure;
+       exposure->val = clamp_t(s64, val * ISX031_SHT_TIME_SP1_UNIT_USEC,
+                               exposure->minimum, exposure->maximum);
+
+out:
+       pm_runtime_put(dev);
+
+       return ret;
+}
+
+static const struct v4l2_ctrl_ops isx031_ctrl_ops = {
+       .s_ctrl = isx031_set_ctrl,
+       .g_volatile_ctrl = isx031_get_ctrl,
+};
+
+static int isx031_ctrls_init(struct isx031 *sensor)
+{
+       struct v4l2_fwnode_device_properties props;
+       struct v4l2_ctrl *link_freq;
+       struct v4l2_ctrl_handler *hdl = &sensor->ctrls;
+       int ret;
+
+       ret = v4l2_fwnode_device_parse(sensor->dev, &props);
+       if (ret)
+               return ret;
+
+       v4l2_ctrl_handler_init(hdl, 5);
+
+       /* There's a need to set the link frequency because IPU6 dictates it. */
+       link_freq = v4l2_ctrl_new_int_menu(hdl, &isx031_ctrl_ops,
+                                          V4L2_CID_LINK_FREQ,
+                                          ARRAY_SIZE(isx031_link_frequencies) - 1, 0,
+                                          isx031_link_frequencies);
+
+       sensor->ctrl_exposure_auto =
+               v4l2_ctrl_new_std_menu(hdl, &isx031_ctrl_ops,
+                                      V4L2_CID_EXPOSURE_AUTO,
+                                      V4L2_EXPOSURE_MANUAL, 0,
+                                      V4L2_EXPOSURE_AUTO);
+
+       sensor->ctrl_exposure =
+               v4l2_ctrl_new_std(hdl, &isx031_ctrl_ops, V4L2_CID_EXPOSURE,
+                                 ISX031_EXPOSURE_MIN,
+                                 isx031_exposure_max(supported_modes[0].fps),
+                                 ISX031_EXPOSURE_STEP, ISX031_EXPOSURE_DEF);
+

drivers/media/i2c/isx031.c line 874

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       /* Expose the orientation/rotation properties of the module. */
+       ret = v4l2_ctrl_new_fwnode_properties(hdl, &isx031_ctrl_ops, &props);
+       if (ret) {
+               v4l2_ctrl_handler_free(hdl);
+               return ret;
+       }
+
+       link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+       sensor->ctrl_exposure_auto->flags |= V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
+       sensor->ctrl_exposure->flags |= V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;

There's no check that these three controls were created successfully and hence not NULL before the flags are set.
Amend the
if (ret)
following v4l2_ctrl_new_fwnode_properties() to
if (ret || ctrl_hdlr->error)
would cover that.

Quoted 31 lines
drivers/media/i2c/isx031.c new file +31 -0
+
+       /*
+        * Exposure is read back from the sensor in auto mode, so it has to be
+        * part of an auto cluster: that makes the core mark it volatile (and
+        * inactive) while auto exposure is selected.
+        */
+       v4l2_ctrl_auto_cluster(2, &sensor->ctrl_exposure_auto,
+                              V4L2_EXPOSURE_MANUAL, true);
+
+       sensor->sd.ctrl_handler = hdl;
+
+       return 0;
+}
+
+static void isx031_remove(struct i2c_client *client)
+{
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+       struct isx031 *isx031 = to_isx031(sd);
+
+       v4l2_async_unregister_subdev(sd);
+       v4l2_subdev_cleanup(sd);
+       media_entity_cleanup(&sd->entity);
+       v4l2_ctrl_handler_free(&isx031->ctrls);
+
+       pm_runtime_disable(&client->dev);
+       if (!pm_runtime_status_suspended(&client->dev)) {
+               isx031_power_off(&client->dev);
+               pm_runtime_set_suspended(&client->dev);
+       }
+}
+

drivers/media/i2c/isx031.c line 915

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+static int isx031_probe(struct i2c_client *client)
+{
+       struct device *dev = &client->dev;
+       struct v4l2_subdev *sd;
+       struct isx031 *isx031;
+       unsigned long freq;
+       struct clk *clk;
+       int ret;
+
+       isx031 = devm_kzalloc(&client->dev, sizeof(*isx031), GFP_KERNEL);

There's just been a tree-wide conversion to devm_kzalloc_obj. This should follow suit.

Quoted 74 lines
drivers/media/i2c/isx031.c new file +74 -0
+       if (!isx031)
+               return -ENOMEM;
+
+       isx031->dev = dev;
+
+       ret = isx031_parse_endpoint(dev);
+       if (ret)
+               return ret;
+
+       clk = devm_v4l2_sensor_clk_get(dev, NULL);
+       if (IS_ERR(clk))
+               return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
+
+       freq = clk_get_rate(clk);
+       if (freq != ISX031_INCK_FREQ)
+               return dev_err_probe(dev, -EINVAL,
+                                    "External clock %lu Hz not supported\n",
+                                    freq);
+
+       isx031->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+                                                    GPIOD_OUT_HIGH);
+       if (IS_ERR(isx031->reset_gpio))
+               return dev_err_probe(dev, PTR_ERR(isx031->reset_gpio),
+                                    "Failed to get reset gpio\n");
+
+       /*
+        * With external frame sync, FSIN arrives over GMSL, so the line must not
+        * be claimed and driven locally.
+        */
+       if (!isx031->frame_sync) {
+               isx031->fsin_gpio = devm_gpiod_get_optional(dev, "fsin",
+                                                           GPIOD_OUT_LOW);
+               if (IS_ERR(isx031->fsin_gpio))
+                       return dev_err_probe(dev, PTR_ERR(isx031->fsin_gpio),
+                                            "Failed to get fsin gpio\n");
+       }
+
+       isx031->regmap = devm_cci_regmap_init_i2c(client, 16);
+       if (IS_ERR(isx031->regmap))
+               return dev_err_probe(dev, PTR_ERR(isx031->regmap),
+                                    "Failed to init regmap\n");
+
+       /* Initialize subdevice */
+       sd = &isx031->sd;
+       v4l2_i2c_subdev_init(sd, client, &isx031_subdev_ops);
+
+       /* The sensor may not be in reset at this point, so reset it explicitly */
+       isx031_power_off(dev);
+       if (isx031->reset_gpio)
+               msleep(ISX031_REG_SLEEP_50MS);
+
+       ret = isx031_resume(dev);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to power on sensor\n");
+               goto err_power_off;
+       }
+
+       ret = isx031_ctrls_init(isx031);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to init sensor ctrls\n");
+               goto err_power_off;
+       }
+
+       sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+       sd->internal_ops = &isx031_internal_ops;
+       sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
+
+       /* Initialize subdev media pad */
+       isx031->pad.flags = MEDIA_PAD_FL_SOURCE;
+       ret = media_entity_pads_init(&sd->entity, 1, &isx031->pad);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to init entity pads\n");
+               goto err_ctrl_free;
+       }

drivers/media/i2c/isx031.c line 999

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+       isx031->sd.state_lock = isx031->sd.ctrl_handler->lock;
+       ret = v4l2_subdev_init_finalize(&isx031->sd);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to finalize V4L2 subdev\n");
+               goto err_media_cleanup;
+       }
+
+       pm_runtime_set_active(dev);
+       pm_runtime_enable(dev);

pm_runtime_use_autosuspend?

Quoted 17 lines
drivers/media/i2c/isx031.c new file +17 -0
+
+       ret = v4l2_async_register_subdev_sensor(&isx031->sd);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to register V4L2 subdev\n");
+               goto err_pm_disable;
+       }
+
+       pm_runtime_idle(dev);
+
+       return 0;
+
+err_pm_disable:
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+       v4l2_subdev_cleanup(&isx031->sd);
+err_media_cleanup:
+       media_entity_cleanup(&isx031->sd.entity);

drivers/media/i2c/isx031.c line 1026

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+err_ctrl_free:
+       v4l2_ctrl_handler_free(&isx031->ctrls);
+err_power_off:
+       isx031_power_off(dev);
+
+       return ret;
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(isx031_pm_ops, isx031_power_off,
+                                isx031_resume, NULL);

More commonly these would be named _power_off and _power_on, or _suspend and _resume. One of each feels odd.

Dave

Quoted 30 lines
drivers/media/i2c/isx031.c new file +26 -1
+
+static const struct acpi_device_id isx031_acpi_ids[] = {
+       { "INTC113C" },
+       {}
+};
+
+MODULE_DEVICE_TABLE(acpi, isx031_acpi_ids);
+
+static struct i2c_driver isx031_i2c_driver = {
+       .driver = {
+               .name = "isx031",
+               .acpi_match_table = isx031_acpi_ids,
+               .pm = pm_sleep_ptr(&isx031_pm_ops),
+       },
+       .probe = isx031_probe,
+       .remove = isx031_remove,
+};
+
+module_i2c_driver(isx031_i2c_driver);
+
+MODULE_DESCRIPTION("isx031 sensor driver");
+MODULE_AUTHOR("Hao Yao <[email protected]>");
+MODULE_AUTHOR("Jonathan Lui <[email protected]>");
+MODULE_AUTHOR("Wei Khang, Goh <[email protected]>");
+MODULE_AUTHOR("Yew Chang Ching <[email protected]>");
+MODULE_LICENSE("GPL");
--

2.43.0

author Applied
#4 lore raw

RE: [PATCH v4 2/2] media: i2c: add isx031 sensor driver

Hi Dave,

Thanks for taking a look.

Quoted 6 lines

From: Dave Stevenson <[email protected]>
Sent: Wednesday, 2 December, 2026 7:59 PM
To: Sodhi, Vunny <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v4 2/2] media: i2c: add isx031 sensor driver

Quoted 9 lines

Hi

I did a brief read through as I see this sensor is offered by a few companies with a GMSL serializer, so that opens up use cases.

I may have failed in my searching, but I couldn't find V1 or V2 of this series on linux-media, nor any note about what changed between versions. Those would have been useful to see what previous reviewers had said.

Apologies for the confusion. v1 and v2 were never posted to linux-media; they were only reviewed internally, and I should not have
counted them in the upstream version number. This posting is the first public version of the driver, so there is no earlier upstream review
to refer to.
For the reference, I have update v5 cover letter accordingly to include version history.

Quoted 223 lines · On Wed, 2 Dec 2026 at 09:36, <[email protected]> wrote:
Quoted 222 lines

From: "Yew, Chang Ching" <[email protected]>

Add a V4L2 sub-device driver for the Sony ISX031 YUV422 image sensor. The sensor is controlled over I2C with the CCI helpers and outputs MIPI CSI-2.

Supported modes:
- 1920x1536 at 30 fps
- 1920x1080 at 30 fps
- 1280x720 at 30 fps

The frame rate is selected according to the configured CSI-2 lane count. The driver supports runtime PM, auto/manual exposure controls and an optional external frame-sync input (fsin GPIO).

The driver is ACPI-only: it binds through the INTC113C HID and relies on the IPU bridge for the endpoint (lane count) and device properties.

---
 MAINTAINERS                |    7 +
 drivers/media/i2c/Kconfig  |   11 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/isx031.c | 1052
++++++++++++++++++++++++++++++++++++
 4 files changed, 1071 insertions(+)
 create mode 100644 drivers/media/i2c/isx031.c
MAINTAINERS index renamed from MAINTAINERS +0 -0

ccbcdc0799b9..ccb3a1e077e9 100644

MAINTAINERS +7 -0
@@ -26266,6 +26266,12 @@ S:     Maintained
 F:     Documentation/devicetree/bindings/media/i2c/sony,imx678.yaml
 F:     drivers/media/i2c/imx678.c
 
+SONY ISX031 SENSOR DRIVER
+M:     Yew, Chang Ching <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     drivers/media/i2c/isx031.c
+
 SONY MEMORYSTICK SUBSYSTEM
 M:     Maxim Levitsky <[email protected]>
 M:     Alex Dubov <[email protected]>
@@ -29374,6 +29380,7 @@ F:      drivers/media/i2c/ar*
 F:     drivers/media/i2c/gc*
 F:     drivers/media/i2c/hi*
 F:     drivers/media/i2c/imx*
+F:     drivers/media/i2c/isx*
 F:     drivers/media/i2c/mt*
 F:     drivers/media/i2c/og*
 F:     drivers/media/i2c/ov*
drivers/media/i2c/Kconfig +11 -0
index 11c30252540e..cdca5c8fcf53 100644
@@ -300,6 +300,17 @@ config VIDEO_IMX415
          To compile this driver as a module, choose M here: the
          module will be called imx415.
 
+config VIDEO_ISX031
+       tristate "ISX031 sensor support"
+       depends on ACPI || COMPILE_TEST
+       select V4L2_CCI_I2C
+       help
+         This is a Video4Linux2 sensor-level driver for the
+         Sony ISX031 camera.
+
+         To compile this driver as a module, choose M here: the
+         module will be called isx031.
+
 config VIDEO_IMX471
        tristate "Sony IMX471 sensor support"
        select V4L2_CCI_I2C
drivers/media/i2c/Makefile +1 -0
index 59e31af457cd..53449ed01969 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_VIDEO_IMX576) += imx576.o
 obj-$(CONFIG_VIDEO_IMX678) += imx678.o
 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
+obj-$(CONFIG_VIDEO_ISX031) += isx031.o
 obj-$(CONFIG_VIDEO_IT6625) += it6625.o
 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
 obj-$(CONFIG_VIDEO_LM3560) += lm3560.o diff --git

a/drivers/media/i2c/isx031.c b/drivers/media/i2c/isx031.c new file mode 100644 index 000000000000..22bfa016265e

drivers/media/i2c/isx031.c new file +121 -0
@@ -0,0 +1,1052 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2022-2026 Intel Corporation.
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/units.h>
+
+#include <media/mipi-csi2.h>
+#include <media/v4l2-cci.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+
+#define to_isx031(_sd) container_of(_sd, struct isx031, sd)
+
+#define ISX031_OTP_TYPE_NAME           CCI_REG16_LE(0x7E8A)
+#define ISX031_OTP_TYPE_NAME_MASK      0x0FFF
+#define ISX031_OTP_MODULE_ID_L         0x031
+
+#define ISX031_REG_MODE_SET_F          CCI_REG8(0x8A01)
+#define ISX031_MODE_STANDBY            0x00
+#define ISX031_MODE_STREAMING          0x80
+
+#define ISX031_REG_SENSOR_STATE                CCI_REG8(0x6005)
+#define ISX031_STATE_STREAMING         0x05
+#define ISX031_STATE_STARTUP           0x02
+
+#define ISX031_REG_MODE_SET_F_LOCK     CCI_REG8(0xBEF0)
+#define ISX031_MODE_UNLOCK             0x53
+
+#define ISX031_REG_MODE_SELECT         CCI_REG8(0x8A00)
+#define ISX031_MODE_4LANES_30FPS       0x17
+
+#define ISX031_NUM_LANES               4
+
+#define ISX031_REG_AEMODE              CCI_REG8(0xABC0)
+#define ISX031_AEMODE_AUTO             0x00
+#define ISX031_AEMODE_FULL_ME          0x03
+#define ISX031_REG_FME_SHTVAL          CCI_REG32_LE(0xABEC)
+#define ISX031_REG_FME_SHTVAL_UNIT     CCI_REG8(0xABF0)
+#define ISX031_REG_FME_SHTVAL_S1       CCI_REG32_LE(0xABF4)
+#define ISX031_REG_FME_SHTVAL_S1_UNIT  CCI_REG8(0xABF8)
+#define ISX031_FME_SHTVAL_UNIT_USEC    0x03
+
+/* Exposure time applied by the sensor in either AE mode, in 100 us units */
+#define ISX031_REG_SHT_TIME_SP1                CCI_REG16_LE(0x61A2)
+#define ISX031_SHT_TIME_SP1_UNIT_USEC  100
+
+#define ISX031_INCK_FREQ               (24 * HZ_PER_MHZ)
+
+#define ISX031_READ_REG_RETRY_TIMEOUT  50
+#define ISX031_REG_SLEEP_50MS          50
+
+/*
+ * Exposure is in microseconds. Full manual exposure (Full ME,
+AEMODE=3) is
+ * limited to one frame period so it does not lower the frame rate.
+ */
+#define ISX031_EXPOSURE_MIN            1200
+#define ISX031_EXPOSURE_STEP           1
+#define ISX031_EXPOSURE_DEF            ISX031_EXPOSURE_MIN
+
+struct isx031_reg_list {
+       u32 num_of_regs;
+       const struct cci_reg_sequence *regs; };
+
+struct isx031_mode {
+       u32 width;      /* Frame width in pixels */
+       u32 height;     /* Frame height in pixels */
+       u32 code;       /* MEDIA_BUS_FMT */
+       u16 fps;        /* MODE_FPS */
+       u8 datatype;    /* CSI-2 data type ID */
+
+       /* Sensor register settings for a specific resolution */
+       const struct isx031_reg_list reg_list; };
+
+struct isx031 {
+       struct v4l2_subdev sd;
+       struct v4l2_ctrl_handler ctrls;
+       struct v4l2_ctrl *ctrl_exposure_auto;
+       struct v4l2_ctrl *ctrl_exposure;
+
+       struct device *dev;
+       struct regmap *regmap;
+
+       struct gpio_desc *reset_gpio;
+       struct gpio_desc *fsin_gpio;
+       struct media_pad pad;
+
+       bool frame_sync;        /* External frame sync over GMSL (opt-in) */
+};
+
+static const s64 isx031_link_frequencies[] = {
+       288 * HZ_PER_MHZ,       /* 576 Mbps/lane DDR */
+};
+
+static const struct cci_reg_sequence isx031_init_reg[] = {
+       {CCI_REG8(0xFFFF), 0x00}, /* Select mode */
+       {CCI_REG8(0x0171), 0x00}, /* Close F_EBD */
+       {CCI_REG8(0x0172), 0x00}, /* Close R_EBD */
+       {CCI_REG8(0xBF14), 0x00}, /* SG_MODE_APL */
+       {CCI_REG8(0x8AF0), 0x00}, /* Internal sync */ };
+
+static const struct cci_reg_sequence isx031_framesync_stream_reg[] = {
+       {CCI_REG8(0x8AF0), 0x02}, /* External pulse-based sync */
+       {CCI_REG8(0x8AF1), 0x00},
+       {CCI_REG8(0x8AFE), 0x00},
+       {CCI_REG8(0x8AFF), 0x0C},
+       {CCI_REG8(0xBF14), 0x02}, /* SG_MODE_APL */ };
+
+/*
+ * Crop settings are frame-rate independent; the frame rate is
+selected
+ * separately through ISX031_REG_MODE_SELECT.
Quoted 9 lines

drivers/media/i2c/isx031.c line 128

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+ */
+static const struct cci_reg_sequence isx031_1920_1536_reg[] = {
+       {CCI_REG8(0x8AA8), 0x01}, /* Crop enable */
+       {CCI_REG8(0x8AAA), 0x80}, /* H size = 1920 */
+       {CCI_REG8(0x8AAB), 0x07},

CCI_REG16_LE(0x8AAA), 0x0780) ?
Ditto for most of the other registers.

I merged each pair of adjacent 8-bit registers in the crop tables into a single CCI_REG16_LE()

Quoted 17 lines
Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+       {CCI_REG8(0x8AAC), 0x00}, /* H croped 0 */
+       {CCI_REG8(0x8AAD), 0x00},
+       {CCI_REG8(0x8AAE), 0x00}, /* V size 1536 */
+       {CCI_REG8(0x8AAF), 0x06},
+       {CCI_REG8(0x8AB0), 0x00}, /* V cropped 0 */
+       {CCI_REG8(0x8AB1), 0x00},

drivers/media/i2c/isx031.c line 144

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       {CCI_REG8(0x8ADA), 0x03}, /* DCROP_DATA_SEL */
+       {CCI_REG8(0xBF04), 0x01},
+       {CCI_REG8(0xBF06), 0x80},
+       {CCI_REG8(0xBF07), 0x07},
+       {CCI_REG8(0xBF08), 0x00},
+       {CCI_REG8(0xBF09), 0x00},
+       {CCI_REG8(0xBF0A), 0x00},
+       {CCI_REG8(0xBF0B), 0x06},
+       {CCI_REG8(0xBF0C), 0x00},
+       {CCI_REG8(0xBF0D), 0x00},
Quoted 4 lines

All these registers appear to be reuse of values from the struct isx031_mode, or simple computation for offsets based on those values and the array size. Could they be set programmatically instead of tables of registers?

Yes. The three per-mode crop tables are gone. The crop registers are now computed from the mode's width and height in a new isx031_set_crop(), which isx031_enable_streams() calls.

Quoted 83 lines
Quoted 68 lines
drivers/media/i2c/isx031.c new file +68 -0
+};
+
+static const struct cci_reg_sequence isx031_1920_1080_reg[] = {
+       {CCI_REG8(0x8AA8), 0x01}, /* Crop enable */
+       {CCI_REG8(0x8AAA), 0x80}, /* H size = 1920 */
+       {CCI_REG8(0x8AAB), 0x07},
+       {CCI_REG8(0x8AAC), 0x00}, /* H croped 0 */
+       {CCI_REG8(0x8AAD), 0x00},
+       {CCI_REG8(0x8AAE), 0x38}, /* V size 1080 */
+       {CCI_REG8(0x8AAF), 0x04},
+       {CCI_REG8(0x8AB0), 0xE4}, /* V cropped 228*2 */
+       {CCI_REG8(0x8AB1), 0x00},
+       {CCI_REG8(0x8ADA), 0x03}, /* DCROP_DATA_SEL */
+       {CCI_REG8(0xBF04), 0x01},
+       {CCI_REG8(0xBF06), 0x80},
+       {CCI_REG8(0xBF07), 0x07},
+       {CCI_REG8(0xBF08), 0x00},
+       {CCI_REG8(0xBF09), 0x00},
+       {CCI_REG8(0xBF0A), 0x38},
+       {CCI_REG8(0xBF0B), 0x04},
+       {CCI_REG8(0xBF0C), 0xE4},
+       {CCI_REG8(0xBF0D), 0x00},
+};
+
+static const struct cci_reg_sequence isx031_1280_720_30fps_reg[] = {
+       {CCI_REG8(0x8AA8), 0x01}, /* Crop enable */
+       {CCI_REG8(0x8AAA), 0x00}, /* H size = 1280 */
+       {CCI_REG8(0x8AAB), 0x05},
+       {CCI_REG8(0x8AAC), 0x40}, /* H croped 320*2 */
+       {CCI_REG8(0x8AAD), 0x01},
+       {CCI_REG8(0x8AAE), 0xD0}, /* V size 720 */
+       {CCI_REG8(0x8AAF), 0x02},
+       {CCI_REG8(0x8AB0), 0x98}, /* V cropped 408*2 */
+       {CCI_REG8(0x8AB1), 0x01},
+       {CCI_REG8(0x8ADA), 0x03}, /* DCROP_DATA_SEL */
+       {CCI_REG8(0xBF04), 0x01},
+       {CCI_REG8(0xBF06), 0x00},
+       {CCI_REG8(0xBF07), 0x05},
+       {CCI_REG8(0xBF08), 0x40},
+       {CCI_REG8(0xBF09), 0x01},
+       {CCI_REG8(0xBF0A), 0xD0},
+       {CCI_REG8(0xBF0B), 0x02},
+       {CCI_REG8(0xBF0C), 0x98},
+       {CCI_REG8(0xBF0D), 0x01},
+};
+
+static const struct isx031_reg_list isx031_init_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_init_reg),
+       .regs = isx031_init_reg,
+};
+
+static const struct isx031_reg_list isx031_framesync_stream_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_framesync_stream_reg),
+       .regs = isx031_framesync_stream_reg, };
+
+static const struct isx031_reg_list isx031_1920_1536_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_1920_1536_reg),
+       .regs = isx031_1920_1536_reg,
+};
+
+static const struct isx031_reg_list isx031_1920_1080_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_1920_1080_reg),
+       .regs = isx031_1920_1080_reg,
+};
+
+static const struct isx031_reg_list isx031_1280_720_30fps_reg_list = {
+       .num_of_regs = ARRAY_SIZE(isx031_1280_720_30fps_reg),
+       .regs = isx031_1280_720_30fps_reg, };

drivers/media/i2c/isx031.c line 224

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+static const struct isx031_mode supported_modes[] = {
+       {
+               .width          = 1920,
+               .height         = 1536,
+               .code           = MEDIA_BUS_FMT_UYVY8_1X16,
+               .datatype       = MIPI_CSI2_DT_YUV422_8B,
+               .fps            = 30,
+               .reg_list       = isx031_1920_1536_reg_list,
+       },

There's been a push for free-form cropping from sensors rather than tables of modes. Is there a reason that this sensor has to have a specific table of modes?

Quoted 3 lines

code and datatype don't change between modes and that's acknowledged in isx031_enum_mbus_code(), so they could be removed.

No, there's no hardware reason for a mode table. The ISX031 crops with a plain
window: size and offset, multiples of two, with no binning or scaling and the same frame rate at any size. I replaced the table with free-form cropping

Quoted 100 lines
Quoted 21 lines
drivers/media/i2c/isx031.c new file +21 -0
+       {
+               .width          = 1920,
+               .height         = 1080,
+               .code           = MEDIA_BUS_FMT_UYVY8_1X16,
+               .datatype       = MIPI_CSI2_DT_YUV422_8B,
+               .fps            = 30,
+               .reg_list       = isx031_1920_1080_reg_list,
+       },
+       {
+               .width          = 1280,
+               .height         = 720,
+               .code           = MEDIA_BUS_FMT_UYVY8_1X16,
+               .datatype       = MIPI_CSI2_DT_YUV422_8B,
+               .fps            = 30,
+               .reg_list       = isx031_1280_720_30fps_reg_list,
+       },
+};
+
+static int isx031_wait_state(struct isx031 *isx031, u32 state) {
+       u64 val = 0;
+       int read_ret;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       int ret;
+
+       /*
+        * read_poll_timeout() retries the read itself; the register value has
+        * to be checked here so that a refused transition is reported.
+        */
+       ret = read_poll_timeout(cci_read, read_ret,
+                               !read_ret && val == state,
+                               10 * USEC_PER_MSEC,
+                               ISX031_READ_REG_RETRY_TIMEOUT * 10 *

USEC_PER_MSEC,

drivers/media/i2c/isx031.c

Quoted 3 lines
drivers/media/i2c/isx031.c new file +3 -0
+                               false, isx031->regmap,
+                               ISX031_REG_SENSOR_STATE, &val, NULL);
+       /* Seen over GMSL: DEVSTS never reports the new state, yet streaming

works */

Quoted 25 lines
drivers/media/i2c/isx031.c new file +25 -0
+       if (ret)
+               dev_warn(isx031->dev,
+                        "Sensor state 0x%02llx, expected 0x%02x, continuing\n",
+                        val, state);
+
+       return 0;
+}
+
+static int isx031_mode_transit(struct isx031 *isx031, int state) {
+       struct device *dev = isx031->dev;
+       int ret;
+       u32 mode;
+
+       if (state == ISX031_STATE_STARTUP)
+               mode = ISX031_MODE_STANDBY;
+       else if (state == ISX031_STATE_STREAMING)
+               mode = ISX031_MODE_STREAMING;
+       else
+               return -EINVAL;
+
+       ret = cci_write(isx031->regmap, ISX031_REG_MODE_SELECT,
+                       ISX031_MODE_4LANES_30FPS, NULL);
+       if (ret) {
+               dev_err(dev, "Failed to set drive mode\n");
+               return ret;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       }
+
+       ret = cci_write(isx031->regmap, ISX031_REG_MODE_SET_F_LOCK,
+                       ISX031_MODE_UNLOCK, NULL);
+       if (ret) {
+               dev_err(dev, "Failed to unlock mode\n");
+               return ret;
+       }
+
+       ret = cci_write(isx031->regmap, ISX031_REG_MODE_SET_F, mode,

NULL);

drivers/media/i2c/isx031.c

Quoted 12 lines
drivers/media/i2c/isx031.c new file +12 -0
+       if (ret) {
+               dev_err(dev, "Failed to set mode 0x%x\n", mode);
+               return ret;
+       }
+
+       return isx031_wait_state(isx031, state); }
+
+static int isx031_initialize_module(struct isx031 *isx031) {
+       int ret;
+       u64 val;
+
+       ret = cci_read(isx031->regmap, ISX031_REG_SENSOR_STATE, &val,

NULL);

drivers/media/i2c/isx031.c

Quoted 7 lines
drivers/media/i2c/isx031.c new file +7 -0
+       if (ret) {
+               dev_err(isx031->dev, "Failed to read sensor state\n");
+               return ret;
+       }
+
+       /* If sensor is streaming, transition to startup before
+ initialization */

isx031_initialize_module is only called from isx031_resume, which brings the sensor out of reset and therefore it is presumably in ISX031_STATE_STARTUP already.
Admittedly the reset_gpio is optional, but it would feel cleaner to put the sensor into ISX031_STATE_STARTUP in isx031_power_off before asserting reset to cover that, therefore guaranteeing the state.

Quoted 4 lines

Actually you do call isx031_mode_transit(isx031, ISX031_STATE_STARTUP); from disable_streams, which really makes me wonder how you got into this state in the first place.

Agreed. Within the driver the sensor can't be left streaming, since disable_streams() always transitions to STARTUP

Quoted 95 lines
Quoted 9 lines
drivers/media/i2c/isx031.c new file +9 -0
+       if (val == ISX031_STATE_STREAMING) {
+               ret = isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+               if (ret)
+                       return ret;
+       }
+
+       cci_multi_reg_write(isx031->regmap, isx031_init_reg_list.regs,
+                           isx031_init_reg_list.num_of_regs, &ret);
+

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       return ret;
+}
+
+static int isx031_identify_module(struct isx031 *isx031) {
+       struct device *dev = isx031->dev;
+       u64 name = 0;
+       int read_ret;
+       int ret;
+
+       /* After reset the sensor first NAKs, then reads 0xffff until OTP is loaded

*/

drivers/media/i2c/isx031.c

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+       ret = read_poll_timeout(cci_read, read_ret,
+                               !read_ret && (name & ISX031_OTP_TYPE_NAME_MASK) ==
+                                            ISX031_OTP_MODULE_ID_L,
+                               10 * USEC_PER_MSEC,
+                               ISX031_READ_REG_RETRY_TIMEOUT * 10 *

USEC_PER_MSEC,

Quoted 15 lines
drivers/media/i2c/isx031.c new file +15 -0
+                               false, isx031->regmap, ISX031_OTP_TYPE_NAME,
+                               &name, NULL);
+       if (read_ret) {
+               dev_err(dev, "Failed to read OTP type name register\n");
+               return read_ret;
+       }
+       if (ret) {
+               dev_err(dev,
+                       "Invalid module ID: expected 0x%04x, got 0x%04llx\n",
+                       ISX031_OTP_MODULE_ID_L,
+                       name & ISX031_OTP_TYPE_NAME_MASK);
+               return -ENODEV;
+       }
+
+       return 0;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+}
+
+static void isx031_update_pad_format(const struct isx031_mode *mode,
+                                    struct v4l2_mbus_framefmt *fmt) {
+       fmt->width = mode->width;
+       fmt->height = mode->height;
+       fmt->code = mode->code;
+       fmt->field = V4L2_FIELD_NONE;
+       fmt->colorspace = V4L2_COLORSPACE_SRGB;
+       fmt->ycbcr_enc =

V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);

Quoted 28 lines
drivers/media/i2c/isx031.c new file +28 -0
+       fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
+       fmt->xfer_func =
+V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
+}
+
+static int isx031_parse_endpoint(struct device *dev) {
+       struct fwnode_handle *endpoint;
+       struct v4l2_fwnode_endpoint bus_cfg = {
+               .bus_type = V4L2_MBUS_CSI2_DPHY,
+       };
+       unsigned long link_freq_bitmap;
+       int ret;
+
+       endpoint =
+               fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
+                                               0, 0,
+                                               FWNODE_GRAPH_ENDPOINT_NEXT);
+       if (!endpoint)
+               return dev_err_probe(dev, -EPROBE_DEFER,
+                                    "waiting for fwnode graph
+ endpoint\n");
+
+       ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
+       fwnode_handle_put(endpoint);
+       if (ret)
+               return dev_err_probe(dev, ret,
+                                    "Failed to parse endpoint
+ node\n");

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+       if (bus_cfg.bus.mipi_csi2.num_data_lanes != ISX031_NUM_LANES) {
+               dev_err(dev, "Only %u data lanes are supported\n",
+                       ISX031_NUM_LANES);
+               ret = -EINVAL;
+               goto out_err;
+       }
+
+       /* Endpoints created for the sensor behind a GMSL link carry
+ no frequencies */

Doesn't the GMSL serializer need to be configured at any level to know what rate data is coming in, or have Sony really combined the serializer into the sensor such that there is no CSI bus?

Quoted 4 lines

The comment at the top of the file says "The sensor is controlled over I2C with the CCI helpers and outputs MIPI CSI-2.", so it feels a little odd that there are any references to GMSL in the driver at all.

Correct, the ISX031 is a plain CSI-2 sensor; on GMSL modules a separate serializer sits after it and gets the link rate from its CSI-2 endpoint. I'll drop all GMSL references.

Quoted 141 lines
Quoted 130 lines
drivers/media/i2c/isx031.c new file +130 -0
+       if (bus_cfg.nr_of_link_frequencies) {
+               ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies,
+                                              bus_cfg.nr_of_link_frequencies,
+                                              isx031_link_frequencies,
+                                              ARRAY_SIZE(isx031_link_frequencies),
+                                              &link_freq_bitmap);
+               if (ret)
+                       goto out_err;
+       }
+
+out_err:
+       v4l2_fwnode_endpoint_free(&bus_cfg);
+       return ret;
+}
+
+static const struct isx031_mode *
+isx031_state_mode(struct v4l2_subdev_state *state) {
+       const struct v4l2_mbus_framefmt *fmt;
+
+       fmt = v4l2_subdev_state_get_format(state, 0);
+
+       return v4l2_find_nearest_size(supported_modes,
+                                     ARRAY_SIZE(supported_modes),
+                                     width, height, fmt->width,
+fmt->height); }
+
+static int isx031_enable_streams(struct v4l2_subdev *subdev,
+                                struct v4l2_subdev_state *state,
+                                u32 pad, u64 streams_mask) {
+       struct isx031 *isx031 = to_isx031(subdev);
+       struct device *dev = isx031->dev;
+       const struct isx031_mode *mode = isx031_state_mode(state);
+       const struct isx031_reg_list *reg_list;
+       int ret;
+
+       ret = pm_runtime_resume_and_get(dev);
+       if (ret < 0)
+               return ret;
+
+       /* Mode registers must be reprogrammed in standby */
+       cci_write(isx031->regmap, ISX031_REG_MODE_SET_F_LOCK,
+                 ISX031_MODE_UNLOCK, &ret);
+       cci_write(isx031->regmap, ISX031_REG_MODE_SET_F,
+                 ISX031_MODE_STANDBY, &ret);
+       if (ret) {
+               dev_err(dev, "Failed to enter standby: %d\n", ret);
+               goto err_put;
+       }
+       msleep(ISX031_REG_SLEEP_50MS);
+
+       reg_list = &mode->reg_list;
+       cci_multi_reg_write(isx031->regmap, reg_list->regs,
+                           reg_list->num_of_regs, &ret);
+       if (isx031->frame_sync) {
+               reg_list = &isx031_framesync_stream_reg_list;
+               cci_multi_reg_write(isx031->regmap, reg_list->regs,
+                                   reg_list->num_of_regs, &ret);
+       }
+       if (ret) {
+               dev_err(dev, "Failed to set stream mode: %d\n", ret);
+               goto err_put;
+       }
+
+       ret = isx031_mode_transit(isx031, ISX031_STATE_STREAMING);
+       if (ret) {
+               dev_err(dev, "Failed to start streaming: %d\n", ret);
+               goto err_stop;
+       }
+
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL_UNIT,
+                 ISX031_FME_SHTVAL_UNIT_USEC, &ret);
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL_S1_UNIT,
+                 ISX031_FME_SHTVAL_UNIT_USEC, &ret);
+       if (ret) {
+               dev_err(dev, "Failed to set exposure unit: %d\n", ret);
+               goto err_stop;
+       }
+
+       /*
+        * The STREAMING transition resets the AE registers, so re-apply the
+        * cached control values once the sensor runs. This runs with the state
+        * lock held, which v4l2_subdev_enable_streams() also uses as the
+        * control handler lock, as required by __v4l2_ctrl_handler_setup().
+        */
+       ret = __v4l2_ctrl_handler_setup(&isx031->ctrls);
+       if (ret) {
+               dev_err(dev, "Failed to apply controls, stopping stream: %d\n",
+                       ret);
+               goto err_stop;
+       }
+
+       /* AEMODE must not be changed while streaming */
+       __v4l2_ctrl_grab(isx031->ctrl_exposure_auto, true);
+
+       return 0;
+
+err_stop:
+       isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+err_put:
+       pm_runtime_put(dev);
+
+       return ret;
+}
+
+static int isx031_disable_streams(struct v4l2_subdev *subdev,
+                                 struct v4l2_subdev_state *state,
+                                 u32 pad, u64 streams_mask) {
+       struct isx031 *isx031 = to_isx031(subdev);
+       int ret;
+
+       __v4l2_ctrl_grab(isx031->ctrl_exposure_auto, false);
+
+       ret = isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+       if (ret)
+               dev_err(isx031->dev, "Failed to stop streaming: %d\n",
+ ret);
+
+       pm_runtime_put(isx031->dev);
+
+       return 0;
+}
+
+static int isx031_power_off(struct device *dev) {
+       struct i2c_client *client = to_i2c_client(dev);
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+       struct isx031 *isx031 = to_isx031(sd);
+
+       /* Active low gpio reset, set 1 to power off sensor */
+       gpiod_set_value_cansleep(isx031->reset_gpio, 1);
+

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       return 0;
+}
+
+static int isx031_resume(struct device *dev) {
+       struct i2c_client *client = to_i2c_client(dev);
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+       struct isx031 *isx031 = to_isx031(sd);
+       int ret;
+
+       /* Active low; isx031_identify_module() polls until the sensor has

booted */

Quoted 9 lines

drivers/media/i2c/isx031.c line 551

Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+       gpiod_set_value_cansleep(isx031->reset_gpio, 0);
+
+       /* S4 clears the GPIO bias/config, so re-set fsin direction and level */
+       gpiod_direction_output(isx031->fsin_gpio, 0);
+
+       ret = isx031_identify_module(isx031);

Is there a need to identify the module on every resume?

No. Checking the module ID is only needed once, at probe, so I moved it there. The ID poll in isx031_resume() was also doing a second job: waiting for the sensor to boot after reset. So resume now waits on the sensor's state register instead.

Quoted 115 lines
Quoted 115 lines
drivers/media/i2c/isx031.c new file +115 -0
+       if (ret) {
+               dev_err(dev, "Failed to identify sensor module: %d\n", ret);
+               return ret;
+       }
+
+       return isx031_initialize_module(isx031); }
+
+static int isx031_get_frame_desc(struct v4l2_subdev *sd,
+                                unsigned int pad,
+                                struct v4l2_mbus_frame_desc *desc) {
+       struct v4l2_subdev_state *state;
+       const struct isx031_mode *mode;
+
+       state = v4l2_subdev_lock_and_get_active_state(sd);
+       mode = isx031_state_mode(state);
+       v4l2_subdev_unlock_state(state);
+
+       desc->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
+       desc->num_entries = 1;
+       desc->entry[0].pixelcode = mode->code;
+       desc->entry[0].stream = 0;
+       desc->entry[0].bus.csi2.vc = 0;
+       desc->entry[0].bus.csi2.dt = mode->datatype;
+       return 0;
+}
+
+static u32 isx031_exposure_max(u16 fps) {
+       return USEC_PER_SEC / fps;
+}
+
+/* Called with the control handler lock (sd->state_lock) held */
+static int isx031_update_exposure_range(struct isx031 *isx031,
+                                       const struct isx031_mode
+*mode) {
+       struct v4l2_ctrl *exposure = isx031->ctrl_exposure;
+       s64 max = isx031_exposure_max(mode->fps);
+
+       return __v4l2_ctrl_modify_range(exposure, exposure->minimum, max,
+                                       exposure->step,
+                                       min(exposure->default_value,
+max)); }
+
+static int isx031_set_format(struct v4l2_subdev *sd,
+                            const struct v4l2_subdev_client_info *ci,
+                            struct v4l2_subdev_state *sd_state,
+                            struct v4l2_subdev_format *fmt) {
+       struct isx031 *isx031 = to_isx031(sd);
+       const struct isx031_mode *mode;
+       struct v4l2_fract *interval;
+
+       /* The sensor keeps the old mode until the next stream start. */
+       if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+           media_entity_is_streaming(&sd->entity))
+               return -EBUSY;
+
+       mode = v4l2_find_nearest_size(supported_modes,
+                                     ARRAY_SIZE(supported_modes),
+                                     width, height,
+                                     fmt->format.width,
+ fmt->format.height);
+
+       isx031_update_pad_format(mode, &fmt->format);
+       *v4l2_subdev_state_get_format(sd_state, fmt->pad) =
+ fmt->format;
+
+       interval = v4l2_subdev_state_get_interval(sd_state, fmt->pad);
+       interval->numerator = 1;
+       interval->denominator = mode->fps;
+
+       if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
+               return isx031_update_exposure_range(isx031, mode);
+
+       return 0;
+}
+
+static int isx031_init_state(struct v4l2_subdev *sd,
+                            struct v4l2_subdev_state *sd_state) {
+       struct v4l2_fract *interval =
+v4l2_subdev_state_get_interval(sd_state, 0);
+
+       isx031_update_pad_format(&supported_modes[0],
+                                v4l2_subdev_state_get_format(sd_state, 0));
+       interval->numerator = 1;
+       interval->denominator = supported_modes[0].fps;
+
+       return 0;
+}
+
+static int isx031_enum_mbus_code(struct v4l2_subdev *sd,
+                                struct v4l2_subdev_state *sd_state,
+                                struct v4l2_subdev_mbus_code_enum
+*code) {
+       /* All modes share a single media-bus code */
+       if (code->index > 0)
+               return -EINVAL;
+
+       code->code = supported_modes[0].code;
+
+       return 0;
+}
+
+static int isx031_enum_frame_size(struct v4l2_subdev *sd,
+                                 struct v4l2_subdev_state *sd_state,
+                                 struct v4l2_subdev_frame_size_enum
+*fse) {
+       unsigned int i, count = 0;
+
+       for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
+               if (supported_modes[i].code != fse->code)
+                       continue;
+
+               /* Skip fps variants that repeat an already-listed resolution */
+               if (i > 0 &&
+                   supported_modes[i].code == supported_modes[i - 1].code &&
+                   supported_modes[i].width == supported_modes[i - 1].width &&
Quoted 9 lines

drivers/media/i2c/isx031.c line 658

Quoted 2 lines
drivers/media/i2c/isx031.c new file +2 -0
+                   supported_modes[i].height == supported_modes[i -
+ 1].height)

There aren't any modes that are repeats, and you've stated earlier that
* Crop settings are frame-rate independent; the frame rate is selected
* separately through ISX031_REG_MODE_SELECT."
so why are the mode tables (if they remain) including a frame rate in the first place?

Dropped modes.

Quoted 208 lines
Quoted 76 lines
drivers/media/i2c/isx031.c new file +76 -0
+                       continue;
+
+               if (count == fse->index) {
+                       fse->min_width = supported_modes[i].width;
+                       fse->max_width = supported_modes[i].width;
+                       fse->min_height = supported_modes[i].height;
+                       fse->max_height = supported_modes[i].height;
+                       return 0;
+               }
+               count++;
+       }
+
+       return -EINVAL;
+}
+
+static int isx031_enum_frame_interval(struct v4l2_subdev *sd,
+                                     struct v4l2_subdev_state *sd_state,
+                                     struct
+v4l2_subdev_frame_interval_enum *fie) {
+       unsigned int i, count = 0;
+
+       for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
+               if (supported_modes[i].code != fie->code ||
+                   supported_modes[i].width != fie->width ||
+                   supported_modes[i].height != fie->height)
+                       continue;
+
+               if (count == fie->index) {
+                       fie->interval.numerator = 1;
+                       fie->interval.denominator = supported_modes[i].fps;
+                       return 0;
+               }
+               count++;
+       }
+
+       return -EINVAL;
+}
+
+/* Each resolution has a single fixed frame rate, set along with the
+format */ static int isx031_set_frame_interval(struct v4l2_subdev *sd,
+                                    struct v4l2_subdev_state *sd_state,
+                                    struct v4l2_subdev_frame_interval
+*fi) {
+       fi->interval = *v4l2_subdev_state_get_interval(sd_state,
+fi->pad);
+
+       return 0;
+}
+
+static const struct v4l2_subdev_video_ops isx031_video_ops = {
+       .s_stream = v4l2_subdev_s_stream_helper, };
+
+static const struct v4l2_subdev_pad_ops isx031_pad_ops = {
+       .set_fmt = isx031_set_format,
+       .get_fmt = v4l2_subdev_get_fmt,
+       .get_frame_desc = isx031_get_frame_desc,
+       .enable_streams = isx031_enable_streams,
+       .disable_streams = isx031_disable_streams,
+       .enum_mbus_code = isx031_enum_mbus_code,
+       .enum_frame_size = isx031_enum_frame_size,
+       .enum_frame_interval = isx031_enum_frame_interval,
+       .get_frame_interval = v4l2_subdev_get_frame_interval,
+       .set_frame_interval = isx031_set_frame_interval, };
+
+static const struct v4l2_subdev_ops isx031_subdev_ops = {
+       .video = &isx031_video_ops,
+       .pad = &isx031_pad_ops,
+};
+
+static const struct v4l2_subdev_internal_ops isx031_internal_ops = {
+       .init_state = isx031_init_state, };
+
+static int isx031_set_exposure_value(struct isx031 *isx031, u32 val)
+{
+       int ret = 0;
+

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL, val, &ret);
+       cci_write(isx031->regmap, ISX031_REG_FME_SHTVAL_S1, val,
+ &ret);
+
+       return ret;
+}
+
+static int isx031_apply_ctrls(struct isx031 *isx031) {
+       struct device *dev = isx031->dev;
+       bool manual = isx031->ctrl_exposure_auto->val ==

V4L2_EXPOSURE_MANUAL;

Quoted 16 lines
drivers/media/i2c/isx031.c new file +16 -0
+       u32 val;
+       int ret = 0;
+
+       val = manual ? ISX031_AEMODE_FULL_ME : ISX031_AEMODE_AUTO;
+
+       cci_write(isx031->regmap, ISX031_REG_AEMODE, val, &ret);
+       if (ret) {
+               dev_err(dev, "Failed to write AEMODE=0x%02x: %d\n", val, ret);
+               return ret;
+       }
+
+       if (manual)
+               return isx031_set_exposure_value(isx031,
+
+ isx031->ctrl_exposure->val);
+

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       return 0;
+}
+
+static int isx031_set_ctrl(struct v4l2_ctrl *ctrl) {
+       struct isx031 *isx031 = container_of(ctrl->handler,
+                                            struct isx031, ctrls);
+       struct device *dev = isx031->dev;
+       int ret;
+
+       /* Cached values are applied by __v4l2_ctrl_handler_setup() at stream

start */

Quoted 20 lines
drivers/media/i2c/isx031.c new file +20 -0
+       ret = pm_runtime_get_if_in_use(dev);
+       if (ret <= 0)
+               return 0;
+
+       ret = isx031_apply_ctrls(isx031);
+
+       pm_runtime_put(dev);
+
+       return ret;
+}
+
+static int isx031_get_ctrl(struct v4l2_ctrl *ctrl) {
+       struct isx031 *isx031 = container_of(ctrl->handler,
+                                            struct isx031, ctrls);
+       struct device *dev = isx031->dev;
+       struct v4l2_ctrl *exposure;
+       u64 val;
+       int ret;
+
+       /* If not powered up, the framework falls back to cur.val */

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       ret = pm_runtime_get_if_in_use(dev);
+       if (ret <= 0)
+               return 0;
+
+       /*
+        * FME_SHTVAL only applies in Full ME mode and keeps the last manual
+        * value under AE, so report the exposure the sensor actually applies.
+        * This is called on the cluster master, so update the exposure control.
+        */
+       ret = cci_read(isx031->regmap, ISX031_REG_SHT_TIME_SP1, &val,

NULL);

Quoted 41 lines
drivers/media/i2c/isx031.c new file +41 -0
+       if (ret) {
+               dev_err(dev, "Failed to read SHT_TIME_SP1: %d\n", ret);
+               goto out;
+       }
+       exposure = isx031->ctrl_exposure;
+       exposure->val = clamp_t(s64, val * ISX031_SHT_TIME_SP1_UNIT_USEC,
+                               exposure->minimum, exposure->maximum);
+
+out:
+       pm_runtime_put(dev);
+
+       return ret;
+}
+
+static const struct v4l2_ctrl_ops isx031_ctrl_ops = {
+       .s_ctrl = isx031_set_ctrl,
+       .g_volatile_ctrl = isx031_get_ctrl, };
+
+static int isx031_ctrls_init(struct isx031 *sensor) {
+       struct v4l2_fwnode_device_properties props;
+       struct v4l2_ctrl *link_freq;
+       struct v4l2_ctrl_handler *hdl = &sensor->ctrls;
+       int ret;
+
+       ret = v4l2_fwnode_device_parse(sensor->dev, &props);
+       if (ret)
+               return ret;
+
+       v4l2_ctrl_handler_init(hdl, 5);
+
+       /* There's a need to set the link frequency because IPU6 dictates it. */
+       link_freq = v4l2_ctrl_new_int_menu(hdl, &isx031_ctrl_ops,
+                                          V4L2_CID_LINK_FREQ,
+                                          ARRAY_SIZE(isx031_link_frequencies) - 1, 0,
+                                          isx031_link_frequencies);
+
+       sensor->ctrl_exposure_auto =
+               v4l2_ctrl_new_std_menu(hdl, &isx031_ctrl_ops,
+                                      V4L2_CID_EXPOSURE_AUTO,
+                                      V4L2_EXPOSURE_MANUAL, 0,
+                                      V4L2_EXPOSURE_AUTO);

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+       sensor->ctrl_exposure =
+               v4l2_ctrl_new_std(hdl, &isx031_ctrl_ops, V4L2_CID_EXPOSURE,
+                                 ISX031_EXPOSURE_MIN,
+                                 isx031_exposure_max(supported_modes[0].fps),
+                                 ISX031_EXPOSURE_STEP,
+ ISX031_EXPOSURE_DEF);
+
+       /* Expose the orientation/rotation properties of the module. */
+       ret = v4l2_ctrl_new_fwnode_properties(hdl, &isx031_ctrl_ops,

&props);

drivers/media/i2c/isx031.c

Quoted 7 lines
drivers/media/i2c/isx031.c new file +7 -0
+       if (ret) {
+               v4l2_ctrl_handler_free(hdl);
+               return ret;
+       }
+
+       link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+       sensor->ctrl_exposure_auto->flags |=

V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;

drivers/media/i2c/isx031.c line 860

Quoted 2 lines
drivers/media/i2c/isx031.c new file +2 -0
+       sensor->ctrl_exposure->flags |=
+ V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
Quoted 7 lines

There's no check that these three controls were created successfully and hence not NULL before the flags are set.
Amend the
if (ret)
following v4l2_ctrl_new_fwnode_properties() to if (ret || ctrl_hdlr->error) would cover that.

v4l2_ctrl_new_fwnode_properties() returns hdl->error straight away if an earlier control failed, so ret is already non-zero in that case and we bail out before touching ->flags.
I'll switch to checking hdl->error directly to make that obvious.

Quoted 35 lines
Quoted 35 lines
drivers/media/i2c/isx031.c new file +35 -0
+
+       /*
+        * Exposure is read back from the sensor in auto mode, so it has to be
+        * part of an auto cluster: that makes the core mark it volatile (and
+        * inactive) while auto exposure is selected.
+        */
+       v4l2_ctrl_auto_cluster(2, &sensor->ctrl_exposure_auto,
+                              V4L2_EXPOSURE_MANUAL, true);
+
+       sensor->sd.ctrl_handler = hdl;
+
+       return 0;
+}
+
+static void isx031_remove(struct i2c_client *client) {
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+       struct isx031 *isx031 = to_isx031(sd);
+
+       v4l2_async_unregister_subdev(sd);
+       v4l2_subdev_cleanup(sd);
+       media_entity_cleanup(&sd->entity);
+       v4l2_ctrl_handler_free(&isx031->ctrls);
+
+       pm_runtime_disable(&client->dev);
+       if (!pm_runtime_status_suspended(&client->dev)) {
+               isx031_power_off(&client->dev);
+               pm_runtime_set_suspended(&client->dev);
+       }
+}
+
+static int isx031_probe(struct i2c_client *client) {
+       struct device *dev = &client->dev;
+       struct v4l2_subdev *sd;
+       struct isx031 *isx031;
+       unsigned long freq;
Quoted 9 lines

drivers/media/i2c/isx031.c

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+       struct clk *clk;
+       int ret;
+
+       isx031 = devm_kzalloc(&client->dev, sizeof(*isx031),
+ GFP_KERNEL);

There's just been a tree-wide conversion to devm_kzalloc_obj. This should follow suit.

Done

Quoted 80 lines
Quoted 18 lines
drivers/media/i2c/isx031.c new file +18 -0
+       if (!isx031)
+               return -ENOMEM;
+
+       isx031->dev = dev;
+
+       ret = isx031_parse_endpoint(dev);
+       if (ret)
+               return ret;
+
+       clk = devm_v4l2_sensor_clk_get(dev, NULL);
+       if (IS_ERR(clk))
+               return dev_err_probe(dev, PTR_ERR(clk), "Failed to get
+ clock\n");
+
+       freq = clk_get_rate(clk);
+       if (freq != ISX031_INCK_FREQ)
+               return dev_err_probe(dev, -EINVAL,
+                                    "External clock %lu Hz not supported\n",

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+                                    freq);
+
+       isx031->reset_gpio = devm_gpiod_get_optional(dev, "reset",
+                                                    GPIOD_OUT_HIGH);
+       if (IS_ERR(isx031->reset_gpio))
+               return dev_err_probe(dev, PTR_ERR(isx031->reset_gpio),
+                                    "Failed to get reset gpio\n");
+
+       /*
+        * With external frame sync, FSIN arrives over GMSL, so the line must

not

Quoted 51 lines
drivers/media/i2c/isx031.c new file +51 -0
+        * be claimed and driven locally.
+        */
+       if (!isx031->frame_sync) {
+               isx031->fsin_gpio = devm_gpiod_get_optional(dev, "fsin",
+                                                           GPIOD_OUT_LOW);
+               if (IS_ERR(isx031->fsin_gpio))
+                       return dev_err_probe(dev, PTR_ERR(isx031->fsin_gpio),
+                                            "Failed to get fsin gpio\n");
+       }
+
+       isx031->regmap = devm_cci_regmap_init_i2c(client, 16);
+       if (IS_ERR(isx031->regmap))
+               return dev_err_probe(dev, PTR_ERR(isx031->regmap),
+                                    "Failed to init regmap\n");
+
+       /* Initialize subdevice */
+       sd = &isx031->sd;
+       v4l2_i2c_subdev_init(sd, client, &isx031_subdev_ops);
+
+       /* The sensor may not be in reset at this point, so reset it explicitly */
+       isx031_power_off(dev);
+       if (isx031->reset_gpio)
+               msleep(ISX031_REG_SLEEP_50MS);
+
+       ret = isx031_resume(dev);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to power on sensor\n");
+               goto err_power_off;
+       }
+
+       ret = isx031_ctrls_init(isx031);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to init sensor ctrls\n");
+               goto err_power_off;
+       }
+
+       sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+       sd->internal_ops = &isx031_internal_ops;
+       sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
+
+       /* Initialize subdev media pad */
+       isx031->pad.flags = MEDIA_PAD_FL_SOURCE;
+       ret = media_entity_pads_init(&sd->entity, 1, &isx031->pad);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to init entity pads\n");
+               goto err_ctrl_free;
+       }
+
+       isx031->sd.state_lock = isx031->sd.ctrl_handler->lock;
+       ret = v4l2_subdev_init_finalize(&isx031->sd);
+       if (ret) {
Quoted 9 lines

drivers/media/i2c/isx031.c line 999

Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+               dev_err_probe(dev, ret, "Failed to finalize V4L2 subdev\n");
+               goto err_media_cleanup;
+       }
+
+       pm_runtime_set_active(dev);
+       pm_runtime_enable(dev);

pm_runtime_use_autosuspend?

Changed to pm_runtime_use_autosuspend()

Quoted 27 lines
Quoted 16 lines
drivers/media/i2c/isx031.c new file +16 -0
+
+       ret = v4l2_async_register_subdev_sensor(&isx031->sd);
+       if (ret) {
+               dev_err_probe(dev, ret, "Failed to register V4L2 subdev\n");
+               goto err_pm_disable;
+       }
+
+       pm_runtime_idle(dev);
+
+       return 0;
+
+err_pm_disable:
+       pm_runtime_disable(dev);
+       pm_runtime_set_suspended(dev);
+       v4l2_subdev_cleanup(&isx031->sd);
+err_media_cleanup:

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+       media_entity_cleanup(&isx031->sd.entity);
+err_ctrl_free:
+       v4l2_ctrl_handler_free(&isx031->ctrls);
+err_power_off:
+       isx031_power_off(dev);
+
+       return ret;
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(isx031_pm_ops,

isx031_power_off,

Quoted 7 lines

drivers/media/i2c/isx031.c line 1026

Quoted 1 line
drivers/media/i2c/isx031.c new file +1 -0
+                                isx031_resume, NULL);

More commonly these would be named _power_off and _power_on, or _suspend and _resume. One of each feels odd.

Dave

Agreed. The runtime PM callbacks are now isx031_suspend()/isx031_resume(), which wrap the isx031_power_off()/isx031_power_on() helpers

Quoted 22 lines
Quoted 11 lines
drivers/media/i2c/isx031.c new file +11 -0
+
+static const struct acpi_device_id isx031_acpi_ids[] = {
+       { "INTC113C" },
+       {}
+};
+
+MODULE_DEVICE_TABLE(acpi, isx031_acpi_ids);
+
+static struct i2c_driver isx031_i2c_driver = {
+       .driver = {
+               .name = "isx031",

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+               .acpi_match_table = isx031_acpi_ids,
+               .pm = pm_sleep_ptr(&isx031_pm_ops),
+       },
+       .probe = isx031_probe,
+       .remove = isx031_remove,
+};
+
+module_i2c_driver(isx031_i2c_driver);
+
+MODULE_DESCRIPTION("isx031 sensor driver"); MODULE_AUTHOR("Hao

Yao

Quoted 9 lines
Quoted 9 lines
drivers/media/i2c/isx031.c new file +5 -1
+<[email protected]>"); MODULE_AUTHOR("Jonathan Lui
+<[email protected]>");
+MODULE_AUTHOR("Wei Khang, Goh <[email protected]>");
+MODULE_AUTHOR("Yew Chang Ching <[email protected]>");
+MODULE_LICENSE("GPL");
--

2.43.0

Best Regards
Vunny

#5 lore raw

Re: [PATCH v4 2/2] media: i2c: add isx031 sensor driver

Hi Vunny, Dave,

Quoted 22 lines · Vunny
Quoted 14 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       /* Expose the orientation/rotation properties of the module. */
+       ret = v4l2_ctrl_new_fwnode_properties(hdl, &isx031_ctrl_ops,

&props);

drivers/media/i2c/isx031.c

Quoted 7 lines
drivers/media/i2c/isx031.c new file +7 -0
+       if (ret) {
+               v4l2_ctrl_handler_free(hdl);
+               return ret;
+       }
+
+       link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+       sensor->ctrl_exposure_auto->flags |=

V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;

Quoted 2 lines

+ sensor->ctrl_exposure->flags |=
+ V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;

Quoted 7 lines

There's no check that these three controls were created successfully and hence not NULL before the flags are set.
Amend the
if (ret)
following v4l2_ctrl_new_fwnode_properties() to if (ret || ctrl_hdlr->error) would cover that.

Quoted 3 lines

v4l2_ctrl_new_fwnode_properties() returns hdl->error straight away if an earlier control failed, so ret is already non-zero in that case and we bail out before touching ->flags.
I'll switch to checking hdl->error directly to make that obvious.

Please don't. The functions operating on control handler return the handler's error, including v4l2_ctrl_new_fwnode_properties().

Signature · 3 lines
Regards,

Sakari Ailus