v4l2-dv-timings.c 20 KB

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