18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ciV4L2 sub-devices 48c2ecf20Sopenharmony_ci---------------- 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ciMany drivers need to communicate with sub-devices. These devices can do all 78c2ecf20Sopenharmony_cisort of tasks, but most commonly they handle audio and/or video muxing, 88c2ecf20Sopenharmony_ciencoding or decoding. For webcams common sub-devices are sensors and camera 98c2ecf20Sopenharmony_cicontrollers. 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ciUsually these are I2C devices, but not necessarily. In order to provide the 128c2ecf20Sopenharmony_cidriver with a consistent interface to these sub-devices the 138c2ecf20Sopenharmony_ci:c:type:`v4l2_subdev` struct (v4l2-subdev.h) was created. 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ciEach sub-device driver must have a :c:type:`v4l2_subdev` struct. This struct 168c2ecf20Sopenharmony_cican be stand-alone for simple sub-devices or it might be embedded in a larger 178c2ecf20Sopenharmony_cistruct if more state information needs to be stored. Usually there is a 188c2ecf20Sopenharmony_cilow-level device struct (e.g. ``i2c_client``) that contains the device data as 198c2ecf20Sopenharmony_cisetup by the kernel. It is recommended to store that pointer in the private 208c2ecf20Sopenharmony_cidata of :c:type:`v4l2_subdev` using :c:func:`v4l2_set_subdevdata`. That makes 218c2ecf20Sopenharmony_ciit easy to go from a :c:type:`v4l2_subdev` to the actual low-level bus-specific 228c2ecf20Sopenharmony_cidevice data. 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ciYou also need a way to go from the low-level struct to :c:type:`v4l2_subdev`. 258c2ecf20Sopenharmony_ciFor the common i2c_client struct the i2c_set_clientdata() call is used to store 268c2ecf20Sopenharmony_cia :c:type:`v4l2_subdev` pointer, for other buses you may have to use other 278c2ecf20Sopenharmony_cimethods. 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciBridges might also need to store per-subdev private data, such as a pointer to 308c2ecf20Sopenharmony_cibridge-specific per-subdev private data. The :c:type:`v4l2_subdev` structure 318c2ecf20Sopenharmony_ciprovides host private data for that purpose that can be accessed with 328c2ecf20Sopenharmony_ci:c:func:`v4l2_get_subdev_hostdata` and :c:func:`v4l2_set_subdev_hostdata`. 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciFrom the bridge driver perspective, you load the sub-device module and somehow 358c2ecf20Sopenharmony_ciobtain the :c:type:`v4l2_subdev` pointer. For i2c devices this is easy: you call 368c2ecf20Sopenharmony_ci``i2c_get_clientdata()``. For other buses something similar needs to be done. 378c2ecf20Sopenharmony_ciHelper functions exist for sub-devices on an I2C bus that do most of this 388c2ecf20Sopenharmony_citricky work for you. 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ciEach :c:type:`v4l2_subdev` contains function pointers that sub-device drivers 418c2ecf20Sopenharmony_cican implement (or leave ``NULL`` if it is not applicable). Since sub-devices can 428c2ecf20Sopenharmony_cido so many different things and you do not want to end up with a huge ops struct 438c2ecf20Sopenharmony_ciof which only a handful of ops are commonly implemented, the function pointers 448c2ecf20Sopenharmony_ciare sorted according to category and each category has its own ops struct. 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ciThe top-level ops struct contains pointers to the category ops structs, which 478c2ecf20Sopenharmony_cimay be NULL if the subdev driver does not support anything from that category. 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ciIt looks like this: 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci.. code-block:: c 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci struct v4l2_subdev_core_ops { 548c2ecf20Sopenharmony_ci int (*log_status)(struct v4l2_subdev *sd); 558c2ecf20Sopenharmony_ci int (*init)(struct v4l2_subdev *sd, u32 val); 568c2ecf20Sopenharmony_ci ... 578c2ecf20Sopenharmony_ci }; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci struct v4l2_subdev_tuner_ops { 608c2ecf20Sopenharmony_ci ... 618c2ecf20Sopenharmony_ci }; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci struct v4l2_subdev_audio_ops { 648c2ecf20Sopenharmony_ci ... 658c2ecf20Sopenharmony_ci }; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci struct v4l2_subdev_video_ops { 688c2ecf20Sopenharmony_ci ... 698c2ecf20Sopenharmony_ci }; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_ops { 728c2ecf20Sopenharmony_ci ... 738c2ecf20Sopenharmony_ci }; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci struct v4l2_subdev_ops { 768c2ecf20Sopenharmony_ci const struct v4l2_subdev_core_ops *core; 778c2ecf20Sopenharmony_ci const struct v4l2_subdev_tuner_ops *tuner; 788c2ecf20Sopenharmony_ci const struct v4l2_subdev_audio_ops *audio; 798c2ecf20Sopenharmony_ci const struct v4l2_subdev_video_ops *video; 808c2ecf20Sopenharmony_ci const struct v4l2_subdev_pad_ops *video; 818c2ecf20Sopenharmony_ci }; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciThe core ops are common to all subdevs, the other categories are implemented 848c2ecf20Sopenharmony_cidepending on the sub-device. E.g. a video device is unlikely to support the 858c2ecf20Sopenharmony_ciaudio ops and vice versa. 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciThis setup limits the number of function pointers while still making it easy 888c2ecf20Sopenharmony_cito add new ops and categories. 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciA sub-device driver initializes the :c:type:`v4l2_subdev` struct using: 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci :c:func:`v4l2_subdev_init <v4l2_subdev_init>` 938c2ecf20Sopenharmony_ci (:c:type:`sd <v4l2_subdev>`, &\ :c:type:`ops <v4l2_subdev_ops>`). 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciAfterwards you need to initialize :c:type:`sd <v4l2_subdev>`->name with a 978c2ecf20Sopenharmony_ciunique name and set the module owner. This is done for you if you use the 988c2ecf20Sopenharmony_cii2c helper functions. 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ciIf integration with the media framework is needed, you must initialize the 1018c2ecf20Sopenharmony_ci:c:type:`media_entity` struct embedded in the :c:type:`v4l2_subdev` struct 1028c2ecf20Sopenharmony_ci(entity field) by calling :c:func:`media_entity_pads_init`, if the entity has 1038c2ecf20Sopenharmony_cipads: 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci.. code-block:: c 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci struct media_pad *pads = &my_sd->pads; 1088c2ecf20Sopenharmony_ci int err; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci err = media_entity_pads_init(&sd->entity, npads, pads); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ciThe pads array must have been previously initialized. There is no need to 1138c2ecf20Sopenharmony_cimanually set the struct media_entity function and name fields, but the 1148c2ecf20Sopenharmony_cirevision field must be initialized if needed. 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ciA reference to the entity will be automatically acquired/released when the 1178c2ecf20Sopenharmony_cisubdev device node (if any) is opened/closed. 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciDon't forget to cleanup the media entity before the sub-device is destroyed: 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci.. code-block:: c 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci media_entity_cleanup(&sd->entity); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ciIf the subdev driver intends to process video and integrate with the media 1268c2ecf20Sopenharmony_ciframework, it must implement format related functionality using 1278c2ecf20Sopenharmony_ci:c:type:`v4l2_subdev_pad_ops` instead of :c:type:`v4l2_subdev_video_ops`. 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ciIn that case, the subdev driver may set the link_validate field to provide 1308c2ecf20Sopenharmony_ciits own link validation function. The link validation function is called for 1318c2ecf20Sopenharmony_cievery link in the pipeline where both of the ends of the links are V4L2 1328c2ecf20Sopenharmony_cisub-devices. The driver is still responsible for validating the correctness 1338c2ecf20Sopenharmony_ciof the format configuration between sub-devices and video nodes. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ciIf link_validate op is not set, the default function 1368c2ecf20Sopenharmony_ci:c:func:`v4l2_subdev_link_validate_default` is used instead. This function 1378c2ecf20Sopenharmony_ciensures that width, height and the media bus pixel code are equal on both source 1388c2ecf20Sopenharmony_ciand sink of the link. Subdev drivers are also free to use this function to 1398c2ecf20Sopenharmony_ciperform the checks mentioned above in addition to their own checks. 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ciSubdev registration 1428c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ciThere are currently two ways to register subdevices with the V4L2 core. The 1458c2ecf20Sopenharmony_cifirst (traditional) possibility is to have subdevices registered by bridge 1468c2ecf20Sopenharmony_cidrivers. This can be done when the bridge driver has the complete information 1478c2ecf20Sopenharmony_ciabout subdevices connected to it and knows exactly when to register them. This 1488c2ecf20Sopenharmony_ciis typically the case for internal subdevices, like video data processing units 1498c2ecf20Sopenharmony_ciwithin SoCs or complex PCI(e) boards, camera sensors in USB cameras or connected 1508c2ecf20Sopenharmony_cito SoCs, which pass information about them to bridge drivers, usually in their 1518c2ecf20Sopenharmony_ciplatform data. 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ciThere are however also situations where subdevices have to be registered 1548c2ecf20Sopenharmony_ciasynchronously to bridge devices. An example of such a configuration is a Device 1558c2ecf20Sopenharmony_ciTree based system where information about subdevices is made available to the 1568c2ecf20Sopenharmony_cisystem independently from the bridge devices, e.g. when subdevices are defined 1578c2ecf20Sopenharmony_ciin DT as I2C device nodes. The API used in this second case is described further 1588c2ecf20Sopenharmony_cibelow. 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ciUsing one or the other registration method only affects the probing process, the 1618c2ecf20Sopenharmony_cirun-time bridge-subdevice interaction is in both cases the same. 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ciIn the **synchronous** case a device (bridge) driver needs to register the 1648c2ecf20Sopenharmony_ci:c:type:`v4l2_subdev` with the v4l2_device: 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci :c:func:`v4l2_device_register_subdev <v4l2_device_register_subdev>` 1678c2ecf20Sopenharmony_ci (:c:type:`v4l2_dev <v4l2_device>`, :c:type:`sd <v4l2_subdev>`). 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ciThis can fail if the subdev module disappeared before it could be registered. 1708c2ecf20Sopenharmony_ciAfter this function was called successfully the subdev->dev field points to 1718c2ecf20Sopenharmony_cithe :c:type:`v4l2_device`. 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ciIf the v4l2_device parent device has a non-NULL mdev field, the sub-device 1748c2ecf20Sopenharmony_cientity will be automatically registered with the media device. 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ciYou can unregister a sub-device using: 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci :c:func:`v4l2_device_unregister_subdev <v4l2_device_unregister_subdev>` 1798c2ecf20Sopenharmony_ci (:c:type:`sd <v4l2_subdev>`). 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ciAfterwards the subdev module can be unloaded and 1838c2ecf20Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->dev == ``NULL``. 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ciIn the **asynchronous** case subdevice probing can be invoked independently of 1868c2ecf20Sopenharmony_cithe bridge driver availability. The subdevice driver then has to verify whether 1878c2ecf20Sopenharmony_ciall the requirements for a successful probing are satisfied. This can include a 1888c2ecf20Sopenharmony_cicheck for a master clock availability. If any of the conditions aren't satisfied 1898c2ecf20Sopenharmony_cithe driver might decide to return ``-EPROBE_DEFER`` to request further reprobing 1908c2ecf20Sopenharmony_ciattempts. Once all conditions are met the subdevice shall be registered using 1918c2ecf20Sopenharmony_cithe :c:func:`v4l2_async_register_subdev` function. Unregistration is 1928c2ecf20Sopenharmony_ciperformed using the :c:func:`v4l2_async_unregister_subdev` call. Subdevices 1938c2ecf20Sopenharmony_ciregistered this way are stored in a global list of subdevices, ready to be 1948c2ecf20Sopenharmony_cipicked up by bridge drivers. 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ciBridge drivers in turn have to register a notifier object. This is 1978c2ecf20Sopenharmony_ciperformed using the :c:func:`v4l2_async_notifier_register` call. To 1988c2ecf20Sopenharmony_ciunregister the notifier the driver has to call 1998c2ecf20Sopenharmony_ci:c:func:`v4l2_async_notifier_unregister`. The former of the two functions 2008c2ecf20Sopenharmony_citakes two arguments: a pointer to struct :c:type:`v4l2_device` and a 2018c2ecf20Sopenharmony_cipointer to struct :c:type:`v4l2_async_notifier`. 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ciBefore registering the notifier, bridge drivers must do two things: 2048c2ecf20Sopenharmony_cifirst, the notifier must be initialized using the 2058c2ecf20Sopenharmony_ci:c:func:`v4l2_async_notifier_init`. Second, bridge drivers can then 2068c2ecf20Sopenharmony_cibegin to form a list of subdevice descriptors that the bridge device 2078c2ecf20Sopenharmony_cineeds for its operation. Subdevice descriptors are added to the notifier 2088c2ecf20Sopenharmony_ciusing the :c:func:`v4l2_async_notifier_add_subdev` call. This function 2098c2ecf20Sopenharmony_citakes two arguments: a pointer to struct :c:type:`v4l2_async_notifier`, 2108c2ecf20Sopenharmony_ciand a pointer to the subdevice descripter, which is of type struct 2118c2ecf20Sopenharmony_ci:c:type:`v4l2_async_subdev`. 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciThe V4L2 core will then use these descriptors to match asynchronously 2148c2ecf20Sopenharmony_ciregistered subdevices to them. If a match is detected the ``.bound()`` 2158c2ecf20Sopenharmony_cinotifier callback is called. After all subdevices have been located the 2168c2ecf20Sopenharmony_ci.complete() callback is called. When a subdevice is removed from the 2178c2ecf20Sopenharmony_cisystem the .unbind() method is called. All three callbacks are optional. 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciCalling subdev operations 2208c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~ 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ciThe advantage of using :c:type:`v4l2_subdev` is that it is a generic struct and 2238c2ecf20Sopenharmony_cidoes not contain any knowledge about the underlying hardware. So a driver might 2248c2ecf20Sopenharmony_cicontain several subdevs that use an I2C bus, but also a subdev that is 2258c2ecf20Sopenharmony_cicontrolled through GPIO pins. This distinction is only relevant when setting 2268c2ecf20Sopenharmony_ciup the device, but once the subdev is registered it is completely transparent. 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ciOnce te subdev has been registered you can call an ops function either 2298c2ecf20Sopenharmony_cidirectly: 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci.. code-block:: c 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci err = sd->ops->core->g_std(sd, &norm); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_cibut it is better and easier to use this macro: 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci.. code-block:: c 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci err = v4l2_subdev_call(sd, core, g_std, &norm); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ciThe macro will do the right ``NULL`` pointer checks and returns ``-ENODEV`` 2428c2ecf20Sopenharmony_ciif :c:type:`sd <v4l2_subdev>` is ``NULL``, ``-ENOIOCTLCMD`` if either 2438c2ecf20Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->core or :c:type:`sd <v4l2_subdev>`->core->g_std is ``NULL``, or the actual result of the 2448c2ecf20Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->ops->core->g_std ops. 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ciIt is also possible to call all or a subset of the sub-devices: 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci.. code-block:: c 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci v4l2_device_call_all(v4l2_dev, 0, core, g_std, &norm); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ciAny subdev that does not support this ops is skipped and error results are 2538c2ecf20Sopenharmony_ciignored. If you want to check for errors use this: 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci.. code-block:: c 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_std, &norm); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ciAny error except ``-ENOIOCTLCMD`` will exit the loop with that error. If no 2608c2ecf20Sopenharmony_cierrors (except ``-ENOIOCTLCMD``) occurred, then 0 is returned. 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ciThe second argument to both calls is a group ID. If 0, then all subdevs are 2638c2ecf20Sopenharmony_cicalled. If non-zero, then only those whose group ID match that value will 2648c2ecf20Sopenharmony_cibe called. Before a bridge driver registers a subdev it can set 2658c2ecf20Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->grp_id to whatever value it wants (it's 0 by 2668c2ecf20Sopenharmony_cidefault). This value is owned by the bridge driver and the sub-device driver 2678c2ecf20Sopenharmony_ciwill never modify or use it. 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ciThe group ID gives the bridge driver more control how callbacks are called. 2708c2ecf20Sopenharmony_ciFor example, there may be multiple audio chips on a board, each capable of 2718c2ecf20Sopenharmony_cichanging the volume. But usually only one will actually be used when the 2728c2ecf20Sopenharmony_ciuser want to change the volume. You can set the group ID for that subdev to 2738c2ecf20Sopenharmony_cie.g. AUDIO_CONTROLLER and specify that as the group ID value when calling 2748c2ecf20Sopenharmony_ci``v4l2_device_call_all()``. That ensures that it will only go to the subdev 2758c2ecf20Sopenharmony_cithat needs it. 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ciIf the sub-device needs to notify its v4l2_device parent of an event, then 2788c2ecf20Sopenharmony_ciit can call ``v4l2_subdev_notify(sd, notification, arg)``. This macro checks 2798c2ecf20Sopenharmony_ciwhether there is a ``notify()`` callback defined and returns ``-ENODEV`` if not. 2808c2ecf20Sopenharmony_ciOtherwise the result of the ``notify()`` call is returned. 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciV4L2 sub-device userspace API 2838c2ecf20Sopenharmony_ci----------------------------- 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ciBridge drivers traditionally expose one or multiple video nodes to userspace, 2868c2ecf20Sopenharmony_ciand control subdevices through the :c:type:`v4l2_subdev_ops` operations in 2878c2ecf20Sopenharmony_ciresponse to video node operations. This hides the complexity of the underlying 2888c2ecf20Sopenharmony_cihardware from applications. For complex devices, finer-grained control of the 2898c2ecf20Sopenharmony_cidevice than what the video nodes offer may be required. In those cases, bridge 2908c2ecf20Sopenharmony_cidrivers that implement :ref:`the media controller API <media_controller>` may 2918c2ecf20Sopenharmony_ciopt for making the subdevice operations directly accessible from userpace. 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ciDevice nodes named ``v4l-subdev``\ *X* can be created in ``/dev`` to access 2948c2ecf20Sopenharmony_cisub-devices directly. If a sub-device supports direct userspace configuration 2958c2ecf20Sopenharmony_ciit must set the ``V4L2_SUBDEV_FL_HAS_DEVNODE`` flag before being registered. 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ciAfter registering sub-devices, the :c:type:`v4l2_device` driver can create 2988c2ecf20Sopenharmony_cidevice nodes for all registered sub-devices marked with 2998c2ecf20Sopenharmony_ci``V4L2_SUBDEV_FL_HAS_DEVNODE`` by calling 3008c2ecf20Sopenharmony_ci:c:func:`v4l2_device_register_subdev_nodes`. Those device nodes will be 3018c2ecf20Sopenharmony_ciautomatically removed when sub-devices are unregistered. 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ciThe device node handles a subset of the V4L2 API. 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci``VIDIOC_QUERYCTRL``, 3068c2ecf20Sopenharmony_ci``VIDIOC_QUERYMENU``, 3078c2ecf20Sopenharmony_ci``VIDIOC_G_CTRL``, 3088c2ecf20Sopenharmony_ci``VIDIOC_S_CTRL``, 3098c2ecf20Sopenharmony_ci``VIDIOC_G_EXT_CTRLS``, 3108c2ecf20Sopenharmony_ci``VIDIOC_S_EXT_CTRLS`` and 3118c2ecf20Sopenharmony_ci``VIDIOC_TRY_EXT_CTRLS``: 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci The controls ioctls are identical to the ones defined in V4L2. They 3148c2ecf20Sopenharmony_ci behave identically, with the only exception that they deal only with 3158c2ecf20Sopenharmony_ci controls implemented in the sub-device. Depending on the driver, those 3168c2ecf20Sopenharmony_ci controls can be also be accessed through one (or several) V4L2 device 3178c2ecf20Sopenharmony_ci nodes. 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci``VIDIOC_DQEVENT``, 3208c2ecf20Sopenharmony_ci``VIDIOC_SUBSCRIBE_EVENT`` and 3218c2ecf20Sopenharmony_ci``VIDIOC_UNSUBSCRIBE_EVENT`` 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci The events ioctls are identical to the ones defined in V4L2. They 3248c2ecf20Sopenharmony_ci behave identically, with the only exception that they deal only with 3258c2ecf20Sopenharmony_ci events generated by the sub-device. Depending on the driver, those 3268c2ecf20Sopenharmony_ci events can also be reported by one (or several) V4L2 device nodes. 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci Sub-device drivers that want to use events need to set the 3298c2ecf20Sopenharmony_ci ``V4L2_SUBDEV_FL_HAS_EVENTS`` :c:type:`v4l2_subdev`.flags before registering 3308c2ecf20Sopenharmony_ci the sub-device. After registration events can be queued as usual on the 3318c2ecf20Sopenharmony_ci :c:type:`v4l2_subdev`.devnode device node. 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci To properly support events, the ``poll()`` file operation is also 3348c2ecf20Sopenharmony_ci implemented. 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ciPrivate ioctls 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci All ioctls not in the above list are passed directly to the sub-device 3398c2ecf20Sopenharmony_ci driver through the core::ioctl operation. 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ciRead-only sub-device userspace API 3428c2ecf20Sopenharmony_ci---------------------------------- 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ciBridge drivers that control their connected subdevices through direct calls to 3458c2ecf20Sopenharmony_cithe kernel API realized by :c:type:`v4l2_subdev_ops` structure do not usually 3468c2ecf20Sopenharmony_ciwant userspace to be able to change the same parameters through the subdevice 3478c2ecf20Sopenharmony_cidevice node and thus do not usually register any. 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ciIt is sometimes useful to report to userspace the current subdevice 3508c2ecf20Sopenharmony_ciconfiguration through a read-only API, that does not permit applications to 3518c2ecf20Sopenharmony_cichange to the device parameters but allows interfacing to the subdevice device 3528c2ecf20Sopenharmony_cinode to inspect them. 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ciFor instance, to implement cameras based on computational photography, userspace 3558c2ecf20Sopenharmony_cineeds to know the detailed camera sensor configuration (in terms of skipping, 3568c2ecf20Sopenharmony_cibinning, cropping and scaling) for each supported output resolution. To support 3578c2ecf20Sopenharmony_cisuch use cases, bridge drivers may expose the subdevice operations to userspace 3588c2ecf20Sopenharmony_cithrough a read-only API. 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ciTo create a read-only device node for all the subdevices registered with the 3618c2ecf20Sopenharmony_ci``V4L2_SUBDEV_FL_HAS_DEVNODE`` set, the :c:type:`v4l2_device` driver should call 3628c2ecf20Sopenharmony_ci:c:func:`v4l2_device_register_ro_subdev_nodes`. 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ciAccess to the following ioctls for userspace applications is restricted on 3658c2ecf20Sopenharmony_cisub-device device nodes registered with 3668c2ecf20Sopenharmony_ci:c:func:`v4l2_device_register_ro_subdev_nodes`. 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci``VIDIOC_SUBDEV_S_FMT``, 3698c2ecf20Sopenharmony_ci``VIDIOC_SUBDEV_S_CROP``, 3708c2ecf20Sopenharmony_ci``VIDIOC_SUBDEV_S_SELECTION``: 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci These ioctls are only allowed on a read-only subdevice device node 3738c2ecf20Sopenharmony_ci for the :ref:`V4L2_SUBDEV_FORMAT_TRY <v4l2-subdev-format-whence>` 3748c2ecf20Sopenharmony_ci formats and selection rectangles. 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci``VIDIOC_SUBDEV_S_FRAME_INTERVAL``, 3778c2ecf20Sopenharmony_ci``VIDIOC_SUBDEV_S_DV_TIMINGS``, 3788c2ecf20Sopenharmony_ci``VIDIOC_SUBDEV_S_STD``: 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci These ioctls are not allowed on a read-only subdevice node. 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ciIn case the ioctl is not allowed, or the format to modify is set to 3838c2ecf20Sopenharmony_ci``V4L2_SUBDEV_FORMAT_ACTIVE``, the core returns a negative error code and 3848c2ecf20Sopenharmony_cithe errno variable is set to ``-EPERM``. 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ciI2C sub-device drivers 3878c2ecf20Sopenharmony_ci---------------------- 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ciSince these drivers are so common, special helper functions are available to 3908c2ecf20Sopenharmony_ciease the use of these drivers (``v4l2-common.h``). 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ciThe recommended method of adding :c:type:`v4l2_subdev` support to an I2C driver 3938c2ecf20Sopenharmony_ciis to embed the :c:type:`v4l2_subdev` struct into the state struct that is 3948c2ecf20Sopenharmony_cicreated for each I2C device instance. Very simple devices have no state 3958c2ecf20Sopenharmony_cistruct and in that case you can just create a :c:type:`v4l2_subdev` directly. 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ciA typical state struct would look like this (where 'chipname' is replaced by 3988c2ecf20Sopenharmony_cithe name of the chip): 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci.. code-block:: c 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci struct chipname_state { 4038c2ecf20Sopenharmony_ci struct v4l2_subdev sd; 4048c2ecf20Sopenharmony_ci ... /* additional state fields */ 4058c2ecf20Sopenharmony_ci }; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ciInitialize the :c:type:`v4l2_subdev` struct as follows: 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci.. code-block:: c 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci v4l2_i2c_subdev_init(&state->sd, client, subdev_ops); 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ciThis function will fill in all the fields of :c:type:`v4l2_subdev` ensure that 4148c2ecf20Sopenharmony_cithe :c:type:`v4l2_subdev` and i2c_client both point to one another. 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ciYou should also add a helper inline function to go from a :c:type:`v4l2_subdev` 4178c2ecf20Sopenharmony_cipointer to a chipname_state struct: 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci.. code-block:: c 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci static inline struct chipname_state *to_state(struct v4l2_subdev *sd) 4228c2ecf20Sopenharmony_ci { 4238c2ecf20Sopenharmony_ci return container_of(sd, struct chipname_state, sd); 4248c2ecf20Sopenharmony_ci } 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ciUse this to go from the :c:type:`v4l2_subdev` struct to the ``i2c_client`` 4278c2ecf20Sopenharmony_cistruct: 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci.. code-block:: c 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(sd); 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ciAnd this to go from an ``i2c_client`` to a :c:type:`v4l2_subdev` struct: 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci.. code-block:: c 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci struct v4l2_subdev *sd = i2c_get_clientdata(client); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ciMake sure to call 4408c2ecf20Sopenharmony_ci:c:func:`v4l2_device_unregister_subdev`\ (:c:type:`sd <v4l2_subdev>`) 4418c2ecf20Sopenharmony_ciwhen the ``remove()`` callback is called. This will unregister the sub-device 4428c2ecf20Sopenharmony_cifrom the bridge driver. It is safe to call this even if the sub-device was 4438c2ecf20Sopenharmony_cinever registered. 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ciYou need to do this because when the bridge driver destroys the i2c adapter 4468c2ecf20Sopenharmony_cithe ``remove()`` callbacks are called of the i2c devices on that adapter. 4478c2ecf20Sopenharmony_ciAfter that the corresponding v4l2_subdev structures are invalid, so they 4488c2ecf20Sopenharmony_cihave to be unregistered first. Calling 4498c2ecf20Sopenharmony_ci:c:func:`v4l2_device_unregister_subdev`\ (:c:type:`sd <v4l2_subdev>`) 4508c2ecf20Sopenharmony_cifrom the ``remove()`` callback ensures that this is always done correctly. 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ciThe bridge driver also has some helper functions it can use: 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci.. code-block:: c 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci struct v4l2_subdev *sd = v4l2_i2c_new_subdev(v4l2_dev, adapter, 4588c2ecf20Sopenharmony_ci "module_foo", "chipid", 0x36, NULL); 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ciThis loads the given module (can be ``NULL`` if no module needs to be loaded) 4618c2ecf20Sopenharmony_ciand calls :c:func:`i2c_new_client_device` with the given ``i2c_adapter`` and 4628c2ecf20Sopenharmony_cichip/address arguments. If all goes well, then it registers the subdev with 4638c2ecf20Sopenharmony_cithe v4l2_device. 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ciYou can also use the last argument of :c:func:`v4l2_i2c_new_subdev` to pass 4668c2ecf20Sopenharmony_cian array of possible I2C addresses that it should probe. These probe addresses 4678c2ecf20Sopenharmony_ciare only used if the previous argument is 0. A non-zero argument means that you 4688c2ecf20Sopenharmony_ciknow the exact i2c address so in that case no probing will take place. 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ciBoth functions return ``NULL`` if something went wrong. 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ciNote that the chipid you pass to :c:func:`v4l2_i2c_new_subdev` is usually 4738c2ecf20Sopenharmony_cithe same as the module name. It allows you to specify a chip variant, e.g. 4748c2ecf20Sopenharmony_ci"saa7114" or "saa7115". In general though the i2c driver autodetects this. 4758c2ecf20Sopenharmony_ciThe use of chipid is something that needs to be looked at more closely at a 4768c2ecf20Sopenharmony_cilater date. It differs between i2c drivers and as such can be confusing. 4778c2ecf20Sopenharmony_ciTo see which chip variants are supported you can look in the i2c driver code 4788c2ecf20Sopenharmony_cifor the i2c_device_id table. This lists all the possibilities. 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ciThere are one more helper function: 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci:c:func:`v4l2_i2c_new_subdev_board` uses an :c:type:`i2c_board_info` struct 4838c2ecf20Sopenharmony_ciwhich is passed to the i2c driver and replaces the irq, platform_data and addr 4848c2ecf20Sopenharmony_ciarguments. 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ciIf the subdev supports the s_config core ops, then that op is called with 4878c2ecf20Sopenharmony_cithe irq and platform_data arguments after the subdev was setup. 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ciThe :c:func:`v4l2_i2c_new_subdev` function will call 4908c2ecf20Sopenharmony_ci:c:func:`v4l2_i2c_new_subdev_board`, internally filling a 4918c2ecf20Sopenharmony_ci:c:type:`i2c_board_info` structure using the ``client_type`` and the 4928c2ecf20Sopenharmony_ci``addr`` to fill it. 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ciV4L2 sub-device functions and data structures 4958c2ecf20Sopenharmony_ci--------------------------------------------- 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci.. kernel-doc:: include/media/v4l2-subdev.h 498