1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * v4l2-dv-timings - dv-timings helper functions
4  *
5  * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/rational.h>
13 #include <linux/videodev2.h>
14 #include <linux/v4l2-dv-timings.h>
15 #include <media/v4l2-dv-timings.h>
16 #include <linux/math64.h>
17 #include <linux/hdmi.h>
18 
19 MODULE_AUTHOR("Hans Verkuil");
20 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
21 MODULE_LICENSE("GPL");
22 
23 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
24 	V4L2_DV_BT_CEA_640X480P59_94,
25 	V4L2_DV_BT_CEA_720X480I59_94,
26 	V4L2_DV_BT_CEA_720X480P59_94,
27 	V4L2_DV_BT_CEA_720X576I50,
28 	V4L2_DV_BT_CEA_720X576P50,
29 	V4L2_DV_BT_CEA_1280X720P24,
30 	V4L2_DV_BT_CEA_1280X720P25,
31 	V4L2_DV_BT_CEA_1280X720P30,
32 	V4L2_DV_BT_CEA_1280X720P50,
33 	V4L2_DV_BT_CEA_1280X720P60,
34 	V4L2_DV_BT_CEA_1920X1080P24,
35 	V4L2_DV_BT_CEA_1920X1080P25,
36 	V4L2_DV_BT_CEA_1920X1080P30,
37 	V4L2_DV_BT_CEA_1920X1080I50,
38 	V4L2_DV_BT_CEA_1920X1080P50,
39 	V4L2_DV_BT_CEA_1920X1080I60,
40 	V4L2_DV_BT_CEA_1920X1080P60,
41 	V4L2_DV_BT_DMT_640X350P85,
42 	V4L2_DV_BT_DMT_640X400P85,
43 	V4L2_DV_BT_DMT_720X400P85,
44 	V4L2_DV_BT_DMT_640X480P72,
45 	V4L2_DV_BT_DMT_640X480P75,
46 	V4L2_DV_BT_DMT_640X480P85,
47 	V4L2_DV_BT_DMT_800X600P56,
48 	V4L2_DV_BT_DMT_800X600P60,
49 	V4L2_DV_BT_DMT_800X600P72,
50 	V4L2_DV_BT_DMT_800X600P75,
51 	V4L2_DV_BT_DMT_800X600P85,
52 	V4L2_DV_BT_DMT_800X600P120_RB,
53 	V4L2_DV_BT_DMT_848X480P60,
54 	V4L2_DV_BT_DMT_1024X768I43,
55 	V4L2_DV_BT_DMT_1024X768P60,
56 	V4L2_DV_BT_DMT_1024X768P70,
57 	V4L2_DV_BT_DMT_1024X768P75,
58 	V4L2_DV_BT_DMT_1024X768P85,
59 	V4L2_DV_BT_DMT_1024X768P120_RB,
60 	V4L2_DV_BT_DMT_1152X864P75,
61 	V4L2_DV_BT_DMT_1280X768P60_RB,
62 	V4L2_DV_BT_DMT_1280X768P60,
63 	V4L2_DV_BT_DMT_1280X768P75,
64 	V4L2_DV_BT_DMT_1280X768P85,
65 	V4L2_DV_BT_DMT_1280X768P120_RB,
66 	V4L2_DV_BT_DMT_1280X800P60_RB,
67 	V4L2_DV_BT_DMT_1280X800P60,
68 	V4L2_DV_BT_DMT_1280X800P75,
69 	V4L2_DV_BT_DMT_1280X800P85,
70 	V4L2_DV_BT_DMT_1280X800P120_RB,
71 	V4L2_DV_BT_DMT_1280X960P60,
72 	V4L2_DV_BT_DMT_1280X960P85,
73 	V4L2_DV_BT_DMT_1280X960P120_RB,
74 	V4L2_DV_BT_DMT_1280X1024P60,
75 	V4L2_DV_BT_DMT_1280X1024P75,
76 	V4L2_DV_BT_DMT_1280X1024P85,
77 	V4L2_DV_BT_DMT_1280X1024P120_RB,
78 	V4L2_DV_BT_DMT_1360X768P60,
79 	V4L2_DV_BT_DMT_1360X768P120_RB,
80 	V4L2_DV_BT_DMT_1366X768P60,
81 	V4L2_DV_BT_DMT_1366X768P60_RB,
82 	V4L2_DV_BT_DMT_1400X1050P60_RB,
83 	V4L2_DV_BT_DMT_1400X1050P60,
84 	V4L2_DV_BT_DMT_1400X1050P75,
85 	V4L2_DV_BT_DMT_1400X1050P85,
86 	V4L2_DV_BT_DMT_1400X1050P120_RB,
87 	V4L2_DV_BT_DMT_1440X900P60_RB,
88 	V4L2_DV_BT_DMT_1440X900P60,
89 	V4L2_DV_BT_DMT_1440X900P75,
90 	V4L2_DV_BT_DMT_1440X900P85,
91 	V4L2_DV_BT_DMT_1440X900P120_RB,
92 	V4L2_DV_BT_DMT_1600X900P60_RB,
93 	V4L2_DV_BT_DMT_1600X1200P60,
94 	V4L2_DV_BT_DMT_1600X1200P65,
95 	V4L2_DV_BT_DMT_1600X1200P70,
96 	V4L2_DV_BT_DMT_1600X1200P75,
97 	V4L2_DV_BT_DMT_1600X1200P85,
98 	V4L2_DV_BT_DMT_1600X1200P120_RB,
99 	V4L2_DV_BT_DMT_1680X1050P60_RB,
100 	V4L2_DV_BT_DMT_1680X1050P60,
101 	V4L2_DV_BT_DMT_1680X1050P75,
102 	V4L2_DV_BT_DMT_1680X1050P85,
103 	V4L2_DV_BT_DMT_1680X1050P120_RB,
104 	V4L2_DV_BT_DMT_1792X1344P60,
105 	V4L2_DV_BT_DMT_1792X1344P75,
106 	V4L2_DV_BT_DMT_1792X1344P120_RB,
107 	V4L2_DV_BT_DMT_1856X1392P60,
108 	V4L2_DV_BT_DMT_1856X1392P75,
109 	V4L2_DV_BT_DMT_1856X1392P120_RB,
110 	V4L2_DV_BT_DMT_1920X1200P60_RB,
111 	V4L2_DV_BT_DMT_1920X1200P60,
112 	V4L2_DV_BT_DMT_1920X1200P75,
113 	V4L2_DV_BT_DMT_1920X1200P85,
114 	V4L2_DV_BT_DMT_1920X1200P120_RB,
115 	V4L2_DV_BT_DMT_1920X1440P60,
116 	V4L2_DV_BT_DMT_1920X1440P75,
117 	V4L2_DV_BT_DMT_1920X1440P120_RB,
118 	V4L2_DV_BT_DMT_2048X1152P60_RB,
119 	V4L2_DV_BT_DMT_2560X1600P60_RB,
120 	V4L2_DV_BT_DMT_2560X1600P60,
121 	V4L2_DV_BT_DMT_2560X1600P75,
122 	V4L2_DV_BT_DMT_2560X1600P85,
123 	V4L2_DV_BT_DMT_2560X1600P120_RB,
124 	V4L2_DV_BT_CEA_3840X2160P24,
125 	V4L2_DV_BT_CEA_3840X2160P25,
126 	V4L2_DV_BT_CEA_3840X2160P30,
127 	V4L2_DV_BT_CEA_3840X2160P50,
128 	V4L2_DV_BT_CEA_3840X2160P60,
129 	V4L2_DV_BT_CEA_4096X2160P24,
130 	V4L2_DV_BT_CEA_4096X2160P25,
131 	V4L2_DV_BT_CEA_4096X2160P30,
132 	V4L2_DV_BT_CEA_4096X2160P50,
133 	V4L2_DV_BT_DMT_4096X2160P59_94_RB,
134 	V4L2_DV_BT_CEA_4096X2160P60,
135 	{ }
136 };
137 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
138 
v4l2_valid_dv_timings(const struct v4l2_dv_timings * t,const struct v4l2_dv_timings_cap * dvcap,v4l2_check_dv_timings_fnc fnc,void * fnc_handle)139 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
140 			   const struct v4l2_dv_timings_cap *dvcap,
141 			   v4l2_check_dv_timings_fnc fnc,
142 			   void *fnc_handle)
143 {
144 	const struct v4l2_bt_timings *bt = &t->bt;
145 	const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
146 	u32 caps = cap->capabilities;
147 
148 	if (t->type != V4L2_DV_BT_656_1120)
149 		return false;
150 	if (t->type != dvcap->type ||
151 	    bt->height < cap->min_height ||
152 	    bt->height > cap->max_height ||
153 	    bt->width < cap->min_width ||
154 	    bt->width > cap->max_width ||
155 	    bt->pixelclock < cap->min_pixelclock ||
156 	    bt->pixelclock > cap->max_pixelclock ||
157 	    (!(caps & V4L2_DV_BT_CAP_CUSTOM) &&
158 	     cap->standards && bt->standards &&
159 	     !(bt->standards & cap->standards)) ||
160 	    (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
161 	    (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
162 		return false;
163 	return fnc == NULL || fnc(t, fnc_handle);
164 }
165 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
166 
v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings * t,const struct v4l2_dv_timings_cap * cap,v4l2_check_dv_timings_fnc fnc,void * fnc_handle)167 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
168 			     const struct v4l2_dv_timings_cap *cap,
169 			     v4l2_check_dv_timings_fnc fnc,
170 			     void *fnc_handle)
171 {
172 	u32 i, idx;
173 
174 	memset(t->reserved, 0, sizeof(t->reserved));
175 	for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
176 		if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
177 					  fnc, fnc_handle) &&
178 		    idx++ == t->index) {
179 			t->timings = v4l2_dv_timings_presets[i];
180 			return 0;
181 		}
182 	}
183 	return -EINVAL;
184 }
185 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
186 
v4l2_find_dv_timings_cap(struct v4l2_dv_timings * t,const struct v4l2_dv_timings_cap * cap,unsigned pclock_delta,v4l2_check_dv_timings_fnc fnc,void * fnc_handle)187 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
188 			      const struct v4l2_dv_timings_cap *cap,
189 			      unsigned pclock_delta,
190 			      v4l2_check_dv_timings_fnc fnc,
191 			      void *fnc_handle)
192 {
193 	int i;
194 
195 	if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
196 		return false;
197 
198 	for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) {
199 		if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
200 					  fnc, fnc_handle) &&
201 		    v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
202 					  pclock_delta, false)) {
203 			u32 flags = t->bt.flags & V4L2_DV_FL_REDUCED_FPS;
204 
205 			*t = v4l2_dv_timings_presets[i];
206 			if (can_reduce_fps(&t->bt))
207 				t->bt.flags |= flags;
208 
209 			return true;
210 		}
211 	}
212 	return false;
213 }
214 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
215 
v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings * t,u8 vic)216 bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic)
217 {
218 	unsigned int i;
219 
220 	for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) {
221 		const struct v4l2_bt_timings *bt =
222 			&v4l2_dv_timings_presets[i].bt;
223 
224 		if ((bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) &&
225 		    bt->cea861_vic == vic) {
226 			*t = v4l2_dv_timings_presets[i];
227 			return true;
228 		}
229 	}
230 	return false;
231 }
232 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cea861_vic);
233 
234 /**
235  * v4l2_match_dv_timings - check if two timings match
236  * @t1: compare this v4l2_dv_timings struct...
237  * @t2: with this struct.
238  * @pclock_delta: the allowed pixelclock deviation.
239  * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
240  *	match.
241  *
242  * Compare t1 with t2 with a given margin of error for the pixelclock.
243  */
v4l2_match_dv_timings(const struct v4l2_dv_timings * t1,const struct v4l2_dv_timings * t2,unsigned pclock_delta,bool match_reduced_fps)244 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
245 			   const struct v4l2_dv_timings *t2,
246 			   unsigned pclock_delta, bool match_reduced_fps)
247 {
248 	if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
249 		return false;
250 	if (t1->bt.width == t2->bt.width &&
251 	    t1->bt.height == t2->bt.height &&
252 	    t1->bt.interlaced == t2->bt.interlaced &&
253 	    t1->bt.polarities == t2->bt.polarities &&
254 	    t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
255 	    t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
256 	    t1->bt.hfrontporch == t2->bt.hfrontporch &&
257 	    t1->bt.hsync == t2->bt.hsync &&
258 	    t1->bt.hbackporch == t2->bt.hbackporch &&
259 	    t1->bt.vfrontporch == t2->bt.vfrontporch &&
260 	    t1->bt.vsync == t2->bt.vsync &&
261 	    t1->bt.vbackporch == t2->bt.vbackporch &&
262 	    (!match_reduced_fps ||
263 	     (t1->bt.flags & V4L2_DV_FL_REDUCED_FPS) ==
264 		(t2->bt.flags & V4L2_DV_FL_REDUCED_FPS)) &&
265 	    (!t1->bt.interlaced ||
266 		(t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
267 		 t1->bt.il_vsync == t2->bt.il_vsync &&
268 		 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
269 		return true;
270 	return false;
271 }
272 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
273 
v4l2_print_dv_timings(const char * dev_prefix,const char * prefix,const struct v4l2_dv_timings * t,bool detailed)274 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
275 			   const struct v4l2_dv_timings *t, bool detailed)
276 {
277 	const struct v4l2_bt_timings *bt = &t->bt;
278 	u32 htot, vtot;
279 	u32 fps;
280 
281 	if (t->type != V4L2_DV_BT_656_1120)
282 		return;
283 
284 	htot = V4L2_DV_BT_FRAME_WIDTH(bt);
285 	vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
286 	if (bt->interlaced)
287 		vtot /= 2;
288 
289 	fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
290 				  (htot * vtot)) : 0;
291 
292 	if (prefix == NULL)
293 		prefix = "";
294 
295 	pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
296 		bt->width, bt->height, bt->interlaced ? "i" : "p",
297 		fps / 100, fps % 100, htot, vtot);
298 
299 	if (!detailed)
300 		return;
301 
302 	pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
303 			dev_prefix, bt->hfrontporch,
304 			(bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
305 			bt->hsync, bt->hbackporch);
306 	pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
307 			dev_prefix, bt->vfrontporch,
308 			(bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
309 			bt->vsync, bt->vbackporch);
310 	if (bt->interlaced)
311 		pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
312 			dev_prefix, bt->il_vfrontporch,
313 			(bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
314 			bt->il_vsync, bt->il_vbackporch);
315 	pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
316 	pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s%s%s%s\n",
317 			dev_prefix, bt->flags,
318 			(bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
319 			" REDUCED_BLANKING" : "",
320 			((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
321 			 bt->vsync == 8) ? " (V2)" : "",
322 			(bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
323 			" CAN_REDUCE_FPS" : "",
324 			(bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
325 			" REDUCED_FPS" : "",
326 			(bt->flags & V4L2_DV_FL_HALF_LINE) ?
327 			" HALF_LINE" : "",
328 			(bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
329 			" CE_VIDEO" : "",
330 			(bt->flags & V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE) ?
331 			" FIRST_FIELD_EXTRA_LINE" : "",
332 			(bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT) ?
333 			" HAS_PICTURE_ASPECT" : "",
334 			(bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) ?
335 			" HAS_CEA861_VIC" : "",
336 			(bt->flags & V4L2_DV_FL_HAS_HDMI_VIC) ?
337 			" HAS_HDMI_VIC" : "");
338 	pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix, bt->standards,
339 			(bt->standards & V4L2_DV_BT_STD_CEA861) ?  " CEA" : "",
340 			(bt->standards & V4L2_DV_BT_STD_DMT) ?  " DMT" : "",
341 			(bt->standards & V4L2_DV_BT_STD_CVT) ?  " CVT" : "",
342 			(bt->standards & V4L2_DV_BT_STD_GTF) ?  " GTF" : "",
343 			(bt->standards & V4L2_DV_BT_STD_SDI) ?  " SDI" : "");
344 	if (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT)
345 		pr_info("%s: picture aspect (hor:vert): %u:%u\n", dev_prefix,
346 			bt->picture_aspect.numerator,
347 			bt->picture_aspect.denominator);
348 	if (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC)
349 		pr_info("%s: CEA-861 VIC: %u\n", dev_prefix, bt->cea861_vic);
350 	if (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC)
351 		pr_info("%s: HDMI VIC: %u\n", dev_prefix, bt->hdmi_vic);
352 }
353 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
354 
v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings * t)355 struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t)
356 {
357 	struct v4l2_fract ratio = { 1, 1 };
358 	unsigned long n, d;
359 
360 	if (t->type != V4L2_DV_BT_656_1120)
361 		return ratio;
362 	if (!(t->bt.flags & V4L2_DV_FL_HAS_PICTURE_ASPECT))
363 		return ratio;
364 
365 	ratio.numerator = t->bt.width * t->bt.picture_aspect.denominator;
366 	ratio.denominator = t->bt.height * t->bt.picture_aspect.numerator;
367 
368 	rational_best_approximation(ratio.numerator, ratio.denominator,
369 				    ratio.numerator, ratio.denominator, &n, &d);
370 	ratio.numerator = n;
371 	ratio.denominator = d;
372 	return ratio;
373 }
374 EXPORT_SYMBOL_GPL(v4l2_dv_timings_aspect_ratio);
375 
376 /*
377  * CVT defines
378  * Based on Coordinated Video Timings Standard
379  * version 1.1 September 10, 2003
380  */
381 
382 #define CVT_PXL_CLK_GRAN	250000	/* pixel clock granularity */
383 #define CVT_PXL_CLK_GRAN_RB_V2 1000	/* granularity for reduced blanking v2*/
384 
385 /* Normal blanking */
386 #define CVT_MIN_V_BPORCH	7	/* lines */
387 #define CVT_MIN_V_PORCH_RND	3	/* lines */
388 #define CVT_MIN_VSYNC_BP	550	/* min time of vsync + back porch (us) */
389 #define CVT_HSYNC_PERCENT       8       /* nominal hsync as percentage of line */
390 
391 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
392 #define CVT_CELL_GRAN		8	/* character cell granularity */
393 #define CVT_M			600	/* blanking formula gradient */
394 #define CVT_C			40	/* blanking formula offset */
395 #define CVT_K			128	/* blanking formula scaling factor */
396 #define CVT_J			20	/* blanking formula scaling factor */
397 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
398 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
399 
400 /* Reduced Blanking */
401 #define CVT_RB_MIN_V_BPORCH    7       /* lines  */
402 #define CVT_RB_V_FPORCH        3       /* lines  */
403 #define CVT_RB_MIN_V_BLANK   460       /* us     */
404 #define CVT_RB_H_SYNC         32       /* pixels */
405 #define CVT_RB_H_BLANK       160       /* pixels */
406 /* Reduce blanking Version 2 */
407 #define CVT_RB_V2_H_BLANK     80       /* pixels */
408 #define CVT_RB_MIN_V_FPORCH    3       /* lines  */
409 #define CVT_RB_V2_MIN_V_FPORCH 1       /* lines  */
410 #define CVT_RB_V_BPORCH        6       /* lines  */
411 
412 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
413  * @frame_height - the total height of the frame (including blanking) in lines.
414  * @hfreq - the horizontal frequency in Hz.
415  * @vsync - the height of the vertical sync in lines.
416  * @active_width - active width of image (does not include blanking). This
417  * information is needed only in case of version 2 of reduced blanking.
418  * In other cases, this parameter does not have any effect on timings.
419  * @polarities - the horizontal and vertical polarities (same as struct
420  *		v4l2_bt_timings polarities).
421  * @interlaced - if this flag is true, it indicates interlaced format
422  * @fmt - the resulting timings.
423  *
424  * This function will attempt to detect if the given values correspond to a
425  * valid CVT format. If so, then it will return true, and fmt will be filled
426  * in with the found CVT timings.
427  */
v4l2_detect_cvt(unsigned frame_height,unsigned hfreq,unsigned vsync,unsigned active_width,u32 polarities,bool interlaced,struct v4l2_dv_timings * fmt)428 bool v4l2_detect_cvt(unsigned frame_height,
429 		     unsigned hfreq,
430 		     unsigned vsync,
431 		     unsigned active_width,
432 		     u32 polarities,
433 		     bool interlaced,
434 		     struct v4l2_dv_timings *fmt)
435 {
436 	int  v_fp, v_bp, h_fp, h_bp, hsync;
437 	int  frame_width, image_height, image_width;
438 	bool reduced_blanking;
439 	bool rb_v2 = false;
440 	unsigned pix_clk;
441 
442 	if (vsync < 4 || vsync > 8)
443 		return false;
444 
445 	if (polarities == V4L2_DV_VSYNC_POS_POL)
446 		reduced_blanking = false;
447 	else if (polarities == V4L2_DV_HSYNC_POS_POL)
448 		reduced_blanking = true;
449 	else
450 		return false;
451 
452 	if (reduced_blanking && vsync == 8)
453 		rb_v2 = true;
454 
455 	if (rb_v2 && active_width == 0)
456 		return false;
457 
458 	if (!rb_v2 && vsync > 7)
459 		return false;
460 
461 	if (hfreq == 0)
462 		return false;
463 
464 	/* Vertical */
465 	if (reduced_blanking) {
466 		if (rb_v2) {
467 			v_bp = CVT_RB_V_BPORCH;
468 			v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
469 			v_fp -= vsync + v_bp;
470 
471 			if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
472 				v_fp = CVT_RB_V2_MIN_V_FPORCH;
473 		} else {
474 			v_fp = CVT_RB_V_FPORCH;
475 			v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
476 			v_bp -= vsync + v_fp;
477 
478 			if (v_bp < CVT_RB_MIN_V_BPORCH)
479 				v_bp = CVT_RB_MIN_V_BPORCH;
480 		}
481 	} else {
482 		v_fp = CVT_MIN_V_PORCH_RND;
483 		v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
484 
485 		if (v_bp < CVT_MIN_V_BPORCH)
486 			v_bp = CVT_MIN_V_BPORCH;
487 	}
488 
489 	if (interlaced)
490 		image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
491 	else
492 		image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
493 
494 	if (image_height < 0)
495 		return false;
496 
497 	/* Aspect ratio based on vsync */
498 	switch (vsync) {
499 	case 4:
500 		image_width = (image_height * 4) / 3;
501 		break;
502 	case 5:
503 		image_width = (image_height * 16) / 9;
504 		break;
505 	case 6:
506 		image_width = (image_height * 16) / 10;
507 		break;
508 	case 7:
509 		/* special case */
510 		if (image_height == 1024)
511 			image_width = (image_height * 5) / 4;
512 		else if (image_height == 768)
513 			image_width = (image_height * 15) / 9;
514 		else
515 			return false;
516 		break;
517 	case 8:
518 		image_width = active_width;
519 		break;
520 	default:
521 		return false;
522 	}
523 
524 	if (!rb_v2)
525 		image_width = image_width & ~7;
526 
527 	/* Horizontal */
528 	if (reduced_blanking) {
529 		int h_blank;
530 		int clk_gran;
531 
532 		h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
533 		clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
534 
535 		pix_clk = (image_width + h_blank) * hfreq;
536 		pix_clk = (pix_clk / clk_gran) * clk_gran;
537 
538 		h_bp  = h_blank / 2;
539 		hsync = CVT_RB_H_SYNC;
540 		h_fp  = h_blank - h_bp - hsync;
541 
542 		frame_width = image_width + h_blank;
543 	} else {
544 		unsigned ideal_duty_cycle_per_myriad =
545 			100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
546 		int h_blank;
547 
548 		if (ideal_duty_cycle_per_myriad < 2000)
549 			ideal_duty_cycle_per_myriad = 2000;
550 
551 		h_blank = image_width * ideal_duty_cycle_per_myriad /
552 					(10000 - ideal_duty_cycle_per_myriad);
553 		h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
554 
555 		pix_clk = (image_width + h_blank) * hfreq;
556 		pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
557 
558 		h_bp = h_blank / 2;
559 		frame_width = image_width + h_blank;
560 
561 		hsync = frame_width * CVT_HSYNC_PERCENT / 100;
562 		hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
563 		h_fp = h_blank - hsync - h_bp;
564 	}
565 
566 	fmt->type = V4L2_DV_BT_656_1120;
567 	fmt->bt.polarities = polarities;
568 	fmt->bt.width = image_width;
569 	fmt->bt.height = image_height;
570 	fmt->bt.hfrontporch = h_fp;
571 	fmt->bt.vfrontporch = v_fp;
572 	fmt->bt.hsync = hsync;
573 	fmt->bt.vsync = vsync;
574 	fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
575 
576 	if (!interlaced) {
577 		fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
578 		fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
579 	} else {
580 		fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
581 				      2 * vsync) / 2;
582 		fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
583 					2 * vsync - fmt->bt.vbackporch;
584 		fmt->bt.il_vfrontporch = v_fp;
585 		fmt->bt.il_vsync = vsync;
586 		fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
587 		fmt->bt.interlaced = V4L2_DV_INTERLACED;
588 	}
589 
590 	fmt->bt.pixelclock = pix_clk;
591 	fmt->bt.standards = V4L2_DV_BT_STD_CVT;
592 
593 	if (reduced_blanking)
594 		fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
595 
596 	return true;
597 }
598 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
599 
600 /*
601  * GTF defines
602  * Based on Generalized Timing Formula Standard
603  * Version 1.1 September 2, 1999
604  */
605 
606 #define GTF_PXL_CLK_GRAN	250000	/* pixel clock granularity */
607 
608 #define GTF_MIN_VSYNC_BP	550	/* min time of vsync + back porch (us) */
609 #define GTF_V_FP		1	/* vertical front porch (lines) */
610 #define GTF_CELL_GRAN		8	/* character cell granularity */
611 
612 /* Default */
613 #define GTF_D_M			600	/* blanking formula gradient */
614 #define GTF_D_C			40	/* blanking formula offset */
615 #define GTF_D_K			128	/* blanking formula scaling factor */
616 #define GTF_D_J			20	/* blanking formula scaling factor */
617 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
618 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
619 
620 /* Secondary */
621 #define GTF_S_M			3600	/* blanking formula gradient */
622 #define GTF_S_C			40	/* blanking formula offset */
623 #define GTF_S_K			128	/* blanking formula scaling factor */
624 #define GTF_S_J			35	/* blanking formula scaling factor */
625 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
626 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
627 
628 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
629  * @frame_height - the total height of the frame (including blanking) in lines.
630  * @hfreq - the horizontal frequency in Hz.
631  * @vsync - the height of the vertical sync in lines.
632  * @polarities - the horizontal and vertical polarities (same as struct
633  *		v4l2_bt_timings polarities).
634  * @interlaced - if this flag is true, it indicates interlaced format
635  * @aspect - preferred aspect ratio. GTF has no method of determining the
636  *		aspect ratio in order to derive the image width from the
637  *		image height, so it has to be passed explicitly. Usually
638  *		the native screen aspect ratio is used for this. If it
639  *		is not filled in correctly, then 16:9 will be assumed.
640  * @fmt - the resulting timings.
641  *
642  * This function will attempt to detect if the given values correspond to a
643  * valid GTF format. If so, then it will return true, and fmt will be filled
644  * in with the found GTF timings.
645  */
v4l2_detect_gtf(unsigned frame_height,unsigned hfreq,unsigned vsync,u32 polarities,bool interlaced,struct v4l2_fract aspect,struct v4l2_dv_timings * fmt)646 bool v4l2_detect_gtf(unsigned frame_height,
647 		unsigned hfreq,
648 		unsigned vsync,
649 		u32 polarities,
650 		bool interlaced,
651 		struct v4l2_fract aspect,
652 		struct v4l2_dv_timings *fmt)
653 {
654 	int pix_clk;
655 	int  v_fp, v_bp, h_fp, hsync;
656 	int frame_width, image_height, image_width;
657 	bool default_gtf;
658 	int h_blank;
659 
660 	if (vsync != 3)
661 		return false;
662 
663 	if (polarities == V4L2_DV_VSYNC_POS_POL)
664 		default_gtf = true;
665 	else if (polarities == V4L2_DV_HSYNC_POS_POL)
666 		default_gtf = false;
667 	else
668 		return false;
669 
670 	if (hfreq == 0)
671 		return false;
672 
673 	/* Vertical */
674 	v_fp = GTF_V_FP;
675 	v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
676 	if (interlaced)
677 		image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
678 	else
679 		image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
680 
681 	if (image_height < 0)
682 		return false;
683 
684 	if (aspect.numerator == 0 || aspect.denominator == 0) {
685 		aspect.numerator = 16;
686 		aspect.denominator = 9;
687 	}
688 	image_width = ((image_height * aspect.numerator) / aspect.denominator);
689 	image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
690 
691 	/* Horizontal */
692 	if (default_gtf) {
693 		u64 num;
694 		u32 den;
695 
696 		num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
697 		      ((u64)image_width * GTF_D_M_PRIME * 1000));
698 		den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
699 		      (2 * GTF_CELL_GRAN);
700 		h_blank = div_u64((num + (den >> 1)), den);
701 		h_blank *= (2 * GTF_CELL_GRAN);
702 	} else {
703 		u64 num;
704 		u32 den;
705 
706 		num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
707 		      ((u64)image_width * GTF_S_M_PRIME * 1000));
708 		den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
709 		      (2 * GTF_CELL_GRAN);
710 		h_blank = div_u64((num + (den >> 1)), den);
711 		h_blank *= (2 * GTF_CELL_GRAN);
712 	}
713 
714 	frame_width = image_width + h_blank;
715 
716 	pix_clk = (image_width + h_blank) * hfreq;
717 	pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
718 
719 	hsync = (frame_width * 8 + 50) / 100;
720 	hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
721 
722 	h_fp = h_blank / 2 - hsync;
723 
724 	fmt->type = V4L2_DV_BT_656_1120;
725 	fmt->bt.polarities = polarities;
726 	fmt->bt.width = image_width;
727 	fmt->bt.height = image_height;
728 	fmt->bt.hfrontporch = h_fp;
729 	fmt->bt.vfrontporch = v_fp;
730 	fmt->bt.hsync = hsync;
731 	fmt->bt.vsync = vsync;
732 	fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
733 
734 	if (!interlaced) {
735 		fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
736 		fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
737 	} else {
738 		fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
739 				      2 * vsync) / 2;
740 		fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
741 					2 * vsync - fmt->bt.vbackporch;
742 		fmt->bt.il_vfrontporch = v_fp;
743 		fmt->bt.il_vsync = vsync;
744 		fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
745 		fmt->bt.interlaced = V4L2_DV_INTERLACED;
746 	}
747 
748 	fmt->bt.pixelclock = pix_clk;
749 	fmt->bt.standards = V4L2_DV_BT_STD_GTF;
750 
751 	if (!default_gtf)
752 		fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
753 
754 	return true;
755 }
756 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
757 
758 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
759  *	0x15 and 0x16 from the EDID.
760  * @hor_landscape - byte 0x15 from the EDID.
761  * @vert_portrait - byte 0x16 from the EDID.
762  *
763  * Determines the aspect ratio from the EDID.
764  * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
765  * "Horizontal and Vertical Screen Size or Aspect Ratio"
766  */
v4l2_calc_aspect_ratio(u8 hor_landscape,u8 vert_portrait)767 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
768 {
769 	struct v4l2_fract aspect = { 16, 9 };
770 	u8 ratio;
771 
772 	/* Nothing filled in, fallback to 16:9 */
773 	if (!hor_landscape && !vert_portrait)
774 		return aspect;
775 	/* Both filled in, so they are interpreted as the screen size in cm */
776 	if (hor_landscape && vert_portrait) {
777 		aspect.numerator = hor_landscape;
778 		aspect.denominator = vert_portrait;
779 		return aspect;
780 	}
781 	/* Only one is filled in, so interpret them as a ratio:
782 	   (val + 99) / 100 */
783 	ratio = hor_landscape | vert_portrait;
784 	/* Change some rounded values into the exact aspect ratio */
785 	if (ratio == 79) {
786 		aspect.numerator = 16;
787 		aspect.denominator = 9;
788 	} else if (ratio == 34) {
789 		aspect.numerator = 4;
790 		aspect.denominator = 3;
791 	} else if (ratio == 68) {
792 		aspect.numerator = 15;
793 		aspect.denominator = 9;
794 	} else {
795 		aspect.numerator = hor_landscape + 99;
796 		aspect.denominator = 100;
797 	}
798 	if (hor_landscape)
799 		return aspect;
800 	/* The aspect ratio is for portrait, so swap numerator and denominator */
801 	swap(aspect.denominator, aspect.numerator);
802 	return aspect;
803 }
804 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
805 
806 /** v4l2_hdmi_rx_colorimetry - determine HDMI colorimetry information
807  *	based on various InfoFrames.
808  * @avi: the AVI InfoFrame
809  * @hdmi: the HDMI Vendor InfoFrame, may be NULL
810  * @height: the frame height
811  *
812  * Determines the HDMI colorimetry information, i.e. how the HDMI
813  * pixel color data should be interpreted.
814  *
815  * Note that some of the newer features (DCI-P3, HDR) are not yet
816  * implemented: the hdmi.h header needs to be updated to the HDMI 2.0
817  * and CTA-861-G standards.
818  */
819 struct v4l2_hdmi_colorimetry
v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe * avi,const struct hdmi_vendor_infoframe * hdmi,unsigned int height)820 v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
821 			 const struct hdmi_vendor_infoframe *hdmi,
822 			 unsigned int height)
823 {
824 	struct v4l2_hdmi_colorimetry c = {
825 		V4L2_COLORSPACE_SRGB,
826 		V4L2_YCBCR_ENC_DEFAULT,
827 		V4L2_QUANTIZATION_FULL_RANGE,
828 		V4L2_XFER_FUNC_SRGB
829 	};
830 	bool is_ce = avi->video_code || (hdmi && hdmi->vic);
831 	bool is_sdtv = height <= 576;
832 	bool default_is_lim_range_rgb = avi->video_code > 1;
833 
834 	switch (avi->colorspace) {
835 	case HDMI_COLORSPACE_RGB:
836 		/* RGB pixel encoding */
837 		switch (avi->colorimetry) {
838 		case HDMI_COLORIMETRY_EXTENDED:
839 			switch (avi->extended_colorimetry) {
840 			case HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB:
841 				c.colorspace = V4L2_COLORSPACE_ADOBERGB;
842 				c.xfer_func = V4L2_XFER_FUNC_ADOBERGB;
843 				break;
844 			case HDMI_EXTENDED_COLORIMETRY_BT2020:
845 				c.colorspace = V4L2_COLORSPACE_BT2020;
846 				c.xfer_func = V4L2_XFER_FUNC_709;
847 				break;
848 			default:
849 				break;
850 			}
851 			break;
852 		default:
853 			break;
854 		}
855 		switch (avi->quantization_range) {
856 		case HDMI_QUANTIZATION_RANGE_LIMITED:
857 			c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
858 			break;
859 		case HDMI_QUANTIZATION_RANGE_FULL:
860 			break;
861 		default:
862 			if (default_is_lim_range_rgb)
863 				c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
864 			break;
865 		}
866 		break;
867 
868 	default:
869 		/* YCbCr pixel encoding */
870 		c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
871 		switch (avi->colorimetry) {
872 		case HDMI_COLORIMETRY_NONE:
873 			if (!is_ce)
874 				break;
875 			if (is_sdtv) {
876 				c.colorspace = V4L2_COLORSPACE_SMPTE170M;
877 				c.ycbcr_enc = V4L2_YCBCR_ENC_601;
878 			} else {
879 				c.colorspace = V4L2_COLORSPACE_REC709;
880 				c.ycbcr_enc = V4L2_YCBCR_ENC_709;
881 			}
882 			c.xfer_func = V4L2_XFER_FUNC_709;
883 			break;
884 		case HDMI_COLORIMETRY_ITU_601:
885 			c.colorspace = V4L2_COLORSPACE_SMPTE170M;
886 			c.ycbcr_enc = V4L2_YCBCR_ENC_601;
887 			c.xfer_func = V4L2_XFER_FUNC_709;
888 			break;
889 		case HDMI_COLORIMETRY_ITU_709:
890 			c.colorspace = V4L2_COLORSPACE_REC709;
891 			c.ycbcr_enc = V4L2_YCBCR_ENC_709;
892 			c.xfer_func = V4L2_XFER_FUNC_709;
893 			break;
894 		case HDMI_COLORIMETRY_EXTENDED:
895 			switch (avi->extended_colorimetry) {
896 			case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
897 				c.colorspace = V4L2_COLORSPACE_REC709;
898 				c.ycbcr_enc = V4L2_YCBCR_ENC_XV709;
899 				c.xfer_func = V4L2_XFER_FUNC_709;
900 				break;
901 			case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
902 				c.colorspace = V4L2_COLORSPACE_REC709;
903 				c.ycbcr_enc = V4L2_YCBCR_ENC_XV601;
904 				c.xfer_func = V4L2_XFER_FUNC_709;
905 				break;
906 			case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
907 				c.colorspace = V4L2_COLORSPACE_SRGB;
908 				c.ycbcr_enc = V4L2_YCBCR_ENC_601;
909 				c.xfer_func = V4L2_XFER_FUNC_SRGB;
910 				break;
911 			case HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601:
912 				c.colorspace = V4L2_COLORSPACE_ADOBERGB;
913 				c.ycbcr_enc = V4L2_YCBCR_ENC_601;
914 				c.xfer_func = V4L2_XFER_FUNC_ADOBERGB;
915 				break;
916 			case HDMI_EXTENDED_COLORIMETRY_BT2020:
917 				c.colorspace = V4L2_COLORSPACE_BT2020;
918 				c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020;
919 				c.xfer_func = V4L2_XFER_FUNC_709;
920 				break;
921 			case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
922 				c.colorspace = V4L2_COLORSPACE_BT2020;
923 				c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020_CONST_LUM;
924 				c.xfer_func = V4L2_XFER_FUNC_709;
925 				break;
926 			default: /* fall back to ITU_709 */
927 				c.colorspace = V4L2_COLORSPACE_REC709;
928 				c.ycbcr_enc = V4L2_YCBCR_ENC_709;
929 				c.xfer_func = V4L2_XFER_FUNC_709;
930 				break;
931 			}
932 			break;
933 		default:
934 			break;
935 		}
936 		/*
937 		 * YCC Quantization Range signaling is more-or-less broken,
938 		 * let's just ignore this.
939 		 */
940 		break;
941 	}
942 	return c;
943 }
944 EXPORT_SYMBOL_GPL(v4l2_hdmi_rx_colorimetry);
945