Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 Writing camera sensor drivers
0004 =============================
0005 
0006 CSI-2 and parallel (BT.601 and BT.656) busses
0007 ---------------------------------------------
0008 
0009 Please see :ref:`transmitter-receiver`.
0010 
0011 Handling clocks
0012 ---------------
0013 
0014 Camera sensors have an internal clock tree including a PLL and a number of
0015 divisors. The clock tree is generally configured by the driver based on a few
0016 input parameters that are specific to the hardware:: the external clock frequency
0017 and the link frequency. The two parameters generally are obtained from system
0018 firmware. **No other frequencies should be used in any circumstances.**
0019 
0020 The reason why the clock frequencies are so important is that the clock signals
0021 come out of the SoC, and in many cases a specific frequency is designed to be
0022 used in the system. Using another frequency may cause harmful effects
0023 elsewhere. Therefore only the pre-determined frequencies are configurable by the
0024 user.
0025 
0026 ACPI
0027 ~~~~
0028 
0029 Read the ``clock-frequency`` _DSD property to denote the frequency. The driver
0030 can rely on this frequency being used.
0031 
0032 Devicetree
0033 ~~~~~~~~~~
0034 
0035 The currently preferred way to achieve this is using ``assigned-clocks``,
0036 ``assigned-clock-parents`` and ``assigned-clock-rates`` properties. See
0037 ``Documentation/devicetree/bindings/clock/clock-bindings.txt`` for more
0038 information. The driver then gets the frequency using ``clk_get_rate()``.
0039 
0040 This approach has the drawback that there's no guarantee that the frequency
0041 hasn't been modified directly or indirectly by another driver, or supported by
0042 the board's clock tree to begin with. Changes to the Common Clock Framework API
0043 are required to ensure reliability.
0044 
0045 Frame size
0046 ----------
0047 
0048 There are two distinct ways to configure the frame size produced by camera
0049 sensors.
0050 
0051 Freely configurable camera sensor drivers
0052 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0053 
0054 Freely configurable camera sensor drivers expose the device's internal
0055 processing pipeline as one or more sub-devices with different cropping and
0056 scaling configurations. The output size of the device is the result of a series
0057 of cropping and scaling operations from the device's pixel array's size.
0058 
0059 An example of such a driver is the CCS driver (see ``drivers/media/i2c/ccs``).
0060 
0061 Register list based drivers
0062 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0063 
0064 Register list based drivers generally, instead of able to configure the device
0065 they control based on user requests, are limited to a number of preset
0066 configurations that combine a number of different parameters that on hardware
0067 level are independent. How a driver picks such configuration is based on the
0068 format set on a source pad at the end of the device's internal pipeline.
0069 
0070 Most sensor drivers are implemented this way, see e.g.
0071 ``drivers/media/i2c/imx319.c`` for an example.
0072 
0073 Frame interval configuration
0074 ----------------------------
0075 
0076 There are two different methods for obtaining possibilities for different frame
0077 intervals as well as configuring the frame interval. Which one to implement
0078 depends on the type of the device.
0079 
0080 Raw camera sensors
0081 ~~~~~~~~~~~~~~~~~~
0082 
0083 Instead of a high level parameter such as frame interval, the frame interval is
0084 a result of the configuration of a number of camera sensor implementation
0085 specific parameters. Luckily, these parameters tend to be the same for more or
0086 less all modern raw camera sensors.
0087 
0088 The frame interval is calculated using the following equation::
0089 
0090         frame interval = (analogue crop width + horizontal blanking) *
0091                          (analogue crop height + vertical blanking) / pixel rate
0092 
0093 The formula is bus independent and is applicable for raw timing parameters on
0094 large variety of devices beyond camera sensors. Devices that have no analogue
0095 crop, use the full source image size, i.e. pixel array size.
0096 
0097 Horizontal and vertical blanking are specified by ``V4L2_CID_HBLANK`` and
0098 ``V4L2_CID_VBLANK``, respectively. The unit of the ``V4L2_CID_HBLANK`` control
0099 is pixels and the unit of the ``V4L2_CID_VBLANK`` is lines. The pixel rate in
0100 the sensor's **pixel array** is specified by ``V4L2_CID_PIXEL_RATE`` in the same
0101 sub-device. The unit of that control is pixels per second.
0102 
0103 Register list based drivers need to implement read-only sub-device nodes for the
0104 purpose. Devices that are not register list based need these to configure the
0105 device's internal processing pipeline.
0106 
0107 The first entity in the linear pipeline is the pixel array. The pixel array may
0108 be followed by other entities that are there to allow configuring binning,
0109 skipping, scaling or digital crop :ref:`v4l2-subdev-selections`.
0110 
0111 USB cameras etc. devices
0112 ~~~~~~~~~~~~~~~~~~~~~~~~
0113 
0114 USB video class hardware, as well as many cameras offering a similar higher
0115 level interface natively, generally use the concept of frame interval (or frame
0116 rate) on device level in firmware or hardware. This means lower level controls
0117 implemented by raw cameras may not be used on uAPI (or even kAPI) to control the
0118 frame interval on these devices.
0119 
0120 Power management
0121 ----------------
0122 
0123 Always use runtime PM to manage the power states of your device. Camera sensor
0124 drivers are in no way special in this respect: they are responsible for
0125 controlling the power state of the device they otherwise control as well. In
0126 general, the device must be powered on at least when its registers are being
0127 accessed and when it is streaming.
0128 
0129 Existing camera sensor drivers may rely on the old
0130 struct v4l2_subdev_core_ops->s_power() callback for bridge or ISP drivers to
0131 manage their power state. This is however **deprecated**. If you feel you need
0132 to begin calling an s_power from an ISP or a bridge driver, instead please add
0133 runtime PM support to the sensor driver you are using. Likewise, new drivers
0134 should not use s_power.
0135 
0136 Please see examples in e.g. ``drivers/media/i2c/ov8856.c`` and
0137 ``drivers/media/i2c/ccs/ccs-core.c``. The two drivers work in both ACPI
0138 and DT based systems.
0139 
0140 Control framework
0141 ~~~~~~~~~~~~~~~~~
0142 
0143 ``v4l2_ctrl_handler_setup()`` function may not be used in the device's runtime
0144 PM ``runtime_resume`` callback, as it has no way to figure out the power state
0145 of the device. This is because the power state of the device is only changed
0146 after the power state transition has taken place. The ``s_ctrl`` callback can be
0147 used to obtain device's power state after the power state transition:
0148 
0149 .. c:function:: int pm_runtime_get_if_in_use(struct device *dev);
0150 
0151 The function returns a non-zero value if it succeeded getting the power count or
0152 runtime PM was disabled, in either of which cases the driver may proceed to
0153 access the device.