camera-sensor.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _media_writing_camera_sensor_drivers:
  3. Writing camera sensor drivers
  4. =============================
  5. This document covers the in-kernel APIs only. For the best practices on
  6. userspace API implementation in camera sensor drivers, please see
  7. :ref:`media_using_camera_sensor_drivers`.
  8. CSI-2, parallel and BT.656 buses
  9. --------------------------------
  10. Please see :ref:`transmitter-receiver`.
  11. Handling clocks
  12. ---------------
  13. Camera sensors have an internal clock tree including a PLL and a number of
  14. divisors. The clock tree is generally configured by the driver based on a few
  15. input parameters that are specific to the hardware: the external clock frequency
  16. and the link frequency. The two parameters generally are obtained from system
  17. firmware. **No other frequencies should be used in any circumstances.**
  18. The reason why the clock frequencies are so important is that the clock signals
  19. come out of the SoC, and in many cases a specific frequency is designed to be
  20. used in the system. Using another frequency may cause harmful effects
  21. elsewhere. Therefore only the pre-determined frequencies are configurable by the
  22. user.
  23. ACPI
  24. ~~~~
  25. Read the ``clock-frequency`` _DSD property to denote the frequency. The driver
  26. can rely on this frequency being used.
  27. Devicetree
  28. ~~~~~~~~~~
  29. The preferred way to achieve this is using ``assigned-clocks``,
  30. ``assigned-clock-parents`` and ``assigned-clock-rates`` properties. See the
  31. `clock device tree bindings
  32. <https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/clock/clock.yaml>`_
  33. for more information. The driver then gets the frequency using
  34. ``clk_get_rate()``.
  35. This approach has the drawback that there's no guarantee that the frequency
  36. hasn't been modified directly or indirectly by another driver, or supported by
  37. the board's clock tree to begin with. Changes to the Common Clock Framework API
  38. are required to ensure reliability.
  39. Power management
  40. ----------------
  41. Camera sensors are used in conjunction with other devices to form a camera
  42. pipeline. They must obey the rules listed herein to ensure coherent power
  43. management over the pipeline.
  44. Camera sensor drivers are responsible for controlling the power state of the
  45. device they otherwise control as well. They shall use runtime PM to manage
  46. power states. Runtime PM shall be enabled at probe time and disabled at remove
  47. time. Drivers should enable runtime PM autosuspend. Also see
  48. :ref:`async sub-device registration <media-registering-async-subdevs>`.
  49. The runtime PM handlers shall handle clocks, regulators, GPIOs, and other
  50. system resources required to power the sensor up and down. For drivers that
  51. don't use any of those resources (such as drivers that support ACPI systems
  52. only), the runtime PM handlers may be left unimplemented.
  53. In general, the device shall be powered on at least when its registers are
  54. being accessed and when it is streaming. Drivers should use
  55. ``pm_runtime_resume_and_get()`` when starting streaming and
  56. ``pm_runtime_put()`` or ``pm_runtime_put_autosuspend()`` when stopping
  57. streaming. They may power the device up at probe time (for example to read
  58. identification registers), but should not keep it powered unconditionally after
  59. probe.
  60. At system suspend time, the whole camera pipeline must stop streaming, and
  61. restart when the system is resumed. This requires coordination between the
  62. camera sensor and the rest of the camera pipeline. Bridge drivers are
  63. responsible for this coordination, and instruct camera sensors to stop and
  64. restart streaming by calling the appropriate subdev operations
  65. (``.s_stream()``, ``.enable_streams()`` or ``.disable_streams()``). Camera
  66. sensor drivers shall therefore **not** keep track of the streaming state to
  67. stop streaming in the PM suspend handler and restart it in the resume handler.
  68. Drivers should in general not implement the system PM handlers.
  69. Camera sensor drivers shall **not** implement the subdev ``.s_power()``
  70. operation, as it is deprecated. While this operation is implemented in some
  71. existing drivers as they predate the deprecation, new drivers shall use runtime
  72. PM instead. If you feel you need to begin calling ``.s_power()`` from an ISP or
  73. a bridge driver, instead add runtime PM support to the sensor driver you are
  74. using and drop its ``.s_power()`` handler.
  75. Please also see :ref:`examples <media-camera-sensor-examples>`.
  76. Control framework
  77. ~~~~~~~~~~~~~~~~~
  78. ``v4l2_ctrl_handler_setup()`` function may not be used in the device's runtime
  79. PM ``runtime_resume`` callback, as it has no way to figure out the power state
  80. of the device. This is because the power state of the device is only changed
  81. after the power state transition has taken place. The ``s_ctrl`` callback can be
  82. used to obtain device's power state after the power state transition:
  83. .. c:function:: int pm_runtime_get_if_in_use(struct device *dev);
  84. The function returns a non-zero value if it succeeded getting the power count or
  85. runtime PM was disabled, in either of which cases the driver may proceed to
  86. access the device.
  87. Rotation, orientation and flipping
  88. ----------------------------------
  89. Use ``v4l2_fwnode_device_parse()`` to obtain rotation and orientation
  90. information from system firmware and ``v4l2_ctrl_new_fwnode_properties()`` to
  91. register the appropriate controls.
  92. .. _media-camera-sensor-examples:
  93. Example drivers
  94. ---------------
  95. Features implemented by sensor drivers vary, and depending on the set of
  96. supported features and other qualities, particular sensor drivers better serve
  97. the purpose of an example. The following drivers are known to be good examples:
  98. .. flat-table:: Example sensor drivers
  99. :header-rows: 0
  100. :widths: 1 1 1 2
  101. * - Driver name
  102. - File(s)
  103. - Driver type
  104. - Example topic
  105. * - CCS
  106. - ``drivers/media/i2c/ccs/``
  107. - Freely configurable
  108. - Power management (ACPI and DT), UAPI
  109. * - imx219
  110. - ``drivers/media/i2c/imx219.c``
  111. - Register list based
  112. - Power management (DT), UAPI, mode selection
  113. * - imx319
  114. - ``drivers/media/i2c/imx319.c``
  115. - Register list based
  116. - Power management (ACPI and DT)