653 lines · c
1// SPDX-License-Identifier: GPL-2.0+2/*3 * vsp1_entity.c -- R-Car VSP1 Base Entity4 *5 * Copyright (C) 2013-2014 Renesas Electronics Corporation6 *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 23void 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 the35 * 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 the48 * 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 bit65 * 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 72void 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 82void 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 91void 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 Operations104 */105 106/**107 * vsp1_entity_get_state - Get the subdev state for an entity108 * @entity: the entity109 * @sd_state: the TRY state110 * @which: state selector (ACTIVE or TRY)111 *112 * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold113 * the entity lock to access the returned configuration.114 *115 * Return the subdev state requested by the which argument. The TRY state is116 * passed explicitly to the function through the sd_state argument and simply117 * returned when requested. The ACTIVE state comes from the entity structure.118 */119struct v4l2_subdev_state *120vsp1_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 handler135 * @subdev: V4L2 subdevice136 * @sd_state: V4L2 subdev state137 * @fmt: V4L2 subdev format138 *139 * This function implements the subdev get_fmt pad operation. It can be used as140 * a direct drop-in for the operation handler.141 */142int 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 handler162 * @subdev: V4L2 subdevice163 * @sd_state: V4L2 subdev state164 * @code: Media bus code enumeration165 * @codes: Array of supported media bus codes166 * @ncodes: Number of supported media bus codes167 *168 * This function implements the subdev enum_mbus_code pad operation for entities169 * that do not support format conversion. It enumerates the given supported170 * media bus codes on the sink pad and reports a source pad format identical to171 * the sink pad.172 */173int 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 format191 * 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 handler211 * @subdev: V4L2 subdevice212 * @sd_state: V4L2 subdev state213 * @fse: Frame size enumeration214 * @min_width: Minimum image width215 * @min_height: Minimum image height216 * @max_width: Maximum image width217 * @max_height: Maximum image height218 *219 * This function implements the subdev enum_frame_size pad operation for220 * entities that do not support scaling or cropping. It reports the given221 * minimum and maximum frame width and height on the sink pad, and a fixed222 * source pad size identical to the sink pad.223 */224int 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 to256 * 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 264done:265 mutex_unlock(&entity->lock);266 return ret;267}268 269/*270 * vsp1_subdev_set_pad_format - Subdev pad set_fmt handler271 * @subdev: V4L2 subdevice272 * @sd_state: V4L2 subdev state273 * @fmt: V4L2 subdev format274 * @codes: Array of supported media bus codes275 * @ncodes: Number of supported media bus codes276 * @min_width: Minimum image width277 * @min_height: Minimum image height278 * @max_width: Maximum image width279 * @max_height: Maximum image height280 *281 * This function implements the subdev set_fmt pad operation for entities that282 * do not support scaling or cropping. It defaults to the first supplied media283 * bus code if the requested code isn't supported, clamps the size to the284 * supplied minimum and maximum, and propagates the sink pad format to the285 * source pad.286 */287int 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 not319 * 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 353done:354 mutex_unlock(&entity->lock);355 return ret;356}357 358static 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_TRY368 : V4L2_SUBDEV_FORMAT_ACTIVE,369 };370 371 v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &format);372 }373 374 return 0;375}376 377static const struct v4l2_subdev_internal_ops vsp1_entity_internal_ops = {378 .init_state = vsp1_entity_init_state,379};380 381/* -----------------------------------------------------------------------------382 * Media Operations383 */384 385static inline struct vsp1_entity *386media_entity_to_vsp1_entity(struct media_entity *entity)387{388 return container_of(entity, struct vsp1_entity, subdev.entity);389}390 391static 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 *sink404 = media_entity_to_vsp1_entity(sink_pad->entity);405 406 /*407 * Fan-out is limited to one for the normal data path plus408 * 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 425static 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 448int 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 else455 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 link460 * @pad: Pad at the local end of the link461 *462 * Search for a remote pad connected to the given pad by iterating over all463 * 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 be466 * higher than one for the data pipelines, except for the links to the HGO and467 * HGT that can be enabled in addition to a regular data link. When traversing468 * outgoing links this function ignores HGO and HGT entities and should thus be469 * used in place of the generic media_pad_remote_pad_first() function to470 * traverse data pipelines.471 *472 * Return a pointer to the pad at the remote end of the first found enabled473 * link, or NULL if no enabled link has been found.474 */475struct 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 * Initialization508 */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 530static 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 562int 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_SOURCE604 : 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 selection628 * rectangles.629 */630 /*631 * FIXME: Drop this call, drivers are not supposed to use632 * __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 644void 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