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)
23 #define VIMC_FRAME_MAX_WIDTH 4096
24 #define VIMC_FRAME_MAX_HEIGHT 2160
25 #define VIMC_FRAME_MIN_WIDTH 16
26 #define VIMC_FRAME_MIN_HEIGHT 16
28 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
30 /* Source and sink pad checks */
31 #define VIMC_IS_SRC(pad) (pad)
32 #define VIMC_IS_SINK(pad) (!(pad))
35 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
37 * @fmt: the pointer to struct v4l2_pix_format or
38 * struct v4l2_mbus_framefmt
40 * Entities must check if colorimetry given by the userspace is valid, if not
41 * then set them as DEFAULT
43 #define vimc_colorimetry_clamp(fmt) \
45 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
46 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
47 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
48 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
49 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
50 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
52 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
53 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
54 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
55 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
56 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
57 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
61 * struct vimc_platform_data - platform data to components
63 * @entity_name: The name of the entity to be created
65 * Board setup code will often provide additional information using the device's
66 * platform_data field to hold additional information.
67 * When injecting a new platform_device in the component system the core needs
68 * to provide to the corresponding submodules the name of the entity that should
69 * be used when registering the subdevice in the Media Controller system.
71 struct vimc_platform_data {
76 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
78 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
79 * @bbp: number of bytes each pixel occupies
80 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
82 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
83 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
93 * struct vimc_ent_device - core struct that represents a node in the topology
95 * @ent: the pointer to struct media_entity for the node
96 * @pads: the list of pads of the node
97 * @process_frame: callback send a frame to that node
98 * @vdev_get_format: callback that returns the current format a pad, used
99 * only when is_media_entity_v4l2_video_device(ent) returns
102 * Each node of the topology must create a vimc_ent_device struct. Depending on
103 * the node it will be of an instance of v4l2_subdev or video_device struct
104 * where both contains a struct media_entity.
105 * Those structures should embedded the vimc_ent_device struct through
106 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
107 * vimc_ent_device struct to be retrieved from the corresponding struct
110 struct vimc_ent_device {
111 struct media_entity *ent;
112 struct media_pad *pads;
113 void * (*process_frame)(struct vimc_ent_device *ved,
115 void (*vdev_get_format)(struct vimc_ent_device *ved,
116 struct v4l2_pix_format *fmt);
120 * struct vimc_device - main device for vimc driver
122 * @pdev pointer to the platform device
123 * @pipe_cfg pointer to the vimc pipeline configuration structure
124 * @ent_devs array of vimc_ent_device pointers
125 * @mdev the associated media_device parent
126 * @v4l2_dev Internal v4l2 parent device
129 struct platform_device pdev;
130 const struct vimc_pipeline_config *pipe_cfg;
131 struct vimc_ent_device **ent_devs;
132 struct media_device mdev;
133 struct v4l2_device v4l2_dev;
137 * struct vimc_ent_config Structure which describes individual
138 * configuration for each entity
141 * @ved pointer to vimc_ent_device (a node in the
143 * @add subdev add hook - initializes and registers
144 * subdev called from vimc-core
145 * @rm subdev rm hook - unregisters and frees
146 * subdev called from vimc-core
148 struct vimc_ent_config {
150 struct vimc_ent_device *(*add)(struct vimc_device *vimc,
151 const char *vcfg_name);
152 void (*rm)(struct vimc_device *vimc, struct vimc_ent_device *ved);
155 /* prototypes for vimc_ent_config add and rm hooks */
156 struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
157 const char *vcfg_name);
158 void vimc_cap_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
160 struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
161 const char *vcfg_name);
162 void vimc_deb_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
164 struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
165 const char *vcfg_name);
166 void vimc_sca_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
168 struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
169 const char *vcfg_name);
170 void vimc_sen_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
173 * vimc_pads_init - initialize pads
175 * @num_pads: number of pads to initialize
176 * @pads_flags: flags to use in each pad
178 * Helper functions to allocate/initialize pads
180 struct media_pad *vimc_pads_init(u16 num_pads,
181 const unsigned long *pads_flag);
184 * vimc_pads_cleanup - free pads
186 * @pads: pointer to the pads
188 * Helper function to free the pads initialized with vimc_pads_init
190 static inline void vimc_pads_cleanup(struct media_pad *pads)
196 * vimc_pipeline_s_stream - start stream through the pipeline
198 * @ent: the pointer to struct media_entity for the node
199 * @enable: 1 to start the stream and 0 to stop
201 * Helper function to call the s_stream of the subdevices connected
202 * in all the sink pads of the entity
204 int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
207 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
209 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
211 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
214 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
216 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
218 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
221 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
223 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
225 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
228 * vimc_ent_sd_register - initialize and register a subdev node
230 * @ved: the vimc_ent_device struct to be initialize
231 * @sd: the v4l2_subdev struct to be initialize and registered
232 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
233 * @name: name of the sub-device. Please notice that the name must be
235 * @function: media entity function defined by MEDIA_ENT_F_* macros
236 * @num_pads: number of pads to initialize
237 * @pads_flag: flags to use in each pad
238 * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
239 * @sd_ops: pointer to &struct v4l2_subdev_ops.
241 * Helper function initialize and register the struct vimc_ent_device and struct
242 * v4l2_subdev which represents a subdev node in the topology
244 int vimc_ent_sd_register(struct vimc_ent_device *ved,
245 struct v4l2_subdev *sd,
246 struct v4l2_device *v4l2_dev,
247 const char *const name,
250 const unsigned long *pads_flag,
251 const struct v4l2_subdev_internal_ops *sd_int_ops,
252 const struct v4l2_subdev_ops *sd_ops);
255 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
257 * @ved: the vimc_ent_device struct to be cleaned up
258 * @sd: the v4l2_subdev struct to be unregistered
260 * Helper function cleanup and unregister the struct vimc_ent_device and struct
261 * v4l2_subdev which represents a subdev node in the topology
263 void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
264 struct v4l2_subdev *sd);
267 * vimc_link_validate - validates a media link
269 * @link: pointer to &struct media_link
271 * This function calls validates if a media link is valid for streaming.
273 int vimc_link_validate(struct media_link *link);