From patchwork Mon Jan 29 16:34:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 10190045 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8F44860375 for ; Mon, 29 Jan 2018 16:35:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D03F204BA for ; Mon, 29 Jan 2018 16:35:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 62081223A6; Mon, 29 Jan 2018 16:35:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F018A204BA for ; Mon, 29 Jan 2018 16:35:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751753AbeA2Qfz (ORCPT ); Mon, 29 Jan 2018 11:35:55 -0500 Received: from bin-mail-out-06.binero.net ([195.74.38.229]:7331 "EHLO bin-vsp-out-03.atm.binero.net" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751737AbeA2Qfy (ORCPT ); Mon, 29 Jan 2018 11:35:54 -0500 X-Halon-ID: 7786efce-0512-11e8-9564-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (unknown [89.233.230.99]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id 7786efce-0512-11e8-9564-0050569116f7; Mon, 29 Jan 2018 17:35:51 +0100 (CET) From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= To: Laurent Pinchart , Hans Verkuil , linux-media@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, tomoharu.fukawa.eb@renesas.com, Kieran Bingham , =?UTF-8?q?Niklas=20S=C3=B6derlund?= Subject: [PATCH v10 26/30] rcar-vin: add link notify for Gen3 Date: Mon, 29 Jan 2018 17:34:31 +0100 Message-Id: <20180129163435.24936-27-niklas.soderlund+renesas@ragnatech.se> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129163435.24936-1-niklas.soderlund+renesas@ragnatech.se> References: <20180129163435.24936-1-niklas.soderlund+renesas@ragnatech.se> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the ability to process media device link change request. Link enabling is a bit complicated on Gen3, whether or not it's possible to enable a link depends on what other links already are enabled. On Gen3 the 8 VINs are split into two subgroup's (VIN0-3 and VIN4-7) and from a routing perspective these two groups are independent of each other. Each subgroup's 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 -EMLINK. Signed-off-by: Niklas Söderlund --- drivers/media/platform/rcar-vin/rcar-core.c | 129 ++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/drivers/media/platform/rcar-vin/rcar-core.c b/drivers/media/platform/rcar-vin/rcar-core.c index f08277a0dc11f477..7ceff0de40078580 100644 --- a/drivers/media/platform/rcar-vin/rcar-core.c +++ b/drivers/media/platform/rcar-vin/rcar-core.c @@ -24,6 +24,7 @@ #include #include +#include #include "rcar-vin.h" @@ -44,6 +45,133 @@ */ #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4) +/* ----------------------------------------------------------------------------- + * Media Controller link notification + */ + +/* group lock should be held when calling this function */ +static int rvin_group_entity_to_csi_id(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->csi[i].subdev == sd) + return i; + + return -ENODEV; +} + +static unsigned int rvin_group_get_mask(struct rvin_dev *vin, + enum rvin_csi_id csi_id, + unsigned char chan) +{ + const struct rvin_group_route *route; + unsigned int mask = 0; + + for (route = vin->info->routes; route->mask; route++) { + if (route->vin == vin->id && + route->csi == csi_id && + route->chan == chan) { + vin_dbg(vin, "Adding route: vin: %d csi: %d chan: %d\n", + route->vin, route->csi, route->chan); + mask |= route->mask; + } + } + + return mask; +} + +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); + unsigned int i, master_id, chan, mask_new, mask = ~0; + struct media_entity *entity; + struct video_device *vdev; + struct media_pad *csi_pad; + struct rvin_dev *vin = NULL; + int csi_id, ret; + + ret = v4l2_pipeline_link_notify(link, flags, notification); + if (ret) + return ret; + + /* Only care about link enablement for VIN nodes */ + if (!(flags & MEDIA_LNK_FL_ENABLED) || + !is_media_entity_v4l2_video_device(link->sink->entity)) + return 0; + + /* If any entity are in use don't allow link changes */ + media_device_for_each_entity(entity, &group->mdev) + if (entity->use_count) + return -EBUSY; + + mutex_lock(&group->lock); + + /* Find VIN and its master for which the link */ + entity = link->sink->entity; + vdev = media_entity_to_video_device(entity); + for (i = 0; i < RCAR_VIN_NUM; i++) { + if (group->vin[i] && &group->vin[i]->vdev == vdev) { + vin = group->vin[i]; + master_id = rvin_group_id_to_master(vin->id); + break; + } + } + if (WARN_ON(!vin || !group->vin[master_id])) { + ret = -ENODEV; + goto out; + } + + /* Build a mask for already enabled links */ + for (i = master_id; i < master_id + 4; i++) { + if (!group->vin[i]) + continue; + + /* Get remote CSI-2, if any */ + csi_pad = media_entity_remote_pad( + &group->vin[i]->vdev.entity.pads[0]); + if (!csi_pad) + continue; + + csi_id = rvin_group_entity_to_csi_id(group, csi_pad->entity); + chan = rvin_group_csi_pad_to_chan(csi_pad->index); + + mask &= rvin_group_get_mask(group->vin[i], csi_id, chan); + } + + /* Add the new link to the existing mask and check if it works */ + csi_id = rvin_group_entity_to_csi_id(group, link->source->entity); + chan = rvin_group_csi_pad_to_chan(link->source->index); + mask_new = mask & rvin_group_get_mask(vin, csi_id, chan); + + vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask, mask_new); + + if (!mask_new) { + ret = -EMLINK; + goto out; + } + + /* New valid CHSEL found, set the new value */ + rvin_set_channel_routing(group->vin[master_id], __ffs(mask_new)); +out: + mutex_unlock(&group->lock); + + return ret; +} + +static const struct media_device_ops rvin_media_ops = { + .link_notify = rvin_group_link_notify, +}; + /* ----------------------------------------------------------------------------- * Gen3 CSI2 Group Allocator */ @@ -85,6 +213,7 @@ static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin) vin_dbg(vin, "found %u enabled VIN's in DT", group->count); mdev->dev = vin->dev; + mdev->ops = &rvin_media_ops; match = of_match_node(vin->dev->driver->of_match_table, vin->dev->of_node);