1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * vimc-common.h Virtual Media Controller Driver
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
8 #ifndef _VIMC_COMMON_H_
9 #define _VIMC_COMMON_H_
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <media/media-device.h>
14 #include <media/v4l2-device.h>
16 #define VIMC_PDEV_NAME "vimc"
18 /* VIMC-specific controls */
19 #define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
20 #define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
21 #define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
22 #define VIMC_CID_MEAN_WIN_SIZE (VIMC_CID_VIMC_BASE + 1)
24 #define VIMC_FRAME_MAX_WIDTH 4096
25 #define VIMC_FRAME_MAX_HEIGHT 2160
26 #define VIMC_FRAME_MIN_WIDTH 16
27 #define VIMC_FRAME_MIN_HEIGHT 16
29 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
31 /* Source and sink pad checks */
32 #define VIMC_IS_SRC(pad) (pad)
33 #define VIMC_IS_SINK(pad) (!(pad))
36 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
38 * @fmt: the pointer to struct v4l2_pix_format or
39 * struct v4l2_mbus_framefmt
41 * Entities must check if colorimetry given by the userspace is valid, if not
42 * then set them as DEFAULT
44 #define vimc_colorimetry_clamp(fmt) \
46 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
47 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
48 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
49 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
50 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
51 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
53 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
54 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
55 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
56 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
57 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
58 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
62 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
64 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
65 * @bbp: number of bytes each pixel occupies
66 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
68 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
69 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
79 * struct vimc_ent_device - core struct that represents an entity in the
82 * @dev: a pointer of the device struct of the driver
83 * @ent: the pointer to struct media_entity for the node
84 * @process_frame: callback send a frame to that node
85 * @vdev_get_format: callback that returns the current format a pad, used
86 * only when is_media_entity_v4l2_video_device(ent) returns
89 * Each node of the topology must create a vimc_ent_device struct. Depending on
90 * the node it will be of an instance of v4l2_subdev or video_device struct
91 * where both contains a struct media_entity.
92 * Those structures should embedded the vimc_ent_device struct through
93 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
94 * vimc_ent_device struct to be retrieved from the corresponding struct
97 struct vimc_ent_device
{
99 struct media_entity
*ent
;
100 void * (*process_frame
)(struct vimc_ent_device
*ved
,
102 void (*vdev_get_format
)(struct vimc_ent_device
*ved
,
103 struct v4l2_pix_format
*fmt
);
107 * struct vimc_device - main device for vimc driver
109 * @pdev pointer to the platform device
110 * @pipe_cfg pointer to the vimc pipeline configuration structure
111 * @ent_devs array of vimc_ent_device pointers
112 * @mdev the associated media_device parent
113 * @v4l2_dev Internal v4l2 parent device
116 struct platform_device pdev
;
117 const struct vimc_pipeline_config
*pipe_cfg
;
118 struct vimc_ent_device
**ent_devs
;
119 struct media_device mdev
;
120 struct v4l2_device v4l2_dev
;
124 * struct vimc_ent_config Structure which describes individual
125 * configuration for each entity
128 * @ved pointer to vimc_ent_device (a node in the
130 * @add subdev add hook - initializes and registers
131 * subdev called from vimc-core
132 * @rm subdev rm hook - unregisters and frees
133 * subdev called from vimc-core
135 struct vimc_ent_config
{
137 struct vimc_ent_device
*(*add
)(struct vimc_device
*vimc
,
138 const char *vcfg_name
);
139 void (*rm
)(struct vimc_device
*vimc
, struct vimc_ent_device
*ved
);
143 * vimc_is_source - returns true if the entity has only source pads
145 * @ent: pointer to &struct media_entity
148 bool vimc_is_source(struct media_entity
*ent
);
150 /* prototypes for vimc_ent_config add and rm hooks */
151 struct vimc_ent_device
*vimc_cap_add(struct vimc_device
*vimc
,
152 const char *vcfg_name
);
153 void vimc_cap_rm(struct vimc_device
*vimc
, struct vimc_ent_device
*ved
);
155 struct vimc_ent_device
*vimc_deb_add(struct vimc_device
*vimc
,
156 const char *vcfg_name
);
157 void vimc_deb_rm(struct vimc_device
*vimc
, struct vimc_ent_device
*ved
);
159 struct vimc_ent_device
*vimc_sca_add(struct vimc_device
*vimc
,
160 const char *vcfg_name
);
161 void vimc_sca_rm(struct vimc_device
*vimc
, struct vimc_ent_device
*ved
);
163 struct vimc_ent_device
*vimc_sen_add(struct vimc_device
*vimc
,
164 const char *vcfg_name
);
165 void vimc_sen_rm(struct vimc_device
*vimc
, struct vimc_ent_device
*ved
);
168 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
170 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
172 const struct vimc_pix_map
*vimc_pix_map_by_index(unsigned int i
);
175 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
177 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
179 const struct vimc_pix_map
*vimc_pix_map_by_code(u32 code
);
182 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
184 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
186 const struct vimc_pix_map
*vimc_pix_map_by_pixelformat(u32 pixelformat
);
189 * vimc_ent_sd_register - initialize and register a subdev node
191 * @ved: the vimc_ent_device struct to be initialize
192 * @sd: the v4l2_subdev struct to be initialize and registered
193 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
194 * @name: name of the sub-device. Please notice that the name must be
196 * @function: media entity function defined by MEDIA_ENT_F_* macros
197 * @num_pads: number of pads to initialize
198 * @pads: the array of pads of the entity, the caller should set the
200 * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
201 * @sd_ops: pointer to &struct v4l2_subdev_ops.
203 * Helper function initialize and register the struct vimc_ent_device and struct
204 * v4l2_subdev which represents a subdev node in the topology
206 int vimc_ent_sd_register(struct vimc_ent_device
*ved
,
207 struct v4l2_subdev
*sd
,
208 struct v4l2_device
*v4l2_dev
,
209 const char *const name
,
212 struct media_pad
*pads
,
213 const struct v4l2_subdev_internal_ops
*sd_int_ops
,
214 const struct v4l2_subdev_ops
*sd_ops
);
217 * vimc_vdev_link_validate - validates a media link
219 * @link: pointer to &struct media_link
221 * This function calls validates if a media link is valid for streaming.
223 int vimc_vdev_link_validate(struct media_link
*link
);