1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Media Controller ancillary functions
6 * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@kernel.org>
7 * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com>
8 * Copyright (C) 2006-2010 Nokia Corporation
9 * Copyright (c) 2016 Intel Corporation.
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/usb.h>
15 #include <media/media-device.h>
16 #include <media/media-entity.h>
17 #include <media/v4l2-fh.h>
18 #include <media/v4l2-mc.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/videobuf2-core.h>
22 int v4l2_mc_create_media_graph(struct media_device *mdev)
25 struct media_entity *entity;
26 struct media_entity *if_vid = NULL, *if_aud = NULL;
27 struct media_entity *tuner = NULL, *decoder = NULL;
28 struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL;
29 bool is_webcam = false;
31 int ret, pad_sink, pad_source;
36 media_device_for_each_entity(entity, mdev) {
37 switch (entity->function) {
38 case MEDIA_ENT_F_IF_VID_DECODER:
41 case MEDIA_ENT_F_IF_AUD_DECODER:
44 case MEDIA_ENT_F_TUNER:
47 case MEDIA_ENT_F_ATV_DECODER:
50 case MEDIA_ENT_F_IO_V4L:
53 case MEDIA_ENT_F_IO_VBI:
56 case MEDIA_ENT_F_IO_SWRADIO:
59 case MEDIA_ENT_F_CAM_SENSOR:
65 /* It should have at least one I/O entity */
66 if (!io_v4l && !io_vbi && !io_swradio) {
67 dev_warn(mdev->dev, "Didn't find any I/O entity\n");
72 * Here, webcams are modelled on a very simple way: the sensor is
73 * connected directly to the I/O entity. All dirty details, like
74 * scaler and crop HW are hidden. While such mapping is not enough
75 * for mc-centric hardware, it is enough for v4l2 interface centric
76 * PC-consumer's hardware.
80 dev_warn(mdev->dev, "Didn't find a MEDIA_ENT_F_IO_V4L\n");
84 media_device_for_each_entity(entity, mdev) {
85 if (entity->function != MEDIA_ENT_F_CAM_SENSOR)
87 ret = media_create_pad_link(entity, 0,
89 MEDIA_LNK_FL_ENABLED);
91 dev_warn(mdev->dev, "Failed to create a sensor link\n");
99 /* The device isn't a webcam. So, it should have a decoder */
101 dev_warn(mdev->dev, "Decoder not found\n");
105 /* Link the tuner and IF video output pads */
108 pad_source = media_get_pad_index(tuner,
111 pad_sink = media_get_pad_index(if_vid,
114 if (pad_source < 0 || pad_sink < 0) {
115 dev_warn(mdev->dev, "Couldn't get tuner and/or PLL pad(s): (%d, %d)\n",
116 pad_source, pad_sink);
119 ret = media_create_pad_link(tuner, pad_source,
121 MEDIA_LNK_FL_ENABLED);
123 dev_warn(mdev->dev, "Couldn't create tuner->PLL link)\n");
127 pad_source = media_get_pad_index(if_vid,
130 pad_sink = media_get_pad_index(decoder,
133 if (pad_source < 0 || pad_sink < 0) {
134 dev_warn(mdev->dev, "get decoder and/or PLL pad(s): (%d, %d)\n",
135 pad_source, pad_sink);
138 ret = media_create_pad_link(if_vid, pad_source,
140 MEDIA_LNK_FL_ENABLED);
142 dev_warn(mdev->dev, "couldn't link PLL to decoder\n");
146 pad_source = media_get_pad_index(tuner,
149 pad_sink = media_get_pad_index(decoder,
152 if (pad_source < 0 || pad_sink < 0) {
153 dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s): (%d, %d)\n",
154 pad_source, pad_sink);
157 ret = media_create_pad_link(tuner, pad_source,
159 MEDIA_LNK_FL_ENABLED);
165 pad_source = media_get_pad_index(tuner,
168 pad_sink = media_get_pad_index(if_aud,
171 if (pad_source < 0 || pad_sink < 0) {
172 dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s) for audio: (%d, %d)\n",
173 pad_source, pad_sink);
176 ret = media_create_pad_link(tuner, pad_source,
178 MEDIA_LNK_FL_ENABLED);
180 dev_warn(mdev->dev, "couldn't link tuner->audio PLL\n");
189 /* Create demod to V4L, VBI and SDR radio links */
191 pad_source = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
193 if (pad_source < 0) {
194 dev_warn(mdev->dev, "couldn't get decoder output pad for V4L I/O\n");
197 ret = media_create_pad_link(decoder, pad_source,
199 MEDIA_LNK_FL_ENABLED);
201 dev_warn(mdev->dev, "couldn't link decoder output to V4L I/O\n");
207 pad_source = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
209 if (pad_source < 0) {
210 dev_warn(mdev->dev, "couldn't get decoder output pad for SDR\n");
213 ret = media_create_pad_link(decoder, pad_source,
215 MEDIA_LNK_FL_ENABLED);
217 dev_warn(mdev->dev, "couldn't link decoder output to SDR\n");
223 pad_source = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
225 if (pad_source < 0) {
226 dev_warn(mdev->dev, "couldn't get decoder output pad for VBI\n");
229 ret = media_create_pad_link(decoder, pad_source,
231 MEDIA_LNK_FL_ENABLED);
233 dev_warn(mdev->dev, "couldn't link decoder output to VBI\n");
238 /* Create links for the media connectors */
239 flags = MEDIA_LNK_FL_ENABLED;
240 media_device_for_each_entity(entity, mdev) {
241 switch (entity->function) {
242 case MEDIA_ENT_F_CONN_RF:
245 pad_sink = media_get_pad_index(tuner, MEDIA_PAD_FL_SINK,
248 dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
251 ret = media_create_pad_link(entity, 0, tuner,
255 case MEDIA_ENT_F_CONN_SVIDEO:
256 case MEDIA_ENT_F_CONN_COMPOSITE:
257 pad_sink = media_get_pad_index(decoder,
261 dev_warn(mdev->dev, "couldn't get decoder analog pad sink\n");
264 ret = media_create_pad_link(entity, 0, decoder,
279 EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
281 int v4l_enable_media_source(struct video_device *vdev)
283 struct media_device *mdev = vdev->entity.graph_obj.mdev;
289 mutex_lock(&mdev->graph_mutex);
290 if (!mdev->enable_source)
292 err = mdev->enable_source(&vdev->entity, &vdev->pipe);
296 mutex_unlock(&mdev->graph_mutex);
299 EXPORT_SYMBOL_GPL(v4l_enable_media_source);
301 void v4l_disable_media_source(struct video_device *vdev)
303 struct media_device *mdev = vdev->entity.graph_obj.mdev;
306 mutex_lock(&mdev->graph_mutex);
307 if (mdev->disable_source)
308 mdev->disable_source(&vdev->entity);
309 mutex_unlock(&mdev->graph_mutex);
312 EXPORT_SYMBOL_GPL(v4l_disable_media_source);
314 int v4l_vb2q_enable_media_source(struct vb2_queue *q)
316 struct v4l2_fh *fh = q->owner;
319 return v4l_enable_media_source(fh->vdev);
322 EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);
324 int v4l2_create_fwnode_links_to_pad(struct v4l2_subdev *src_sd,
325 struct media_pad *sink, u32 flags)
327 struct fwnode_handle *endpoint;
329 if (!(sink->flags & MEDIA_PAD_FL_SINK))
332 fwnode_graph_for_each_endpoint(dev_fwnode(src_sd->dev), endpoint) {
333 struct fwnode_handle *remote_ep;
334 int src_idx, sink_idx, ret;
335 struct media_pad *src;
337 src_idx = media_entity_get_fwnode_pad(&src_sd->entity,
339 MEDIA_PAD_FL_SOURCE);
343 remote_ep = fwnode_graph_get_remote_endpoint(endpoint);
348 * ask the sink to verify it owns the remote endpoint,
349 * and translate to a sink pad.
351 sink_idx = media_entity_get_fwnode_pad(sink->entity,
354 fwnode_handle_put(remote_ep);
356 if (sink_idx < 0 || sink_idx != sink->index)
360 * the source endpoint corresponds to one of its source pads,
361 * the source endpoint connects to an endpoint at the sink
362 * entity, and the sink endpoint corresponds to the sink
363 * pad requested, so we have found an endpoint connection
364 * that works, create the media link for it.
367 src = &src_sd->entity.pads[src_idx];
369 /* skip if link already exists */
370 if (media_entity_find_link(src, sink))
373 dev_dbg(src_sd->dev, "creating link %s:%d -> %s:%d\n",
374 src_sd->entity.name, src_idx,
375 sink->entity->name, sink_idx);
377 ret = media_create_pad_link(&src_sd->entity, src_idx,
378 sink->entity, sink_idx, flags);
381 "link %s:%d -> %s:%d failed with %d\n",
382 src_sd->entity.name, src_idx,
383 sink->entity->name, sink_idx, ret);
385 fwnode_handle_put(endpoint);
392 EXPORT_SYMBOL_GPL(v4l2_create_fwnode_links_to_pad);
394 int v4l2_create_fwnode_links(struct v4l2_subdev *src_sd,
395 struct v4l2_subdev *sink_sd)
399 for (i = 0; i < sink_sd->entity.num_pads; i++) {
400 struct media_pad *pad = &sink_sd->entity.pads[i];
403 if (!(pad->flags & MEDIA_PAD_FL_SINK))
406 ret = v4l2_create_fwnode_links_to_pad(src_sd, pad, 0);
413 EXPORT_SYMBOL_GPL(v4l2_create_fwnode_links);
415 /* -----------------------------------------------------------------------------
416 * Pipeline power management
418 * Entities must be powered up when part of a pipeline that contains at least
419 * one open video device node.
421 * To achieve this use the entity use_count field to track the number of users.
422 * For entities corresponding to video device nodes the use_count field stores
423 * the users count of the node. For entities corresponding to subdevs the
424 * use_count field stores the total number of users of all video device nodes
427 * The v4l2_pipeline_pm_{get, put}() functions must be called in the open() and
428 * close() handlers of video device nodes. It increments or decrements the use
429 * count of all subdev entities in the pipeline.
431 * To react to link management on powered pipelines, the link setup notification
432 * callback updates the use count of all entities in the source and sink sides
437 * pipeline_pm_use_count - Count the number of users of a pipeline
438 * @entity: The entity
440 * Return the total number of users of all video device nodes in the pipeline.
442 static int pipeline_pm_use_count(struct media_entity *entity,
443 struct media_graph *graph)
447 media_graph_walk_start(graph, entity);
449 while ((entity = media_graph_walk_next(graph))) {
450 if (is_media_entity_v4l2_video_device(entity))
451 use += entity->use_count;
458 * pipeline_pm_power_one - Apply power change to an entity
459 * @entity: The entity
460 * @change: Use count change
462 * Change the entity use count by @change. If the entity is a subdev update its
463 * power state by calling the core::s_power operation when the use count goes
464 * from 0 to != 0 or from != 0 to 0.
466 * Return 0 on success or a negative error code on failure.
468 static int pipeline_pm_power_one(struct media_entity *entity, int change)
470 struct v4l2_subdev *subdev;
473 subdev = is_media_entity_v4l2_subdev(entity)
474 ? media_entity_to_v4l2_subdev(entity) : NULL;
476 if (entity->use_count == 0 && change > 0 && subdev != NULL) {
477 ret = v4l2_subdev_call(subdev, core, s_power, 1);
478 if (ret < 0 && ret != -ENOIOCTLCMD)
482 entity->use_count += change;
483 WARN_ON(entity->use_count < 0);
485 if (entity->use_count == 0 && change < 0 && subdev != NULL)
486 v4l2_subdev_call(subdev, core, s_power, 0);
492 * pipeline_pm_power - Apply power change to all entities in a pipeline
493 * @entity: The entity
494 * @change: Use count change
496 * Walk the pipeline to update the use count and the power state of all non-node
499 * Return 0 on success or a negative error code on failure.
501 static int pipeline_pm_power(struct media_entity *entity, int change,
502 struct media_graph *graph)
504 struct media_entity *first = entity;
510 media_graph_walk_start(graph, entity);
512 while (!ret && (entity = media_graph_walk_next(graph)))
513 if (is_media_entity_v4l2_subdev(entity))
514 ret = pipeline_pm_power_one(entity, change);
519 media_graph_walk_start(graph, first);
521 while ((first = media_graph_walk_next(graph))
523 if (is_media_entity_v4l2_subdev(first))
524 pipeline_pm_power_one(first, -change);
529 static int v4l2_pipeline_pm_use(struct media_entity *entity, unsigned int use)
531 struct media_device *mdev = entity->graph_obj.mdev;
532 int change = use ? 1 : -1;
535 mutex_lock(&mdev->graph_mutex);
537 /* Apply use count to node. */
538 entity->use_count += change;
539 WARN_ON(entity->use_count < 0);
541 /* Apply power change to connected non-nodes. */
542 ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk);
544 entity->use_count -= change;
546 mutex_unlock(&mdev->graph_mutex);
551 int v4l2_pipeline_pm_get(struct media_entity *entity)
553 return v4l2_pipeline_pm_use(entity, 1);
555 EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_get);
557 void v4l2_pipeline_pm_put(struct media_entity *entity)
559 /* Powering off entities shouldn't fail. */
560 WARN_ON(v4l2_pipeline_pm_use(entity, 0));
562 EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_put);
564 int v4l2_pipeline_link_notify(struct media_link *link, u32 flags,
565 unsigned int notification)
567 struct media_graph *graph = &link->graph_obj.mdev->pm_count_walk;
568 struct media_entity *source = link->source->entity;
569 struct media_entity *sink = link->sink->entity;
574 source_use = pipeline_pm_use_count(source, graph);
575 sink_use = pipeline_pm_use_count(sink, graph);
577 if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
578 !(flags & MEDIA_LNK_FL_ENABLED)) {
579 /* Powering off entities is assumed to never fail. */
580 pipeline_pm_power(source, -sink_use, graph);
581 pipeline_pm_power(sink, -source_use, graph);
585 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
586 (flags & MEDIA_LNK_FL_ENABLED)) {
588 ret = pipeline_pm_power(source, sink_use, graph);
592 ret = pipeline_pm_power(sink, source_use, graph);
594 pipeline_pm_power(source, -sink_use, graph);
599 EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify);