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