xref: /linux/drivers/media/platform/renesas/vsp1/vsp1_entity.c (revision b734412619821f3ed63ba63533f539672cb7a76d)
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 
vsp1_entity_route_setup(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_body * dlb)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 
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)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 
vsp1_entity_configure_frame(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)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 
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)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 
vsp1_entity_adjust_color_space(struct v4l2_mbus_framefmt * format)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 *
vsp1_entity_get_state(struct vsp1_entity * entity,struct v4l2_subdev_state * sd_state,enum v4l2_subdev_format_whence which)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  */
vsp1_subdev_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)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 	mutex_lock(&entity->lock);
176 	fmt->format = *v4l2_subdev_state_get_format(state, fmt->pad);
177 	mutex_unlock(&entity->lock);
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  */
vsp1_subdev_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)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 		mutex_lock(&entity->lock);
220 		format = v4l2_subdev_state_get_format(state, 0);
221 		code->code = format->code;
222 		mutex_unlock(&entity->lock);
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  */
vsp1_subdev_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)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  */
vsp1_subdev_set_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)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 	int ret = 0;
312 
313 	mutex_lock(&entity->lock);
314 
315 	state = vsp1_entity_get_state(entity, sd_state, fmt->which);
316 	if (!state) {
317 		ret = -EINVAL;
318 		goto done;
319 	}
320 
321 	format = v4l2_subdev_state_get_format(state, fmt->pad);
322 
323 	if (fmt->pad == entity->source_pad) {
324 		/* The output format can't be modified. */
325 		fmt->format = *format;
326 		goto done;
327 	}
328 
329 	/*
330 	 * Default to the first media bus code if the requested format is not
331 	 * supported.
332 	 */
333 	for (i = 0; i < entity->num_codes; ++i) {
334 		if (fmt->format.code == entity->codes[i])
335 			break;
336 	}
337 
338 	format->code = i < entity->num_codes
339 		     ? entity->codes[i] : entity->codes[0];
340 	format->width = clamp_t(unsigned int, fmt->format.width,
341 				entity->min_width, entity->max_width);
342 	format->height = clamp_t(unsigned int, fmt->format.height,
343 				 entity->min_height, entity->max_height);
344 	format->field = V4L2_FIELD_NONE;
345 
346 	format->colorspace = fmt->format.colorspace;
347 	format->xfer_func = fmt->format.xfer_func;
348 	format->ycbcr_enc = fmt->format.ycbcr_enc;
349 	format->quantization = fmt->format.quantization;
350 
351 	vsp1_entity_adjust_color_space(format);
352 
353 	fmt->format = *format;
354 
355 	/* Propagate the format to the source pad. */
356 	format = v4l2_subdev_state_get_format(state, entity->source_pad);
357 	*format = fmt->format;
358 
359 	/* Reset the crop and compose rectangles. */
360 	selection = v4l2_subdev_state_get_crop(state, fmt->pad);
361 	selection->left = 0;
362 	selection->top = 0;
363 	selection->width = format->width;
364 	selection->height = format->height;
365 
366 	selection = v4l2_subdev_state_get_compose(state, fmt->pad);
367 	selection->left = 0;
368 	selection->top = 0;
369 	selection->width = format->width;
370 	selection->height = format->height;
371 
372 done:
373 	mutex_unlock(&entity->lock);
374 	return ret;
375 }
376 
vsp1_entity_init_state(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state)377 static int vsp1_entity_init_state(struct v4l2_subdev *subdev,
378 				  struct v4l2_subdev_state *sd_state)
379 {
380 	unsigned int pad;
381 
382 	/* Initialize all pad formats with default values. */
383 	for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
384 		struct v4l2_subdev_format format = {
385 			.pad = pad,
386 			.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
387 			       : V4L2_SUBDEV_FORMAT_ACTIVE,
388 		};
389 
390 		v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &format);
391 	}
392 
393 	return 0;
394 }
395 
396 static const struct v4l2_subdev_internal_ops vsp1_entity_internal_ops = {
397 	.init_state = vsp1_entity_init_state,
398 };
399 
400 const struct v4l2_subdev_core_ops vsp1_entity_core_ops = {
401 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
402 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
403 };
404 
405 /* -----------------------------------------------------------------------------
406  * Media Operations
407  */
408 
409 static inline struct vsp1_entity *
media_entity_to_vsp1_entity(struct media_entity * entity)410 media_entity_to_vsp1_entity(struct media_entity *entity)
411 {
412 	return container_of(entity, struct vsp1_entity, subdev.entity);
413 }
414 
vsp1_entity_link_setup_source(const struct media_pad * source_pad,const struct media_pad * sink_pad,u32 flags)415 static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
416 					 const struct media_pad *sink_pad,
417 					 u32 flags)
418 {
419 	struct vsp1_entity *source;
420 
421 	source = media_entity_to_vsp1_entity(source_pad->entity);
422 
423 	if (!source->route)
424 		return 0;
425 
426 	if (flags & MEDIA_LNK_FL_ENABLED) {
427 		struct vsp1_entity *sink
428 			= media_entity_to_vsp1_entity(sink_pad->entity);
429 
430 		/*
431 		 * Fan-out is limited to one for the normal data path plus
432 		 * optional HGO and HGT. We ignore the HGO and HGT here.
433 		 */
434 		if (sink->type != VSP1_ENTITY_HGO &&
435 		    sink->type != VSP1_ENTITY_HGT) {
436 			if (source->sink)
437 				return -EBUSY;
438 			source->sink = sink;
439 			source->sink_pad = sink_pad->index;
440 		}
441 	} else {
442 		source->sink = NULL;
443 		source->sink_pad = 0;
444 	}
445 
446 	return 0;
447 }
448 
vsp1_entity_link_setup_sink(const struct media_pad * source_pad,const struct media_pad * sink_pad,u32 flags)449 static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
450 				       const struct media_pad *sink_pad,
451 				       u32 flags)
452 {
453 	struct vsp1_entity *sink;
454 	struct vsp1_entity *source;
455 
456 	sink = media_entity_to_vsp1_entity(sink_pad->entity);
457 	source = media_entity_to_vsp1_entity(source_pad->entity);
458 
459 	if (flags & MEDIA_LNK_FL_ENABLED) {
460 		/* Fan-in is limited to one. */
461 		if (sink->sources[sink_pad->index])
462 			return -EBUSY;
463 
464 		sink->sources[sink_pad->index] = source;
465 	} else {
466 		sink->sources[sink_pad->index] = NULL;
467 	}
468 
469 	return 0;
470 }
471 
vsp1_entity_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)472 int vsp1_entity_link_setup(struct media_entity *entity,
473 			   const struct media_pad *local,
474 			   const struct media_pad *remote, u32 flags)
475 {
476 	if (local->flags & MEDIA_PAD_FL_SOURCE)
477 		return vsp1_entity_link_setup_source(local, remote, flags);
478 	else
479 		return vsp1_entity_link_setup_sink(remote, local, flags);
480 }
481 
482 /**
483  * vsp1_entity_remote_pad - Find the pad at the remote end of a link
484  * @pad: Pad at the local end of the link
485  *
486  * Search for a remote pad connected to the given pad by iterating over all
487  * links originating or terminating at that pad until an enabled link is found.
488  *
489  * Our link setup implementation guarantees that the output fan-out will not be
490  * higher than one for the data pipelines, except for the links to the HGO and
491  * HGT that can be enabled in addition to a regular data link. When traversing
492  * outgoing links this function ignores HGO and HGT entities and should thus be
493  * used in place of the generic media_pad_remote_pad_first() function to
494  * traverse data pipelines.
495  *
496  * Return a pointer to the pad at the remote end of the first found enabled
497  * link, or NULL if no enabled link has been found.
498  */
vsp1_entity_remote_pad(struct media_pad * pad)499 struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
500 {
501 	struct media_link *link;
502 
503 	list_for_each_entry(link, &pad->entity->links, list) {
504 		struct vsp1_entity *entity;
505 
506 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
507 			continue;
508 
509 		/* If we're the sink the source will never be an HGO or HGT. */
510 		if (link->sink == pad)
511 			return link->source;
512 
513 		if (link->source != pad)
514 			continue;
515 
516 		/* If the sink isn't a subdevice it can't be an HGO or HGT. */
517 		if (!is_media_entity_v4l2_subdev(link->sink->entity))
518 			return link->sink;
519 
520 		entity = media_entity_to_vsp1_entity(link->sink->entity);
521 		if (entity->type != VSP1_ENTITY_HGO &&
522 		    entity->type != VSP1_ENTITY_HGT)
523 			return link->sink;
524 	}
525 
526 	return NULL;
527 
528 }
529 
530 /* -----------------------------------------------------------------------------
531  * Initialization
532  */
533 
534 #define VSP1_ENTITY_ROUTE(ent)						\
535 	{ VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE,			\
536 	  { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
537 
538 #define VSP1_ENTITY_ROUTE_RPF(idx)					\
539 	{ VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx),			\
540 	  { 0, }, VI6_DPR_NODE_RPF(idx) }
541 
542 #define VSP1_ENTITY_ROUTE_UDS(idx)					\
543 	{ VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx),			\
544 	  { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
545 
546 #define VSP1_ENTITY_ROUTE_UIF(idx)					\
547 	{ VSP1_ENTITY_UIF, idx, VI6_DPR_UIF_ROUTE(idx),			\
548 	  { VI6_DPR_NODE_UIF(idx) }, VI6_DPR_NODE_UIF(idx) }
549 
550 #define VSP1_ENTITY_ROUTE_WPF(idx)					\
551 	{ VSP1_ENTITY_WPF, idx, 0,					\
552 	  { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
553 
554 static const struct vsp1_route vsp1_routes[] = {
555 	{ VSP1_ENTITY_IIF, 0, VI6_DPR_BRU_ROUTE,
556 	  { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
557 	    VI6_DPR_NODE_BRU_IN(3) }, VI6_DPR_NODE_WPF(0) },
558 	{ VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE,
559 	  { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 },
560 	{ VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
561 	  { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
562 	    VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
563 	    VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
564 	VSP1_ENTITY_ROUTE(CLU),
565 	{ VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
566 	{ VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
567 	VSP1_ENTITY_ROUTE(HSI),
568 	VSP1_ENTITY_ROUTE(HST),
569 	{ VSP1_ENTITY_LIF, 0, 0, { 0, }, 0 },
570 	{ VSP1_ENTITY_LIF, 1, 0, { 0, }, 0 },
571 	VSP1_ENTITY_ROUTE(LUT),
572 	VSP1_ENTITY_ROUTE_RPF(0),
573 	VSP1_ENTITY_ROUTE_RPF(1),
574 	VSP1_ENTITY_ROUTE_RPF(2),
575 	VSP1_ENTITY_ROUTE_RPF(3),
576 	VSP1_ENTITY_ROUTE_RPF(4),
577 	VSP1_ENTITY_ROUTE(SRU),
578 	VSP1_ENTITY_ROUTE_UDS(0),
579 	VSP1_ENTITY_ROUTE_UDS(1),
580 	VSP1_ENTITY_ROUTE_UDS(2),
581 	VSP1_ENTITY_ROUTE_UIF(0),	/* Named UIF4 in the documentation */
582 	VSP1_ENTITY_ROUTE_UIF(1),	/* Named UIF5 in the documentation */
583 	VSP1_ENTITY_ROUTE_WPF(0),
584 	VSP1_ENTITY_ROUTE_WPF(1),
585 	VSP1_ENTITY_ROUTE_WPF(2),
586 	VSP1_ENTITY_ROUTE_WPF(3),
587 };
588 
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)589 int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
590 		     const char *name, unsigned int num_pads,
591 		     const struct v4l2_subdev_ops *ops, u32 function)
592 {
593 	static struct lock_class_key key;
594 	struct v4l2_subdev *subdev;
595 	unsigned int i;
596 	int ret;
597 
598 	for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
599 		if (vsp1_routes[i].type == entity->type &&
600 		    vsp1_routes[i].index == entity->index) {
601 			entity->route = &vsp1_routes[i];
602 			break;
603 		}
604 	}
605 
606 	if (i == ARRAY_SIZE(vsp1_routes))
607 		return -EINVAL;
608 
609 	mutex_init(&entity->lock);
610 
611 	entity->vsp1 = vsp1;
612 	entity->source_pad = num_pads - 1;
613 
614 	/* Allocate and initialize pads. */
615 	entity->pads = devm_kcalloc(vsp1->dev,
616 				    num_pads, sizeof(*entity->pads),
617 				    GFP_KERNEL);
618 	if (entity->pads == NULL)
619 		return -ENOMEM;
620 
621 	for (i = 0; i < num_pads - 1; ++i)
622 		entity->pads[i].flags = MEDIA_PAD_FL_SINK;
623 
624 	entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
625 				       sizeof(*entity->sources), GFP_KERNEL);
626 	if (entity->sources == NULL)
627 		return -ENOMEM;
628 
629 	/* Single-pad entities only have a sink. */
630 	entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
631 					 : MEDIA_PAD_FL_SINK;
632 
633 	/* Initialize the media entity. */
634 	ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
635 				     entity->pads);
636 	if (ret < 0)
637 		return ret;
638 
639 	/* Initialize the V4L2 subdev. */
640 	subdev = &entity->subdev;
641 	v4l2_subdev_init(subdev, ops);
642 	subdev->internal_ops = &vsp1_entity_internal_ops;
643 
644 	subdev->entity.function = function;
645 	subdev->entity.ops = &vsp1->media_ops;
646 	subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
647 
648 	if (ops->core == &vsp1_entity_core_ops)
649 		subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS;
650 
651 	snprintf(subdev->name, sizeof(subdev->name), "%s %s",
652 		 dev_name(vsp1->dev), name);
653 
654 	vsp1_entity_init_state(subdev, NULL);
655 
656 	/*
657 	 * Allocate the subdev state to store formats and selection
658 	 * rectangles.
659 	 */
660 	/*
661 	 * FIXME: Drop this call, drivers are not supposed to use
662 	 * __v4l2_subdev_state_alloc().
663 	 */
664 	entity->state = __v4l2_subdev_state_alloc(&entity->subdev,
665 						  "vsp1:state->lock", &key);
666 	if (IS_ERR(entity->state)) {
667 		media_entity_cleanup(&entity->subdev.entity);
668 		return PTR_ERR(entity->state);
669 	}
670 
671 	return 0;
672 }
673 
vsp1_entity_destroy(struct vsp1_entity * entity)674 void vsp1_entity_destroy(struct vsp1_entity *entity)
675 {
676 	if (entity->ops && entity->ops->destroy)
677 		entity->ops->destroy(entity);
678 	if (entity->subdev.ctrl_handler)
679 		v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
680 	__v4l2_subdev_state_free(entity->state);
681 	media_entity_cleanup(&entity->subdev.entity);
682 }
683