Re: [PATCH] media: v4l2-async: link ancillary device runtime PM to the sensor's
From: Hans de Goede
Date: Thu Dec 03 2026 - 11:26:28 EST
Hi,
On 30-Nov-26 15:42, Hans de Goede wrote:
> Hi,
>
> On 22-Sep-26 21:39, Cédric Bellegarde wrote:
>> When a sensor's fwnode references an ancillary lens or flash device
>> (e.g. via the "lens-focus" or "flash-leds" properties),
>> v4l2_async_create_ancillary_links() already creates a media controller
>> link between the two entities, but their runtime PM states remain
>> independent.
>>
>> This is a problem for devices such as VCM lens actuators, which are
>> typically spring-loaded: holding a position away from the spring's
>> rest point requires continuous power, and the position is not retained
>> once power is cut. If such an actuator is allowed to runtime-suspend
>> independently of the sensor, the lens can drift back to its rest
>> position during an otherwise active capture session.
>
> In my experience at the moment we actually mostly have the reverse problem,
> on x86_64 devices a runtime-pm link is created from the VCM device
> to the sensor device, because they are both powered on through the ACPI
> power-on sequences from the sensor.
>
> And VCM drivers power-on the VCM as soon as the /dev/v4l-subdev# device
> for the VCM is opened, which now also powers the sensor on leading to
> undesirable power-consumption especially since libcamera typically opens
> all v4l-subdev# devices as soon as a libcamera context is created which
> e.g. pipewire does.
>
> I do like this patch though since I think it can solve all of the above
> problems in one go.
>
> What we are seeing on x86 at least is that camera-modules are designed
> with regulators, but also e.g. power-down / reset pins shared between
> sensor and VCM, assuming that the sensor + vcm are always powered on/off
> together.
>
> And sensors are already powered-on only when streaming (vs the VCM
> drivers powering them on as soon as the /dev/v4l-subdev# device is open).
>
> So what I think we should do is:
>
> 1. Add this patch to always automatically power-up the VCM when a sensor
> powers up (so when streaming for proper sensor drivers)
>
> 2. Drop the runtime_pm_get() and put() calls from the open()/close()
> v4l2_subdev_internal_ops for VCM devices so that they no longer are
> always on as soon as userspace has the /dev/v4l-subdev# VCM node open.
>
> This also requires no longer probing / powering up during probe() as
> this will fail when patch 3 is added into the mix.
>
> 3. Drop the runtime-pm link added by drivers/media/pci/intel/ipu-bridge.c,
> removing:
>
> device_link_add(&vcm_client->dev, data->sensor, DL_FLAG_PM_RUNTIME);
>
> which is in essence the same link from this patch in the other direction.
> Note:
>
> 3a) This needs to be last to avoid regressions.
> 3b) Maybe we should not drop this, avoiding the need for changes to
> probe() in VCM drivers. Although IMHO just delaying the VCM detect
> code till first power-on would be better.
>
> Assuming we can all agree as the way forward to solve the power-management
> issues surrounding VCM devices then a series implementing this would be very
> welcome!
Ok, so thinking more about this I'm afraid that this is not going to work.
The problem is that on x86 the sensor must be powered-up *first* since some
of the rails, etc. are only described on the sensor side. So the existing
runtime-pm link from VCM to sensor on x86 is correct.
But we do want to get rid of the auto power-on on /dev/v4l-subdev open and
instead only power-on together with the sensor, but *after* the sensor's
runtime-resume method has run.
So what I think we should do is resurrect this patch-set from me:
https://lore.kernel.org/linux-media/202409a01211834.145186-1-hdegoede@xxxxxxxxxx/
One of the objections against that one was (ab)using the s_stream() API:
".s_stream() doesn't conceptually make sense for VCMs. Furthermore,
.s_stream() is being replaced with .enable_streams() and
.disable_streams(), which will make even less sense. We need a different
API."
The other objection was about the design of it. But based on the libcamera
softISP from last week where there seemed to be consensus that powering
the sensor and VCM on together is what we want since the hw is often
designed with that in mind and powering on only 1 of the 2 often leads
to problems like the unpowered device blocking the i2c bus.
So my plan is to write + submit a new version of the linked patch,
but then use pm_runtime_resume_and_get() on the VCM's v4l2_subdev.dev
instead of s_stream(). As suggested by Sakari in his review of
the linked patch.
Combined with a suitable autosuspend delay on VCM drivers opting
in to this behavior so that the VCM does not turn off + on again
on a quick stream stop + start to change the sensor mode.
Regards,
Hans
p.s.
We could go further then this and make the v4l2-core also do
the pm_runtime_resume_and_get() for the sensor itself on stream-on
this way we can runtime-resume both before doing any I2C transfers,
which is really what we want because of the 2 being so tightly coupled.
And also moving the runtime-pm management into the core instead
of having every sensor driver do this itself on stream on seems
to make sense to me.
>
>
>
>
>
>> Add V4L2_SUBDEV_FL_PM_LINK to allow an ancillary subdevice to request
>> that its runtime PM state be linked to the associated sensor.
>>
>> Signed-off-by: Cédric Bellegarde <cedric.bellegarde@xxxxxxxxxxxx>
>> ---
>> Link runtime PM of ancillary devices such as lens actuators to their associated sensor,
>> while allowing actuators to autosuspend when idle.
>>
>> Example usage:
>> https://gitlab.com/gnumdk/linux/-/commit/82b2d71415a1d95b2170e19ce025afc8bbdcf145
>> ---
>> drivers/media/v4l2-core/v4l2-async.c | 18 ++++++++++++++----
>> include/media/v4l2-subdev.h | 7 +++++++
>> 2 files changed, 21 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/media/v4l2-core/v4l2-async.c b/drivers/media/v4l2-core/v4l2-async.c
>> index 460bf3dbbb88..22df627153b8 100644
>> --- a/drivers/media/v4l2-core/v4l2-async.c
>> +++ b/drivers/media/v4l2-core/v4l2-async.c
>> @@ -318,6 +318,7 @@ static int v4l2_async_create_ancillary_links(struct v4l2_async_notifier *n,
>> {
>> #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
>> struct media_link *link;
>> + struct device_link *devlink;
>>
>> if (sd->entity.function != MEDIA_ENT_F_LENS &&
>> sd->entity.function != MEDIA_ENT_F_FLASH)
>> @@ -331,11 +332,20 @@ static int v4l2_async_create_ancillary_links(struct v4l2_async_notifier *n,
>> }
>>
>> link = media_create_ancillary_link(&n->sd->entity, &sd->entity);
>> -
>> - return IS_ERR(link) ? PTR_ERR(link) : 0;
>> -#else
>> - return 0;
>> + if (IS_ERR(link))
>> + return PTR_ERR(link);
>> +
>> + if (sd->flags & V4L2_SUBDEV_FL_PM_LINK) {
>> + devlink = device_link_add(n->sd->dev, sd->dev,
>> + DL_FLAG_PM_RUNTIME |
>> + DL_FLAG_AUTOREMOVE_CONSUMER);
>> + if (!devlink)
>> + dev_warn(notifier_dev(n),
>> + "failed to link power management of %s to %s\n",
>> + dev_name(sd->dev), dev_name(n->sd->dev));
>> + }
>> #endif
>> + return 0;
>> }
>>
>> static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
>> diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
>> index d256b7ec8f84..9f64b10afd10 100644
>> --- a/include/media/v4l2-subdev.h
>> +++ b/include/media/v4l2-subdev.h
>> @@ -972,6 +972,13 @@ struct v4l2_subdev_internal_ops {
>> * - Multiple streams per pad are supported
>> */
>> #define V4L2_SUBDEV_FL_STREAMS (1U << 4)
>> +/*
>> + * Set this flag to keep the subdevice active while its associated sensor is active.
>> + *
>> + * This is intended for ancillary devices, such as lens actuators, whose
>> + * hardware state or physical position cannot be retained while powered off.
>> + */
>> +#define V4L2_SUBDEV_FL_PM_LINK (1U << 5)
>>
>> struct regulator_bulk_data;
>>
>>
>> ---
>> base-commit: df2908090cda368b01ff43709f51890076c56157
>> change-id: 20260911-sensors_pm-787eac01aa18
>>
>> Best regards,
>