From patchwork Fri Apr 24 21:53:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 3529 Return-Path: Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 276EA62F6D for ; Fri, 24 Apr 2020 23:50:15 +0200 (CEST) X-Originating-IP: 87.3.55.240 Received: from uno.homenet.telecomitalia.it (host240-55-dynamic.3-87-r.retail.telecomitalia.it [87.3.55.240]) (Authenticated sender: jacopo@jmondi.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 70E2F60002; Fri, 24 Apr 2020 21:50:14 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Fri, 24 Apr 2020 23:53:02 +0200 Message-Id: <20200424215304.558317-12-jacopo@jmondi.org> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200424215304.558317-1-jacopo@jmondi.org> References: <20200424215304.558317-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 11/13] libcamera: camera_sensor: Add method to get sensor info 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: , X-List-Received-Date: Fri, 24 Apr 2020 21:50:15 -0000 Add method to retrieve the CameraSensorInfo to the CameraSensor class. Signed-off-by: Jacopo Mondi --- src/libcamera/camera_sensor.cpp | 84 +++++++++++++++++++++++++++ src/libcamera/include/camera_sensor.h | 1 + 2 files changed, 85 insertions(+) diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index e39f277622ae..c1ef4adb579c 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -492,6 +492,90 @@ int CameraSensor::setControls(ControlList *ctrls) * \return The list of camera sensor properties */ +/** + * \brief Assemble and return the camera sensor info + * \param[out] info The camera sensor info that report the sensor information + * + * The CameraSensorInfo content is assembled by inspecting the currently + * applied sensor configuration and the sensor static properties. + * + * \return 0 on success, a negative error code otherwise + */ +int CameraSensor::sensorInfo(CameraSensorInfo *info) const +{ + /* + * Make sure the sub-device exports all the controls we need to operate. + * + * Currently V4L2_CID_PIXEL_RATE and V4L2_CID_HBLANK are required. + * + * \todo This is an hard policy that all sensors that want to export + * a properly populated CameraSensorInfo have to adhere to. Consider if + * it's worth relaxing it and providing default values instead. + */ + const ControlInfoMap &controlMap = subdev_->controls(); + if (controlMap.find(V4L2_CID_PIXEL_RATE) == controlMap.end()) { + LOG(CameraSensor, Error) << "'Pixel rate' control not available"; + return -EINVAL; + } + + if (controlMap.find(V4L2_CID_HBLANK) == controlMap.end()) { + LOG(CameraSensor, Error) << "'HBLANK' control not available"; + return -EINVAL; + } + + /* + * Construct the camera sensor name using the media entity name. + * + * \todo There is no standardized naming scheme for sensor entities + * in the Linux kernel at the moment. The most common naming scheme + * is the one obtained by associating the sensor name and its I2C + * device and bus addresses in the form of: "devname i2c-adapt:i2c-addr" + * Assume this is the standard naming scheme and parse the first part + * of the entity name to obtain "devname". + */ + std::string entityName = subdev_->entity()->name(); + info->name = *utils::split(entityName, " ").begin(); + + /* + * Get the active area size from the ActiveAreas property. + * + * \todo The ActiveAreas property aims to describe multiple + * active area rectangles. At the moment only a single active + * area rectangle is exposed by the subdevice API. Use that single + * rectangle width and height to construct the ActiveAreaSize. + */ + Span activeArea = properties_.get(properties::ActiveAreas); + info->activeAreaSize = { static_cast(activeArea[2]), + static_cast(activeArea[3]) }; + + /* The bit-depth and image size depend on the currently applied format. */ + V4L2SubdeviceFormat format{}; + int ret = subdev_->getFormat(0, &format); + if (ret) + return ret; + info->bitsPerPixel = format.bitsPerPixel(); + info->outputSize = format.size; + + /* It's mandatory for the subdevice to report its crop rectangle. */ + ret = subdev_->getCropRectangle(0, &info->analogCrop); + if (ret) { + LOG(CameraSensor, Error) + << "Failed to construct camera sensor info: " + << "the camera sensor does not report the crop rectangle"; + return ret; + } + + int64_t pixelRate; + subdev_->getControl(V4L2_CID_PIXEL_RATE, &pixelRate); + info->pixelClock = pixelRate; + + int32_t hblank; + subdev_->getControl(V4L2_CID_HBLANK, &hblank); + info->lineLength = info->outputSize.width + hblank; + + return 0; +} + std::string CameraSensor::logPrefix() const { return "'" + subdev_->entity()->name() + "'"; diff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h index e19852d4cabe..b162c3886b1d 100644 --- a/src/libcamera/include/camera_sensor.h +++ b/src/libcamera/include/camera_sensor.h @@ -61,6 +61,7 @@ public: int setControls(ControlList *ctrls); const ControlList &properties() const { return properties_; } + int sensorInfo(CameraSensorInfo *info) const; protected: std::string logPrefix() const;