v4l2-dv-timings.c 19 KB

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