162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ciV4L2 sub-devices 462306a36Sopenharmony_ci---------------- 562306a36Sopenharmony_ci 662306a36Sopenharmony_ciMany drivers need to communicate with sub-devices. These devices can do all 762306a36Sopenharmony_cisort of tasks, but most commonly they handle audio and/or video muxing, 862306a36Sopenharmony_ciencoding or decoding. For webcams common sub-devices are sensors and camera 962306a36Sopenharmony_cicontrollers. 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ciUsually these are I2C devices, but not necessarily. In order to provide the 1262306a36Sopenharmony_cidriver with a consistent interface to these sub-devices the 1362306a36Sopenharmony_ci:c:type:`v4l2_subdev` struct (v4l2-subdev.h) was created. 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciEach sub-device driver must have a :c:type:`v4l2_subdev` struct. This struct 1662306a36Sopenharmony_cican be stand-alone for simple sub-devices or it might be embedded in a larger 1762306a36Sopenharmony_cistruct if more state information needs to be stored. Usually there is a 1862306a36Sopenharmony_cilow-level device struct (e.g. ``i2c_client``) that contains the device data as 1962306a36Sopenharmony_cisetup by the kernel. It is recommended to store that pointer in the private 2062306a36Sopenharmony_cidata of :c:type:`v4l2_subdev` using :c:func:`v4l2_set_subdevdata`. That makes 2162306a36Sopenharmony_ciit easy to go from a :c:type:`v4l2_subdev` to the actual low-level bus-specific 2262306a36Sopenharmony_cidevice data. 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciYou also need a way to go from the low-level struct to :c:type:`v4l2_subdev`. 2562306a36Sopenharmony_ciFor the common i2c_client struct the i2c_set_clientdata() call is used to store 2662306a36Sopenharmony_cia :c:type:`v4l2_subdev` pointer, for other buses you may have to use other 2762306a36Sopenharmony_cimethods. 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ciBridges might also need to store per-subdev private data, such as a pointer to 3062306a36Sopenharmony_cibridge-specific per-subdev private data. The :c:type:`v4l2_subdev` structure 3162306a36Sopenharmony_ciprovides host private data for that purpose that can be accessed with 3262306a36Sopenharmony_ci:c:func:`v4l2_get_subdev_hostdata` and :c:func:`v4l2_set_subdev_hostdata`. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciFrom the bridge driver perspective, you load the sub-device module and somehow 3562306a36Sopenharmony_ciobtain the :c:type:`v4l2_subdev` pointer. For i2c devices this is easy: you call 3662306a36Sopenharmony_ci``i2c_get_clientdata()``. For other buses something similar needs to be done. 3762306a36Sopenharmony_ciHelper functions exist for sub-devices on an I2C bus that do most of this 3862306a36Sopenharmony_citricky work for you. 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciEach :c:type:`v4l2_subdev` contains function pointers that sub-device drivers 4162306a36Sopenharmony_cican implement (or leave ``NULL`` if it is not applicable). Since sub-devices can 4262306a36Sopenharmony_cido so many different things and you do not want to end up with a huge ops struct 4362306a36Sopenharmony_ciof which only a handful of ops are commonly implemented, the function pointers 4462306a36Sopenharmony_ciare sorted according to category and each category has its own ops struct. 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciThe top-level ops struct contains pointers to the category ops structs, which 4762306a36Sopenharmony_cimay be NULL if the subdev driver does not support anything from that category. 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciIt looks like this: 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci.. code-block:: c 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci struct v4l2_subdev_core_ops { 5462306a36Sopenharmony_ci int (*log_status)(struct v4l2_subdev *sd); 5562306a36Sopenharmony_ci int (*init)(struct v4l2_subdev *sd, u32 val); 5662306a36Sopenharmony_ci ... 5762306a36Sopenharmony_ci }; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci struct v4l2_subdev_tuner_ops { 6062306a36Sopenharmony_ci ... 6162306a36Sopenharmony_ci }; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci struct v4l2_subdev_audio_ops { 6462306a36Sopenharmony_ci ... 6562306a36Sopenharmony_ci }; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci struct v4l2_subdev_video_ops { 6862306a36Sopenharmony_ci ... 6962306a36Sopenharmony_ci }; 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci struct v4l2_subdev_pad_ops { 7262306a36Sopenharmony_ci ... 7362306a36Sopenharmony_ci }; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci struct v4l2_subdev_ops { 7662306a36Sopenharmony_ci const struct v4l2_subdev_core_ops *core; 7762306a36Sopenharmony_ci const struct v4l2_subdev_tuner_ops *tuner; 7862306a36Sopenharmony_ci const struct v4l2_subdev_audio_ops *audio; 7962306a36Sopenharmony_ci const struct v4l2_subdev_video_ops *video; 8062306a36Sopenharmony_ci const struct v4l2_subdev_pad_ops *video; 8162306a36Sopenharmony_ci }; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ciThe core ops are common to all subdevs, the other categories are implemented 8462306a36Sopenharmony_cidepending on the sub-device. E.g. a video device is unlikely to support the 8562306a36Sopenharmony_ciaudio ops and vice versa. 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ciThis setup limits the number of function pointers while still making it easy 8862306a36Sopenharmony_cito add new ops and categories. 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciA sub-device driver initializes the :c:type:`v4l2_subdev` struct using: 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci :c:func:`v4l2_subdev_init <v4l2_subdev_init>` 9362306a36Sopenharmony_ci (:c:type:`sd <v4l2_subdev>`, &\ :c:type:`ops <v4l2_subdev_ops>`). 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciAfterwards you need to initialize :c:type:`sd <v4l2_subdev>`->name with a 9762306a36Sopenharmony_ciunique name and set the module owner. This is done for you if you use the 9862306a36Sopenharmony_cii2c helper functions. 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciIf integration with the media framework is needed, you must initialize the 10162306a36Sopenharmony_ci:c:type:`media_entity` struct embedded in the :c:type:`v4l2_subdev` struct 10262306a36Sopenharmony_ci(entity field) by calling :c:func:`media_entity_pads_init`, if the entity has 10362306a36Sopenharmony_cipads: 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci.. code-block:: c 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci struct media_pad *pads = &my_sd->pads; 10862306a36Sopenharmony_ci int err; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci err = media_entity_pads_init(&sd->entity, npads, pads); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ciThe pads array must have been previously initialized. There is no need to 11362306a36Sopenharmony_cimanually set the struct media_entity function and name fields, but the 11462306a36Sopenharmony_cirevision field must be initialized if needed. 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ciA reference to the entity will be automatically acquired/released when the 11762306a36Sopenharmony_cisubdev device node (if any) is opened/closed. 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ciDon't forget to cleanup the media entity before the sub-device is destroyed: 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci.. code-block:: c 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci media_entity_cleanup(&sd->entity); 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ciIf a sub-device driver implements sink pads, the subdev driver may set the 12662306a36Sopenharmony_cilink_validate field in :c:type:`v4l2_subdev_pad_ops` to provide its own link 12762306a36Sopenharmony_civalidation function. For every link in the pipeline, the link_validate pad 12862306a36Sopenharmony_cioperation of the sink end of the link is called. In both cases the driver is 12962306a36Sopenharmony_cistill responsible for validating the correctness of the format configuration 13062306a36Sopenharmony_cibetween sub-devices and video nodes. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciIf link_validate op is not set, the default function 13362306a36Sopenharmony_ci:c:func:`v4l2_subdev_link_validate_default` is used instead. This function 13462306a36Sopenharmony_ciensures that width, height and the media bus pixel code are equal on both source 13562306a36Sopenharmony_ciand sink of the link. Subdev drivers are also free to use this function to 13662306a36Sopenharmony_ciperform the checks mentioned above in addition to their own checks. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciSubdev registration 13962306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~ 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ciThere are currently two ways to register subdevices with the V4L2 core. The 14262306a36Sopenharmony_cifirst (traditional) possibility is to have subdevices registered by bridge 14362306a36Sopenharmony_cidrivers. This can be done when the bridge driver has the complete information 14462306a36Sopenharmony_ciabout subdevices connected to it and knows exactly when to register them. This 14562306a36Sopenharmony_ciis typically the case for internal subdevices, like video data processing units 14662306a36Sopenharmony_ciwithin SoCs or complex PCI(e) boards, camera sensors in USB cameras or connected 14762306a36Sopenharmony_cito SoCs, which pass information about them to bridge drivers, usually in their 14862306a36Sopenharmony_ciplatform data. 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ciThere are however also situations where subdevices have to be registered 15162306a36Sopenharmony_ciasynchronously to bridge devices. An example of such a configuration is a Device 15262306a36Sopenharmony_ciTree based system where information about subdevices is made available to the 15362306a36Sopenharmony_cisystem independently from the bridge devices, e.g. when subdevices are defined 15462306a36Sopenharmony_ciin DT as I2C device nodes. The API used in this second case is described further 15562306a36Sopenharmony_cibelow. 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ciUsing one or the other registration method only affects the probing process, the 15862306a36Sopenharmony_cirun-time bridge-subdevice interaction is in both cases the same. 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ciRegistering synchronous sub-devices 16162306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ciIn the **synchronous** case a device (bridge) driver needs to register the 16462306a36Sopenharmony_ci:c:type:`v4l2_subdev` with the v4l2_device: 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci :c:func:`v4l2_device_register_subdev <v4l2_device_register_subdev>` 16762306a36Sopenharmony_ci (:c:type:`v4l2_dev <v4l2_device>`, :c:type:`sd <v4l2_subdev>`). 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ciThis can fail if the subdev module disappeared before it could be registered. 17062306a36Sopenharmony_ciAfter this function was called successfully the subdev->dev field points to 17162306a36Sopenharmony_cithe :c:type:`v4l2_device`. 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ciIf the v4l2_device parent device has a non-NULL mdev field, the sub-device 17462306a36Sopenharmony_cientity will be automatically registered with the media device. 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ciYou can unregister a sub-device using: 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci :c:func:`v4l2_device_unregister_subdev <v4l2_device_unregister_subdev>` 17962306a36Sopenharmony_ci (:c:type:`sd <v4l2_subdev>`). 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ciAfterwards the subdev module can be unloaded and 18262306a36Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->dev == ``NULL``. 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ciRegistering asynchronous sub-devices 18562306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ciIn the **asynchronous** case subdevice probing can be invoked independently of 18862306a36Sopenharmony_cithe bridge driver availability. The subdevice driver then has to verify whether 18962306a36Sopenharmony_ciall the requirements for a successful probing are satisfied. This can include a 19062306a36Sopenharmony_cicheck for a master clock availability. If any of the conditions aren't satisfied 19162306a36Sopenharmony_cithe driver might decide to return ``-EPROBE_DEFER`` to request further reprobing 19262306a36Sopenharmony_ciattempts. Once all conditions are met the subdevice shall be registered using 19362306a36Sopenharmony_cithe :c:func:`v4l2_async_register_subdev` function. Unregistration is 19462306a36Sopenharmony_ciperformed using the :c:func:`v4l2_async_unregister_subdev` call. Subdevices 19562306a36Sopenharmony_ciregistered this way are stored in a global list of subdevices, ready to be 19662306a36Sopenharmony_cipicked up by bridge drivers. 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ciAsynchronous sub-device notifiers 19962306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ciBridge drivers in turn have to register a notifier object. This is performed 20262306a36Sopenharmony_ciusing the :c:func:`v4l2_async_nf_register` call. To unregister the notifier the 20362306a36Sopenharmony_cidriver has to call :c:func:`v4l2_async_nf_unregister`. Before releasing memory 20462306a36Sopenharmony_ciof an unregister notifier, it must be cleaned up by calling 20562306a36Sopenharmony_ci:c:func:`v4l2_async_nf_cleanup`. 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ciBefore registering the notifier, bridge drivers must do two things: first, the 20862306a36Sopenharmony_cinotifier must be initialized using the :c:func:`v4l2_async_nf_init`. Second, 20962306a36Sopenharmony_cibridge drivers can then begin to form a list of async connection descriptors 21062306a36Sopenharmony_cithat the bridge device needs for its 21162306a36Sopenharmony_cioperation. :c:func:`v4l2_async_nf_add_fwnode`, 21262306a36Sopenharmony_ci:c:func:`v4l2_async_nf_add_fwnode_remote` and :c:func:`v4l2_async_nf_add_i2c` 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ciAsync connection descriptors describe connections to external sub-devices the 21562306a36Sopenharmony_cidrivers for which are not yet probed. Based on an async connection, a media data 21662306a36Sopenharmony_cior ancillary link may be created when the related sub-device becomes 21762306a36Sopenharmony_ciavailable. There may be one or more async connections to a given sub-device but 21862306a36Sopenharmony_cithis is not known at the time of adding the connections to the notifier. Async 21962306a36Sopenharmony_ciconnections are bound as matching async sub-devices are found, one by one. 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ciAsynchronous sub-device notifier for sub-devices 22262306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ciA driver that registers an asynchronous sub-device may also register an 22562306a36Sopenharmony_ciasynchronous notifier. This is called an asynchronous sub-device notifier andthe 22662306a36Sopenharmony_ciprocess is similar to that of a bridge driver apart from that the notifier is 22762306a36Sopenharmony_ciinitialised using :c:func:`v4l2_async_subdev_nf_init` instead. A sub-device 22862306a36Sopenharmony_cinotifier may complete only after the V4L2 device becomes available, i.e. there's 22962306a36Sopenharmony_cia path via async sub-devices and notifiers to a notifier that is not an 23062306a36Sopenharmony_ciasynchronous sub-device notifier. 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ciAsynchronous sub-device registration helper for camera sensor drivers 23362306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci:c:func:`v4l2_async_register_subdev_sensor` is a helper function for sensor 23662306a36Sopenharmony_cidrivers registering their own async connection, but it also registers a notifier 23762306a36Sopenharmony_ciand further registers async connections for lens and flash devices found in 23862306a36Sopenharmony_cifirmware. The notifier for the sub-device is unregistered and cleaned up with 23962306a36Sopenharmony_cithe async sub-device, using :c:func:`v4l2_async_unregister_subdev`. 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ciAsynchronous sub-device notifier example 24262306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciThese functions allocate an async connection descriptor which is of type struct 24562306a36Sopenharmony_ci:c:type:`v4l2_async_connection` embedded in a driver-specific struct. The &struct 24662306a36Sopenharmony_ci:c:type:`v4l2_async_connection` shall be the first member of this struct: 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci.. code-block:: c 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci struct my_async_connection { 25162306a36Sopenharmony_ci struct v4l2_async_connection asc; 25262306a36Sopenharmony_ci ... 25362306a36Sopenharmony_ci }; 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci struct my_async_connection *my_asc; 25662306a36Sopenharmony_ci struct fwnode_handle *ep; 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci ... 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci my_asc = v4l2_async_nf_add_fwnode_remote(¬ifier, ep, 26162306a36Sopenharmony_ci struct my_async_connection); 26262306a36Sopenharmony_ci fwnode_handle_put(ep); 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci if (IS_ERR(my_asc)) 26562306a36Sopenharmony_ci return PTR_ERR(my_asc); 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ciAsynchronous sub-device notifier callbacks 26862306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ciThe V4L2 core will then use these connection descriptors to match asynchronously 27162306a36Sopenharmony_ciregistered subdevices to them. If a match is detected the ``.bound()`` notifier 27262306a36Sopenharmony_cicallback is called. After all connections have been bound the .complete() 27362306a36Sopenharmony_cicallback is called. When a connection is removed from the system the 27462306a36Sopenharmony_ci``.unbind()`` method is called. All three callbacks are optional. 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ciDrivers can store any type of custom data in their driver-specific 27762306a36Sopenharmony_ci:c:type:`v4l2_async_connection` wrapper. If any of that data requires special 27862306a36Sopenharmony_cihandling when the structure is freed, drivers must implement the ``.destroy()`` 27962306a36Sopenharmony_cinotifier callback. The framework will call it right before freeing the 28062306a36Sopenharmony_ci:c:type:`v4l2_async_connection`. 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ciCalling subdev operations 28362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~ 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ciThe advantage of using :c:type:`v4l2_subdev` is that it is a generic struct and 28662306a36Sopenharmony_cidoes not contain any knowledge about the underlying hardware. So a driver might 28762306a36Sopenharmony_cicontain several subdevs that use an I2C bus, but also a subdev that is 28862306a36Sopenharmony_cicontrolled through GPIO pins. This distinction is only relevant when setting 28962306a36Sopenharmony_ciup the device, but once the subdev is registered it is completely transparent. 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ciOnce the subdev has been registered you can call an ops function either 29262306a36Sopenharmony_cidirectly: 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci.. code-block:: c 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci err = sd->ops->core->g_std(sd, &norm); 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_cibut it is better and easier to use this macro: 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci.. code-block:: c 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci err = v4l2_subdev_call(sd, core, g_std, &norm); 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ciThe macro will do the right ``NULL`` pointer checks and returns ``-ENODEV`` 30562306a36Sopenharmony_ciif :c:type:`sd <v4l2_subdev>` is ``NULL``, ``-ENOIOCTLCMD`` if either 30662306a36Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->core or :c:type:`sd <v4l2_subdev>`->core->g_std is ``NULL``, or the actual result of the 30762306a36Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->ops->core->g_std ops. 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ciIt is also possible to call all or a subset of the sub-devices: 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci.. code-block:: c 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci v4l2_device_call_all(v4l2_dev, 0, core, g_std, &norm); 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ciAny subdev that does not support this ops is skipped and error results are 31662306a36Sopenharmony_ciignored. If you want to check for errors use this: 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci.. code-block:: c 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_std, &norm); 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ciAny error except ``-ENOIOCTLCMD`` will exit the loop with that error. If no 32362306a36Sopenharmony_cierrors (except ``-ENOIOCTLCMD``) occurred, then 0 is returned. 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ciThe second argument to both calls is a group ID. If 0, then all subdevs are 32662306a36Sopenharmony_cicalled. If non-zero, then only those whose group ID match that value will 32762306a36Sopenharmony_cibe called. Before a bridge driver registers a subdev it can set 32862306a36Sopenharmony_ci:c:type:`sd <v4l2_subdev>`->grp_id to whatever value it wants (it's 0 by 32962306a36Sopenharmony_cidefault). This value is owned by the bridge driver and the sub-device driver 33062306a36Sopenharmony_ciwill never modify or use it. 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ciThe group ID gives the bridge driver more control how callbacks are called. 33362306a36Sopenharmony_ciFor example, there may be multiple audio chips on a board, each capable of 33462306a36Sopenharmony_cichanging the volume. But usually only one will actually be used when the 33562306a36Sopenharmony_ciuser want to change the volume. You can set the group ID for that subdev to 33662306a36Sopenharmony_cie.g. AUDIO_CONTROLLER and specify that as the group ID value when calling 33762306a36Sopenharmony_ci``v4l2_device_call_all()``. That ensures that it will only go to the subdev 33862306a36Sopenharmony_cithat needs it. 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ciIf the sub-device needs to notify its v4l2_device parent of an event, then 34162306a36Sopenharmony_ciit can call ``v4l2_subdev_notify(sd, notification, arg)``. This macro checks 34262306a36Sopenharmony_ciwhether there is a ``notify()`` callback defined and returns ``-ENODEV`` if not. 34362306a36Sopenharmony_ciOtherwise the result of the ``notify()`` call is returned. 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ciV4L2 sub-device userspace API 34662306a36Sopenharmony_ci----------------------------- 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ciBridge drivers traditionally expose one or multiple video nodes to userspace, 34962306a36Sopenharmony_ciand control subdevices through the :c:type:`v4l2_subdev_ops` operations in 35062306a36Sopenharmony_ciresponse to video node operations. This hides the complexity of the underlying 35162306a36Sopenharmony_cihardware from applications. For complex devices, finer-grained control of the 35262306a36Sopenharmony_cidevice than what the video nodes offer may be required. In those cases, bridge 35362306a36Sopenharmony_cidrivers that implement :ref:`the media controller API <media_controller>` may 35462306a36Sopenharmony_ciopt for making the subdevice operations directly accessible from userspace. 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ciDevice nodes named ``v4l-subdev``\ *X* can be created in ``/dev`` to access 35762306a36Sopenharmony_cisub-devices directly. If a sub-device supports direct userspace configuration 35862306a36Sopenharmony_ciit must set the ``V4L2_SUBDEV_FL_HAS_DEVNODE`` flag before being registered. 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ciAfter registering sub-devices, the :c:type:`v4l2_device` driver can create 36162306a36Sopenharmony_cidevice nodes for all registered sub-devices marked with 36262306a36Sopenharmony_ci``V4L2_SUBDEV_FL_HAS_DEVNODE`` by calling 36362306a36Sopenharmony_ci:c:func:`v4l2_device_register_subdev_nodes`. Those device nodes will be 36462306a36Sopenharmony_ciautomatically removed when sub-devices are unregistered. 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ciThe device node handles a subset of the V4L2 API. 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci``VIDIOC_QUERYCTRL``, 36962306a36Sopenharmony_ci``VIDIOC_QUERYMENU``, 37062306a36Sopenharmony_ci``VIDIOC_G_CTRL``, 37162306a36Sopenharmony_ci``VIDIOC_S_CTRL``, 37262306a36Sopenharmony_ci``VIDIOC_G_EXT_CTRLS``, 37362306a36Sopenharmony_ci``VIDIOC_S_EXT_CTRLS`` and 37462306a36Sopenharmony_ci``VIDIOC_TRY_EXT_CTRLS``: 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci The controls ioctls are identical to the ones defined in V4L2. They 37762306a36Sopenharmony_ci behave identically, with the only exception that they deal only with 37862306a36Sopenharmony_ci controls implemented in the sub-device. Depending on the driver, those 37962306a36Sopenharmony_ci controls can be also be accessed through one (or several) V4L2 device 38062306a36Sopenharmony_ci nodes. 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci``VIDIOC_DQEVENT``, 38362306a36Sopenharmony_ci``VIDIOC_SUBSCRIBE_EVENT`` and 38462306a36Sopenharmony_ci``VIDIOC_UNSUBSCRIBE_EVENT`` 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci The events ioctls are identical to the ones defined in V4L2. They 38762306a36Sopenharmony_ci behave identically, with the only exception that they deal only with 38862306a36Sopenharmony_ci events generated by the sub-device. Depending on the driver, those 38962306a36Sopenharmony_ci events can also be reported by one (or several) V4L2 device nodes. 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ci Sub-device drivers that want to use events need to set the 39262306a36Sopenharmony_ci ``V4L2_SUBDEV_FL_HAS_EVENTS`` :c:type:`v4l2_subdev`.flags before registering 39362306a36Sopenharmony_ci the sub-device. After registration events can be queued as usual on the 39462306a36Sopenharmony_ci :c:type:`v4l2_subdev`.devnode device node. 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci To properly support events, the ``poll()`` file operation is also 39762306a36Sopenharmony_ci implemented. 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ciPrivate ioctls 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci All ioctls not in the above list are passed directly to the sub-device 40262306a36Sopenharmony_ci driver through the core::ioctl operation. 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ciRead-only sub-device userspace API 40562306a36Sopenharmony_ci---------------------------------- 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ciBridge drivers that control their connected subdevices through direct calls to 40862306a36Sopenharmony_cithe kernel API realized by :c:type:`v4l2_subdev_ops` structure do not usually 40962306a36Sopenharmony_ciwant userspace to be able to change the same parameters through the subdevice 41062306a36Sopenharmony_cidevice node and thus do not usually register any. 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ciIt is sometimes useful to report to userspace the current subdevice 41362306a36Sopenharmony_ciconfiguration through a read-only API, that does not permit applications to 41462306a36Sopenharmony_cichange to the device parameters but allows interfacing to the subdevice device 41562306a36Sopenharmony_cinode to inspect them. 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ciFor instance, to implement cameras based on computational photography, userspace 41862306a36Sopenharmony_cineeds to know the detailed camera sensor configuration (in terms of skipping, 41962306a36Sopenharmony_cibinning, cropping and scaling) for each supported output resolution. To support 42062306a36Sopenharmony_cisuch use cases, bridge drivers may expose the subdevice operations to userspace 42162306a36Sopenharmony_cithrough a read-only API. 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ciTo create a read-only device node for all the subdevices registered with the 42462306a36Sopenharmony_ci``V4L2_SUBDEV_FL_HAS_DEVNODE`` set, the :c:type:`v4l2_device` driver should call 42562306a36Sopenharmony_ci:c:func:`v4l2_device_register_ro_subdev_nodes`. 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ciAccess to the following ioctls for userspace applications is restricted on 42862306a36Sopenharmony_cisub-device device nodes registered with 42962306a36Sopenharmony_ci:c:func:`v4l2_device_register_ro_subdev_nodes`. 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci``VIDIOC_SUBDEV_S_FMT``, 43262306a36Sopenharmony_ci``VIDIOC_SUBDEV_S_CROP``, 43362306a36Sopenharmony_ci``VIDIOC_SUBDEV_S_SELECTION``: 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci These ioctls are only allowed on a read-only subdevice device node 43662306a36Sopenharmony_ci for the :ref:`V4L2_SUBDEV_FORMAT_TRY <v4l2-subdev-format-whence>` 43762306a36Sopenharmony_ci formats and selection rectangles. 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ci``VIDIOC_SUBDEV_S_FRAME_INTERVAL``, 44062306a36Sopenharmony_ci``VIDIOC_SUBDEV_S_DV_TIMINGS``, 44162306a36Sopenharmony_ci``VIDIOC_SUBDEV_S_STD``: 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ci These ioctls are not allowed on a read-only subdevice node. 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ciIn case the ioctl is not allowed, or the format to modify is set to 44662306a36Sopenharmony_ci``V4L2_SUBDEV_FORMAT_ACTIVE``, the core returns a negative error code and 44762306a36Sopenharmony_cithe errno variable is set to ``-EPERM``. 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ciI2C sub-device drivers 45062306a36Sopenharmony_ci---------------------- 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ciSince these drivers are so common, special helper functions are available to 45362306a36Sopenharmony_ciease the use of these drivers (``v4l2-common.h``). 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ciThe recommended method of adding :c:type:`v4l2_subdev` support to an I2C driver 45662306a36Sopenharmony_ciis to embed the :c:type:`v4l2_subdev` struct into the state struct that is 45762306a36Sopenharmony_cicreated for each I2C device instance. Very simple devices have no state 45862306a36Sopenharmony_cistruct and in that case you can just create a :c:type:`v4l2_subdev` directly. 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ciA typical state struct would look like this (where 'chipname' is replaced by 46162306a36Sopenharmony_cithe name of the chip): 46262306a36Sopenharmony_ci 46362306a36Sopenharmony_ci.. code-block:: c 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_ci struct chipname_state { 46662306a36Sopenharmony_ci struct v4l2_subdev sd; 46762306a36Sopenharmony_ci ... /* additional state fields */ 46862306a36Sopenharmony_ci }; 46962306a36Sopenharmony_ci 47062306a36Sopenharmony_ciInitialize the :c:type:`v4l2_subdev` struct as follows: 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ci.. code-block:: c 47362306a36Sopenharmony_ci 47462306a36Sopenharmony_ci v4l2_i2c_subdev_init(&state->sd, client, subdev_ops); 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ciThis function will fill in all the fields of :c:type:`v4l2_subdev` ensure that 47762306a36Sopenharmony_cithe :c:type:`v4l2_subdev` and i2c_client both point to one another. 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ciYou should also add a helper inline function to go from a :c:type:`v4l2_subdev` 48062306a36Sopenharmony_cipointer to a chipname_state struct: 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ci.. code-block:: c 48362306a36Sopenharmony_ci 48462306a36Sopenharmony_ci static inline struct chipname_state *to_state(struct v4l2_subdev *sd) 48562306a36Sopenharmony_ci { 48662306a36Sopenharmony_ci return container_of(sd, struct chipname_state, sd); 48762306a36Sopenharmony_ci } 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ciUse this to go from the :c:type:`v4l2_subdev` struct to the ``i2c_client`` 49062306a36Sopenharmony_cistruct: 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci.. code-block:: c 49362306a36Sopenharmony_ci 49462306a36Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(sd); 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ciAnd this to go from an ``i2c_client`` to a :c:type:`v4l2_subdev` struct: 49762306a36Sopenharmony_ci 49862306a36Sopenharmony_ci.. code-block:: c 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_ci struct v4l2_subdev *sd = i2c_get_clientdata(client); 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ciMake sure to call 50362306a36Sopenharmony_ci:c:func:`v4l2_device_unregister_subdev`\ (:c:type:`sd <v4l2_subdev>`) 50462306a36Sopenharmony_ciwhen the ``remove()`` callback is called. This will unregister the sub-device 50562306a36Sopenharmony_cifrom the bridge driver. It is safe to call this even if the sub-device was 50662306a36Sopenharmony_cinever registered. 50762306a36Sopenharmony_ci 50862306a36Sopenharmony_ciYou need to do this because when the bridge driver destroys the i2c adapter 50962306a36Sopenharmony_cithe ``remove()`` callbacks are called of the i2c devices on that adapter. 51062306a36Sopenharmony_ciAfter that the corresponding v4l2_subdev structures are invalid, so they 51162306a36Sopenharmony_cihave to be unregistered first. Calling 51262306a36Sopenharmony_ci:c:func:`v4l2_device_unregister_subdev`\ (:c:type:`sd <v4l2_subdev>`) 51362306a36Sopenharmony_cifrom the ``remove()`` callback ensures that this is always done correctly. 51462306a36Sopenharmony_ci 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_ciThe bridge driver also has some helper functions it can use: 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ci.. code-block:: c 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci struct v4l2_subdev *sd = v4l2_i2c_new_subdev(v4l2_dev, adapter, 52162306a36Sopenharmony_ci "module_foo", "chipid", 0x36, NULL); 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ciThis loads the given module (can be ``NULL`` if no module needs to be loaded) 52462306a36Sopenharmony_ciand calls :c:func:`i2c_new_client_device` with the given ``i2c_adapter`` and 52562306a36Sopenharmony_cichip/address arguments. If all goes well, then it registers the subdev with 52662306a36Sopenharmony_cithe v4l2_device. 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_ciYou can also use the last argument of :c:func:`v4l2_i2c_new_subdev` to pass 52962306a36Sopenharmony_cian array of possible I2C addresses that it should probe. These probe addresses 53062306a36Sopenharmony_ciare only used if the previous argument is 0. A non-zero argument means that you 53162306a36Sopenharmony_ciknow the exact i2c address so in that case no probing will take place. 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ciBoth functions return ``NULL`` if something went wrong. 53462306a36Sopenharmony_ci 53562306a36Sopenharmony_ciNote that the chipid you pass to :c:func:`v4l2_i2c_new_subdev` is usually 53662306a36Sopenharmony_cithe same as the module name. It allows you to specify a chip variant, e.g. 53762306a36Sopenharmony_ci"saa7114" or "saa7115". In general though the i2c driver autodetects this. 53862306a36Sopenharmony_ciThe use of chipid is something that needs to be looked at more closely at a 53962306a36Sopenharmony_cilater date. It differs between i2c drivers and as such can be confusing. 54062306a36Sopenharmony_ciTo see which chip variants are supported you can look in the i2c driver code 54162306a36Sopenharmony_cifor the i2c_device_id table. This lists all the possibilities. 54262306a36Sopenharmony_ci 54362306a36Sopenharmony_ciThere are one more helper function: 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ci:c:func:`v4l2_i2c_new_subdev_board` uses an :c:type:`i2c_board_info` struct 54662306a36Sopenharmony_ciwhich is passed to the i2c driver and replaces the irq, platform_data and addr 54762306a36Sopenharmony_ciarguments. 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ciIf the subdev supports the s_config core ops, then that op is called with 55062306a36Sopenharmony_cithe irq and platform_data arguments after the subdev was setup. 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ciThe :c:func:`v4l2_i2c_new_subdev` function will call 55362306a36Sopenharmony_ci:c:func:`v4l2_i2c_new_subdev_board`, internally filling a 55462306a36Sopenharmony_ci:c:type:`i2c_board_info` structure using the ``client_type`` and the 55562306a36Sopenharmony_ci``addr`` to fill it. 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ciCentrally managed subdev active state 55862306a36Sopenharmony_ci------------------------------------- 55962306a36Sopenharmony_ci 56062306a36Sopenharmony_ciTraditionally V4L2 subdev drivers maintained internal state for the active 56162306a36Sopenharmony_cidevice configuration. This is often implemented as e.g. an array of struct 56262306a36Sopenharmony_civ4l2_mbus_framefmt, one entry for each pad, and similarly for crop and compose 56362306a36Sopenharmony_cirectangles. 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ciIn addition to the active configuration, each subdev file handle has an array of 56662306a36Sopenharmony_cistruct v4l2_subdev_pad_config, managed by the V4L2 core, which contains the try 56762306a36Sopenharmony_ciconfiguration. 56862306a36Sopenharmony_ci 56962306a36Sopenharmony_ciTo simplify the subdev drivers the V4L2 subdev API now optionally supports a 57062306a36Sopenharmony_cicentrally managed active configuration represented by 57162306a36Sopenharmony_ci:c:type:`v4l2_subdev_state`. One instance of state, which contains the active 57262306a36Sopenharmony_cidevice configuration, is stored in the sub-device itself as part of 57362306a36Sopenharmony_cithe :c:type:`v4l2_subdev` structure, while the core associates a try state to 57462306a36Sopenharmony_cieach open file handle, to store the try configuration related to that file 57562306a36Sopenharmony_cihandle. 57662306a36Sopenharmony_ci 57762306a36Sopenharmony_ciSub-device drivers can opt-in and use state to manage their active configuration 57862306a36Sopenharmony_ciby initializing the subdevice state with a call to v4l2_subdev_init_finalize() 57962306a36Sopenharmony_cibefore registering the sub-device. They must also call v4l2_subdev_cleanup() 58062306a36Sopenharmony_cito release all the allocated resources before unregistering the sub-device. 58162306a36Sopenharmony_ciThe core automatically allocates and initializes a state for each open file 58262306a36Sopenharmony_cihandle to store the try configurations and frees it when closing the file 58362306a36Sopenharmony_cihandle. 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ciV4L2 sub-device operations that use both the :ref:`ACTIVE and TRY formats 58662306a36Sopenharmony_ci<v4l2-subdev-format-whence>` receive the correct state to operate on through 58762306a36Sopenharmony_cithe 'state' parameter. The state must be locked and unlocked by the 58862306a36Sopenharmony_cicaller by calling :c:func:`v4l2_subdev_lock_state()` and 58962306a36Sopenharmony_ci:c:func:`v4l2_subdev_unlock_state()`. The caller can do so by calling the subdev 59062306a36Sopenharmony_cioperation through the :c:func:`v4l2_subdev_call_state_active()` macro. 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ciOperations that do not receive a state parameter implicitly operate on the 59362306a36Sopenharmony_cisubdevice active state, which drivers can exclusively access by 59462306a36Sopenharmony_cicalling :c:func:`v4l2_subdev_lock_and_get_active_state()`. The sub-device active 59562306a36Sopenharmony_cistate must equally be released by calling :c:func:`v4l2_subdev_unlock_state()`. 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ciDrivers must never manually access the state stored in the :c:type:`v4l2_subdev` 59862306a36Sopenharmony_cior in the file handle without going through the designated helpers. 59962306a36Sopenharmony_ci 60062306a36Sopenharmony_ciWhile the V4L2 core passes the correct try or active state to the subdevice 60162306a36Sopenharmony_cioperations, many existing device drivers pass a NULL state when calling 60262306a36Sopenharmony_cioperations with :c:func:`v4l2_subdev_call()`. This legacy construct causes 60362306a36Sopenharmony_ciissues with subdevice drivers that let the V4L2 core manage the active state, 60462306a36Sopenharmony_cias they expect to receive the appropriate state as a parameter. To help the 60562306a36Sopenharmony_ciconversion of subdevice drivers to a managed active state without having to 60662306a36Sopenharmony_ciconvert all callers at the same time, an additional wrapper layer has been 60762306a36Sopenharmony_ciadded to v4l2_subdev_call(), which handles the NULL case by getting and locking 60862306a36Sopenharmony_cithe callee's active state with :c:func:`v4l2_subdev_lock_and_get_active_state()`, 60962306a36Sopenharmony_ciand unlocking the state after the call. 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ciThe whole subdev state is in reality split into three parts: the 61262306a36Sopenharmony_civ4l2_subdev_state, subdev controls and subdev driver's internal state. In the 61362306a36Sopenharmony_cifuture these parts should be combined into a single state. For the time being 61462306a36Sopenharmony_ciwe need a way to handle the locking for these parts. This can be accomplished 61562306a36Sopenharmony_ciby sharing a lock. The v4l2_ctrl_handler already supports this via its 'lock' 61662306a36Sopenharmony_cipointer and the same model is used with states. The driver can do the following 61762306a36Sopenharmony_cibefore calling v4l2_subdev_init_finalize(): 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ci.. code-block:: c 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci sd->ctrl_handler->lock = &priv->mutex; 62262306a36Sopenharmony_ci sd->state_lock = &priv->mutex; 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ciThis shares the driver's private mutex between the controls and the states. 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ciStreams, multiplexed media pads and internal routing 62762306a36Sopenharmony_ci---------------------------------------------------- 62862306a36Sopenharmony_ci 62962306a36Sopenharmony_ciA subdevice driver can implement support for multiplexed streams by setting 63062306a36Sopenharmony_cithe V4L2_SUBDEV_FL_STREAMS subdev flag and implementing support for 63162306a36Sopenharmony_cicentrally managed subdev active state, routing and stream based 63262306a36Sopenharmony_ciconfiguration. 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ciV4L2 sub-device functions and data structures 63562306a36Sopenharmony_ci--------------------------------------------- 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ci.. kernel-doc:: include/media/v4l2-subdev.h 638