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