v4l2-dv-timings.c 23 KB

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