1 /* 2 * v4l2-dv-timings - dv-timings helper functions 3 * 4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 5 * 6 * This program is free software; you may redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; version 2 of the License. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 17 * SOFTWARE. 18 * 19 */ 20 21 #include <linux/module.h> 22 #include <linux/types.h> 23 #include <linux/kernel.h> 24 #include <linux/errno.h> 25 #include <linux/videodev2.h> 26 #include <linux/v4l2-dv-timings.h> 27 #include <media/v4l2-dv-timings.h> 28 29 MODULE_AUTHOR("Hans Verkuil"); 30 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions"); 31 MODULE_LICENSE("GPL"); 32 33 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = { 34 V4L2_DV_BT_CEA_640X480P59_94, 35 V4L2_DV_BT_CEA_720X480I59_94, 36 V4L2_DV_BT_CEA_720X480P59_94, 37 V4L2_DV_BT_CEA_720X576I50, 38 V4L2_DV_BT_CEA_720X576P50, 39 V4L2_DV_BT_CEA_1280X720P24, 40 V4L2_DV_BT_CEA_1280X720P25, 41 V4L2_DV_BT_CEA_1280X720P30, 42 V4L2_DV_BT_CEA_1280X720P50, 43 V4L2_DV_BT_CEA_1280X720P60, 44 V4L2_DV_BT_CEA_1920X1080P24, 45 V4L2_DV_BT_CEA_1920X1080P25, 46 V4L2_DV_BT_CEA_1920X1080P30, 47 V4L2_DV_BT_CEA_1920X1080I50, 48 V4L2_DV_BT_CEA_1920X1080P50, 49 V4L2_DV_BT_CEA_1920X1080I60, 50 V4L2_DV_BT_CEA_1920X1080P60, 51 V4L2_DV_BT_DMT_640X350P85, 52 V4L2_DV_BT_DMT_640X400P85, 53 V4L2_DV_BT_DMT_720X400P85, 54 V4L2_DV_BT_DMT_640X480P72, 55 V4L2_DV_BT_DMT_640X480P75, 56 V4L2_DV_BT_DMT_640X480P85, 57 V4L2_DV_BT_DMT_800X600P56, 58 V4L2_DV_BT_DMT_800X600P60, 59 V4L2_DV_BT_DMT_800X600P72, 60 V4L2_DV_BT_DMT_800X600P75, 61 V4L2_DV_BT_DMT_800X600P85, 62 V4L2_DV_BT_DMT_800X600P120_RB, 63 V4L2_DV_BT_DMT_848X480P60, 64 V4L2_DV_BT_DMT_1024X768I43, 65 V4L2_DV_BT_DMT_1024X768P60, 66 V4L2_DV_BT_DMT_1024X768P70, 67 V4L2_DV_BT_DMT_1024X768P75, 68 V4L2_DV_BT_DMT_1024X768P85, 69 V4L2_DV_BT_DMT_1024X768P120_RB, 70 V4L2_DV_BT_DMT_1152X864P75, 71 V4L2_DV_BT_DMT_1280X768P60_RB, 72 V4L2_DV_BT_DMT_1280X768P60, 73 V4L2_DV_BT_DMT_1280X768P75, 74 V4L2_DV_BT_DMT_1280X768P85, 75 V4L2_DV_BT_DMT_1280X768P120_RB, 76 V4L2_DV_BT_DMT_1280X800P60_RB, 77 V4L2_DV_BT_DMT_1280X800P60, 78 V4L2_DV_BT_DMT_1280X800P75, 79 V4L2_DV_BT_DMT_1280X800P85, 80 V4L2_DV_BT_DMT_1280X800P120_RB, 81 V4L2_DV_BT_DMT_1280X960P60, 82 V4L2_DV_BT_DMT_1280X960P85, 83 V4L2_DV_BT_DMT_1280X960P120_RB, 84 V4L2_DV_BT_DMT_1280X1024P60, 85 V4L2_DV_BT_DMT_1280X1024P75, 86 V4L2_DV_BT_DMT_1280X1024P85, 87 V4L2_DV_BT_DMT_1280X1024P120_RB, 88 V4L2_DV_BT_DMT_1360X768P60, 89 V4L2_DV_BT_DMT_1360X768P120_RB, 90 V4L2_DV_BT_DMT_1366X768P60, 91 V4L2_DV_BT_DMT_1366X768P60_RB, 92 V4L2_DV_BT_DMT_1400X1050P60_RB, 93 V4L2_DV_BT_DMT_1400X1050P60, 94 V4L2_DV_BT_DMT_1400X1050P75, 95 V4L2_DV_BT_DMT_1400X1050P85, 96 V4L2_DV_BT_DMT_1400X1050P120_RB, 97 V4L2_DV_BT_DMT_1440X900P60_RB, 98 V4L2_DV_BT_DMT_1440X900P60, 99 V4L2_DV_BT_DMT_1440X900P75, 100 V4L2_DV_BT_DMT_1440X900P85, 101 V4L2_DV_BT_DMT_1440X900P120_RB, 102 V4L2_DV_BT_DMT_1600X900P60_RB, 103 V4L2_DV_BT_DMT_1600X1200P60, 104 V4L2_DV_BT_DMT_1600X1200P65, 105 V4L2_DV_BT_DMT_1600X1200P70, 106 V4L2_DV_BT_DMT_1600X1200P75, 107 V4L2_DV_BT_DMT_1600X1200P85, 108 V4L2_DV_BT_DMT_1600X1200P120_RB, 109 V4L2_DV_BT_DMT_1680X1050P60_RB, 110 V4L2_DV_BT_DMT_1680X1050P60, 111 V4L2_DV_BT_DMT_1680X1050P75, 112 V4L2_DV_BT_DMT_1680X1050P85, 113 V4L2_DV_BT_DMT_1680X1050P120_RB, 114 V4L2_DV_BT_DMT_1792X1344P60, 115 V4L2_DV_BT_DMT_1792X1344P75, 116 V4L2_DV_BT_DMT_1792X1344P120_RB, 117 V4L2_DV_BT_DMT_1856X1392P60, 118 V4L2_DV_BT_DMT_1856X1392P75, 119 V4L2_DV_BT_DMT_1856X1392P120_RB, 120 V4L2_DV_BT_DMT_1920X1200P60_RB, 121 V4L2_DV_BT_DMT_1920X1200P60, 122 V4L2_DV_BT_DMT_1920X1200P75, 123 V4L2_DV_BT_DMT_1920X1200P85, 124 V4L2_DV_BT_DMT_1920X1200P120_RB, 125 V4L2_DV_BT_DMT_1920X1440P60, 126 V4L2_DV_BT_DMT_1920X1440P75, 127 V4L2_DV_BT_DMT_1920X1440P120_RB, 128 V4L2_DV_BT_DMT_2048X1152P60_RB, 129 V4L2_DV_BT_DMT_2560X1600P60_RB, 130 V4L2_DV_BT_DMT_2560X1600P60, 131 V4L2_DV_BT_DMT_2560X1600P75, 132 V4L2_DV_BT_DMT_2560X1600P85, 133 V4L2_DV_BT_DMT_2560X1600P120_RB, 134 V4L2_DV_BT_CEA_3840X2160P24, 135 V4L2_DV_BT_CEA_3840X2160P25, 136 V4L2_DV_BT_CEA_3840X2160P30, 137 V4L2_DV_BT_CEA_3840X2160P50, 138 V4L2_DV_BT_CEA_3840X2160P60, 139 V4L2_DV_BT_CEA_4096X2160P24, 140 V4L2_DV_BT_CEA_4096X2160P25, 141 V4L2_DV_BT_CEA_4096X2160P30, 142 V4L2_DV_BT_CEA_4096X2160P50, 143 V4L2_DV_BT_DMT_4096X2160P59_94_RB, 144 V4L2_DV_BT_CEA_4096X2160P60, 145 { } 146 }; 147 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets); 148 149 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t, 150 const struct v4l2_dv_timings_cap *dvcap, 151 v4l2_check_dv_timings_fnc fnc, 152 void *fnc_handle) 153 { 154 const struct v4l2_bt_timings *bt = &t->bt; 155 const struct v4l2_bt_timings_cap *cap = &dvcap->bt; 156 u32 caps = cap->capabilities; 157 158 if (t->type != V4L2_DV_BT_656_1120) 159 return false; 160 if (t->type != dvcap->type || 161 bt->height < cap->min_height || 162 bt->height > cap->max_height || 163 bt->width < cap->min_width || 164 bt->width > cap->max_width || 165 bt->pixelclock < cap->min_pixelclock || 166 bt->pixelclock > cap->max_pixelclock || 167 (cap->standards && !(bt->standards & cap->standards)) || 168 (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) || 169 (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE))) 170 return false; 171 return fnc == NULL || fnc(t, fnc_handle); 172 } 173 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings); 174 175 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t, 176 const struct v4l2_dv_timings_cap *cap, 177 v4l2_check_dv_timings_fnc fnc, 178 void *fnc_handle) 179 { 180 u32 i, idx; 181 182 memset(t->reserved, 0, sizeof(t->reserved)); 183 for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) { 184 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap, 185 fnc, fnc_handle) && 186 idx++ == t->index) { 187 t->timings = v4l2_dv_timings_presets[i]; 188 return 0; 189 } 190 } 191 return -EINVAL; 192 } 193 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap); 194 195 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t, 196 const struct v4l2_dv_timings_cap *cap, 197 unsigned pclock_delta, 198 v4l2_check_dv_timings_fnc fnc, 199 void *fnc_handle) 200 { 201 int i; 202 203 if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle)) 204 return false; 205 206 for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) { 207 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap, 208 fnc, fnc_handle) && 209 v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i, 210 pclock_delta)) { 211 *t = v4l2_dv_timings_presets[i]; 212 return true; 213 } 214 } 215 return false; 216 } 217 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap); 218 219 /** 220 * v4l2_match_dv_timings - check if two timings match 221 * @t1 - compare this v4l2_dv_timings struct... 222 * @t2 - with this struct. 223 * @pclock_delta - the allowed pixelclock deviation. 224 * 225 * Compare t1 with t2 with a given margin of error for the pixelclock. 226 */ 227 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1, 228 const struct v4l2_dv_timings *t2, 229 unsigned pclock_delta) 230 { 231 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120) 232 return false; 233 if (t1->bt.width == t2->bt.width && 234 t1->bt.height == t2->bt.height && 235 t1->bt.interlaced == t2->bt.interlaced && 236 t1->bt.polarities == t2->bt.polarities && 237 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta && 238 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta && 239 t1->bt.hfrontporch == t2->bt.hfrontporch && 240 t1->bt.vfrontporch == t2->bt.vfrontporch && 241 t1->bt.vsync == t2->bt.vsync && 242 t1->bt.vbackporch == t2->bt.vbackporch && 243 (!t1->bt.interlaced || 244 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch && 245 t1->bt.il_vsync == t2->bt.il_vsync && 246 t1->bt.il_vbackporch == t2->bt.il_vbackporch))) 247 return true; 248 return false; 249 } 250 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings); 251 252 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix, 253 const struct v4l2_dv_timings *t, bool detailed) 254 { 255 const struct v4l2_bt_timings *bt = &t->bt; 256 u32 htot, vtot; 257 258 if (t->type != V4L2_DV_BT_656_1120) 259 return; 260 261 htot = V4L2_DV_BT_FRAME_WIDTH(bt); 262 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt); 263 264 if (prefix == NULL) 265 prefix = ""; 266 267 pr_info("%s: %s%ux%u%s%u (%ux%u)\n", dev_prefix, prefix, 268 bt->width, bt->height, bt->interlaced ? "i" : "p", 269 (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0, 270 htot, vtot); 271 272 if (!detailed) 273 return; 274 275 pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n", 276 dev_prefix, bt->hfrontporch, 277 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-", 278 bt->hsync, bt->hbackporch); 279 pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n", 280 dev_prefix, bt->vfrontporch, 281 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-", 282 bt->vsync, bt->vbackporch); 283 pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock); 284 pr_info("%s: flags (0x%x):%s%s%s%s\n", dev_prefix, bt->flags, 285 (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ? 286 " REDUCED_BLANKING" : "", 287 (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ? 288 " CAN_REDUCE_FPS" : "", 289 (bt->flags & V4L2_DV_FL_REDUCED_FPS) ? 290 " REDUCED_FPS" : "", 291 (bt->flags & V4L2_DV_FL_HALF_LINE) ? 292 " HALF_LINE" : ""); 293 pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards, 294 (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "", 295 (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "", 296 (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "", 297 (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : ""); 298 } 299 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings); 300 301 /* 302 * CVT defines 303 * Based on Coordinated Video Timings Standard 304 * version 1.1 September 10, 2003 305 */ 306 307 #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */ 308 309 /* Normal blanking */ 310 #define CVT_MIN_V_BPORCH 7 /* lines */ 311 #define CVT_MIN_V_PORCH_RND 3 /* lines */ 312 #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ 313 314 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */ 315 #define CVT_CELL_GRAN 8 /* character cell granularity */ 316 #define CVT_M 600 /* blanking formula gradient */ 317 #define CVT_C 40 /* blanking formula offset */ 318 #define CVT_K 128 /* blanking formula scaling factor */ 319 #define CVT_J 20 /* blanking formula scaling factor */ 320 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J) 321 #define CVT_M_PRIME (CVT_K * CVT_M / 256) 322 323 /* Reduced Blanking */ 324 #define CVT_RB_MIN_V_BPORCH 7 /* lines */ 325 #define CVT_RB_V_FPORCH 3 /* lines */ 326 #define CVT_RB_MIN_V_BLANK 460 /* us */ 327 #define CVT_RB_H_SYNC 32 /* pixels */ 328 #define CVT_RB_H_BPORCH 80 /* pixels */ 329 #define CVT_RB_H_BLANK 160 /* pixels */ 330 331 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard 332 * @frame_height - the total height of the frame (including blanking) in lines. 333 * @hfreq - the horizontal frequency in Hz. 334 * @vsync - the height of the vertical sync in lines. 335 * @polarities - the horizontal and vertical polarities (same as struct 336 * v4l2_bt_timings polarities). 337 * @fmt - the resulting timings. 338 * 339 * This function will attempt to detect if the given values correspond to a 340 * valid CVT format. If so, then it will return true, and fmt will be filled 341 * in with the found CVT timings. 342 * 343 * TODO: VESA defined a new version 2 of their reduced blanking 344 * formula. Support for that is currently missing in this CVT 345 * detection function. 346 */ 347 bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, 348 u32 polarities, struct v4l2_dv_timings *fmt) 349 { 350 int v_fp, v_bp, h_fp, h_bp, hsync; 351 int frame_width, image_height, image_width; 352 bool reduced_blanking; 353 unsigned pix_clk; 354 355 if (vsync < 4 || vsync > 7) 356 return false; 357 358 if (polarities == V4L2_DV_VSYNC_POS_POL) 359 reduced_blanking = false; 360 else if (polarities == V4L2_DV_HSYNC_POS_POL) 361 reduced_blanking = true; 362 else 363 return false; 364 365 /* Vertical */ 366 if (reduced_blanking) { 367 v_fp = CVT_RB_V_FPORCH; 368 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 1999999) / 1000000; 369 v_bp -= vsync + v_fp; 370 371 if (v_bp < CVT_RB_MIN_V_BPORCH) 372 v_bp = CVT_RB_MIN_V_BPORCH; 373 } else { 374 v_fp = CVT_MIN_V_PORCH_RND; 375 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 1999999) / 1000000 - vsync; 376 377 if (v_bp < CVT_MIN_V_BPORCH) 378 v_bp = CVT_MIN_V_BPORCH; 379 } 380 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; 381 382 /* Aspect ratio based on vsync */ 383 switch (vsync) { 384 case 4: 385 image_width = (image_height * 4) / 3; 386 break; 387 case 5: 388 image_width = (image_height * 16) / 9; 389 break; 390 case 6: 391 image_width = (image_height * 16) / 10; 392 break; 393 case 7: 394 /* special case */ 395 if (image_height == 1024) 396 image_width = (image_height * 5) / 4; 397 else if (image_height == 768) 398 image_width = (image_height * 15) / 9; 399 else 400 return false; 401 break; 402 default: 403 return false; 404 } 405 406 image_width = image_width & ~7; 407 408 /* Horizontal */ 409 if (reduced_blanking) { 410 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq; 411 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; 412 413 h_bp = CVT_RB_H_BPORCH; 414 hsync = CVT_RB_H_SYNC; 415 h_fp = CVT_RB_H_BLANK - h_bp - hsync; 416 417 frame_width = image_width + CVT_RB_H_BLANK; 418 } else { 419 unsigned ideal_duty_cycle_per_myriad = 420 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq; 421 int h_blank; 422 423 if (ideal_duty_cycle_per_myriad < 2000) 424 ideal_duty_cycle_per_myriad = 2000; 425 426 h_blank = image_width * ideal_duty_cycle_per_myriad / 427 (10000 - ideal_duty_cycle_per_myriad); 428 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN; 429 430 pix_clk = (image_width + h_blank) * hfreq; 431 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; 432 433 h_bp = h_blank / 2; 434 frame_width = image_width + h_blank; 435 436 hsync = (frame_width * 8 + 50) / 100; 437 hsync = hsync - hsync % CVT_CELL_GRAN; 438 h_fp = h_blank - hsync - h_bp; 439 } 440 441 fmt->type = V4L2_DV_BT_656_1120; 442 fmt->bt.polarities = polarities; 443 fmt->bt.width = image_width; 444 fmt->bt.height = image_height; 445 fmt->bt.hfrontporch = h_fp; 446 fmt->bt.vfrontporch = v_fp; 447 fmt->bt.hsync = hsync; 448 fmt->bt.vsync = vsync; 449 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; 450 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; 451 fmt->bt.pixelclock = pix_clk; 452 fmt->bt.standards = V4L2_DV_BT_STD_CVT; 453 if (reduced_blanking) 454 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; 455 return true; 456 } 457 EXPORT_SYMBOL_GPL(v4l2_detect_cvt); 458 459 /* 460 * GTF defines 461 * Based on Generalized Timing Formula Standard 462 * Version 1.1 September 2, 1999 463 */ 464 465 #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */ 466 467 #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ 468 #define GTF_V_FP 1 /* vertical front porch (lines) */ 469 #define GTF_CELL_GRAN 8 /* character cell granularity */ 470 471 /* Default */ 472 #define GTF_D_M 600 /* blanking formula gradient */ 473 #define GTF_D_C 40 /* blanking formula offset */ 474 #define GTF_D_K 128 /* blanking formula scaling factor */ 475 #define GTF_D_J 20 /* blanking formula scaling factor */ 476 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J) 477 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256) 478 479 /* Secondary */ 480 #define GTF_S_M 3600 /* blanking formula gradient */ 481 #define GTF_S_C 40 /* blanking formula offset */ 482 #define GTF_S_K 128 /* blanking formula scaling factor */ 483 #define GTF_S_J 35 /* blanking formula scaling factor */ 484 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J) 485 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256) 486 487 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard 488 * @frame_height - the total height of the frame (including blanking) in lines. 489 * @hfreq - the horizontal frequency in Hz. 490 * @vsync - the height of the vertical sync in lines. 491 * @polarities - the horizontal and vertical polarities (same as struct 492 * v4l2_bt_timings polarities). 493 * @aspect - preferred aspect ratio. GTF has no method of determining the 494 * aspect ratio in order to derive the image width from the 495 * image height, so it has to be passed explicitly. Usually 496 * the native screen aspect ratio is used for this. If it 497 * is not filled in correctly, then 16:9 will be assumed. 498 * @fmt - the resulting timings. 499 * 500 * This function will attempt to detect if the given values correspond to a 501 * valid GTF format. If so, then it will return true, and fmt will be filled 502 * in with the found GTF timings. 503 */ 504 bool v4l2_detect_gtf(unsigned frame_height, 505 unsigned hfreq, 506 unsigned vsync, 507 u32 polarities, 508 struct v4l2_fract aspect, 509 struct v4l2_dv_timings *fmt) 510 { 511 int pix_clk; 512 int v_fp, v_bp, h_fp, hsync; 513 int frame_width, image_height, image_width; 514 bool default_gtf; 515 int h_blank; 516 517 if (vsync != 3) 518 return false; 519 520 if (polarities == V4L2_DV_VSYNC_POS_POL) 521 default_gtf = true; 522 else if (polarities == V4L2_DV_HSYNC_POS_POL) 523 default_gtf = false; 524 else 525 return false; 526 527 /* Vertical */ 528 v_fp = GTF_V_FP; 529 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync; 530 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; 531 532 if (aspect.numerator == 0 || aspect.denominator == 0) { 533 aspect.numerator = 16; 534 aspect.denominator = 9; 535 } 536 image_width = ((image_height * aspect.numerator) / aspect.denominator); 537 image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1); 538 539 /* Horizontal */ 540 if (default_gtf) 541 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) - 542 (image_width * GTF_D_M_PRIME * 1000) + 543 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) / 544 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000); 545 else 546 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) - 547 (image_width * GTF_S_M_PRIME * 1000) + 548 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) / 549 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000); 550 551 h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN); 552 frame_width = image_width + h_blank; 553 554 pix_clk = (image_width + h_blank) * hfreq; 555 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN; 556 557 hsync = (frame_width * 8 + 50) / 100; 558 hsync = hsync - hsync % GTF_CELL_GRAN; 559 560 h_fp = h_blank / 2 - hsync; 561 562 fmt->type = V4L2_DV_BT_656_1120; 563 fmt->bt.polarities = polarities; 564 fmt->bt.width = image_width; 565 fmt->bt.height = image_height; 566 fmt->bt.hfrontporch = h_fp; 567 fmt->bt.vfrontporch = v_fp; 568 fmt->bt.hsync = hsync; 569 fmt->bt.vsync = vsync; 570 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; 571 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; 572 fmt->bt.pixelclock = pix_clk; 573 fmt->bt.standards = V4L2_DV_BT_STD_GTF; 574 if (!default_gtf) 575 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; 576 return true; 577 } 578 EXPORT_SYMBOL_GPL(v4l2_detect_gtf); 579 580 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes 581 * 0x15 and 0x16 from the EDID. 582 * @hor_landscape - byte 0x15 from the EDID. 583 * @vert_portrait - byte 0x16 from the EDID. 584 * 585 * Determines the aspect ratio from the EDID. 586 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2: 587 * "Horizontal and Vertical Screen Size or Aspect Ratio" 588 */ 589 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait) 590 { 591 struct v4l2_fract aspect = { 16, 9 }; 592 u32 tmp; 593 u8 ratio; 594 595 /* Nothing filled in, fallback to 16:9 */ 596 if (!hor_landscape && !vert_portrait) 597 return aspect; 598 /* Both filled in, so they are interpreted as the screen size in cm */ 599 if (hor_landscape && vert_portrait) { 600 aspect.numerator = hor_landscape; 601 aspect.denominator = vert_portrait; 602 return aspect; 603 } 604 /* Only one is filled in, so interpret them as a ratio: 605 (val + 99) / 100 */ 606 ratio = hor_landscape | vert_portrait; 607 /* Change some rounded values into the exact aspect ratio */ 608 if (ratio == 79) { 609 aspect.numerator = 16; 610 aspect.denominator = 9; 611 } else if (ratio == 34) { 612 aspect.numerator = 4; 613 aspect.numerator = 3; 614 } else if (ratio == 68) { 615 aspect.numerator = 15; 616 aspect.numerator = 9; 617 } else { 618 aspect.numerator = hor_landscape + 99; 619 aspect.denominator = 100; 620 } 621 if (hor_landscape) 622 return aspect; 623 /* The aspect ratio is for portrait, so swap numerator and denominator */ 624 tmp = aspect.denominator; 625 aspect.denominator = aspect.numerator; 626 aspect.numerator = tmp; 627 return aspect; 628 } 629 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio); 630