From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 31C0BC43458 for ; Mon, 29 Jun 2026 08:47:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=tbcsr9OeYOyGVXBKcW5DPQK0aWjHb2qNxYH4mK/JEnM=; b=wwwB+VMsfavrWU3fQFwO8K9xJI XR6lXCRLN4EuuPLW8O8jGdAjQ8uWhXLwfhdVxgsUJCpRhPh5A52R6d/FhRNjlj98WfZQ4qDxP4HMw LOpZbqo8+lcV/9JcQWdDZuU/2INSPqnFH36H5naHnfxaTDNQZwVUQIX2ZpHCgS8b4CkNAPfJzQJCX DFHoHDYCOvKp3XP2cQj+C2cKSNu8T7IzDCgZpwB9hGy50mpeLc7IaacXT4ki5YBMVDJxJmJOybBEg pTGPLtwfOK9VhDaXnC1yn/44nfokF0JT22dpamgGqti1m9/nswPyLi0KTUcJdD865qsfFozqOOosY oE97uFGA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1we7ds-0000000E1oS-0pmm; Mon, 29 Jun 2026 08:47:00 +0000 Received: from perceval.ideasonboard.com ([213.167.242.64]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1we7do-0000000E1nf-400I for linux-arm-kernel@lists.infradead.org; Mon, 29 Jun 2026 08:46:58 +0000 Received: from killaraus.ideasonboard.com (2001-14ba-70f3-e800--a06.rev.dnainternet.fi [IPv6:2001:14ba:70f3:e800::a06]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6FEA812F; Mon, 29 Jun 2026 10:46:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1782722772; bh=Sn6/pnvMV+7cTSNOzKy0Jhpp/BOj27Lxn1QBk+3onIU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bRRdsd0ZsSf7fogMbtUYfbhUgfsQVwCCWZMWTcDEYFOv/+OytRbbANuM5dUqzbuy3 AiceQ6X0uU/iuq6kVZtojs33momkbdMk0wZ48tpch4aYrjGbtUZEHWaQrJjQaMyDXT W3pV5fPerzMH5K9W95hHM/hjKRT3TWFzf5dlx7os= Date: Mon, 29 Jun 2026 11:46:54 +0300 From: Laurent Pinchart To: Frank.Li@oss.nxp.com Cc: Sakari Ailus , Mauro Carvalho Chehab , Michael Riesch , Frank Li , Martin Kepplinger-Novakovic , Rui Miguel Silva , Purism Kernel Team , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, imx@lists.linux.dev, Guoniu Zhou , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v6 3/9] media: subdev: Add media_async_register_subdev() helper Message-ID: <20260629084654.GB3054459@killaraus.ideasonboard.com> References: <20260624-imx8qxp_pcam-v6-0-4b3f45920d2f@nxp.com> <20260624-imx8qxp_pcam-v6-3-4b3f45920d2f@nxp.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20260624-imx8qxp_pcam-v6-3-4b3f45920d2f@nxp.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260629_014657_141930_1FB3C435 X-CRM114-Status: GOOD ( 31.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Jun 24, 2026 at 04:37:50PM -0400, Frank.Li@oss.nxp.com wrote: > From: Frank Li > > Add media_async_register_subdev(), a helper to register a V4L2 sub-device > with the asynchronous sub-device framework. For the reason stated by Sakari in patch 1/9 (dependency from MC to V4L2), I don't think a "media_async_register_subdev()" function is a good idea. > The helper assumes a 1:1 mapping between firmware endpoints and media pads. > During registration it parses the firmware graph, creates media pads for > all endpoints, and registers common asynchronous notifiers for sink > endpoints. These notifiers automatically create media links when the > corresponding remote source devices become available. > > The set_pad_by_ep() callback allows drivers to determine the media pad > associated with a firmware endpoint and identify whether the endpoint > represents a sink pad. > > By centralizing firmware graph parsing, media pad creation, notifier > registration, and link creation, this helper reduces duplicated code and > simplifies error handling in V4L2 sub-device drivers. > > Signed-off-by: Frank Li > --- > change in v6 > - new patch > --- > drivers/media/v4l2-core/v4l2-fwnode.c | 155 ++++++++++++++++++++++++++++++++++ > include/media/v4l2-async.h | 39 +++++++++ > 2 files changed, 194 insertions(+) > > diff --git a/drivers/media/v4l2-core/v4l2-fwnode.c b/drivers/media/v4l2-core/v4l2-fwnode.c > index 62a3a452f7884..169059654478f 100644 > --- a/drivers/media/v4l2-core/v4l2-fwnode.c > +++ b/drivers/media/v4l2-core/v4l2-fwnode.c > @@ -26,6 +26,7 @@ > > #include > #include > +#include > #include > > #include "v4l2-subdev-priv.h" > @@ -1302,6 +1303,160 @@ int __v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd, struct module *m > } > EXPORT_SYMBOL_GPL(__v4l2_async_register_subdev_sensor); > > +static int v4l2_common_notifier_bound(struct v4l2_async_notifier *notifier, > + struct v4l2_subdev *sd, > + struct v4l2_async_connection *asd) > +{ > + struct media_pad *pad = NULL; > + int ret; > + > + if (asd->match.type != V4L2_ASYNC_MATCH_TYPE_FWNODE) > + return -EINVAL; > + > + if (!asd->match.fwnode) > + return -EINVAL; > + > + struct fwnode_handle *remote __free(fwnode_handle) = > + fwnode_graph_get_remote_endpoint(asd->match.fwnode); > + > + for (int i = 0; i < notifier->sd->entity.num_pads; i++) { > + if (notifier->sd->entity.pads[i].vep.base.local_fwnode == remote) { > + pad = ¬ifier->sd->entity.pads[i]; > + break; > + } > + } > + > + if (!pad) { > + dev_err(notifier->sd->dev, "failed to find sink pad\n"); > + return -EINVAL; > + } > + > + ret = v4l2_create_fwnode_links_to_pad(sd, pad, MEDIA_LNK_FL_ENABLED); > + if (ret) { > + dev_err(sd->dev, "failed to link source pad\n"); > + return ret; > + } > + > + return 0; > +} > + > +static const struct v4l2_async_notifier_operations v4l2_common_notifier_ops = { > + .bound = v4l2_common_notifier_bound, > +}; > + > +static int > +v4l2_async_nf_parse_fwnode(struct device *dev, struct media_pad *pads, > + struct v4l2_async_notifier *notifier) > +{ > + struct v4l2_subdev *sd = notifier->sd; > + struct v4l2_async_connection *asd; > + struct media_pad *pad; > + int ret; > + > + if (!sd->internal_ops->set_pad_by_ep) > + return dev_err_probe(dev, -EINVAL, > + "Missed valiate_endpoint() callback\n"); > + pad = pads; > + > + fwnode_graph_for_each_endpoint_scoped(dev_fwnode(dev), ep) { > + u32 flags; > + > + ret = v4l2_fwnode_endpoint_parse(ep, &pad->vep); > + if (ret) > + return dev_err_probe(dev, ret, "failed to parse endpoint\n"); > + > + ret = sd->internal_ops->set_pad_by_ep(sd, pad); > + if (ret < 0) > + return dev_err_probe(dev, ret, "Can support endponit\n"); > + > + flags = pad->flags; > + > + pad++; > + > + if (flags & MEDIA_PAD_FL_SOURCE) > + continue; /* Bypass source port */ > + > + notifier->ops = &v4l2_common_notifier_ops; > + > + asd = v4l2_async_nf_add_fwnode_remote(notifier, ep, > + struct v4l2_async_connection); > + if (IS_ERR(asd)) > + return dev_err_probe(dev, PTR_ERR(asd), > + "failed to add notifier\n"); > + } > + > + return 0; > +} > + > +void media_async_subdev_cleanup(struct v4l2_subdev *sd) > +{ > + v4l2_async_unregister_subdev(sd); > + v4l2_subdev_cleanup(sd); > + media_entity_cleanup(&sd->entity); > + v4l2_async_nf_unregister(sd->subdev_notifier); > + v4l2_async_nf_cleanup(sd->subdev_notifier); > + kfree(sd->entity.pads); > +} > +EXPORT_SYMBOL_GPL(media_async_subdev_cleanup); > + > +int __media_async_register_subdev(struct v4l2_subdev *sd, struct module *module) > +{ > + struct device *dev = sd->dev; > + u32 ep_count; > + int ret; > + > + if (WARN_ON(!sd->dev)) > + return -ENODEV; > + > + struct v4l2_async_notifier *notifier __free(kfree) = kzalloc_obj(*notifier); > + if (!notifier) > + return -ENOMEM; > + > + v4l2_async_subdev_nf_init(notifier, sd); > + > + ep_count = fwnode_graph_get_endpoint_count(dev_fwnode(dev), 0); > + if (!ep_count) > + return dev_err_probe(dev, -EINVAL, "No connected endpoints\n"); > + > + struct media_pad *pads __free(kfree) = kzalloc_objs(struct media_pad, ep_count); > + if (!pads) > + return -ENOMEM; > + > + ret = v4l2_async_nf_parse_fwnode(dev, pads, notifier); > + if (ret < 0) > + return ret; > + > + ret = media_entity_pads_init(&sd->entity, ep_count, pads); > + if (ret) > + goto out_cleanup; > + > + ret = v4l2_async_nf_register(notifier); > + if (ret < 0) > + goto out_cleanup; > + > + ret = v4l2_subdev_init_finalize(sd); > + if (ret) > + goto out_unregister; > + > + ret = __v4l2_async_register_subdev(sd, module); > + if (ret < 0) > + goto out_unregister; > + > + sd->subdev_notifier = no_free_ptr(notifier); > + retain_and_null_ptr(pads); > + > + return 0; > + > +out_unregister: > + v4l2_async_nf_unregister(notifier); > + > +out_cleanup: > + v4l2_async_nf_cleanup(notifier); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(__media_async_register_subdev); > + > MODULE_DESCRIPTION("V4L2 fwnode binding parsing library"); > MODULE_LICENSE("GPL"); > MODULE_AUTHOR("Sakari Ailus "); > diff --git a/include/media/v4l2-async.h b/include/media/v4l2-async.h > index 54a2d9620ed5b..ca41820f776c5 100644 > --- a/include/media/v4l2-async.h > +++ b/include/media/v4l2-async.h > @@ -345,4 +345,43 @@ __v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd, struct module *modul > * @sd: pointer to &struct v4l2_subdev > */ > void v4l2_async_unregister_subdev(struct v4l2_subdev *sd); > + > +enum v4l2_subdev_1to1_pads { > + V4L2_SUBDEV_1TO1_PADS_SINK, > + V4L2_SUBDEV_1TO1_PADS_SOURCE, > + V4L2_SUBDEV_1TO1_PADS_TOTAL, > +}; > + > +/** > + * media_async_register_subdev - registers a sub-device to the asynchronous > + * sub-device framework and parse set up common > + * related devices > + * > + * @sd: pointer to struct &v4l2_subdev > + * > + * Register a V4L2 sub-device with the asynchronous sub-device framework. > + * In addition to v4l2_async_register_subdev(), this function parses the > + * firmware graph, creates media pads for the endpoints, and registers common > + * notifiers to create media links between connected devices. > + * > + * This function also init media_pads. > + * > + * The sub-device is unregistered and cleanup by media_async_subdev_cleanup() > + * > + * While registered, the subdev module is marked as in-use. > + * > + * An error is returned if the module is no longer loaded on any attempts > + * to register it. > + */ > +#define media_async_register_subdev(sd_1to1) \ > + __media_async_register_subdev(sd_1to1, THIS_MODULE) > + > +int __media_async_register_subdev(struct v4l2_subdev *sd_1to1, struct module *module); > + > +/** > + * media_async_subdev_cleanup - unregistered and cleanup subdev and media pads > + * @sd_1to1: pointer to struct &v4l2_subdev_1to1 > + */ > +void media_async_subdev_cleanup(struct v4l2_subdev *sd_1to1); > + > #endif -- Regards, Laurent Pinchart