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> |
Prashant Laddha | c9bc9f5 | 2015-05-05 10:16:27 -0300 | [diff] [blame] | 28 | #include <linux/math64.h> |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 29 | |
Hans Verkuil | c4885ad | 2014-03-04 04:51:50 -0300 | [diff] [blame] | 30 | MODULE_AUTHOR("Hans Verkuil"); |
| 31 | MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 34 | const struct v4l2_dv_timings v4l2_dv_timings_presets[] = { |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 35 | V4L2_DV_BT_CEA_640X480P59_94, |
| 36 | V4L2_DV_BT_CEA_720X480I59_94, |
| 37 | V4L2_DV_BT_CEA_720X480P59_94, |
| 38 | V4L2_DV_BT_CEA_720X576I50, |
| 39 | V4L2_DV_BT_CEA_720X576P50, |
| 40 | V4L2_DV_BT_CEA_1280X720P24, |
| 41 | V4L2_DV_BT_CEA_1280X720P25, |
| 42 | V4L2_DV_BT_CEA_1280X720P30, |
| 43 | V4L2_DV_BT_CEA_1280X720P50, |
| 44 | V4L2_DV_BT_CEA_1280X720P60, |
| 45 | V4L2_DV_BT_CEA_1920X1080P24, |
| 46 | V4L2_DV_BT_CEA_1920X1080P25, |
| 47 | V4L2_DV_BT_CEA_1920X1080P30, |
| 48 | V4L2_DV_BT_CEA_1920X1080I50, |
| 49 | V4L2_DV_BT_CEA_1920X1080P50, |
| 50 | V4L2_DV_BT_CEA_1920X1080I60, |
| 51 | V4L2_DV_BT_CEA_1920X1080P60, |
| 52 | V4L2_DV_BT_DMT_640X350P85, |
| 53 | V4L2_DV_BT_DMT_640X400P85, |
| 54 | V4L2_DV_BT_DMT_720X400P85, |
| 55 | V4L2_DV_BT_DMT_640X480P72, |
| 56 | V4L2_DV_BT_DMT_640X480P75, |
| 57 | V4L2_DV_BT_DMT_640X480P85, |
| 58 | V4L2_DV_BT_DMT_800X600P56, |
| 59 | V4L2_DV_BT_DMT_800X600P60, |
| 60 | V4L2_DV_BT_DMT_800X600P72, |
| 61 | V4L2_DV_BT_DMT_800X600P75, |
| 62 | V4L2_DV_BT_DMT_800X600P85, |
| 63 | V4L2_DV_BT_DMT_800X600P120_RB, |
| 64 | V4L2_DV_BT_DMT_848X480P60, |
| 65 | V4L2_DV_BT_DMT_1024X768I43, |
| 66 | V4L2_DV_BT_DMT_1024X768P60, |
| 67 | V4L2_DV_BT_DMT_1024X768P70, |
| 68 | V4L2_DV_BT_DMT_1024X768P75, |
| 69 | V4L2_DV_BT_DMT_1024X768P85, |
| 70 | V4L2_DV_BT_DMT_1024X768P120_RB, |
| 71 | V4L2_DV_BT_DMT_1152X864P75, |
| 72 | V4L2_DV_BT_DMT_1280X768P60_RB, |
| 73 | V4L2_DV_BT_DMT_1280X768P60, |
| 74 | V4L2_DV_BT_DMT_1280X768P75, |
| 75 | V4L2_DV_BT_DMT_1280X768P85, |
| 76 | V4L2_DV_BT_DMT_1280X768P120_RB, |
| 77 | V4L2_DV_BT_DMT_1280X800P60_RB, |
| 78 | V4L2_DV_BT_DMT_1280X800P60, |
| 79 | V4L2_DV_BT_DMT_1280X800P75, |
| 80 | V4L2_DV_BT_DMT_1280X800P85, |
| 81 | V4L2_DV_BT_DMT_1280X800P120_RB, |
| 82 | V4L2_DV_BT_DMT_1280X960P60, |
| 83 | V4L2_DV_BT_DMT_1280X960P85, |
| 84 | V4L2_DV_BT_DMT_1280X960P120_RB, |
| 85 | V4L2_DV_BT_DMT_1280X1024P60, |
| 86 | V4L2_DV_BT_DMT_1280X1024P75, |
| 87 | V4L2_DV_BT_DMT_1280X1024P85, |
| 88 | V4L2_DV_BT_DMT_1280X1024P120_RB, |
| 89 | V4L2_DV_BT_DMT_1360X768P60, |
| 90 | V4L2_DV_BT_DMT_1360X768P120_RB, |
| 91 | V4L2_DV_BT_DMT_1366X768P60, |
| 92 | V4L2_DV_BT_DMT_1366X768P60_RB, |
| 93 | V4L2_DV_BT_DMT_1400X1050P60_RB, |
| 94 | V4L2_DV_BT_DMT_1400X1050P60, |
| 95 | V4L2_DV_BT_DMT_1400X1050P75, |
| 96 | V4L2_DV_BT_DMT_1400X1050P85, |
| 97 | V4L2_DV_BT_DMT_1400X1050P120_RB, |
| 98 | V4L2_DV_BT_DMT_1440X900P60_RB, |
| 99 | V4L2_DV_BT_DMT_1440X900P60, |
| 100 | V4L2_DV_BT_DMT_1440X900P75, |
| 101 | V4L2_DV_BT_DMT_1440X900P85, |
| 102 | V4L2_DV_BT_DMT_1440X900P120_RB, |
| 103 | V4L2_DV_BT_DMT_1600X900P60_RB, |
| 104 | V4L2_DV_BT_DMT_1600X1200P60, |
| 105 | V4L2_DV_BT_DMT_1600X1200P65, |
| 106 | V4L2_DV_BT_DMT_1600X1200P70, |
| 107 | V4L2_DV_BT_DMT_1600X1200P75, |
| 108 | V4L2_DV_BT_DMT_1600X1200P85, |
| 109 | V4L2_DV_BT_DMT_1600X1200P120_RB, |
| 110 | V4L2_DV_BT_DMT_1680X1050P60_RB, |
| 111 | V4L2_DV_BT_DMT_1680X1050P60, |
| 112 | V4L2_DV_BT_DMT_1680X1050P75, |
| 113 | V4L2_DV_BT_DMT_1680X1050P85, |
| 114 | V4L2_DV_BT_DMT_1680X1050P120_RB, |
| 115 | V4L2_DV_BT_DMT_1792X1344P60, |
| 116 | V4L2_DV_BT_DMT_1792X1344P75, |
| 117 | V4L2_DV_BT_DMT_1792X1344P120_RB, |
| 118 | V4L2_DV_BT_DMT_1856X1392P60, |
| 119 | V4L2_DV_BT_DMT_1856X1392P75, |
| 120 | V4L2_DV_BT_DMT_1856X1392P120_RB, |
| 121 | V4L2_DV_BT_DMT_1920X1200P60_RB, |
| 122 | V4L2_DV_BT_DMT_1920X1200P60, |
| 123 | V4L2_DV_BT_DMT_1920X1200P75, |
| 124 | V4L2_DV_BT_DMT_1920X1200P85, |
| 125 | V4L2_DV_BT_DMT_1920X1200P120_RB, |
| 126 | V4L2_DV_BT_DMT_1920X1440P60, |
| 127 | V4L2_DV_BT_DMT_1920X1440P75, |
| 128 | V4L2_DV_BT_DMT_1920X1440P120_RB, |
| 129 | V4L2_DV_BT_DMT_2048X1152P60_RB, |
| 130 | V4L2_DV_BT_DMT_2560X1600P60_RB, |
| 131 | V4L2_DV_BT_DMT_2560X1600P60, |
| 132 | V4L2_DV_BT_DMT_2560X1600P75, |
| 133 | V4L2_DV_BT_DMT_2560X1600P85, |
| 134 | V4L2_DV_BT_DMT_2560X1600P120_RB, |
Hans Verkuil | bc96f30 | 2014-04-08 05:01:52 -0300 | [diff] [blame] | 135 | V4L2_DV_BT_CEA_3840X2160P24, |
| 136 | V4L2_DV_BT_CEA_3840X2160P25, |
| 137 | V4L2_DV_BT_CEA_3840X2160P30, |
| 138 | V4L2_DV_BT_CEA_3840X2160P50, |
| 139 | V4L2_DV_BT_CEA_3840X2160P60, |
| 140 | V4L2_DV_BT_CEA_4096X2160P24, |
| 141 | V4L2_DV_BT_CEA_4096X2160P25, |
| 142 | V4L2_DV_BT_CEA_4096X2160P30, |
| 143 | V4L2_DV_BT_CEA_4096X2160P50, |
| 144 | V4L2_DV_BT_DMT_4096X2160P59_94_RB, |
| 145 | V4L2_DV_BT_CEA_4096X2160P60, |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 146 | { } |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 147 | }; |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 148 | EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets); |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 149 | |
Hans Verkuil | 70b6549 | 2013-08-19 10:23:33 -0300 | [diff] [blame] | 150 | bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t, |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 151 | const struct v4l2_dv_timings_cap *dvcap, |
| 152 | v4l2_check_dv_timings_fnc fnc, |
| 153 | void *fnc_handle) |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 154 | { |
| 155 | const struct v4l2_bt_timings *bt = &t->bt; |
| 156 | const struct v4l2_bt_timings_cap *cap = &dvcap->bt; |
| 157 | u32 caps = cap->capabilities; |
| 158 | |
| 159 | if (t->type != V4L2_DV_BT_656_1120) |
| 160 | return false; |
| 161 | if (t->type != dvcap->type || |
| 162 | bt->height < cap->min_height || |
| 163 | bt->height > cap->max_height || |
| 164 | bt->width < cap->min_width || |
| 165 | bt->width > cap->max_width || |
| 166 | bt->pixelclock < cap->min_pixelclock || |
| 167 | bt->pixelclock > cap->max_pixelclock || |
Hans Verkuil | c166845 | 2014-09-20 07:36:38 -0300 | [diff] [blame] | 168 | (cap->standards && bt->standards && |
| 169 | !(bt->standards & cap->standards)) || |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 170 | (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) || |
| 171 | (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE))) |
| 172 | return false; |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 173 | return fnc == NULL || fnc(t, fnc_handle); |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 174 | } |
Hans Verkuil | 70b6549 | 2013-08-19 10:23:33 -0300 | [diff] [blame] | 175 | EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings); |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 176 | |
| 177 | int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t, |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 178 | const struct v4l2_dv_timings_cap *cap, |
| 179 | v4l2_check_dv_timings_fnc fnc, |
| 180 | void *fnc_handle) |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 181 | { |
| 182 | u32 i, idx; |
| 183 | |
| 184 | memset(t->reserved, 0, sizeof(t->reserved)); |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 185 | for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) { |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 186 | if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap, |
| 187 | fnc, fnc_handle) && |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 188 | idx++ == t->index) { |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 189 | t->timings = v4l2_dv_timings_presets[i]; |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | } |
| 193 | return -EINVAL; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap); |
| 196 | |
| 197 | bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t, |
| 198 | const struct v4l2_dv_timings_cap *cap, |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 199 | unsigned pclock_delta, |
| 200 | v4l2_check_dv_timings_fnc fnc, |
| 201 | void *fnc_handle) |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 202 | { |
| 203 | int i; |
| 204 | |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 205 | if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle)) |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 206 | return false; |
| 207 | |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 208 | for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) { |
Hans Verkuil | b8f0fff | 2013-08-19 11:21:50 -0300 | [diff] [blame] | 209 | if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap, |
| 210 | fnc, fnc_handle) && |
| 211 | v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i, |
| 212 | pclock_delta)) { |
Hans Verkuil | d1c65ad | 2013-08-19 10:19:54 -0300 | [diff] [blame] | 213 | *t = v4l2_dv_timings_presets[i]; |
Hans Verkuil | b18787e | 2013-07-29 08:40:55 -0300 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap); |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 220 | |
| 221 | /** |
Hans Verkuil | ef1ed8f | 2013-08-15 08:28:47 -0300 | [diff] [blame] | 222 | * v4l2_match_dv_timings - check if two timings match |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 223 | * @t1 - compare this v4l2_dv_timings struct... |
| 224 | * @t2 - with this struct. |
| 225 | * @pclock_delta - the allowed pixelclock deviation. |
| 226 | * |
| 227 | * Compare t1 with t2 with a given margin of error for the pixelclock. |
| 228 | */ |
Hans Verkuil | ef1ed8f | 2013-08-15 08:28:47 -0300 | [diff] [blame] | 229 | bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1, |
| 230 | const struct v4l2_dv_timings *t2, |
| 231 | unsigned pclock_delta) |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 232 | { |
| 233 | if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120) |
| 234 | return false; |
| 235 | if (t1->bt.width == t2->bt.width && |
| 236 | t1->bt.height == t2->bt.height && |
| 237 | t1->bt.interlaced == t2->bt.interlaced && |
| 238 | t1->bt.polarities == t2->bt.polarities && |
| 239 | t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta && |
| 240 | t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta && |
| 241 | t1->bt.hfrontporch == t2->bt.hfrontporch && |
Mats Randgaard | 74c9c0f | 2015-11-12 10:34:55 -0200 | [diff] [blame^] | 242 | t1->bt.hsync == t2->bt.hsync && |
| 243 | t1->bt.hbackporch == t2->bt.hbackporch && |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 244 | t1->bt.vfrontporch == t2->bt.vfrontporch && |
| 245 | t1->bt.vsync == t2->bt.vsync && |
| 246 | t1->bt.vbackporch == t2->bt.vbackporch && |
| 247 | (!t1->bt.interlaced || |
| 248 | (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch && |
| 249 | t1->bt.il_vsync == t2->bt.il_vsync && |
| 250 | t1->bt.il_vbackporch == t2->bt.il_vbackporch))) |
| 251 | return true; |
| 252 | return false; |
| 253 | } |
Hans Verkuil | ef1ed8f | 2013-08-15 08:28:47 -0300 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(v4l2_match_dv_timings); |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 255 | |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 256 | void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix, |
| 257 | const struct v4l2_dv_timings *t, bool detailed) |
| 258 | { |
| 259 | const struct v4l2_bt_timings *bt = &t->bt; |
| 260 | u32 htot, vtot; |
Prashant Laddha | 227da85 | 2015-06-12 08:48:10 -0300 | [diff] [blame] | 261 | u32 fps; |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 262 | |
| 263 | if (t->type != V4L2_DV_BT_656_1120) |
| 264 | return; |
| 265 | |
| 266 | htot = V4L2_DV_BT_FRAME_WIDTH(bt); |
| 267 | vtot = V4L2_DV_BT_FRAME_HEIGHT(bt); |
Hans Verkuil | 8cf6874 | 2015-06-05 05:30:02 -0300 | [diff] [blame] | 268 | if (bt->interlaced) |
| 269 | vtot /= 2; |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 270 | |
Prashant Laddha | 227da85 | 2015-06-12 08:48:10 -0300 | [diff] [blame] | 271 | fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock), |
| 272 | (htot * vtot)) : 0; |
| 273 | |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 274 | if (prefix == NULL) |
| 275 | prefix = ""; |
| 276 | |
Prashant Laddha | 227da85 | 2015-06-12 08:48:10 -0300 | [diff] [blame] | 277 | pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix, |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 278 | bt->width, bt->height, bt->interlaced ? "i" : "p", |
Prashant Laddha | 227da85 | 2015-06-12 08:48:10 -0300 | [diff] [blame] | 279 | fps / 100, fps % 100, htot, vtot); |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 280 | |
| 281 | if (!detailed) |
| 282 | return; |
| 283 | |
| 284 | pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n", |
| 285 | dev_prefix, bt->hfrontporch, |
| 286 | (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-", |
| 287 | bt->hsync, bt->hbackporch); |
| 288 | pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n", |
| 289 | dev_prefix, bt->vfrontporch, |
| 290 | (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-", |
| 291 | bt->vsync, bt->vbackporch); |
Hans Verkuil | 8cf6874 | 2015-06-05 05:30:02 -0300 | [diff] [blame] | 292 | if (bt->interlaced) |
| 293 | pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n", |
| 294 | dev_prefix, bt->il_vfrontporch, |
| 295 | (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-", |
| 296 | bt->il_vsync, bt->il_vbackporch); |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 297 | pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock); |
Hans Verkuil | 1b3b384 | 2015-06-12 03:52:54 -0300 | [diff] [blame] | 298 | pr_info("%s: flags (0x%x):%s%s%s%s%s%s\n", dev_prefix, bt->flags, |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 299 | (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ? |
| 300 | " REDUCED_BLANKING" : "", |
Hans Verkuil | 1b3b384 | 2015-06-12 03:52:54 -0300 | [diff] [blame] | 301 | ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) && |
| 302 | bt->vsync == 8) ? " (V2)" : "", |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 303 | (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ? |
| 304 | " CAN_REDUCE_FPS" : "", |
| 305 | (bt->flags & V4L2_DV_FL_REDUCED_FPS) ? |
| 306 | " REDUCED_FPS" : "", |
| 307 | (bt->flags & V4L2_DV_FL_HALF_LINE) ? |
Hans Verkuil | 74d802d | 2015-04-03 06:27:02 -0300 | [diff] [blame] | 308 | " HALF_LINE" : "", |
| 309 | (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ? |
| 310 | " CE_VIDEO" : ""); |
Hans Verkuil | 0216dc2 | 2013-08-15 08:02:40 -0300 | [diff] [blame] | 311 | pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards, |
| 312 | (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "", |
| 313 | (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "", |
| 314 | (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "", |
| 315 | (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : ""); |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(v4l2_print_dv_timings); |
| 318 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 319 | /* |
| 320 | * CVT defines |
| 321 | * Based on Coordinated Video Timings Standard |
| 322 | * version 1.1 September 10, 2003 |
| 323 | */ |
| 324 | |
| 325 | #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */ |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 326 | #define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/ |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 327 | |
| 328 | /* Normal blanking */ |
| 329 | #define CVT_MIN_V_BPORCH 7 /* lines */ |
| 330 | #define CVT_MIN_V_PORCH_RND 3 /* lines */ |
| 331 | #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ |
Prashant Laddha | 9c3f205 | 2015-04-22 14:32:37 -0300 | [diff] [blame] | 332 | #define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */ |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 333 | |
| 334 | /* Normal blanking for CVT uses GTF to calculate horizontal blanking */ |
| 335 | #define CVT_CELL_GRAN 8 /* character cell granularity */ |
| 336 | #define CVT_M 600 /* blanking formula gradient */ |
| 337 | #define CVT_C 40 /* blanking formula offset */ |
| 338 | #define CVT_K 128 /* blanking formula scaling factor */ |
| 339 | #define CVT_J 20 /* blanking formula scaling factor */ |
| 340 | #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J) |
| 341 | #define CVT_M_PRIME (CVT_K * CVT_M / 256) |
| 342 | |
| 343 | /* Reduced Blanking */ |
| 344 | #define CVT_RB_MIN_V_BPORCH 7 /* lines */ |
| 345 | #define CVT_RB_V_FPORCH 3 /* lines */ |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 346 | #define CVT_RB_MIN_V_BLANK 460 /* us */ |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 347 | #define CVT_RB_H_SYNC 32 /* pixels */ |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 348 | #define CVT_RB_H_BLANK 160 /* pixels */ |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 349 | /* Reduce blanking Version 2 */ |
| 350 | #define CVT_RB_V2_H_BLANK 80 /* pixels */ |
| 351 | #define CVT_RB_MIN_V_FPORCH 3 /* lines */ |
| 352 | #define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */ |
| 353 | #define CVT_RB_V_BPORCH 6 /* lines */ |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 354 | |
| 355 | /** v4l2_detect_cvt - detect if the given timings follow the CVT standard |
| 356 | * @frame_height - the total height of the frame (including blanking) in lines. |
| 357 | * @hfreq - the horizontal frequency in Hz. |
| 358 | * @vsync - the height of the vertical sync in lines. |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 359 | * @active_width - active width of image (does not include blanking). This |
| 360 | * information is needed only in case of version 2 of reduced blanking. |
| 361 | * In other cases, this parameter does not have any effect on timings. |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 362 | * @polarities - the horizontal and vertical polarities (same as struct |
| 363 | * v4l2_bt_timings polarities). |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 364 | * @interlaced - if this flag is true, it indicates interlaced format |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 365 | * @fmt - the resulting timings. |
| 366 | * |
| 367 | * This function will attempt to detect if the given values correspond to a |
| 368 | * valid CVT format. If so, then it will return true, and fmt will be filled |
| 369 | * in with the found CVT timings. |
| 370 | */ |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 371 | bool v4l2_detect_cvt(unsigned frame_height, |
| 372 | unsigned hfreq, |
| 373 | unsigned vsync, |
| 374 | unsigned active_width, |
| 375 | u32 polarities, |
| 376 | bool interlaced, |
| 377 | struct v4l2_dv_timings *fmt) |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 378 | { |
| 379 | int v_fp, v_bp, h_fp, h_bp, hsync; |
| 380 | int frame_width, image_height, image_width; |
| 381 | bool reduced_blanking; |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 382 | bool rb_v2 = false; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 383 | unsigned pix_clk; |
| 384 | |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 385 | if (vsync < 4 || vsync > 8) |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 386 | return false; |
| 387 | |
| 388 | if (polarities == V4L2_DV_VSYNC_POS_POL) |
| 389 | reduced_blanking = false; |
| 390 | else if (polarities == V4L2_DV_HSYNC_POS_POL) |
| 391 | reduced_blanking = true; |
| 392 | else |
| 393 | return false; |
| 394 | |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 395 | if (reduced_blanking && vsync == 8) |
| 396 | rb_v2 = true; |
| 397 | |
| 398 | if (rb_v2 && active_width == 0) |
| 399 | return false; |
| 400 | |
| 401 | if (!rb_v2 && vsync > 7) |
| 402 | return false; |
| 403 | |
Prashant Laddha | 947ed99 | 2015-04-22 14:32:36 -0300 | [diff] [blame] | 404 | if (hfreq == 0) |
| 405 | return false; |
| 406 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 407 | /* Vertical */ |
| 408 | if (reduced_blanking) { |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 409 | if (rb_v2) { |
| 410 | v_bp = CVT_RB_V_BPORCH; |
| 411 | v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1; |
| 412 | v_fp -= vsync + v_bp; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 413 | |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 414 | if (v_fp < CVT_RB_V2_MIN_V_FPORCH) |
| 415 | v_fp = CVT_RB_V2_MIN_V_FPORCH; |
| 416 | } else { |
| 417 | v_fp = CVT_RB_V_FPORCH; |
| 418 | v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1; |
| 419 | v_bp -= vsync + v_fp; |
| 420 | |
| 421 | if (v_bp < CVT_RB_MIN_V_BPORCH) |
| 422 | v_bp = CVT_RB_MIN_V_BPORCH; |
| 423 | } |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 424 | } else { |
| 425 | v_fp = CVT_MIN_V_PORCH_RND; |
Prashant Laddha | f674765 | 2015-04-22 14:32:34 -0300 | [diff] [blame] | 426 | v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 427 | |
| 428 | if (v_bp < CVT_MIN_V_BPORCH) |
| 429 | v_bp = CVT_MIN_V_BPORCH; |
| 430 | } |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 431 | |
| 432 | if (interlaced) |
| 433 | image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1; |
| 434 | else |
| 435 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 436 | |
Prashant Laddha | 947ed99 | 2015-04-22 14:32:36 -0300 | [diff] [blame] | 437 | if (image_height < 0) |
| 438 | return false; |
| 439 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 440 | /* Aspect ratio based on vsync */ |
| 441 | switch (vsync) { |
| 442 | case 4: |
| 443 | image_width = (image_height * 4) / 3; |
| 444 | break; |
| 445 | case 5: |
| 446 | image_width = (image_height * 16) / 9; |
| 447 | break; |
| 448 | case 6: |
| 449 | image_width = (image_height * 16) / 10; |
| 450 | break; |
| 451 | case 7: |
| 452 | /* special case */ |
| 453 | if (image_height == 1024) |
| 454 | image_width = (image_height * 5) / 4; |
| 455 | else if (image_height == 768) |
| 456 | image_width = (image_height * 15) / 9; |
| 457 | else |
| 458 | return false; |
| 459 | break; |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 460 | case 8: |
| 461 | image_width = active_width; |
| 462 | break; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 463 | default: |
| 464 | return false; |
| 465 | } |
| 466 | |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 467 | if (!rb_v2) |
| 468 | image_width = image_width & ~7; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 469 | |
| 470 | /* Horizontal */ |
| 471 | if (reduced_blanking) { |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 472 | int h_blank; |
| 473 | int clk_gran; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 474 | |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 475 | h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK; |
| 476 | clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN; |
| 477 | |
| 478 | pix_clk = (image_width + h_blank) * hfreq; |
| 479 | pix_clk = (pix_clk / clk_gran) * clk_gran; |
| 480 | |
| 481 | h_bp = h_blank / 2; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 482 | hsync = CVT_RB_H_SYNC; |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 483 | h_fp = h_blank - h_bp - hsync; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 484 | |
Prashant Laddha | 5fea1bb | 2015-06-10 13:51:42 -0300 | [diff] [blame] | 485 | frame_width = image_width + h_blank; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 486 | } else { |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 487 | unsigned ideal_duty_cycle_per_myriad = |
| 488 | 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 489 | int h_blank; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 490 | |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 491 | if (ideal_duty_cycle_per_myriad < 2000) |
| 492 | ideal_duty_cycle_per_myriad = 2000; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 493 | |
Martin Bugge | c3e75c7 | 2013-08-14 04:58:38 -0300 | [diff] [blame] | 494 | h_blank = image_width * ideal_duty_cycle_per_myriad / |
| 495 | (10000 - ideal_duty_cycle_per_myriad); |
| 496 | h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 497 | |
| 498 | pix_clk = (image_width + h_blank) * hfreq; |
| 499 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; |
| 500 | |
| 501 | h_bp = h_blank / 2; |
| 502 | frame_width = image_width + h_blank; |
| 503 | |
Prashant Laddha | 9c3f205 | 2015-04-22 14:32:37 -0300 | [diff] [blame] | 504 | hsync = frame_width * CVT_HSYNC_PERCENT / 100; |
Prashant Laddha | d7ed5a3 | 2015-04-22 14:32:35 -0300 | [diff] [blame] | 505 | hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 506 | h_fp = h_blank - hsync - h_bp; |
| 507 | } |
| 508 | |
Hans Verkuil | 074ca43 | 2013-08-19 08:38:29 -0300 | [diff] [blame] | 509 | fmt->type = V4L2_DV_BT_656_1120; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 510 | fmt->bt.polarities = polarities; |
| 511 | fmt->bt.width = image_width; |
| 512 | fmt->bt.height = image_height; |
| 513 | fmt->bt.hfrontporch = h_fp; |
| 514 | fmt->bt.vfrontporch = v_fp; |
| 515 | fmt->bt.hsync = hsync; |
| 516 | fmt->bt.vsync = vsync; |
| 517 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 518 | |
| 519 | if (!interlaced) { |
| 520 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; |
| 521 | fmt->bt.interlaced = V4L2_DV_PROGRESSIVE; |
| 522 | } else { |
| 523 | fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp - |
| 524 | 2 * vsync) / 2; |
| 525 | fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp - |
| 526 | 2 * vsync - fmt->bt.vbackporch; |
| 527 | fmt->bt.il_vfrontporch = v_fp; |
| 528 | fmt->bt.il_vsync = vsync; |
| 529 | fmt->bt.flags |= V4L2_DV_FL_HALF_LINE; |
| 530 | fmt->bt.interlaced = V4L2_DV_INTERLACED; |
| 531 | } |
| 532 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 533 | fmt->bt.pixelclock = pix_clk; |
| 534 | fmt->bt.standards = V4L2_DV_BT_STD_CVT; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 535 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 536 | if (reduced_blanking) |
| 537 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 538 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 539 | return true; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(v4l2_detect_cvt); |
| 542 | |
| 543 | /* |
| 544 | * GTF defines |
| 545 | * Based on Generalized Timing Formula Standard |
| 546 | * Version 1.1 September 2, 1999 |
| 547 | */ |
| 548 | |
| 549 | #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */ |
| 550 | |
| 551 | #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ |
| 552 | #define GTF_V_FP 1 /* vertical front porch (lines) */ |
| 553 | #define GTF_CELL_GRAN 8 /* character cell granularity */ |
| 554 | |
| 555 | /* Default */ |
| 556 | #define GTF_D_M 600 /* blanking formula gradient */ |
| 557 | #define GTF_D_C 40 /* blanking formula offset */ |
| 558 | #define GTF_D_K 128 /* blanking formula scaling factor */ |
| 559 | #define GTF_D_J 20 /* blanking formula scaling factor */ |
| 560 | #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J) |
| 561 | #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256) |
| 562 | |
| 563 | /* Secondary */ |
| 564 | #define GTF_S_M 3600 /* blanking formula gradient */ |
| 565 | #define GTF_S_C 40 /* blanking formula offset */ |
| 566 | #define GTF_S_K 128 /* blanking formula scaling factor */ |
| 567 | #define GTF_S_J 35 /* blanking formula scaling factor */ |
| 568 | #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J) |
| 569 | #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256) |
| 570 | |
| 571 | /** v4l2_detect_gtf - detect if the given timings follow the GTF standard |
| 572 | * @frame_height - the total height of the frame (including blanking) in lines. |
| 573 | * @hfreq - the horizontal frequency in Hz. |
| 574 | * @vsync - the height of the vertical sync in lines. |
| 575 | * @polarities - the horizontal and vertical polarities (same as struct |
| 576 | * v4l2_bt_timings polarities). |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 577 | * @interlaced - if this flag is true, it indicates interlaced format |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 578 | * @aspect - preferred aspect ratio. GTF has no method of determining the |
| 579 | * aspect ratio in order to derive the image width from the |
| 580 | * image height, so it has to be passed explicitly. Usually |
| 581 | * the native screen aspect ratio is used for this. If it |
| 582 | * is not filled in correctly, then 16:9 will be assumed. |
| 583 | * @fmt - the resulting timings. |
| 584 | * |
| 585 | * This function will attempt to detect if the given values correspond to a |
| 586 | * valid GTF format. If so, then it will return true, and fmt will be filled |
| 587 | * in with the found GTF timings. |
| 588 | */ |
| 589 | bool v4l2_detect_gtf(unsigned frame_height, |
| 590 | unsigned hfreq, |
| 591 | unsigned vsync, |
| 592 | u32 polarities, |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 593 | bool interlaced, |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 594 | struct v4l2_fract aspect, |
| 595 | struct v4l2_dv_timings *fmt) |
| 596 | { |
| 597 | int pix_clk; |
| 598 | int v_fp, v_bp, h_fp, hsync; |
| 599 | int frame_width, image_height, image_width; |
| 600 | bool default_gtf; |
| 601 | int h_blank; |
| 602 | |
| 603 | if (vsync != 3) |
| 604 | return false; |
| 605 | |
| 606 | if (polarities == V4L2_DV_VSYNC_POS_POL) |
| 607 | default_gtf = true; |
| 608 | else if (polarities == V4L2_DV_HSYNC_POS_POL) |
| 609 | default_gtf = false; |
| 610 | else |
| 611 | return false; |
| 612 | |
Prashant Laddha | 947ed99 | 2015-04-22 14:32:36 -0300 | [diff] [blame] | 613 | if (hfreq == 0) |
| 614 | return false; |
| 615 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 616 | /* Vertical */ |
| 617 | v_fp = GTF_V_FP; |
Prashant Laddha | f674765 | 2015-04-22 14:32:34 -0300 | [diff] [blame] | 618 | v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 619 | if (interlaced) |
| 620 | image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1; |
| 621 | else |
| 622 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 623 | |
Prashant Laddha | 947ed99 | 2015-04-22 14:32:36 -0300 | [diff] [blame] | 624 | if (image_height < 0) |
| 625 | return false; |
| 626 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 627 | if (aspect.numerator == 0 || aspect.denominator == 0) { |
| 628 | aspect.numerator = 16; |
| 629 | aspect.denominator = 9; |
| 630 | } |
| 631 | image_width = ((image_height * aspect.numerator) / aspect.denominator); |
Martin Bugge | 257cc4b | 2014-01-23 06:40:00 -0300 | [diff] [blame] | 632 | image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1); |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 633 | |
| 634 | /* Horizontal */ |
Prashant Laddha | c9bc9f5 | 2015-05-05 10:16:27 -0300 | [diff] [blame] | 635 | if (default_gtf) { |
| 636 | u64 num; |
| 637 | u32 den; |
| 638 | |
| 639 | num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) - |
| 640 | ((u64)image_width * GTF_D_M_PRIME * 1000)); |
Prashant Laddha | dc0cf4c | 2015-06-05 05:13:31 -0300 | [diff] [blame] | 641 | den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) * |
| 642 | (2 * GTF_CELL_GRAN); |
Prashant Laddha | c9bc9f5 | 2015-05-05 10:16:27 -0300 | [diff] [blame] | 643 | h_blank = div_u64((num + (den >> 1)), den); |
Prashant Laddha | dc0cf4c | 2015-06-05 05:13:31 -0300 | [diff] [blame] | 644 | h_blank *= (2 * GTF_CELL_GRAN); |
Prashant Laddha | c9bc9f5 | 2015-05-05 10:16:27 -0300 | [diff] [blame] | 645 | } else { |
| 646 | u64 num; |
| 647 | u32 den; |
| 648 | |
| 649 | num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) - |
| 650 | ((u64)image_width * GTF_S_M_PRIME * 1000)); |
Prashant Laddha | dc0cf4c | 2015-06-05 05:13:31 -0300 | [diff] [blame] | 651 | den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) * |
| 652 | (2 * GTF_CELL_GRAN); |
Prashant Laddha | c9bc9f5 | 2015-05-05 10:16:27 -0300 | [diff] [blame] | 653 | h_blank = div_u64((num + (den >> 1)), den); |
Prashant Laddha | dc0cf4c | 2015-06-05 05:13:31 -0300 | [diff] [blame] | 654 | h_blank *= (2 * GTF_CELL_GRAN); |
Prashant Laddha | c9bc9f5 | 2015-05-05 10:16:27 -0300 | [diff] [blame] | 655 | } |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 656 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 657 | frame_width = image_width + h_blank; |
| 658 | |
| 659 | pix_clk = (image_width + h_blank) * hfreq; |
| 660 | pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN; |
| 661 | |
| 662 | hsync = (frame_width * 8 + 50) / 100; |
Prashant Laddha | d7ed5a3 | 2015-04-22 14:32:35 -0300 | [diff] [blame] | 663 | hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 664 | |
| 665 | h_fp = h_blank / 2 - hsync; |
| 666 | |
Hans Verkuil | 074ca43 | 2013-08-19 08:38:29 -0300 | [diff] [blame] | 667 | fmt->type = V4L2_DV_BT_656_1120; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 668 | fmt->bt.polarities = polarities; |
| 669 | fmt->bt.width = image_width; |
| 670 | fmt->bt.height = image_height; |
| 671 | fmt->bt.hfrontporch = h_fp; |
| 672 | fmt->bt.vfrontporch = v_fp; |
| 673 | fmt->bt.hsync = hsync; |
| 674 | fmt->bt.vsync = vsync; |
| 675 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 676 | |
| 677 | if (!interlaced) { |
| 678 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; |
| 679 | fmt->bt.interlaced = V4L2_DV_PROGRESSIVE; |
| 680 | } else { |
| 681 | fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp - |
| 682 | 2 * vsync) / 2; |
| 683 | fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp - |
| 684 | 2 * vsync - fmt->bt.vbackporch; |
| 685 | fmt->bt.il_vfrontporch = v_fp; |
| 686 | fmt->bt.il_vsync = vsync; |
| 687 | fmt->bt.flags |= V4L2_DV_FL_HALF_LINE; |
| 688 | fmt->bt.interlaced = V4L2_DV_INTERLACED; |
| 689 | } |
| 690 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 691 | fmt->bt.pixelclock = pix_clk; |
| 692 | fmt->bt.standards = V4L2_DV_BT_STD_GTF; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 693 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 694 | if (!default_gtf) |
| 695 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; |
Prashant Laddha | 061ddda | 2015-05-22 02:27:34 -0300 | [diff] [blame] | 696 | |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 697 | return true; |
| 698 | } |
| 699 | EXPORT_SYMBOL_GPL(v4l2_detect_gtf); |
| 700 | |
| 701 | /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes |
| 702 | * 0x15 and 0x16 from the EDID. |
| 703 | * @hor_landscape - byte 0x15 from the EDID. |
| 704 | * @vert_portrait - byte 0x16 from the EDID. |
| 705 | * |
| 706 | * Determines the aspect ratio from the EDID. |
| 707 | * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2: |
| 708 | * "Horizontal and Vertical Screen Size or Aspect Ratio" |
| 709 | */ |
| 710 | struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait) |
| 711 | { |
| 712 | struct v4l2_fract aspect = { 16, 9 }; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 713 | u8 ratio; |
| 714 | |
| 715 | /* Nothing filled in, fallback to 16:9 */ |
| 716 | if (!hor_landscape && !vert_portrait) |
| 717 | return aspect; |
| 718 | /* Both filled in, so they are interpreted as the screen size in cm */ |
| 719 | if (hor_landscape && vert_portrait) { |
| 720 | aspect.numerator = hor_landscape; |
| 721 | aspect.denominator = vert_portrait; |
| 722 | return aspect; |
| 723 | } |
| 724 | /* Only one is filled in, so interpret them as a ratio: |
| 725 | (val + 99) / 100 */ |
| 726 | ratio = hor_landscape | vert_portrait; |
| 727 | /* Change some rounded values into the exact aspect ratio */ |
| 728 | if (ratio == 79) { |
| 729 | aspect.numerator = 16; |
| 730 | aspect.denominator = 9; |
| 731 | } else if (ratio == 34) { |
| 732 | aspect.numerator = 4; |
Rickard Strandqvist | f71920e | 2014-06-14 08:37:09 -0300 | [diff] [blame] | 733 | aspect.denominator = 3; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 734 | } else if (ratio == 68) { |
| 735 | aspect.numerator = 15; |
Rickard Strandqvist | f71920e | 2014-06-14 08:37:09 -0300 | [diff] [blame] | 736 | aspect.denominator = 9; |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 737 | } else { |
| 738 | aspect.numerator = hor_landscape + 99; |
| 739 | aspect.denominator = 100; |
| 740 | } |
| 741 | if (hor_landscape) |
| 742 | return aspect; |
| 743 | /* The aspect ratio is for portrait, so swap numerator and denominator */ |
Fabian Frederick | fd75242 | 2015-06-10 13:32:33 -0300 | [diff] [blame] | 744 | swap(aspect.denominator, aspect.numerator); |
Hans Verkuil | 2576415 | 2013-07-29 08:40:56 -0300 | [diff] [blame] | 745 | return aspect; |
| 746 | } |
| 747 | EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio); |