xref: /linux/drivers/media/platform/renesas/vsp1/vsp1_entity.c (revision 8c13415c8a4383447c21ec832b20b3b283f0e01a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_entity.c  --  R-Car VSP1 Base Entity
4  *
5  * Copyright (C) 2013-2014 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/cleanup.h>
11 #include <linux/device.h>
12 #include <linux/gfp.h>
13 #include <linux/mutex.h>
14 
15 #include <media/media-entity.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-subdev.h>
19 
20 #include "vsp1.h"
21 #include "vsp1_dl.h"
22 #include "vsp1_entity.h"
23 #include "vsp1_pipe.h"
24 #include "vsp1_rwpf.h"
25 
26 void vsp1_entity_route_setup(struct vsp1_entity *entity,
27 			     struct vsp1_pipeline *pipe,
28 			     struct vsp1_dl_body *dlb)
29 {
30 	struct vsp1_entity *source;
31 	u32 route;
32 
33 	if (entity->type == VSP1_ENTITY_HGO) {
34 		u32 smppt;
35 
36 		/*
37 		 * The HGO is a special case, its routing is configured on the
38 		 * sink pad.
39 		 */
40 		source = entity->sources[0];
41 		smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
42 		      | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
43 
44 		vsp1_dl_body_write(dlb, VI6_DPR_HGO_SMPPT, smppt);
45 		return;
46 	} else if (entity->type == VSP1_ENTITY_HGT) {
47 		u32 smppt;
48 
49 		/*
50 		 * The HGT is a special case, its routing is configured on the
51 		 * sink pad.
52 		 */
53 		source = entity->sources[0];
54 		smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
55 		      | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
56 
57 		vsp1_dl_body_write(dlb, VI6_DPR_HGT_SMPPT, smppt);
58 		return;
59 	}
60 
61 	source = entity;
62 	if (source->route->reg == 0)
63 		return;
64 
65 	route = source->sink->route->inputs[source->sink_pad];
66 	/*
67 	 * The ILV and BRS share the same data path route. The extra BRSSEL bit
68 	 * selects between the ILV and BRS.
69 	 *
70 	 * The BRU and IIF share the same data path route. The extra IIFSEL bit
71 	 * selects between the IIF and BRU.
72 	 */
73 	if (source->type == VSP1_ENTITY_BRS)
74 		route |= VI6_DPR_ROUTE_BRSSEL;
75 	else if (source->type == VSP1_ENTITY_IIF)
76 		route |= VI6_DPR_ROUTE_IIFSEL;
77 	vsp1_dl_body_write(dlb, source->route->reg, route);
78 }
79 
80 void vsp1_entity_configure_stream(struct vsp1_entity *entity,
81 				  struct v4l2_subdev_state *state,
82 				  struct vsp1_pipeline *pipe,
83 				  struct vsp1_dl_list *dl,
84 				  struct vsp1_dl_body *dlb)
85 {
86 	if (entity->ops->configure_stream)
87 		entity->ops->configure_stream(entity, state, pipe, dl, dlb);
88 }
89 
90 void vsp1_entity_configure_frame(struct vsp1_entity *entity,
91 				 struct vsp1_pipeline *pipe,
92 				 struct vsp1_dl_list *dl,
93 				 struct vsp1_dl_body *dlb)
94 {
95 	if (entity->ops->configure_frame)
96 		entity->ops->configure_frame(entity, pipe, dl, dlb);
97 }
98 
99 void vsp1_entity_configure_partition(struct vsp1_entity *entity,
100 				     struct vsp1_pipeline *pipe,
101 				     const struct vsp1_partition *partition,
102 				     struct vsp1_dl_list *dl,
103 				     struct vsp1_dl_body *dlb)
104 {
105 	if (entity->ops->configure_partition)
106 		entity->ops->configure_partition(entity, pipe, partition,
107 						 dl, dlb);
108 }
109 
110 void vsp1_entity_adjust_color_space(struct v4l2_mbus_framefmt *format)
111 {
112 	u8 xfer_func = format->xfer_func;
113 	u8 ycbcr_enc = format->ycbcr_enc;
114 	u8 quantization = format->quantization;
115 
116 	vsp1_adjust_color_space(format->code, &format->colorspace, &xfer_func,
117 				&ycbcr_enc, &quantization);
118 
119 	format->xfer_func = xfer_func;
120 	format->ycbcr_enc = ycbcr_enc;
121 	format->quantization = quantization;
122 }
123 
124 /* -----------------------------------------------------------------------------
125  * V4L2 Subdevice Operations
126  */
127 
128 /**
129  * vsp1_entity_get_state - Get the subdev state for an entity
130  * @entity: the entity
131  * @sd_state: the TRY state
132  * @which: state selector (ACTIVE or TRY)
133  *
134  * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
135  * the entity lock to access the returned configuration.
136  *
137  * Return the subdev state requested by the which argument. The TRY state is
138  * passed explicitly to the function through the sd_state argument and simply
139  * returned when requested. The ACTIVE state comes from the entity structure.
140  */
141 struct v4l2_subdev_state *
142 vsp1_entity_get_state(struct vsp1_entity *entity,
143 		      struct v4l2_subdev_state *sd_state,
144 		      enum v4l2_subdev_format_whence which)
145 {
146 	switch (which) {
147 	case V4L2_SUBDEV_FORMAT_ACTIVE:
148 		return entity->state;
149 	case V4L2_SUBDEV_FORMAT_TRY:
150 	default:
151 		return sd_state;
152 	}
153 }
154 
155 /*
156  * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
157  * @subdev: V4L2 subdevice
158  * @sd_state: V4L2 subdev state
159  * @fmt: V4L2 subdev format
160  *
161  * This function implements the subdev get_fmt pad operation. It can be used as
162  * a direct drop-in for the operation handler.
163  */
164 int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
165 			       struct v4l2_subdev_state *sd_state,
166 			       struct v4l2_subdev_format *fmt)
167 {
168 	struct vsp1_entity *entity = to_vsp1_entity(subdev);
169 	struct v4l2_subdev_state *state;
170 
171 	state = vsp1_entity_get_state(entity, sd_state, fmt->which);
172 	if (!state)
173 		return -EINVAL;
174 
175 	guard(mutex)(&entity->lock);
176 
177 	fmt->format = *v4l2_subdev_state_get_format(state, fmt->pad);
178 
179 	return 0;
180 }
181 
182 /*
183  * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
184  * @subdev: V4L2 subdevice
185  * @sd_state: V4L2 subdev state
186  * @code: Media bus code enumeration
187  *
188  * This function implements the subdev enum_mbus_code pad operation for entities
189  * that do not support format conversion. It enumerates the given supported
190  * media bus codes on the sink pad and reports a source pad format identical to
191  * the sink pad.
192  */
193 int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
194 			       struct v4l2_subdev_state *sd_state,
195 			       struct v4l2_subdev_mbus_code_enum *code)
196 {
197 	struct vsp1_entity *entity = to_vsp1_entity(subdev);
198 
199 	if (code->pad == 0) {
200 		if (code->index >= entity->num_codes)
201 			return -EINVAL;
202 
203 		code->code = entity->codes[code->index];
204 	} else {
205 		struct v4l2_subdev_state *state;
206 		struct v4l2_mbus_framefmt *format;
207 
208 		/*
209 		 * The entity can't perform format conversion, the sink format
210 		 * is always identical to the source format.
211 		 */
212 		if (code->index)
213 			return -EINVAL;
214 
215 		state = vsp1_entity_get_state(entity, sd_state, code->which);
216 		if (!state)
217 			return -EINVAL;
218 
219 		scoped_guard(mutex, &entity->lock) {
220 			format = v4l2_subdev_state_get_format(state, 0);
221 			code->code = format->code;
222 		}
223 	}
224 
225 	return 0;
226 }
227 
228 /*
229  * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
230  * @subdev: V4L2 subdevice
231  * @sd_state: V4L2 subdev state
232  * @fse: Frame size enumeration
233  *
234  * This function implements the subdev enum_frame_size pad operation for
235  * entities that do not support scaling or cropping. It reports the given
236  * minimum and maximum frame width and height on the sink pad, and a fixed
237  * source pad size identical to the sink pad.
238  */
239 int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
240 				struct v4l2_subdev_state *sd_state,
241 				struct v4l2_subdev_frame_size_enum *fse)
242 {
243 	struct vsp1_entity *entity = to_vsp1_entity(subdev);
244 
245 	if (fse->index)
246 		return -EINVAL;
247 
248 	if (fse->pad == 0) {
249 		unsigned int i;
250 
251 		for (i = 0; i < entity->num_codes; ++i) {
252 			if (fse->code == entity->codes[i])
253 				break;
254 		}
255 
256 		if (i == entity->num_codes)
257 			return -EINVAL;
258 
259 		fse->min_width = entity->min_width;
260 		fse->max_width = entity->max_width;
261 		fse->min_height = entity->min_height;
262 		fse->max_height = entity->max_height;
263 	} else {
264 		struct v4l2_subdev_state *state;
265 		struct v4l2_mbus_framefmt *format;
266 
267 		state = vsp1_entity_get_state(entity, sd_state, fse->which);
268 		if (!state)
269 			return -EINVAL;
270 
271 		/*
272 		 * The media bus code and size on the source pad are fixed and
273 		 * always identical to the sink pad.
274 		 */
275 		format = v4l2_subdev_state_get_format(state, 0);
276 
277 		guard(mutex)(&entity->lock);
278 
279 		if (fse->code != format->code)
280 			return -EINVAL;
281 
282 		fse->min_width = format->width;
283 		fse->max_width = format->width;
284 		fse->min_height = format->height;
285 		fse->max_height = format->height;
286 	}
287 
288 	return 0;
289 }
290 
291 /*
292  * vsp1_subdev_set_pad_format - Subdev pad set_fmt handler
293  * @subdev: V4L2 subdevice
294  * @sd_state: V4L2 subdev state
295  * @fmt: V4L2 subdev format
296  *
297  * This function implements the subdev set_fmt pad operation for entities that
298  * do not support scaling or cropping. It defaults to the first supported media
299  * bus code if the requested code isn't supported, clamps the size to the
300  * entity's limits, and propagates the sink pad format to the source pad.
301  */
302 int vsp1_subdev_set_pad_format(struct v4l2_subdev *subdev,
303 			       struct v4l2_subdev_state *sd_state,
304 			       struct v4l2_subdev_format *fmt)
305 {
306 	struct vsp1_entity *entity = to_vsp1_entity(subdev);
307 	struct v4l2_subdev_state *state;
308 	struct v4l2_mbus_framefmt *format;
309 	struct v4l2_rect *selection;
310 	unsigned int i;
311 
312 	guard(mutex)(&entity->lock);
313 
314 	state = vsp1_entity_get_state(entity, sd_state, fmt->which);
315 	if (!state)
316 		return -EINVAL;
317 
318 	format = v4l2_subdev_state_get_format(state, fmt->pad);
319 
320 	if (fmt->pad == entity->source_pad) {
321 		/* The output format can't be modified. */
322 		fmt->format = *format;
323 		return 0;
324 	}
325 
326 	/*
327 	 * Default to the first media bus code if the requested format is not
328 	 * supported.
329 	 */
330 	for (i = 0; i < entity->num_codes; ++i) {
331 		if (fmt->format.code == entity->codes[i])
332 			break;
333 	}
334 
335 	format->code = i < entity->num_codes
336 		     ? entity->codes[i] : entity->codes[0];
337 	format->width = clamp_t(unsigned int, fmt->format.width,
338 				entity->min_width, entity->max_width);
339 	format->height = clamp_t(unsigned int, fmt->format.height,
340 				 entity->min_height, entity->max_height);
341 	format->field = V4L2_FIELD_NONE;
342 
343 	format->colorspace = fmt->format.colorspace;
344 	format->xfer_func = fmt->format.xfer_func;
345 	format->ycbcr_enc = fmt->format.ycbcr_enc;
346 	format->quantization = fmt->format.quantization;
347 
348 	vsp1_entity_adjust_color_space(format);
349 
350 	fmt->format = *format;
351 
352 	/* Propagate the format to the source pad. */
353 	format = v4l2_subdev_state_get_format(state, entity->source_pad);
354 	*format = fmt->format;
355 
356 	/* Reset the crop and compose rectangles. */
357 	selection = v4l2_subdev_state_get_crop(state, fmt->pad);
358 	selection->left = 0;
359 	selection->top = 0;
360 	selection->width = format->width;
361 	selection->height = format->height;
362 
363 	selection = v4l2_subdev_state_get_compose(state, fmt->pad);
364 	selection->left = 0;
365 	selection->top = 0;
366 	selection->width = format->width;
367 	selection->height = format->height;
368 
369 	return 0;
370 }
371 
372 static int vsp1_entity_init_state(struct v4l2_subdev *subdev,
373 				  struct v4l2_subdev_state *sd_state)
374 {
375 	unsigned int pad;
376 
377 	/* Initialize all pad formats with default values. */
378 	for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
379 		struct v4l2_subdev_format format = {
380 			.pad = pad,
381 			.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
382 			       : V4L2_SUBDEV_FORMAT_ACTIVE,
383 		};
384 
385 		v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &format);
386 	}
387 
388 	return 0;
389 }
390 
391 static const struct v4l2_subdev_internal_ops vsp1_entity_internal_ops = {
392 	.init_state = vsp1_entity_init_state,
393 };
394 
395 const struct v4l2_subdev_core_ops vsp1_entity_core_ops = {
396 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
397 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
398 };
399 
400 /* -----------------------------------------------------------------------------
401  * Media Operations
402  */
403 
404 static inline struct vsp1_entity *
405 media_entity_to_vsp1_entity(struct media_entity *entity)
406 {
407 	return container_of(entity, struct vsp1_entity, subdev.entity);
408 }
409 
410 static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
411 					 const struct media_pad *sink_pad,
412 					 u32 flags)
413 {
414 	struct vsp1_entity *source;
415 
416 	source = media_entity_to_vsp1_entity(source_pad->entity);
417 
418 	if (!source->route)
419 		return 0;
420 
421 	if (flags & MEDIA_LNK_FL_ENABLED) {
422 		struct vsp1_entity *sink
423 			= media_entity_to_vsp1_entity(sink_pad->entity);
424 
425 		/*
426 		 * Fan-out is limited to one for the normal data path plus
427 		 * optional HGO and HGT. We ignore the HGO and HGT here.
428 		 */
429 		if (sink->type != VSP1_ENTITY_HGO &&
430 		    sink->type != VSP1_ENTITY_HGT) {
431 			if (source->sink)
432 				return -EBUSY;
433 			source->sink = sink;
434 			source->sink_pad = sink_pad->index;
435 		}
436 	} else {
437 		source->sink = NULL;
438 		source->sink_pad = 0;
439 	}
440 
441 	return 0;
442 }
443 
444 static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
445 				       const struct media_pad *sink_pad,
446 				       u32 flags)
447 {
448 	struct vsp1_entity *sink;
449 	struct vsp1_entity *source;
450 
451 	sink = media_entity_to_vsp1_entity(sink_pad->entity);
452 	source = media_entity_to_vsp1_entity(source_pad->entity);
453 
454 	if (flags & MEDIA_LNK_FL_ENABLED) {
455 		/* Fan-in is limited to one. */
456 		if (sink->sources[sink_pad->index])
457 			return -EBUSY;
458 
459 		sink->sources[sink_pad->index] = source;
460 	} else {
461 		sink->sources[sink_pad->index] = NULL;
462 	}
463 
464 	return 0;
465 }
466 
467 int vsp1_entity_link_setup(struct media_entity *entity,
468 			   const struct media_pad *local,
469 			   const struct media_pad *remote, u32 flags)
470 {
471 	if (local->flags & MEDIA_PAD_FL_SOURCE)
472 		return vsp1_entity_link_setup_source(local, remote, flags);
473 	else
474 		return vsp1_entity_link_setup_sink(remote, local, flags);
475 }
476 
477 /**
478  * vsp1_entity_remote_pad - Find the pad at the remote end of a link
479  * @pad: Pad at the local end of the link
480  *
481  * Search for a remote pad connected to the given pad by iterating over all
482  * links originating or terminating at that pad until an enabled link is found.
483  *
484  * Our link setup implementation guarantees that the output fan-out will not be
485  * higher than one for the data pipelines, except for the links to the HGO and
486  * HGT that can be enabled in addition to a regular data link. When traversing
487  * outgoing links this function ignores HGO and HGT entities and should thus be
488  * used in place of the generic media_pad_remote_pad_first() function to
489  * traverse data pipelines.
490  *
491  * Return a pointer to the pad at the remote end of the first found enabled
492  * link, or NULL if no enabled link has been found.
493  */
494 struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
495 {
496 	struct media_link *link;
497 
498 	list_for_each_entry(link, &pad->entity->links, list) {
499 		struct vsp1_entity *entity;
500 
501 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
502 			continue;
503 
504 		/* If we're the sink the source will never be an HGO or HGT. */
505 		if (link->sink == pad)
506 			return link->source;
507 
508 		if (link->source != pad)
509 			continue;
510 
511 		/* If the sink isn't a subdevice it can't be an HGO or HGT. */
512 		if (!is_media_entity_v4l2_subdev(link->sink->entity))
513 			return link->sink;
514 
515 		entity = media_entity_to_vsp1_entity(link->sink->entity);
516 		if (entity->type != VSP1_ENTITY_HGO &&
517 		    entity->type != VSP1_ENTITY_HGT)
518 			return link->sink;
519 	}
520 
521 	return NULL;
522 
523 }
524 
525 /* -----------------------------------------------------------------------------
526  * Initialization
527  */
528 
529 #define VSP1_ENTITY_ROUTE(ent)						\
530 	{ VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE,			\
531 	  { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
532 
533 #define VSP1_ENTITY_ROUTE_RPF(idx)					\
534 	{ VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx),			\
535 	  { 0, }, VI6_DPR_NODE_RPF(idx) }
536 
537 #define VSP1_ENTITY_ROUTE_UDS(idx)					\
538 	{ VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx),			\
539 	  { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
540 
541 #define VSP1_ENTITY_ROUTE_UIF(idx)					\
542 	{ VSP1_ENTITY_UIF, idx, VI6_DPR_UIF_ROUTE(idx),			\
543 	  { VI6_DPR_NODE_UIF(idx) }, VI6_DPR_NODE_UIF(idx) }
544 
545 #define VSP1_ENTITY_ROUTE_WPF(idx)					\
546 	{ VSP1_ENTITY_WPF, idx, 0,					\
547 	  { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
548 
549 static const struct vsp1_route vsp1_routes[] = {
550 	{ VSP1_ENTITY_IIF, 0, VI6_DPR_BRU_ROUTE,
551 	  { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
552 	    VI6_DPR_NODE_BRU_IN(3) }, VI6_DPR_NODE_WPF(0) },
553 	{ VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE,
554 	  { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 },
555 	{ VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
556 	  { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
557 	    VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
558 	    VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
559 	VSP1_ENTITY_ROUTE(CLU),
560 	{ VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
561 	{ VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
562 	VSP1_ENTITY_ROUTE(HSI),
563 	VSP1_ENTITY_ROUTE(HST),
564 	{ VSP1_ENTITY_LIF, 0, 0, { 0, }, 0 },
565 	{ VSP1_ENTITY_LIF, 1, 0, { 0, }, 0 },
566 	VSP1_ENTITY_ROUTE(LUT),
567 	VSP1_ENTITY_ROUTE_RPF(0),
568 	VSP1_ENTITY_ROUTE_RPF(1),
569 	VSP1_ENTITY_ROUTE_RPF(2),
570 	VSP1_ENTITY_ROUTE_RPF(3),
571 	VSP1_ENTITY_ROUTE_RPF(4),
572 	VSP1_ENTITY_ROUTE(SRU),
573 	VSP1_ENTITY_ROUTE_UDS(0),
574 	VSP1_ENTITY_ROUTE_UDS(1),
575 	VSP1_ENTITY_ROUTE_UDS(2),
576 	VSP1_ENTITY_ROUTE_UIF(0),	/* Named UIF4 in the documentation */
577 	VSP1_ENTITY_ROUTE_UIF(1),	/* Named UIF5 in the documentation */
578 	VSP1_ENTITY_ROUTE_WPF(0),
579 	VSP1_ENTITY_ROUTE_WPF(1),
580 	VSP1_ENTITY_ROUTE_WPF(2),
581 	VSP1_ENTITY_ROUTE_WPF(3),
582 };
583 
584 int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
585 		     const char *name, unsigned int num_pads,
586 		     const struct v4l2_subdev_ops *ops, u32 function)
587 {
588 	static struct lock_class_key key;
589 	struct v4l2_subdev *subdev;
590 	unsigned int i;
591 	int ret;
592 
593 	for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
594 		if (vsp1_routes[i].type == entity->type &&
595 		    vsp1_routes[i].index == entity->index) {
596 			entity->route = &vsp1_routes[i];
597 			break;
598 		}
599 	}
600 
601 	if (i == ARRAY_SIZE(vsp1_routes))
602 		return -EINVAL;
603 
604 	mutex_init(&entity->lock);
605 
606 	entity->vsp1 = vsp1;
607 	entity->source_pad = num_pads - 1;
608 
609 	/* Allocate and initialize pads. */
610 	entity->pads = devm_kcalloc(vsp1->dev,
611 				    num_pads, sizeof(*entity->pads),
612 				    GFP_KERNEL);
613 	if (entity->pads == NULL)
614 		return -ENOMEM;
615 
616 	for (i = 0; i < num_pads - 1; ++i)
617 		entity->pads[i].flags = MEDIA_PAD_FL_SINK;
618 
619 	entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
620 				       sizeof(*entity->sources), GFP_KERNEL);
621 	if (entity->sources == NULL)
622 		return -ENOMEM;
623 
624 	/* Single-pad entities only have a sink. */
625 	entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
626 					 : MEDIA_PAD_FL_SINK;
627 
628 	/* Initialize the media entity. */
629 	ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
630 				     entity->pads);
631 	if (ret < 0)
632 		return ret;
633 
634 	/* Initialize the V4L2 subdev. */
635 	subdev = &entity->subdev;
636 	v4l2_subdev_init(subdev, ops);
637 	subdev->internal_ops = &vsp1_entity_internal_ops;
638 
639 	subdev->entity.function = function;
640 	subdev->entity.ops = &vsp1->media_ops;
641 	subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
642 
643 	if (ops->core == &vsp1_entity_core_ops)
644 		subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
645 
646 	snprintf(subdev->name, sizeof(subdev->name), "%s %s",
647 		 dev_name(vsp1->dev), name);
648 
649 	vsp1_entity_init_state(subdev, NULL);
650 
651 	/*
652 	 * Allocate the subdev state to store formats and selection
653 	 * rectangles.
654 	 */
655 	/*
656 	 * FIXME: Drop this call, drivers are not supposed to use
657 	 * __v4l2_subdev_state_alloc().
658 	 */
659 	entity->state = __v4l2_subdev_state_alloc(&entity->subdev,
660 						  "vsp1:state->lock", &key);
661 	if (IS_ERR(entity->state)) {
662 		media_entity_cleanup(&entity->subdev.entity);
663 		return PTR_ERR(entity->state);
664 	}
665 
666 	return 0;
667 }
668 
669 void vsp1_entity_destroy(struct vsp1_entity *entity)
670 {
671 	if (entity->ops && entity->ops->destroy)
672 		entity->ops->destroy(entity);
673 	if (entity->subdev.ctrl_handler)
674 		v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
675 	__v4l2_subdev_state_free(entity->state);
676 	media_entity_cleanup(&entity->subdev.entity);
677 }
678