Jump to content

Camera

From Nura Wiki
Note This page is written to the best (but limited) knowledge. If you have any improvements to the text, feel free to edit the page and correct or expand it. Contributions welcome!

A smartphone normally has multiple cameras (also named sensors) which are made by companies such as Sony, OmniVision or Samsung.

These are normally controlled via an I2C bus and deliver image data via MIPI CSI-2 (Camera Serial Interface). Some sensors are connected via a parallel interface rather than serial (CSI-2) which seems to be called digital video port (DVP). This fact is currently mostly ignored in this article and covers CSI-2 only for now. Many of the same concepts should apply to parallel also.

In many cases, the sensor package also comes with additional components:

  • VCM (voice coil motor) actuator which can physically move the lens for focusing, image stabilization or optical zoom;
  • EEPROM, which presumably contains calibration data and some other details.

A sensor normally has the following connections to the SoC:

  • A clock input, commonly named MCLK or XVCLK
  • Several power inputs, commonly named e.g. AVDD (analog supply), DVDD (digital supply), DOVDD/IOVCC (I/O supply)
  • One or more GPIOs for reset and/or shutdown purposes. These are not used in some setups when e.g. the reset pin is connected to DOVDD, so when the power supplies are on, the sensor is on.
  • Two pins for the I2C connection (SDA & SCL). Also named SCCB (serial camera control bus) or CCI (camera control instance or camera control interface) or just 2-wire serial communication
  • Between 4 and 10 pins for MIPI CSI-2 connection (2 pins for the clock lane, 2 pins for each of the 1-4 data lanes depending on the sensor and setup)

On the SoC side, the following components exist:

  • I2C bus (one or more, sometimes shared between multiple camera sensors, sometimes one camera sensor per bus)
  • Receiver of the physical MIPI CSI-2 signals (multiple in most cases)
  • Various other blocks for (efficient) image processing

Some sensors support lane remapping or lane swap for the MIPI CSI-2 connection, when the hardware design requires that. The sensor must also be configured for the right number of lanes from the driver side (declared in data-lanes property) so that it sends the data correctly.

Sensor example

Below is a device tree example of a sensor where we provide the aforementioned clocks, supplies and GPIOs. The pinctrl ensures that the MCLK0 pin on the SoC is set to the correct function.

The sensor node can also provide orientation (front or rear camera) and rotation (0°, 90°, 180°, 270°) which define metadata for user space.

You can also link lens-focus and flash-leds to the VCM and flash LED respectively. This information can (or at least could) be used by user space to e.g. trigger the flash when you take a picture with the rear camera. Note that the flash node in the device tree must actually correspond to a driver that exposes the flash as a V4L2 (!) subdevice, not just as a Linux LED.

For more information about these properties, see Documentation/devicetree/bindings/media/video-interface-devices.yaml.

&cci_i2c0 {
	ov8865: camera-sensor@10 {
		compatible = "ovti,ov8865";
		reg = <0x10>;

		pinctrl-0 = <&mclk0_pin_a>;
		pinctrl-names = "default";

		clocks = <&mmcc CAMSS_MCLK0_CLK>;

		avdd-supply = <&pm8941_l17>;
		dovdd-supply = <&pm8941_lvs3>;
		dvdd-supply = <&pm8941_l3>;

		powerdown-gpios = <&tlmm 89 GPIO_ACTIVE_LOW>;
		reset-gpios = <&tlmm 90 GPIO_ACTIVE_LOW>;

		orientation = <1>; /* Rear camera */
		rotation = <90>;

		lens-focus = <&ad5823>;
		flash-leds = <&pm8941_flash>;

		port {
			ov8865_ep: endpoint {
				data-lanes = <1 2 3 4>;
				bus-type = <MEDIA_BUS_TYPE_CSI2_DPHY>;
				link-frequencies = /bits/ 64 <360000000>;
				remote-endpoint = <&csiphy0_ep>;
			};
		};
	};
};

Sensors status

Sensor Parent Driver Status I2C Address Found in Notes
Hynix Hi-556 5MP sr556 hynix,sr556 WIP sdm450-samsung-a6plte-r4
OmniVision OV5670 5MP ov5670 ovti,ov5670 Y 0x36 or 0x10 msm8953-xiaomi-markw
OmniVision OV5675 5MP ov5675 ovti,ov5675 Y 0x36 or 0x10 msm8953-xiaomi-daisy
msm8953-xiaomi-vince
msm8917-motorola-nora
OmniVision OV8856 8MP ov8856 ovti,ov8856 0x36 or 0x10 sm7150-xiaomi-davinci Untested, but the probe is successful
OmniVision OV8865 8MP ov8865 ovti,ov8865 Y 0x36 or 0x10 msm8916-wingtech-wt88047 Usage in Redmi 2: https://github.com/msm8916-mainline/linux/pull/345
Samsung ISOCELL 3L6 13MP s5k3l6 samsung,s5k3l6xx WIP imx8mq-librem5
sm7150-xiaomi-davinci
sm7150-xiaomi-davinci is untested, driver probes with changes to allow 19.2MHz clock
Samsung ISOCELL 3L8 13MP s5k3l8 samsung,s5k3l8 samsung,s5k3l8-untested WIP 0x2d or 0x10 msm8953-xiaomi-markw
msm8953-xiaomi-mido
msm8937-xiaomi-land
msm8937-xiaomi-prada
msm8917-motorola-nora
Samsung ISOCELL 3P8 16MP s5k3p8 samsung,s5k3p8sp samsung,s5k3p8-untested WIP 0x2d or 0x10 msm8953-xiaomi-ysl
Samsung LSI S5C73M3 8MP S5C73M3 samsung,s5c73m3 Y exynos4412-samsung-{m0,m3,t03g,t0lte} TRY syscalls need implementing (see FIXME in s5c73m3-core.c).
Sony IMX214 13MP imx214 sony,imx214 Y msm8916-longcheer-l8910
msm8939-longcheer-l9100
Sony IMX355 8MP imx355 sony,imx355 Y sdm670-google-sargo
Sony IMX363 12MP imx363 sony,imx363 Y sdm670-google-sargo

Qualcomm

Qualcomm SoCs contain a block called CAMSS (camera subsystem). For more information about some of the components that are part of it, see the Linux Kernel documentation on the CAMSS driver.

A Qualcomm SoC usually contains between two and four 4-lane CSIPHY receivers, where each sensor is connected to one dedicated CSIPHY. For bring-up, you can focus on the CSIPHY receivers and probably ignore the other components of the subsystem.

The I2C bus used for the camera sensors is not a regular QUP/BLSP bus: it's the CCI (camera control interface), which has a separate driver in Linux.

Example

The following example shows how the CAMSS side can be configured in the dts. Each port here is a CSIPHY receiver, i.e. the part that receives CSI-2 data. In this example, CSIPHY0 and CSIPHY2 are used. CSIPHY1 is unused.

In the schematics, there are 5 equal lanes per CSIPHY, where normally (e.g. for 4 data lanes) the CSI data lanes are connected to lane 0, 2, 3 and 4, while the clock lane is connected to lane 1. This is represented using the clock-lanes and data-lanes properties.

Note Both properties refer to the lane number and aren't a count of how many lanes are used for a particular purpose. A sensor with one data lane would have data-lanes set to 0 and clock-lanes set to 1 on the SoC side.

For more information about these properties, see Documentation/devicetree/bindings/media/video-interfaces.yaml.

&camss {
	vdda-supply = <&pm8941_l12>;
	
	status = "okay";

	ports {
		port@0 {
			reg = <0>;
			csiphy0_ep: endpoint {
				clock-lanes = <1>;
				data-lanes = <0 2 3 4>;
				bus-type = <MEDIA_BUS_TYPE_CSI2_DPHY>;
				link-frequencies = /bits/ 64 <360000000>;
				remote-endpoint = <&ov8865_ep>;
			};
		};
		port@2 {
			reg = <2>;
			/* [...] */
		};
	};
};

Downstream devicetree

In most cases, the downstream device tree does not adequately describe the actual hardware setup, but you can still extract some useful information from it.

  • csiphy-sd-index: which CSIPHY the sensor is connected to.
  • cci-master: which CCI (I2C) bus the sensor sits on.
  • clocks: the clock used for the sensor.
  • gpios: various GPIOs (reset, shutdown, etc). Double-check the effective function of the GPIO with the names provided in the dts. For example, CAM_VDIG would be a fixed-regulator that provides power to one of the supply rails. You can probably ignore the gpios named CAMIF_MCLK.

Bring-up

Before bringing up the sensor, you should make sure the I2C bus (CCI) works correctly. For this, you can enable the bus in the dts and run i2cdetect (needs kernel module i2c-dev): the output may show no devices listed, since everything's turned off by default. If it hangs and there are timeout errors in dmesg, make sure that the pull-up for the I2C bus is enabled. In many cases, this is a regulator used to supply a camera. If unsure, try enabling various regulators by putting regulator-always-on; on the regulator node.

To enable the sensor, I2C communication must first be established. Enable the required regulators and force enable the sensor clock with regulator-fixed-clock and regulator-always-on. The pinctrl must be configured properly, otherwise the clock signal won't actually reach the sensor.

An example of forcing on mclk2:

/ {
    fake-reg-mclk2 {
        compatible = "regulator-fixed-clock";
        regulator-name = "fake-reg-mclk2";
        pinctrl-names = "default";
        pinctrl-0 = <&cam_mclk2_default>;
        clocks = <&camcc CAM_CC_MCLK2_CLK>;
        regulator-always-on;
    };
};

&tlmm {
    cam_mclk2_default: cam-mclk2-default {
        pins = "gpio31";
        function = "cam_mclk2";
        drive-strength = <2>;
        bias-disable;
    };
};

Verify that a sensor appears in i2cdetect. If multiple I2C addresses show up, only one of them is the actual sensor, while the others might be the EEPROM or VCM.

If you have access to a datasheet for the sensor, be careful as with any I2C device the address might be given as an 8-bit address while Linux always uses 7-bit addresses. For example, if the datasheet says the address is 0x20, it might actually be 0x10 instead (shift one bit to the right).

When you know which I2C address the sensor uses and it appears, try reading to the chip ID register to make sure communication really works and you're talking to the correct chip.

Some sensors may use 16-bit registers. In the following example, the IMX582 has register 0x0016 which holds the value 0x0582, and the sensor is connected to I2C bus 3 (check i2cdetect -l) with the address 0x1a.

$ i2cset -y 3 0x1a 0x00 0x16
$ i2cget -y 3 0x1a
0x05
$ i2cget -y 3 0x1a
0x82
TODO TODO: Expand on CSI-2 stuff and what to do after I2C communication is established. (+ Some drivers in Linux support configuration of the number of CSI-2 lanes (data-lanes) but some do not.)

Testing

TODO TODO: Introduce manual exposure and gain with v4l-ctl, "media-ctl -p", cam, bayer format, test patterns in ISP and sensor, v4l-ctl for flash/torch, example for qcam usage that works on FP2

libcamera

There is a set of useful utilities to troubleshoot the libcamera stack: libcamera-tools qcam. The most useful commands include:

  • cam --list: Looks for cameras in different locations, printing useful debug output and a list of the cameras it detected. The number at the beginning of an entry will be used to identify cameras in later commands.
  • cam --camera #num --list-properties: If a camera is found, this command lists the camera's properties. It is recommended to look for the location and rotation properties, as they are often missing from the device tree files.
  • cam --camera #num --info: Lists all modes reported by the camera. This can be useful to troubleshoot missing resolutions on formats.
  • qcam: Opens a new window that allows selecting the right camera to test a direct video streaming from libcamera. This command needs to be run in a terminal with the whole environment setup.

In addition to that, as of July 2024, libcamera requires big contiguous-memory-address (CMA) buffers. Their size can be checked with cat /proc/meminfo | grep Cma, and is usually in the order of a hundred megabytes. These buffers can be configured in two ways:

  1. Adding cma=256M to the kernel command-line parameters. For testing, it can be added to the append variable in /etc/deviceinfo, after which sudo mkinitfs must be run.
  2. Modifying CONFIG_CMA_SIZE_MBYTES in the kernel config. (proposal to do it by default: pmaports#3368)

Routing

Some camera-related drivers (such as the Qualcomm Camera Subsystem) use the kernel Media Controller API to expose routing controls to user space. Routing can be done manually using the media-ctl command.

The routing entities have names and pads. All entities can be listed with this information using the command:

$ media-ctl -p
Media controller API version ...

Media device information
------------------------
...

Device topology
- entity 1: msm_csiphy0 (2 pads, 4 links)
            ...
 	pad0: Sink
		[fmt:UYVY8_2X8/1920x1080 field:none colorspace:srgb]
		<- "imx355 4-001a":0 [ENABLED,IMMUTABLE]
	pad1: Source
		[fmt:UYVY8_2X8/1920x1080 field:none colorspace:srgb]
		-> "msm_csid0":0 []
		-> "msm_csid1":0 []
		-> "msm_csid2":0 []
...

In this example, there is an enabled link between entity "msm_csiphy0", pad 0, and entity "imx355 4-001a", pad 0.

Links can be enabled and disabled with the command media-ctl -l '"ENTITY":PAD->"ENTITY":PAD[ENABLED]'. Here is an example that enables then disables the link between msm_csiphy0 (pad 1) and msm_csid0 (pad 0):

$ media-ctl -l '"msm_csiphy0":1->"msm_csid0":0[1]'
$ media-ctl -l '"msm_csiphy0":1->"msm_csid0":0[0]'

The formats should be consistent between pads, unless your driver supports format shifting. The format of the pads can be changed with media-ctl -V:

$ media-ctl -V '"msm_csiphy0":0[fmt:SRGGB10_1X10/3280x2464]'

Apps

There are multiple camera apps available, in various states of development and functionality.

Tweaks

Restarting Pipewire

At least on SDM845 devices, libcamera + WirePlumber is unstable. Restarting WirePlumber or the Camera app can help.

To make daily driving less painful, PipeWire can be configured to restart .desktop. Create a cam-refresh file in /usr/bin with the following content:

/usr/bin/cam-refresh
#!/usr/bin/env bash
echo "Killing Snapshot"
killall snapshot # It will freeze when pipewire stops
sleep 0.5 # Sleep can help with stream freeze
echo "Killing WirePlumber"
killall wireplumber
echo "Killing PipeWire"
killall pipewire
sleep 0.1
echo "Starting PipeWire"
pipewire &
sleep 1
echo "Starting WirePlumber"
wireplumber &

Mark it as executable:

$ sudo chmod +x /usr/bin/cam-refresh

Then create a .desktop file in /usr/share/applications/ with the following content:

/usr/share/applications/camera-refresh.desktop
[Desktop Entry]
Type=Application
Name=Refresh camera
Icon=camera-switch-symbolic
Exec=cam-refresh
Terminal=false

See also