Skip to contentSkip to conversation

media: Add Lenovo Yoga Book camera support

PATCH In review Maurizio Casciano 3 participants 19 messages Nov 21 – Nov 23

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v11 cover media: Add Lenovo Yoga Book camera support

Add front and rear camera support for the Lenovo Yoga Book X91L. The front sensor is OV2740 and the rear sensor is OV8858 with a WV517S focus actuator. AtomISP ISP2401 can capture RAW Bayer from both cameras.

This review series is based on media-next. Apply David Heidelberg's posted V4L2 subdevice release fix first. The prerequisite patch ID below identifies that patch. WV517S needs it before the V4L2 core can release the actuator's final reference.

Changes since v10:

- Drop the upstream ACPI initializer and INT3477 ID changes. Reuse the existing rear IPU bridge entry and shared Cherry Trail PCI ID.
- Limit OV8858 19.2 MHz support to four-lane R2A. Carry the 2014 Intel OV8858 notice and record the sources of the register table.
- Drop the per-component white-balance controls from both sensors.
- Use div64_s64() for CSI-2 timing and make AtomISP RAW negotiation and enumeration follow the sensor's advertised formats.
- Defer WV517S I/O until use and hold its state through final release. Preserve existing camera nodes if registration of a new node fails.
- Keep the OV2740 probe retry as a separate diagnostic patch. The cold-boot logs did not show a natural I2C failure or recovery.

The revised sources built the full x86-64 image and modules. The focused Sparse checks, i386 AtomISP object link and WV517S build with CONFIG_PM disabled also passed.
The mocked registration test reproduced the previous rollback and verified that existing nodes survive each simulated failure. The format and notifier fixtures also passed.

On the X91L, the exact revised 7.4 image bound both sensors and WV517S at boot. Each camera delivered 30 distinct RAW and 20 distinct Preview YU12 frames before and after lens rebind. The capture and sensor nodes survived rebind, focus controls worked, and the actuator returned to runtime suspend. Physical focus travel, 24 MHz OV8858 hardware and X91F lane configuration remain open.

Codex drafted the rollback fix and cover text and consolidated the previously tested follow-ups. Individual commits describe its role.

v10: https://lore.kernel.org/linux-media/20261002-submission-media-yogabook-v10-b4-final-v10-0-817ec765b257@gmail.com/T/
V4L2 prerequisite: https://lore.kernel.org/r/[email protected]/

With Best Regards,
Maurizio Casciano

Maurizio Casciano (14):
media: v4l2-core: Preserve existing subdevice nodes on rollback
media: ov8858: Extract digital gain programming
media: ov8858: Support a 19.2 MHz input clock on four-lane R2A
media: ov2740: Add OVTI2740 ACPI ID
media: intel: ipu-bridge: Add the Yoga Book OVTI2740 entry
media: atomisp: Add Yoga Book camera configuration
media: ov2740: support 288 MHz link frequency
media: intel: ipu-bridge: select OV2740 frequency by IPU ID
media: atomisp: Derive CSI-2 timing from the sensor link frequency
media: ov2740: release group hold after gain write errors
media: atomisp: Use struct v4l2_area for padding
media: atomisp: Allow raw Bayer capture
media: i2c: Add WV517S lens actuator driver
media: atomisp: Preserve the capture graph when a lens rebinds

 MAINTAINERS                                   |   1 +
 drivers/media/i2c/Kconfig                     |  11 +
 drivers/media/i2c/Makefile                    |   1 +
 drivers/media/i2c/ov2740.c                    |  65 +++-
 drivers/media/i2c/ov8858.c                    | 172 +++++++---
 drivers/media/i2c/wv517s.c                    | 320 ++++++++++++++++++
 drivers/media/pci/intel/ipu-bridge.c          |   9 +
 drivers/media/v4l2-core/v4l2-device.c         |  26 +-
 .../staging/media/atomisp/pci/atomisp_cmd.c   | 103 +++---
 .../staging/media/atomisp/pci/atomisp_cmd.h   |   6 +-
 .../staging/media/atomisp/pci/atomisp_csi2.c  |  24 +-
 .../media/atomisp/pci/atomisp_csi2_bridge.c   |  36 +-
 .../staging/media/atomisp/pci/atomisp_ioctl.c | 117 +++++--
 .../media/atomisp/pci/atomisp_subdev.c        |   4 +-
 .../media/atomisp/pci/atomisp_subdev.h        |   3 +-
 include/media/v4l2-device.h                   |   3 +
 16 files changed, 748 insertions(+), 153 deletions(-)
 create mode 100644 drivers/media/i2c/wv517s.c

base-commit: ccf78740d63813c452c035b57d17a650492562fa
prerequisite-patch-id: def08de8e36aa31f6d2360428b29f6b0f4c8e1c8

Signature · 1 line
2.53.0
#1 lore raw

PATCH v11 1/14 media: v4l2-core: Preserve existing subdevice nodes on rollback

Subdevice node registration skips nodes which already exist. On failure, however, it walks the subdevice list from the beginning and unregisters nodes regardless of whether the current call created them. On AtomISP this can remove the camera sensor nodes when a lens rebind fails.

Keep a list of the video devices registered by this call and unregister only those devices on failure. Record each device before creating its media interface link so a failure in that step rolls back the new node as well. Calls with no missing nodes need no bookkeeping allocation.

Codex drafted the change and tested allocation, registration and media-link failure using the registration helper with mocked operations.

Fixes db0f4691d974 ("[media] v4l: Allow calling v4l2_device_register_subdev_nodes() multiple times")
---
 drivers/media/v4l2-core/v4l2-device.c | 26 ++++++++++++++++++++------
 include/media/v4l2-device.h           |  3 +++
 2 files changed, 23 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-device.c +20 -6
index b4036105c..8cfca95ce 100644
@@ -191,10 +191,23 @@ static void v4l2_device_release_subdev_node(struct video_device *vdev)
 int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
 					bool read_only)
 {
+	unsigned int count = 0, registered = 0;
+	struct video_device **new_nodes;
 	struct video_device *vdev;
 	struct v4l2_subdev *sd;
 	int err;
 
+	list_for_each_entry(sd, &v4l2_dev->subdevs, list)
+		if ((sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE) && !sd->devnode)
+			count++;
+
+	if (!count)
+		return 0;
+
+	new_nodes = kcalloc(count, sizeof(*new_nodes), GFP_KERNEL);
+	if (!new_nodes)
+		return -ENOMEM;
+
 	/* Register a device node for every subdev marked with the
 	 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
 	 */
@@ -228,6 +241,7 @@ int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
 			kfree(vdev);
 			goto clean_up;
 		}
+		new_nodes[registered++] = vdev;
 #if defined(CONFIG_MEDIA_CONTROLLER)
 		sd->entity.info.dev.major = VIDEO_MAJOR;
 		sd->entity.info.dev.minor = vdev->minor;
@@ -247,15 +261,15 @@ int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
 		}
 #endif
 	}
-	return 0;
+	err = 0;
+	goto out;
 
 clean_up:
-	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
-		if (!sd->devnode)
-			break;
-		video_unregister_device(sd->devnode);
-	}
+	while (registered)
+		video_unregister_device(new_nodes[--registered]);
 
+out:
+	kfree(new_nodes);
 	return err;
 }
 EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
include/media/v4l2-device.h +3 -0
index 25f69b1b8..6d8834d40 100644
@@ -183,6 +183,9 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
  * @read_only: subdevices read-only flag. True to register the subdevices
  *	device nodes in read-only mode, false to allow full access to the
  *	subdevice userspace API.
+ *
+ * Existing nodes are kept even if registration of a new node fails.
+ * The caller must serialize this operation with changes to the subdevice list.
  */
 int __must_check
 __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
Signature · 1 line
2.53.0
#4 lore raw

PATCH v11 2/14 media: ov8858: Extract digital gain programming

In reply to Maurizio Casciano

Move the existing packed digital-gain register programming into a helper so clock-specific gain handling can be added separately.

This is a pure refactoring: preserve both the register packing and the explanatory comment without changing the bytes written to the sensor.

---
 drivers/media/i2c/ov8858.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/media/i2c/ov8858.c +18 -12
index 97448fa75..295c34817 100644
@@ -1525,6 +1525,23 @@ static int ov8858_enable_test_pattern(struct ov8858 *ov8858, u32 pattern)
 	return ov8858_write(ov8858, OV8858_REG_TEST_PATTERN, val, NULL);
 }
 
+static int ov8858_set_long_digital_gain(struct ov8858 *ov8858, u32 gain)
+{
+	u16 long_gain;
+
+	/*
+	 * Digital gain is assembled as:
+	 * 0x350a[7:0] = dgain[13:6]
+	 * 0x350b[5:0] = dgain[5:0]
+	 * Reassemble the control value to write it in one go.
+	 */
+	long_gain = (gain & OV8858_LONG_DIGIGAIN_L_MASK) |
+		    ((gain & OV8858_LONG_DIGIGAIN_H_MASK) <<
+		     OV8858_LONG_DIGIGAIN_H_SHIFT);
+
+	return ov8858_write(ov8858, OV8858_REG_LONG_DIGIGAIN, long_gain, NULL);
+}
+
 static int ov8858_set_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct ov8858 *ov8858 = container_of(ctrl->handler,
@@ -1533,7 +1550,6 @@ static int ov8858_set_ctrl(struct v4l2_ctrl *ctrl)
 	struct i2c_client *client = v4l2_get_subdevdata(&ov8858->subdev);
 	struct v4l2_mbus_framefmt *format;
 	struct v4l2_subdev_state *state;
-	u16 digi_gain;
 	s64 max_exp;
 	int ret;
 
@@ -1572,17 +1588,7 @@ static int ov8858_set_ctrl(struct v4l2_ctrl *ctrl)
 				   ctrl->val, NULL);
 		break;
 	case V4L2_CID_DIGITAL_GAIN:
-		/*
-		 * Digital gain is assembled as:
-		 * 0x350a[7:0] = dgain[13:6]
-		 * 0x350b[5:0] = dgain[5:0]
-		 * Reassemble the control value to write it in one go.
-		 */
-		digi_gain = (ctrl->val & OV8858_LONG_DIGIGAIN_L_MASK)
-			  | ((ctrl->val & OV8858_LONG_DIGIGAIN_H_MASK) <<
-			      OV8858_LONG_DIGIGAIN_H_SHIFT);
-		ret = ov8858_write(ov8858, OV8858_REG_LONG_DIGIGAIN,
-				   digi_gain, NULL);
+		ret = ov8858_set_long_digital_gain(ov8858, ctrl->val);
 		break;
 	case V4L2_CID_VBLANK:
 		ret = ov8858_write(ov8858, OV8858_REG_VTS,
Signature · 1 line
2.53.0
#5 lore raw

PATCH v11 3/14 media: ov8858: Support a 19.2 MHz input clock on four-lane R2A

In reply to Maurizio Casciano

The Yoga Book rear sensor uses a 19.2 MHz input clock with a 360 MHz CSI-2 link frequency. Restrict the new clock table to four-lane R2A, the configuration tested on the Yoga Book X91L. Retain the 24 MHz tables for each existing variant and lane count. Base the power-on delay on the selected input frequency.

Eighteen entries in the 19.2 MHz table match Intel's GPLv2 OV8858 initialization table from the Yoga Book vendor kernel. Retain its 2014 Intel copyright notice. The existing driver already uses 0x3f08 = 0x08. The four adjusted PLL entries at 0x0300, 0x0302, 0x030b and 0x030d are retained from the first public Yoga Book patch. The source of those adjustments before that posting is unverified.

Codex split the original patch, added the revision and lane-count check and compared the register sources.

---
 drivers/media/i2c/ov8858.c | 142 ++++++++++++++++++++++++++++---------
 1 file changed, 107 insertions(+), 35 deletions(-)
drivers/media/i2c/ov8858.c +107 -35
index 295c34817..7b337dc93 100644
@@ -3,10 +3,9 @@
  * Copyright (C) 2023 Jacopo Mondi <[email protected]>
  * Copyright (C) 2022 Nicholas Roth <[email protected]>
  * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
+ * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  */
 
-#include <linux/unaligned.h>
-
 #include <linux/acpi.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
@@ -19,6 +18,8 @@
 #include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
+#include <linux/unaligned.h>
+#include <linux/units.h>
 
 #include <media/media-entity.h>
 #include <media/v4l2-async.h>
@@ -29,8 +30,9 @@
 #include <media/v4l2-mediabus.h>
 #include <media/v4l2-subdev.h>
 
-#define OV8858_LINK_FREQ		360000000U
-#define OV8858_XVCLK_FREQ		24000000
+#define OV8858_LINK_FREQ		(360 * HZ_PER_MHZ)
+#define OV8858_XVCLK_FREQ_24MHZ		(24000 * HZ_PER_KHZ)
+#define OV8858_XVCLK_FREQ_19_2MHZ	(19200 * HZ_PER_KHZ)
 
 #define OV8858_REG_SIZE_SHIFT		16
 #define OV8858_REG_ADDR_MASK		0xffff
@@ -105,6 +107,7 @@ struct ov8858_mode {
 
 struct ov8858 {
 	struct clk		*xvclk;
+	unsigned long		xvclk_rate;
 	struct gpio_desc	*reset_gpio;
 	struct gpio_desc	*pwdn_gpio;
 	struct regulator_bulk_data supplies[ARRAY_SIZE(ov8858_supply_names)];
@@ -118,10 +121,82 @@ struct ov8858 {
 	struct v4l2_ctrl	*vblank;
 
 	const struct regval	*global_regs;
+	const struct regval	*xvclk_regs;
 
 	unsigned int		num_lanes;
 };
 
+/* Keep input-clock programming separate from the common sensor setup. */
+static const struct regval ov8858_24mhz_360mhz_r1a_2lane[] = {
+	{0x0302, 0x1e},
+	{0x0303, 0x00},
+	{0x0304, 0x03},
+	{0x030e, 0x00},
+	{0x030f, 0x09},
+	{0x0312, 0x01},
+	{0x031e, 0x0c},
+	{0x4837, 0x16},
+	{REG_NULL, 0x00}
+};
+
+static const struct regval ov8858_24mhz_360mhz_r2a_2lane[] = {
+	{0x0302, 0x1e},
+	{0x0303, 0x00},
+	{0x0304, 0x03},
+	{0x030e, 0x02},
+	{0x030f, 0x04},
+	{0x0312, 0x03},
+	{0x031e, 0x0c},
+	{0x4837, 0x16},
+	{REG_NULL, 0x00}
+};
+
+static const struct regval ov8858_24mhz_360mhz_r2a_4lane[] = {
+	{0x0302, 0x1e},
+	{0x0303, 0x00},
+	{0x0304, 0x03},
+	{0x030e, 0x00},
+	{0x030f, 0x04},
+	{0x0312, 0x01},
+	{0x031e, 0x0c},
+	{0x4837, 0x16},
+	{REG_NULL, 0x00}
+};
+
+/*
+ * Register settings for a 19.2 MHz input and 360 MHz CSI-2 link.
+ *
+ * Besides the corrected sensor/MIPI PLL divisors, keep the final common
+ * black-level settings here. The per-mode tables retain their resolution
+ * dependent black-column anchors and window sizes.
+ */
+static const struct regval ov8858_19_2mhz_360mhz[] = {
+	{0x0300, 0x00},
+	{0x0302, 0x27},
+	{0x0303, 0x00},
+	{0x0304, 0x03},
+	{0x030b, 0x00},
+	{0x030d, 0x27},
+	{0x030e, 0x00},
+	{0x030f, 0x04},
+	{0x0312, 0x01},
+	{0x031e, 0x0c},
+	{0x3f08, 0x08},
+	{0x400a, 0x01},
+	{0x400d, 0x10},
+	{0x4011, 0x20},
+	{0x403e, 0x08},
+	{0x4040, 0x07},
+	{0x4041, 0xc6},
+	{0x4202, 0x00},
+	{0x4500, 0x58},
+	{0x470b, 0x28},
+	{0x4837, 0x15},
+	{0x58f4, 0x32},
+	{0x58f8, 0x3d},
+	{REG_NULL, 0x00}
+};
+
 static inline struct ov8858 *sd_to_ov8858(struct v4l2_subdev *sd)
 {
 	return container_of(sd, struct ov8858, subdev);
@@ -132,13 +207,6 @@ static const struct regval ov8858_global_regs_r1a[] = {
 	{0x0100, 0x00},
 	{0x0100, 0x00},
 	{0x0100, 0x00},
-	{0x0302, 0x1e},
-	{0x0303, 0x00},
-	{0x0304, 0x03},
-	{0x030e, 0x00},
-	{0x030f, 0x09},
-	{0x0312, 0x01},
-	{0x031e, 0x0c},
 	{0x3600, 0x00},
 	{0x3601, 0x00},
 	{0x3602, 0x00},
@@ -371,7 +439,6 @@ static const struct regval ov8858_global_regs_r1a[] = {
 	{0x4600, 0x00},
 	{0x4601, 0xcb},
 	{0x481f, 0x32},
-	{0x4837, 0x16},
 	{0x4850, 0x10},
 	{0x4851, 0x32},
 	{0x4b00, 0x2a},
@@ -406,13 +473,6 @@ static const struct regval ov8858_global_regs_r2a_2lane[] = {
 	 */
 	{0x0103, 0x01}, /* software reset */
 	{0x0100, 0x00}, /* software standby */
-	{0x0302, 0x1e}, /* pll1_multi */
-	{0x0303, 0x00}, /* pll1_divm */
-	{0x0304, 0x03}, /* pll1_div_mipi */
-	{0x030e, 0x02}, /* pll2_rdiv */
-	{0x030f, 0x04}, /* pll2_divsp */
-	{0x0312, 0x03}, /* pll2_pre_div0, pll2_r_divdac */
-	{0x031e, 0x0c}, /* pll1_no_lat */
 	{0x3600, 0x00},
 	{0x3601, 0x00},
 	{0x3602, 0x00},
@@ -649,7 +709,6 @@ static const struct regval ov8858_global_regs_r2a_2lane[] = {
 	{0x4600, 0x00},
 	{0x4601, 0xcb},
 	{0x481f, 0x32}, /* clk prepare min */
-	{0x4837, 0x16}, /* global timing */
 	{0x4850, 0x10}, /* lane 1 = 1, lane 0 = 0 */
 	{0x4851, 0x32}, /* lane 3 = 3, lane 2 = 2 */
 	{0x4b00, 0x2a},
@@ -811,13 +870,6 @@ static const struct regval ov8858_global_regs_r2a_4lane[] = {
 	{0x0103, 0x01}, /* software reset for OVTATool only */
 	{0x0103, 0x01}, /* software reset */
 	{0x0100, 0x00}, /* software standby */
-	{0x0302, 0x1e}, /* pll1_multi */
-	{0x0303, 0x00}, /* pll1_divm */
-	{0x0304, 0x03}, /* pll1_div_mipi */
-	{0x030e, 0x00}, /* pll2_rdiv */
-	{0x030f, 0x04}, /* pll2_divsp */
-	{0x0312, 0x01}, /* pll2_pre_div0, pll2_r_divdac */
-	{0x031e, 0x0c}, /* pll1_no_lat */
 	{0x3600, 0x00},
 	{0x3601, 0x00},
 	{0x3602, 0x00},
@@ -1054,7 +1106,6 @@ static const struct regval ov8858_global_regs_r2a_4lane[] = {
 	{0x4600, 0x00},
 	{0x4601, 0xcb},
 	{0x481f, 0x32}, /* clk prepare min */
-	{0x4837, 0x16}, /* global timing */
 	{0x4850, 0x10}, /* lane 1 = 1, lane 0 = 0 */
 	{0x4851, 0x32}, /* lane 3 = 3, lane 2 = 2 */
 	{0x4b00, 0x2a},
@@ -1346,6 +1397,10 @@ static int ov8858_start_stream(struct ov8858 *ov8858,
 	if (ret)
 		return ret;
 
+	ret = ov8858_write_array(ov8858, ov8858->xvclk_regs);
+	if (ret)
+		return ret;
+
 	/* 200 usec max to let PLL stabilize. */
 	fsleep(200);
 
@@ -1624,9 +1679,6 @@ static int ov8858_power_on(struct ov8858 *ov8858)
 	unsigned long delay_us;
 	int ret;
 
-	if (clk_get_rate(ov8858->xvclk) != OV8858_XVCLK_FREQ)
-		dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
-
 	ret = clk_prepare_enable(ov8858->xvclk);
 	if (ret < 0) {
 		dev_err(dev, "Failed to enable xvclk\n");
@@ -1645,7 +1697,7 @@ static int ov8858_power_on(struct ov8858 *ov8858)
 	 * transaction, but a double sleep between the release of gpios
 	 * helps with sporadic failures observed at probe time.
 	 */
-	delay_us = DIV_ROUND_UP(8192, OV8858_XVCLK_FREQ / 1000 / 1000);
+	delay_us = DIV_ROUND_UP(8192, ov8858->xvclk_rate / HZ_PER_MHZ);
 
 	gpiod_set_value_cansleep(ov8858->reset_gpio, 0);
 	fsleep(delay_us);
@@ -1806,15 +1858,20 @@ static int ov8858_check_sensor_id(struct ov8858 *ov8858)
 
 	if (id == OV8858_R2A) {
 		/* R2A supports 2 and 4 lanes modes. */
-		ov8858->global_regs = ov8858->num_lanes == 4
-				    ? ov8858_global_regs_r2a_4lane
-				    : ov8858_global_regs_r2a_2lane;
+		if (ov8858->num_lanes == 4) {
+			ov8858->global_regs = ov8858_global_regs_r2a_4lane;
+			ov8858->xvclk_regs = ov8858_24mhz_360mhz_r2a_4lane;
+		} else {
+			ov8858->global_regs = ov8858_global_regs_r2a_2lane;
+			ov8858->xvclk_regs = ov8858_24mhz_360mhz_r2a_2lane;
+		}
 	} else if (ov8858->num_lanes == 2) {
 		/*
 		 * R1A only supports 2 lanes mode and it's only partially
 		 * supported.
 		 */
 		ov8858->global_regs = ov8858_global_regs_r1a;
+		ov8858->xvclk_regs = ov8858_24mhz_360mhz_r1a_2lane;
 		dev_warn(&client->dev, "R1A may not work well!\n");
 	} else {
 		dev_err(&client->dev,
@@ -1822,6 +1879,14 @@ static int ov8858_check_sensor_id(struct ov8858 *ov8858)
 		return -EINVAL;
 	}
 
+	if (ov8858->xvclk_rate == OV8858_XVCLK_FREQ_19_2MHZ) {
+		if (id != OV8858_R2A || ov8858->num_lanes != 4)
+			return dev_err_probe(&client->dev, -EINVAL,
+					     "19.2 MHz requires four lanes and R2A\n");
+
+		ov8858->xvclk_regs = ov8858_19_2mhz_360mhz;
+	}
+
 	return 0;
 }
 
@@ -1889,6 +1954,13 @@ static int ov8858_probe(struct i2c_client *client)
 		return dev_err_probe(dev, PTR_ERR(ov8858->xvclk),
 				     "Failed to get xvclk\n");
 
+	ov8858->xvclk_rate = clk_get_rate(ov8858->xvclk);
+	if (ov8858->xvclk_rate != OV8858_XVCLK_FREQ_19_2MHZ &&
+	    ov8858->xvclk_rate != OV8858_XVCLK_FREQ_24MHZ)
+		return dev_err_probe(dev, -EINVAL,
+				     "Unsupported xvclk rate %lu Hz\n",
+				     ov8858->xvclk_rate);
+
 	ov8858->reset_gpio = devm_gpiod_get_optional(dev, "reset",
 						     GPIOD_OUT_HIGH);
 	if (IS_ERR(ov8858->reset_gpio))
Signature · 1 line
2.53.0
#6 lore raw

PATCH v11 4/14 media: ov2740: Add OVTI2740 ACPI ID

In reply to Maurizio Casciano

Firmware may enumerate OV2740 image sensors using the OVTI2740 ACPI ID. Add it to the existing ACPI match table.

---
 drivers/media/i2c/ov2740.c | 1 +
 1 file changed, 1 insertion(+)
drivers/media/i2c/ov2740.c +1 -0
index b7c0f2890..7d786d09f 100644
@@ -1463,6 +1463,7 @@ static DEFINE_RUNTIME_DEV_PM_OPS(ov2740_pm_ops, ov2740_suspend, ov2740_resume,
 
 static const struct acpi_device_id ov2740_acpi_ids[] = {
 	{ .id = "INT3474" },
+	{ .id = "OVTI2740" },
 	{ }
 };
 
Signature · 1 line
2.53.0
#7 lore raw

PATCH v11 5/14 media: intel: ipu-bridge: Add the Yoga Book OVTI2740 entry

In reply to Maurizio Casciano

The Yoga Book YB1-X91 uses OVTI2740 for its OV2740 front sensor. Add its default link frequency so the IPU bridge can construct the software endpoint. The existing INT3477 entry covers the rear sensor.

---
 drivers/media/pci/intel/ipu-bridge.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/pci/intel/ipu-bridge.c +2 -0
index 2a38c4bd8..5f6baac2c 100644
@@ -115,6 +115,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
 	/* Omnivision OV2680 */
 	IPU_SENSOR_CONFIG("OVTI2680", 1, 331200000),
+	/* Omnivision OV2740 */
+	IPU_SENSOR_CONFIG("OVTI2740", 1, 360000000),
 	/* Omnivision OV5675 */
 	IPU_SENSOR_CONFIG("OVTI5675", 1, 450000000),
 	/* Omnivision OV5693 */
Signature · 1 line
2.53.0
#8 lore raw

PATCH v11 6/14 media: atomisp: Add Yoga Book camera configuration

In reply to Maurizio Casciano

The Yoga Book YB1-X91 firmware lacks usable AtomISP configuration for both cameras. Rear INT3477 OV8858 uses four CSI-2 lanes and a WV517S actuator, while the front OVTI2740 firmware reports the wrong lane count.

Add the rear sensor configuration and a DMI-scoped two-lane override for OVTI2740:00. Add the front sensor configuration separately once its link frequency can be supplied at the same time.

---
 .../media/atomisp/pci/atomisp_csi2_bridge.c   | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c +18 -1
index cca91c6d7..56da454f0 100644
@@ -74,6 +74,12 @@ static struct gmin_cfg_var lenovo_ideapad_miix_310_vars[] = {
 	{}
 };
 
+static struct gmin_cfg_var lenovo_yogabook_x91_vars[] = {
+	/* The vendor driver and sensor modes use two CSI data lanes. */
+	{ "OVTI2740:00", "CsiLanes", "2" },
+	{}
+};
+
 static struct gmin_cfg_var xiaomi_mipad2_vars[] = {
 	/* _DSM contains the wrong CsiPort for the front facing OV5693 sensor */
 	{ "INT33BE:00", "CsiPort", "0" },
@@ -83,6 +89,14 @@ static struct gmin_cfg_var xiaomi_mipad2_vars[] = {
 };
 
 static const struct dmi_system_id gmin_cfg_dmi_overrides[] = {
+	{
+		/* Lenovo Yoga Book X91L */
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
+			DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91L"),
+		},
+		.driver_data = lenovo_yogabook_x91_vars,
+	},
 	{
 		/* Lenovo Ideapad Miix 310 */
 		.matches = {
@@ -359,7 +373,10 @@ static const struct acpi_device_id atomisp_sensor_configs[] = {
 	 * the sensor fails to start streaming when instantiating
 	 * an i2c-client for the VCM, so it is disabled for now.
 	 */
-	ATOMISP_SENSOR_CONFIG("INT33BE", 2, false),	/* OV5693 */
+	/* OV5693 */
+	ATOMISP_SENSOR_CONFIG("INT33BE", 2, false),
+	/* OV8858 */
+	ATOMISP_SENSOR_CONFIG("INT3477", 4, true),
 	{}
 };
 
Signature · 1 line
2.53.0
#9 lore raw

PATCH v11 7/14 media: ov2740: support 288 MHz link frequency

In reply to Maurizio Casciano

The Lenovo Yoga Book YB1-X91 front camera uses two CSI-2 lanes at a 288 MHz link frequency. Add the matching PLL configuration and mode timing.

Reuse the existing 1932x1092 crop and mode register list so the Bayer order remains SGRBG across link frequencies. Cropping the CSI-2 receiver output to a smaller processed frame is a userspace pipeline decision.

---
 drivers/media/i2c/ov2740.c | 43 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
drivers/media/i2c/ov2740.c +41 -2
index 7d786d09f..30eb58eed 100644
@@ -12,12 +12,14 @@
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
 #include <linux/unaligned.h>
+#include <linux/units.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-fwnode.h>
 
-#define OV2740_LINK_FREQ_360MHZ		360000000ULL
-#define OV2740_LINK_FREQ_180MHZ		180000000ULL
+#define OV2740_LINK_FREQ_360MHZ		(360 * HZ_PER_MHZ)
+#define OV2740_LINK_FREQ_288MHZ		(288 * HZ_PER_MHZ)
+#define OV2740_LINK_FREQ_180MHZ		(180 * HZ_PER_MHZ)
 #define OV2740_SCLK			72000000LL
 #define OV2740_MCLK			19200000
 #define OV2740_DATA_LANES		2
@@ -91,6 +93,7 @@ struct nvm_data {
 
 enum {
 	OV2740_LINK_FREQ_360MHZ_INDEX,
+	OV2740_LINK_FREQ_288MHZ_INDEX,
 	OV2740_LINK_FREQ_180MHZ_INDEX,
 };
 
@@ -142,6 +145,14 @@ static const struct ov2740_reg mipi_data_rate_720mbps[] = {
 	{0x0312, 0x11},
 };
 
+static const struct ov2740_reg mipi_data_rate_576mbps[] = {
+	{0x0302, 0x1e},
+	{0x0303, 0x00},
+	{0x030d, 0x1e},
+	{0x030e, 0x02},
+	{0x0312, 0x01},
+};
+
 static const struct ov2740_reg mipi_data_rate_360mbps[] = {
 	{0x0302, 0x4b},
 	{0x0303, 0x01},
@@ -468,6 +479,7 @@ static const char * const ov2740_test_pattern_menu[] = {
 
 static const s64 link_freq_menu_items[] = {
 	OV2740_LINK_FREQ_360MHZ,
+	OV2740_LINK_FREQ_288MHZ,
 	OV2740_LINK_FREQ_180MHZ,
 };
 
@@ -478,6 +490,12 @@ static const struct ov2740_link_freq_config link_freq_configs[] = {
 			.regs = mipi_data_rate_720mbps,
 		}
 	},
+	[OV2740_LINK_FREQ_288MHZ_INDEX] = {
+		.reg_list = {
+			.num_of_regs = ARRAY_SIZE(mipi_data_rate_576mbps),
+			.regs = mipi_data_rate_576mbps,
+		}
+	},
 	[OV2740_LINK_FREQ_180MHZ_INDEX] = {
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
@@ -502,6 +520,22 @@ static const struct ov2740_mode supported_modes_360mhz[] = {
 	},
 };
 
+static const struct ov2740_mode supported_modes_288mhz[] = {
+	{
+		.width = 1932,
+		.height = 1092,
+		.hts = 2160,
+		.vts_min = 1776,
+		.vts_def = 1776,
+		.vts_max = 32767,
+		.reg_list = {
+			.num_of_regs = ARRAY_SIZE(mode_1932x1092_regs_360mhz),
+			.regs = mode_1932x1092_regs_360mhz,
+		},
+		.link_freq_index = OV2740_LINK_FREQ_288MHZ_INDEX,
+	},
+};
+
 static const struct ov2740_mode supported_modes_180mhz[] = {
 	{
 		.width = 1932,
@@ -1179,6 +1213,11 @@ static int ov2740_check_hwcfg(struct ov2740 *ov2740)
 			ov2740->supported_modes_count =
 				ARRAY_SIZE(supported_modes_360mhz);
 			break;
+		case OV2740_LINK_FREQ_288MHZ_INDEX:
+			ov2740->supported_modes = supported_modes_288mhz;
+			ov2740->supported_modes_count =
+				ARRAY_SIZE(supported_modes_288mhz);
+			break;
 		case OV2740_LINK_FREQ_180MHZ_INDEX:
 			ov2740->supported_modes = supported_modes_180mhz;
 			ov2740->supported_modes_count =
Signature · 1 line
2.53.0
#10 lore raw

PATCH v11 8/14 media: intel: ipu-bridge: select OV2740 frequency by IPU ID

In reply to Maurizio Casciano

The OV2740 connected to the Cherry Trail AtomISP uses a 288 MHz link, while the generic OVTI2740 configuration uses 360 MHz.

Use the bridge's existing PCI-device matching mechanism to add a Cherry Trail-specific OVTI2740 entry before the generic fallback. This keeps the platform distinction in the bridge and avoids adding a second per-sensor link-frequency override path.

---
 drivers/media/pci/intel/ipu-bridge.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/media/pci/intel/ipu-bridge.c +7 -0
index 5f6baac2c..5dbd74e2b 100644
@@ -58,6 +58,11 @@ static const u16 ipu6_ov5693_ncont_clk[] = {
 	0
 };
 
+static const u16 atomisp_cht_pci_ids[] = {
+	PCI_DEVICE_ID_INTEL_ISP_BSW,
+	0
+};
+
 static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	/* GalaxyCore GC5035 */
 	IPU_SENSOR_CONFIG("GCTI5035", 2, 175200000, 422400000),
@@ -116,6 +121,8 @@ static const struct ipu_sensor_config ipu_supported_sensors[] = {
 	/* Omnivision OV2680 */
 	IPU_SENSOR_CONFIG("OVTI2680", 1, 331200000),
 	/* Omnivision OV2740 */
+	IPU_SENSOR_CONFIG_MATCH_FL("OVTI2740", atomisp_cht_pci_ids, 0,
+				   1, 288000000),
 	IPU_SENSOR_CONFIG("OVTI2740", 1, 360000000),
 	/* Omnivision OV5675 */
 	IPU_SENSOR_CONFIG("OVTI5675", 1, 450000000),
Signature · 1 line
2.53.0
#11 lore raw

PATCH v11 9/14 media: atomisp: Derive CSI-2 timing from the sensor link frequency

In reply to Maurizio Casciano

The ISP2401 CSI-2 receiver timing depends on the sensor link frequency. Query it through v4l2_get_link_freq() instead of reading a 32-bit control value directly.

Keep the frequency as s64 to avoid an arbitrary S32_MAX limit. Use div64_s64() so 32-bit builds use the kernel division helper. Fall back to the default timings when the frequency query or divisor is invalid.

Sashiko identified the 32-bit division issue. Codex reproduced it and drafted the division helper change.

---
 .../staging/media/atomisp/pci/atomisp_csi2.c  | 24 ++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/staging/media/atomisp/pci/atomisp_csi2.c +16 -8
index 71df4ef62..f72fd0c3c 100644
@@ -5,6 +5,9 @@
  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
  */
 
+#include <linux/math64.h>
+
+#include <media/v4l2-common.h>
 #include <media/v4l2-event.h>
 #include <media/v4l2-mediabus.h>
 #include "atomisp_cmd.h"
@@ -210,7 +213,7 @@ int atomisp_mipi_csi2_register_entities(struct atomisp_mipi_csi2_device *csi2,
 static const int LIMIT_SHIFT = 6;	/* Limit numeric range into 31 bits */
 
 static int
-atomisp_csi2_configure_calc(const short int coeffs[2], int mipi_freq, int def)
+atomisp_csi2_configure_calc(const short int coeffs[2], s64 mipi_freq, int def)
 {
 	/* Delay counter accuracy, 1/0.0625 for ANN/CHT, 1/0.125 for BXT */
 	static const int accinv = 16;		/* 1 / COUNT_ACC */
@@ -220,7 +223,7 @@ atomisp_csi2_configure_calc(const short int coeffs[2], int mipi_freq, int def)
 		return def;
 
 	r = accinv * coeffs[1] * (500000000 >> LIMIT_SHIFT);
-	r /= mipi_freq >> LIMIT_SHIFT;
+	r = div64_s64(r, mipi_freq >> LIMIT_SHIFT);
 	r += accinv * coeffs[0];
 
 	return r;
@@ -289,18 +292,18 @@ static void atomisp_csi2_configure_isp2401(struct atomisp_sub_device *asd)
 	int dat_termen;
 	int dat_settle;
 
-	struct v4l2_control ctrl;
 	struct atomisp_device *isp = asd->isp;
-	int mipi_freq = 0;
 	enum atomisp_camera_port port;
+	struct v4l2_subdev *sensor;
+	s64 mipi_freq;
 	int n;
 
 	port = isp->inputs[asd->input_curr].port;
 
-	ctrl.id = V4L2_CID_LINK_FREQ;
-	if (v4l2_g_ctrl
-	    (isp->inputs[asd->input_curr].sensor->ctrl_handler, &ctrl) == 0)
-		mipi_freq = ctrl.value;
+	sensor = isp->inputs[asd->input_curr].sensor;
+	mipi_freq = v4l2_get_link_freq(&sensor->entity.pads[0], 0, 0);
+	if (mipi_freq < 0)
+		mipi_freq = 0;
 
 	clk_termen = atomisp_csi2_configure_calc(coeff_clk_termen, mipi_freq,
 						 TERMEN_DEFAULT);
@@ -311,6 +314,11 @@ static void atomisp_csi2_configure_isp2401(struct atomisp_sub_device *asd)
 	dat_settle = atomisp_csi2_configure_calc(coeff_dat_settle, mipi_freq,
 						 SETTLE_DEFAULT);
 
+	dev_dbg(isp->dev,
+		"CSI port %u link frequency %lld Hz, clk timing %d/%d, data timing %d/%d\n",
+		port, mipi_freq, clk_termen, clk_settle,
+		dat_termen, dat_settle);
+
 	for (n = 0; n < csi2_port_lanes[port] + 1; n++) {
 		hrt_address base = csi2_port_base[port] + csi2_lane_base[n];
 
Signature · 1 line
2.53.0
#12 lore raw

PATCH v11 10/14 media: ov2740: release group hold after gain write errors

In reply to Maurizio Casciano

Digital-gain updates start a register group before writing the red, green and blue gains. The current error paths return immediately when one of those writes fails, leaving the group open and never issuing its end and launch commands. Later control updates can then be accumulated in an unfinished group instead of taking effect.

Once group hold has started, always attempt both commands needed to end and launch the group. Preserve the first gain-write error; report an end or launch error only when all gain writes succeeded.

Fixes 84363509c725 ("media: ov2740: use group write for digital gain")
---
 drivers/media/i2c/ov2740.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/media/i2c/ov2740.c +9 -12
index 30eb58eed..b9a951e92 100644
@@ -696,7 +696,7 @@ static int ov2740_identify_module(struct ov2740 *ov2740)
 
 static int ov2740_update_digital_gain(struct ov2740 *ov2740, u32 d_gain)
 {
-	int ret;
+	int end_ret, launch_ret, ret;
 
 	ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
 			       OV2740_GROUP_HOLD_START);
@@ -705,24 +705,21 @@ static int ov2740_update_digital_gain(struct ov2740 *ov2740, u32 d_gain)
 
 	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_R_GAIN, 2, d_gain);
 	if (ret)
-		return ret;
+		goto release_group;
 
 	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_G_GAIN, 2, d_gain);
 	if (ret)
-		return ret;
+		goto release_group;
 
 	ret = ov2740_write_reg(ov2740, OV2740_REG_MWB_B_GAIN, 2, d_gain);
-	if (ret)
-		return ret;
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
-			       OV2740_GROUP_HOLD_END);
-	if (ret)
-		return ret;
+release_group:
+	end_ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
+				   OV2740_GROUP_HOLD_END);
+	launch_ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
+				      OV2740_GROUP_HOLD_LAUNCH);
 
-	ret = ov2740_write_reg(ov2740, OV2740_REG_GROUP_ACCESS, 1,
-			       OV2740_GROUP_HOLD_LAUNCH);
-	return ret;
+	return ret ?: end_ret ?: launch_ret;
 }
 
 static int ov2740_test_pattern(struct ov2740 *ov2740, u32 pattern)
Signature · 1 line
2.53.0
#13 lore raw

PATCH v11 11/14 media: atomisp: Use struct v4l2_area for padding

In reply to Maurizio Casciano

The padding helper passes width and height as four separate scalar arguments even though they form two logical dimensions.

Pass the requested size and returned padding as struct v4l2_area values. This makes the dimensions explicit and simplifies all three callers.

---
 .../staging/media/atomisp/pci/atomisp_cmd.c   | 69 +++++++++++--------
 .../staging/media/atomisp/pci/atomisp_cmd.h   |  6 +-
 .../staging/media/atomisp/pci/atomisp_ioctl.c | 15 ++--
 .../media/atomisp/pci/atomisp_subdev.c        |  4 +-
 .../media/atomisp/pci/atomisp_subdev.h        |  3 +-
 5 files changed, 54 insertions(+), 43 deletions(-)
drivers/staging/media/atomisp/pci/atomisp_cmd.c +40 -29
index e66a7d1c7..c2978e587 100644
@@ -3561,9 +3561,9 @@ static void atomisp_fill_pix_format(struct v4l2_pix_format *f,
 	f->xfer_func = V4L2_XFER_FUNC_709;
 }
 
-/* Get sensor padding values for the non padded width x height resolution */
-void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height,
-			 u32 *padding_w, u32 *padding_h)
+/* Get sensor padding values for the non-padded size */
+void atomisp_get_padding(struct atomisp_device *isp, struct v4l2_area size,
+			 struct v4l2_area *pad)
 {
 	struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
 	struct v4l2_rect native_rect = input->native_rect;
@@ -3573,22 +3573,24 @@ void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height,
 	struct v4l2_mbus_framefmt *sink;
 
 	if (!input->crop_support) {
-		*padding_w = pad_w;
-		*padding_h = pad_h;
+		pad->width = pad_w;
+		pad->height = pad_h;
 		return;
 	}
 
-	width = min(width, input->active_rect.width);
-	height = min(height, input->active_rect.height);
+	size.width = min(size.width, input->active_rect.width);
+	size.height = min(size.height, input->active_rect.height);
 
-	if (input->binning_support && width <= (input->active_rect.width / 2) &&
-				      height <= (input->active_rect.height / 2)) {
+	if (input->binning_support &&
+	    size.width <= (input->active_rect.width / 2) &&
+	    size.height <= (input->active_rect.height / 2)) {
 		native_rect.width /= 2;
 		native_rect.height /= 2;
 	}
 
-	*padding_w = min_t(u32, (native_rect.width - width) & ~1, pad_w);
-	*padding_h = min_t(u32, (native_rect.height - height) & ~1, pad_h);
+	pad->width = min_t(u32, (native_rect.width - size.width) & ~1, pad_w);
+	pad->height = min_t(u32, (native_rect.height - size.height) & ~1,
+			    pad_h);
 
 	/* The below minimum padding requirements are for BYT / ISP2400 only */
 	if (IS_ISP2401)
@@ -3616,8 +3618,18 @@ void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height,
 		min_pad_h += 2;
 
 apply_min_padding:
-	*padding_w = max_t(u32, *padding_w, min_pad_w);
-	*padding_h = max_t(u32, *padding_h, min_pad_h);
+	pad->width = max_t(u32, pad->width, min_pad_w);
+	pad->height = max_t(u32, pad->height, min_pad_h);
+}
+
+static void
+atomisp_get_pix_padding(struct atomisp_device *isp,
+			struct v4l2_pix_format *f,
+			struct v4l2_area *pad)
+{
+	struct v4l2_area size = { .width = f->width, .height = f->height };
+
+	atomisp_get_padding(isp, size, pad);
 }
 
 int atomisp_s_sensor_power(struct atomisp_device *isp, unsigned int input, bool on)
@@ -3805,7 +3817,7 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 	const struct atomisp_format_bridge *fmt, *snr_fmt;
 	struct atomisp_sub_device *asd = &isp->asd;
 	struct v4l2_mbus_framefmt ffmt = { };
-	u32 padding_w, padding_h;
+	struct v4l2_area padding;
 	int ret;
 
 	fmt = atomisp_get_format_bridge(f->pixelformat);
@@ -3832,10 +3844,10 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 	 * resolution + padding. Add padding here and remove it again after
 	 * the set_fmt call, like atomisp_set_fmt_to_snr() does.
 	 */
-	atomisp_get_padding(isp, f->width, f->height, &padding_w, &padding_h);
+	atomisp_get_pix_padding(isp, f, &padding);
 	v4l2_fill_mbus_format(&ffmt, f, fmt->mbus_code);
-	ffmt.width += padding_w;
-	ffmt.height += padding_h;
+	ffmt.width += padding.width;
+	ffmt.height += padding.height;
 
 	dev_dbg(isp->dev, "try_mbus_fmt: try %ux%u\n", ffmt.width, ffmt.height);
 
@@ -3852,8 +3864,8 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 		return -EINVAL;
 	}
 
-	f->width = ffmt.width - padding_w;
-	f->height = ffmt.height - padding_h;
+	f->width = ffmt.width - padding.width;
+	f->height = ffmt.height - padding.height;
 
 	/*
 	 * If the format is jpeg or custom RAW, then the width and height will
@@ -4273,11 +4285,12 @@ static int atomisp_set_fmt_to_snr(struct video_device *vdev, const struct v4l2_p
 		return -EINVAL;
 
 	v4l2_fill_mbus_format(&ffmt, f, format->mbus_code);
-	ffmt.height += asd->sink_pad_padding_h + dvs_env_h;
-	ffmt.width += asd->sink_pad_padding_w + dvs_env_w;
+	ffmt.height += asd->sink_pad_padding.height + dvs_env_h;
+	ffmt.width += asd->sink_pad_padding.width + dvs_env_w;
 
 	dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
-		ffmt.width, ffmt.height, asd->sink_pad_padding_w, asd->sink_pad_padding_h,
+		ffmt.width, ffmt.height, asd->sink_pad_padding.width,
+		asd->sink_pad_padding.height,
 		dvs_env_w, dvs_env_h);
 
 	__atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
@@ -4370,13 +4383,11 @@ int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
 				V4L2_SUBDEV_FORMAT_ACTIVE,
 				ATOMISP_SUBDEV_PAD_SOURCE, &isp_source_fmt);
 
-	if (atomisp_subdev_format_conversion(asd)) {
-		atomisp_get_padding(isp, f->fmt.pix.width, f->fmt.pix.height,
-				    &asd->sink_pad_padding_w, &asd->sink_pad_padding_h);
-	} else {
-		asd->sink_pad_padding_w = 0;
-		asd->sink_pad_padding_h = 0;
-	}
+	if (atomisp_subdev_format_conversion(asd))
+		atomisp_get_pix_padding(isp, &f->fmt.pix,
+					&asd->sink_pad_padding);
+	else
+		asd->sink_pad_padding = (struct v4l2_area) { };
 
 	atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
 				&dvs_env_w, &dvs_env_h);
drivers/staging/media/atomisp/pci/atomisp_cmd.h +3 -3
index d3d1f2574..8a84e774f 100644
@@ -225,9 +225,9 @@ int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
 int atomisp_compare_grid(struct atomisp_sub_device *asd,
 			 struct atomisp_grid_info *atomgrid);
 
-/* Get sensor padding values for the non padded width x height resolution */
-void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height,
-			 u32 *padding_w, u32 *padding_h);
+/* Get sensor padding values for the non-padded size */
+void atomisp_get_padding(struct atomisp_device *isp, struct v4l2_area size,
+			 struct v4l2_area *pad);
 
 /* Set sensor power (no-op if already on/off) */
 int atomisp_s_sensor_power(struct atomisp_device *isp, unsigned int input, bool on);
drivers/staging/media/atomisp/pci/atomisp_ioctl.c +8 -7
index 50366bf10..87c29a940 100644
@@ -457,7 +457,7 @@ static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
 					      const struct v4l2_rect *native,
 					      int *valid_sizes)
 {
-	static const struct v4l2_frmsize_discrete frame_sizes[] = {
+	static const struct v4l2_area frame_sizes[] = {
 		{ 1920, 1440 },
 		{ 1920, 1200 },
 		{ 1920, 1080 },
@@ -470,15 +470,15 @@ static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
 		{  800,  600 },
 		{  640,  480 },
 	};
-	u32 padding_w, padding_h;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(frame_sizes); i++) {
-		atomisp_get_padding(isp, frame_sizes[i].width, frame_sizes[i].height,
-				    &padding_w, &padding_h);
+		struct v4l2_area padding;
 
-		if ((frame_sizes[i].width + padding_w) > native->width ||
-		    (frame_sizes[i].height + padding_h) > native->height)
+		atomisp_get_padding(isp, frame_sizes[i], &padding);
+
+		if ((frame_sizes[i].width + padding.width) > native->width ||
+		    (frame_sizes[i].height + padding.height) > native->height)
 			continue;
 
 		/*
@@ -491,7 +491,8 @@ static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
 
 		if (*valid_sizes == fsize->index) {
 			fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
-			fsize->discrete = frame_sizes[i];
+			fsize->discrete.width = frame_sizes[i].width;
+			fsize->discrete.height = frame_sizes[i].height;
 			return 0;
 		}
 
drivers/staging/media/atomisp/pci/atomisp_subdev.c +2 -2
index bdd2d9b18..495032966 100644
@@ -327,8 +327,8 @@ int atomisp_subdev_set_selection(struct v4l2_subdev *sd,
 
 		if (atomisp_subdev_format_conversion(isp_sd)
 		    && crop[pad]->width && crop[pad]->height) {
-			crop[pad]->width -= isp_sd->sink_pad_padding_w;
-			crop[pad]->height -= isp_sd->sink_pad_padding_h;
+			crop[pad]->width -= isp_sd->sink_pad_padding.width;
+			crop[pad]->height -= isp_sd->sink_pad_padding.height;
 		}
 
 		if (isp_sd->params.video_dis_en &&
drivers/staging/media/atomisp/pci/atomisp_subdev.h +1 -2
index b12bb65be..0045f380d 100644
@@ -214,8 +214,7 @@ struct atomisp_sub_device {
 	struct media_pad pads[ATOMISP_SUBDEV_PADS_NUM];
 	struct atomisp_pad_format fmt[ATOMISP_SUBDEV_PADS_NUM];
 	/* Padding for currently set sink-pad fmt */
-	u32 sink_pad_padding_w;
-	u32 sink_pad_padding_h;
+	struct v4l2_area sink_pad_padding;
 
 	unsigned int output;
 	struct atomisp_video_pipe video_out;
Signature · 1 line
2.53.0
#14 lore raw

PATCH v11 12/14 media: atomisp: Allow raw Bayer capture

In reply to Maurizio Casciano

AtomISP currently rejects raw formats and substitutes YUV420. Expose the supported raw Bayer formats so userspace can select unprocessed sensor frames with VIDIOC_S_FMT.

Enumerate the sensor's media-bus codes and pass the requested RAW code to discrete frame-size enumeration. Crop-capable sensors report their native RAW sizes without conversion padding. Match TRY_FMT to S_FMT when sensor negotiation falls back to native processed output.

RAW formats expose the sensor transport frame for the ISP2401 copy pipeline. Processed formats keep the existing padding rules.

Sashiko identified the negotiation and enumeration cases. Codex drafted the follow-up and exercised it with simulated sensor replies.

---
 .../staging/media/atomisp/pci/atomisp_cmd.c   |  36 ++++--
 .../staging/media/atomisp/pci/atomisp_ioctl.c | 106 +++++++++++++-----
 2 files changed, 106 insertions(+), 36 deletions(-)
drivers/staging/media/atomisp/pci/atomisp_cmd.c +26 -10
index c2978e587..bada0eec6 100644
@@ -3818,11 +3818,11 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 	struct atomisp_sub_device *asd = &isp->asd;
 	struct v4l2_mbus_framefmt ffmt = { };
 	struct v4l2_area padding;
+	bool retried = false;
 	int ret;
 
 	fmt = atomisp_get_format_bridge(f->pixelformat);
-	/* Currently, raw formats are broken!!! */
-	if (!fmt || fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
+	if (!fmt) {
 		f->pixelformat = V4L2_PIX_FMT_YUV420;
 
 		fmt = atomisp_get_format_bridge(f->pixelformat);
@@ -3840,23 +3840,24 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 	}
 
 	/*
-	 * atomisp_set_fmt() will set the sensor resolution to the requested
-	 * resolution + padding. Add padding here and remove it again after
-	 * the set_fmt call, like atomisp_set_fmt_to_snr() does.
+	 * Match S_FMT: only Bayer-to-processed conversion needs padding.
+	 * Start with the requested format and retry without padding if the
+	 * sensor supplies a native processed format.
 	 */
-	atomisp_get_pix_padding(isp, f, &padding);
+	if (fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
+		padding = (struct v4l2_area) { };
+	else
+		atomisp_get_pix_padding(isp, f, &padding);
+
+retry:
 	v4l2_fill_mbus_format(&ffmt, f, fmt->mbus_code);
 	ffmt.width += padding.width;
 	ffmt.height += padding.height;
 
-	dev_dbg(isp->dev, "try_mbus_fmt: try %ux%u\n", ffmt.width, ffmt.height);
-
 	ret = atomisp_set_sensor_crop_and_fmt(isp, &ffmt, V4L2_SUBDEV_FORMAT_TRY);
 	if (ret)
 		return ret;
 
-	dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n", ffmt.width, ffmt.height);
-
 	snr_fmt = atomisp_get_format_bridge_from_mbus(ffmt.code);
 	if (!snr_fmt) {
 		dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
@@ -3864,6 +3865,21 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 		return -EINVAL;
 	}
 
+	if (fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
+		fmt = snr_fmt;
+		f->pixelformat = fmt->pixelformat;
+	} else if (!atomisp_is_mbuscode_raw(ffmt.code) &&
+		   (padding.width || padding.height)) {
+		padding = (struct v4l2_area) { };
+		retried = true;
+		goto retry;
+	} else if (retried && atomisp_is_mbuscode_raw(ffmt.code)) {
+		return -EINVAL;
+	}
+
+	if (ffmt.width < padding.width || ffmt.height < padding.height)
+		return -EINVAL;
+
 	f->width = ffmt.width - padding.width;
 	f->height = ffmt.height - padding.height;
 
drivers/staging/media/atomisp/pci/atomisp_ioctl.c +80 -26

187 lines not on this page · raw message

Signature · 1 line
2.53.0
#15 lore raw

PATCH v11 13/14 media: i2c: Add WV517S lens actuator driver

In reply to Maurizio Casciano

The Lenovo Yoga Book YB1-X91 rear camera has a WV517S voice-coil actuator. Add a V4L2 lens subdevice with a standard 10-bit FOCUS_ABSOLUTE control. Set the ringing-control mode internally.

Defer register I/O until open or a focus request so runtime PM can use the IPU bridge sensor link to power the actuator. Use regmap and restore the last successfully written focus position on resume. With PM disabled, initialize the device on first use.

Hold a reference for each V4L2 registration until final subdevice release. Remove access to the hardware and balance the open runtime PM reference before unregistering the device. This depends on David Heidelberg's V4L2 core fix, which clears the devnode pointer before invoking the subdevice release callback.

Retain the Intel copyright notice for the register addresses and drive-mode value from the vendor WV517 driver. Sashiko identified the runtime PM and lifetime issues. Codex drafted the follow-up.

---
 MAINTAINERS                |   1 +
 drivers/media/i2c/Kconfig  |  11 ++
 drivers/media/i2c/Makefile |   1 +
 drivers/media/i2c/wv517s.c | 320 +++++++++++++++++++++++++++++++++++++
 4 files changed, 333 insertions(+)
 create mode 100644 drivers/media/i2c/wv517s.c
MAINTAINERS +1 -0
index f72ce07a4..2491353d8 100644
@@ -29324,6 +29324,7 @@ S:	Maintained
 F:	drivers/media/i2c/ak*
 F:	drivers/media/i2c/dw*
 F:	drivers/media/i2c/lm*
+F:	drivers/media/i2c/wv517s.c
 
 V4L2 CAMERA SENSOR DRIVERS
 M:	Sakari Ailus <[email protected]>
drivers/media/i2c/Kconfig +11 -0
index 162b8ea02..e97ef7749 100644
@@ -1046,6 +1046,17 @@ config VIDEO_DW9807_VCM
 	  capability. This is designed for linear control of
 	  voice coil motors, controlled via I2C serial interface.
 
+config VIDEO_WV517S
+	tristate "WV517S lens voice coil support"
+	select REGMAP_I2C
+	help
+	  This is a driver for the WV517S camera lens voice coil. The driver
+	  supports 10-bit focus control and exposes the actuator through the
+	  standard V4L2 lens sub-device interface.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called wv517s.
+
 endif
 
 menu "Flash devices"
drivers/media/i2c/Makefile +1 -0
index a750942e9..9a40d0dbc 100644
@@ -181,4 +181,5 @@ obj-$(CONFIG_VIDEO_VP27SMPX) += vp27smpx.o
 obj-$(CONFIG_VIDEO_VPX3220) += vpx3220.o
 obj-$(CONFIG_VIDEO_WM8739) += wm8739.o
 obj-$(CONFIG_VIDEO_WM8775) += wm8775.o
+obj-$(CONFIG_VIDEO_WV517S) += wv517s.o
 obj-$(CONFIG_VIDEO_INTEL_CVS) += cvs/
drivers/media/i2c/wv517s.c new file +320 -0

320 lines not on this page · raw message

Signature · 1 line
2.53.0
#17 lore raw

PATCH v11 14/14 media: atomisp: Preserve the capture graph when a lens rebinds

In reply to Maurizio Casciano

The root notifier completes again when the lens subdevice is rebound. Rebuilding the entire graph adds the original sensors a second time and attempts to register the capture video device again.

If the capture node already exists and the sensors are unchanged, register only missing subdevice nodes. Reject a changed sensor graph until the driver is reinitialized. The preceding V4L2 rollback fix keeps existing sensor nodes if new node registration fails. The X91L rebind trial exposed the original issue. Codex drafted the notifier change.

---
 .../media/atomisp/pci/atomisp_csi2_bridge.c     | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c +16 -1
index 56da454f0..72048de9b 100644
@@ -508,8 +508,23 @@ static void atomisp_notifier_unbind(struct v4l2_async_notifier *notifier,
 static int atomisp_notifier_complete(struct v4l2_async_notifier *notifier)
 {
 	struct atomisp_device *isp = notifier_to_atomisp(notifier);
+	unsigned int i;
 
-	return atomisp_register_device_nodes(isp);
+	if (!video_is_registered(&isp->asd.video_out.vdev))
+		return atomisp_register_device_nodes(isp);
+
+	/*
+	 * Completing the lens notifier again only needs a new lens node.
+	 * The existing graph is valid as long as its sensors are unchanged.
+	 */
+	for (i = 0; i < isp->input_cnt; i++) {
+		const struct atomisp_input_subdev *input = &isp->inputs[i];
+
+		if (input->csi_remote_source != isp->sensor_subdevs[input->port])
+			return -EBUSY;
+	}
+
+	return v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
 }
 
 static const struct v4l2_async_notifier_operations atomisp_async_ops = {
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v11 01/14] media: v4l2-core: Preserve existing subdevice nodes on rollback

In reply to Maurizio Casciano
Quoted 5 lines · Maurizio Casciano

Subdevice node registration skips nodes which already exist. On failure, however, it walks the subdevice list from the beginning and unregisters nodes regardless of whether the current call created them. On AtomISP this can remove the camera sensor nodes when a lens rebind fails.

Quoted 7 lines

Keep a list of the video devices registered by this call and unregister only those devices on failure. Record each device before creating its media interface link so a failure in that step rolls back the new node as well. Calls with no missing nodes need no bookkeeping allocation.

Codex drafted the change and tested allocation, registration and media-link failure using the registration helper with mocked operations.

And you didn't understand the change, because this is clearly not right. NAK.

Quoted 83 lines
Fixes db0f4691d974 ("[media] v4l: Allow calling v4l2_device_register_subdev_nodes() multiple times")
---
 drivers/media/v4l2-core/v4l2-device.c | 26 ++++++++++++++++++++------
 include/media/v4l2-device.h           |  3 +++
 2 files changed, 23 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-device.c +20 -6
index b4036105c..8cfca95ce 100644
@@ -191,10 +191,23 @@ static void v4l2_device_release_subdev_node(struct video_device *vdev)
 int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
 					bool read_only)
 {
+	unsigned int count = 0, registered = 0;
+	struct video_device **new_nodes;
 	struct video_device *vdev;
 	struct v4l2_subdev *sd;
 	int err;
 
+	list_for_each_entry(sd, &v4l2_dev->subdevs, list)
+		if ((sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE) && !sd->devnode)
+			count++;
+
+	if (!count)
+		return 0;
+
+	new_nodes = kcalloc(count, sizeof(*new_nodes), GFP_KERNEL);
+	if (!new_nodes)
+		return -ENOMEM;
+
 	/* Register a device node for every subdev marked with the
 	 * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
 	 */
@@ -228,6 +241,7 @@ int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
 			kfree(vdev);
 			goto clean_up;
 		}
+		new_nodes[registered++] = vdev;
 #if defined(CONFIG_MEDIA_CONTROLLER)
 		sd->entity.info.dev.major = VIDEO_MAJOR;
 		sd->entity.info.dev.minor = vdev->minor;
@@ -247,15 +261,15 @@ int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
 		}
 #endif
 	}
-	return 0;
+	err = 0;
+	goto out;
 
 clean_up:
-	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
-		if (!sd->devnode)
-			break;
-		video_unregister_device(sd->devnode);
-	}
+	while (registered)
+		video_unregister_device(new_nodes[--registered]);
 
+out:
+	kfree(new_nodes);
 	return err;
 }
 EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
include/media/v4l2-device.h +3 -0
index 25f69b1b8..6d8834d40 100644
@@ -183,6 +183,9 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
  * @read_only: subdevices read-only flag. True to register the subdevices
  *	device nodes in read-only mode, false to allow full access to the
  *	subdevice userspace API.
+ *
+ * Existing nodes are kept even if registration of a new node fails.
+ * The caller must serialize this operation with changes to the subdevice list.
  */
 int __must_check
 __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
Signature · 1 line
2.53.0
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

Re: [PATCH v11 01/14] media: v4l2-core: Preserve existing subdevice nodes on rollback

Hi Laurent,

Quoted 1 line

NAK.

Thanks for reviewing. The test only checked which nodes were removed. On re-reading the error path, I saw that unregistering a new node can invoke the subdevice release callback while the subdevice is still registered. The test didn't cover that ordering.

Is that the issue you had in mind? If you see a different problem, could you point me to the code path or assumption I missed?

With Best Regards,
Maurizio Casciano

#16 lore raw

Re: [PATCH v11 13/14] media: i2c: Add WV517S lens actuator driver

In reply to Maurizio Casciano
Quoted 15 lines · Maurizio Casciano

The Lenovo Yoga Book YB1-X91 rear camera has a WV517S voice-coil actuator. Add a V4L2 lens subdevice with a standard 10-bit FOCUS_ABSOLUTE control. Set the ringing-control mode internally.

Defer register I/O until open or a focus request so runtime PM can use the IPU bridge sensor link to power the actuator. Use regmap and restore the last successfully written focus position on resume. With PM disabled, initialize the device on first use.

Hold a reference for each V4L2 registration until final subdevice release. Remove access to the hardware and balance the open runtime PM reference before unregistering the device. This depends on David Heidelberg's V4L2 core fix, which clears the devnode pointer before invoking the subdevice release callback.

Quoted 4 lines

Retain the Intel copyright notice for the register addresses and drive-mode value from the vendor WV517 driver.

Sashiko identified the runtime PM and lifetime issues. Codex drafted the follow-up.

This line should be in the cover letter, no need to poison the commit message.

Quoted 3 lines

Do not add unreferred Link tags.

If you need a one, add it in a form of Link: $URL [1] and refer via [1] in the commit message.

...

drivers/media/i2c/Kconfig config VIDEO_DW9807_VCM · line 1050

Quoted 2 lines
drivers/media/i2c/Kconfig +2 -0
+config VIDEO_WV517S
+	tristate "WV517S lens voice coil support"

Make this...

drivers/media/i2c/Kconfig config VIDEO_DW9807_VCM

Quoted 11 lines
drivers/media/i2c/Kconfig +10 -0
+	select REGMAP_I2C
+	help
+	  This is a driver for the WV517S camera lens voice coil. The driver
+	  supports 10-bit focus control and exposes the actuator through the
+	  standard V4L2 lens sub-device interface.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called wv517s.
 
+/*
+ * WV517S voice-coil motor driver

...this...

Quoted 3 lines

+ * Copyright (c) 2014 Intel Corporation.
+ * Copyright (C) 2026 Maurizio Casciano <[email protected]>
+ */

...and this one

Quoted 1 line

+MODULE_DESCRIPTION("WV517S VCM driver");

to share the same or quite similar text.

Quoted 10 lines
drivers/media/i2c/wv517s.c new file +10 -0
+#include <linux/container_of.h>
+#include <linux/device.h>
+#include <linux/i2c.h>
+#include <linux/kref.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/types.h>

...

drivers/media/i2c/wv517s.c line 48

Quoted 13 lines
drivers/media/i2c/wv517s.c new file +12 -0
+struct wv517s_device {
+	struct v4l2_ctrl_handler ctrl_handler;
+	struct v4l2_subdev sd;
+	struct regmap *regmap;
+	struct kref ref;
+	/* Serialize hardware access and open/close against driver removal. */
+	struct mutex lock;
+	enum wv517s_registration registration;
+	unsigned int users;
+	u16 focus;
 
+	bool initialized;
+	bool removed;

If they are protected by the same lock, you can make them bitfields

	bool XXX:1;
Quoted 1 line

+};

...

drivers/media/i2c/wv517s.c line 60

Quoted 5 lines
drivers/media/i2c/wv517s.c new file +5 -0
+static const struct regmap_config wv517s_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 16,
+	.max_register = WV517S_REG_DRIVE_MODE,
+	.val_format_endian = REGMAP_ENDIAN_BIG,

No cache? Why?

Quoted 1 line

+};

...

drivers/media/i2c/wv517s.c line 66

Quoted 4 lines
drivers/media/i2c/wv517s.c new file +4 -0
+static int wv517s_resume(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct wv517s_device *wv517s = to_wv517s(sd);
	struct regmap *map = wv517s->regmap;

drivers/media/i2c/wv517s.c line 70

Quoted 4 lines
drivers/media/i2c/wv517s.c new file +4 -0
+	int ret;
+
+	ret = regmap_write(wv517s->regmap, WV517S_REG_DRIVE_MODE,
+			   WV517S_DRIVE_MODE_12_6_MS);
	ret = regmap_write(map, WV517S_REG_DRIVE_MODE, WV517S_DRIVE_MODE_12_6_MS);

(I believe it's fine to have 83 characters in this case.)

drivers/media/i2c/wv517s.c line 79

Quoted 9 lines
drivers/media/i2c/wv517s.c new file +9 -0
+	if (ret)
+		return ret;
+
+	/*
+	 * Every update of focus holds a runtime PM reference. Do not acquire
+	 * the driver's mutex here: the caller may hold it while resuming.
+	 */
+	return regmap_write(wv517s->regmap, WV517S_REG_FOCUS,
+			    READ_ONCE(wv517s->focus));
	return regmap_write(map, WV517S_REG_FOCUS, READ_ONCE(wv517s->focus));
Quoted 1 line

+}

You may use the same trick (temporary variable for regmap) elsewhere.

...

Quoted 6 lines
drivers/media/i2c/wv517s.c new file +6 -0
+static int wv517s_power_get(struct wv517s_device *wv517s)
+{
+	struct device *dev = wv517s->sd.dev;
+	int ret;
+
+	if (IS_ENABLED(CONFIG_PM))

drivers/media/i2c/wv517s.c line 99

Quoted 10 lines
drivers/media/i2c/wv517s.c new file +9 -0
+		return pm_runtime_resume_and_get(dev);
+
+	if (wv517s->initialized)
+		return 0;
 
+	ret = wv517s_resume(dev);
+	if (!ret)
+		wv517s->initialized = true;
+
+	return ret;

Standard pattern is

	if (ret)
		return ret;

	wv517s->initialized = true;
	return 0;
Quoted 1 line

+}

...

drivers/media/i2c/wv517s.c line 111

Quoted 10 lines
drivers/media/i2c/wv517s.c new file +10 -0
+static int wv517s_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct wv517s_device *wv517s =
+		container_of(ctrl->handler, struct wv517s_device, ctrl_handler);
+	int ret;
+
+	if (ctrl->id != V4L2_CID_FOCUS_ABSOLUTE)
+		return -EINVAL;
+
+	guard(mutex)(&wv517s->lock);

Add a blank line here, so it will be visible that guard()() is used. Do the same elsewhere.

Quoted 14 lines
drivers/media/i2c/wv517s.c new file +14 -0
+	if (wv517s->removed)
+		return -ENODEV;
+
+	ret = wv517s_power_get(wv517s);
+	if (ret)
+		return ret;
+
+	ret = regmap_write(wv517s->regmap, WV517S_REG_FOCUS, ctrl->val);
+	if (!ret)
+		WRITE_ONCE(wv517s->focus, ctrl->val);
+
+	pm_runtime_put(wv517s->sd.dev);
+	return ret;
+}

...

Quoted 5 lines
drivers/media/i2c/wv517s.c new file +5 -0
+static int wv517s_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct wv517s_device *wv517s;
+	int ret;

drivers/media/i2c/wv517s.c line 245

Quoted 10 lines
drivers/media/i2c/wv517s.c new file +10 -0
+
+	wv517s = kzalloc_obj(*wv517s);
+	if (!wv517s)
+		return -ENOMEM;
+
+	wv517s->regmap = devm_regmap_init_i2c(client, &wv517s_regmap_config);
+	if (IS_ERR(wv517s->regmap)) {
+		ret = dev_err_probe(dev, PTR_ERR(wv517s->regmap),
+				    "failed to initialize regmap\n");
+		kfree(wv517s);

Huh?! This a mess with devm. Use devm_kzalloc_obj() instead.

drivers/media/i2c/wv517s.c line 249

Quoted 4 lines
drivers/media/i2c/wv517s.c new file +4 -0
+		return ret;
+	}
+
+	kref_init(&wv517s->ref);

Move it after mutex, because it's not managed.

drivers/media/i2c/wv517s.c line 250

Quoted 1 line
drivers/media/i2c/wv517s.c new file +1 -0
+	mutex_init(&wv517s->lock);

devm_mutex_init()

Quoted 5 lines
drivers/media/i2c/wv517s.c new file +5 -0
+	wv517s->focus = WV517S_DEFAULT_FOCUS_POSITION;
+	v4l2_i2c_subdev_init(&wv517s->sd, client, &wv517s_subdev_ops);
+	wv517s->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+	wv517s->sd.internal_ops = &wv517s_internal_ops;
+	wv517s->sd.entity.function = MEDIA_ENT_F_LENS;

drivers/media/i2c/wv517s.c line 264

Quoted 10 lines
drivers/media/i2c/wv517s.c new file +9 -0
+
+	v4l2_ctrl_handler_init(&wv517s->ctrl_handler, 1);
+	v4l2_ctrl_new_std(&wv517s->ctrl_handler, &wv517s_ctrl_ops,
+			  V4L2_CID_FOCUS_ABSOLUTE, 0,
+			  WV517S_MAX_FOCUS_POSITION, 1,
+			  WV517S_DEFAULT_FOCUS_POSITION);
+	if (wv517s->ctrl_handler.error) {
+		ret = wv517s->ctrl_handler.error;
 
+		goto err_free_ctrl_handler;

No goto should come after devm_*() call (there are some rare exceptions possible, but they should be understood and commented).

Quoted 26 lines
drivers/media/i2c/wv517s.c new file +25 -0
+	}
+	wv517s->sd.ctrl_handler = &wv517s->ctrl_handler;
+
+	ret = media_entity_pads_init(&wv517s->sd.entity, 0, NULL);
+	if (ret)
+		goto err_free_ctrl_handler;
+
+	/* Leave the actuator suspended until a consumer requests focus. */
+	pm_runtime_enable(dev);
+
+	ret = v4l2_async_register_subdev(&wv517s->sd);
+	if (ret) {
+		wv517s_stop(wv517s);
+		kref_put(&wv517s->ref, wv517s_free);
+		return ret;
+	}
+
+	return 0;
+
+err_free_ctrl_handler:
+	v4l2_ctrl_handler_free(&wv517s->ctrl_handler);
 
+	mutex_destroy(&wv517s->lock);
+	kfree(wv517s);
+	return ret;
+}

drivers/media/i2c/wv517s.c line 299

Quoted 10 lines
drivers/media/i2c/wv517s.c new file +10 -0
+
+static void wv517s_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct wv517s_device *wv517s = to_wv517s(sd);
+
+	wv517s_stop(wv517s);
+	v4l2_async_unregister_subdev(sd);
+	kref_put(&wv517s->ref, wv517s_free);
+}

I don't know where you got the idea with kref, but not so many media drivers use it. And actually none of the existing voice-coil ones. I believe all this mess comes from AI and misunderstanding of how it should be done.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw

Re: [PATCH v11 14/14] media: atomisp: Preserve the capture graph when a lens rebinds

In reply to Maurizio Casciano
Quoted 11 lines · Maurizio Casciano

The root notifier completes again when the lens subdevice is rebound. Rebuilding the entire graph adds the original sensors a second time and attempts to register the capture video device again.

If the capture node already exists and the sensors are unchanged, register only missing subdevice nodes. Reject a changed sensor graph until the driver is reinitialized. The preceding V4L2 rollback fix keeps existing sensor nodes if new node registration fails. The X91L rebind trial exposed the original issue. Codex drafted the notifier change.

Don't put unreferred Link tags.

...

Quoted 5 lines

static int atomisp_notifier_complete(struct v4l2_async_notifier *notifier) {

drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c +1 -0
 	struct atomisp_device *isp = notifier_to_atomisp(notifier);
+	unsigned int i;
 

drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c static void atomisp_notifier_unbind(struct v4l2_async_notifier *notifier,

Quoted 10 lines
drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c +8 -1
-	return atomisp_register_device_nodes(isp);
+	if (!video_is_registered(&isp->asd.video_out.vdev))
+		return atomisp_register_device_nodes(isp);
+
+	/*
+	 * Completing the lens notifier again only needs a new lens node.
+	 * The existing graph is valid as long as its sensors are unchanged.
+	 */
 
+	for (i = 0; i < isp->input_cnt; i++) {
	for (unsigned int i = 0; i < isp->input_cnt; i++) {
Quoted 8 lines
drivers/staging/media/atomisp/pci/atomisp_csi2_bridge.c +7 -0
+		const struct atomisp_input_subdev *input = &isp->inputs[i];
+
+		if (input->csi_remote_source != isp->sensor_subdevs[input->port])
+			return -EBUSY;
+	}
+
+	return v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko