summaryrefslogtreecommitdiff
path: root/src/qcam/assets/feathericons/send.svg
blob: 42ef2a24384180c179db73ce177a6ca2009eac5d (plain)
1
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-send"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>
61'>61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * A camera lens
 */

#include "libcamera/internal/camera_lens.h"

#include <libcamera/base/utils.h>

#include "libcamera/internal/v4l2_subdevice.h"

/**
 * \file camera_lens.h
 * \brief A camera lens controller
 */

namespace libcamera {

LOG_DEFINE_CATEGORY(CameraLens)

/**
 * \class CameraLens
 * \brief A camera lens based on V4L2 subdevices
 *
 * The CameraLens class eases handling of lens for pipeline handlers by
 * hiding the details of the V4L2 subdevice kernel API and caching lens
 * information.
 */

/**
 * \brief Construct a CameraLens
 * \param[in] entity The media entity backing the camera lens controller
 *
 * Once constructed the instance must be initialized with init().
 */
CameraLens::CameraLens(const MediaEntity *entity)
	: entity_(entity)
{
}

/**
 * \brief Destroy a CameraLens
 */
CameraLens::~CameraLens() = default;

/**
 * \brief Initialize the camera lens instance
 *
 * This function performs the initialisation steps of the CameraLens that may
 * fail. It shall be called once and only once after constructing the instance.
 *
 * \return 0 on success or a negative error code otherwise
 */
int CameraLens::init()
{
	if (entity_->function() != MEDIA_ENT_F_LENS) {
		LOG(CameraLens, Error)
			<< "Invalid lens function "
			<< utils::hex(entity_->function());
		return -EINVAL;
	}

	/* Create and open the subdev. */
	subdev_ = std::make_unique<V4L2Subdevice>(entity_);
	int ret = subdev_->open();
	if (ret < 0)
		return ret;

	ret = validateLensDriver();
	if (ret)
		return ret;

	model_ = subdev_->model();
	return 0;
}

/**
 * \brief This function sets the focal point of the lens to a specific position.
 * \param[in] position The focal point of the lens
 *
 * This function sets the value of focal point of the lens as in \a position.
 *
 * \return 0 on success or -EINVAL otherwise
 */
int CameraLens::setFocusPosition(int32_t position)
{
	ControlList lensCtrls(subdev_->controls());
	lensCtrls.set(V4L2_CID_FOCUS_ABSOLUTE, static_cast<int32_t>(position));

	if (subdev_->setControls(&lensCtrls))
		return -EINVAL;

	return 0;
}

int CameraLens::validateLensDriver()
{
	int ret = 0;
	static constexpr uint32_t mandatoryControls[] = {
		V4L2_CID_FOCUS_ABSOLUTE,
	};

	const ControlInfoMap &controls = subdev_->controls();
	for (uint32_t ctrl : mandatoryControls) {
		if (!controls.count(ctrl)) {
			LOG(CameraLens, Error)
				<< "Mandatory V4L2 control " << utils::hex(ctrl)
				<< " not available";
			ret = -EINVAL;
		}
	}

	if (ret) {
		LOG(CameraLens, Error)
			<< "The lens kernel driver needs to be fixed";
		LOG(CameraLens, Error)
			<< "See Documentation/lens_driver_requirements.rst in"
			<< " the libcamera sources for more information";
		return ret;
	}

	return ret;
}

/**
 * \fn CameraLens::model()
 * \brief Retrieve the lens model name
 *
 * The lens model name is a free-formed string that uniquely identifies the
 * lens model.
 *
 * \return The lens model name
 */

std::string CameraLens::logPrefix() const
{
	return "'" + entity_->name() + "'";
}

/**
 * \fn CameraLens::controls()
 * \brief Retrieve the V4L2 controls of the lens' subdev
 *
 * \return A map of the V4L2 controls supported by the lens' driver
 */
const ControlInfoMap &CameraLens::controls() const
{
	return subdev_->controls();
}

} /* namespace libcamera */