From: Mattijs Korpershoek <mkorpershoek@kernel.org>
To: Hans Verkuil <hverkuil+cisco@kernel.org>,
Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
Kieran Bingham <kieran.bingham@ideasonboard.com>,
Sakari Ailus <sakari.ailus@linux.intel.com>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
Andy Walls <awalls@md.metrocast.net>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Mattijs Korpershoek <mkorpershoek@kernel.org>,
Nicolas Dufresne <nicolas.dufres@collabora.com>,
Hans de Goede <hansg@kernel.org>,
Michael Riesch <michael.riesch@collabora.com>,
Dave Stevenson <dave.stevenson@raspberrypi.com>,
Maxime Ripard <mripard@kernel.org>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH RFC v2 16/16] media: imx219: Implement .detect() sensor operation
Date: Wed, 18 Nov 2026 13:44:00 +0100 [thread overview]
Message-ID: <20261118-v4l2-sensor-detect-v2-16-1bb1a445b556@kernel.org> (raw)
In-Reply-To: <20261118-v4l2-sensor-detect-v2-0-1bb1a445b556@kernel.org>
Now that the driver can probe() with a missing sensor, userspace should
be able to query the sensor connection state via the media device.
Implement the .detect() sensor operation so that userspace can do that
using the MEDIA_IOC_G_ENTITY_STATUS ioctl.
Also call media_detect_poll_enable() when streaming successfully started
and poll_disable() when disabling streaming to make sure that the media
device reports the status to userspace while streaming.
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
drivers/media/i2c/imx219.c | 47 +++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 42 insertions(+), 5 deletions(-)
diff --git a/drivers/media/i2c/imx219.c b/drivers/media/i2c/imx219.c
index 02d065fb88d0..5d4432a97eee 100644
--- a/drivers/media/i2c/imx219.c
+++ b/drivers/media/i2c/imx219.c
@@ -28,6 +28,7 @@
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-mediabus.h>
+#include <media/v4l2-subdev.h>
/* Chip ID */
#define IMX219_REG_CHIP_ID CCI_REG16(0x0000)
@@ -742,6 +743,10 @@ static int imx219_enable_streams(struct v4l2_subdev *sd,
if (ret < 0)
return ret;
+ ret = media_entity_detect_or_fail(&sd->entity);
+ if (ret)
+ goto err_rpm_put;
+
/* Send all registers that are common to all modes */
ret = cci_multi_reg_write(imx219->regmap, imx219_common_regs,
ARRAY_SIZE(imx219_common_regs), NULL);
@@ -780,6 +785,8 @@ static int imx219_enable_streams(struct v4l2_subdev *sd,
__v4l2_ctrl_grab(imx219->vflip, true);
__v4l2_ctrl_grab(imx219->hflip, true);
+ media_detect_poll_enable(&sd->entity);
+
return 0;
err_rpm_put:
@@ -795,6 +802,8 @@ static int imx219_disable_streams(struct v4l2_subdev *sd,
struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
int ret;
+ media_detect_poll_disable(&sd->entity);
+
/* set stream off register */
ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
IMX219_MODE_STANDBY, NULL);
@@ -1006,6 +1015,33 @@ static int imx219_init_state(struct v4l2_subdev *sd,
return imx219_set_pad_format(sd, NULL, state, &fmt);
}
+static int imx219_read_chip_id(struct imx219 *imx219, u64 *chip_id)
+{
+ return cci_read(imx219->regmap, IMX219_REG_CHIP_ID, chip_id, NULL);
+}
+
+static int imx219_detect(struct media_entity *entity)
+{
+ struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
+ struct i2c_client *client = v4l2_get_subdevdata(sd);
+ struct imx219 *imx219 = to_imx219(sd);
+ u64 chip_id;
+ int ret;
+
+ ret = pm_runtime_resume_and_get(&client->dev);
+ if (ret < 0)
+ return ret;
+
+ if (imx219_read_chip_id(imx219, &chip_id))
+ ret = MEDIA_ENT_STATUS_DISCONNECTED;
+ else
+ ret = MEDIA_ENT_STATUS_CONNECTED;
+
+ pm_runtime_put_autosuspend(&client->dev);
+
+ return ret;
+}
+
static const struct v4l2_subdev_video_ops imx219_video_ops = {
.s_stream = v4l2_subdev_s_stream_helper,
};
@@ -1029,6 +1065,10 @@ static const struct v4l2_subdev_internal_ops imx219_internal_ops = {
.init_state = imx219_init_state,
};
+static const struct media_entity_operations imx219_entity_ops = {
+ .detect = imx219_detect,
+};
+
/* -----------------------------------------------------------------------------
* Power management
*/
@@ -1099,11 +1139,6 @@ static int imx219_get_regulators(struct imx219 *imx219)
imx219->supplies);
}
-static int imx219_read_chip_id(struct imx219 *imx219, u64 *chip_id)
-{
- return cci_read(imx219->regmap, IMX219_REG_CHIP_ID, chip_id, NULL);
-}
-
static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
{
struct fwnode_handle *endpoint;
@@ -1237,6 +1272,7 @@ static int imx219_probe(struct i2c_client *client)
goto error_power_off;
}
dev_dbg(&client->dev, "failed to read chip id %x\n", IMX219_CHIP_ID);
+ imx219->sd.entity.status = MEDIA_ENT_STATUS_DISCONNECTED;
} else if (val != IMX219_CHIP_ID) {
ret = dev_err_probe(&client->dev, -EIO,
"chip id mismatch: %x!=%llx\n",
@@ -1271,6 +1307,7 @@ static int imx219_probe(struct i2c_client *client)
/* Initialize subdev */
imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+ imx219->sd.entity.ops = &imx219_entity_ops;
/* Initialize source pad */
imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
--
2.56.0
prev parent reply other threads:[~2026-11-18 12:44 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 01/16] media: mc: Add struct media_device_fh pointer to media device IOCTL op Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 02/16] media: mc: Add per-file-handle data support Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 03/16] v4l: event: Separate event related information from struct v4l2_fh Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 04/16] v4l: event: Rework data structures for separating events and V4L2 Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 05/16] v4l: event: Split the event framework into generic and V4L2 bits Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 06/16] media: mc: Maintain a list of open file handles in a media device Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 07/16] media: mc: Add MEDIA_IOC_DQEVENT Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 08/16] media: mc: Add MEDIA_EVENT_ENTITY_REGISTRATION Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 09/16] Documentation: media: Add Media controller event documentation Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 10/16] media: media.h: Add entity connection status Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 11/16] media: mc: Add .detect() to media_entity_operations Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 12/16] media: mc: Add MEDIA_IOC_G_ENTITY_STATUS ioctl Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 13/16] media: mc: Add MEDIA_EVENT_ENTITY_STATUS_CHANGE event Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 14/16] media: mc: Add detect polling helpers for drivers Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 15/16] media: imx219: Allow driver probe with missing sensor Mattijs Korpershoek
2026-11-18 12:44 ` Mattijs Korpershoek [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118-v4l2-sensor-detect-v2-16-1bb1a445b556@kernel.org \
--to=mkorpershoek@kernel.org \
--cc=awalls@md.metrocast.net \
--cc=dave.stevenson@raspberrypi.com \
--cc=gustavoars@kernel.org \
--cc=hansg@kernel.org \
--cc=hverkuil+cisco@kernel.org \
--cc=kees@kernel.org \
--cc=kieran.bingham@ideasonboard.com \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=michael.riesch@collabora.com \
--cc=mripard@kernel.org \
--cc=nicolas.dufres@collabora.com \
--cc=sakari.ailus@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®