1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunWriting camera sensor drivers 4*4882a593Smuzhiyun============================= 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunCSI-2 7*4882a593Smuzhiyun----- 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunPlease see what is written on :ref:`MIPI_CSI_2`. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunHandling clocks 12*4882a593Smuzhiyun--------------- 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunCamera sensors have an internal clock tree including a PLL and a number of 15*4882a593Smuzhiyundivisors. The clock tree is generally configured by the driver based on a few 16*4882a593Smuzhiyuninput parameters that are specific to the hardware:: the external clock frequency 17*4882a593Smuzhiyunand the link frequency. The two parameters generally are obtained from system 18*4882a593Smuzhiyunfirmware. No other frequencies should be used in any circumstances. 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunThe reason why the clock frequencies are so important is that the clock signals 21*4882a593Smuzhiyuncome out of the SoC, and in many cases a specific frequency is designed to be 22*4882a593Smuzhiyunused in the system. Using another frequency may cause harmful effects 23*4882a593Smuzhiyunelsewhere. Therefore only the pre-determined frequencies are configurable by the 24*4882a593Smuzhiyunuser. 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunFrame size 27*4882a593Smuzhiyun---------- 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunThere are two distinct ways to configure the frame size produced by camera 30*4882a593Smuzhiyunsensors. 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunFreely configurable camera sensor drivers 33*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunFreely configurable camera sensor drivers expose the device's internal 36*4882a593Smuzhiyunprocessing pipeline as one or more sub-devices with different cropping and 37*4882a593Smuzhiyunscaling configurations. The output size of the device is the result of a series 38*4882a593Smuzhiyunof cropping and scaling operations from the device's pixel array's size. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunAn example of such a driver is the smiapp driver (see drivers/media/i2c/smiapp). 41*4882a593Smuzhiyun 42*4882a593SmuzhiyunRegister list based drivers 43*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~ 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunRegister list based drivers generally, instead of able to configure the device 46*4882a593Smuzhiyunthey control based on user requests, are limited to a number of preset 47*4882a593Smuzhiyunconfigurations that combine a number of different parameters that on hardware 48*4882a593Smuzhiyunlevel are independent. How a driver picks such configuration is based on the 49*4882a593Smuzhiyunformat set on a source pad at the end of the device's internal pipeline. 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunMost sensor drivers are implemented this way, see e.g. 52*4882a593Smuzhiyundrivers/media/i2c/imx319.c for an example. 53*4882a593Smuzhiyun 54*4882a593SmuzhiyunFrame interval configuration 55*4882a593Smuzhiyun---------------------------- 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunThere are two different methods for obtaining possibilities for different frame 58*4882a593Smuzhiyunintervals as well as configuring the frame interval. Which one to implement 59*4882a593Smuzhiyundepends on the type of the device. 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunRaw camera sensors 62*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 63*4882a593Smuzhiyun 64*4882a593SmuzhiyunInstead of a high level parameter such as frame interval, the frame interval is 65*4882a593Smuzhiyuna result of the configuration of a number of camera sensor implementation 66*4882a593Smuzhiyunspecific parameters. Luckily, these parameters tend to be the same for more or 67*4882a593Smuzhiyunless all modern raw camera sensors. 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunThe frame interval is calculated using the following equation:: 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun frame interval = (analogue crop width + horizontal blanking) * 72*4882a593Smuzhiyun (analogue crop height + vertical blanking) / pixel rate 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunThe formula is bus independent and is applicable for raw timing parameters on 75*4882a593Smuzhiyunlarge variety of devices beyond camera sensors. Devices that have no analogue 76*4882a593Smuzhiyuncrop, use the full source image size, i.e. pixel array size. 77*4882a593Smuzhiyun 78*4882a593SmuzhiyunHorizontal and vertical blanking are specified by ``V4L2_CID_HBLANK`` and 79*4882a593Smuzhiyun``V4L2_CID_VBLANK``, respectively. The unit of these controls are lines. The 80*4882a593Smuzhiyunpixel rate is specified by ``V4L2_CID_PIXEL_RATE`` in the same sub-device. The 81*4882a593Smuzhiyununit of that control is Hz. 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunRegister list based drivers need to implement read-only sub-device nodes for the 84*4882a593Smuzhiyunpurpose. Devices that are not register list based need these to configure the 85*4882a593Smuzhiyundevice's internal processing pipeline. 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunThe first entity in the linear pipeline is the pixel array. The pixel array may 88*4882a593Smuzhiyunbe followed by other entities that are there to allow configuring binning, 89*4882a593Smuzhiyunskipping, scaling or digital crop :ref:`v4l2-subdev-selections`. 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunUSB cameras etc. devices 92*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunUSB video class hardware, as well as many cameras offering a similar higher 95*4882a593Smuzhiyunlevel interface natively, generally use the concept of frame interval (or frame 96*4882a593Smuzhiyunrate) on device level in firmware or hardware. This means lower level controls 97*4882a593Smuzhiyunimplemented by raw cameras may not be used on uAPI (or even kAPI) to control the 98*4882a593Smuzhiyunframe interval on these devices. 99*4882a593Smuzhiyun 100*4882a593SmuzhiyunPower management 101*4882a593Smuzhiyun---------------- 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunAlways use runtime PM to manage the power states of your device. Camera sensor 104*4882a593Smuzhiyundrivers are in no way special in this respect: they are responsible for 105*4882a593Smuzhiyuncontrolling the power state of the device they otherwise control as well. In 106*4882a593Smuzhiyungeneral, the device must be powered on at least when its registers are being 107*4882a593Smuzhiyunaccessed and when it is streaming. 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunExisting camera sensor drivers may rely on the old 110*4882a593Smuzhiyun:c:type:`v4l2_subdev_core_ops`->s_power() callback for bridge or ISP drivers to 111*4882a593Smuzhiyunmanage their power state. This is however **deprecated**. If you feel you need 112*4882a593Smuzhiyunto begin calling an s_power from an ISP or a bridge driver, instead please add 113*4882a593Smuzhiyunruntime PM support to the sensor driver you are using. Likewise, new drivers 114*4882a593Smuzhiyunshould not use s_power. 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunPlease see examples in e.g. ``drivers/media/i2c/ov8856.c`` and 117*4882a593Smuzhiyun``drivers/media/i2c/smiapp/smiapp-core.c``. The two drivers work in both ACPI 118*4882a593Smuzhiyunand DT based systems. 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunControl framework 121*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~ 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun``v4l2_ctrl_handler_setup()`` function may not be used in the device's runtime 124*4882a593SmuzhiyunPM ``runtime_resume`` callback, as it has no way to figure out the power state 125*4882a593Smuzhiyunof the device. This is because the power state of the device is only changed 126*4882a593Smuzhiyunafter the power state transition has taken place. The ``s_ctrl`` callback can be 127*4882a593Smuzhiyunused to obtain device's power state after the power state transition: 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun.. c:function:: 130*4882a593Smuzhiyun int pm_runtime_get_if_in_use(struct device *dev); 131*4882a593Smuzhiyun 132*4882a593SmuzhiyunThe function returns a non-zero value if it succeeded getting the power count or 133*4882a593Smuzhiyunruntime PM was disabled, in either of which cases the driver may proceed to 134*4882a593Smuzhiyunaccess the device. 135