summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-06-12 00:14:27 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-06-12 12:00:31 +0300
commit1566e67f76e949edddc03a34971ed45bee1ab4a6 (patch)
tree89b5858f2186d9a08daaab3c618c4d38da62856e /include
parent2a253be3339d70f92c8e3d63ea19bafafc628219 (diff)
ipa: rkisp1: goc: Use copydoc
Use the Doygen \copydoc command instead of duplicating documentation. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'include')
0 files changed, 0 insertions, 0 deletions
'n88' href='#n88'>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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi (Trading) Limited
 *
 * cam_helper_imx219.cpp - camera helper for imx219 sensor
 */

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * We have observed the imx219 embedded data stream randomly return junk
 * reister values.  Do not rely on embedded data until this has been resolved.
 */
#define ENABLE_EMBEDDED_DATA 0

#include "cam_helper.hpp"
#if ENABLE_EMBEDDED_DATA
#include "md_parser.hpp"
#endif

using namespace RPiController;

/* Metadata parser implementation specific to Sony IMX219 sensors. */

class MdParserImx219 : public MdParserSmia
{
public:
	MdParserImx219();
	Status Parse(void *data) override;
	Status GetExposureLines(unsigned int &lines) override;
	Status GetGainCode(unsigned int &gain_code) override;
private:
	/* Offset of the register's value in the metadata block. */
	int reg_offsets_[3];
	/* Value of the register, once read from the metadata block. */
	int reg_values_[3];
};

class CamHelperImx219 : public CamHelper
{
public:
	CamHelperImx219();
	uint32_t GainCode(double gain) const override;
	double Gain(uint32_t gain_code) const override;
	unsigned int MistrustFramesModeSwitch() const override;
	bool SensorEmbeddedDataPresent() const override;

private:
	/*
	 * Smallest difference between the frame length and integration time,
	 * in units of lines.
	 */
	static constexpr int frameIntegrationDiff = 4;
};

CamHelperImx219::CamHelperImx219()
#if ENABLE_EMBEDDED_DATA
	: CamHelper(new MdParserImx219(), frameIntegrationDiff)
#else
	: CamHelper(nullptr, frameIntegrationDiff)
#endif
{
}

uint32_t CamHelperImx219::GainCode(double gain) const
{
	return (uint32_t)(256 - 256 / gain);
}

double CamHelperImx219::Gain(uint32_t gain_code) const
{
	return 256.0 / (256 - gain_code);
}

unsigned int CamHelperImx219::MistrustFramesModeSwitch() const
{
	/*
	 * For reasons unknown, we do occasionally get a bogus metadata frame
	 * at a mode switch (though not at start-up). Possibly warrants some
	 * investigation, though not a big deal.
	 */
	return 1;
}

bool CamHelperImx219::SensorEmbeddedDataPresent() const
{
	return ENABLE_EMBEDDED_DATA;
}

static CamHelper *Create()
{
	return new CamHelperImx219();
}

static RegisterCamHelper reg("imx219", &Create);

/*
 * We care about one gain register and a pair of exposure registers. Their I2C
 * addresses from the Sony IMX219 datasheet:
 */
#define GAIN_REG 0x157
#define EXPHI_REG 0x15A
#define EXPLO_REG 0x15B

/*
 * Index of each into the reg_offsets and reg_values arrays. Must be in
 * register address order.
 */
#define GAIN_INDEX 0
#define EXPHI_INDEX 1
#define EXPLO_INDEX 2

MdParserImx219::MdParserImx219()
{
	reg_offsets_[0] = reg_offsets_[1] = reg_offsets_[2] = -1;
}

MdParser::Status MdParserImx219::Parse(void *data)
{
	bool try_again = false;

	if (reset_) {
		/*
		 * Search again through the metadata for the gain and exposure
		 * registers.
		 */
		assert(bits_per_pixel_);
		assert(num_lines_ || buffer_size_bytes_);
		/* Need to be ordered */
		uint32_t regs[3] = { GAIN_REG, EXPHI_REG, EXPLO_REG };
		reg_offsets_[0] = reg_offsets_[1] = reg_offsets_[2] = -1;
		int ret = static_cast<int>(findRegs(static_cast<uint8_t *>(data),
						    regs, reg_offsets_, 3));
		/*
		 * > 0 means "worked partially but parse again next time",
		 * < 0 means "hard error".
		 */
		if (ret > 0)
			try_again = true;
		else if (ret < 0)
			return ERROR;
	}

	for (int i = 0; i < 3; i++) {
		if (reg_offsets_[i] == -1)
			continue;

		reg_values_[i] = static_cast<uint8_t *>(data)[reg_offsets_[i]];
	}

	/* Re-parse next time if we were unhappy in some way. */
	reset_ = try_again;

	return OK;
}

MdParser::Status MdParserImx219::GetExposureLines(unsigned int &lines)
{
	if (reg_offsets_[EXPHI_INDEX] == -1 || reg_offsets_[EXPLO_INDEX] == -1)
		return NOTFOUND;

	lines = reg_values_[EXPHI_INDEX] * 256 + reg_values_[EXPLO_INDEX];

	return OK;
}

MdParser::Status MdParserImx219::GetGainCode(unsigned int &gain_code)
{
	if (reg_offsets_[GAIN_INDEX] == -1)
		return NOTFOUND;

	gain_code = reg_values_[GAIN_INDEX];

	return OK;
}