linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>
To: Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
	Hans Verkuil <hverkuil@xs4all.nl>
Cc: linux-media@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	tomoharu.fukawa.eb@renesas.com,
	"Sakari Ailus" <sakari.ailus@linux.intel.com>,
	"Geert Uytterhoeven" <geert@linux-m68k.org>,
	"Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>
Subject: [PATCHv2 24/32] media: rcar-vin: add link notify for Gen3
Date: Sat, 12 Nov 2016 14:12:08 +0100	[thread overview]
Message-ID: <20161112131216.22635-25-niklas.soderlund+renesas@ragnatech.se> (raw)
In-Reply-To: <20161112131216.22635-1-niklas.soderlund+renesas@ragnatech.se>

Add the ability to process media device link change request. Link
enablement are a bit complicated on Gen3, if it's possible to enable a
link depends on what other links already are enabled. On Gen3 the 8 VIN
are split into two subgroups (VIN0-3 and VIN4-7) and from a routing
perspective these two groups are independent of each other. Each
subgroups routing is controlled by the subgroup VIN master instance
(VIN0 and VIN4).

There are a limited number of possible route setups available for each
subgroup and the configuration of each setup is dictated by the
hardware. On H3 for example there are 6 possible route setups for each
subgroup to choose from.

This leads to the media device link notification code being rather large
since it will find the best routing configuration to try and accommodate
as many links as possible. When it's not possible to enable a new link
due to hardware constrains the link_notifier callback will return
-EBUSY.

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

diff --git a/drivers/media/platform/rcar-vin/rcar-core.c b/drivers/media/platform/rcar-vin/rcar-core.c
index de75ca8..0092ab4 100644
--- a/drivers/media/platform/rcar-vin/rcar-core.c
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -26,6 +26,209 @@
 #include "rcar-vin.h"
 
 /* -----------------------------------------------------------------------------
+ * Media Controller link notification
+ */
+
+static unsigned int rvin_group_csi_pad_to_chan(unsigned int pad)
+{
+	/*
+	 * The CSI2 driver is rcar-csi2 and we know it's pad layout are
+	 * 0: Source 1-4: Sinks so if we remove one from the pad we
+	 * get the rcar-vin internal CSI2 channel number
+	 */
+	return pad - 1;
+}
+
+/* group lock should be held when calling this function */
+static int rvin_group_entity_to_vin_num(struct rvin_group *group,
+					struct media_entity *entity)
+{
+	struct video_device *vdev;
+	int i;
+
+	if (!is_media_entity_v4l2_video_device(entity))
+		return -ENODEV;
+
+	vdev = media_entity_to_video_device(entity);
+
+	for (i = 0; i < RCAR_VIN_NUM; i++) {
+		if (!group->vin[i])
+			continue;
+
+		if (&group->vin[i]->vdev == vdev)
+			return i;
+	}
+
+	return -ENODEV;
+}
+
+/* group lock should be held when calling this function */
+static int rvin_group_entity_to_csi_num(struct rvin_group *group,
+					struct media_entity *entity)
+{
+	struct v4l2_subdev *sd;
+	int i;
+
+	if (!is_media_entity_v4l2_subdev(entity))
+		return -ENODEV;
+
+	sd = media_entity_to_v4l2_subdev(entity);
+
+	for (i = 0; i < RVIN_CSI_MAX; i++)
+		if (group->bridge[i].subdev == sd)
+			return i;
+
+	return -ENODEV;
+}
+
+/* group lock should be held when calling this function */
+static void __rvin_group_build_link_list(struct rvin_group *group,
+					 struct rvin_group_chsel *map,
+					 int start, int len)
+{
+	struct media_pad *vin_pad, *remote_pad;
+	unsigned int n;
+
+	for (n = 0; n < len; n++) {
+		map[n].csi = -1;
+		map[n].chan = -1;
+
+		if (!group->vin[start + n])
+			continue;
+
+		vin_pad = &group->vin[start + n]->vdev.entity.pads[RVIN_SINK];
+
+		remote_pad = media_entity_remote_pad(vin_pad);
+		if (!remote_pad)
+			continue;
+
+		map[n].csi =
+			rvin_group_entity_to_csi_num(group, remote_pad->entity);
+		map[n].chan = rvin_group_csi_pad_to_chan(remote_pad->index);
+	}
+}
+
+/* group lock should be held when calling this function */
+static int __rvin_group_try_get_chsel(struct rvin_group *group,
+				      struct rvin_group_chsel *map,
+				      int start, int len)
+{
+	const struct rvin_group_chsel *sel;
+	unsigned int i, n;
+	int chsel;
+
+	for (i = 0; i < group->vin[start]->info->num_chsels; i++) {
+		chsel = i;
+		for (n = 0; n < len; n++) {
+
+			/* If the link is not active it's OK */
+			if (map[n].csi == -1)
+				continue;
+
+			/* Check if chsel match requested link */
+			sel = &group->vin[start]->info->chsels[start + n][i];
+			if (map[n].csi != sel->csi ||
+			    map[n].chan != sel->chan) {
+				chsel = -1;
+				break;
+			}
+		}
+
+		/* A chsel which satisfy the links have been found */
+		if (chsel != -1)
+			return chsel;
+	}
+
+	/* No chsel can satisfy the requested links */
+	return -1;
+}
+
+/* group lock should be held when calling this function */
+static bool rvin_group_in_use(struct rvin_group *group)
+{
+	struct media_entity *entity;
+
+	media_device_for_each_entity(entity, &group->mdev)
+		if (entity->use_count)
+			return true;
+
+	return false;
+}
+
+static int rvin_group_link_notify(struct media_link *link, u32 flags,
+				  unsigned int notification)
+{
+	struct rvin_group *group = container_of(link->graph_obj.mdev,
+						struct rvin_group, mdev);
+	struct rvin_group_chsel chsel_map[4];
+	int vin_num, vin_master, csi_num, csi_chan;
+	unsigned int chsel;
+
+	mutex_lock(&group->lock);
+
+	vin_num = rvin_group_entity_to_vin_num(group, link->sink->entity);
+	csi_num = rvin_group_entity_to_csi_num(group, link->source->entity);
+	csi_chan = rvin_group_csi_pad_to_chan(link->source->index);
+
+	/*
+	 * Figure out which VIN node is the subgroup master.
+	 *
+	 * VIN0-3 are controlled by VIN0
+	 * VIN4-7 are controlled by VIN4
+	 */
+	vin_master = vin_num < 4 ? 0 : 4;
+
+	/* If not all devices exists something is horribly wrong */
+	if (vin_num < 0 || csi_num < 0 || !group->vin[vin_master])
+		goto error;
+
+	/* Special checking only needed for links which are to be enabled */
+	if (notification != MEDIA_DEV_NOTIFY_PRE_LINK_CH ||
+	    !(flags & MEDIA_LNK_FL_ENABLED))
+		goto out;
+
+	/* If any link in the group are in use, no new link can be enabled */
+	if (rvin_group_in_use(group))
+		goto error;
+
+	/* If the VIN already have a active link it's busy */
+	if (media_entity_remote_pad(&link->sink->entity->pads[RVIN_SINK]))
+		goto error;
+
+	/* Build list of active links */
+	__rvin_group_build_link_list(group, chsel_map, vin_master, 4);
+
+	/* Add the new proposed link */
+	chsel_map[vin_num - vin_master].csi = csi_num;
+	chsel_map[vin_num - vin_master].chan = csi_chan;
+
+	/* See if there is a chsel value which match our link selection */
+	chsel = __rvin_group_try_get_chsel(group, chsel_map, vin_master, 4);
+
+	/* No chsel can provide the request links */
+	if (chsel == -1)
+		goto error;
+
+	/* Update chsel value at group master */
+	if (rvin_set_chsel(group->vin[vin_master], chsel))
+		goto error;
+
+out:
+	mutex_unlock(&group->lock);
+
+	return v4l2_pipeline_link_notify(link, flags, notification);
+error:
+	mutex_unlock(&group->lock);
+
+	return -EBUSY;
+}
+
+
+static const struct media_device_ops rvin_media_ops = {
+	.link_notify = rvin_group_link_notify,
+};
+
+/* -----------------------------------------------------------------------------
  * Gen3 CSI2 Group Allocator
  */
 
@@ -100,6 +303,8 @@ static struct rvin_group *rvin_group_allocate(struct rvin_dev *vin)
 		mdev->driver_version = LINUX_VERSION_CODE;
 		media_device_init(mdev);
 
+		mdev->ops = &rvin_media_ops;
+
 		ret = media_device_register(mdev);
 		if (ret) {
 			vin_err(vin, "Failed to register media device\n");
-- 
2.10.2


  parent reply	other threads:[~2016-11-12 13:13 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-12 13:11 [PATCHv2 00/32] rcar-vin: Add Gen3 with media controller support Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 01/32] media: entity: Add has_route entity operation Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 02/32] media: entity: Add media_entity_has_route() function Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 03/32] media: rcar-vin: reset bytesperline and sizeimage when resetting format Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 04/32] media: rcar-vin: use rvin_reset_format() in S_DV_TIMINGS Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 05/32] media: rcar-vin: fix how pads are handled for v4l2 subdeivce operations Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 06/32] media: rcar-vin: fix standard in input enumeration Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 07/32] media: rcar-vin: add wrapper to get rvin_graph_entity Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 08/32] media: rcar-vin: move subdev source and sink pad index to rvin_graph_entity Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 09/32] media: rcar-vin: move pad number discovery to async complete handler Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 10/32] media: rcar-vin: use pad information when verifying media bus format Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 11/32] media: rcar-vin: refactor pad lookup code Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 12/32] media: rcar-vin: split rvin_s_fmt_vid_cap() Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 13/32] media: rcar-vin: register the video device early Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 14/32] media: rcar-vin: move chip information to own struct Niklas Söderlund
2016-11-12 13:11 ` [PATCHv2 15/32] media: rcar-vin: move max width and height information to chip information Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 16/32] media: rcar-vin: change name of video device Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 17/32] media: rcar-vin: clarify error message from the digital notifier Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 18/32] media: rcar-vin: enable Gen3 hardware configuration Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 19/32] media: rcar-vin: add functions to manipulate Gen3 CHSEL value Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 20/32] media: rcar-vin: expose a sink pad if we are on Gen3 Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 21/32] media: rcar-vin: add group allocator functions Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 22/32] media: rcar-vin: add chsel information to rvin_info Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 23/32] media: rcar-vin: parse Gen3 OF and setup media graph Niklas Söderlund
2016-11-12 13:12 ` Niklas Söderlund [this message]
2016-11-12 13:12 ` [PATCHv2 25/32] media: rcar-vin: enable CSI2 group subdevices in lookup helpers Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 26/32] media: rcar-vin: add helpers for bridge Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 27/32] media: rcar-vin: start/stop the CSI2 bridge stream Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 28/32] media: rcar-vin: propagate format to bridge Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 29/32] media: rcar-vin: attach to CSI2 group when the video device is opened Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 30/32] media: rcar-vin: add Gen3 devicetree bindings documentation Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 31/32] media: rcar-vin: enable support for r8a7795 Niklas Söderlund
2016-11-12 13:12 ` [PATCHv2 32/32] media: rcar-vin: enable support for r8a7796 Niklas Söderlund

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=20161112131216.22635-25-niklas.soderlund+renesas@ragnatech.se \
    --to=niklas.soderlund+renesas@ragnatech.se \
    --cc=geert@linux-m68k.org \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=tomoharu.fukawa.eb@renesas.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;
as well as URLs for NNTP newsgroup(s).