v4l2-dv-timings.c 28 KB

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