diff mbox

[06/16] rcar-vin: refactor pad lookup code

Message ID 20170314185957.25253-7-niklas.soderlund+renesas@ragnatech.se (mailing list archive)
State New, archived
Headers show

Commit Message

Niklas Söderlund March 14, 2017, 6:59 p.m. UTC
The pad lookup code can be broken out to increase readability and to
reduce code duplication.

Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
---
 drivers/media/platform/rcar-vin/rcar-v4l2.c | 38 +++++++++++++++++------------
 1 file changed, 23 insertions(+), 15 deletions(-)

Comments

Laurent Pinchart May 10, 2017, 1:21 p.m. UTC | #1
Hi Niklas,

Thank you for the patch.

On Tuesday 14 Mar 2017 19:59:47 Niklas Söderlund wrote:
> The pad lookup code can be broken out to increase readability and to
> reduce code duplication.
> 
> Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
> ---
>  drivers/media/platform/rcar-vin/rcar-v4l2.c | 38 +++++++++++++------------
>  1 file changed, 23 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-v4l2.c
> b/drivers/media/platform/rcar-vin/rcar-v4l2.c index
> 1a75191539b0e7d7..ce29a21888da48d5 100644
> --- a/drivers/media/platform/rcar-vin/rcar-v4l2.c
> +++ b/drivers/media/platform/rcar-vin/rcar-v4l2.c
> @@ -870,11 +870,25 @@ static void rvin_notify(struct v4l2_subdev *sd,
>  	}
>  }
> 
> +static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
> +{
> +	unsigned int pad;
> +
> +	if (sd->entity.num_pads <= 1)
> +		return 0;
> +
> +	for (pad = 0; pad < sd->entity.num_pads; pad++)
> +		if (sd->entity.pads[pad].flags & direction)
> +			return pad;
> +
> +	return -EINVAL;
> +}
> +
>  int rvin_v4l2_probe(struct rvin_dev *vin)
>  {
>  	struct video_device *vdev = &vin->vdev;
>  	struct v4l2_subdev *sd = vin_to_source(vin);
> -	int pad_idx, ret;
> +	int ret;
> 
>  	v4l2_set_subdev_hostdata(sd, vin);
> 
> @@ -920,21 +934,15 @@ int rvin_v4l2_probe(struct rvin_dev *vin)
>  	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
>  		V4L2_CAP_READWRITE;
> 
> -	vin->digital.source_pad = 0;
> -	for (pad_idx = 0; pad_idx < sd->entity.num_pads; pad_idx++)
> -		if (sd->entity.pads[pad_idx].flags == MEDIA_PAD_FL_SOURCE)
> -			break;
> -	if (pad_idx >= sd->entity.num_pads)
> -		return -EINVAL;
> -
> -	vin->digital.source_pad = pad_idx;
> +	ret = rvin_find_pad(sd, MEDIA_PAD_FL_SOURCE);
> +	if (ret < 0)
> +		return ret;
> +	vin->digital.source_pad = ret;
> 
> -	vin->digital.sink_pad = 0;
> -	for (pad_idx = 0; pad_idx < sd->entity.num_pads; pad_idx++)
> -		if (sd->entity.pads[pad_idx].flags == MEDIA_PAD_FL_SINK) {
> -			vin->digital.sink_pad = pad_idx;
> -			break;
> -		}
> +	ret = rvin_find_pad(sd, MEDIA_PAD_FL_SINK);
> +	if (ret < 0)
> +		return ret;
> +	vin->digital.sink_pad = ret;

The driver didn't previously consider the lack of a sink pad as an error. As 
camera sensor subdevs typically have no sink pad, I don't think you should 
change this.

>  	vin->format.pixelformat	= RVIN_DEFAULT_FORMAT;
>  	rvin_reset_format(vin);
diff mbox

Patch

diff --git a/drivers/media/platform/rcar-vin/rcar-v4l2.c b/drivers/media/platform/rcar-vin/rcar-v4l2.c
index 1a75191539b0e7d7..ce29a21888da48d5 100644
--- a/drivers/media/platform/rcar-vin/rcar-v4l2.c
+++ b/drivers/media/platform/rcar-vin/rcar-v4l2.c
@@ -870,11 +870,25 @@  static void rvin_notify(struct v4l2_subdev *sd,
 	}
 }
 
+static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
+{
+	unsigned int pad;
+
+	if (sd->entity.num_pads <= 1)
+		return 0;
+
+	for (pad = 0; pad < sd->entity.num_pads; pad++)
+		if (sd->entity.pads[pad].flags & direction)
+			return pad;
+
+	return -EINVAL;
+}
+
 int rvin_v4l2_probe(struct rvin_dev *vin)
 {
 	struct video_device *vdev = &vin->vdev;
 	struct v4l2_subdev *sd = vin_to_source(vin);
-	int pad_idx, ret;
+	int ret;
 
 	v4l2_set_subdev_hostdata(sd, vin);
 
@@ -920,21 +934,15 @@  int rvin_v4l2_probe(struct rvin_dev *vin)
 	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
 		V4L2_CAP_READWRITE;
 
-	vin->digital.source_pad = 0;
-	for (pad_idx = 0; pad_idx < sd->entity.num_pads; pad_idx++)
-		if (sd->entity.pads[pad_idx].flags == MEDIA_PAD_FL_SOURCE)
-			break;
-	if (pad_idx >= sd->entity.num_pads)
-		return -EINVAL;
-
-	vin->digital.source_pad = pad_idx;
+	ret = rvin_find_pad(sd, MEDIA_PAD_FL_SOURCE);
+	if (ret < 0)
+		return ret;
+	vin->digital.source_pad = ret;
 
-	vin->digital.sink_pad = 0;
-	for (pad_idx = 0; pad_idx < sd->entity.num_pads; pad_idx++)
-		if (sd->entity.pads[pad_idx].flags == MEDIA_PAD_FL_SINK) {
-			vin->digital.sink_pad = pad_idx;
-			break;
-		}
+	ret = rvin_find_pad(sd, MEDIA_PAD_FL_SINK);
+	if (ret < 0)
+		return ret;
+	vin->digital.sink_pad = ret;
 
 	vin->format.pixelformat	= RVIN_DEFAULT_FORMAT;
 	rvin_reset_format(vin);