xref: /linux/drivers/media/v4l2-core/v4l2-common.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Video for Linux Two
4  *
5  *	A generic video device interface for the LINUX operating system
6  *	using a set of device structures/vectors for low level operations.
7  *
8  *	This file replaces the videodev.c file that comes with the
9  *	regular kernel distribution.
10  *
11  * Author:	Bill Dirks <bill@thedirks.org>
12  *		based on code by Alan Cox, <alan@cymru.net>
13  */
14 
15 /*
16  * Video capture interface for Linux
17  *
18  *	A generic video device interface for the LINUX operating system
19  *	using a set of device structures/vectors for low level operations.
20  *
21  * Author:	Alan Cox, <alan@lxorguk.ukuu.org.uk>
22  *
23  * Fixes:
24  */
25 
26 /*
27  * Video4linux 1/2 integration by Justin Schoeman
28  * <justin@suntiger.ee.up.ac.za>
29  * 2.4 PROCFS support ported from 2.4 kernels by
30  *  Iñaki García Etxebarria <garetxe@euskalnet.net>
31  * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
32  * 2.4 devfs support ported from 2.4 kernels by
33  *  Dan Merillat <dan@merillat.org>
34  * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
35  */
36 
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/uaccess.h>
44 #include <asm/io.h>
45 #include <asm/div64.h>
46 #include <media/v4l2-common.h>
47 #include <media/v4l2-device.h>
48 #include <media/v4l2-ctrls.h>
49 
50 #include <linux/videodev2.h>
51 
52 /*
53  *
54  *	V 4 L 2   D R I V E R   H E L P E R   A P I
55  *
56  */
57 
58 /*
59  *  Video Standard Operations (contributed by Michael Schimek)
60  */
61 
62 /* Helper functions for control handling			     */
63 
64 /* Fill in a struct v4l2_queryctrl */
65 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _step, s32 _def)
66 {
67 	const char *name;
68 	s64 min = _min;
69 	s64 max = _max;
70 	u64 step = _step;
71 	s64 def = _def;
72 
73 	v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
74 		       &min, &max, &step, &def, &qctrl->flags);
75 
76 	if (name == NULL)
77 		return -EINVAL;
78 
79 	qctrl->minimum = min;
80 	qctrl->maximum = max;
81 	qctrl->step = step;
82 	qctrl->default_value = def;
83 	qctrl->reserved[0] = qctrl->reserved[1] = 0;
84 	strscpy(qctrl->name, name, sizeof(qctrl->name));
85 	return 0;
86 }
87 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
88 
89 /* Clamp x to be between min and max, aligned to a multiple of 2^align.  min
90  * and max don't have to be aligned, but there must be at least one valid
91  * value.  E.g., min=17,max=31,align=4 is not allowed as there are no multiples
92  * of 16 between 17 and 31.  */
93 static unsigned int clamp_align(unsigned int x, unsigned int min,
94 				unsigned int max, unsigned int align)
95 {
96 	/* Bits that must be zero to be aligned */
97 	unsigned int mask = ~((1 << align) - 1);
98 
99 	/* Clamp to aligned min and max */
100 	x = clamp(x, (min + ~mask) & mask, max & mask);
101 
102 	/* Round to nearest aligned value */
103 	if (align)
104 		x = (x + (1 << (align - 1))) & mask;
105 
106 	return x;
107 }
108 
109 static unsigned int clamp_roundup(unsigned int x, unsigned int min,
110 				   unsigned int max, unsigned int alignment)
111 {
112 	x = clamp(x, min, max);
113 	if (alignment)
114 		x = round_up(x, alignment);
115 
116 	return x;
117 }
118 
119 void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
120 			   unsigned int walign,
121 			   u32 *h, unsigned int hmin, unsigned int hmax,
122 			   unsigned int halign, unsigned int salign)
123 {
124 	*w = clamp_align(*w, wmin, wmax, walign);
125 	*h = clamp_align(*h, hmin, hmax, halign);
126 
127 	/* Usually we don't need to align the size and are done now. */
128 	if (!salign)
129 		return;
130 
131 	/* How much alignment do we have? */
132 	walign = __ffs(*w);
133 	halign = __ffs(*h);
134 	/* Enough to satisfy the image alignment? */
135 	if (walign + halign < salign) {
136 		/* Max walign where there is still a valid width */
137 		unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
138 		/* Max halign where there is still a valid height */
139 		unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
140 
141 		/* up the smaller alignment until we have enough */
142 		do {
143 			if (halign >= hmaxa ||
144 			    (walign <= halign && walign < wmaxa)) {
145 				*w = clamp_align(*w, wmin, wmax, walign + 1);
146 				walign = __ffs(*w);
147 			} else {
148 				*h = clamp_align(*h, hmin, hmax, halign + 1);
149 				halign = __ffs(*h);
150 			}
151 		} while (halign + walign < salign);
152 	}
153 }
154 EXPORT_SYMBOL_GPL(v4l_bound_align_image);
155 
156 const void *
157 __v4l2_find_nearest_size_conditional(const void *array, size_t array_size,
158 				     size_t entry_size, size_t width_offset,
159 				     size_t height_offset, s32 width,
160 				     s32 height,
161 				     bool (*func)(const void *array,
162 						  size_t index,
163 						  const void *context),
164 				     const void *context)
165 {
166 	u32 error, min_error = U32_MAX;
167 	const void *best = NULL;
168 	size_t i;
169 
170 	if (!array)
171 		return NULL;
172 
173 	for (i = 0; i < array_size; i++, array += entry_size) {
174 		const u32 *entry_width = array + width_offset;
175 		const u32 *entry_height = array + height_offset;
176 
177 		if (func && !func(array, i, context))
178 			continue;
179 
180 		error = abs(*entry_width - width) + abs(*entry_height - height);
181 		if (error > min_error)
182 			continue;
183 
184 		min_error = error;
185 		best = array;
186 		if (!error)
187 			break;
188 	}
189 
190 	return best;
191 }
192 EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size_conditional);
193 
194 int v4l2_g_parm_cap(struct video_device *vdev,
195 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a)
196 {
197 	struct v4l2_subdev_frame_interval ival = { 0 };
198 	int ret;
199 
200 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
201 	    a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
202 		return -EINVAL;
203 
204 	if (vdev->device_caps & V4L2_CAP_READWRITE)
205 		a->parm.capture.readbuffers = 2;
206 	if (v4l2_subdev_has_op(sd, pad, get_frame_interval))
207 		a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
208 	ret = v4l2_subdev_call_state_active(sd, pad, get_frame_interval, &ival);
209 	if (!ret)
210 		a->parm.capture.timeperframe = ival.interval;
211 	return ret;
212 }
213 EXPORT_SYMBOL_GPL(v4l2_g_parm_cap);
214 
215 int v4l2_s_parm_cap(struct video_device *vdev,
216 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a)
217 {
218 	struct v4l2_subdev_frame_interval ival = {
219 		.interval = a->parm.capture.timeperframe
220 	};
221 	int ret;
222 
223 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
224 	    a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
225 		return -EINVAL;
226 
227 	memset(&a->parm, 0, sizeof(a->parm));
228 	if (vdev->device_caps & V4L2_CAP_READWRITE)
229 		a->parm.capture.readbuffers = 2;
230 	else
231 		a->parm.capture.readbuffers = 0;
232 
233 	if (v4l2_subdev_has_op(sd, pad, get_frame_interval))
234 		a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
235 	ret = v4l2_subdev_call_state_active(sd, pad, set_frame_interval, &ival);
236 	if (!ret)
237 		a->parm.capture.timeperframe = ival.interval;
238 	return ret;
239 }
240 EXPORT_SYMBOL_GPL(v4l2_s_parm_cap);
241 
242 const struct v4l2_format_info *v4l2_format_info(u32 format)
243 {
244 	static const struct v4l2_format_info formats[] = {
245 		/* RGB formats */
246 		{ .format = V4L2_PIX_FMT_BGR24,   .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
247 		{ .format = V4L2_PIX_FMT_RGB24,   .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
248 		{ .format = V4L2_PIX_FMT_HSV24,   .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
249 		{ .format = V4L2_PIX_FMT_BGR32,   .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
250 		{ .format = V4L2_PIX_FMT_XBGR32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
251 		{ .format = V4L2_PIX_FMT_BGRX32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
252 		{ .format = V4L2_PIX_FMT_RGB32,   .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
253 		{ .format = V4L2_PIX_FMT_XRGB32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
254 		{ .format = V4L2_PIX_FMT_RGBX32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
255 		{ .format = V4L2_PIX_FMT_HSV32,   .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
256 		{ .format = V4L2_PIX_FMT_ARGB32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
257 		{ .format = V4L2_PIX_FMT_RGBA32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
258 		{ .format = V4L2_PIX_FMT_ABGR32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
259 		{ .format = V4L2_PIX_FMT_BGRA32,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
260 		{ .format = V4L2_PIX_FMT_RGB565,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
261 		{ .format = V4L2_PIX_FMT_RGB565X, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
262 		{ .format = V4L2_PIX_FMT_RGB555,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
263 		{ .format = V4L2_PIX_FMT_BGR666,  .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
264 		{ .format = V4L2_PIX_FMT_BGR48_12, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 6, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
265 		{ .format = V4L2_PIX_FMT_BGR48, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 6, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
266 		{ .format = V4L2_PIX_FMT_RGB48, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 6, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
267 		{ .format = V4L2_PIX_FMT_ABGR64_12, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 8, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
268 		{ .format = V4L2_PIX_FMT_RGBA1010102, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
269 		{ .format = V4L2_PIX_FMT_RGBX1010102, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
270 		{ .format = V4L2_PIX_FMT_ARGB2101010, .pixel_enc = V4L2_PIXEL_ENC_RGB, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
271 
272 		/* YUV packed formats */
273 		{ .format = V4L2_PIX_FMT_YUYV,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
274 		{ .format = V4L2_PIX_FMT_YVYU,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
275 		{ .format = V4L2_PIX_FMT_UYVY,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
276 		{ .format = V4L2_PIX_FMT_VYUY,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
277 		{ .format = V4L2_PIX_FMT_Y210,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
278 		{ .format = V4L2_PIX_FMT_Y212,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
279 		{ .format = V4L2_PIX_FMT_Y216,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 4, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
280 		{ .format = V4L2_PIX_FMT_YUV48_12, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 6, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
281 		{ .format = V4L2_PIX_FMT_MT2110T, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 5, 10, 0, 0 }, .bpp_div = { 4, 4, 1, 1 }, .hdiv = 2, .vdiv = 2,
282 		  .block_w = { 16, 8, 0, 0 }, .block_h = { 32, 16, 0, 0 }},
283 		{ .format = V4L2_PIX_FMT_MT2110R, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 5, 10, 0, 0 }, .bpp_div = { 4, 4, 1, 1 }, .hdiv = 2, .vdiv = 2,
284 		  .block_w = { 16, 8, 0, 0 }, .block_h = { 32, 16, 0, 0 }},
285 
286 		/* YUV planar formats */
287 		{ .format = V4L2_PIX_FMT_NV12,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
288 		{ .format = V4L2_PIX_FMT_NV21,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
289 		{ .format = V4L2_PIX_FMT_NV15,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 5, 10, 0, 0 }, .bpp_div = { 4, 4, 1, 1 }, .hdiv = 2, .vdiv = 2 },
290 		{ .format = V4L2_PIX_FMT_NV16,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
291 		{ .format = V4L2_PIX_FMT_NV61,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
292 		{ .format = V4L2_PIX_FMT_NV20,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 5, 10, 0, 0 }, .bpp_div = { 4, 4, 1, 1 }, .hdiv = 2, .vdiv = 1 },
293 		{ .format = V4L2_PIX_FMT_NV24,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
294 		{ .format = V4L2_PIX_FMT_NV42,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
295 		{ .format = V4L2_PIX_FMT_P010,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 2, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
296 		{ .format = V4L2_PIX_FMT_P012,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 2, 4, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
297 
298 		{ .format = V4L2_PIX_FMT_YUV410,  .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 4, .vdiv = 4 },
299 		{ .format = V4L2_PIX_FMT_YVU410,  .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 4, .vdiv = 4 },
300 		{ .format = V4L2_PIX_FMT_YUV411P, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 4, .vdiv = 1 },
301 		{ .format = V4L2_PIX_FMT_YUV420,  .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
302 		{ .format = V4L2_PIX_FMT_YVU420,  .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
303 		{ .format = V4L2_PIX_FMT_YUV422P, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
304 		{ .format = V4L2_PIX_FMT_GREY,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
305 
306 		/* Tiled YUV formats */
307 		{ .format = V4L2_PIX_FMT_NV12_4L4, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
308 		{ .format = V4L2_PIX_FMT_NV15_4L4, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 5, 10, 0, 0 }, .bpp_div = { 4, 4, 1, 1 }, .hdiv = 2, .vdiv = 2,
309 		  .block_w = { 4, 2, 0, 0 }, .block_h = { 1, 1, 0, 0 }},
310 		{ .format = V4L2_PIX_FMT_P010_4L4, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 2, 4, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
311 
312 		/* YUV planar formats, non contiguous variant */
313 		{ .format = V4L2_PIX_FMT_YUV420M, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 3, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
314 		{ .format = V4L2_PIX_FMT_YVU420M, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 3, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
315 		{ .format = V4L2_PIX_FMT_YUV422M, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 3, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
316 		{ .format = V4L2_PIX_FMT_YVU422M, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 3, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
317 		{ .format = V4L2_PIX_FMT_YUV444M, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 3, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
318 		{ .format = V4L2_PIX_FMT_YVU444M, .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 3, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
319 
320 		{ .format = V4L2_PIX_FMT_NV12M,   .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
321 		{ .format = V4L2_PIX_FMT_NV21M,   .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
322 		{ .format = V4L2_PIX_FMT_NV16M,   .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
323 		{ .format = V4L2_PIX_FMT_NV61M,   .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
324 		{ .format = V4L2_PIX_FMT_P012M,   .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 2, 4, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
325 
326 		/* Tiled YUV formats, non contiguous variant */
327 		{ .format = V4L2_PIX_FMT_NV12MT,        .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2,
328 		  .block_w = { 64, 32, 0, 0 },	.block_h = { 32, 16, 0, 0 }},
329 		{ .format = V4L2_PIX_FMT_NV12MT_16X16,  .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 2, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2,
330 		  .block_w = { 16,  8, 0, 0 },	.block_h = { 16,  8, 0, 0 }},
331 
332 		/* Bayer RGB formats */
333 		{ .format = V4L2_PIX_FMT_SBGGR8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
334 		{ .format = V4L2_PIX_FMT_SGBRG8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
335 		{ .format = V4L2_PIX_FMT_SGRBG8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
336 		{ .format = V4L2_PIX_FMT_SRGGB8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
337 		{ .format = V4L2_PIX_FMT_SBGGR10,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
338 		{ .format = V4L2_PIX_FMT_SGBRG10,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
339 		{ .format = V4L2_PIX_FMT_SGRBG10,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
340 		{ .format = V4L2_PIX_FMT_SRGGB10,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
341 		{ .format = V4L2_PIX_FMT_SBGGR10P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 5, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
342 		{ .format = V4L2_PIX_FMT_SGBRG10P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 5, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
343 		{ .format = V4L2_PIX_FMT_SGRBG10P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 5, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
344 		{ .format = V4L2_PIX_FMT_SRGGB10P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 5, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
345 		{ .format = V4L2_PIX_FMT_SBGGR10ALAW8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
346 		{ .format = V4L2_PIX_FMT_SGBRG10ALAW8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
347 		{ .format = V4L2_PIX_FMT_SGRBG10ALAW8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
348 		{ .format = V4L2_PIX_FMT_SRGGB10ALAW8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
349 		{ .format = V4L2_PIX_FMT_SBGGR10DPCM8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
350 		{ .format = V4L2_PIX_FMT_SGBRG10DPCM8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
351 		{ .format = V4L2_PIX_FMT_SGRBG10DPCM8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
352 		{ .format = V4L2_PIX_FMT_SRGGB10DPCM8,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 1, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
353 		{ .format = V4L2_PIX_FMT_SBGGR12,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
354 		{ .format = V4L2_PIX_FMT_SGBRG12,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
355 		{ .format = V4L2_PIX_FMT_SGRBG12,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
356 		{ .format = V4L2_PIX_FMT_SRGGB12,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
357 		{ .format = V4L2_PIX_FMT_SBGGR12P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 2, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
358 		{ .format = V4L2_PIX_FMT_SGBRG12P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 2, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
359 		{ .format = V4L2_PIX_FMT_SGRBG12P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 2, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
360 		{ .format = V4L2_PIX_FMT_SRGGB12P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 3, 0, 0, 0 }, .bpp_div = { 2, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
361 		{ .format = V4L2_PIX_FMT_SBGGR14,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
362 		{ .format = V4L2_PIX_FMT_SGBRG14,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
363 		{ .format = V4L2_PIX_FMT_SGRBG14,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
364 		{ .format = V4L2_PIX_FMT_SRGGB14,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
365 		{ .format = V4L2_PIX_FMT_SBGGR14P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 7, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
366 		{ .format = V4L2_PIX_FMT_SGBRG14P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 7, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
367 		{ .format = V4L2_PIX_FMT_SGRBG14P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 7, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
368 		{ .format = V4L2_PIX_FMT_SRGGB14P,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 7, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
369 		{ .format = V4L2_PIX_FMT_SBGGR16,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
370 		{ .format = V4L2_PIX_FMT_SGBRG16,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
371 		{ .format = V4L2_PIX_FMT_SGRBG16,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
372 		{ .format = V4L2_PIX_FMT_SRGGB16,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 2, 0, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
373 
374 		/* Renesas Camera Data Receiver Unit formats, bayer order agnostic */
375 		{ .format = V4L2_PIX_FMT_RAW_CRU10,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 8, 0, 0, 0 }, .bpp_div = { 6, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
376 		{ .format = V4L2_PIX_FMT_RAW_CRU12,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 8, 0, 0, 0 }, .bpp_div = { 5, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
377 		{ .format = V4L2_PIX_FMT_RAW_CRU14,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 8, 0, 0, 0 }, .bpp_div = { 4, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
378 		{ .format = V4L2_PIX_FMT_RAW_CRU20,	.pixel_enc = V4L2_PIXEL_ENC_BAYER, .mem_planes = 1, .comp_planes = 1, .bpp = { 8, 0, 0, 0 }, .bpp_div = { 3, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
379 	};
380 	unsigned int i;
381 
382 	for (i = 0; i < ARRAY_SIZE(formats); ++i)
383 		if (formats[i].format == format)
384 			return &formats[i];
385 	return NULL;
386 }
387 EXPORT_SYMBOL(v4l2_format_info);
388 
389 static inline unsigned int v4l2_format_block_width(const struct v4l2_format_info *info, int plane)
390 {
391 	if (!info->block_w[plane])
392 		return 1;
393 	return info->block_w[plane];
394 }
395 
396 static inline unsigned int v4l2_format_block_height(const struct v4l2_format_info *info, int plane)
397 {
398 	if (!info->block_h[plane])
399 		return 1;
400 	return info->block_h[plane];
401 }
402 
403 static inline unsigned int v4l2_format_plane_stride(const struct v4l2_format_info *info, int plane,
404 						    unsigned int width)
405 {
406 	unsigned int hdiv = plane ? info->hdiv : 1;
407 	unsigned int aligned_width =
408 		ALIGN(width, v4l2_format_block_width(info, plane));
409 
410 	return DIV_ROUND_UP(aligned_width, hdiv) *
411 	       info->bpp[plane] / info->bpp_div[plane];
412 }
413 
414 static inline unsigned int v4l2_format_plane_height(const struct v4l2_format_info *info, int plane,
415 						    unsigned int height)
416 {
417 	unsigned int vdiv = plane ? info->vdiv : 1;
418 	unsigned int aligned_height =
419 		ALIGN(height, v4l2_format_block_height(info, plane));
420 
421 	return DIV_ROUND_UP(aligned_height, vdiv);
422 }
423 
424 static inline unsigned int v4l2_format_plane_size(const struct v4l2_format_info *info, int plane,
425 						  unsigned int width, unsigned int height)
426 {
427 	return v4l2_format_plane_stride(info, plane, width) *
428 	       v4l2_format_plane_height(info, plane, height);
429 }
430 
431 void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,
432 				    const struct v4l2_frmsize_stepwise *frmsize)
433 {
434 	if (!frmsize)
435 		return;
436 
437 	/*
438 	 * Clamp width/height to meet min/max constraints and round it up to
439 	 * macroblock alignment.
440 	 */
441 	*width = clamp_roundup(*width, frmsize->min_width, frmsize->max_width,
442 			       frmsize->step_width);
443 	*height = clamp_roundup(*height, frmsize->min_height, frmsize->max_height,
444 				frmsize->step_height);
445 }
446 EXPORT_SYMBOL_GPL(v4l2_apply_frmsize_constraints);
447 
448 int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt,
449 			u32 pixelformat, u32 width, u32 height)
450 {
451 	const struct v4l2_format_info *info;
452 	struct v4l2_plane_pix_format *plane;
453 	int i;
454 
455 	info = v4l2_format_info(pixelformat);
456 	if (!info)
457 		return -EINVAL;
458 
459 	pixfmt->width = width;
460 	pixfmt->height = height;
461 	pixfmt->pixelformat = pixelformat;
462 	pixfmt->num_planes = info->mem_planes;
463 
464 	if (info->mem_planes == 1) {
465 		plane = &pixfmt->plane_fmt[0];
466 		plane->bytesperline = v4l2_format_plane_stride(info, 0, width);
467 		plane->sizeimage = 0;
468 
469 		for (i = 0; i < info->comp_planes; i++)
470 			plane->sizeimage +=
471 				v4l2_format_plane_size(info, i, width, height);
472 	} else {
473 		for (i = 0; i < info->comp_planes; i++) {
474 			plane = &pixfmt->plane_fmt[i];
475 			plane->bytesperline =
476 				v4l2_format_plane_stride(info, i, width);
477 			plane->sizeimage = plane->bytesperline *
478 				v4l2_format_plane_height(info, i, height);
479 		}
480 	}
481 	return 0;
482 }
483 EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt_mp);
484 
485 int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
486 		     u32 width, u32 height)
487 {
488 	const struct v4l2_format_info *info;
489 	int i;
490 
491 	info = v4l2_format_info(pixelformat);
492 	if (!info)
493 		return -EINVAL;
494 
495 	/* Single planar API cannot be used for multi plane formats. */
496 	if (info->mem_planes > 1)
497 		return -EINVAL;
498 
499 	pixfmt->width = width;
500 	pixfmt->height = height;
501 	pixfmt->pixelformat = pixelformat;
502 	pixfmt->bytesperline = v4l2_format_plane_stride(info, 0, width);
503 	pixfmt->sizeimage = 0;
504 
505 	for (i = 0; i < info->comp_planes; i++)
506 		pixfmt->sizeimage +=
507 			v4l2_format_plane_size(info, i, width, height);
508 	return 0;
509 }
510 EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt);
511 
512 s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
513 			      unsigned int mul, unsigned int div)
514 {
515 	struct v4l2_ctrl *ctrl;
516 	s64 freq;
517 
518 	ctrl = v4l2_ctrl_find(handler, V4L2_CID_LINK_FREQ);
519 	if (ctrl) {
520 		struct v4l2_querymenu qm = { .id = V4L2_CID_LINK_FREQ };
521 		int ret;
522 
523 		qm.index = v4l2_ctrl_g_ctrl(ctrl);
524 
525 		ret = v4l2_querymenu(handler, &qm);
526 		if (ret)
527 			return -ENOENT;
528 
529 		freq = qm.value;
530 	} else {
531 		if (!mul || !div)
532 			return -ENOENT;
533 
534 		ctrl = v4l2_ctrl_find(handler, V4L2_CID_PIXEL_RATE);
535 		if (!ctrl)
536 			return -ENOENT;
537 
538 		freq = div_u64(v4l2_ctrl_g_ctrl_int64(ctrl) * mul, div);
539 
540 		pr_warn_once("%s: Link frequency estimated using pixel rate: result might be inaccurate\n",
541 			     __func__);
542 		pr_warn_once("%s: Consider implementing support for V4L2_CID_LINK_FREQ in the transmitter driver\n",
543 			     __func__);
544 	}
545 
546 	return freq > 0 ? freq : -EINVAL;
547 }
548 EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
549 
550 #ifdef CONFIG_MEDIA_CONTROLLER
551 s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
552 			     unsigned int div)
553 {
554 	struct v4l2_mbus_config mbus_config = {};
555 	struct v4l2_subdev *sd;
556 	int ret;
557 
558 	sd = media_entity_to_v4l2_subdev(pad->entity);
559 	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
560 			       &mbus_config);
561 	if (ret < 0 && ret != -ENOIOCTLCMD)
562 		return ret;
563 
564 	if (mbus_config.link_freq)
565 		return mbus_config.link_freq;
566 
567 	/*
568 	 * Fall back to using the link frequency control if the media bus config
569 	 * doesn't provide a link frequency.
570 	 */
571 	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
572 }
573 EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
574 #endif /* CONFIG_MEDIA_CONTROLLER */
575 
576 /*
577  * Simplify a fraction using a simple continued fraction decomposition. The
578  * idea here is to convert fractions such as 333333/10000000 to 1/30 using
579  * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
580  * arbitrary parameters to remove non-significative terms from the simple
581  * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
582  * respectively seems to give nice results.
583  */
584 void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
585 		unsigned int n_terms, unsigned int threshold)
586 {
587 	u32 *an;
588 	u32 x, y, r;
589 	unsigned int i, n;
590 
591 	an = kmalloc_array(n_terms, sizeof(*an), GFP_KERNEL);
592 	if (an == NULL)
593 		return;
594 
595 	/*
596 	 * Convert the fraction to a simple continued fraction. See
597 	 * https://en.wikipedia.org/wiki/Continued_fraction
598 	 * Stop if the current term is bigger than or equal to the given
599 	 * threshold.
600 	 */
601 	x = *numerator;
602 	y = *denominator;
603 
604 	for (n = 0; n < n_terms && y != 0; ++n) {
605 		an[n] = x / y;
606 		if (an[n] >= threshold) {
607 			if (n < 2)
608 				n++;
609 			break;
610 		}
611 
612 		r = x - an[n] * y;
613 		x = y;
614 		y = r;
615 	}
616 
617 	/* Expand the simple continued fraction back to an integer fraction. */
618 	x = 0;
619 	y = 1;
620 
621 	for (i = n; i > 0; --i) {
622 		r = y;
623 		y = an[i-1] * y + x;
624 		x = r;
625 	}
626 
627 	*numerator = y;
628 	*denominator = x;
629 	kfree(an);
630 }
631 EXPORT_SYMBOL_GPL(v4l2_simplify_fraction);
632 
633 /*
634  * Convert a fraction to a frame interval in 100ns multiples. The idea here is
635  * to compute numerator / denominator * 10000000 using 32 bit fixed point
636  * arithmetic only.
637  */
638 u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator)
639 {
640 	u32 multiplier;
641 
642 	/* Saturate the result if the operation would overflow. */
643 	if (denominator == 0 ||
644 	    numerator/denominator >= ((u32)-1)/10000000)
645 		return (u32)-1;
646 
647 	/*
648 	 * Divide both the denominator and the multiplier by two until
649 	 * numerator * multiplier doesn't overflow. If anyone knows a better
650 	 * algorithm please let me know.
651 	 */
652 	multiplier = 10000000;
653 	while (numerator > ((u32)-1)/multiplier) {
654 		multiplier /= 2;
655 		denominator /= 2;
656 	}
657 
658 	return denominator ? numerator * multiplier / denominator : 0;
659 }
660 EXPORT_SYMBOL_GPL(v4l2_fraction_to_interval);
661 
662 int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
663 			     unsigned int num_of_fw_link_freqs,
664 			     const s64 *driver_link_freqs,
665 			     unsigned int num_of_driver_link_freqs,
666 			     unsigned long *bitmap)
667 {
668 	unsigned int i;
669 
670 	*bitmap = 0;
671 
672 	if (!num_of_fw_link_freqs) {
673 		dev_err(dev, "no link frequencies in firmware\n");
674 		return -ENODATA;
675 	}
676 
677 	for (i = 0; i < num_of_fw_link_freqs; i++) {
678 		unsigned int j;
679 
680 		for (j = 0; j < num_of_driver_link_freqs; j++) {
681 			if (fw_link_freqs[i] != driver_link_freqs[j])
682 				continue;
683 
684 			dev_dbg(dev, "enabling link frequency %lld Hz\n",
685 				driver_link_freqs[j]);
686 			*bitmap |= BIT(j);
687 			break;
688 		}
689 	}
690 
691 	if (!*bitmap) {
692 		dev_err(dev, "no matching link frequencies found\n");
693 
694 		dev_dbg(dev, "specified in firmware:\n");
695 		for (i = 0; i < num_of_fw_link_freqs; i++)
696 			dev_dbg(dev, "\t%llu Hz\n", fw_link_freqs[i]);
697 
698 		dev_dbg(dev, "driver supported:\n");
699 		for (i = 0; i < num_of_driver_link_freqs; i++)
700 			dev_dbg(dev, "\t%lld Hz\n", driver_link_freqs[i]);
701 
702 		return -ENOENT;
703 	}
704 
705 	return 0;
706 }
707 EXPORT_SYMBOL_GPL(v4l2_link_freq_to_bitmap);
708