1 /*
2 * vimc-common.h Virtual Media Controller Driver
3 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18 #ifndef _VIMC_COMMON_H_
19 #define _VIMC_COMMON_H_
20
21 #include <linux/slab.h>
22 #include <media/media-device.h>
23 #include <media/v4l2-device.h>
24
25 /* VIMC-specific controls */
26 #define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
27 #define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
28 #define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
29
30 #define VIMC_FRAME_MAX_WIDTH 4096
31 #define VIMC_FRAME_MAX_HEIGHT 2160
32 #define VIMC_FRAME_MIN_WIDTH 16
33 #define VIMC_FRAME_MIN_HEIGHT 16
34
35 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
36
37 /**
38 * struct 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_platform_data - platform data to components
65 *
66 * @entity_name: The name of the entity to be created
67 *
68 * Board setup code will often provide additional information using the device's
69 * platform_data field to hold additional information.
70 * When injecting a new platform_device in the component system the core needs
71 * to provide to the corresponding submodules the name of the entity that should
72 * be used when registering the subdevice in the Media Controller system.
73 */
74 struct vimc_platform_data {
75 char entity_name[32];
76 };
77
78 /**
79 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
80 *
81 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
82 * @bbp: number of bytes each pixel occupies
83 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
84 *
85 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
86 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
87 */
88 struct vimc_pix_map {
89 unsigned int code;
90 unsigned int bpp;
91 u32 pixelformat;
92 bool bayer;
93 };
94
95 /**
96 * struct vimc_ent_device - core struct that represents a node in the topology
97 *
98 * @ent: the pointer to struct media_entity for the node
99 * @pads: the list of pads of the node
100 * @process_frame: callback send a frame to that node
101 * @vdev_get_format: callback that returns the current format a pad, used
102 * only when is_media_entity_v4l2_video_device(ent) returns
103 * true
104 *
105 * Each node of the topology must create a vimc_ent_device struct. Depending on
106 * the node it will be of an instance of v4l2_subdev or video_device struct
107 * where both contains a struct media_entity.
108 * Those structures should embedded the vimc_ent_device struct through
109 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
110 * vimc_ent_device struct to be retrieved from the corresponding struct
111 * media_entity
112 */
113 struct vimc_ent_device {
114 struct media_entity *ent;
115 struct media_pad *pads;
116 void (*process_frame)(struct vimc_ent_device *ved,
117 struct media_pad *sink, const void *frame);
118 void (*vdev_get_format)(struct vimc_ent_device *ved,
119 struct v4l2_pix_format *fmt);
120 };
121
122 /**
123 * vimc_propagate_frame - propagate a frame through the topology
124 *
125 * @src: the source pad where the frame is being originated
126 * @frame: the frame to be propagated
127 *
128 * This function will call the process_frame callback from the vimc_ent_device
129 * struct of the nodes directly connected to the @src pad
130 */
131 int vimc_propagate_frame(struct media_pad *src, const void *frame);
132
133 /**
134 * vimc_pads_init - initialize pads
135 *
136 * @num_pads: number of pads to initialize
137 * @pads_flags: flags to use in each pad
138 *
139 * Helper functions to allocate/initialize pads
140 */
141 struct media_pad *vimc_pads_init(u16 num_pads,
142 const unsigned long *pads_flag);
143
144 /**
145 * vimc_pads_cleanup - free pads
146 *
147 * @pads: pointer to the pads
148 *
149 * Helper function to free the pads initialized with vimc_pads_init
150 */
vimc_pads_cleanup(struct media_pad * pads)151 static inline void vimc_pads_cleanup(struct media_pad *pads)
152 {
153 kfree(pads);
154 }
155
156 /**
157 * vimc_pipeline_s_stream - start stream through the pipeline
158 *
159 * @ent: the pointer to struct media_entity for the node
160 * @enable: 1 to start the stream and 0 to stop
161 *
162 * Helper function to call the s_stream of the subdevices connected
163 * in all the sink pads of the entity
164 */
165 int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
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_pix_map_by_code - get vimc_pix_map struct by media bus code
176 *
177 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
178 */
179 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
180
181 /**
182 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
183 *
184 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
185 */
186 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
187
188 /**
189 * vimc_ent_sd_register - initialize and register a subdev node
190 *
191 * @ved: the vimc_ent_device struct to be initialize
192 * @sd: the v4l2_subdev struct to be initialize and registered
193 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
194 * @name: name of the sub-device. Please notice that the name must be
195 * unique.
196 * @function: media entity function defined by MEDIA_ENT_F_* macros
197 * @num_pads: number of pads to initialize
198 * @pads_flag: flags to use in each pad
199 * @sd_ops: pointer to &struct v4l2_subdev_ops.
200 *
201 * Helper function initialize and register the struct vimc_ent_device and struct
202 * v4l2_subdev which represents a subdev node in the topology
203 */
204 int vimc_ent_sd_register(struct vimc_ent_device *ved,
205 struct v4l2_subdev *sd,
206 struct v4l2_device *v4l2_dev,
207 const char *const name,
208 u32 function,
209 u16 num_pads,
210 const unsigned long *pads_flag,
211 const struct v4l2_subdev_ops *sd_ops);
212
213 /**
214 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
215 *
216 * @ved: the vimc_ent_device struct to be cleaned up
217 * @sd: the v4l2_subdev struct to be unregistered
218 *
219 * Helper function cleanup and unregister the struct vimc_ent_device and struct
220 * v4l2_subdev which represents a subdev node in the topology
221 */
222 void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
223 struct v4l2_subdev *sd);
224
225 /**
226 * vimc_link_validate - validates a media link
227 *
228 * @link: pointer to &struct media_link
229 *
230 * This function calls validates if a media link is valid for streaming.
231 */
232 int vimc_link_validate(struct media_link *link);
233
234 #endif
235