v4l2-dv-timings.c 33 KB

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