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 
16 #define VIMC_PDEV_NAME "vimc"
17 
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)
23 
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
28 
29 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
30 
31 /* Source and sink pad checks */
32 #define VIMC_IS_SRC(pad)	(pad)
33 #define VIMC_IS_SINK(pad)	(!(pad))
34 
35 #define VIMC_PIX_FMT_MAX_CODES 8
36 
37 /**
38  * vimc_colorimetry_clamp - Adjust colorimetry parameters
39  *
40  * @fmt:		the pointer to struct v4l2_pix_format or
41  *			struct v4l2_mbus_framefmt
42  *
43  * Entities must check if colorimetry given by the userspace is valid, if not
44  * then set them as DEFAULT
45  */
46 #define vimc_colorimetry_clamp(fmt)					\
47 do {									\
48 	if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT		\
49 	    || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {		\
50 		(fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;		\
51 		(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;		\
52 		(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;	\
53 		(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;		\
54 	}								\
55 	if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)		\
56 		(fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;		\
57 	if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)		\
58 		(fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;	\
59 	if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)		\
60 		(fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;		\
61 } while (0)
62 
63 /**
64  * struct vimc_pix_map - maps media bus code with v4l2 pixel format
65  *
66  * @code:		media bus format code defined by MEDIA_BUS_FMT_* macros
67  * @bpp:		number of bytes each pixel occupies
68  * @pixelformat:	pixel format defined by V4L2_PIX_FMT_* macros
69  * @bayer:		true if this is a bayer format
70  *
71  * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
72  * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
73  */
74 struct vimc_pix_map {
75 	unsigned int code[VIMC_PIX_FMT_MAX_CODES];
76 	unsigned int bpp;
77 	u32 pixelformat;
78 	bool bayer;
79 };
80 
81 /**
82  * struct vimc_ent_device - core struct that represents an entity in the
83  * topology
84  *
85  * @dev:		a pointer of the device struct of the driver
86  * @ent:		the pointer to struct media_entity for the node
87  * @process_frame:	callback send a frame to that node
88  * @vdev_get_format:	callback that returns the current format a pad, used
89  *			only when is_media_entity_v4l2_video_device(ent) returns
90  *			true
91  *
92  * Each node of the topology must create a vimc_ent_device struct. Depending on
93  * the node it will be of an instance of v4l2_subdev or video_device struct
94  * where both contains a struct media_entity.
95  * Those structures should embedded the vimc_ent_device struct through
96  * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the
97  * vimc_ent_device struct to be retrieved from the corresponding struct
98  * media_entity
99  */
100 struct vimc_ent_device {
101 	struct device *dev;
102 	struct media_entity *ent;
103 	void * (*process_frame)(struct vimc_ent_device *ved,
104 				const void *frame);
105 	void (*vdev_get_format)(struct vimc_ent_device *ved,
106 			      struct v4l2_pix_format *fmt);
107 };
108 
109 /**
110  * struct vimc_device - main device for vimc driver
111  *
112  * @pipe_cfg:	pointer to the vimc pipeline configuration structure
113  * @ent_devs:	array of vimc_ent_device pointers
114  * @mdev:	the associated media_device parent
115  * @v4l2_dev:	Internal v4l2 parent device
116  */
117 struct vimc_device {
118 	const struct vimc_pipeline_config *pipe_cfg;
119 	struct vimc_ent_device **ent_devs;
120 	struct media_device mdev;
121 	struct v4l2_device v4l2_dev;
122 };
123 
124 /**
125  * struct vimc_ent_type		Structure for the callbacks of the entity types
126  *
127  *
128  * @add:			initializes and registers
129  *				vimc entity - called from vimc-core
130  * @unregister:			unregisters vimc entity - called from vimc-core
131  * @release:			releases vimc entity - called from the v4l2_dev
132  *				release callback
133  */
134 struct vimc_ent_type {
135 	struct vimc_ent_device *(*add)(struct vimc_device *vimc,
136 				       const char *vcfg_name);
137 	void (*unregister)(struct vimc_ent_device *ved);
138 	void (*release)(struct vimc_ent_device *ved);
139 };
140 
141 /**
142  * struct vimc_ent_config	Structure which describes individual
143  *				configuration for each entity
144  *
145  * @name:			entity name
146  * @type:			contain the callbacks of this entity type
147  *
148  */
149 struct vimc_ent_config {
150 	const char *name;
151 	struct vimc_ent_type *type;
152 };
153 
154 /**
155  * vimc_is_source - returns true if the entity has only source pads
156  *
157  * @ent: pointer to &struct media_entity
158  *
159  */
160 bool vimc_is_source(struct media_entity *ent);
161 
162 extern struct vimc_ent_type vimc_sen_type;
163 extern struct vimc_ent_type vimc_deb_type;
164 extern struct vimc_ent_type vimc_sca_type;
165 extern struct vimc_ent_type vimc_cap_type;
166 
167 /**
168  * vimc_pix_map_by_index - get vimc_pix_map struct by its index
169  *
170  * @i:			index of the vimc_pix_map struct in vimc_pix_map_list
171  */
172 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
173 
174 /**
175  * vimc_mbus_code_by_index - get mbus code by its index
176  *
177  * @index:		index of the mbus code in vimc_pix_map_list
178  *
179  * Returns 0 if no mbus code is found for the given index.
180  */
181 u32 vimc_mbus_code_by_index(unsigned int index);
182 
183 /**
184  * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
185  *
186  * @code:		media bus format code defined by MEDIA_BUS_FMT_* macros
187  */
188 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
189 
190 /**
191  * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
192  *
193  * @pixelformat:	pixel format defined by V4L2_PIX_FMT_* macros
194  */
195 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
196 
197 /**
198  * vimc_ent_sd_register - initialize and register a subdev node
199  *
200  * @ved:	the vimc_ent_device struct to be initialize
201  * @sd:		the v4l2_subdev struct to be initialize and registered
202  * @v4l2_dev:	the v4l2 device to register the v4l2_subdev
203  * @name:	name of the sub-device. Please notice that the name must be
204  *		unique.
205  * @function:	media entity function defined by MEDIA_ENT_F_* macros
206  * @num_pads:	number of pads to initialize
207  * @pads:	the array of pads of the entity, the caller should set the
208  *		flags of the pads
209  * @sd_ops:	pointer to &struct v4l2_subdev_ops.
210  *
211  * Helper function initialize and register the struct vimc_ent_device and struct
212  * v4l2_subdev which represents a subdev node in the topology
213  */
214 int vimc_ent_sd_register(struct vimc_ent_device *ved,
215 			 struct v4l2_subdev *sd,
216 			 struct v4l2_device *v4l2_dev,
217 			 const char *const name,
218 			 u32 function,
219 			 u16 num_pads,
220 			 struct media_pad *pads,
221 			 const struct v4l2_subdev_ops *sd_ops);
222 
223 /**
224  * vimc_vdev_link_validate - validates a media link
225  *
226  * @link: pointer to &struct media_link
227  *
228  * This function calls validates if a media link is valid for streaming.
229  */
230 int vimc_vdev_link_validate(struct media_link *link);
231 
232 #endif
233