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)
31 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
33 * @fmt: the pointer to struct v4l2_pix_format or
34 * struct v4l2_mbus_framefmt
36 * Entities must check if colorimetry given by the userspace is valid, if not
37 * then set them as DEFAULT
39 #define vimc_colorimetry_clamp(fmt) \
41 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
42 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
43 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
44 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
45 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
46 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
48 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
49 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
50 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
51 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
52 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
53 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
57 * struct vimc_platform_data - platform data to components
59 * @entity_name: The name of the entity to be created
61 * Board setup code will often provide additional information using the device's
62 * platform_data field to hold additional information.
63 * When injecting a new platform_device in the component system the core needs
64 * to provide to the corresponding submodules the name of the entity that should
65 * be used when registering the subdevice in the Media Controller system.
67 struct vimc_platform_data {
72 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
74 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
75 * @bbp: number of bytes each pixel occupies
76 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
78 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
79 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
89 * struct vimc_ent_device - core struct that represents a node in the topology
91 * @ent: the pointer to struct media_entity for the node
92 * @pads: the list of pads of the node
93 * @process_frame: callback send a frame to that node
94 * @vdev_get_format: callback that returns the current format a pad, used
95 * only when is_media_entity_v4l2_video_device(ent) returns
98 * Each node of the topology must create a vimc_ent_device struct. Depending on
99 * the node it will be of an instance of v4l2_subdev or video_device struct
100 * where both contains a struct media_entity.
101 * Those structures should embedded the vimc_ent_device struct through
102 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
103 * vimc_ent_device struct to be retrieved from the corresponding struct
106 struct vimc_ent_device {
107 struct media_entity *ent;
108 struct media_pad *pads;
109 void * (*process_frame)(struct vimc_ent_device *ved,
111 void (*vdev_get_format)(struct vimc_ent_device *ved,
112 struct v4l2_pix_format *fmt);
116 * struct vimc_device - main device for vimc driver
118 * @pdev pointer to the platform device
119 * @pipe_cfg pointer to the vimc pipeline configuration structure
120 * @ent_devs array of vimc_ent_device pointers
121 * @mdev the associated media_device parent
122 * @v4l2_dev Internal v4l2 parent device
125 struct platform_device pdev;
126 const struct vimc_pipeline_config *pipe_cfg;
127 struct vimc_ent_device **ent_devs;
128 struct media_device mdev;
129 struct v4l2_device v4l2_dev;
133 * struct vimc_ent_config Structure which describes individual
134 * configuration for each entity
137 * @ved pointer to vimc_ent_device (a node in the
139 * @add subdev add hook - initializes and registers
140 * subdev called from vimc-core
141 * @rm subdev rm hook - unregisters and frees
142 * subdev called from vimc-core
144 struct vimc_ent_config {
146 struct vimc_ent_device *(*add)(struct vimc_device *vimc,
147 const char *vcfg_name);
148 void (*rm)(struct vimc_device *vimc, struct vimc_ent_device *ved);
151 /* prototypes for vimc_ent_config add and rm hooks */
152 struct vimc_ent_device *vimc_cap_add(struct vimc_device *vimc,
153 const char *vcfg_name);
154 void vimc_cap_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
156 struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
157 const char *vcfg_name);
158 void vimc_deb_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
160 struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
161 const char *vcfg_name);
162 void vimc_sca_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
164 struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
165 const char *vcfg_name);
166 void vimc_sen_rm(struct vimc_device *vimc, struct vimc_ent_device *ved);
169 * vimc_pads_init - initialize pads
171 * @num_pads: number of pads to initialize
172 * @pads_flags: flags to use in each pad
174 * Helper functions to allocate/initialize pads
176 struct media_pad *vimc_pads_init(u16 num_pads,
177 const unsigned long *pads_flag);
180 * vimc_pads_cleanup - free pads
182 * @pads: pointer to the pads
184 * Helper function to free the pads initialized with vimc_pads_init
186 static inline void vimc_pads_cleanup(struct media_pad *pads)
192 * vimc_pipeline_s_stream - start stream through the pipeline
194 * @ent: the pointer to struct media_entity for the node
195 * @enable: 1 to start the stream and 0 to stop
197 * Helper function to call the s_stream of the subdevices connected
198 * in all the sink pads of the entity
200 int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
203 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
205 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
207 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
210 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
212 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
214 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
217 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
219 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
221 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
224 * vimc_ent_sd_register - initialize and register a subdev node
226 * @ved: the vimc_ent_device struct to be initialize
227 * @sd: the v4l2_subdev struct to be initialize and registered
228 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
229 * @name: name of the sub-device. Please notice that the name must be
231 * @function: media entity function defined by MEDIA_ENT_F_* macros
232 * @num_pads: number of pads to initialize
233 * @pads_flag: flags to use in each pad
234 * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
235 * @sd_ops: pointer to &struct v4l2_subdev_ops.
237 * Helper function initialize and register the struct vimc_ent_device and struct
238 * v4l2_subdev which represents a subdev node in the topology
240 int vimc_ent_sd_register(struct vimc_ent_device *ved,
241 struct v4l2_subdev *sd,
242 struct v4l2_device *v4l2_dev,
243 const char *const name,
246 const unsigned long *pads_flag,
247 const struct v4l2_subdev_internal_ops *sd_int_ops,
248 const struct v4l2_subdev_ops *sd_ops);
251 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
253 * @ved: the vimc_ent_device struct to be cleaned up
254 * @sd: the v4l2_subdev struct to be unregistered
256 * Helper function cleanup and unregister the struct vimc_ent_device and struct
257 * v4l2_subdev which represents a subdev node in the topology
259 void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
260 struct v4l2_subdev *sd);
263 * vimc_link_validate - validates a media link
265 * @link: pointer to &struct media_link
267 * This function calls validates if a media link is valid for streaming.
269 int vimc_link_validate(struct media_link *link);