diff mbox series

[v5,03/23] media: v4l2-subdev: add v4l2_subdev_get_fwnode_pad_default

Message ID 20200420003930.11463-4-slongerbeam@gmail.com (mailing list archive)
State New, archived
Headers show
Series media: imx: Create media links in bound notifiers | expand

Commit Message

Steve Longerbeam April 20, 2020, 12:39 a.m. UTC
Add a convenience function that can be used as the .get_fwnode_pad
operation for subdevices that map port numbers and pad indexes 1:1.
The function verifies the endpoint is owned by the subdevice, and if
so returns the endpoint port number.

Signed-off-by: Steve Longerbeam <slongerbeam@gmail.com>
---
 drivers/media/v4l2-core/v4l2-subdev.c | 25 +++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 17 +++++++++++++++++
 2 files changed, 42 insertions(+)

Comments

Sakari Ailus April 29, 2020, 2:39 p.m. UTC | #1
Hi Steve,

On Sun, Apr 19, 2020 at 05:39:10PM -0700, Steve Longerbeam wrote:
> Add a convenience function that can be used as the .get_fwnode_pad
> operation for subdevices that map port numbers and pad indexes 1:1.
> The function verifies the endpoint is owned by the subdevice, and if
> so returns the endpoint port number.
> 
> Signed-off-by: Steve Longerbeam <slongerbeam@gmail.com>
> ---
>  drivers/media/v4l2-core/v4l2-subdev.c | 25 +++++++++++++++++++++++++
>  include/media/v4l2-subdev.h           | 17 +++++++++++++++++
>  2 files changed, 42 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index a376b351135f..d5b5cd7a6049 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -696,6 +696,31 @@ const struct v4l2_file_operations v4l2_subdev_fops = {
>  };
>  
>  #ifdef CONFIG_MEDIA_CONTROLLER
> +
> +int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
> +				       struct fwnode_endpoint *endpoint)
> +{
> +	struct fwnode_handle *ep;
> +	struct v4l2_subdev *sd;
> +
> +	if (!is_media_entity_v4l2_subdev(entity))
> +		return -EINVAL;
> +
> +	sd = media_entity_to_v4l2_subdev(entity);
> +
> +	fwnode_graph_for_each_endpoint(dev_fwnode(sd->dev), ep) {
> +		if (ep != endpoint->local_fwnode)
> +			continue;

If the purpose is just to check a given endpoint belongs to a device, could
it be done in a more simple way? E.g.:

	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
	fwnode_handle_put(fwnode);

	if (dev_fwnode(sd->dev) == fwnode)
		return endpoint->port;

	return -ENXIO;

> +
> +		fwnode_handle_put(ep);
> +
> +		return endpoint->port;
> +	}
> +
> +	return -ENXIO;
> +}
> +EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_default);
> +
>  int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
>  				      struct media_link *link,
>  				      struct v4l2_subdev_format *source_fmt,
> diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
> index a4848de59852..940181323427 100644
> --- a/include/media/v4l2-subdev.h
> +++ b/include/media/v4l2-subdev.h
> @@ -1027,6 +1027,23 @@ static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
>  
>  #ifdef CONFIG_MEDIA_CONTROLLER
>  
> +/**
> + * v4l2_subdev_get_fwnode_pad_default - Get pad number from a subdev fwnode
> + *                                      endpoint, assuming 1:1 port:pad
> + *
> + * @entity - Pointer to the subdev entity
> + * @endpoint - Pointer to a parsed fwnode endpoint
> + *
> + * This function can be used as the .get_fwnode_pad operation for
> + * subdevices that map port numbers and pad indexes 1:1. If the endpoint
> + * is owned by the subdevice, the function returns the endpoint port
> + * number.
> + *
> + * Returns the endpoint port number on success or a negative error code.
> + */
> +int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
> +				       struct fwnode_endpoint *endpoint);
> +
>  /**
>   * v4l2_subdev_link_validate_default - validates a media link
>   *
Steve Longerbeam April 29, 2020, 8:49 p.m. UTC | #2
Hi Sakari,

On 4/29/20 7:39 AM, Sakari Ailus wrote:
> Hi Steve,
>
> On Sun, Apr 19, 2020 at 05:39:10PM -0700, Steve Longerbeam wrote:
>> Add a convenience function that can be used as the .get_fwnode_pad
>> operation for subdevices that map port numbers and pad indexes 1:1.
>> The function verifies the endpoint is owned by the subdevice, and if
>> so returns the endpoint port number.
>>
>> Signed-off-by: Steve Longerbeam <slongerbeam@gmail.com>
>> ---
>>   drivers/media/v4l2-core/v4l2-subdev.c | 25 +++++++++++++++++++++++++
>>   include/media/v4l2-subdev.h           | 17 +++++++++++++++++
>>   2 files changed, 42 insertions(+)
>>
>> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
>> index a376b351135f..d5b5cd7a6049 100644
>> --- a/drivers/media/v4l2-core/v4l2-subdev.c
>> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
>> @@ -696,6 +696,31 @@ const struct v4l2_file_operations v4l2_subdev_fops = {
>>   };
>>   
>>   #ifdef CONFIG_MEDIA_CONTROLLER
>> +
>> +int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
>> +				       struct fwnode_endpoint *endpoint)
>> +{
>> +	struct fwnode_handle *ep;
>> +	struct v4l2_subdev *sd;
>> +
>> +	if (!is_media_entity_v4l2_subdev(entity))
>> +		return -EINVAL;
>> +
>> +	sd = media_entity_to_v4l2_subdev(entity);
>> +
>> +	fwnode_graph_for_each_endpoint(dev_fwnode(sd->dev), ep) {
>> +		if (ep != endpoint->local_fwnode)
>> +			continue;
> If the purpose is just to check a given endpoint belongs to a device, could
> it be done in a more simple way? E.g.:
>
> 	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
> 	fwnode_handle_put(fwnode);
>
> 	if (dev_fwnode(sd->dev) == fwnode)
> 		return endpoint->port;
>
> 	return -ENXIO;

Sorry you are right, I was stuck on confirming the endpoint itself is 
one of the devices endpoints, but yes, above is all that is needed to 
confirm the endpoint is owned by the device, I'll make that change.

Steve


>
>> +
>> +		fwnode_handle_put(ep);
>> +
>> +		return endpoint->port;
>> +	}
>> +
>> +	return -ENXIO;
>> +}
>> +EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_default);
>> +
>>   int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
>>   				      struct media_link *link,
>>   				      struct v4l2_subdev_format *source_fmt,
>> diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
>> index a4848de59852..940181323427 100644
>> --- a/include/media/v4l2-subdev.h
>> +++ b/include/media/v4l2-subdev.h
>> @@ -1027,6 +1027,23 @@ static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
>>   
>>   #ifdef CONFIG_MEDIA_CONTROLLER
>>   
>> +/**
>> + * v4l2_subdev_get_fwnode_pad_default - Get pad number from a subdev fwnode
>> + *                                      endpoint, assuming 1:1 port:pad
>> + *
>> + * @entity - Pointer to the subdev entity
>> + * @endpoint - Pointer to a parsed fwnode endpoint
>> + *
>> + * This function can be used as the .get_fwnode_pad operation for
>> + * subdevices that map port numbers and pad indexes 1:1. If the endpoint
>> + * is owned by the subdevice, the function returns the endpoint port
>> + * number.
>> + *
>> + * Returns the endpoint port number on success or a negative error code.
>> + */
>> +int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
>> +				       struct fwnode_endpoint *endpoint);
>> +
>>   /**
>>    * v4l2_subdev_link_validate_default - validates a media link
>>    *
Sakari Ailus April 29, 2020, 9:14 p.m. UTC | #3
Hi Steve,

On Wed, Apr 29, 2020 at 01:49:35PM -0700, Steve Longerbeam wrote:
> Hi Sakari,
> 
> On 4/29/20 7:39 AM, Sakari Ailus wrote:
> > Hi Steve,
> > 
> > On Sun, Apr 19, 2020 at 05:39:10PM -0700, Steve Longerbeam wrote:
> > > Add a convenience function that can be used as the .get_fwnode_pad
> > > operation for subdevices that map port numbers and pad indexes 1:1.
> > > The function verifies the endpoint is owned by the subdevice, and if
> > > so returns the endpoint port number.
> > > 
> > > Signed-off-by: Steve Longerbeam <slongerbeam@gmail.com>
> > > ---
> > >   drivers/media/v4l2-core/v4l2-subdev.c | 25 +++++++++++++++++++++++++
> > >   include/media/v4l2-subdev.h           | 17 +++++++++++++++++
> > >   2 files changed, 42 insertions(+)
> > > 
> > > diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> > > index a376b351135f..d5b5cd7a6049 100644
> > > --- a/drivers/media/v4l2-core/v4l2-subdev.c
> > > +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> > > @@ -696,6 +696,31 @@ const struct v4l2_file_operations v4l2_subdev_fops = {
> > >   };
> > >   #ifdef CONFIG_MEDIA_CONTROLLER
> > > +
> > > +int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
> > > +				       struct fwnode_endpoint *endpoint)
> > > +{
> > > +	struct fwnode_handle *ep;
> > > +	struct v4l2_subdev *sd;
> > > +
> > > +	if (!is_media_entity_v4l2_subdev(entity))
> > > +		return -EINVAL;
> > > +
> > > +	sd = media_entity_to_v4l2_subdev(entity);
> > > +
> > > +	fwnode_graph_for_each_endpoint(dev_fwnode(sd->dev), ep) {
> > > +		if (ep != endpoint->local_fwnode)
> > > +			continue;
> > If the purpose is just to check a given endpoint belongs to a device, could
> > it be done in a more simple way? E.g.:
> > 
> > 	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
> > 	fwnode_handle_put(fwnode);
> > 
> > 	if (dev_fwnode(sd->dev) == fwnode)
> > 		return endpoint->port;
> > 
> > 	return -ENXIO;
> 
> Sorry you are right, I was stuck on confirming the endpoint itself is one of
> the devices endpoints, but yes, above is all that is needed to confirm the
> endpoint is owned by the device, I'll make that change.

Thanks.

Could you calso call the function e.g. v4l2_subdev_get_fwnode_pad_1_to_1 or
such? I presume there will be a few of these, and it depends on the device
what suits it.
diff mbox series

Patch

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index a376b351135f..d5b5cd7a6049 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -696,6 +696,31 @@  const struct v4l2_file_operations v4l2_subdev_fops = {
 };
 
 #ifdef CONFIG_MEDIA_CONTROLLER
+
+int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
+				       struct fwnode_endpoint *endpoint)
+{
+	struct fwnode_handle *ep;
+	struct v4l2_subdev *sd;
+
+	if (!is_media_entity_v4l2_subdev(entity))
+		return -EINVAL;
+
+	sd = media_entity_to_v4l2_subdev(entity);
+
+	fwnode_graph_for_each_endpoint(dev_fwnode(sd->dev), ep) {
+		if (ep != endpoint->local_fwnode)
+			continue;
+
+		fwnode_handle_put(ep);
+
+		return endpoint->port;
+	}
+
+	return -ENXIO;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_default);
+
 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
 				      struct media_link *link,
 				      struct v4l2_subdev_format *source_fmt,
diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
index a4848de59852..940181323427 100644
--- a/include/media/v4l2-subdev.h
+++ b/include/media/v4l2-subdev.h
@@ -1027,6 +1027,23 @@  static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
 
 #ifdef CONFIG_MEDIA_CONTROLLER
 
+/**
+ * v4l2_subdev_get_fwnode_pad_default - Get pad number from a subdev fwnode
+ *                                      endpoint, assuming 1:1 port:pad
+ *
+ * @entity - Pointer to the subdev entity
+ * @endpoint - Pointer to a parsed fwnode endpoint
+ *
+ * This function can be used as the .get_fwnode_pad operation for
+ * subdevices that map port numbers and pad indexes 1:1. If the endpoint
+ * is owned by the subdevice, the function returns the endpoint port
+ * number.
+ *
+ * Returns the endpoint port number on success or a negative error code.
+ */
+int v4l2_subdev_get_fwnode_pad_default(struct media_entity *entity,
+				       struct fwnode_endpoint *endpoint);
+
 /**
  * v4l2_subdev_link_validate_default - validates a media link
  *