Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 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> |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 27 | #include <media/v4l2-dv-timings.h> |
| 28 | |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame^] | 29 | const struct v4l2_dv_timings v4l2_dv_timings_presets[] = { |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 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, |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame^] | 130 | { } |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 131 | }; |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame^] | 132 | EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets); |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 133 | |
| 134 | bool v4l2_dv_valid_timings(const struct v4l2_dv_timings *t, |
| 135 | const struct v4l2_dv_timings_cap *dvcap) |
| 136 | { |
| 137 | const struct v4l2_bt_timings *bt = &t->bt; |
| 138 | const struct v4l2_bt_timings_cap *cap = &dvcap->bt; |
| 139 | u32 caps = cap->capabilities; |
| 140 | |
| 141 | if (t->type != V4L2_DV_BT_656_1120) |
| 142 | return false; |
| 143 | if (t->type != dvcap->type || |
| 144 | bt->height < cap->min_height || |
| 145 | bt->height > cap->max_height || |
| 146 | bt->width < cap->min_width || |
| 147 | bt->width > cap->max_width || |
| 148 | bt->pixelclock < cap->min_pixelclock || |
| 149 | bt->pixelclock > cap->max_pixelclock || |
| 150 | (cap->standards && !(bt->standards & cap->standards)) || |
| 151 | (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) || |
| 152 | (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE))) |
| 153 | return false; |
| 154 | return true; |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(v4l2_dv_valid_timings); |
| 157 | |
| 158 | int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t, |
| 159 | const struct v4l2_dv_timings_cap *cap) |
| 160 | { |
| 161 | u32 i, idx; |
| 162 | |
| 163 | memset(t->reserved, 0, sizeof(t->reserved)); |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame^] | 164 | for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) { |
| 165 | if (v4l2_dv_valid_timings(v4l2_dv_timings_presets + i, cap) && |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 166 | idx++ == t->index) { |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame^] | 167 | t->timings = v4l2_dv_timings_presets[i]; |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap); |
| 174 | |
| 175 | bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t, |
| 176 | const struct v4l2_dv_timings_cap *cap, |
| 177 | unsigned pclock_delta) |
| 178 | { |
| 179 | int i; |
| 180 | |
| 181 | if (!v4l2_dv_valid_timings(t, cap)) |
| 182 | return false; |
| 183 | |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame^] | 184 | for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) { |
| 185 | if (v4l2_dv_valid_timings(v4l2_dv_timings_presets + i, cap) && |
| 186 | v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i, pclock_delta)) { |
| 187 | *t = v4l2_dv_timings_presets[i]; |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap); |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 194 | |
| 195 | /** |
Hans Verkuil | ef1ed8f | 2013-08-15 08:28:47 -0300 | [diff] [blame] | 196 | * v4l2_match_dv_timings - check if two timings match |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 197 | * @t1 - compare this v4l2_dv_timings struct... |
| 198 | * @t2 - with this struct. |
| 199 | * @pclock_delta - the allowed pixelclock deviation. |
| 200 | * |
| 201 | * Compare t1 with t2 with a given margin of error for the pixelclock. |
| 202 | */ |
Hans Verkuil | ef1ed8f | 2013-08-15 08:28:47 -0300 | [diff] [blame] | 203 | bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1, |
| 204 | const struct v4l2_dv_timings *t2, |
| 205 | unsigned pclock_delta) |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 206 | { |
| 207 | if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120) |
| 208 | return false; |
| 209 | if (t1->bt.width == t2->bt.width && |
| 210 | t1->bt.height == t2->bt.height && |
| 211 | t1->bt.interlaced == t2->bt.interlaced && |
| 212 | t1->bt.polarities == t2->bt.polarities && |
| 213 | t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta && |
| 214 | t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta && |
| 215 | t1->bt.hfrontporch == t2->bt.hfrontporch && |
| 216 | t1->bt.vfrontporch == t2->bt.vfrontporch && |
| 217 | t1->bt.vsync == t2->bt.vsync && |
| 218 | t1->bt.vbackporch == t2->bt.vbackporch && |
| 219 | (!t1->bt.interlaced || |
| 220 | (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch && |
| 221 | t1->bt.il_vsync == t2->bt.il_vsync && |
| 222 | t1->bt.il_vbackporch == t2->bt.il_vbackporch))) |
| 223 | return true; |
| 224 | return false; |
| 225 | } |
Hans Verkuil | ef1ed8f | 2013-08-15 08:28:47 -0300 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(v4l2_match_dv_timings); |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 227 | |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 228 | void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix, |
| 229 | const struct v4l2_dv_timings *t, bool detailed) |
| 230 | { |
| 231 | const struct v4l2_bt_timings *bt = &t->bt; |
| 232 | u32 htot, vtot; |
| 233 | |
| 234 | if (t->type != V4L2_DV_BT_656_1120) |
| 235 | return; |
| 236 | |
| 237 | htot = V4L2_DV_BT_FRAME_WIDTH(bt); |
| 238 | vtot = V4L2_DV_BT_FRAME_HEIGHT(bt); |
| 239 | |
| 240 | if (prefix == NULL) |
| 241 | prefix = ""; |
| 242 | |
| 243 | pr_info("%s: %s%ux%u%s%u (%ux%u)\n", dev_prefix, prefix, |
| 244 | bt->width, bt->height, bt->interlaced ? "i" : "p", |
| 245 | (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0, |
| 246 | htot, vtot); |
| 247 | |
| 248 | if (!detailed) |
| 249 | return; |
| 250 | |
| 251 | pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n", |
| 252 | dev_prefix, bt->hfrontporch, |
| 253 | (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-", |
| 254 | bt->hsync, bt->hbackporch); |
| 255 | pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n", |
| 256 | dev_prefix, bt->vfrontporch, |
| 257 | (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-", |
| 258 | bt->vsync, bt->vbackporch); |
| 259 | pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock); |
| 260 | pr_info("%s: flags (0x%x):%s%s%s%s\n", dev_prefix, bt->flags, |
| 261 | (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ? |
| 262 | " REDUCED_BLANKING" : "", |
| 263 | (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ? |
| 264 | " CAN_REDUCE_FPS" : "", |
| 265 | (bt->flags & V4L2_DV_FL_REDUCED_FPS) ? |
| 266 | " REDUCED_FPS" : "", |
| 267 | (bt->flags & V4L2_DV_FL_HALF_LINE) ? |
| 268 | " HALF_LINE" : ""); |
| 269 | pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards, |
| 270 | (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "", |
| 271 | (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "", |
| 272 | (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "", |
| 273 | (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : ""); |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(v4l2_print_dv_timings); |
| 276 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 277 | /* |
| 278 | * CVT defines |
| 279 | * Based on Coordinated Video Timings Standard |
| 280 | * version 1.1 September 10, 2003 |
| 281 | */ |
| 282 | |
| 283 | #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */ |
| 284 | |
| 285 | /* Normal blanking */ |
| 286 | #define CVT_MIN_V_BPORCH 7 /* lines */ |
| 287 | #define CVT_MIN_V_PORCH_RND 3 /* lines */ |
| 288 | #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ |
| 289 | |
| 290 | /* Normal blanking for CVT uses GTF to calculate horizontal blanking */ |
| 291 | #define CVT_CELL_GRAN 8 /* character cell granularity */ |
| 292 | #define CVT_M 600 /* blanking formula gradient */ |
| 293 | #define CVT_C 40 /* blanking formula offset */ |
| 294 | #define CVT_K 128 /* blanking formula scaling factor */ |
| 295 | #define CVT_J 20 /* blanking formula scaling factor */ |
| 296 | #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J) |
| 297 | #define CVT_M_PRIME (CVT_K * CVT_M / 256) |
| 298 | |
| 299 | /* Reduced Blanking */ |
| 300 | #define CVT_RB_MIN_V_BPORCH 7 /* lines */ |
| 301 | #define CVT_RB_V_FPORCH 3 /* lines */ |
| 302 | #define CVT_RB_MIN_V_BLANK 460 /* us */ |
| 303 | #define CVT_RB_H_SYNC 32 /* pixels */ |
| 304 | #define CVT_RB_H_BPORCH 80 /* pixels */ |
| 305 | #define CVT_RB_H_BLANK 160 /* pixels */ |
| 306 | |
| 307 | /** v4l2_detect_cvt - detect if the given timings follow the CVT standard |
| 308 | * @frame_height - the total height of the frame (including blanking) in lines. |
| 309 | * @hfreq - the horizontal frequency in Hz. |
| 310 | * @vsync - the height of the vertical sync in lines. |
| 311 | * @polarities - the horizontal and vertical polarities (same as struct |
| 312 | * v4l2_bt_timings polarities). |
| 313 | * @fmt - the resulting timings. |
| 314 | * |
| 315 | * This function will attempt to detect if the given values correspond to a |
| 316 | * valid CVT format. If so, then it will return true, and fmt will be filled |
| 317 | * in with the found CVT timings. |
| 318 | */ |
| 319 | bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, |
| 320 | u32 polarities, struct v4l2_dv_timings *fmt) |
| 321 | { |
| 322 | int v_fp, v_bp, h_fp, h_bp, hsync; |
| 323 | int frame_width, image_height, image_width; |
| 324 | bool reduced_blanking; |
| 325 | unsigned pix_clk; |
| 326 | |
| 327 | if (vsync < 4 || vsync > 7) |
| 328 | return false; |
| 329 | |
| 330 | if (polarities == V4L2_DV_VSYNC_POS_POL) |
| 331 | reduced_blanking = false; |
| 332 | else if (polarities == V4L2_DV_HSYNC_POS_POL) |
| 333 | reduced_blanking = true; |
| 334 | else |
| 335 | return false; |
| 336 | |
| 337 | /* Vertical */ |
| 338 | if (reduced_blanking) { |
| 339 | v_fp = CVT_RB_V_FPORCH; |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 340 | v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 1999999) / 1000000; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 341 | v_bp -= vsync + v_fp; |
| 342 | |
| 343 | if (v_bp < CVT_RB_MIN_V_BPORCH) |
| 344 | v_bp = CVT_RB_MIN_V_BPORCH; |
| 345 | } else { |
| 346 | v_fp = CVT_MIN_V_PORCH_RND; |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 347 | v_bp = (CVT_MIN_VSYNC_BP * hfreq + 1999999) / 1000000 - vsync; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 348 | |
| 349 | if (v_bp < CVT_MIN_V_BPORCH) |
| 350 | v_bp = CVT_MIN_V_BPORCH; |
| 351 | } |
| 352 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; |
| 353 | |
| 354 | /* Aspect ratio based on vsync */ |
| 355 | switch (vsync) { |
| 356 | case 4: |
| 357 | image_width = (image_height * 4) / 3; |
| 358 | break; |
| 359 | case 5: |
| 360 | image_width = (image_height * 16) / 9; |
| 361 | break; |
| 362 | case 6: |
| 363 | image_width = (image_height * 16) / 10; |
| 364 | break; |
| 365 | case 7: |
| 366 | /* special case */ |
| 367 | if (image_height == 1024) |
| 368 | image_width = (image_height * 5) / 4; |
| 369 | else if (image_height == 768) |
| 370 | image_width = (image_height * 15) / 9; |
| 371 | else |
| 372 | return false; |
| 373 | break; |
| 374 | default: |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | image_width = image_width & ~7; |
| 379 | |
| 380 | /* Horizontal */ |
| 381 | if (reduced_blanking) { |
| 382 | pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq; |
| 383 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; |
| 384 | |
| 385 | h_bp = CVT_RB_H_BPORCH; |
| 386 | hsync = CVT_RB_H_SYNC; |
| 387 | h_fp = CVT_RB_H_BLANK - h_bp - hsync; |
| 388 | |
| 389 | frame_width = image_width + CVT_RB_H_BLANK; |
| 390 | } else { |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 391 | unsigned ideal_duty_cycle_per_myriad = |
| 392 | 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 393 | int h_blank; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 394 | |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 395 | if (ideal_duty_cycle_per_myriad < 2000) |
| 396 | ideal_duty_cycle_per_myriad = 2000; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 397 | |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 398 | h_blank = image_width * ideal_duty_cycle_per_myriad / |
| 399 | (10000 - ideal_duty_cycle_per_myriad); |
| 400 | h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 401 | |
| 402 | pix_clk = (image_width + h_blank) * hfreq; |
| 403 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; |
| 404 | |
| 405 | h_bp = h_blank / 2; |
| 406 | frame_width = image_width + h_blank; |
| 407 | |
| 408 | hsync = (frame_width * 8 + 50) / 100; |
| 409 | hsync = hsync - hsync % CVT_CELL_GRAN; |
| 410 | h_fp = h_blank - hsync - h_bp; |
| 411 | } |
| 412 | |
Hans Verkuil | 074ca43 | 2013-08-19 08:38:29 -0300 | [diff] [blame] | 413 | fmt->type = V4L2_DV_BT_656_1120; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 414 | fmt->bt.polarities = polarities; |
| 415 | fmt->bt.width = image_width; |
| 416 | fmt->bt.height = image_height; |
| 417 | fmt->bt.hfrontporch = h_fp; |
| 418 | fmt->bt.vfrontporch = v_fp; |
| 419 | fmt->bt.hsync = hsync; |
| 420 | fmt->bt.vsync = vsync; |
| 421 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; |
| 422 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; |
| 423 | fmt->bt.pixelclock = pix_clk; |
| 424 | fmt->bt.standards = V4L2_DV_BT_STD_CVT; |
| 425 | if (reduced_blanking) |
| 426 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; |
| 427 | return true; |
| 428 | } |
| 429 | EXPORT_SYMBOL_GPL(v4l2_detect_cvt); |
| 430 | |
| 431 | /* |
| 432 | * GTF defines |
| 433 | * Based on Generalized Timing Formula Standard |
| 434 | * Version 1.1 September 2, 1999 |
| 435 | */ |
| 436 | |
| 437 | #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */ |
| 438 | |
| 439 | #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ |
| 440 | #define GTF_V_FP 1 /* vertical front porch (lines) */ |
| 441 | #define GTF_CELL_GRAN 8 /* character cell granularity */ |
| 442 | |
| 443 | /* Default */ |
| 444 | #define GTF_D_M 600 /* blanking formula gradient */ |
| 445 | #define GTF_D_C 40 /* blanking formula offset */ |
| 446 | #define GTF_D_K 128 /* blanking formula scaling factor */ |
| 447 | #define GTF_D_J 20 /* blanking formula scaling factor */ |
| 448 | #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J) |
| 449 | #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256) |
| 450 | |
| 451 | /* Secondary */ |
| 452 | #define GTF_S_M 3600 /* blanking formula gradient */ |
| 453 | #define GTF_S_C 40 /* blanking formula offset */ |
| 454 | #define GTF_S_K 128 /* blanking formula scaling factor */ |
| 455 | #define GTF_S_J 35 /* blanking formula scaling factor */ |
| 456 | #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J) |
| 457 | #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256) |
| 458 | |
| 459 | /** v4l2_detect_gtf - detect if the given timings follow the GTF standard |
| 460 | * @frame_height - the total height of the frame (including blanking) in lines. |
| 461 | * @hfreq - the horizontal frequency in Hz. |
| 462 | * @vsync - the height of the vertical sync in lines. |
| 463 | * @polarities - the horizontal and vertical polarities (same as struct |
| 464 | * v4l2_bt_timings polarities). |
| 465 | * @aspect - preferred aspect ratio. GTF has no method of determining the |
| 466 | * aspect ratio in order to derive the image width from the |
| 467 | * image height, so it has to be passed explicitly. Usually |
| 468 | * the native screen aspect ratio is used for this. If it |
| 469 | * is not filled in correctly, then 16:9 will be assumed. |
| 470 | * @fmt - the resulting timings. |
| 471 | * |
| 472 | * This function will attempt to detect if the given values correspond to a |
| 473 | * valid GTF format. If so, then it will return true, and fmt will be filled |
| 474 | * in with the found GTF timings. |
| 475 | */ |
| 476 | bool v4l2_detect_gtf(unsigned frame_height, |
| 477 | unsigned hfreq, |
| 478 | unsigned vsync, |
| 479 | u32 polarities, |
| 480 | struct v4l2_fract aspect, |
| 481 | struct v4l2_dv_timings *fmt) |
| 482 | { |
| 483 | int pix_clk; |
| 484 | int v_fp, v_bp, h_fp, hsync; |
| 485 | int frame_width, image_height, image_width; |
| 486 | bool default_gtf; |
| 487 | int h_blank; |
| 488 | |
| 489 | if (vsync != 3) |
| 490 | return false; |
| 491 | |
| 492 | if (polarities == V4L2_DV_VSYNC_POS_POL) |
| 493 | default_gtf = true; |
| 494 | else if (polarities == V4L2_DV_HSYNC_POS_POL) |
| 495 | default_gtf = false; |
| 496 | else |
| 497 | return false; |
| 498 | |
| 499 | /* Vertical */ |
| 500 | v_fp = GTF_V_FP; |
| 501 | v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync; |
| 502 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; |
| 503 | |
| 504 | if (aspect.numerator == 0 || aspect.denominator == 0) { |
| 505 | aspect.numerator = 16; |
| 506 | aspect.denominator = 9; |
| 507 | } |
| 508 | image_width = ((image_height * aspect.numerator) / aspect.denominator); |
| 509 | |
| 510 | /* Horizontal */ |
| 511 | if (default_gtf) |
| 512 | h_blank = ((image_width * GTF_D_C_PRIME * hfreq) - |
| 513 | (image_width * GTF_D_M_PRIME * 1000) + |
| 514 | (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) / |
| 515 | (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000); |
| 516 | else |
| 517 | h_blank = ((image_width * GTF_S_C_PRIME * hfreq) - |
| 518 | (image_width * GTF_S_M_PRIME * 1000) + |
| 519 | (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) / |
| 520 | (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000); |
| 521 | |
| 522 | h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN); |
| 523 | frame_width = image_width + h_blank; |
| 524 | |
| 525 | pix_clk = (image_width + h_blank) * hfreq; |
| 526 | pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN; |
| 527 | |
| 528 | hsync = (frame_width * 8 + 50) / 100; |
| 529 | hsync = hsync - hsync % GTF_CELL_GRAN; |
| 530 | |
| 531 | h_fp = h_blank / 2 - hsync; |
| 532 | |
Hans Verkuil | 074ca43 | 2013-08-19 08:38:29 -0300 | [diff] [blame] | 533 | fmt->type = V4L2_DV_BT_656_1120; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 534 | fmt->bt.polarities = polarities; |
| 535 | fmt->bt.width = image_width; |
| 536 | fmt->bt.height = image_height; |
| 537 | fmt->bt.hfrontporch = h_fp; |
| 538 | fmt->bt.vfrontporch = v_fp; |
| 539 | fmt->bt.hsync = hsync; |
| 540 | fmt->bt.vsync = vsync; |
| 541 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; |
| 542 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; |
| 543 | fmt->bt.pixelclock = pix_clk; |
| 544 | fmt->bt.standards = V4L2_DV_BT_STD_GTF; |
| 545 | if (!default_gtf) |
| 546 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; |
| 547 | return true; |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(v4l2_detect_gtf); |
| 550 | |
| 551 | /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes |
| 552 | * 0x15 and 0x16 from the EDID. |
| 553 | * @hor_landscape - byte 0x15 from the EDID. |
| 554 | * @vert_portrait - byte 0x16 from the EDID. |
| 555 | * |
| 556 | * Determines the aspect ratio from the EDID. |
| 557 | * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2: |
| 558 | * "Horizontal and Vertical Screen Size or Aspect Ratio" |
| 559 | */ |
| 560 | struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait) |
| 561 | { |
| 562 | struct v4l2_fract aspect = { 16, 9 }; |
| 563 | u32 tmp; |
| 564 | u8 ratio; |
| 565 | |
| 566 | /* Nothing filled in, fallback to 16:9 */ |
| 567 | if (!hor_landscape && !vert_portrait) |
| 568 | return aspect; |
| 569 | /* Both filled in, so they are interpreted as the screen size in cm */ |
| 570 | if (hor_landscape && vert_portrait) { |
| 571 | aspect.numerator = hor_landscape; |
| 572 | aspect.denominator = vert_portrait; |
| 573 | return aspect; |
| 574 | } |
| 575 | /* Only one is filled in, so interpret them as a ratio: |
| 576 | (val + 99) / 100 */ |
| 577 | ratio = hor_landscape | vert_portrait; |
| 578 | /* Change some rounded values into the exact aspect ratio */ |
| 579 | if (ratio == 79) { |
| 580 | aspect.numerator = 16; |
| 581 | aspect.denominator = 9; |
| 582 | } else if (ratio == 34) { |
| 583 | aspect.numerator = 4; |
| 584 | aspect.numerator = 3; |
| 585 | } else if (ratio == 68) { |
| 586 | aspect.numerator = 15; |
| 587 | aspect.numerator = 9; |
| 588 | } else { |
| 589 | aspect.numerator = hor_landscape + 99; |
| 590 | aspect.denominator = 100; |
| 591 | } |
| 592 | if (hor_landscape) |
| 593 | return aspect; |
| 594 | /* The aspect ratio is for portrait, so swap numerator and denominator */ |
| 595 | tmp = aspect.denominator; |
| 596 | aspect.denominator = aspect.numerator; |
| 597 | aspect.numerator = tmp; |
| 598 | return aspect; |
| 599 | } |
| 600 | EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio); |