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