xref: /openbmc/linux/Documentation/driver-api/media/camera-sensor.rst (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1e4cf8c58SSakari Ailus.. SPDX-License-Identifier: GPL-2.0
2e4cf8c58SSakari Ailus
3e4cf8c58SSakari AilusWriting camera sensor drivers
4e4cf8c58SSakari Ailus=============================
5e4cf8c58SSakari Ailus
6b9a54336SSakari AilusCSI-2 and parallel (BT.601 and BT.656) busses
7b9a54336SSakari Ailus---------------------------------------------
8e4cf8c58SSakari Ailus
9b9a54336SSakari AilusPlease see :ref:`transmitter-receiver`.
10e4cf8c58SSakari Ailus
11e4cf8c58SSakari AilusHandling clocks
12e4cf8c58SSakari Ailus---------------
13e4cf8c58SSakari Ailus
14e4cf8c58SSakari AilusCamera sensors have an internal clock tree including a PLL and a number of
15e4cf8c58SSakari Ailusdivisors. The clock tree is generally configured by the driver based on a few
16e4cf8c58SSakari Ailusinput parameters that are specific to the hardware:: the external clock frequency
17e4cf8c58SSakari Ailusand the link frequency. The two parameters generally are obtained from system
182225cf44SSakari Ailusfirmware. **No other frequencies should be used in any circumstances.**
19e4cf8c58SSakari Ailus
20e4cf8c58SSakari AilusThe reason why the clock frequencies are so important is that the clock signals
21e4cf8c58SSakari Ailuscome out of the SoC, and in many cases a specific frequency is designed to be
22e4cf8c58SSakari Ailusused in the system. Using another frequency may cause harmful effects
23e4cf8c58SSakari Ailuselsewhere. Therefore only the pre-determined frequencies are configurable by the
24e4cf8c58SSakari Ailususer.
25e4cf8c58SSakari Ailus
262225cf44SSakari AilusACPI
272225cf44SSakari Ailus~~~~
282225cf44SSakari Ailus
29b9a54336SSakari AilusRead the ``clock-frequency`` _DSD property to denote the frequency. The driver
30b9a54336SSakari Ailuscan rely on this frequency being used.
312225cf44SSakari Ailus
322225cf44SSakari AilusDevicetree
332225cf44SSakari Ailus~~~~~~~~~~
342225cf44SSakari Ailus
35b9a54336SSakari AilusThe currently preferred way to achieve this is using ``assigned-clocks``,
36b9a54336SSakari Ailus``assigned-clock-parents`` and ``assigned-clock-rates`` properties. See
37b9a54336SSakari Ailus``Documentation/devicetree/bindings/clock/clock-bindings.txt`` for more
38b9a54336SSakari Ailusinformation. The driver then gets the frequency using ``clk_get_rate()``.
392225cf44SSakari Ailus
402225cf44SSakari AilusThis approach has the drawback that there's no guarantee that the frequency
412225cf44SSakari Ailushasn't been modified directly or indirectly by another driver, or supported by
422225cf44SSakari Ailusthe board's clock tree to begin with. Changes to the Common Clock Framework API
432225cf44SSakari Ailusare required to ensure reliability.
442225cf44SSakari Ailus
45e4cf8c58SSakari AilusFrame size
46e4cf8c58SSakari Ailus----------
47e4cf8c58SSakari Ailus
48e4cf8c58SSakari AilusThere are two distinct ways to configure the frame size produced by camera
49e4cf8c58SSakari Ailussensors.
50e4cf8c58SSakari Ailus
51e4cf8c58SSakari AilusFreely configurable camera sensor drivers
52e4cf8c58SSakari Ailus~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53e4cf8c58SSakari Ailus
54e4cf8c58SSakari AilusFreely configurable camera sensor drivers expose the device's internal
55e4cf8c58SSakari Ailusprocessing pipeline as one or more sub-devices with different cropping and
56e4cf8c58SSakari Ailusscaling configurations. The output size of the device is the result of a series
57e4cf8c58SSakari Ailusof cropping and scaling operations from the device's pixel array's size.
58e4cf8c58SSakari Ailus
59b9a54336SSakari AilusAn example of such a driver is the CCS driver (see ``drivers/media/i2c/ccs``).
60e4cf8c58SSakari Ailus
61e4cf8c58SSakari AilusRegister list based drivers
62e4cf8c58SSakari Ailus~~~~~~~~~~~~~~~~~~~~~~~~~~~
63e4cf8c58SSakari Ailus
64e4cf8c58SSakari AilusRegister list based drivers generally, instead of able to configure the device
65e4cf8c58SSakari Ailusthey control based on user requests, are limited to a number of preset
66e4cf8c58SSakari Ailusconfigurations that combine a number of different parameters that on hardware
67e4cf8c58SSakari Ailuslevel are independent. How a driver picks such configuration is based on the
68e4cf8c58SSakari Ailusformat set on a source pad at the end of the device's internal pipeline.
69e4cf8c58SSakari Ailus
70e4cf8c58SSakari AilusMost sensor drivers are implemented this way, see e.g.
71b9a54336SSakari Ailus``drivers/media/i2c/imx319.c`` for an example.
72e4cf8c58SSakari Ailus
73e4cf8c58SSakari AilusFrame interval configuration
74e4cf8c58SSakari Ailus----------------------------
75e4cf8c58SSakari Ailus
76e4cf8c58SSakari AilusThere are two different methods for obtaining possibilities for different frame
77e4cf8c58SSakari Ailusintervals as well as configuring the frame interval. Which one to implement
78e4cf8c58SSakari Ailusdepends on the type of the device.
79e4cf8c58SSakari Ailus
80e4cf8c58SSakari AilusRaw camera sensors
81e4cf8c58SSakari Ailus~~~~~~~~~~~~~~~~~~
82e4cf8c58SSakari Ailus
83e4cf8c58SSakari AilusInstead of a high level parameter such as frame interval, the frame interval is
84e4cf8c58SSakari Ailusa result of the configuration of a number of camera sensor implementation
85e4cf8c58SSakari Ailusspecific parameters. Luckily, these parameters tend to be the same for more or
86e4cf8c58SSakari Ailusless all modern raw camera sensors.
87e4cf8c58SSakari Ailus
88e4cf8c58SSakari AilusThe frame interval is calculated using the following equation::
89e4cf8c58SSakari Ailus
90e4cf8c58SSakari Ailus	frame interval = (analogue crop width + horizontal blanking) *
91e4cf8c58SSakari Ailus			 (analogue crop height + vertical blanking) / pixel rate
92e4cf8c58SSakari Ailus
93e4cf8c58SSakari AilusThe formula is bus independent and is applicable for raw timing parameters on
94e4cf8c58SSakari Ailuslarge variety of devices beyond camera sensors. Devices that have no analogue
95e4cf8c58SSakari Ailuscrop, use the full source image size, i.e. pixel array size.
96e4cf8c58SSakari Ailus
97e4cf8c58SSakari AilusHorizontal and vertical blanking are specified by ``V4L2_CID_HBLANK`` and
98b9a54336SSakari Ailus``V4L2_CID_VBLANK``, respectively. The unit of the ``V4L2_CID_HBLANK`` control
99b9a54336SSakari Ailusis pixels and the unit of the ``V4L2_CID_VBLANK`` is lines. The pixel rate in
100b9a54336SSakari Ailusthe sensor's **pixel array** is specified by ``V4L2_CID_PIXEL_RATE`` in the same
101b9a54336SSakari Ailussub-device. The unit of that control is pixels per second.
102e4cf8c58SSakari Ailus
103e4cf8c58SSakari AilusRegister list based drivers need to implement read-only sub-device nodes for the
104e4cf8c58SSakari Ailuspurpose. Devices that are not register list based need these to configure the
105e4cf8c58SSakari Ailusdevice's internal processing pipeline.
106e4cf8c58SSakari Ailus
107e4cf8c58SSakari AilusThe first entity in the linear pipeline is the pixel array. The pixel array may
108e4cf8c58SSakari Ailusbe followed by other entities that are there to allow configuring binning,
109e4cf8c58SSakari Ailusskipping, scaling or digital crop :ref:`v4l2-subdev-selections`.
110e4cf8c58SSakari Ailus
111e4cf8c58SSakari AilusUSB cameras etc. devices
112e4cf8c58SSakari Ailus~~~~~~~~~~~~~~~~~~~~~~~~
113e4cf8c58SSakari Ailus
114e4cf8c58SSakari AilusUSB video class hardware, as well as many cameras offering a similar higher
115e4cf8c58SSakari Ailuslevel interface natively, generally use the concept of frame interval (or frame
116e4cf8c58SSakari Ailusrate) on device level in firmware or hardware. This means lower level controls
117e4cf8c58SSakari Ailusimplemented by raw cameras may not be used on uAPI (or even kAPI) to control the
118e4cf8c58SSakari Ailusframe interval on these devices.
119e4cf8c58SSakari Ailus
120e4cf8c58SSakari AilusPower management
121e4cf8c58SSakari Ailus----------------
122e4cf8c58SSakari Ailus
123e4cf8c58SSakari AilusAlways use runtime PM to manage the power states of your device. Camera sensor
124e4cf8c58SSakari Ailusdrivers are in no way special in this respect: they are responsible for
125e4cf8c58SSakari Ailuscontrolling the power state of the device they otherwise control as well. In
126e4cf8c58SSakari Ailusgeneral, the device must be powered on at least when its registers are being
127e4cf8c58SSakari Ailusaccessed and when it is streaming.
128e4cf8c58SSakari Ailus
129e4cf8c58SSakari AilusExisting camera sensor drivers may rely on the old
130b9a54336SSakari Ailusstruct v4l2_subdev_core_ops->s_power() callback for bridge or ISP drivers to
131e4cf8c58SSakari Ailusmanage their power state. This is however **deprecated**. If you feel you need
132e4cf8c58SSakari Ailusto begin calling an s_power from an ISP or a bridge driver, instead please add
133e4cf8c58SSakari Ailusruntime PM support to the sensor driver you are using. Likewise, new drivers
134e4cf8c58SSakari Ailusshould not use s_power.
135e4cf8c58SSakari Ailus
136e4cf8c58SSakari AilusPlease see examples in e.g. ``drivers/media/i2c/ov8856.c`` and
137b9a54336SSakari Ailus``drivers/media/i2c/ccs/ccs-core.c``. The two drivers work in both ACPI
138e4cf8c58SSakari Ailusand DT based systems.
139e4cf8c58SSakari Ailus
140e4cf8c58SSakari AilusControl framework
141e4cf8c58SSakari Ailus~~~~~~~~~~~~~~~~~
142e4cf8c58SSakari Ailus
143e4cf8c58SSakari Ailus``v4l2_ctrl_handler_setup()`` function may not be used in the device's runtime
144e4cf8c58SSakari AilusPM ``runtime_resume`` callback, as it has no way to figure out the power state
145e4cf8c58SSakari Ailusof the device. This is because the power state of the device is only changed
146e4cf8c58SSakari Ailusafter the power state transition has taken place. The ``s_ctrl`` callback can be
147e4cf8c58SSakari Ailusused to obtain device's power state after the power state transition:
148e4cf8c58SSakari Ailus
149f993b298SMauro Carvalho Chehab.. c:function:: int pm_runtime_get_if_in_use(struct device *dev);
150e4cf8c58SSakari Ailus
151e4cf8c58SSakari AilusThe function returns a non-zero value if it succeeded getting the power count or
152e4cf8c58SSakari Ailusruntime PM was disabled, in either of which cases the driver may proceed to
153e4cf8c58SSakari Ailusaccess the device.
154*71511a24SSakari Ailus
155*71511a24SSakari AilusRotation, orientation and flipping
156*71511a24SSakari Ailus----------------------------------
157*71511a24SSakari Ailus
158*71511a24SSakari AilusSome systems have the camera sensor mounted upside down compared to its natural
159*71511a24SSakari Ailusmounting rotation. In such cases, drivers shall expose the information to
160*71511a24SSakari Ailususerspace with the :ref:`V4L2_CID_CAMERA_SENSOR_ROTATION
161*71511a24SSakari Ailus<v4l2-camera-sensor-rotation>` control.
162*71511a24SSakari Ailus
163*71511a24SSakari AilusSensor drivers shall also report the sensor's mounting orientation with the
164*71511a24SSakari Ailus:ref:`V4L2_CID_CAMERA_SENSOR_ORIENTATION <v4l2-camera-sensor-orientation>`.
165*71511a24SSakari Ailus
166*71511a24SSakari AilusUse ``v4l2_fwnode_device_parse()`` to obtain rotation and orientation
167*71511a24SSakari Ailusinformation from system firmware and ``v4l2_ctrl_new_fwnode_properties()`` to
168*71511a24SSakari Ailusregister the appropriate controls.
169*71511a24SSakari Ailus
170*71511a24SSakari AilusSensor drivers that have any vertical or horizontal flips embedded in the
171*71511a24SSakari Ailusregister programming sequences shall initialize the V4L2_CID_HFLIP and
172*71511a24SSakari AilusV4L2_CID_VFLIP controls with the values programmed by the register sequences.
173*71511a24SSakari AilusThe default values of these controls shall be 0 (disabled). Especially these
174*71511a24SSakari Ailuscontrols shall not be inverted, independently of the sensor's mounting
175*71511a24SSakari Ailusrotation.
176