v4l2-dv-timings.c 35 KB

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