18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * vsp1_entity.c  --  R-Car VSP1 Base Entity
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013-2014 Renesas Electronics Corporation
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/device.h>
118c2ecf20Sopenharmony_ci#include <linux/gfp.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <media/media-entity.h>
148c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
158c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "vsp1.h"
188c2ecf20Sopenharmony_ci#include "vsp1_dl.h"
198c2ecf20Sopenharmony_ci#include "vsp1_entity.h"
208c2ecf20Sopenharmony_ci#include "vsp1_pipe.h"
218c2ecf20Sopenharmony_ci#include "vsp1_rwpf.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_civoid vsp1_entity_route_setup(struct vsp1_entity *entity,
248c2ecf20Sopenharmony_ci			     struct vsp1_pipeline *pipe,
258c2ecf20Sopenharmony_ci			     struct vsp1_dl_body *dlb)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct vsp1_entity *source;
288c2ecf20Sopenharmony_ci	u32 route;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	if (entity->type == VSP1_ENTITY_HGO) {
318c2ecf20Sopenharmony_ci		u32 smppt;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci		/*
348c2ecf20Sopenharmony_ci		 * The HGO is a special case, its routing is configured on the
358c2ecf20Sopenharmony_ci		 * sink pad.
368c2ecf20Sopenharmony_ci		 */
378c2ecf20Sopenharmony_ci		source = entity->sources[0];
388c2ecf20Sopenharmony_ci		smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
398c2ecf20Sopenharmony_ci		      | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci		vsp1_dl_body_write(dlb, VI6_DPR_HGO_SMPPT, smppt);
428c2ecf20Sopenharmony_ci		return;
438c2ecf20Sopenharmony_ci	} else if (entity->type == VSP1_ENTITY_HGT) {
448c2ecf20Sopenharmony_ci		u32 smppt;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci		/*
478c2ecf20Sopenharmony_ci		 * The HGT is a special case, its routing is configured on the
488c2ecf20Sopenharmony_ci		 * sink pad.
498c2ecf20Sopenharmony_ci		 */
508c2ecf20Sopenharmony_ci		source = entity->sources[0];
518c2ecf20Sopenharmony_ci		smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
528c2ecf20Sopenharmony_ci		      | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci		vsp1_dl_body_write(dlb, VI6_DPR_HGT_SMPPT, smppt);
558c2ecf20Sopenharmony_ci		return;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	source = entity;
598c2ecf20Sopenharmony_ci	if (source->route->reg == 0)
608c2ecf20Sopenharmony_ci		return;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	route = source->sink->route->inputs[source->sink_pad];
638c2ecf20Sopenharmony_ci	/*
648c2ecf20Sopenharmony_ci	 * The ILV and BRS share the same data path route. The extra BRSSEL bit
658c2ecf20Sopenharmony_ci	 * selects between the ILV and BRS.
668c2ecf20Sopenharmony_ci	 */
678c2ecf20Sopenharmony_ci	if (source->type == VSP1_ENTITY_BRS)
688c2ecf20Sopenharmony_ci		route |= VI6_DPR_ROUTE_BRSSEL;
698c2ecf20Sopenharmony_ci	vsp1_dl_body_write(dlb, source->route->reg, route);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_civoid vsp1_entity_configure_stream(struct vsp1_entity *entity,
738c2ecf20Sopenharmony_ci				  struct vsp1_pipeline *pipe,
748c2ecf20Sopenharmony_ci				  struct vsp1_dl_list *dl,
758c2ecf20Sopenharmony_ci				  struct vsp1_dl_body *dlb)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	if (entity->ops->configure_stream)
788c2ecf20Sopenharmony_ci		entity->ops->configure_stream(entity, pipe, dl, dlb);
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_civoid vsp1_entity_configure_frame(struct vsp1_entity *entity,
828c2ecf20Sopenharmony_ci				 struct vsp1_pipeline *pipe,
838c2ecf20Sopenharmony_ci				 struct vsp1_dl_list *dl,
848c2ecf20Sopenharmony_ci				 struct vsp1_dl_body *dlb)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	if (entity->ops->configure_frame)
878c2ecf20Sopenharmony_ci		entity->ops->configure_frame(entity, pipe, dl, dlb);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_civoid vsp1_entity_configure_partition(struct vsp1_entity *entity,
918c2ecf20Sopenharmony_ci				     struct vsp1_pipeline *pipe,
928c2ecf20Sopenharmony_ci				     struct vsp1_dl_list *dl,
938c2ecf20Sopenharmony_ci				     struct vsp1_dl_body *dlb)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	if (entity->ops->configure_partition)
968c2ecf20Sopenharmony_ci		entity->ops->configure_partition(entity, pipe, dl, dlb);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
1008c2ecf20Sopenharmony_ci * V4L2 Subdevice Operations
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/**
1048c2ecf20Sopenharmony_ci * vsp1_entity_get_pad_config - Get the pad configuration for an entity
1058c2ecf20Sopenharmony_ci * @entity: the entity
1068c2ecf20Sopenharmony_ci * @cfg: the TRY pad configuration
1078c2ecf20Sopenharmony_ci * @which: configuration selector (ACTIVE or TRY)
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
1108c2ecf20Sopenharmony_ci * the entity lock to access the returned configuration.
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * Return the pad configuration requested by the which argument. The TRY
1138c2ecf20Sopenharmony_ci * configuration is passed explicitly to the function through the cfg argument
1148c2ecf20Sopenharmony_ci * and simply returned when requested. The ACTIVE configuration comes from the
1158c2ecf20Sopenharmony_ci * entity structure.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_cistruct v4l2_subdev_pad_config *
1188c2ecf20Sopenharmony_civsp1_entity_get_pad_config(struct vsp1_entity *entity,
1198c2ecf20Sopenharmony_ci			   struct v4l2_subdev_pad_config *cfg,
1208c2ecf20Sopenharmony_ci			   enum v4l2_subdev_format_whence which)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	switch (which) {
1238c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_ACTIVE:
1248c2ecf20Sopenharmony_ci		return entity->config;
1258c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_TRY:
1268c2ecf20Sopenharmony_ci	default:
1278c2ecf20Sopenharmony_ci		return cfg;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/**
1328c2ecf20Sopenharmony_ci * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
1338c2ecf20Sopenharmony_ci * @entity: the entity
1348c2ecf20Sopenharmony_ci * @cfg: the configuration storage
1358c2ecf20Sopenharmony_ci * @pad: the pad number
1368c2ecf20Sopenharmony_ci *
1378c2ecf20Sopenharmony_ci * Return the format stored in the given configuration for an entity's pad. The
1388c2ecf20Sopenharmony_ci * configuration can be an ACTIVE or TRY configuration.
1398c2ecf20Sopenharmony_ci */
1408c2ecf20Sopenharmony_cistruct v4l2_mbus_framefmt *
1418c2ecf20Sopenharmony_civsp1_entity_get_pad_format(struct vsp1_entity *entity,
1428c2ecf20Sopenharmony_ci			   struct v4l2_subdev_pad_config *cfg,
1438c2ecf20Sopenharmony_ci			   unsigned int pad)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/**
1498c2ecf20Sopenharmony_ci * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
1508c2ecf20Sopenharmony_ci * @entity: the entity
1518c2ecf20Sopenharmony_ci * @cfg: the configuration storage
1528c2ecf20Sopenharmony_ci * @pad: the pad number
1538c2ecf20Sopenharmony_ci * @target: the selection target
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * Return the selection rectangle stored in the given configuration for an
1568c2ecf20Sopenharmony_ci * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
1578c2ecf20Sopenharmony_ci * selection target can be COMPOSE or CROP.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_cistruct v4l2_rect *
1608c2ecf20Sopenharmony_civsp1_entity_get_pad_selection(struct vsp1_entity *entity,
1618c2ecf20Sopenharmony_ci			      struct v4l2_subdev_pad_config *cfg,
1628c2ecf20Sopenharmony_ci			      unsigned int pad, unsigned int target)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	switch (target) {
1658c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_COMPOSE:
1668c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
1678c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP:
1688c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
1698c2ecf20Sopenharmony_ci	default:
1708c2ecf20Sopenharmony_ci		return NULL;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/*
1758c2ecf20Sopenharmony_ci * vsp1_entity_init_cfg - Initialize formats on all pads
1768c2ecf20Sopenharmony_ci * @subdev: V4L2 subdevice
1778c2ecf20Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * Initialize all pad formats with default values in the given pad config. This
1808c2ecf20Sopenharmony_ci * function can be used as a handler for the subdev pad::init_cfg operation.
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_ciint vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
1838c2ecf20Sopenharmony_ci			 struct v4l2_subdev_pad_config *cfg)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct v4l2_subdev_format format;
1868c2ecf20Sopenharmony_ci	unsigned int pad;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
1898c2ecf20Sopenharmony_ci		memset(&format, 0, sizeof(format));
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		format.pad = pad;
1928c2ecf20Sopenharmony_ci		format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
1938c2ecf20Sopenharmony_ci			     : V4L2_SUBDEV_FORMAT_ACTIVE;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/*
2028c2ecf20Sopenharmony_ci * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
2038c2ecf20Sopenharmony_ci * @subdev: V4L2 subdevice
2048c2ecf20Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
2058c2ecf20Sopenharmony_ci * @fmt: V4L2 subdev format
2068c2ecf20Sopenharmony_ci *
2078c2ecf20Sopenharmony_ci * This function implements the subdev get_fmt pad operation. It can be used as
2088c2ecf20Sopenharmony_ci * a direct drop-in for the operation handler.
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_ciint vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
2118c2ecf20Sopenharmony_ci			       struct v4l2_subdev_pad_config *cfg,
2128c2ecf20Sopenharmony_ci			       struct v4l2_subdev_format *fmt)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct vsp1_entity *entity = to_vsp1_entity(subdev);
2158c2ecf20Sopenharmony_ci	struct v4l2_subdev_pad_config *config;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
2188c2ecf20Sopenharmony_ci	if (!config)
2198c2ecf20Sopenharmony_ci		return -EINVAL;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	mutex_lock(&entity->lock);
2228c2ecf20Sopenharmony_ci	fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
2238c2ecf20Sopenharmony_ci	mutex_unlock(&entity->lock);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return 0;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/*
2298c2ecf20Sopenharmony_ci * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
2308c2ecf20Sopenharmony_ci * @subdev: V4L2 subdevice
2318c2ecf20Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
2328c2ecf20Sopenharmony_ci * @code: Media bus code enumeration
2338c2ecf20Sopenharmony_ci * @codes: Array of supported media bus codes
2348c2ecf20Sopenharmony_ci * @ncodes: Number of supported media bus codes
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * This function implements the subdev enum_mbus_code pad operation for entities
2378c2ecf20Sopenharmony_ci * that do not support format conversion. It enumerates the given supported
2388c2ecf20Sopenharmony_ci * media bus codes on the sink pad and reports a source pad format identical to
2398c2ecf20Sopenharmony_ci * the sink pad.
2408c2ecf20Sopenharmony_ci */
2418c2ecf20Sopenharmony_ciint vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
2428c2ecf20Sopenharmony_ci			       struct v4l2_subdev_pad_config *cfg,
2438c2ecf20Sopenharmony_ci			       struct v4l2_subdev_mbus_code_enum *code,
2448c2ecf20Sopenharmony_ci			       const unsigned int *codes, unsigned int ncodes)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct vsp1_entity *entity = to_vsp1_entity(subdev);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (code->pad == 0) {
2498c2ecf20Sopenharmony_ci		if (code->index >= ncodes)
2508c2ecf20Sopenharmony_ci			return -EINVAL;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci		code->code = codes[code->index];
2538c2ecf20Sopenharmony_ci	} else {
2548c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *config;
2558c2ecf20Sopenharmony_ci		struct v4l2_mbus_framefmt *format;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		/*
2588c2ecf20Sopenharmony_ci		 * The entity can't perform format conversion, the sink format
2598c2ecf20Sopenharmony_ci		 * is always identical to the source format.
2608c2ecf20Sopenharmony_ci		 */
2618c2ecf20Sopenharmony_ci		if (code->index)
2628c2ecf20Sopenharmony_ci			return -EINVAL;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		config = vsp1_entity_get_pad_config(entity, cfg, code->which);
2658c2ecf20Sopenharmony_ci		if (!config)
2668c2ecf20Sopenharmony_ci			return -EINVAL;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		mutex_lock(&entity->lock);
2698c2ecf20Sopenharmony_ci		format = vsp1_entity_get_pad_format(entity, config, 0);
2708c2ecf20Sopenharmony_ci		code->code = format->code;
2718c2ecf20Sopenharmony_ci		mutex_unlock(&entity->lock);
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci/*
2788c2ecf20Sopenharmony_ci * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
2798c2ecf20Sopenharmony_ci * @subdev: V4L2 subdevice
2808c2ecf20Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
2818c2ecf20Sopenharmony_ci * @fse: Frame size enumeration
2828c2ecf20Sopenharmony_ci * @min_width: Minimum image width
2838c2ecf20Sopenharmony_ci * @min_height: Minimum image height
2848c2ecf20Sopenharmony_ci * @max_width: Maximum image width
2858c2ecf20Sopenharmony_ci * @max_height: Maximum image height
2868c2ecf20Sopenharmony_ci *
2878c2ecf20Sopenharmony_ci * This function implements the subdev enum_frame_size pad operation for
2888c2ecf20Sopenharmony_ci * entities that do not support scaling or cropping. It reports the given
2898c2ecf20Sopenharmony_ci * minimum and maximum frame width and height on the sink pad, and a fixed
2908c2ecf20Sopenharmony_ci * source pad size identical to the sink pad.
2918c2ecf20Sopenharmony_ci */
2928c2ecf20Sopenharmony_ciint vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
2938c2ecf20Sopenharmony_ci				struct v4l2_subdev_pad_config *cfg,
2948c2ecf20Sopenharmony_ci				struct v4l2_subdev_frame_size_enum *fse,
2958c2ecf20Sopenharmony_ci				unsigned int min_width, unsigned int min_height,
2968c2ecf20Sopenharmony_ci				unsigned int max_width, unsigned int max_height)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	struct vsp1_entity *entity = to_vsp1_entity(subdev);
2998c2ecf20Sopenharmony_ci	struct v4l2_subdev_pad_config *config;
3008c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
3018c2ecf20Sopenharmony_ci	int ret = 0;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
3048c2ecf20Sopenharmony_ci	if (!config)
3058c2ecf20Sopenharmony_ci		return -EINVAL;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	format = vsp1_entity_get_pad_format(entity, config, fse->pad);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	mutex_lock(&entity->lock);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (fse->index || fse->code != format->code) {
3128c2ecf20Sopenharmony_ci		ret = -EINVAL;
3138c2ecf20Sopenharmony_ci		goto done;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (fse->pad == 0) {
3178c2ecf20Sopenharmony_ci		fse->min_width = min_width;
3188c2ecf20Sopenharmony_ci		fse->max_width = max_width;
3198c2ecf20Sopenharmony_ci		fse->min_height = min_height;
3208c2ecf20Sopenharmony_ci		fse->max_height = max_height;
3218c2ecf20Sopenharmony_ci	} else {
3228c2ecf20Sopenharmony_ci		/*
3238c2ecf20Sopenharmony_ci		 * The size on the source pad are fixed and always identical to
3248c2ecf20Sopenharmony_ci		 * the size on the sink pad.
3258c2ecf20Sopenharmony_ci		 */
3268c2ecf20Sopenharmony_ci		fse->min_width = format->width;
3278c2ecf20Sopenharmony_ci		fse->max_width = format->width;
3288c2ecf20Sopenharmony_ci		fse->min_height = format->height;
3298c2ecf20Sopenharmony_ci		fse->max_height = format->height;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cidone:
3338c2ecf20Sopenharmony_ci	mutex_unlock(&entity->lock);
3348c2ecf20Sopenharmony_ci	return ret;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci/*
3388c2ecf20Sopenharmony_ci * vsp1_subdev_set_pad_format - Subdev pad set_fmt handler
3398c2ecf20Sopenharmony_ci * @subdev: V4L2 subdevice
3408c2ecf20Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
3418c2ecf20Sopenharmony_ci * @fmt: V4L2 subdev format
3428c2ecf20Sopenharmony_ci * @codes: Array of supported media bus codes
3438c2ecf20Sopenharmony_ci * @ncodes: Number of supported media bus codes
3448c2ecf20Sopenharmony_ci * @min_width: Minimum image width
3458c2ecf20Sopenharmony_ci * @min_height: Minimum image height
3468c2ecf20Sopenharmony_ci * @max_width: Maximum image width
3478c2ecf20Sopenharmony_ci * @max_height: Maximum image height
3488c2ecf20Sopenharmony_ci *
3498c2ecf20Sopenharmony_ci * This function implements the subdev set_fmt pad operation for entities that
3508c2ecf20Sopenharmony_ci * do not support scaling or cropping. It defaults to the first supplied media
3518c2ecf20Sopenharmony_ci * bus code if the requested code isn't supported, clamps the size to the
3528c2ecf20Sopenharmony_ci * supplied minimum and maximum, and propagates the sink pad format to the
3538c2ecf20Sopenharmony_ci * source pad.
3548c2ecf20Sopenharmony_ci */
3558c2ecf20Sopenharmony_ciint vsp1_subdev_set_pad_format(struct v4l2_subdev *subdev,
3568c2ecf20Sopenharmony_ci			       struct v4l2_subdev_pad_config *cfg,
3578c2ecf20Sopenharmony_ci			       struct v4l2_subdev_format *fmt,
3588c2ecf20Sopenharmony_ci			       const unsigned int *codes, unsigned int ncodes,
3598c2ecf20Sopenharmony_ci			       unsigned int min_width, unsigned int min_height,
3608c2ecf20Sopenharmony_ci			       unsigned int max_width, unsigned int max_height)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	struct vsp1_entity *entity = to_vsp1_entity(subdev);
3638c2ecf20Sopenharmony_ci	struct v4l2_subdev_pad_config *config;
3648c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
3658c2ecf20Sopenharmony_ci	struct v4l2_rect *selection;
3668c2ecf20Sopenharmony_ci	unsigned int i;
3678c2ecf20Sopenharmony_ci	int ret = 0;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	mutex_lock(&entity->lock);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
3728c2ecf20Sopenharmony_ci	if (!config) {
3738c2ecf20Sopenharmony_ci		ret = -EINVAL;
3748c2ecf20Sopenharmony_ci		goto done;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	format = vsp1_entity_get_pad_format(entity, config, fmt->pad);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if (fmt->pad == entity->source_pad) {
3808c2ecf20Sopenharmony_ci		/* The output format can't be modified. */
3818c2ecf20Sopenharmony_ci		fmt->format = *format;
3828c2ecf20Sopenharmony_ci		goto done;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/*
3868c2ecf20Sopenharmony_ci	 * Default to the first media bus code if the requested format is not
3878c2ecf20Sopenharmony_ci	 * supported.
3888c2ecf20Sopenharmony_ci	 */
3898c2ecf20Sopenharmony_ci	for (i = 0; i < ncodes; ++i) {
3908c2ecf20Sopenharmony_ci		if (fmt->format.code == codes[i])
3918c2ecf20Sopenharmony_ci			break;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	format->code = i < ncodes ? codes[i] : codes[0];
3958c2ecf20Sopenharmony_ci	format->width = clamp_t(unsigned int, fmt->format.width,
3968c2ecf20Sopenharmony_ci				min_width, max_width);
3978c2ecf20Sopenharmony_ci	format->height = clamp_t(unsigned int, fmt->format.height,
3988c2ecf20Sopenharmony_ci				 min_height, max_height);
3998c2ecf20Sopenharmony_ci	format->field = V4L2_FIELD_NONE;
4008c2ecf20Sopenharmony_ci	format->colorspace = V4L2_COLORSPACE_SRGB;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	fmt->format = *format;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/* Propagate the format to the source pad. */
4058c2ecf20Sopenharmony_ci	format = vsp1_entity_get_pad_format(entity, config, entity->source_pad);
4068c2ecf20Sopenharmony_ci	*format = fmt->format;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* Reset the crop and compose rectangles. */
4098c2ecf20Sopenharmony_ci	selection = vsp1_entity_get_pad_selection(entity, config, fmt->pad,
4108c2ecf20Sopenharmony_ci						  V4L2_SEL_TGT_CROP);
4118c2ecf20Sopenharmony_ci	selection->left = 0;
4128c2ecf20Sopenharmony_ci	selection->top = 0;
4138c2ecf20Sopenharmony_ci	selection->width = format->width;
4148c2ecf20Sopenharmony_ci	selection->height = format->height;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	selection = vsp1_entity_get_pad_selection(entity, config, fmt->pad,
4178c2ecf20Sopenharmony_ci						  V4L2_SEL_TGT_COMPOSE);
4188c2ecf20Sopenharmony_ci	selection->left = 0;
4198c2ecf20Sopenharmony_ci	selection->top = 0;
4208c2ecf20Sopenharmony_ci	selection->width = format->width;
4218c2ecf20Sopenharmony_ci	selection->height = format->height;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cidone:
4248c2ecf20Sopenharmony_ci	mutex_unlock(&entity->lock);
4258c2ecf20Sopenharmony_ci	return ret;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
4298c2ecf20Sopenharmony_ci * Media Operations
4308c2ecf20Sopenharmony_ci */
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic inline struct vsp1_entity *
4338c2ecf20Sopenharmony_cimedia_entity_to_vsp1_entity(struct media_entity *entity)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	return container_of(entity, struct vsp1_entity, subdev.entity);
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
4398c2ecf20Sopenharmony_ci					 const struct media_pad *sink_pad,
4408c2ecf20Sopenharmony_ci					 u32 flags)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	struct vsp1_entity *source;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	source = media_entity_to_vsp1_entity(source_pad->entity);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	if (!source->route)
4478c2ecf20Sopenharmony_ci		return 0;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (flags & MEDIA_LNK_FL_ENABLED) {
4508c2ecf20Sopenharmony_ci		struct vsp1_entity *sink
4518c2ecf20Sopenharmony_ci			= media_entity_to_vsp1_entity(sink_pad->entity);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		/*
4548c2ecf20Sopenharmony_ci		 * Fan-out is limited to one for the normal data path plus
4558c2ecf20Sopenharmony_ci		 * optional HGO and HGT. We ignore the HGO and HGT here.
4568c2ecf20Sopenharmony_ci		 */
4578c2ecf20Sopenharmony_ci		if (sink->type != VSP1_ENTITY_HGO &&
4588c2ecf20Sopenharmony_ci		    sink->type != VSP1_ENTITY_HGT) {
4598c2ecf20Sopenharmony_ci			if (source->sink)
4608c2ecf20Sopenharmony_ci				return -EBUSY;
4618c2ecf20Sopenharmony_ci			source->sink = sink;
4628c2ecf20Sopenharmony_ci			source->sink_pad = sink_pad->index;
4638c2ecf20Sopenharmony_ci		}
4648c2ecf20Sopenharmony_ci	} else {
4658c2ecf20Sopenharmony_ci		source->sink = NULL;
4668c2ecf20Sopenharmony_ci		source->sink_pad = 0;
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	return 0;
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
4738c2ecf20Sopenharmony_ci				       const struct media_pad *sink_pad,
4748c2ecf20Sopenharmony_ci				       u32 flags)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	struct vsp1_entity *sink;
4778c2ecf20Sopenharmony_ci	struct vsp1_entity *source;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	sink = media_entity_to_vsp1_entity(sink_pad->entity);
4808c2ecf20Sopenharmony_ci	source = media_entity_to_vsp1_entity(source_pad->entity);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (flags & MEDIA_LNK_FL_ENABLED) {
4838c2ecf20Sopenharmony_ci		/* Fan-in is limited to one. */
4848c2ecf20Sopenharmony_ci		if (sink->sources[sink_pad->index])
4858c2ecf20Sopenharmony_ci			return -EBUSY;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci		sink->sources[sink_pad->index] = source;
4888c2ecf20Sopenharmony_ci	} else {
4898c2ecf20Sopenharmony_ci		sink->sources[sink_pad->index] = NULL;
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return 0;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ciint vsp1_entity_link_setup(struct media_entity *entity,
4968c2ecf20Sopenharmony_ci			   const struct media_pad *local,
4978c2ecf20Sopenharmony_ci			   const struct media_pad *remote, u32 flags)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	if (local->flags & MEDIA_PAD_FL_SOURCE)
5008c2ecf20Sopenharmony_ci		return vsp1_entity_link_setup_source(local, remote, flags);
5018c2ecf20Sopenharmony_ci	else
5028c2ecf20Sopenharmony_ci		return vsp1_entity_link_setup_sink(remote, local, flags);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci/**
5068c2ecf20Sopenharmony_ci * vsp1_entity_remote_pad - Find the pad at the remote end of a link
5078c2ecf20Sopenharmony_ci * @pad: Pad at the local end of the link
5088c2ecf20Sopenharmony_ci *
5098c2ecf20Sopenharmony_ci * Search for a remote pad connected to the given pad by iterating over all
5108c2ecf20Sopenharmony_ci * links originating or terminating at that pad until an enabled link is found.
5118c2ecf20Sopenharmony_ci *
5128c2ecf20Sopenharmony_ci * Our link setup implementation guarantees that the output fan-out will not be
5138c2ecf20Sopenharmony_ci * higher than one for the data pipelines, except for the links to the HGO and
5148c2ecf20Sopenharmony_ci * HGT that can be enabled in addition to a regular data link. When traversing
5158c2ecf20Sopenharmony_ci * outgoing links this function ignores HGO and HGT entities and should thus be
5168c2ecf20Sopenharmony_ci * used in place of the generic media_entity_remote_pad() function to traverse
5178c2ecf20Sopenharmony_ci * data pipelines.
5188c2ecf20Sopenharmony_ci *
5198c2ecf20Sopenharmony_ci * Return a pointer to the pad at the remote end of the first found enabled
5208c2ecf20Sopenharmony_ci * link, or NULL if no enabled link has been found.
5218c2ecf20Sopenharmony_ci */
5228c2ecf20Sopenharmony_cistruct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct media_link *link;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	list_for_each_entry(link, &pad->entity->links, list) {
5278c2ecf20Sopenharmony_ci		struct vsp1_entity *entity;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
5308c2ecf20Sopenharmony_ci			continue;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		/* If we're the sink the source will never be an HGO or HGT. */
5338c2ecf20Sopenharmony_ci		if (link->sink == pad)
5348c2ecf20Sopenharmony_ci			return link->source;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		if (link->source != pad)
5378c2ecf20Sopenharmony_ci			continue;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci		/* If the sink isn't a subdevice it can't be an HGO or HGT. */
5408c2ecf20Sopenharmony_ci		if (!is_media_entity_v4l2_subdev(link->sink->entity))
5418c2ecf20Sopenharmony_ci			return link->sink;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		entity = media_entity_to_vsp1_entity(link->sink->entity);
5448c2ecf20Sopenharmony_ci		if (entity->type != VSP1_ENTITY_HGO &&
5458c2ecf20Sopenharmony_ci		    entity->type != VSP1_ENTITY_HGT)
5468c2ecf20Sopenharmony_ci			return link->sink;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	return NULL;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
5548c2ecf20Sopenharmony_ci * Initialization
5558c2ecf20Sopenharmony_ci */
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci#define VSP1_ENTITY_ROUTE(ent)						\
5588c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE,			\
5598c2ecf20Sopenharmony_ci	  { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci#define VSP1_ENTITY_ROUTE_RPF(idx)					\
5628c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx),			\
5638c2ecf20Sopenharmony_ci	  { 0, }, VI6_DPR_NODE_RPF(idx) }
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci#define VSP1_ENTITY_ROUTE_UDS(idx)					\
5668c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx),			\
5678c2ecf20Sopenharmony_ci	  { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci#define VSP1_ENTITY_ROUTE_UIF(idx)					\
5708c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_UIF, idx, VI6_DPR_UIF_ROUTE(idx),			\
5718c2ecf20Sopenharmony_ci	  { VI6_DPR_NODE_UIF(idx) }, VI6_DPR_NODE_UIF(idx) }
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci#define VSP1_ENTITY_ROUTE_WPF(idx)					\
5748c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_WPF, idx, 0,					\
5758c2ecf20Sopenharmony_ci	  { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_cistatic const struct vsp1_route vsp1_routes[] = {
5788c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE,
5798c2ecf20Sopenharmony_ci	  { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 },
5808c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
5818c2ecf20Sopenharmony_ci	  { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
5828c2ecf20Sopenharmony_ci	    VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
5838c2ecf20Sopenharmony_ci	    VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
5848c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE(CLU),
5858c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
5868c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
5878c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE(HSI),
5888c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE(HST),
5898c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_LIF, 0, 0, { 0, }, 0 },
5908c2ecf20Sopenharmony_ci	{ VSP1_ENTITY_LIF, 1, 0, { 0, }, 0 },
5918c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE(LUT),
5928c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_RPF(0),
5938c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_RPF(1),
5948c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_RPF(2),
5958c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_RPF(3),
5968c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_RPF(4),
5978c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE(SRU),
5988c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_UDS(0),
5998c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_UDS(1),
6008c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_UDS(2),
6018c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_UIF(0),	/* Named UIF4 in the documentation */
6028c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_UIF(1),	/* Named UIF5 in the documentation */
6038c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_WPF(0),
6048c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_WPF(1),
6058c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_WPF(2),
6068c2ecf20Sopenharmony_ci	VSP1_ENTITY_ROUTE_WPF(3),
6078c2ecf20Sopenharmony_ci};
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ciint vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
6108c2ecf20Sopenharmony_ci		     const char *name, unsigned int num_pads,
6118c2ecf20Sopenharmony_ci		     const struct v4l2_subdev_ops *ops, u32 function)
6128c2ecf20Sopenharmony_ci{
6138c2ecf20Sopenharmony_ci	struct v4l2_subdev *subdev;
6148c2ecf20Sopenharmony_ci	unsigned int i;
6158c2ecf20Sopenharmony_ci	int ret;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
6188c2ecf20Sopenharmony_ci		if (vsp1_routes[i].type == entity->type &&
6198c2ecf20Sopenharmony_ci		    vsp1_routes[i].index == entity->index) {
6208c2ecf20Sopenharmony_ci			entity->route = &vsp1_routes[i];
6218c2ecf20Sopenharmony_ci			break;
6228c2ecf20Sopenharmony_ci		}
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if (i == ARRAY_SIZE(vsp1_routes))
6268c2ecf20Sopenharmony_ci		return -EINVAL;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	mutex_init(&entity->lock);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	entity->vsp1 = vsp1;
6318c2ecf20Sopenharmony_ci	entity->source_pad = num_pads - 1;
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	/* Allocate and initialize pads. */
6348c2ecf20Sopenharmony_ci	entity->pads = devm_kcalloc(vsp1->dev,
6358c2ecf20Sopenharmony_ci				    num_pads, sizeof(*entity->pads),
6368c2ecf20Sopenharmony_ci				    GFP_KERNEL);
6378c2ecf20Sopenharmony_ci	if (entity->pads == NULL)
6388c2ecf20Sopenharmony_ci		return -ENOMEM;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	for (i = 0; i < num_pads - 1; ++i)
6418c2ecf20Sopenharmony_ci		entity->pads[i].flags = MEDIA_PAD_FL_SINK;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
6448c2ecf20Sopenharmony_ci				       sizeof(*entity->sources), GFP_KERNEL);
6458c2ecf20Sopenharmony_ci	if (entity->sources == NULL)
6468c2ecf20Sopenharmony_ci		return -ENOMEM;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* Single-pad entities only have a sink. */
6498c2ecf20Sopenharmony_ci	entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
6508c2ecf20Sopenharmony_ci					 : MEDIA_PAD_FL_SINK;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	/* Initialize the media entity. */
6538c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
6548c2ecf20Sopenharmony_ci				     entity->pads);
6558c2ecf20Sopenharmony_ci	if (ret < 0)
6568c2ecf20Sopenharmony_ci		return ret;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	/* Initialize the V4L2 subdev. */
6598c2ecf20Sopenharmony_ci	subdev = &entity->subdev;
6608c2ecf20Sopenharmony_ci	v4l2_subdev_init(subdev, ops);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	subdev->entity.function = function;
6638c2ecf20Sopenharmony_ci	subdev->entity.ops = &vsp1->media_ops;
6648c2ecf20Sopenharmony_ci	subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	snprintf(subdev->name, sizeof(subdev->name), "%s %s",
6678c2ecf20Sopenharmony_ci		 dev_name(vsp1->dev), name);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	vsp1_entity_init_cfg(subdev, NULL);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	/*
6728c2ecf20Sopenharmony_ci	 * Allocate the pad configuration to store formats and selection
6738c2ecf20Sopenharmony_ci	 * rectangles.
6748c2ecf20Sopenharmony_ci	 */
6758c2ecf20Sopenharmony_ci	entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
6768c2ecf20Sopenharmony_ci	if (entity->config == NULL) {
6778c2ecf20Sopenharmony_ci		media_entity_cleanup(&entity->subdev.entity);
6788c2ecf20Sopenharmony_ci		return -ENOMEM;
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	return 0;
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_civoid vsp1_entity_destroy(struct vsp1_entity *entity)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	if (entity->ops && entity->ops->destroy)
6878c2ecf20Sopenharmony_ci		entity->ops->destroy(entity);
6888c2ecf20Sopenharmony_ci	if (entity->subdev.ctrl_handler)
6898c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
6908c2ecf20Sopenharmony_ci	v4l2_subdev_free_pad_config(entity->config);
6918c2ecf20Sopenharmony_ci	media_entity_cleanup(&entity->subdev.entity);
6928c2ecf20Sopenharmony_ci}
693