xref: /linux/drivers/media/test-drivers/vimc/vimc-common.h (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
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/platform_device.h>
12 #include <linux/slab.h>
13 #include <media/media-device.h>
14 #include <media/v4l2-device.h>
15 #include <media/tpg/v4l2-tpg.h>
16 #include <media/v4l2-ctrls.h>
17 
18 #define VIMC_PDEV_NAME "vimc"
19 
20 /* VIMC-specific controls */
21 #define VIMC_CID_VIMC_BASE		(0x00f00000 | 0xf000)
22 #define VIMC_CID_VIMC_CLASS		(0x00f00000 | 1)
23 #define VIMC_CID_TEST_PATTERN		(VIMC_CID_VIMC_BASE + 0)
24 #define VIMC_CID_MEAN_WIN_SIZE		(VIMC_CID_VIMC_BASE + 1)
25 #define VIMC_CID_OSD_TEXT_MODE		(VIMC_CID_VIMC_BASE + 2)
26 
27 #define VIMC_FRAME_MAX_WIDTH 4096
28 #define VIMC_FRAME_MAX_HEIGHT 2160
29 #define VIMC_FRAME_MIN_WIDTH 16
30 #define VIMC_FRAME_MIN_HEIGHT 16
31 
32 #define VIMC_PIXEL_RATE_FIXED		160000000	/* 160 MHz */
33 #define VIMC_HBLANK_FIXED		800
34 /* VBLANK - vertical blanking (primary FPS control) */
35 #define VIMC_VBLANK_MIN			4
36 #define VIMC_VBLANK_MAX			65535
37 #define VIMC_VBLANK_STEP		1
38 #define VIMC_VBLANK_DEFAULT	        3223           /* 30fps vga */
39 #define VIMC_PIXELS_THRESHOLD_30FPS	(1920 * 1080) /* 2073600 pixels */
40 
41 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
42 
43 /* Source and sink pad checks */
44 #define VIMC_IS_SRC(pad)	(pad)
45 #define VIMC_IS_SINK(pad)	(!(pad))
46 
47 #define VIMC_PIX_FMT_MAX_CODES 8
48 
49 extern unsigned int vimc_allocator;
50 
51 enum vimc_allocator_type {
52 	VIMC_ALLOCATOR_VMALLOC = 0,
53 	VIMC_ALLOCATOR_DMA_CONTIG = 1,
54 };
55 
56 /**
57  * vimc_colorimetry_clamp - Adjust colorimetry parameters
58  *
59  * @fmt:		the pointer to struct v4l2_pix_format or
60  *			struct v4l2_mbus_framefmt
61  *
62  * Entities must check if colorimetry given by the userspace is valid, if not
63  * then set them as DEFAULT
64  */
65 #define vimc_colorimetry_clamp(fmt)					\
66 do {									\
67 	if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT		\
68 	    || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {		\
69 		(fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;		\
70 		(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;		\
71 		(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;	\
72 		(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;		\
73 	}								\
74 	if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)		\
75 		(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;		\
76 	if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)		\
77 		(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;	\
78 	if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)		\
79 		(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;		\
80 } while (0)
81 
82 /**
83  * struct vimc_pix_map - maps media bus code with v4l2 pixel format
84  *
85  * @code:		media bus format code defined by MEDIA_BUS_FMT_* macros
86  * @bpp:		number of bytes each pixel occupies
87  * @pixelformat:	pixel format defined by V4L2_PIX_FMT_* macros
88  * @bayer:		true if this is a bayer format
89  *
90  * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
91  * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
92  */
93 struct vimc_pix_map {
94 	unsigned int code[VIMC_PIX_FMT_MAX_CODES];
95 	unsigned int bpp;
96 	u32 pixelformat;
97 	bool bayer;
98 };
99 
100 /**
101  * struct vimc_ent_device - core struct that represents an entity in the
102  * topology
103  *
104  * @dev:		a pointer of the device struct of the driver
105  * @ent:		the pointer to struct media_entity for the node
106  * @process_frame:	callback send a frame to that node
107  * @vdev_get_format:	callback that returns the current format a pad, used
108  *			only when is_media_entity_v4l2_video_device(ent) returns
109  *			true
110  *
111  * Each node of the topology must create a vimc_ent_device struct. Depending on
112  * the node it will be of an instance of v4l2_subdev or video_device struct
113  * where both contains a struct media_entity.
114  * Those structures should embedded the vimc_ent_device struct through
115  * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the
116  * vimc_ent_device struct to be retrieved from the corresponding struct
117  * media_entity
118  */
119 struct vimc_ent_device {
120 	struct device *dev;
121 	struct media_entity *ent;
122 	void * (*process_frame)(struct vimc_ent_device *ved,
123 				const void *frame);
124 	void (*vdev_get_format)(struct vimc_ent_device *ved,
125 			      struct v4l2_pix_format *fmt);
126 };
127 
128 /**
129  * struct vimc_device - main device for vimc driver
130  *
131  * @pipe_cfg:	pointer to the vimc pipeline configuration structure
132  * @ent_devs:	array of vimc_ent_device pointers
133  * @mdev:	the associated media_device parent
134  * @v4l2_dev:	Internal v4l2 parent device
135  */
136 struct vimc_device {
137 	const struct vimc_pipeline_config *pipe_cfg;
138 	struct vimc_ent_device **ent_devs;
139 	struct media_device mdev;
140 	struct v4l2_device v4l2_dev;
141 };
142 
143 /**
144  * struct vimc_ent_type		Structure for the callbacks of the entity types
145  *
146  *
147  * @add:			initializes and registers
148  *				vimc entity - called from vimc-core
149  * @unregister:			unregisters vimc entity - called from vimc-core
150  * @release:			releases vimc entity - called from the v4l2_dev
151  *				release callback
152  */
153 struct vimc_ent_type {
154 	struct vimc_ent_device *(*add)(struct vimc_device *vimc,
155 				       const char *vcfg_name);
156 	void (*unregister)(struct vimc_ent_device *ved);
157 	void (*release)(struct vimc_ent_device *ved);
158 };
159 
160 /**
161  * struct vimc_ent_config	Structure which describes individual
162  *				configuration for each entity
163  *
164  * @name:			entity name
165  * @type:			contain the callbacks of this entity type
166  *
167  */
168 struct vimc_ent_config {
169 	const char *name;
170 	const struct vimc_ent_type *type;
171 };
172 
173 enum vimc_sensor_osd_mode {
174 	VIMC_SENSOR_OSD_SHOW_ALL = 0,
175 	VIMC_SENSOR_OSD_SHOW_COUNTERS = 1,
176 	VIMC_SENSOR_OSD_SHOW_NONE = 2
177 };
178 
179 struct vimc_sensor_device {
180 	struct vimc_ent_device ved;
181 	struct v4l2_subdev sd;
182 	struct tpg_data tpg;
183 	struct v4l2_ctrl_handler hdl;
184 	struct media_pad pad;
185 	struct v4l2_ctrl *pixel_rate;
186 	struct v4l2_ctrl *hblank;
187 	struct v4l2_ctrl *vblank;
188 
189 	u8 *frame;
190 
191 	/*
192 	 * Virtual "hardware" configuration, filled when the stream starts or
193 	 * when controls are set.
194 	 */
195 	struct {
196 		struct v4l2_area size;
197 		enum vimc_sensor_osd_mode osd_value;
198 		u64 start_stream_ts;
199 		unsigned long fps_jiffies;
200 	} hw;
201 };
202 
203 /**
204  * vimc_is_source - returns true if the entity has only source pads
205  *
206  * @ent: pointer to &struct media_entity
207  *
208  */
209 bool vimc_is_source(struct media_entity *ent);
210 
211 extern const struct vimc_ent_type vimc_sensor_type;
212 extern const struct vimc_ent_type vimc_debayer_type;
213 extern const struct vimc_ent_type vimc_scaler_type;
214 extern const struct vimc_ent_type vimc_capture_type;
215 extern const struct vimc_ent_type vimc_lens_type;
216 
217 /**
218  * vimc_pix_map_by_index - get vimc_pix_map struct by its index
219  *
220  * @i:			index of the vimc_pix_map struct in vimc_pix_map_list
221  */
222 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
223 
224 /**
225  * vimc_mbus_code_by_index - get mbus code by its index
226  *
227  * @index:		index of the mbus code in vimc_pix_map_list
228  *
229  * Returns 0 if no mbus code is found for the given index.
230  */
231 u32 vimc_mbus_code_by_index(unsigned int index);
232 
233 /**
234  * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
235  *
236  * @code:		media bus format code defined by MEDIA_BUS_FMT_* macros
237  */
238 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
239 
240 /**
241  * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
242  *
243  * @pixelformat:	pixel format defined by V4L2_PIX_FMT_* macros
244  */
245 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
246 
247 /**
248  * vimc_ent_sd_register - initialize and register a subdev node
249  *
250  * @ved:	the vimc_ent_device struct to be initialize
251  * @sd:		the v4l2_subdev struct to be initialize and registered
252  * @v4l2_dev:	the v4l2 device to register the v4l2_subdev
253  * @name:	name of the sub-device. Please notice that the name must be
254  *		unique.
255  * @function:	media entity function defined by MEDIA_ENT_F_* macros
256  * @num_pads:	number of pads to initialize
257  * @pads:	the array of pads of the entity, the caller should set the
258  *		flags of the pads
259  * @int_ops:	pointer to &struct v4l2_subdev_internal_ops.
260  * @sd_ops:	pointer to &struct v4l2_subdev_ops.
261  *
262  * Helper function initialize and register the struct vimc_ent_device and struct
263  * v4l2_subdev which represents a subdev node in the topology
264  */
265 int vimc_ent_sd_register(struct vimc_ent_device *ved,
266 			 struct v4l2_subdev *sd,
267 			 struct v4l2_device *v4l2_dev,
268 			 const char *const name,
269 			 u32 function,
270 			 u16 num_pads,
271 			 struct media_pad *pads,
272 			 const struct v4l2_subdev_internal_ops *int_ops,
273 			 const struct v4l2_subdev_ops *sd_ops);
274 
275 /**
276  * vimc_vdev_link_validate - validates a media link
277  *
278  * @link: pointer to &struct media_link
279  *
280  * This function calls validates if a media link is valid for streaming.
281  */
282 int vimc_vdev_link_validate(struct media_link *link);
283 
284 #endif
285