1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * vimc-common.h Virtual Media Controller Driver
4 *
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6 */
7
8 #ifndef _VIMC_COMMON_H_
9 #define _VIMC_COMMON_H_
10
11 #include <linux/slab.h>
12 #include <media/media-device.h>
13 #include <media/v4l2-device.h>
14
15 #define VIMC_PDEV_NAME "vimc"
16
17 /* VIMC-specific controls */
18 #define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
19 #define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
20 #define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
21
22 #define VIMC_FRAME_MAX_WIDTH 4096
23 #define VIMC_FRAME_MAX_HEIGHT 2160
24 #define VIMC_FRAME_MIN_WIDTH 16
25 #define VIMC_FRAME_MIN_HEIGHT 16
26
27 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
28
29 /**
30 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
31 *
32 * @fmt: the pointer to struct v4l2_pix_format or
33 * struct v4l2_mbus_framefmt
34 *
35 * Entities must check if colorimetry given by the userspace is valid, if not
36 * then set them as DEFAULT
37 */
38 #define vimc_colorimetry_clamp(fmt) \
39 do { \
40 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
41 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
42 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
43 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
44 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
45 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
46 } \
47 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
48 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
49 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
50 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
51 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
52 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
53 } while (0)
54
55 /**
56 * struct vimc_platform_data - platform data to components
57 *
58 * @entity_name: The name of the entity to be created
59 *
60 * Board setup code will often provide additional information using the device's
61 * platform_data field to hold additional information.
62 * When injecting a new platform_device in the component system the core needs
63 * to provide to the corresponding submodules the name of the entity that should
64 * be used when registering the subdevice in the Media Controller system.
65 */
66 struct vimc_platform_data {
67 char entity_name[32];
68 };
69
70 /**
71 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
72 *
73 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
74 * @bbp: number of bytes each pixel occupies
75 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
76 *
77 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
78 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
79 */
80 struct vimc_pix_map {
81 unsigned int code;
82 unsigned int bpp;
83 u32 pixelformat;
84 bool bayer;
85 };
86
87 /**
88 * struct vimc_ent_device - core struct that represents a node in the topology
89 *
90 * @ent: the pointer to struct media_entity for the node
91 * @pads: the list of pads of the node
92 * @process_frame: callback send a frame to that node
93 * @vdev_get_format: callback that returns the current format a pad, used
94 * only when is_media_entity_v4l2_video_device(ent) returns
95 * true
96 *
97 * Each node of the topology must create a vimc_ent_device struct. Depending on
98 * the node it will be of an instance of v4l2_subdev or video_device struct
99 * where both contains a struct media_entity.
100 * Those structures should embedded the vimc_ent_device struct through
101 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
102 * vimc_ent_device struct to be retrieved from the corresponding struct
103 * media_entity
104 */
105 struct vimc_ent_device {
106 struct media_entity *ent;
107 struct media_pad *pads;
108 void * (*process_frame)(struct vimc_ent_device *ved,
109 const void *frame);
110 void (*vdev_get_format)(struct vimc_ent_device *ved,
111 struct v4l2_pix_format *fmt);
112 };
113
114 /**
115 * vimc_pads_init - initialize pads
116 *
117 * @num_pads: number of pads to initialize
118 * @pads_flags: flags to use in each pad
119 *
120 * Helper functions to allocate/initialize pads
121 */
122 struct media_pad *vimc_pads_init(u16 num_pads,
123 const unsigned long *pads_flag);
124
125 /**
126 * vimc_pads_cleanup - free pads
127 *
128 * @pads: pointer to the pads
129 *
130 * Helper function to free the pads initialized with vimc_pads_init
131 */
vimc_pads_cleanup(struct media_pad * pads)132 static inline void vimc_pads_cleanup(struct media_pad *pads)
133 {
134 kfree(pads);
135 }
136
137 /**
138 * vimc_pipeline_s_stream - start stream through the pipeline
139 *
140 * @ent: the pointer to struct media_entity for the node
141 * @enable: 1 to start the stream and 0 to stop
142 *
143 * Helper function to call the s_stream of the subdevices connected
144 * in all the sink pads of the entity
145 */
146 int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
147
148 /**
149 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
150 *
151 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
152 */
153 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
154
155 /**
156 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
157 *
158 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
159 */
160 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
161
162 /**
163 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
164 *
165 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
166 */
167 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
168
169 /**
170 * vimc_ent_sd_register - initialize and register a subdev node
171 *
172 * @ved: the vimc_ent_device struct to be initialize
173 * @sd: the v4l2_subdev struct to be initialize and registered
174 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
175 * @name: name of the sub-device. Please notice that the name must be
176 * unique.
177 * @function: media entity function defined by MEDIA_ENT_F_* macros
178 * @num_pads: number of pads to initialize
179 * @pads_flag: flags to use in each pad
180 * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
181 * @sd_ops: pointer to &struct v4l2_subdev_ops.
182 *
183 * Helper function initialize and register the struct vimc_ent_device and struct
184 * v4l2_subdev which represents a subdev node in the topology
185 */
186 int vimc_ent_sd_register(struct vimc_ent_device *ved,
187 struct v4l2_subdev *sd,
188 struct v4l2_device *v4l2_dev,
189 const char *const name,
190 u32 function,
191 u16 num_pads,
192 const unsigned long *pads_flag,
193 const struct v4l2_subdev_internal_ops *sd_int_ops,
194 const struct v4l2_subdev_ops *sd_ops);
195
196 /**
197 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
198 *
199 * @ved: the vimc_ent_device struct to be cleaned up
200 * @sd: the v4l2_subdev struct to be unregistered
201 *
202 * Helper function cleanup and unregister the struct vimc_ent_device and struct
203 * v4l2_subdev which represents a subdev node in the topology
204 */
205 void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
206 struct v4l2_subdev *sd);
207
208 /**
209 * vimc_link_validate - validates a media link
210 *
211 * @link: pointer to &struct media_link
212 *
213 * This function calls validates if a media link is valid for streaming.
214 */
215 int vimc_link_validate(struct media_link *link);
216
217 #endif
218