18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * vimc-common.h Virtual Media Controller Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _VIMC_COMMON_H_
98c2ecf20Sopenharmony_ci#define _VIMC_COMMON_H_
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <media/media-device.h>
148c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define VIMC_PDEV_NAME "vimc"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* VIMC-specific controls */
198c2ecf20Sopenharmony_ci#define VIMC_CID_VIMC_BASE		(0x00f00000 | 0xf000)
208c2ecf20Sopenharmony_ci#define VIMC_CID_VIMC_CLASS		(0x00f00000 | 1)
218c2ecf20Sopenharmony_ci#define VIMC_CID_TEST_PATTERN		(VIMC_CID_VIMC_BASE + 0)
228c2ecf20Sopenharmony_ci#define VIMC_CID_MEAN_WIN_SIZE		(VIMC_CID_VIMC_BASE + 1)
238c2ecf20Sopenharmony_ci#define VIMC_CID_OSD_TEXT_MODE		(VIMC_CID_VIMC_BASE + 2)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define VIMC_FRAME_MAX_WIDTH 4096
268c2ecf20Sopenharmony_ci#define VIMC_FRAME_MAX_HEIGHT 2160
278c2ecf20Sopenharmony_ci#define VIMC_FRAME_MIN_WIDTH 16
288c2ecf20Sopenharmony_ci#define VIMC_FRAME_MIN_HEIGHT 16
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Source and sink pad checks */
338c2ecf20Sopenharmony_ci#define VIMC_IS_SRC(pad)	(pad)
348c2ecf20Sopenharmony_ci#define VIMC_IS_SINK(pad)	(!(pad))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define VIMC_PIX_FMT_MAX_CODES 8
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/**
398c2ecf20Sopenharmony_ci * vimc_colorimetry_clamp - Adjust colorimetry parameters
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * @fmt:		the pointer to struct v4l2_pix_format or
428c2ecf20Sopenharmony_ci *			struct v4l2_mbus_framefmt
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * Entities must check if colorimetry given by the userspace is valid, if not
458c2ecf20Sopenharmony_ci * then set them as DEFAULT
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci#define vimc_colorimetry_clamp(fmt)					\
488c2ecf20Sopenharmony_cido {									\
498c2ecf20Sopenharmony_ci	if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT		\
508c2ecf20Sopenharmony_ci	    || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {		\
518c2ecf20Sopenharmony_ci		(fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;		\
528c2ecf20Sopenharmony_ci		(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;		\
538c2ecf20Sopenharmony_ci		(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;	\
548c2ecf20Sopenharmony_ci		(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;		\
558c2ecf20Sopenharmony_ci	}								\
568c2ecf20Sopenharmony_ci	if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)		\
578c2ecf20Sopenharmony_ci		(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;		\
588c2ecf20Sopenharmony_ci	if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)		\
598c2ecf20Sopenharmony_ci		(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;	\
608c2ecf20Sopenharmony_ci	if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)		\
618c2ecf20Sopenharmony_ci		(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;		\
628c2ecf20Sopenharmony_ci} while (0)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/**
658c2ecf20Sopenharmony_ci * struct vimc_pix_map - maps media bus code with v4l2 pixel format
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * @code:		media bus format code defined by MEDIA_BUS_FMT_* macros
688c2ecf20Sopenharmony_ci * @bpp:		number of bytes each pixel occupies
698c2ecf20Sopenharmony_ci * @pixelformat:	pixel format defined by V4L2_PIX_FMT_* macros
708c2ecf20Sopenharmony_ci * @bayer:		true if this is a bayer format
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
738c2ecf20Sopenharmony_ci * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistruct vimc_pix_map {
768c2ecf20Sopenharmony_ci	unsigned int code[VIMC_PIX_FMT_MAX_CODES];
778c2ecf20Sopenharmony_ci	unsigned int bpp;
788c2ecf20Sopenharmony_ci	u32 pixelformat;
798c2ecf20Sopenharmony_ci	bool bayer;
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/**
838c2ecf20Sopenharmony_ci * struct vimc_ent_device - core struct that represents an entity in the
848c2ecf20Sopenharmony_ci * topology
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * @dev:		a pointer of the device struct of the driver
878c2ecf20Sopenharmony_ci * @ent:		the pointer to struct media_entity for the node
888c2ecf20Sopenharmony_ci * @process_frame:	callback send a frame to that node
898c2ecf20Sopenharmony_ci * @vdev_get_format:	callback that returns the current format a pad, used
908c2ecf20Sopenharmony_ci *			only when is_media_entity_v4l2_video_device(ent) returns
918c2ecf20Sopenharmony_ci *			true
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * Each node of the topology must create a vimc_ent_device struct. Depending on
948c2ecf20Sopenharmony_ci * the node it will be of an instance of v4l2_subdev or video_device struct
958c2ecf20Sopenharmony_ci * where both contains a struct media_entity.
968c2ecf20Sopenharmony_ci * Those structures should embedded the vimc_ent_device struct through
978c2ecf20Sopenharmony_ci * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the
988c2ecf20Sopenharmony_ci * vimc_ent_device struct to be retrieved from the corresponding struct
998c2ecf20Sopenharmony_ci * media_entity
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistruct vimc_ent_device {
1028c2ecf20Sopenharmony_ci	struct device *dev;
1038c2ecf20Sopenharmony_ci	struct media_entity *ent;
1048c2ecf20Sopenharmony_ci	void * (*process_frame)(struct vimc_ent_device *ved,
1058c2ecf20Sopenharmony_ci				const void *frame);
1068c2ecf20Sopenharmony_ci	void (*vdev_get_format)(struct vimc_ent_device *ved,
1078c2ecf20Sopenharmony_ci			      struct v4l2_pix_format *fmt);
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/**
1118c2ecf20Sopenharmony_ci * struct vimc_device - main device for vimc driver
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * @pipe_cfg:	pointer to the vimc pipeline configuration structure
1148c2ecf20Sopenharmony_ci * @ent_devs:	array of vimc_ent_device pointers
1158c2ecf20Sopenharmony_ci * @mdev:	the associated media_device parent
1168c2ecf20Sopenharmony_ci * @v4l2_dev:	Internal v4l2 parent device
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_cistruct vimc_device {
1198c2ecf20Sopenharmony_ci	const struct vimc_pipeline_config *pipe_cfg;
1208c2ecf20Sopenharmony_ci	struct vimc_ent_device **ent_devs;
1218c2ecf20Sopenharmony_ci	struct media_device mdev;
1228c2ecf20Sopenharmony_ci	struct v4l2_device v4l2_dev;
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/**
1268c2ecf20Sopenharmony_ci * struct vimc_ent_type		Structure for the callbacks of the entity types
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci *
1298c2ecf20Sopenharmony_ci * @add:			initializes and registers
1308c2ecf20Sopenharmony_ci *				vimc entity - called from vimc-core
1318c2ecf20Sopenharmony_ci * @unregister:			unregisters vimc entity - called from vimc-core
1328c2ecf20Sopenharmony_ci * @release:			releases vimc entity - called from the v4l2_dev
1338c2ecf20Sopenharmony_ci *				release callback
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cistruct vimc_ent_type {
1368c2ecf20Sopenharmony_ci	struct vimc_ent_device *(*add)(struct vimc_device *vimc,
1378c2ecf20Sopenharmony_ci				       const char *vcfg_name);
1388c2ecf20Sopenharmony_ci	void (*unregister)(struct vimc_ent_device *ved);
1398c2ecf20Sopenharmony_ci	void (*release)(struct vimc_ent_device *ved);
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * struct vimc_ent_config	Structure which describes individual
1448c2ecf20Sopenharmony_ci *				configuration for each entity
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * @name:			entity name
1478c2ecf20Sopenharmony_ci * @type:			contain the callbacks of this entity type
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistruct vimc_ent_config {
1518c2ecf20Sopenharmony_ci	const char *name;
1528c2ecf20Sopenharmony_ci	struct vimc_ent_type *type;
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/**
1568c2ecf20Sopenharmony_ci * vimc_is_source - returns true if the entity has only source pads
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * @ent: pointer to &struct media_entity
1598c2ecf20Sopenharmony_ci *
1608c2ecf20Sopenharmony_ci */
1618c2ecf20Sopenharmony_cibool vimc_is_source(struct media_entity *ent);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciextern struct vimc_ent_type vimc_sen_type;
1648c2ecf20Sopenharmony_ciextern struct vimc_ent_type vimc_deb_type;
1658c2ecf20Sopenharmony_ciextern struct vimc_ent_type vimc_sca_type;
1668c2ecf20Sopenharmony_ciextern struct vimc_ent_type vimc_cap_type;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/**
1698c2ecf20Sopenharmony_ci * vimc_pix_map_by_index - get vimc_pix_map struct by its index
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * @i:			index of the vimc_pix_map struct in vimc_pix_map_list
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_ciconst struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/**
1768c2ecf20Sopenharmony_ci * vimc_mbus_code_by_index - get mbus code by its index
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * @index:		index of the mbus code in vimc_pix_map_list
1798c2ecf20Sopenharmony_ci *
1808c2ecf20Sopenharmony_ci * Returns 0 if no mbus code is found for the given index.
1818c2ecf20Sopenharmony_ci */
1828c2ecf20Sopenharmony_ciu32 vimc_mbus_code_by_index(unsigned int index);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/**
1858c2ecf20Sopenharmony_ci * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * @code:		media bus format code defined by MEDIA_BUS_FMT_* macros
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_ciconst struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/**
1928c2ecf20Sopenharmony_ci * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
1938c2ecf20Sopenharmony_ci *
1948c2ecf20Sopenharmony_ci * @pixelformat:	pixel format defined by V4L2_PIX_FMT_* macros
1958c2ecf20Sopenharmony_ci */
1968c2ecf20Sopenharmony_ciconst struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/**
1998c2ecf20Sopenharmony_ci * vimc_ent_sd_register - initialize and register a subdev node
2008c2ecf20Sopenharmony_ci *
2018c2ecf20Sopenharmony_ci * @ved:	the vimc_ent_device struct to be initialize
2028c2ecf20Sopenharmony_ci * @sd:		the v4l2_subdev struct to be initialize and registered
2038c2ecf20Sopenharmony_ci * @v4l2_dev:	the v4l2 device to register the v4l2_subdev
2048c2ecf20Sopenharmony_ci * @name:	name of the sub-device. Please notice that the name must be
2058c2ecf20Sopenharmony_ci *		unique.
2068c2ecf20Sopenharmony_ci * @function:	media entity function defined by MEDIA_ENT_F_* macros
2078c2ecf20Sopenharmony_ci * @num_pads:	number of pads to initialize
2088c2ecf20Sopenharmony_ci * @pads:	the array of pads of the entity, the caller should set the
2098c2ecf20Sopenharmony_ci *		flags of the pads
2108c2ecf20Sopenharmony_ci * @sd_ops:	pointer to &struct v4l2_subdev_ops.
2118c2ecf20Sopenharmony_ci *
2128c2ecf20Sopenharmony_ci * Helper function initialize and register the struct vimc_ent_device and struct
2138c2ecf20Sopenharmony_ci * v4l2_subdev which represents a subdev node in the topology
2148c2ecf20Sopenharmony_ci */
2158c2ecf20Sopenharmony_ciint vimc_ent_sd_register(struct vimc_ent_device *ved,
2168c2ecf20Sopenharmony_ci			 struct v4l2_subdev *sd,
2178c2ecf20Sopenharmony_ci			 struct v4l2_device *v4l2_dev,
2188c2ecf20Sopenharmony_ci			 const char *const name,
2198c2ecf20Sopenharmony_ci			 u32 function,
2208c2ecf20Sopenharmony_ci			 u16 num_pads,
2218c2ecf20Sopenharmony_ci			 struct media_pad *pads,
2228c2ecf20Sopenharmony_ci			 const struct v4l2_subdev_ops *sd_ops);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/**
2258c2ecf20Sopenharmony_ci * vimc_vdev_link_validate - validates a media link
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * @link: pointer to &struct media_link
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * This function calls validates if a media link is valid for streaming.
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_ciint vimc_vdev_link_validate(struct media_link *link);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci#endif
234