2 * vsp1_entity.c -- R-Car VSP1 Base Entity
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/device.h>
15 #include <linux/gfp.h>
17 #include <media/media-entity.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-subdev.h>
23 #include "vsp1_entity.h"
24 #include "vsp1_pipe.h"
25 #include "vsp1_rwpf.h"
27 static inline struct vsp1_entity *
28 media_entity_to_vsp1_entity(struct media_entity *entity)
30 return container_of(entity, struct vsp1_entity, subdev.entity);
33 void vsp1_entity_route_setup(struct vsp1_entity *entity,
34 struct vsp1_pipeline *pipe,
35 struct vsp1_dl_list *dl)
37 struct vsp1_entity *source;
38 struct vsp1_entity *sink;
40 if (entity->type == VSP1_ENTITY_HGO) {
44 * The HGO is a special case, its routing is configured on the
47 source = media_entity_to_vsp1_entity(entity->sources[0]);
48 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
49 | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
51 vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt);
53 } else if (entity->type == VSP1_ENTITY_HGT) {
57 * The HGT is a special case, its routing is configured on the
60 source = media_entity_to_vsp1_entity(entity->sources[0]);
61 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
62 | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
64 vsp1_dl_list_write(dl, VI6_DPR_HGT_SMPPT, smppt);
69 if (source->route->reg == 0)
72 sink = media_entity_to_vsp1_entity(source->sink);
73 vsp1_dl_list_write(dl, source->route->reg,
74 sink->route->inputs[source->sink_pad]);
77 /* -----------------------------------------------------------------------------
78 * V4L2 Subdevice Operations
82 * vsp1_entity_get_pad_config - Get the pad configuration for an entity
84 * @cfg: the TRY pad configuration
85 * @which: configuration selector (ACTIVE or TRY)
87 * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
88 * the entity lock to access the returned configuration.
90 * Return the pad configuration requested by the which argument. The TRY
91 * configuration is passed explicitly to the function through the cfg argument
92 * and simply returned when requested. The ACTIVE configuration comes from the
95 struct v4l2_subdev_pad_config *
96 vsp1_entity_get_pad_config(struct vsp1_entity *entity,
97 struct v4l2_subdev_pad_config *cfg,
98 enum v4l2_subdev_format_whence which)
101 case V4L2_SUBDEV_FORMAT_ACTIVE:
102 return entity->config;
103 case V4L2_SUBDEV_FORMAT_TRY:
110 * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
111 * @entity: the entity
112 * @cfg: the configuration storage
113 * @pad: the pad number
115 * Return the format stored in the given configuration for an entity's pad. The
116 * configuration can be an ACTIVE or TRY configuration.
118 struct v4l2_mbus_framefmt *
119 vsp1_entity_get_pad_format(struct vsp1_entity *entity,
120 struct v4l2_subdev_pad_config *cfg,
123 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
127 * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
128 * @entity: the entity
129 * @cfg: the configuration storage
130 * @pad: the pad number
131 * @target: the selection target
133 * Return the selection rectangle stored in the given configuration for an
134 * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
135 * selection target can be COMPOSE or CROP.
138 vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
139 struct v4l2_subdev_pad_config *cfg,
140 unsigned int pad, unsigned int target)
143 case V4L2_SEL_TGT_COMPOSE:
144 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
145 case V4L2_SEL_TGT_CROP:
146 return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
153 * vsp1_entity_init_cfg - Initialize formats on all pads
154 * @subdev: V4L2 subdevice
155 * @cfg: V4L2 subdev pad configuration
157 * Initialize all pad formats with default values in the given pad config. This
158 * function can be used as a handler for the subdev pad::init_cfg operation.
160 int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
161 struct v4l2_subdev_pad_config *cfg)
163 struct v4l2_subdev_format format;
166 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
167 memset(&format, 0, sizeof(format));
170 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
171 : V4L2_SUBDEV_FORMAT_ACTIVE;
173 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
180 * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
181 * @subdev: V4L2 subdevice
182 * @cfg: V4L2 subdev pad configuration
183 * @fmt: V4L2 subdev format
185 * This function implements the subdev get_fmt pad operation. It can be used as
186 * a direct drop-in for the operation handler.
188 int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
189 struct v4l2_subdev_pad_config *cfg,
190 struct v4l2_subdev_format *fmt)
192 struct vsp1_entity *entity = to_vsp1_entity(subdev);
193 struct v4l2_subdev_pad_config *config;
195 config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
199 mutex_lock(&entity->lock);
200 fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
201 mutex_unlock(&entity->lock);
207 * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
208 * @subdev: V4L2 subdevice
209 * @cfg: V4L2 subdev pad configuration
210 * @code: Media bus code enumeration
211 * @codes: Array of supported media bus codes
212 * @ncodes: Number of supported media bus codes
214 * This function implements the subdev enum_mbus_code pad operation for entities
215 * that do not support format conversion. It enumerates the given supported
216 * media bus codes on the sink pad and reports a source pad format identical to
219 int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
220 struct v4l2_subdev_pad_config *cfg,
221 struct v4l2_subdev_mbus_code_enum *code,
222 const unsigned int *codes, unsigned int ncodes)
224 struct vsp1_entity *entity = to_vsp1_entity(subdev);
226 if (code->pad == 0) {
227 if (code->index >= ncodes)
230 code->code = codes[code->index];
232 struct v4l2_subdev_pad_config *config;
233 struct v4l2_mbus_framefmt *format;
236 * The entity can't perform format conversion, the sink format
237 * is always identical to the source format.
242 config = vsp1_entity_get_pad_config(entity, cfg, code->which);
246 mutex_lock(&entity->lock);
247 format = vsp1_entity_get_pad_format(entity, config, 0);
248 code->code = format->code;
249 mutex_unlock(&entity->lock);
256 * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
257 * @subdev: V4L2 subdevice
258 * @cfg: V4L2 subdev pad configuration
259 * @fse: Frame size enumeration
260 * @min_width: Minimum image width
261 * @min_height: Minimum image height
262 * @max_width: Maximum image width
263 * @max_height: Maximum image height
265 * This function implements the subdev enum_frame_size pad operation for
266 * entities that do not support scaling or cropping. It reports the given
267 * minimum and maximum frame width and height on the sink pad, and a fixed
268 * source pad size identical to the sink pad.
270 int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
271 struct v4l2_subdev_pad_config *cfg,
272 struct v4l2_subdev_frame_size_enum *fse,
273 unsigned int min_width, unsigned int min_height,
274 unsigned int max_width, unsigned int max_height)
276 struct vsp1_entity *entity = to_vsp1_entity(subdev);
277 struct v4l2_subdev_pad_config *config;
278 struct v4l2_mbus_framefmt *format;
281 config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
285 format = vsp1_entity_get_pad_format(entity, config, fse->pad);
287 mutex_lock(&entity->lock);
289 if (fse->index || fse->code != format->code) {
295 fse->min_width = min_width;
296 fse->max_width = max_width;
297 fse->min_height = min_height;
298 fse->max_height = max_height;
301 * The size on the source pad are fixed and always identical to
302 * the size on the sink pad.
304 fse->min_width = format->width;
305 fse->max_width = format->width;
306 fse->min_height = format->height;
307 fse->max_height = format->height;
311 mutex_unlock(&entity->lock);
315 /* -----------------------------------------------------------------------------
319 static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
320 const struct media_pad *sink_pad,
323 struct vsp1_entity *source;
325 source = media_entity_to_vsp1_entity(source_pad->entity);
330 if (flags & MEDIA_LNK_FL_ENABLED) {
331 struct vsp1_entity *sink
332 = media_entity_to_vsp1_entity(sink_pad->entity);
335 * Fan-out is limited to one for the normal data path plus
336 * optional HGO and HGT. We ignore the HGO and HGT here.
338 if (sink->type != VSP1_ENTITY_HGO &&
339 sink->type != VSP1_ENTITY_HGT) {
342 source->sink = sink_pad->entity;
343 source->sink_pad = sink_pad->index;
347 source->sink_pad = 0;
353 static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
354 const struct media_pad *sink_pad,
357 struct vsp1_entity *sink;
359 sink = media_entity_to_vsp1_entity(sink_pad->entity);
361 if (flags & MEDIA_LNK_FL_ENABLED) {
362 /* Fan-in is limited to one. */
363 if (sink->sources[sink_pad->index])
366 sink->sources[sink_pad->index] = source_pad->entity;
368 sink->sources[sink_pad->index] = NULL;
374 int vsp1_entity_link_setup(struct media_entity *entity,
375 const struct media_pad *local,
376 const struct media_pad *remote, u32 flags)
378 if (local->flags & MEDIA_PAD_FL_SOURCE)
379 return vsp1_entity_link_setup_source(local, remote, flags);
381 return vsp1_entity_link_setup_sink(remote, local, flags);
385 * vsp1_entity_remote_pad - Find the pad at the remote end of a link
386 * @pad: Pad at the local end of the link
388 * Search for a remote pad connected to the given pad by iterating over all
389 * links originating or terminating at that pad until an enabled link is found.
391 * Our link setup implementation guarantees that the output fan-out will not be
392 * higher than one for the data pipelines, except for the links to the HGO and
393 * HGT that can be enabled in addition to a regular data link. When traversing
394 * outgoing links this function ignores HGO and HGT entities and should thus be
395 * used in place of the generic media_entity_remote_pad() function to traverse
398 * Return a pointer to the pad at the remote end of the first found enabled
399 * link, or NULL if no enabled link has been found.
401 struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
403 struct media_link *link;
405 list_for_each_entry(link, &pad->entity->links, list) {
406 struct vsp1_entity *entity;
408 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
411 /* If we're the sink the source will never be an HGO or HGT. */
412 if (link->sink == pad)
415 if (link->source != pad)
418 /* If the sink isn't a subdevice it can't be an HGO or HGT. */
419 if (!is_media_entity_v4l2_subdev(link->sink->entity))
422 entity = media_entity_to_vsp1_entity(link->sink->entity);
423 if (entity->type != VSP1_ENTITY_HGO &&
424 entity->type != VSP1_ENTITY_HGT)
432 /* -----------------------------------------------------------------------------
436 #define VSP1_ENTITY_ROUTE(ent) \
437 { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
438 { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
440 #define VSP1_ENTITY_ROUTE_RPF(idx) \
441 { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
442 { 0, }, VI6_DPR_NODE_RPF(idx) }
444 #define VSP1_ENTITY_ROUTE_UDS(idx) \
445 { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
446 { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
448 #define VSP1_ENTITY_ROUTE_WPF(idx) \
449 { VSP1_ENTITY_WPF, idx, 0, \
450 { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
452 static const struct vsp1_route vsp1_routes[] = {
453 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
454 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
455 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
456 VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
457 VSP1_ENTITY_ROUTE(CLU),
458 { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
459 { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
460 VSP1_ENTITY_ROUTE(HSI),
461 VSP1_ENTITY_ROUTE(HST),
462 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
463 VSP1_ENTITY_ROUTE(LUT),
464 VSP1_ENTITY_ROUTE_RPF(0),
465 VSP1_ENTITY_ROUTE_RPF(1),
466 VSP1_ENTITY_ROUTE_RPF(2),
467 VSP1_ENTITY_ROUTE_RPF(3),
468 VSP1_ENTITY_ROUTE_RPF(4),
469 VSP1_ENTITY_ROUTE(SRU),
470 VSP1_ENTITY_ROUTE_UDS(0),
471 VSP1_ENTITY_ROUTE_UDS(1),
472 VSP1_ENTITY_ROUTE_UDS(2),
473 VSP1_ENTITY_ROUTE_WPF(0),
474 VSP1_ENTITY_ROUTE_WPF(1),
475 VSP1_ENTITY_ROUTE_WPF(2),
476 VSP1_ENTITY_ROUTE_WPF(3),
479 int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
480 const char *name, unsigned int num_pads,
481 const struct v4l2_subdev_ops *ops, u32 function)
483 struct v4l2_subdev *subdev;
487 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
488 if (vsp1_routes[i].type == entity->type &&
489 vsp1_routes[i].index == entity->index) {
490 entity->route = &vsp1_routes[i];
495 if (i == ARRAY_SIZE(vsp1_routes))
498 mutex_init(&entity->lock);
501 entity->source_pad = num_pads - 1;
503 /* Allocate and initialize pads. */
504 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
506 if (entity->pads == NULL)
509 for (i = 0; i < num_pads - 1; ++i)
510 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
512 entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
513 sizeof(*entity->sources), GFP_KERNEL);
514 if (entity->sources == NULL)
517 /* Single-pad entities only have a sink. */
518 entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
521 /* Initialize the media entity. */
522 ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
527 /* Initialize the V4L2 subdev. */
528 subdev = &entity->subdev;
529 v4l2_subdev_init(subdev, ops);
531 subdev->entity.function = function;
532 subdev->entity.ops = &vsp1->media_ops;
533 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
535 snprintf(subdev->name, sizeof(subdev->name), "%s %s",
536 dev_name(vsp1->dev), name);
538 vsp1_entity_init_cfg(subdev, NULL);
541 * Allocate the pad configuration to store formats and selection
544 entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
545 if (entity->config == NULL) {
546 media_entity_cleanup(&entity->subdev.entity);
553 void vsp1_entity_destroy(struct vsp1_entity *entity)
555 if (entity->ops && entity->ops->destroy)
556 entity->ops->destroy(entity);
557 if (entity->subdev.ctrl_handler)
558 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
559 v4l2_subdev_free_pad_config(entity->config);
560 media_entity_cleanup(&entity->subdev.entity);