Re: [PATCH v4 23/27] rcar-vin: parse Gen3 OF and setup media graph

2017-05-02 Thread Kieran Bingham
Hi Niklas

Just a quick comment here, as I've come across it while looking at the fw_node
topic.

On 27/04/17 23:41, Niklas Söderlund wrote:
> Parse the VIN Gen3 OF graph and register all CSI-2 devices in the VIN
> group common media device. Once a CSI-2 subdevice is added to the common
> media device list as many links as possible are added.
> 
> The parsing and registering CSI-2 subdevices is a collaborative effort
> shared between all rcar-vin instances which are part of the group.  The
> rcar-vin instance that first sees a new CSI-2 subdevice adds it to its
> private v4l2 async notifier and once it's bound it will be
> available for the whole group.
> 
> Signed-off-by: Niklas Söderlund 
> ---
>  drivers/media/platform/rcar-vin/rcar-core.c | 340 
> ++--
>  1 file changed, 327 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
> b/drivers/media/platform/rcar-vin/rcar-core.c
> index c10770d5ec37816c..9b9da9a419d0b7e1 100644
> --- a/drivers/media/platform/rcar-vin/rcar-core.c
> +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> @@ -158,21 +158,32 @@ static int rvin_parse_v4l2(struct rvin_dev *vin,
>  
>   mbus_cfg->type = v4l2_ep.bus_type;
>  
> - switch (mbus_cfg->type) {
> - case V4L2_MBUS_PARALLEL:
> - vin_dbg(vin, "Found PARALLEL media bus\n");
> - mbus_cfg->flags = v4l2_ep.bus.parallel.flags;
> - break;
> - case V4L2_MBUS_BT656:
> - vin_dbg(vin, "Found BT656 media bus\n");
> - mbus_cfg->flags = 0;
> - break;
> - default:
> - vin_err(vin, "Unknown media bus type\n");
> - return -EINVAL;
> + if (vin->info->chip == RCAR_GEN3) {
> + switch (mbus_cfg->type) {
> + case V4L2_MBUS_CSI2:
> + vin_dbg(vin, "Found CSI-2 media bus\n");
> + mbus_cfg->flags = 0;
> + return 0;
> + default:
> + break;
> + }
> + } else {
> + switch (mbus_cfg->type) {
> + case V4L2_MBUS_PARALLEL:
> + vin_dbg(vin, "Found PARALLEL media bus\n");
> + mbus_cfg->flags = v4l2_ep.bus.parallel.flags;
> + return 0;
> + case V4L2_MBUS_BT656:
> + vin_dbg(vin, "Found BT656 media bus\n");
> + mbus_cfg->flags = 0;
> + return 0;
> + default:
> + break;
> + }
>   }
>  
> - return 0;
> + vin_err(vin, "Unknown media bus type\n");
> + return -EINVAL;
>  }
>  
>  /* 
> -
> @@ -357,6 +368,299 @@ static int rvin_digital_graph_init(struct rvin_dev *vin)
>   * Group async notifier
>   */
>  
> +/* group lock should be held when calling this function */
> +static int rvin_group_add_link(struct rvin_dev *vin,
> +struct media_entity *source,
> +unsigned int source_idx,
> +struct media_entity *sink,
> +unsigned int sink_idx,
> +u32 flags)
> +{
> + struct media_pad *source_pad, *sink_pad;
> + int ret = 0;
> +
> + source_pad = >pads[source_idx];
> + sink_pad = >pads[sink_idx];
> +
> + if (!media_entity_find_link(source_pad, sink_pad))
> + ret = media_create_pad_link(source, source_idx,
> + sink, sink_idx, flags);
> +
> + if (ret)
> + vin_err(vin, "Error adding link from %s to %s\n",
> + source->name, sink->name);
> +
> + return ret;
> +}
> +
> +static int rvin_group_update_links(struct rvin_dev *vin)
> +{
> + struct media_entity *source, *sink;
> + struct rvin_dev *master;
> + unsigned int i, n, idx, chsel, csi;
> + u32 flags;
> + int ret;
> +
> + mutex_lock(>group->lock);
> +
> + for (n = 0; n < RCAR_VIN_NUM; n++) {
> +
> + /* Check that VIN is part of the group */
> + if (!vin->group->vin[n])
> + continue;
> +
> + /* Check that subgroup master is part of the group */
> + master = vin->group->vin[n < 4 ? 0 : 4];
> + if (!master)
> + continue;
> +
> + chsel = rvin_get_chsel(master);
> +
> + for (i = 0; i < vin->info->num_chsels; i++) {
> + csi = vin->info->chsels[n][i].csi;
> +
> + /* If the CSI-2 is out of bounds it's a noop, skip */
> + if (csi >= RVIN_CSI_MAX)
> + continue;
> +
> + /* Check that CSI-2 are part of the group */
> + if (!vin->group->csi[csi].subdev)
> + 

[PATCH v4 23/27] rcar-vin: parse Gen3 OF and setup media graph

2017-04-27 Thread Niklas Söderlund
Parse the VIN Gen3 OF graph and register all CSI-2 devices in the VIN
group common media device. Once a CSI-2 subdevice is added to the common
media device list as many links as possible are added.

The parsing and registering CSI-2 subdevices is a collaborative effort
shared between all rcar-vin instances which are part of the group.  The
rcar-vin instance that first sees a new CSI-2 subdevice adds it to its
private v4l2 async notifier and once it's bound it will be
available for the whole group.

Signed-off-by: Niklas Söderlund 
---
 drivers/media/platform/rcar-vin/rcar-core.c | 340 ++--
 1 file changed, 327 insertions(+), 13 deletions(-)

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c 
b/drivers/media/platform/rcar-vin/rcar-core.c
index c10770d5ec37816c..9b9da9a419d0b7e1 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -158,21 +158,32 @@ static int rvin_parse_v4l2(struct rvin_dev *vin,
 
mbus_cfg->type = v4l2_ep.bus_type;
 
-   switch (mbus_cfg->type) {
-   case V4L2_MBUS_PARALLEL:
-   vin_dbg(vin, "Found PARALLEL media bus\n");
-   mbus_cfg->flags = v4l2_ep.bus.parallel.flags;
-   break;
-   case V4L2_MBUS_BT656:
-   vin_dbg(vin, "Found BT656 media bus\n");
-   mbus_cfg->flags = 0;
-   break;
-   default:
-   vin_err(vin, "Unknown media bus type\n");
-   return -EINVAL;
+   if (vin->info->chip == RCAR_GEN3) {
+   switch (mbus_cfg->type) {
+   case V4L2_MBUS_CSI2:
+   vin_dbg(vin, "Found CSI-2 media bus\n");
+   mbus_cfg->flags = 0;
+   return 0;
+   default:
+   break;
+   }
+   } else {
+   switch (mbus_cfg->type) {
+   case V4L2_MBUS_PARALLEL:
+   vin_dbg(vin, "Found PARALLEL media bus\n");
+   mbus_cfg->flags = v4l2_ep.bus.parallel.flags;
+   return 0;
+   case V4L2_MBUS_BT656:
+   vin_dbg(vin, "Found BT656 media bus\n");
+   mbus_cfg->flags = 0;
+   return 0;
+   default:
+   break;
+   }
}
 
-   return 0;
+   vin_err(vin, "Unknown media bus type\n");
+   return -EINVAL;
 }
 
 /* 
-
@@ -357,6 +368,299 @@ static int rvin_digital_graph_init(struct rvin_dev *vin)
  * Group async notifier
  */
 
+/* group lock should be held when calling this function */
+static int rvin_group_add_link(struct rvin_dev *vin,
+  struct media_entity *source,
+  unsigned int source_idx,
+  struct media_entity *sink,
+  unsigned int sink_idx,
+  u32 flags)
+{
+   struct media_pad *source_pad, *sink_pad;
+   int ret = 0;
+
+   source_pad = >pads[source_idx];
+   sink_pad = >pads[sink_idx];
+
+   if (!media_entity_find_link(source_pad, sink_pad))
+   ret = media_create_pad_link(source, source_idx,
+   sink, sink_idx, flags);
+
+   if (ret)
+   vin_err(vin, "Error adding link from %s to %s\n",
+   source->name, sink->name);
+
+   return ret;
+}
+
+static int rvin_group_update_links(struct rvin_dev *vin)
+{
+   struct media_entity *source, *sink;
+   struct rvin_dev *master;
+   unsigned int i, n, idx, chsel, csi;
+   u32 flags;
+   int ret;
+
+   mutex_lock(>group->lock);
+
+   for (n = 0; n < RCAR_VIN_NUM; n++) {
+
+   /* Check that VIN is part of the group */
+   if (!vin->group->vin[n])
+   continue;
+
+   /* Check that subgroup master is part of the group */
+   master = vin->group->vin[n < 4 ? 0 : 4];
+   if (!master)
+   continue;
+
+   chsel = rvin_get_chsel(master);
+
+   for (i = 0; i < vin->info->num_chsels; i++) {
+   csi = vin->info->chsels[n][i].csi;
+
+   /* If the CSI-2 is out of bounds it's a noop, skip */
+   if (csi >= RVIN_CSI_MAX)
+   continue;
+
+   /* Check that CSI-2 are part of the group */
+   if (!vin->group->csi[csi].subdev)
+   continue;
+
+   source = >group->csi[csi].subdev->entity;
+   sink = >group->vin[n]->vdev->entity;
+   idx = vin->info->chsels[n][i].chan + 1;
+   flags = i == chsel ?