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