v4l2-dv-timings.c 25 KB

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