[v3,1/5] ipa: rpi: cam_helper: Add Mira016 cam helper
diff mbox series

Message ID 20261124-b4-mira016-v3-1-b71d2cf61889@ideasonboard.com
State New
Headers show
Series
  • libcamera: Add Mira016 support
Related show

Commit Message

Jacopo Mondi Nov. 24, 2026, 4:58 p.m. UTC
From: Philippe Baetens <philippebaetens@gmail.com>

AMS-OSRAM Mira016 is a global shutter 0.5Mpixel mono sensor.

Add a cam_helper to the RaspberryPi IPA to support it.

The analogue gain code is the gain in thousandths (1000 is 1x), and the
driver computes the fine gain registers from it, rounding to:

  gain = 57 * 2^n / (ADC_GAIN + 21).

The gainCode() function returns the gain times 1000, and the gain()
function computes the gain as the driver does, so the IPA knows the real
applied gain.

Signed-off-by: Philippe Baetens <philippebaetens@gmail.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 src/ipa/rpi/cam_helper/cam_helper_mira016.cpp | 78 +++++++++++++++++++++++++++
 src/ipa/rpi/cam_helper/meson.build            |  1 +
 2 files changed, 79 insertions(+)

Patch
diff mbox series

diff --git a/src/ipa/rpi/cam_helper/cam_helper_mira016.cpp b/src/ipa/rpi/cam_helper/cam_helper_mira016.cpp
new file mode 100644
index 000000000000..754948a60c7a
--- /dev/null
+++ b/src/ipa/rpi/cam_helper/cam_helper_mira016.cpp
@@ -0,0 +1,78 @@ 
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2026, ams-OSRAM
+ * Copyright (C) 2026, Ideas on Board Oy
+ *
+ * Camera helper for Mira016 sensor
+ */
+
+#include <assert.h>
+#include <cmath>
+
+#include "cam_helper.h"
+
+namespace {
+
+using namespace RPiController;
+
+class CamHelperMira016 : public CamHelper
+{
+public:
+	CamHelperMira016();
+	uint32_t gainCode(double gain) const override;
+	double gain(uint32_t code) const override;
+
+private:
+	/*
+	 * The sensor manual doesn't really specify a minimum difference between
+	 * the exposure time and the frame duration, so pick a safe value of
+	 * 4 frames. The mira016 driver uses the same value as the exposure
+	 * margin.
+	 */
+	static constexpr int frameIntegrationDiff = 4;
+	static constexpr unsigned int kAdcRampStep = 5700;
+};
+
+CamHelperMira016::CamHelperMira016()
+	: CamHelper({}, frameIntegrationDiff)
+{
+}
+
+/*
+ * The driver's analogue gain code is the gain in thousandths (1000 is 1x), up
+ * to 16000 in 8-bit modes, 4000 in 10-bit modes and only 1000 in 12-bit modes.
+ * The driver rounds it to the nearest gain the fine gain registers give:
+ *
+ *   gain = 57 * 2^n / (ADC_GAIN + 21)
+ *
+ * n is the octave of the gain (0 to 4) and ADC_GAIN is 21 to 63. Up to 4x the
+ * 8 and 10-bit modes give the same gains, so the conversion does not depend on
+ * the mode, which matters because the IPA converts the gain limits of a new
+ * mode before it passes that mode to the helper.
+ */
+uint32_t CamHelperMira016::gainCode(double gain) const
+{
+	return std::max<long>(std::lround(gain * 1000), 1000);
+}
+
+double CamHelperMira016::gain(uint32_t gainCode) const
+{
+	uint32_t code = std::clamp<uint32_t>(gainCode, 1000, 16000);
+	uint32_t n = std::floor(std::log2(84.0 * static_cast<double>(code) /
+					  kAdcRampStep));
+
+	uint32_t num = kAdcRampStep << n;
+	int adcGain = ((num + code / 2) / code) - 21;
+	adcGain = std::clamp(adcGain, 0, 63);
+
+	return static_cast<double>(num) / (adcGain + 21) / 1000.0;
+}
+
+CamHelper *create()
+{
+	return new CamHelperMira016();
+}
+
+RegisterCamHelper reg("mira016", &create);
+
+} /* namespace */
diff --git a/src/ipa/rpi/cam_helper/meson.build b/src/ipa/rpi/cam_helper/meson.build
index 6935cecf885f..6fc5f5c12f37 100644
--- a/src/ipa/rpi/cam_helper/meson.build
+++ b/src/ipa/rpi/cam_helper/meson.build
@@ -12,6 +12,7 @@  rpi_ipa_cam_helper_sources = files([
     'cam_helper_imx477.cpp',
     'cam_helper_imx519.cpp',
     'cam_helper_imx708.cpp',
+    'cam_helper_mira016.cpp',
     'cam_helper_mira220.cpp',
     'cam_helper_ov64a40.cpp',
     'cam_helper_ov7251.cpp',