blob: 014a2a97cadd87065247e0eeb678d086b3270104 [file] [log] [blame]
Shuah Khanf61c7bd2018-01-11 17:26:22 -05001// SPDX-License-Identifier: GPL-2.0-or-later
2
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -02003/*
4 * Media Controller ancillary functions
5 *
Mauro Carvalho Chehab5dc8a862016-06-14 15:17:40 -03006 * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@kernel.org>
Shuah Khand0a164f2016-02-11 21:41:25 -02007 * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com>
Sakari Ailus76413792016-02-21 13:25:09 -03008 * Copyright (C) 2006-2010 Nokia Corporation
9 * Copyright (c) 2016 Intel Corporation.
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020010 */
11
12#include <linux/module.h>
Sakari Ailus76413792016-02-21 13:25:09 -030013#include <linux/pci.h>
14#include <linux/usb.h>
Shuah Khand0a164f2016-02-11 21:41:25 -020015#include <media/media-device.h>
Sakari Ailus76413792016-02-21 13:25:09 -030016#include <media/media-entity.h>
Shuah Khand0a164f2016-02-11 21:41:25 -020017#include <media/v4l2-fh.h>
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020018#include <media/v4l2-mc.h>
Sakari Ailus76413792016-02-21 13:25:09 -030019#include <media/v4l2-subdev.h>
Shuah Khand0a164f2016-02-11 21:41:25 -020020#include <media/videobuf2-core.h>
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020021
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020022int v4l2_mc_create_media_graph(struct media_device *mdev)
23
24{
25 struct media_entity *entity;
Mauro Carvalho Chehab153d41a2016-02-12 08:07:59 -020026 struct media_entity *if_vid = NULL, *if_aud = NULL;
Linus Torvalds17084b72016-04-03 07:03:49 -050027 struct media_entity *tuner = NULL, *decoder = NULL;
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020028 struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL;
29 bool is_webcam = false;
30 u32 flags;
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -040031 int ret, pad_sink, pad_source;
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020032
33 if (!mdev)
34 return 0;
35
36 media_device_for_each_entity(entity, mdev) {
37 switch (entity->function) {
38 case MEDIA_ENT_F_IF_VID_DECODER:
39 if_vid = entity;
40 break;
41 case MEDIA_ENT_F_IF_AUD_DECODER:
42 if_aud = entity;
43 break;
44 case MEDIA_ENT_F_TUNER:
45 tuner = entity;
46 break;
47 case MEDIA_ENT_F_ATV_DECODER:
48 decoder = entity;
49 break;
50 case MEDIA_ENT_F_IO_V4L:
51 io_v4l = entity;
52 break;
53 case MEDIA_ENT_F_IO_VBI:
54 io_vbi = entity;
55 break;
56 case MEDIA_ENT_F_IO_SWRADIO:
57 io_swradio = entity;
58 break;
59 case MEDIA_ENT_F_CAM_SENSOR:
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020060 is_webcam = true;
61 break;
62 }
63 }
64
65 /* It should have at least one I/O entity */
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -040066 if (!io_v4l && !io_vbi && !io_swradio) {
67 dev_warn(mdev->dev, "Didn't find any I/O entity\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020068 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -040069 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020070
71 /*
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.
77 */
78 if (is_webcam) {
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -040079 if (!io_v4l) {
80 dev_warn(mdev->dev, "Didn't find a MEDIA_ENT_F_IO_V4L\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020081 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -040082 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020083
84 media_device_for_each_entity(entity, mdev) {
85 if (entity->function != MEDIA_ENT_F_CAM_SENSOR)
86 continue;
87 ret = media_create_pad_link(entity, 0,
88 io_v4l, 0,
89 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -040090 if (ret) {
91 dev_warn(mdev->dev, "Failed to create a sensor link\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020092 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -040093 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -020094 }
95 if (!decoder)
96 return 0;
97 }
98
99 /* The device isn't a webcam. So, it should have a decoder */
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400100 if (!decoder) {
101 dev_warn(mdev->dev, "Decoder not found\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200102 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400103 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200104
105 /* Link the tuner and IF video output pads */
106 if (tuner) {
107 if (if_vid) {
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400108 pad_source = media_get_pad_index(tuner, false,
109 PAD_SIGNAL_ANALOG);
110 pad_sink = media_get_pad_index(if_vid, true,
111 PAD_SIGNAL_ANALOG);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400112 if (pad_source < 0 || pad_sink < 0) {
113 dev_warn(mdev->dev, "Couldn't get tuner and/or PLL pad(s): (%d, %d)\n",
114 pad_source, pad_sink);
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400115 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400116 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400117 ret = media_create_pad_link(tuner, pad_source,
118 if_vid, pad_sink,
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200119 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400120 if (ret) {
121 dev_warn(mdev->dev, "Couldn't create tuner->PLL link)\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200122 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400123 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400124
125 pad_source = media_get_pad_index(if_vid, false,
126 PAD_SIGNAL_ANALOG);
127 pad_sink = media_get_pad_index(decoder, true,
128 PAD_SIGNAL_ANALOG);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400129 if (pad_source < 0 || pad_sink < 0) {
130 dev_warn(mdev->dev, "get decoder and/or PLL pad(s): (%d, %d)\n",
131 pad_source, pad_sink);
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400132 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400133 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400134 ret = media_create_pad_link(if_vid, pad_source,
135 decoder, pad_sink,
136 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400137 if (ret) {
138 dev_warn(mdev->dev, "couldn't link PLL to decoder\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200139 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400140 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200141 } else {
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400142 pad_source = media_get_pad_index(tuner, false,
143 PAD_SIGNAL_ANALOG);
144 pad_sink = media_get_pad_index(decoder, true,
145 PAD_SIGNAL_ANALOG);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400146 if (pad_source < 0 || pad_sink < 0) {
147 dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s): (%d, %d)\n",
148 pad_source, pad_sink);
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400149 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400150 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400151 ret = media_create_pad_link(tuner, pad_source,
152 decoder, pad_sink,
153 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200154 if (ret)
155 return ret;
156 }
157
158 if (if_aud) {
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400159 pad_source = media_get_pad_index(tuner, false,
160 PAD_SIGNAL_AUDIO);
161 pad_sink = media_get_pad_index(if_aud, true,
162 PAD_SIGNAL_AUDIO);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400163 if (pad_source < 0 || pad_sink < 0) {
164 dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s) for audio: (%d, %d)\n",
165 pad_source, pad_sink);
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400166 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400167 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400168 ret = media_create_pad_link(tuner, pad_source,
169 if_aud, pad_sink,
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200170 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400171 if (ret) {
172 dev_warn(mdev->dev, "couldn't link tuner->audio PLL\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200173 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400174 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200175 } else {
176 if_aud = tuner;
177 }
178
179 }
180
181 /* Create demod to V4L, VBI and SDR radio links */
182 if (io_v4l) {
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400183 pad_source = media_get_pad_index(decoder, false, PAD_SIGNAL_DV);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400184 if (pad_source < 0) {
185 dev_warn(mdev->dev, "couldn't get decoder output pad for V4L I/O\n");
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400186 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400187 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400188 ret = media_create_pad_link(decoder, pad_source,
189 io_v4l, 0,
190 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400191 if (ret) {
192 dev_warn(mdev->dev, "couldn't link decoder output to V4L I/O\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200193 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400194 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200195 }
196
197 if (io_swradio) {
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400198 pad_source = media_get_pad_index(decoder, false, PAD_SIGNAL_DV);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400199 if (pad_source < 0) {
200 dev_warn(mdev->dev, "couldn't get decoder output pad for SDR\n");
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400201 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400202 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400203 ret = media_create_pad_link(decoder, pad_source,
204 io_swradio, 0,
205 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400206 if (ret) {
207 dev_warn(mdev->dev, "couldn't link decoder output to SDR\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200208 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400209 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200210 }
211
212 if (io_vbi) {
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400213 pad_source = media_get_pad_index(decoder, false, PAD_SIGNAL_DV);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400214 if (pad_source < 0) {
215 dev_warn(mdev->dev, "couldn't get decoder output pad for VBI\n");
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400216 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400217 }
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400218 ret = media_create_pad_link(decoder, pad_source,
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200219 io_vbi, 0,
220 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400221 if (ret) {
222 dev_warn(mdev->dev, "couldn't link decoder output to VBI\n");
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200223 return ret;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400224 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200225 }
226
227 /* Create links for the media connectors */
228 flags = MEDIA_LNK_FL_ENABLED;
229 media_device_for_each_entity(entity, mdev) {
230 switch (entity->function) {
231 case MEDIA_ENT_F_CONN_RF:
232 if (!tuner)
233 continue;
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400234 pad_sink = media_get_pad_index(tuner, true,
235 PAD_SIGNAL_ANALOG);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400236 if (pad_sink < 0) {
237 dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400238 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400239 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200240 ret = media_create_pad_link(entity, 0, tuner,
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400241 pad_sink,
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200242 flags);
243 break;
244 case MEDIA_ENT_F_CONN_SVIDEO:
245 case MEDIA_ENT_F_CONN_COMPOSITE:
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400246 pad_sink = media_get_pad_index(decoder, true,
247 PAD_SIGNAL_ANALOG);
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400248 if (pad_sink < 0) {
249 dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400250 return -EINVAL;
Mauro Carvalho Chehabcaf276b2018-09-15 00:00:31 -0400251 }
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200252 ret = media_create_pad_link(entity, 0, decoder,
Mauro Carvalho Chehab9d6d20e2018-07-31 09:22:40 -0400253 pad_sink,
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200254 flags);
255 break;
256 default:
257 continue;
258 }
259 if (ret)
260 return ret;
261
262 flags = 0;
263 }
Mauro Carvalho Chehab9822f412016-03-02 10:11:41 -0300264
Mauro Carvalho Chehab54d0dba2016-02-05 07:02:43 -0200265 return 0;
266}
267EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
Shuah Khand0a164f2016-02-11 21:41:25 -0200268
269int v4l_enable_media_source(struct video_device *vdev)
270{
271 struct media_device *mdev = vdev->entity.graph_obj.mdev;
Shuah Khan90cd3662016-11-29 21:59:54 -0200272 int ret = 0, err;
Shuah Khand0a164f2016-02-11 21:41:25 -0200273
Shuah Khan90cd3662016-11-29 21:59:54 -0200274 if (!mdev)
Shuah Khand0a164f2016-02-11 21:41:25 -0200275 return 0;
Shuah Khan90cd3662016-11-29 21:59:54 -0200276
277 mutex_lock(&mdev->graph_mutex);
278 if (!mdev->enable_source)
279 goto end;
280 err = mdev->enable_source(&vdev->entity, &vdev->pipe);
281 if (err)
282 ret = -EBUSY;
283end:
284 mutex_unlock(&mdev->graph_mutex);
285 return ret;
Shuah Khand0a164f2016-02-11 21:41:25 -0200286}
287EXPORT_SYMBOL_GPL(v4l_enable_media_source);
288
289void v4l_disable_media_source(struct video_device *vdev)
290{
291 struct media_device *mdev = vdev->entity.graph_obj.mdev;
292
Shuah Khan90cd3662016-11-29 21:59:54 -0200293 if (mdev) {
294 mutex_lock(&mdev->graph_mutex);
295 if (mdev->disable_source)
296 mdev->disable_source(&vdev->entity);
297 mutex_unlock(&mdev->graph_mutex);
298 }
Shuah Khand0a164f2016-02-11 21:41:25 -0200299}
300EXPORT_SYMBOL_GPL(v4l_disable_media_source);
301
302int v4l_vb2q_enable_media_source(struct vb2_queue *q)
303{
304 struct v4l2_fh *fh = q->owner;
305
Shuah Khan7e538982016-03-03 23:24:58 -0300306 if (fh && fh->vdev)
307 return v4l_enable_media_source(fh->vdev);
308 return 0;
Shuah Khand0a164f2016-02-11 21:41:25 -0200309}
310EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);
Sakari Ailus76413792016-02-21 13:25:09 -0300311
312/* -----------------------------------------------------------------------------
313 * Pipeline power management
314 *
315 * Entities must be powered up when part of a pipeline that contains at least
316 * one open video device node.
317 *
318 * To achieve this use the entity use_count field to track the number of users.
319 * For entities corresponding to video device nodes the use_count field stores
320 * the users count of the node. For entities corresponding to subdevs the
321 * use_count field stores the total number of users of all video device nodes
322 * in the pipeline.
323 *
324 * The v4l2_pipeline_pm_use() function must be called in the open() and
325 * close() handlers of video device nodes. It increments or decrements the use
326 * count of all subdev entities in the pipeline.
327 *
328 * To react to link management on powered pipelines, the link setup notification
329 * callback updates the use count of all entities in the source and sink sides
330 * of the link.
331 */
332
333/*
334 * pipeline_pm_use_count - Count the number of users of a pipeline
335 * @entity: The entity
336 *
337 * Return the total number of users of all video device nodes in the pipeline.
338 */
339static int pipeline_pm_use_count(struct media_entity *entity,
Sakari Ailus20b85222016-11-21 14:48:30 -0200340 struct media_graph *graph)
Sakari Ailus76413792016-02-21 13:25:09 -0300341{
342 int use = 0;
343
Sakari Ailus20b85222016-11-21 14:48:30 -0200344 media_graph_walk_start(graph, entity);
Sakari Ailus76413792016-02-21 13:25:09 -0300345
Sakari Ailus20b85222016-11-21 14:48:30 -0200346 while ((entity = media_graph_walk_next(graph))) {
Laurent Pinchart45b46872016-02-29 08:45:45 -0300347 if (is_media_entity_v4l2_video_device(entity))
Sakari Ailus76413792016-02-21 13:25:09 -0300348 use += entity->use_count;
349 }
350
351 return use;
352}
353
354/*
355 * pipeline_pm_power_one - Apply power change to an entity
356 * @entity: The entity
357 * @change: Use count change
358 *
359 * Change the entity use count by @change. If the entity is a subdev update its
360 * power state by calling the core::s_power operation when the use count goes
361 * from 0 to != 0 or from != 0 to 0.
362 *
363 * Return 0 on success or a negative error code on failure.
364 */
365static int pipeline_pm_power_one(struct media_entity *entity, int change)
366{
367 struct v4l2_subdev *subdev;
368 int ret;
369
370 subdev = is_media_entity_v4l2_subdev(entity)
371 ? media_entity_to_v4l2_subdev(entity) : NULL;
372
373 if (entity->use_count == 0 && change > 0 && subdev != NULL) {
374 ret = v4l2_subdev_call(subdev, core, s_power, 1);
375 if (ret < 0 && ret != -ENOIOCTLCMD)
376 return ret;
377 }
378
379 entity->use_count += change;
380 WARN_ON(entity->use_count < 0);
381
382 if (entity->use_count == 0 && change < 0 && subdev != NULL)
383 v4l2_subdev_call(subdev, core, s_power, 0);
384
385 return 0;
386}
387
388/*
389 * pipeline_pm_power - Apply power change to all entities in a pipeline
390 * @entity: The entity
391 * @change: Use count change
392 *
393 * Walk the pipeline to update the use count and the power state of all non-node
394 * entities.
395 *
396 * Return 0 on success or a negative error code on failure.
397 */
398static int pipeline_pm_power(struct media_entity *entity, int change,
Sakari Ailus20b85222016-11-21 14:48:30 -0200399 struct media_graph *graph)
Sakari Ailus76413792016-02-21 13:25:09 -0300400{
401 struct media_entity *first = entity;
402 int ret = 0;
403
404 if (!change)
405 return 0;
406
Sakari Ailus20b85222016-11-21 14:48:30 -0200407 media_graph_walk_start(graph, entity);
Sakari Ailus76413792016-02-21 13:25:09 -0300408
Sakari Ailus20b85222016-11-21 14:48:30 -0200409 while (!ret && (entity = media_graph_walk_next(graph)))
Sakari Ailus76413792016-02-21 13:25:09 -0300410 if (is_media_entity_v4l2_subdev(entity))
411 ret = pipeline_pm_power_one(entity, change);
412
413 if (!ret)
414 return ret;
415
Sakari Ailus20b85222016-11-21 14:48:30 -0200416 media_graph_walk_start(graph, first);
Sakari Ailus76413792016-02-21 13:25:09 -0300417
Sakari Ailus20b85222016-11-21 14:48:30 -0200418 while ((first = media_graph_walk_next(graph))
Sakari Ailus76413792016-02-21 13:25:09 -0300419 && first != entity)
420 if (is_media_entity_v4l2_subdev(first))
421 pipeline_pm_power_one(first, -change);
422
423 return ret;
424}
425
426int v4l2_pipeline_pm_use(struct media_entity *entity, int use)
427{
428 struct media_device *mdev = entity->graph_obj.mdev;
429 int change = use ? 1 : -1;
430 int ret;
431
432 mutex_lock(&mdev->graph_mutex);
433
434 /* Apply use count to node. */
435 entity->use_count += change;
436 WARN_ON(entity->use_count < 0);
437
438 /* Apply power change to connected non-nodes. */
439 ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk);
440 if (ret < 0)
441 entity->use_count -= change;
442
443 mutex_unlock(&mdev->graph_mutex);
444
445 return ret;
446}
447EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_use);
448
449int v4l2_pipeline_link_notify(struct media_link *link, u32 flags,
450 unsigned int notification)
451{
Sakari Ailus20b85222016-11-21 14:48:30 -0200452 struct media_graph *graph = &link->graph_obj.mdev->pm_count_walk;
Sakari Ailus76413792016-02-21 13:25:09 -0300453 struct media_entity *source = link->source->entity;
454 struct media_entity *sink = link->sink->entity;
455 int source_use;
456 int sink_use;
457 int ret = 0;
458
459 source_use = pipeline_pm_use_count(source, graph);
460 sink_use = pipeline_pm_use_count(sink, graph);
461
462 if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
463 !(flags & MEDIA_LNK_FL_ENABLED)) {
464 /* Powering off entities is assumed to never fail. */
465 pipeline_pm_power(source, -sink_use, graph);
466 pipeline_pm_power(sink, -source_use, graph);
467 return 0;
468 }
469
470 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
471 (flags & MEDIA_LNK_FL_ENABLED)) {
472
473 ret = pipeline_pm_power(source, sink_use, graph);
474 if (ret < 0)
475 return ret;
476
477 ret = pipeline_pm_power(sink, source_use, graph);
478 if (ret < 0)
479 pipeline_pm_power(source, -sink_use, graph);
480 }
481
482 return ret;
483}
484EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify);