blob: 3e2b25b47ff4e0fb17ab92a86137acdc5b015367 [file] [log] [blame]
Mauro Carvalho Chehab840b14d2016-07-20 15:27:04 -03001V4L2 sub-devices
2----------------
3
4Many drivers need to communicate with sub-devices. These devices can do all
5sort of tasks, but most commonly they handle audio and/or video muxing,
6encoding or decoding. For webcams common sub-devices are sensors and camera
7controllers.
8
9Usually these are I2C devices, but not necessarily. In order to provide the
10driver with a consistent interface to these sub-devices the v4l2_subdev struct
11(v4l2-subdev.h) was created.
12
13Each sub-device driver must have a v4l2_subdev struct. This struct can be
14stand-alone for simple sub-devices or it might be embedded in a larger struct
15if more state information needs to be stored. Usually there is a low-level
16device struct (e.g. i2c_client) that contains the device data as setup
17by the kernel. It is recommended to store that pointer in the private
18data of v4l2_subdev using v4l2_set_subdevdata(). That makes it easy to go
19from a v4l2_subdev to the actual low-level bus-specific device data.
20
21You also need a way to go from the low-level struct to v4l2_subdev. For the
22common i2c_client struct the i2c_set_clientdata() call is used to store a
23v4l2_subdev pointer, for other busses you may have to use other methods.
24
25Bridges might also need to store per-subdev private data, such as a pointer to
26bridge-specific per-subdev private data. The v4l2_subdev structure provides
27host private data for that purpose that can be accessed with
28v4l2_get_subdev_hostdata() and v4l2_set_subdev_hostdata().
29
30From the bridge driver perspective you load the sub-device module and somehow
31obtain the v4l2_subdev pointer. For i2c devices this is easy: you call
32i2c_get_clientdata(). For other busses something similar needs to be done.
33Helper functions exists for sub-devices on an I2C bus that do most of this
34tricky work for you.
35
36Each v4l2_subdev contains function pointers that sub-device drivers can
37implement (or leave NULL if it is not applicable). Since sub-devices can do
38so many different things and you do not want to end up with a huge ops struct
39of which only a handful of ops are commonly implemented, the function pointers
40are sorted according to category and each category has its own ops struct.
41
42The top-level ops struct contains pointers to the category ops structs, which
43may be NULL if the subdev driver does not support anything from that category.
44
45It looks like this:
46
47.. code-block:: none
48
49 struct v4l2_subdev_core_ops {
50 int (*log_status)(struct v4l2_subdev *sd);
51 int (*init)(struct v4l2_subdev *sd, u32 val);
52 ...
53 };
54
55 struct v4l2_subdev_tuner_ops {
56 ...
57 };
58
59 struct v4l2_subdev_audio_ops {
60 ...
61 };
62
63 struct v4l2_subdev_video_ops {
64 ...
65 };
66
67 struct v4l2_subdev_pad_ops {
68 ...
69 };
70
71 struct v4l2_subdev_ops {
72 const struct v4l2_subdev_core_ops *core;
73 const struct v4l2_subdev_tuner_ops *tuner;
74 const struct v4l2_subdev_audio_ops *audio;
75 const struct v4l2_subdev_video_ops *video;
76 const struct v4l2_subdev_pad_ops *video;
77 };
78
79The core ops are common to all subdevs, the other categories are implemented
80depending on the sub-device. E.g. a video device is unlikely to support the
81audio ops and vice versa.
82
83This setup limits the number of function pointers while still making it easy
84to add new ops and categories.
85
86A sub-device driver initializes the v4l2_subdev struct using:
87
88.. code-block:: none
89
90 v4l2_subdev_init(sd, &ops);
91
92Afterwards you need to initialize subdev->name with a unique name and set the
93module owner. This is done for you if you use the i2c helper functions.
94
95If integration with the media framework is needed, you must initialize the
96media_entity struct embedded in the v4l2_subdev struct (entity field) by
97calling media_entity_pads_init(), if the entity has pads:
98
99.. code-block:: none
100
101 struct media_pad *pads = &my_sd->pads;
102 int err;
103
104 err = media_entity_pads_init(&sd->entity, npads, pads);
105
106The pads array must have been previously initialized. There is no need to
107manually set the struct media_entity function and name fields, but the
108revision field must be initialized if needed.
109
110A reference to the entity will be automatically acquired/released when the
111subdev device node (if any) is opened/closed.
112
113Don't forget to cleanup the media entity before the sub-device is destroyed:
114
115.. code-block:: none
116
117 media_entity_cleanup(&sd->entity);
118
119If the subdev driver intends to process video and integrate with the media
120framework, it must implement format related functionality using
121v4l2_subdev_pad_ops instead of v4l2_subdev_video_ops.
122
123In that case, the subdev driver may set the link_validate field to provide
124its own link validation function. The link validation function is called for
125every link in the pipeline where both of the ends of the links are V4L2
126sub-devices. The driver is still responsible for validating the correctness
127of the format configuration between sub-devices and video nodes.
128
129If link_validate op is not set, the default function
130v4l2_subdev_link_validate_default() is used instead. This function ensures
131that width, height and the media bus pixel code are equal on both source and
132sink of the link. Subdev drivers are also free to use this function to
133perform the checks mentioned above in addition to their own checks.
134
135There are currently two ways to register subdevices with the V4L2 core. The
136first (traditional) possibility is to have subdevices registered by bridge
137drivers. This can be done when the bridge driver has the complete information
138about subdevices connected to it and knows exactly when to register them. This
139is typically the case for internal subdevices, like video data processing units
140within SoCs or complex PCI(e) boards, camera sensors in USB cameras or connected
141to SoCs, which pass information about them to bridge drivers, usually in their
142platform data.
143
144There are however also situations where subdevices have to be registered
145asynchronously to bridge devices. An example of such a configuration is a Device
146Tree based system where information about subdevices is made available to the
147system independently from the bridge devices, e.g. when subdevices are defined
148in DT as I2C device nodes. The API used in this second case is described further
149below.
150
151Using one or the other registration method only affects the probing process, the
152run-time bridge-subdevice interaction is in both cases the same.
153
154In the synchronous case a device (bridge) driver needs to register the
155v4l2_subdev with the v4l2_device:
156
157.. code-block:: none
158
159 int err = v4l2_device_register_subdev(v4l2_dev, sd);
160
161This can fail if the subdev module disappeared before it could be registered.
162After this function was called successfully the subdev->dev field points to
163the v4l2_device.
164
165If the v4l2_device parent device has a non-NULL mdev field, the sub-device
166entity will be automatically registered with the media device.
167
168You can unregister a sub-device using:
169
170.. code-block:: none
171
172 v4l2_device_unregister_subdev(sd);
173
174Afterwards the subdev module can be unloaded and sd->dev == NULL.
175
176You can call an ops function either directly:
177
178.. code-block:: none
179
180 err = sd->ops->core->g_std(sd, &norm);
181
182but it is better and easier to use this macro:
183
184.. code-block:: none
185
186 err = v4l2_subdev_call(sd, core, g_std, &norm);
187
188The macro will to the right NULL pointer checks and returns -ENODEV if subdev
189is NULL, -ENOIOCTLCMD if either subdev->core or subdev->core->g_std is
190NULL, or the actual result of the subdev->ops->core->g_std ops.
191
192It is also possible to call all or a subset of the sub-devices:
193
194.. code-block:: none
195
196 v4l2_device_call_all(v4l2_dev, 0, core, g_std, &norm);
197
198Any subdev that does not support this ops is skipped and error results are
199ignored. If you want to check for errors use this:
200
201.. code-block:: none
202
203 err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_std, &norm);
204
205Any error except -ENOIOCTLCMD will exit the loop with that error. If no
206errors (except -ENOIOCTLCMD) occurred, then 0 is returned.
207
208The second argument to both calls is a group ID. If 0, then all subdevs are
209called. If non-zero, then only those whose group ID match that value will
210be called. Before a bridge driver registers a subdev it can set sd->grp_id
211to whatever value it wants (it's 0 by default). This value is owned by the
212bridge driver and the sub-device driver will never modify or use it.
213
214The group ID gives the bridge driver more control how callbacks are called.
215For example, there may be multiple audio chips on a board, each capable of
216changing the volume. But usually only one will actually be used when the
217user want to change the volume. You can set the group ID for that subdev to
218e.g. AUDIO_CONTROLLER and specify that as the group ID value when calling
219v4l2_device_call_all(). That ensures that it will only go to the subdev
220that needs it.
221
222If the sub-device needs to notify its v4l2_device parent of an event, then
223it can call v4l2_subdev_notify(sd, notification, arg). This macro checks
224whether there is a notify() callback defined and returns -ENODEV if not.
225Otherwise the result of the notify() call is returned.
226
227The advantage of using v4l2_subdev is that it is a generic struct and does
228not contain any knowledge about the underlying hardware. So a driver might
229contain several subdevs that use an I2C bus, but also a subdev that is
230controlled through GPIO pins. This distinction is only relevant when setting
231up the device, but once the subdev is registered it is completely transparent.
232
233
234In the asynchronous case subdevice probing can be invoked independently of the
235bridge driver availability. The subdevice driver then has to verify whether all
236the requirements for a successful probing are satisfied. This can include a
237check for a master clock availability. If any of the conditions aren't satisfied
238the driver might decide to return -EPROBE_DEFER to request further reprobing
239attempts. Once all conditions are met the subdevice shall be registered using
240the v4l2_async_register_subdev() function. Unregistration is performed using
241the v4l2_async_unregister_subdev() call. Subdevices registered this way are
242stored in a global list of subdevices, ready to be picked up by bridge drivers.
243
244Bridge drivers in turn have to register a notifier object with an array of
245subdevice descriptors that the bridge device needs for its operation. This is
246performed using the v4l2_async_notifier_register() call. To unregister the
247notifier the driver has to call v4l2_async_notifier_unregister(). The former of
248the two functions takes two arguments: a pointer to struct v4l2_device and a
249pointer to struct v4l2_async_notifier. The latter contains a pointer to an array
250of pointers to subdevice descriptors of type struct v4l2_async_subdev type. The
251V4L2 core will then use these descriptors to match asynchronously registered
252subdevices to them. If a match is detected the .bound() notifier callback is
253called. After all subdevices have been located the .complete() callback is
254called. When a subdevice is removed from the system the .unbind() method is
255called. All three callbacks are optional.
256
Mauro Carvalho Chehab58759872016-07-20 14:14:37 -0300257V4L2 subdev kAPI
258^^^^^^^^^^^^^^^^
259
260.. kernel-doc:: include/media/v4l2-subdev.h