xref: /linux/Documentation/driver-api/media/camera-sensor.rst (revision 06d07429858317ded2db7986113a9e0129cd599b)
1e4cf8c58SSakari Ailus.. SPDX-License-Identifier: GPL-2.0
2e4cf8c58SSakari Ailus
3dc887661SSakari Ailus.. _media_writing_camera_sensor_drivers:
4dc887661SSakari Ailus
5e4cf8c58SSakari AilusWriting camera sensor drivers
6e4cf8c58SSakari Ailus=============================
7e4cf8c58SSakari Ailus
8dc887661SSakari AilusThis document covers the in-kernel APIs only. For the best practices on
9dc887661SSakari Ailususerspace API implementation in camera sensor drivers, please see
10dc887661SSakari Ailus:ref:`media_using_camera_sensor_drivers`.
11dc887661SSakari Ailus
12*cff18d8fSSakari AilusCSI-2, parallel and BT.656 buses
13*cff18d8fSSakari Ailus--------------------------------
14e4cf8c58SSakari Ailus
15b9a54336SSakari AilusPlease see :ref:`transmitter-receiver`.
16e4cf8c58SSakari Ailus
17e4cf8c58SSakari AilusHandling clocks
18e4cf8c58SSakari Ailus---------------
19e4cf8c58SSakari Ailus
20e4cf8c58SSakari AilusCamera sensors have an internal clock tree including a PLL and a number of
21e4cf8c58SSakari Ailusdivisors. The clock tree is generally configured by the driver based on a few
228c547f9bSLaurent Pinchartinput parameters that are specific to the hardware: the external clock frequency
23e4cf8c58SSakari Ailusand the link frequency. The two parameters generally are obtained from system
242225cf44SSakari Ailusfirmware. **No other frequencies should be used in any circumstances.**
25e4cf8c58SSakari Ailus
26e4cf8c58SSakari AilusThe reason why the clock frequencies are so important is that the clock signals
27e4cf8c58SSakari Ailuscome out of the SoC, and in many cases a specific frequency is designed to be
28e4cf8c58SSakari Ailusused in the system. Using another frequency may cause harmful effects
29e4cf8c58SSakari Ailuselsewhere. Therefore only the pre-determined frequencies are configurable by the
30e4cf8c58SSakari Ailususer.
31e4cf8c58SSakari Ailus
322225cf44SSakari AilusACPI
332225cf44SSakari Ailus~~~~
342225cf44SSakari Ailus
35b9a54336SSakari AilusRead the ``clock-frequency`` _DSD property to denote the frequency. The driver
36b9a54336SSakari Ailuscan rely on this frequency being used.
372225cf44SSakari Ailus
382225cf44SSakari AilusDevicetree
392225cf44SSakari Ailus~~~~~~~~~~
402225cf44SSakari Ailus
416d032832SLaurent PinchartThe preferred way to achieve this is using ``assigned-clocks``,
426d032832SLaurent Pinchart``assigned-clock-parents`` and ``assigned-clock-rates`` properties. See the
43dc887661SSakari Ailus`clock device tree bindings
44dc887661SSakari Ailus<https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/clock/clock.yaml>`_
456d032832SLaurent Pinchartfor more information. The driver then gets the frequency using
466d032832SLaurent Pinchart``clk_get_rate()``.
472225cf44SSakari Ailus
482225cf44SSakari AilusThis approach has the drawback that there's no guarantee that the frequency
492225cf44SSakari Ailushasn't been modified directly or indirectly by another driver, or supported by
502225cf44SSakari Ailusthe board's clock tree to begin with. Changes to the Common Clock Framework API
512225cf44SSakari Ailusare required to ensure reliability.
522225cf44SSakari Ailus
53f13734b5SLaurent PinchartPower management
54f13734b5SLaurent Pinchart----------------
55f13734b5SLaurent Pinchart
567610bfe7SLaurent PinchartCamera sensors are used in conjunction with other devices to form a camera
577610bfe7SLaurent Pinchartpipeline. They must obey the rules listed herein to ensure coherent power
587610bfe7SLaurent Pinchartmanagement over the pipeline.
59f13734b5SLaurent Pinchart
607610bfe7SLaurent PinchartCamera sensor drivers are responsible for controlling the power state of the
617610bfe7SLaurent Pinchartdevice they otherwise control as well. They shall use runtime PM to manage
627610bfe7SLaurent Pinchartpower states. Runtime PM shall be enabled at probe time and disabled at remove
636a6e49f8SSakari Ailustime. Drivers should enable runtime PM autosuspend. Also see
646a6e49f8SSakari Ailus:ref:`async sub-device registration <media-registering-async-subdevs>`.
65f13734b5SLaurent Pinchart
667610bfe7SLaurent PinchartThe runtime PM handlers shall handle clocks, regulators, GPIOs, and other
677610bfe7SLaurent Pinchartsystem resources required to power the sensor up and down. For drivers that
687610bfe7SLaurent Pinchartdon't use any of those resources (such as drivers that support ACPI systems
697610bfe7SLaurent Pinchartonly), the runtime PM handlers may be left unimplemented.
707610bfe7SLaurent Pinchart
717610bfe7SLaurent PinchartIn general, the device shall be powered on at least when its registers are
727610bfe7SLaurent Pinchartbeing accessed and when it is streaming. Drivers should use
737610bfe7SLaurent Pinchart``pm_runtime_resume_and_get()`` when starting streaming and
747610bfe7SLaurent Pinchart``pm_runtime_put()`` or ``pm_runtime_put_autosuspend()`` when stopping
757610bfe7SLaurent Pinchartstreaming. They may power the device up at probe time (for example to read
767610bfe7SLaurent Pinchartidentification registers), but should not keep it powered unconditionally after
777610bfe7SLaurent Pinchartprobe.
787610bfe7SLaurent Pinchart
797610bfe7SLaurent PinchartAt system suspend time, the whole camera pipeline must stop streaming, and
807610bfe7SLaurent Pinchartrestart when the system is resumed. This requires coordination between the
817610bfe7SLaurent Pinchartcamera sensor and the rest of the camera pipeline. Bridge drivers are
827610bfe7SLaurent Pinchartresponsible for this coordination, and instruct camera sensors to stop and
837610bfe7SLaurent Pinchartrestart streaming by calling the appropriate subdev operations
847610bfe7SLaurent Pinchart(``.s_stream()``, ``.enable_streams()`` or ``.disable_streams()``). Camera
857610bfe7SLaurent Pinchartsensor drivers shall therefore **not** keep track of the streaming state to
867610bfe7SLaurent Pinchartstop streaming in the PM suspend handler and restart it in the resume handler.
877610bfe7SLaurent PinchartDrivers should in general not implement the system PM handlers.
887610bfe7SLaurent Pinchart
897610bfe7SLaurent PinchartCamera sensor drivers shall **not** implement the subdev ``.s_power()``
907610bfe7SLaurent Pinchartoperation, as it is deprecated. While this operation is implemented in some
917610bfe7SLaurent Pinchartexisting drivers as they predate the deprecation, new drivers shall use runtime
927610bfe7SLaurent PinchartPM instead. If you feel you need to begin calling ``.s_power()`` from an ISP or
937610bfe7SLaurent Pincharta bridge driver, instead add runtime PM support to the sensor driver you are
947610bfe7SLaurent Pinchartusing and drop its ``.s_power()`` handler.
957610bfe7SLaurent Pinchart
96dc887661SSakari AilusPlease also see :ref:`examples <media-camera-sensor-examples>`.
97f13734b5SLaurent Pinchart
98f13734b5SLaurent PinchartControl framework
99f13734b5SLaurent Pinchart~~~~~~~~~~~~~~~~~
100f13734b5SLaurent Pinchart
101f13734b5SLaurent Pinchart``v4l2_ctrl_handler_setup()`` function may not be used in the device's runtime
102f13734b5SLaurent PinchartPM ``runtime_resume`` callback, as it has no way to figure out the power state
103f13734b5SLaurent Pinchartof the device. This is because the power state of the device is only changed
104f13734b5SLaurent Pinchartafter the power state transition has taken place. The ``s_ctrl`` callback can be
105f13734b5SLaurent Pinchartused to obtain device's power state after the power state transition:
106f13734b5SLaurent Pinchart
107f13734b5SLaurent Pinchart.. c:function:: int pm_runtime_get_if_in_use(struct device *dev);
108f13734b5SLaurent Pinchart
109f13734b5SLaurent PinchartThe function returns a non-zero value if it succeeded getting the power count or
110f13734b5SLaurent Pinchartruntime PM was disabled, in either of which cases the driver may proceed to
111f13734b5SLaurent Pinchartaccess the device.
112f13734b5SLaurent Pinchart
11371511a24SSakari AilusRotation, orientation and flipping
11471511a24SSakari Ailus----------------------------------
11571511a24SSakari Ailus
11671511a24SSakari AilusUse ``v4l2_fwnode_device_parse()`` to obtain rotation and orientation
11771511a24SSakari Ailusinformation from system firmware and ``v4l2_ctrl_new_fwnode_properties()`` to
11871511a24SSakari Ailusregister the appropriate controls.
11971511a24SSakari Ailus
120dc887661SSakari Ailus.. _media-camera-sensor-examples:
121dc887661SSakari Ailus
122dc887661SSakari AilusExample drivers
123dc887661SSakari Ailus---------------
124dc887661SSakari Ailus
125dc887661SSakari AilusFeatures implemented by sensor drivers vary, and depending on the set of
126dc887661SSakari Ailussupported features and other qualities, particular sensor drivers better serve
127dc887661SSakari Ailusthe purpose of an example. The following drivers are known to be good examples:
128dc887661SSakari Ailus
129dc887661SSakari Ailus.. flat-table:: Example sensor drivers
130dc887661SSakari Ailus    :header-rows: 0
131dc887661SSakari Ailus    :widths:      1 1 1 2
132dc887661SSakari Ailus
133dc887661SSakari Ailus    * - Driver name
134dc887661SSakari Ailus      - File(s)
135dc887661SSakari Ailus      - Driver type
136dc887661SSakari Ailus      - Example topic
137dc887661SSakari Ailus    * - CCS
138dc887661SSakari Ailus      - ``drivers/media/i2c/ccs/``
139dc887661SSakari Ailus      - Freely configurable
140dc887661SSakari Ailus      - Power management (ACPI and DT), UAPI
141dc887661SSakari Ailus    * - imx219
142dc887661SSakari Ailus      - ``drivers/media/i2c/imx219.c``
143dc887661SSakari Ailus      - Register list based
144dc887661SSakari Ailus      - Power management (DT), UAPI, mode selection
145dc887661SSakari Ailus    * - imx319
146dc887661SSakari Ailus      - ``drivers/media/i2c/imx319.c``
147dc887661SSakari Ailus      - Register list based
148dc887661SSakari Ailus      - Power management (ACPI and DT)
149