From patchwork Tue Dec 14 14:00:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 15163 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C2384BF415 for ; Tue, 14 Dec 2021 14:00:10 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D3ED9608F8; Tue, 14 Dec 2021 15:00:08 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="cYiVzQlb"; dkim-atps=neutral Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 015FC605A7 for ; Tue, 14 Dec 2021 15:00:07 +0100 (CET) Received: by mail-wr1-x42d.google.com with SMTP id a9so32518303wrr.8 for ; Tue, 14 Dec 2021 06:00:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZCKX3n4FlvxqPd70WacoWyzyIEYvZ2ike0riBFRMQlU=; b=cYiVzQlbMTTgjd7FF2B2Mf2czFj/SUm6/wCeUNeL6wQVGnIb2WF+5IoCL2xmYY386T Pnl+mFv3Bvb6KI2ba53c+17BtzfQj7KcjUtbt0tPupvsL8jZBknokL8n2tembdQI+Ps4 CHKsF3vpRROgbYLN6ur4UKD71A6uCE7KYCXeiZQSb/rTaOtNkuL2RHSAdR6gqZuU/sxI 2nlshgDhbizO5cN/HwlkUs4+iX+mmGRAJMNw6RL4zfOT9PXITeycJeWf5yMcEtmgn+ec AHBDqZokd5V4T/vMe+SFzrCLvlpbJo8hM5n8sEFQm+ysywKxDpee4GJrPBjNEUMU2NsY NTeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZCKX3n4FlvxqPd70WacoWyzyIEYvZ2ike0riBFRMQlU=; b=h3qJggS/radrAB1kikAh5raP4TnoF3xUOFblbUN9UJm/d78uA5fRZij7Vqm/0epx+K +Vm1U+GCDOHqfKbNyTjmt1yJh4r50neQgRtKBLOl/++X4CC7ZnJU1XEg3uCRpB9ZtoXF 5+f8DaJNnd78KVBgenA9LHTfUkCyjrBVFcIfsJSlAn39bTXal9rLJ1MrHw98ps/INeq8 3SN1iZRrbPN95xfOKbnOTw6qoppOqr7O0e9+BEdCHOGjXCT6dnM4tEZW2wO2pltbc73l VGur89cmAJrTCH+EP6GDtzIzCrNkcA4EnTf5KOHqgmdvqc8oCSwfAPmFIivXqim79oNM 87ag== X-Gm-Message-State: AOAM533MrS3cKO0jGH5oW5Qr/6wqf4BpekmdqksxK+gOrZKMPw6LbT79 ttUFHMFPThVq6boukPs1JrX80nu2jDAx9Q== X-Google-Smtp-Source: ABdhPJzswO0rU8hFYsT9uYGOqdEWMqd9PmLrYJJGTpvBV42aIFbsEicvxnwZhA6vpZOJs7FS5FA3uA== X-Received: by 2002:a5d:6c63:: with SMTP id r3mr6212790wrz.213.1639490406310; Tue, 14 Dec 2021 06:00:06 -0800 (PST) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:8958:60a:582a:396]) by smtp.gmail.com with ESMTPSA id m7sm2160999wml.38.2021.12.14.06.00.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 06:00:05 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Dec 2021 14:00:02 +0000 Message-Id: <20211214140002.3552445-3-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211214140002.3552445-1-naush@raspberrypi.com> References: <20211214140002.3552445-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 2/2] pipeline: raspberrypi: Add support for Video Mux and Bridge devices X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This change will allow the pipeline handler to enumerate and control Video Mux or Bridge devices that may be attached between sensors and a particular Unicam instance. Cascaded mux or bridge devices are also handled. A new member function RPiCameraData::enumerateVideoDevices(), called from PipelineHandlerRPi::registerCamera(), is used to identify and open all mux and bridge subdevices present in the sensor -> Unicam link. Relevent links are enabled/disabled and pad formats correctly set in PipelineHandlerRPi::configure() before the camera is started. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham Reviewed-by: David Plowman --- .../pipeline/raspberrypi/raspberrypi.cpp | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 2a2fb5273eb8..4ec646d3c7a3 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -191,6 +192,8 @@ public: int loadIPA(ipa::RPi::SensorConfig *sensorConfig); int configureIPA(const CameraConfiguration *config); + void enumerateVideoDevices(MediaLink *link); + void statsMetadataComplete(uint32_t bufferId, const ControlList &controls); void runIsp(uint32_t bufferId); void embeddedComplete(uint32_t bufferId); @@ -220,6 +223,11 @@ public: std::vector streams_; /* Stores the ids of the buffers mapped in the IPA. */ std::unordered_set ipaBuffers_; + /* + * Stores a cascade of Video Mux or Bridge devices between the sensor and + * Unicam together with media link across the entities. + */ + std::vector, MediaLink *>> bridgeDevices_; /* DMAHEAP allocation helper. */ RPi::DmaHeap dmaHeap_; @@ -868,6 +876,38 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) */ data->properties_.set(properties::ScalerCropMaximum, data->sensorInfo_.analogCrop); + /* Setup the Video Mux/Bridge entities. */ + for (auto &[device, link] : data->bridgeDevices_) { + /* + * Start by disabling all the sink pad links on the devices in the + * cascade, with the exception of the link connecting the device. + */ + for (const MediaPad *p : device->entity()->pads()) { + if (!(p->flags() & MEDIA_PAD_FL_SINK)) + continue; + + for (MediaLink *l : p->links()) { + if (l != link) + l->setEnabled(false); + } + } + + /* Next, enable the entity -> entity links, and setup the pad format. */ + link->setEnabled(true); + const MediaPad *srcPad = link->sink(); + ret = device->setFormat(srcPad->index(), &sensorFormat); + if (ret) { + LOG(RPI, Error) << "Failed to set format on " << device->entity()->name() + << " pad " << srcPad->index() + << " with format " << format.toString() + << ": " << ret; + return ret; + } + + LOG(RPI, Info) << "Configured media link on device " << device->entity()->name() + << " on pad " << srcPad->index(); + } + return ret; } @@ -1098,6 +1138,13 @@ int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp, Me if (data->sensor_->init()) return -EINVAL; + /* + * Enumerate all the Video Mux/Bridge devices across the sensor -> unicam + * link. There may be a cascade of devices in this link! + */ + MediaLink *link = sensorEntity->getPadByIndex(0)->links()[0]; + data->enumerateVideoDevices(link); + data->sensorFormats_ = populateSensorFormats(data->sensor_); ipa::RPi::SensorConfig sensorConfig; @@ -1447,6 +1494,98 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config) return 0; } +/* + * enumerateVideoDevices() iterates over the Media Controller topology, starting + * at the sensor and finishing at Unicam. For each sensor, RPiCameraData stores + * a unique list of any intermediate video mux or bridge devices connected in a + * cascade, together with the entity to entity link. + * + * Entity pad configuration and link enabling happens at the end of configure(). + * We first disables all pad links on each entity device in the chain, and then + * selectively enabling the specific links to link sensor to Unicam across all + * intermediate muxes and bridges. + * + * In the cascaded topology below, if Sensor1 is used, the Mux2 -> Mux1 link + * will be disabled, and Sensor1 -> Mux1 -> Unicam links enabled. Alternatively, + * if Sensor3 is used, the Sensor2 -> Mux2 and Sensor1 -> Mux1 links are disabled, + * and Sensor3 -> Mux2 -> Mux1 -> Unicam links are enabled. All other links will + * remain unchanged. + * + * +----------+ + * | Unicam | + * +-----^----+ + * | + * +---+---+ + * | Mux1 <-------+ + * +--^----+ | + * | | + * +-----+---+ +---+---+ + * | Sensor1 | | Mux2 |<--+ + * +---------+ +-^-----+ | + * | | + * +-------+-+ +---+-----+ + * | Sensor2 | | Sensor3 | + * +---------+ +---------+ + */ +void RPiCameraData::enumerateVideoDevices(MediaLink *link) +{ + const MediaPad *sinkPad = link->sink(); + const MediaEntity *entity = sinkPad->entity(); + bool unicamFound = false; + + /* We only deal with Video Mux and Bridge devices in cascade. */ + if (entity->function() != MEDIA_ENT_F_VID_MUX && + entity->function() != MEDIA_ENT_F_VID_IF_BRIDGE) + return; + + /* Find the source pad for this Video Mux or Bridge device. */ + const MediaPad *entitySourcePad = nullptr; + for (const MediaPad *pad : entity->pads()) { + if (pad->flags() & MEDIA_PAD_FL_SOURCE) { + /* + * We can only deal with devices that have a single source + * pad. If this device has multple source pads, ignore it + * and this branch in the cascade. + */ + if (entitySourcePad) + return; + + entitySourcePad = pad; + } + } + + LOG(RPI, Info) << "Found video mux device " << entity->name() + << " linked to sink pad " << sinkPad->index(); + + bridgeDevices_.emplace_back(std::make_unique(entity), link); + bridgeDevices_.back().first->open(); + + /* + * Iterate through all the sink pad links down the cascade to find any + * other Video Mux and Bridge devices. + */ + for (MediaLink *l : entitySourcePad->links()) { + enumerateVideoDevices(l); + /* Once we reach the Unicam entity, we are done. */ + if (l->sink()->entity()->name() == "unicam-image") { + unicamFound = true; + break; + } + } + + /* This identifies the end of our entity enumeration recursion. */ + if (link->source()->entity()->function() == MEDIA_ENT_F_CAM_SENSOR) { + /* + * If Unicam is not at the end of this cascade, we cannot configure + * this topology automatically, so remove all entity references. + */ + if (!unicamFound) { + LOG(RPI, Warning) << "Cannot automatically configure this MC topology!"; + bridgeDevices_.clear(); + } + } +} + void RPiCameraData::statsMetadataComplete(uint32_t bufferId, const ControlList &controls) { if (state_ == State::Stopped)