Skip to contentSkip to conversation

media: Add ISX031 sensor support

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

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 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 v4:
- Replace fixed crop register tables with crop-register programming.
- Remove the remaining external frame-sync code.
- Simplify the sensor driver in response to review comments.

Changes since v3:
- 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.

Changes since v2:
- Add ISX031 to the media sensor MAINTAINERS file pattern.
- Simplify mode selection for the supported 4-lane, 30-fps configuration.
- Read the OTP type-name register as little-endian 16-bit data and mask it.
- Update exposure handling and co-development/review trailers.

Changes since v1:
- Update the sensor driver, including OTP register access, exposure limits and mode state handling.
- Update the sensor driver attribution and review trailers.

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           | 953 +++++++++++++++++++++++++++
 drivers/media/pci/intel/ipu-bridge.c |   2 +
 5 files changed, 974 insertions(+)
 create mode 100644 drivers/media/i2c/isx031.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 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 v5 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 | 953 +++++++++++++++++++++++++++++++++++++
 4 files changed, 972 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 +953 -0

953 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

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

Hi Vunny,

A few more comments below...

Quoted 350 lines · On Thu, Dec 03, 2026 at 01:29:44PM +0800, [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 | 953 +++++++++++++++++++++++++++++++++++++
 4 files changed, 972 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 +250 -0
new file mode 100644
index 000000000000..1d5fef203486
@@ -0,0 +1,953 @@
+// 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_PIXEL_ARRAY_WIDTH	1920
+#define ISX031_PIXEL_ARRAY_HEIGHT	1536
+#define ISX031_MIN_WIDTH		64
+#define ISX031_MIN_HEIGHT		64
+
+#define ISX031_MBUS_CODE		MEDIA_BUS_FMT_UYVY8_1X16
+#define ISX031_FPS			30
+
+/* Crop sizes and offsets must be multiples of two */
+#define ISX031_REG_DCROP_ON		CCI_REG8(0x8AA8)
+#define ISX031_REG_DCROP_HSIZE		CCI_REG16_LE(0x8AAA)
+#define ISX031_REG_DCROP_HOFFSET	CCI_REG16_LE(0x8AAC)
+#define ISX031_REG_DCROP_VSIZE		CCI_REG16_LE(0x8AAE)
+#define ISX031_REG_DCROP_VOFFSET	CCI_REG16_LE(0x8AB0)
+#define ISX031_REG_DCROP_DATA_SEL	CCI_REG8(0x8ADA)
+#define ISX031_DCROP_DATA_SEL		0x03
+
+/* Compared against the DCROP_* registers by the Application Lock function */
+#define ISX031_REG_DCROP_ON_APL		CCI_REG8(0xBF04)
+#define ISX031_REG_DCROP_HSIZE_APL	CCI_REG16_LE(0xBF06)
+#define ISX031_REG_DCROP_HOFFSET_APL	CCI_REG16_LE(0xBF08)
+#define ISX031_REG_DCROP_VSIZE_APL	CCI_REG16_LE(0xBF0A)
+#define ISX031_REG_DCROP_VOFFSET_APL	CCI_REG16_LE(0xBF0C)
+
+#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_MAX		(USEC_PER_SEC / ISX031_FPS)
+#define ISX031_EXPOSURE_STEP		1
+#define ISX031_EXPOSURE_DEF		ISX031_EXPOSURE_MIN
+
+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;
+};
+
+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 int isx031_set_crop(struct isx031 *isx031, const struct v4l2_rect *crop)
+{
+	struct regmap *regmap = isx031->regmap;
+	int ret = 0;
+
+	cci_write(regmap, ISX031_REG_DCROP_ON, 1, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HSIZE, crop->width, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HOFFSET, crop->left, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VSIZE, crop->height, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VOFFSET, crop->top, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_DATA_SEL, ISX031_DCROP_DATA_SEL,
+		  &ret);
+	cci_write(regmap, ISX031_REG_DCROP_ON_APL, 1, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HSIZE_APL, crop->width, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HOFFSET_APL, crop->left, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VSIZE_APL, crop->height, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VOFFSET_APL, crop->top, &ret);
+
+	return ret;
+}
+
+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);
+	/* The state register can lag the actual transition, so only warn */
+	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_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(struct v4l2_mbus_framefmt *fmt,
+				     u32 width, u32 height)
+{
+	fmt->width = width;
+	fmt->height = height;
+	fmt->code = ISX031_MBUS_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,
+	};

drivers/media/i2c/isx031.c line 260

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	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");

Please remove this check, it's redundant.

Quoted 149 lines
drivers/media/i2c/isx031.c new file +149 -0
+
+	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");
+
+	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;
+	}
+
+	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);
+
+out_err:
+	v4l2_fwnode_endpoint_free(&bus_cfg);
+	return ret;
+}
+
+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 v4l2_rect *crop = v4l2_subdev_state_get_crop(state, 0);
+	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);
+
+	ret = isx031_set_crop(isx031, crop);
+	if (ret) {
+		dev_err(dev, "Failed to set crop: %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_autosuspend(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_autosuspend(isx031->dev);
+
+	return 0;
+}
+
+static void isx031_power_off(struct isx031 *isx031)
+{
+	/* Without a reset line, standby is the only way to stop the sensor */
+	if (!isx031->reset_gpio)
+		isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+
+	/* Active low gpio reset, set 1 to power off sensor */
+	gpiod_set_value_cansleep(isx031->reset_gpio, 1);
+}
+
+static int isx031_power_on(struct isx031 *isx031)
+{
+	u64 val = 0;
+	int read_ret;
+	int ret;
+
+	/* Active low gpio reset */
+	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);
+
+	/* Without a reset line the sensor was never reset, so has nothing to boot */
+	if (!isx031->reset_gpio)
+		return 0;
+
+	/* After reset the sensor first NAKs, then reads 0xffff until OTP is loaded */
+	ret = read_poll_timeout(cci_read, read_ret,
+				!read_ret && val != 0xffff,
+				10 * USEC_PER_MSEC,
+				ISX031_READ_REG_RETRY_TIMEOUT * 10 * USEC_PER_MSEC,
+				false, isx031->regmap,
+				ISX031_OTP_TYPE_NAME, &val, NULL);
+	if (ret)
+		dev_err(isx031->dev, "Sensor did not boot: OTP 0x%04llx\n", val);

drivers/media/i2c/isx031.c line 419

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+	return ret;
+}
+
+static int isx031_suspend(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+
+	isx031_power_off(to_isx031(sd));

Please refactor this so you have a single set of suspend / resume callbacks.

Quoted 7 lines
drivers/media/i2c/isx031.c new file +7 -0
+
+	return 0;
+}
+
+static int isx031_resume(struct device *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);

drivers/media/i2c/isx031.c line 436

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct isx031 *isx031 = to_isx031(sd);
+	int ret;
+
+	ret = isx031_power_on(isx031);
+	if (ret)
+		return ret;
+
+	return cci_multi_reg_write(isx031->regmap, isx031_init_reg,
+				   ARRAY_SIZE(isx031_init_reg), NULL);

Are there cases where the init register sequence doesn't need to be written to the sensor?

Quoted 228 lines
drivers/media/i2c/isx031.c new file +228 -0
+}
+
+static int isx031_get_frame_desc(struct v4l2_subdev *sd,
+				 unsigned int pad,
+				 struct v4l2_mbus_frame_desc *desc)
+{
+	desc->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
+	desc->num_entries = 1;
+	desc->entry[0].pixelcode = ISX031_MBUS_CODE;
+	desc->entry[0].stream = 0;
+	desc->entry[0].bus.csi2.vc = 0;
+	desc->entry[0].bus.csi2.dt = MIPI_CSI2_DT_YUV422_8B;
+	return 0;
+}
+
+/* The output is the crop rectangle unscaled; the sensor has no scaler */
+static void isx031_update_crop(struct v4l2_subdev_state *sd_state, u32 pad,
+			       const struct v4l2_rect *crop)
+{
+	*v4l2_subdev_state_get_crop(sd_state, pad) = *crop;
+	isx031_update_pad_format(v4l2_subdev_state_get_format(sd_state, pad),
+				 crop->width, crop->height);
+}
+
+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 v4l2_rect crop;
+
+	/* The sensor keeps the old crop until the next stream start. */
+	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+	    media_entity_is_streaming(&sd->entity))
+		return -EBUSY;
+
+	crop.width = clamp_t(u32, round_down(fmt->format.width, 2),
+			     ISX031_MIN_WIDTH, ISX031_PIXEL_ARRAY_WIDTH);
+	crop.height = clamp_t(u32, round_down(fmt->format.height, 2),
+			      ISX031_MIN_HEIGHT, ISX031_PIXEL_ARRAY_HEIGHT);
+	crop.left = round_down((ISX031_PIXEL_ARRAY_WIDTH - crop.width) / 2, 2);
+	crop.top = round_down((ISX031_PIXEL_ARRAY_HEIGHT - crop.height) / 2, 2);
+
+	isx031_update_crop(sd_state, fmt->pad, &crop);
+	fmt->format = *v4l2_subdev_state_get_format(sd_state, fmt->pad);
+
+	return 0;
+}
+
+static int isx031_get_selection(struct v4l2_subdev *sd,
+				const struct v4l2_subdev_client_info *ci,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel)
+{
+	switch (sel->target) {
+	case V4L2_SEL_TGT_CROP:
+		sel->r = *v4l2_subdev_state_get_crop(sd_state, sel->pad);
+		return 0;
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.left = 0;
+		sel->r.top = 0;
+		sel->r.width = ISX031_PIXEL_ARRAY_WIDTH;
+		sel->r.height = ISX031_PIXEL_ARRAY_HEIGHT;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int isx031_set_selection(struct v4l2_subdev *sd,
+				const struct v4l2_subdev_client_info *ci,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct v4l2_rect crop;
+
+	if (sel->target != V4L2_SEL_TGT_CROP)
+		return -EINVAL;
+
+	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+	    media_entity_is_streaming(&sd->entity))
+		return -EBUSY;
+
+	crop.width = clamp_t(u32, round_down(sel->r.width, 2),
+			     ISX031_MIN_WIDTH, ISX031_PIXEL_ARRAY_WIDTH);
+	crop.height = clamp_t(u32, round_down(sel->r.height, 2),
+			      ISX031_MIN_HEIGHT, ISX031_PIXEL_ARRAY_HEIGHT);
+	crop.left = clamp_t(s32, round_down(sel->r.left, 2), 0,
+			    ISX031_PIXEL_ARRAY_WIDTH - crop.width);
+	crop.top = clamp_t(s32, round_down(sel->r.top, 2), 0,
+			   ISX031_PIXEL_ARRAY_HEIGHT - crop.height);
+
+	isx031_update_crop(sd_state, sel->pad, &crop);
+	sel->r = crop;
+
+	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);
+	const struct v4l2_rect crop = {
+		.width = ISX031_PIXEL_ARRAY_WIDTH,
+		.height = ISX031_PIXEL_ARRAY_HEIGHT,
+	};
+
+	isx031_update_crop(sd_state, 0, &crop);
+	interval->numerator = 1;
+	interval->denominator = ISX031_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)
+{
+	if (code->index > 0)
+		return -EINVAL;
+
+	code->code = ISX031_MBUS_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)
+{
+	if (fse->index > 0 || fse->code != ISX031_MBUS_CODE)
+		return -EINVAL;
+
+	fse->min_width = ISX031_MIN_WIDTH;
+	fse->max_width = ISX031_PIXEL_ARRAY_WIDTH;
+	fse->min_height = ISX031_MIN_HEIGHT;
+	fse->max_height = ISX031_PIXEL_ARRAY_HEIGHT;
+
+	return 0;
+}
+
+static int isx031_enum_frame_interval(struct v4l2_subdev *sd,
+				      struct v4l2_subdev_state *sd_state,
+				      struct v4l2_subdev_frame_interval_enum *fie)
+{
+	if (fie->index > 0 || fie->code != ISX031_MBUS_CODE ||
+	    fie->width < ISX031_MIN_WIDTH ||
+	    fie->width > ISX031_PIXEL_ARRAY_WIDTH ||
+	    fie->height < ISX031_MIN_HEIGHT ||
+	    fie->height > ISX031_PIXEL_ARRAY_HEIGHT)
+		return -EINVAL;
+
+	fie->interval.numerator = 1;
+	fie->interval.denominator = ISX031_FPS;
+
+	return 0;
+}
+
+/* The frame rate is fixed regardless of the crop */
+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_selection = isx031_get_selection,
+	.set_selection = isx031_set_selection,
+	.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;

drivers/media/i2c/isx031.c line 674

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -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 */

I'd say this comment is redundant.

drivers/media/i2c/isx031.c line 679

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+	ret = pm_runtime_get_if_in_use(dev);
+	if (ret <= 0)
+		return 0;
+
+	ret = isx031_apply_ctrls(isx031);

Can you move the code from apply_ctrls() above here and do the writes based on the ctrl->id here, as other drivers do?

Quoted 22 lines
drivers/media/i2c/isx031.c new file +22 -0
+
+	pm_runtime_put_autosuspend(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

drivers/media/i2c/isx031.c line 711

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	 * 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,

ctrl->id is presumably exposure already. Worth checking perhaps still?

Quoted 40 lines
drivers/media/i2c/isx031.c new file +40 -0
+				exposure->minimum, exposure->maximum);
+
+out:
+	pm_runtime_put_autosuspend(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,

drivers/media/i2c/isx031.c line 761

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+				  ISX031_EXPOSURE_MIN,
+				  ISX031_EXPOSURE_MAX,
+				  ISX031_EXPOSURE_STEP, ISX031_EXPOSURE_DEF);
+
+	/* Expose the orientation/rotation properties of the module. */
+	v4l2_ctrl_new_fwnode_properties(hdl, &isx031_ctrl_ops, &props);
+	if (hdl->error) {
+		ret = hdl->error;
+		v4l2_ctrl_handler_free(hdl);
+		return ret;

I'm afraid this was better in v3.

Quoted 21 lines
drivers/media/i2c/isx031.c new file +21 -0
+	}
+
+	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;
+
+	/*
+	 * 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)
+{

drivers/media/i2c/isx031.c line 792

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	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);
+	pm_runtime_dont_use_autosuspend(&client->dev);

You can drop this actually. The autosuspend is re-initialised now before probe.

Quoted 35 lines
drivers/media/i2c/isx031.c new file +35 -0
+	if (!pm_runtime_status_suspended(&client->dev)) {
+		isx031_power_off(isx031);
+		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;
+	struct clk *clk;
+	int ret;
+
+	isx031 = devm_kzalloc_obj(dev, *isx031);
+	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);
+

drivers/media/i2c/isx031.c line 837

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	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");
+
+	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");

Can you drop all references to the fsin gpio? The driver now sets it output at low level but does nothing else with it.

drivers/media/i2c/isx031.c line 845

Quoted 8 lines
drivers/media/i2c/isx031.c new file +8 -0
+
+	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;

I'd just use &isx031->sd where you need it. If you prefer to keep sd shorthand, then use it consistently.

drivers/media/i2c/isx031.c line 848

Quoted 3 lines
drivers/media/i2c/isx031.c new file +3 -0
+	v4l2_i2c_subdev_init(sd, client, &isx031_subdev_ops);
+
+	/* The sensor may not be in reset at this point, so reset it explicitly */

The reset GPIO is already set high and presumably the sensor isn't streaming. If you want to support operation without reset GPIO, then I'd be looking at software reset. Probably DT support will be added at some point so it'd be good to keep these calls balanced, too.

Quoted 105 lines
drivers/media/i2c/isx031.c new file +105 -0
+	isx031_power_off(isx031);
+	if (isx031->reset_gpio)
+		msleep(ISX031_REG_SLEEP_50MS);
+
+	ret = isx031_power_on(isx031);
+	if (ret) {
+		dev_err_probe(dev, ret, "Failed to power on sensor\n");
+		goto err_power_off;
+	}
+
+	ret = isx031_identify_module(isx031);
+	if (ret)
+		goto err_power_off;
+
+	ret = cci_multi_reg_write(isx031->regmap, isx031_init_reg,
+				  ARRAY_SIZE(isx031_init_reg), NULL);
+	if (ret) {
+		dev_err_probe(dev, ret, "Failed to initialize 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) {
+		dev_err_probe(dev, ret, "Failed to finalize V4L2 subdev\n");
+		goto err_media_cleanup;
+	}
+
+	pm_runtime_set_active(dev);
+	pm_runtime_enable(dev);
+	/* Booting the sensor out of reset is slow, so avoid it between streams */
+	pm_runtime_set_autosuspend_delay(dev, 1000);
+	pm_runtime_use_autosuspend(dev);
+
+	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_dont_use_autosuspend(dev);
+	pm_runtime_set_suspended(dev);
+	v4l2_subdev_cleanup(&isx031->sd);
+err_media_cleanup:
+	media_entity_cleanup(&isx031->sd.entity);
+err_ctrl_free:
+	v4l2_ctrl_handler_free(&isx031->ctrls);
+err_power_off:
+	isx031_power_off(isx031);
+
+	return ret;
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(isx031_pm_ops, isx031_suspend,
+				 isx031_resume, NULL);
+
+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");
Signature · 3 lines
Regards,

Sakari Ailus
#4 lore raw

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

Hi Sakari,

Quoted 375 lines

From: Sakari Ailus <[email protected]>
Sent: Thursday, 3 December, 2026 6:15 PM
To: Sodhi, Vunny <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v5 2/2] media: i2c: add isx031 sensor driver

Hi Vunny,

A few more comments below...

Quoted 150 lines · On Thu, Dec 03, 2026 at 01:29:44PM +0800, [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 | 953
+++++++++++++++++++++++++++++++++++++
 4 files changed, 972 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..1d5fef203486

drivers/media/i2c/isx031.c new file +50 -0
@@ -0,0 +1,953 @@
+// 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_PIXEL_ARRAY_WIDTH	1920
+#define ISX031_PIXEL_ARRAY_HEIGHT	1536
+#define ISX031_MIN_WIDTH		64
+#define ISX031_MIN_HEIGHT		64
+
+#define ISX031_MBUS_CODE		MEDIA_BUS_FMT_UYVY8_1X16
+#define ISX031_FPS			30
+

drivers/media/i2c/isx031.c line 60

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+/* Crop sizes and offsets must be multiples of two */
+#define ISX031_REG_DCROP_ON		CCI_REG8(0x8AA8)
+#define ISX031_REG_DCROP_HSIZE		CCI_REG16_LE(0x8AAA)
+#define ISX031_REG_DCROP_HOFFSET	CCI_REG16_LE(0x8AAC)
+#define ISX031_REG_DCROP_VSIZE		CCI_REG16_LE(0x8AAE)
+#define ISX031_REG_DCROP_VOFFSET	CCI_REG16_LE(0x8AB0)
+#define ISX031_REG_DCROP_DATA_SEL	CCI_REG8(0x8ADA)
+#define ISX031_DCROP_DATA_SEL		0x03
+
+/* Compared against the DCROP_* registers by the Application Lock function

*/

Quoted 60 lines
drivers/media/i2c/isx031.c new file +60 -0
+#define ISX031_REG_DCROP_ON_APL		CCI_REG8(0xBF04)
+#define ISX031_REG_DCROP_HSIZE_APL	CCI_REG16_LE(0xBF06)
+#define ISX031_REG_DCROP_HOFFSET_APL	CCI_REG16_LE(0xBF08)
+#define ISX031_REG_DCROP_VSIZE_APL	CCI_REG16_LE(0xBF0A)
+#define ISX031_REG_DCROP_VOFFSET_APL	CCI_REG16_LE(0xBF0C)
+
+#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_MAX		(USEC_PER_SEC / ISX031_FPS)
+#define ISX031_EXPOSURE_STEP		1
+#define ISX031_EXPOSURE_DEF		ISX031_EXPOSURE_MIN
+
+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;
+};
+
+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 int isx031_set_crop(struct isx031 *isx031, const struct

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+v4l2_rect *crop) {
+	struct regmap *regmap = isx031->regmap;
+	int ret = 0;
+
+	cci_write(regmap, ISX031_REG_DCROP_ON, 1, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HSIZE, crop->width, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HOFFSET, crop->left, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VSIZE, crop->height, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VOFFSET, crop->top, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_DATA_SEL,

ISX031_DCROP_DATA_SEL,

Quoted 13 lines
drivers/media/i2c/isx031.c new file +13 -0
+		  &ret);
+	cci_write(regmap, ISX031_REG_DCROP_ON_APL, 1, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HSIZE_APL, crop->width, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_HOFFSET_APL, crop->left, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VSIZE_APL, crop->height, &ret);
+	cci_write(regmap, ISX031_REG_DCROP_VOFFSET_APL, crop->top, &ret);
+
+	return ret;
+}
+
+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 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+				false, isx031->regmap,
+				ISX031_REG_SENSOR_STATE, &val, NULL);
+	/* The state register can lag the actual transition, so only warn */
+	if (ret)
+		dev_warn(isx031->dev,
+			 "Sensor state 0x%02llx, expected 0x%02x,

continuing\n",

Quoted 22 lines
drivers/media/i2c/isx031.c new file +22 -0
+			 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);

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+	if (ret) {
+		dev_err(dev, "Failed to set mode 0x%x\n", mode);
+		return ret;
+	}

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+	return isx031_wait_state(isx031, state); }
+
+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 line 207

Quoted 2 lines
drivers/media/i2c/isx031.c new file +2 -0
+	ret = read_poll_timeout(cci_read, read_ret,
+				!read_ret && (name &

ISX031_OTP_TYPE_NAME_MASK) ==

drivers/media/i2c/isx031.c

Quoted 3 lines
drivers/media/i2c/isx031.c new file +3 -0
+					     ISX031_OTP_MODULE_ID_L,
+				10 * USEC_PER_MSEC,
+				ISX031_READ_REG_RETRY_TIMEOUT * 10 *

USEC_PER_MSEC,

drivers/media/i2c/isx031.c line 211

Quoted 1 line
drivers/media/i2c/isx031.c new file +1 -0
+				false, isx031->regmap,

ISX031_OTP_TYPE_NAME,

Quoted 15 lines
drivers/media/i2c/isx031.c new file +15 -0
+				&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(struct v4l2_mbus_framefmt *fmt,
+				     u32 width, u32 height)
+{
+	fmt->width = width;
+	fmt->height = height;
+	fmt->code = ISX031_MBUS_CODE;
+	fmt->field = V4L2_FIELD_NONE;
+	fmt->colorspace = V4L2_COLORSPACE_SRGB;
+	fmt->ycbcr_enc =

V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);

drivers/media/i2c/isx031.c line 238

Quoted 2 lines
drivers/media/i2c/isx031.c new file +2 -0
+	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
+	fmt->xfer_func =

V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+}
+
+static int isx031_parse_endpoint(struct device *dev) {
+	struct fwnode_handle *endpoint;

drivers/media/i2c/isx031.c line 261

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	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);
Quoted 6 lines

drivers/media/i2c/isx031.c line 260

Quoted 3 lines
drivers/media/i2c/isx031.c new file +3 -0
+	if (!endpoint)
+		return dev_err_probe(dev, -EPROBE_DEFER,
+				     "waiting for fwnode graph endpoint\n");

Please remove this check, it's redundant.

Removed the check.

Quoted 150 lines
Quoted 124 lines
drivers/media/i2c/isx031.c new file +124 -0
+
+	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");
+
+	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;
+	}
+
+	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);
+
+out_err:
+	v4l2_fwnode_endpoint_free(&bus_cfg);
+	return ret;
+}
+
+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 v4l2_rect *crop = v4l2_subdev_state_get_crop(state, 0);
+	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);
+
+	ret = isx031_set_crop(isx031, crop);
+	if (ret) {
+		dev_err(dev, "Failed to set crop: %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_autosuspend(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_autosuspend(isx031->dev);
+
+	return 0;
+}
+
+static void isx031_power_off(struct isx031 *isx031) {
+	/* Without a reset line, standby is the only way to stop the sensor */
+	if (!isx031->reset_gpio)
+		isx031_mode_transit(isx031, ISX031_STATE_STARTUP);
+
+	/* Active low gpio reset, set 1 to power off sensor */
+	gpiod_set_value_cansleep(isx031->reset_gpio, 1); }
+
+static int isx031_power_on(struct isx031 *isx031) {
+	u64 val = 0;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	int read_ret;
+	int ret;
+
+	/* Active low gpio reset */
+	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);
+
+	/* Without a reset line the sensor was never reset, so has nothing to boot

*/

drivers/media/i2c/isx031.c

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+	if (!isx031->reset_gpio)
+		return 0;
+
+	/* After reset the sensor first NAKs, then reads 0xffff until OTP is loaded

*/

drivers/media/i2c/isx031.c

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+	ret = read_poll_timeout(cci_read, read_ret,
+				!read_ret && val != 0xffff,
+				10 * USEC_PER_MSEC,
+				ISX031_READ_REG_RETRY_TIMEOUT * 10 *

USEC_PER_MSEC,

drivers/media/i2c/isx031.c

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+				false, isx031->regmap,
+				ISX031_OTP_TYPE_NAME, &val, NULL);
+	if (ret)
+		dev_err(isx031->dev, "Sensor did not boot: OTP 0x%04llx\n",

val);

Quoted 12 lines

drivers/media/i2c/isx031.c line 419

Quoted 9 lines
drivers/media/i2c/isx031.c new file +9 -0
+
+	return ret;
+}
+
+static int isx031_suspend(struct device *dev) {
+	struct i2c_client *client = to_i2c_client(dev);
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+
+	isx031_power_off(to_isx031(sd));

Please refactor this so you have a single set of suspend / resume callbacks.

isx031_power_off() and isx031_power_on() now take a struct device * and are the only PM callbacks. I removed the isx031_suspend() and isx031_resume() wrappers and updated probe() and remove() to call the merged functions

Quoted 11 lines
Quoted 11 lines
drivers/media/i2c/isx031.c new file +11 -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;
+
+	ret = isx031_power_on(isx031);
Quoted 9 lines

drivers/media/i2c/isx031.c line 436

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+	if (ret)
+		return ret;
+
+	return cci_multi_reg_write(isx031->regmap, isx031_init_reg,
+				   ARRAY_SIZE(isx031_init_reg), NULL);

Are there cases where the init register sequence doesn't need to be written to the sensor?

Yes. On boards with no reset GPIO.
With a reset GPIO the registers are cleared on every power off, so the sequence is always needed.
Without a reset GPIO, a runtime suspend only puts the sensor into standby, so rewriting is likely redundant. However, the platform may cut power across system sleep without the driver knowing. It's only five writes, so I've kept it unconditional in power on.

Quoted 227 lines
Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+}
+
+static int isx031_get_frame_desc(struct v4l2_subdev *sd,
+				 unsigned int pad,
+				 struct v4l2_mbus_frame_desc *desc) {
+	desc->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	desc->num_entries = 1;
+	desc->entry[0].pixelcode = ISX031_MBUS_CODE;
+	desc->entry[0].stream = 0;
+	desc->entry[0].bus.csi2.vc = 0;
+	desc->entry[0].bus.csi2.dt = MIPI_CSI2_DT_YUV422_8B;
+	return 0;
+}
+
+/* The output is the crop rectangle unscaled; the sensor has no
+scaler */ static void isx031_update_crop(struct v4l2_subdev_state *sd_state,

u32 pad,

drivers/media/i2c/isx031.c line 445

Quoted 3 lines
drivers/media/i2c/isx031.c new file +3 -0
+			       const struct v4l2_rect *crop) {
+	*v4l2_subdev_state_get_crop(sd_state, pad) = *crop;
+	isx031_update_pad_format(v4l2_subdev_state_get_format(sd_state,

pad),

Quoted 8 lines
drivers/media/i2c/isx031.c new file +8 -0
+				 crop->width, crop->height);
+}
+
+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 v4l2_rect crop;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+
+	/* The sensor keeps the old crop until the next stream start. */
+	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+	    media_entity_is_streaming(&sd->entity))
+		return -EBUSY;
+
+	crop.width = clamp_t(u32, round_down(fmt->format.width, 2),
+			     ISX031_MIN_WIDTH, ISX031_PIXEL_ARRAY_WIDTH);
+	crop.height = clamp_t(u32, round_down(fmt->format.height, 2),
+			      ISX031_MIN_HEIGHT,

ISX031_PIXEL_ARRAY_HEIGHT);

drivers/media/i2c/isx031.c line 464

Quoted 1 line
drivers/media/i2c/isx031.c new file +1 -0
+	crop.left = round_down((ISX031_PIXEL_ARRAY_WIDTH - crop.width) / 2,

2);

Quoted 37 lines
drivers/media/i2c/isx031.c new file +37 -0
+	crop.top = round_down((ISX031_PIXEL_ARRAY_HEIGHT - crop.height) / 2,
+2);
+
+	isx031_update_crop(sd_state, fmt->pad, &crop);
+	fmt->format = *v4l2_subdev_state_get_format(sd_state, fmt->pad);
+
+	return 0;
+}
+
+static int isx031_get_selection(struct v4l2_subdev *sd,
+				const struct v4l2_subdev_client_info *ci,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel) {
+	switch (sel->target) {
+	case V4L2_SEL_TGT_CROP:
+		sel->r = *v4l2_subdev_state_get_crop(sd_state, sel->pad);
+		return 0;
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.left = 0;
+		sel->r.top = 0;
+		sel->r.width = ISX031_PIXEL_ARRAY_WIDTH;
+		sel->r.height = ISX031_PIXEL_ARRAY_HEIGHT;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int isx031_set_selection(struct v4l2_subdev *sd,
+				const struct v4l2_subdev_client_info *ci,
+				struct v4l2_subdev_state *sd_state,
+				struct v4l2_subdev_selection *sel) {
+	struct v4l2_rect crop;
+
+	if (sel->target != V4L2_SEL_TGT_CROP)

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+		return -EINVAL;
+
+	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+	    media_entity_is_streaming(&sd->entity))
+		return -EBUSY;
+
+	crop.width = clamp_t(u32, round_down(sel->r.width, 2),
+			     ISX031_MIN_WIDTH, ISX031_PIXEL_ARRAY_WIDTH);
+	crop.height = clamp_t(u32, round_down(sel->r.height, 2),
+			      ISX031_MIN_HEIGHT,

ISX031_PIXEL_ARRAY_HEIGHT);

Quoted 44 lines
drivers/media/i2c/isx031.c new file +44 -0
+	crop.left = clamp_t(s32, round_down(sel->r.left, 2), 0,
+			    ISX031_PIXEL_ARRAY_WIDTH - crop.width);
+	crop.top = clamp_t(s32, round_down(sel->r.top, 2), 0,
+			   ISX031_PIXEL_ARRAY_HEIGHT - crop.height);
+
+	isx031_update_crop(sd_state, sel->pad, &crop);
+	sel->r = crop;
+
+	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);
+	const struct v4l2_rect crop = {
+		.width = ISX031_PIXEL_ARRAY_WIDTH,
+		.height = ISX031_PIXEL_ARRAY_HEIGHT,
+	};
+
+	isx031_update_crop(sd_state, 0, &crop);
+	interval->numerator = 1;
+	interval->denominator = ISX031_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) {
+	if (code->index > 0)
+		return -EINVAL;
+
+	code->code = ISX031_MBUS_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) {
+	if (fse->index > 0 || fse->code != ISX031_MBUS_CODE)
+		return -EINVAL;
+
+	fse->min_width = ISX031_MIN_WIDTH;

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	fse->max_width = ISX031_PIXEL_ARRAY_WIDTH;
+	fse->min_height = ISX031_MIN_HEIGHT;
+	fse->max_height = ISX031_PIXEL_ARRAY_HEIGHT;
+
+	return 0;
+}
+
+static int isx031_enum_frame_interval(struct v4l2_subdev *sd,
+				      struct v4l2_subdev_state *sd_state,
+				      struct v4l2_subdev_frame_interval_enum

*fie) {

Quoted 51 lines
drivers/media/i2c/isx031.c new file +51 -0
+	if (fie->index > 0 || fie->code != ISX031_MBUS_CODE ||
+	    fie->width < ISX031_MIN_WIDTH ||
+	    fie->width > ISX031_PIXEL_ARRAY_WIDTH ||
+	    fie->height < ISX031_MIN_HEIGHT ||
+	    fie->height > ISX031_PIXEL_ARRAY_HEIGHT)
+		return -EINVAL;
+
+	fie->interval.numerator = 1;
+	fie->interval.denominator = ISX031_FPS;
+
+	return 0;
+}
+
+/* The frame rate is fixed regardless of the crop */ 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_selection = isx031_get_selection,
+	.set_selection = isx031_set_selection,
+	.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 20 lines
drivers/media/i2c/isx031.c new file +20 -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);
+
+	return 0;
+}
+
+static int isx031_set_ctrl(struct v4l2_ctrl *ctrl) {
+	struct isx031 *isx031 = container_of(ctrl->handler,
Quoted 9 lines

drivers/media/i2c/isx031.c

Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+					     struct isx031, ctrls);
+	struct device *dev = isx031->dev;
+	int ret;
+
+	/* Cached values are applied by __v4l2_ctrl_handler_setup() at
+stream start */

I'd say this comment is redundant.

Removed comment.

Quoted 9 lines

drivers/media/i2c/isx031.c line 679

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+	ret = pm_runtime_get_if_in_use(dev);
+	if (ret <= 0)
+		return 0;
+
+	ret = isx031_apply_ctrls(isx031);

Can you move the code from apply_ctrls() above here and do the writes based on the ctrl->id here, as other drivers do?

Code moved accordingly.

Quoted 26 lines
Quoted 15 lines
drivers/media/i2c/isx031.c new file +15 -0
+
+	pm_runtime_put_autosuspend(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 9 lines

drivers/media/i2c/isx031.c line 711

Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -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,

ctrl->id is presumably exposure already. Worth checking perhaps still?

Added check of ctrl->id

Quoted 44 lines
Quoted 19 lines
drivers/media/i2c/isx031.c new file +19 -0
+				exposure->minimum, exposure->maximum);
+
+out:
+	pm_runtime_put_autosuspend(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;
+

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+	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,

drivers/media/i2c/isx031.c

Quoted 13 lines
drivers/media/i2c/isx031.c new file +13 -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,
+				  ISX031_EXPOSURE_STEP,

ISX031_EXPOSURE_DEF);

Quoted 10 lines

drivers/media/i2c/isx031.c line 761

Quoted 7 lines
drivers/media/i2c/isx031.c new file +7 -0
+
+	/* Expose the orientation/rotation properties of the module. */
+	v4l2_ctrl_new_fwnode_properties(hdl, &isx031_ctrl_ops, &props);
+	if (hdl->error) {
+		ret = hdl->error;
+		v4l2_ctrl_handler_free(hdl);
+		return ret;

I'm afraid this was better in v3.

Ack, I'll use the return value of v4l2_ctrl_new_fwnode_properties() as in v3 and won't check hdl->error directly.

Quoted 26 lines

drivers/media/i2c/isx031.c

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+	}
+
+	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 742

Quoted 1 line
drivers/media/i2c/isx031.c new file +1 -0
+	sensor->ctrl_exposure->flags |=

V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;

Quoted 19 lines
drivers/media/i2c/isx031.c new file +19 -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);
Quoted 9 lines

drivers/media/i2c/isx031.c line 792

Quoted 6 lines
drivers/media/i2c/isx031.c new file +6 -0
+	v4l2_subdev_cleanup(sd);
+	media_entity_cleanup(&sd->entity);
+	v4l2_ctrl_handler_free(&isx031->ctrls);
+
+	pm_runtime_disable(&client->dev);
+	pm_runtime_dont_use_autosuspend(&client->dev);

You can drop this actually. The autosuspend is re-initialised now before probe.

Dropped pm_runtime_dont_use_autosuspend()

Quoted 42 lines
Quoted 31 lines
drivers/media/i2c/isx031.c new file +31 -0
+	if (!pm_runtime_status_suspended(&client->dev)) {
+		isx031_power_off(isx031);
+		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;
+	struct clk *clk;
+	int ret;
+
+	isx031 = devm_kzalloc_obj(dev, *isx031);
+	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,

drivers/media/i2c/isx031.c

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+				     "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");
+
+	isx031->fsin_gpio = devm_gpiod_get_optional(dev, "fsin",

GPIOD_OUT_LOW);

Quoted 7 lines

drivers/media/i2c/isx031.c line 837

Quoted 3 lines
drivers/media/i2c/isx031.c new file +3 -0
+	if (IS_ERR(isx031->fsin_gpio))
+		return dev_err_probe(dev, PTR_ERR(isx031->fsin_gpio),
+				     "Failed to get fsin gpio\n");

Can you drop all references to the fsin gpio? The driver now sets it output at low level but does nothing else with it.

Dropped the references of fsin gpio.

Quoted 12 lines

drivers/media/i2c/isx031.c line 845

Quoted 8 lines
drivers/media/i2c/isx031.c new file +8 -0
+
+	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;

I'd just use &isx031->sd where you need it. If you prefer to keep sd shorthand, then use it consistently.

Changed to use &isx031->sd

Quoted 10 lines

drivers/media/i2c/isx031.c

Quoted 4 lines
drivers/media/i2c/isx031.c new file +4 -0
+	v4l2_i2c_subdev_init(sd, client, &isx031_subdev_ops);
+
+	/* The sensor may not be in reset at this point, so reset it
+explicitly */

The reset GPIO is already set high and presumably the sensor isn't streaming. If you want to support operation without reset GPIO, then I'd be looking at software reset. Probably DT support will be added at some point so it'd be good to keep these calls balanced, too.

Agreed. In v6 I've dropped the isx031_power_off() call before isx031_power_on() in probe. The reset GPIO is requested asserted, so probe only waits for the reset hold time before releasing it.

I've also balanced the calls. If isx031_power_on() fails, it puts the sensor back into reset itself, and probe returns directly. Probe only calls isx031_power_off() on error paths after a successful power on.

Quoted 109 lines
Quoted 95 lines
drivers/media/i2c/isx031.c new file +95 -0
+	isx031_power_off(isx031);
+	if (isx031->reset_gpio)
+		msleep(ISX031_REG_SLEEP_50MS);
+
+	ret = isx031_power_on(isx031);
+	if (ret) {
+		dev_err_probe(dev, ret, "Failed to power on sensor\n");
+		goto err_power_off;
+	}
+
+	ret = isx031_identify_module(isx031);
+	if (ret)
+		goto err_power_off;
+
+	ret = cci_multi_reg_write(isx031->regmap, isx031_init_reg,
+				  ARRAY_SIZE(isx031_init_reg), NULL);
+	if (ret) {
+		dev_err_probe(dev, ret, "Failed to initialize 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) {
+		dev_err_probe(dev, ret, "Failed to finalize V4L2 subdev\n");
+		goto err_media_cleanup;
+	}
+
+	pm_runtime_set_active(dev);
+	pm_runtime_enable(dev);
+	/* Booting the sensor out of reset is slow, so avoid it between streams */
+	pm_runtime_set_autosuspend_delay(dev, 1000);
+	pm_runtime_use_autosuspend(dev);
+
+	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_dont_use_autosuspend(dev);
+	pm_runtime_set_suspended(dev);
+	v4l2_subdev_cleanup(&isx031->sd);
+err_media_cleanup:
+	media_entity_cleanup(&isx031->sd.entity);
+err_ctrl_free:
+	v4l2_ctrl_handler_free(&isx031->ctrls);
+err_power_off:
+	isx031_power_off(isx031);
+
+	return ret;
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(isx031_pm_ops, isx031_suspend,
+				 isx031_resume, NULL);
+
+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,

drivers/media/i2c/isx031.c line 953

Quoted 10 lines
drivers/media/i2c/isx031.c new file +10 -0
+};
+
+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");

--
Regards,

Quoted 1 line

Sakari Ailus

Best Regards
Vunny

#5 lore raw

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

Hi Vunny,

Quoted 17 lines · Vunny
Quoted 7 lines
Quoted 7 lines
drivers/media/i2c/isx031.c new file +7 -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;
+
+	ret = isx031_power_on(isx031);
Quoted 9 lines

drivers/media/i2c/isx031.c line 436

Quoted 5 lines
drivers/media/i2c/isx031.c new file +5 -0
+	if (ret)
+		return ret;
+
+	return cci_multi_reg_write(isx031->regmap, isx031_init_reg,
+				   ARRAY_SIZE(isx031_init_reg), NULL);

Are there cases where the init register sequence doesn't need to be written to the sensor?

Quoted 5 lines

Yes. On boards with no reset GPIO.
With a reset GPIO the registers are cleared on every power off, so the sequence is always needed.
Without a reset GPIO, a runtime suspend only puts the sensor into standby, so rewriting is likely redundant. However, the

That's not quite true. When the device is in RPM_SUSPENDED state, it is probably powered off -- whether there's a reset GPIO or not.

Btw. someone said the reset framework should also work on ACPI but I haven't tested it. We probably should make it work on ACPI if it doesn't but that's out of scope of this I think.

Signature · 3 lines
Regards,

Sakari Ailus