v4l2-dv-timings.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. /*
  349. * CVT defines
  350. * Based on Coordinated Video Timings Standard
  351. * version 1.1 September 10, 2003
  352. */
  353. #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
  354. #define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/
  355. /* Normal blanking */
  356. #define CVT_MIN_V_BPORCH 7 /* lines */
  357. #define CVT_MIN_V_PORCH_RND 3 /* lines */
  358. #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
  359. #define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */
  360. /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
  361. #define CVT_CELL_GRAN 8 /* character cell granularity */
  362. #define CVT_M 600 /* blanking formula gradient */
  363. #define CVT_C 40 /* blanking formula offset */
  364. #define CVT_K 128 /* blanking formula scaling factor */
  365. #define CVT_J 20 /* blanking formula scaling factor */
  366. #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
  367. #define CVT_M_PRIME (CVT_K * CVT_M / 256)
  368. /* Reduced Blanking */
  369. #define CVT_RB_MIN_V_BPORCH 7 /* lines */
  370. #define CVT_RB_V_FPORCH 3 /* lines */
  371. #define CVT_RB_MIN_V_BLANK 460 /* us */
  372. #define CVT_RB_H_SYNC 32 /* pixels */
  373. #define CVT_RB_H_BLANK 160 /* pixels */
  374. /* Reduce blanking Version 2 */
  375. #define CVT_RB_V2_H_BLANK 80 /* pixels */
  376. #define CVT_RB_MIN_V_FPORCH 3 /* lines */
  377. #define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */
  378. #define CVT_RB_V_BPORCH 6 /* lines */
  379. /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
  380. * @frame_height - the total height of the frame (including blanking) in lines.
  381. * @hfreq - the horizontal frequency in Hz.
  382. * @vsync - the height of the vertical sync in lines.
  383. * @active_width - active width of image (does not include blanking). This
  384. * information is needed only in case of version 2 of reduced blanking.
  385. * In other cases, this parameter does not have any effect on timings.
  386. * @polarities - the horizontal and vertical polarities (same as struct
  387. * v4l2_bt_timings polarities).
  388. * @interlaced - if this flag is true, it indicates interlaced format
  389. * @fmt - the resulting timings.
  390. *
  391. * This function will attempt to detect if the given values correspond to a
  392. * valid CVT format. If so, then it will return true, and fmt will be filled
  393. * in with the found CVT timings.
  394. */
  395. bool v4l2_detect_cvt(unsigned frame_height,
  396. unsigned hfreq,
  397. unsigned vsync,
  398. unsigned active_width,
  399. u32 polarities,
  400. bool interlaced,
  401. struct v4l2_dv_timings *fmt)
  402. {
  403. int v_fp, v_bp, h_fp, h_bp, hsync;
  404. int frame_width, image_height, image_width;
  405. bool reduced_blanking;
  406. bool rb_v2 = false;
  407. unsigned pix_clk;
  408. if (vsync < 4 || vsync > 8)
  409. return false;
  410. if (polarities == V4L2_DV_VSYNC_POS_POL)
  411. reduced_blanking = false;
  412. else if (polarities == V4L2_DV_HSYNC_POS_POL)
  413. reduced_blanking = true;
  414. else
  415. return false;
  416. if (reduced_blanking && vsync == 8)
  417. rb_v2 = true;
  418. if (rb_v2 && active_width == 0)
  419. return false;
  420. if (!rb_v2 && vsync > 7)
  421. return false;
  422. if (hfreq == 0)
  423. return false;
  424. /* Vertical */
  425. if (reduced_blanking) {
  426. if (rb_v2) {
  427. v_bp = CVT_RB_V_BPORCH;
  428. v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
  429. v_fp -= vsync + v_bp;
  430. if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
  431. v_fp = CVT_RB_V2_MIN_V_FPORCH;
  432. } else {
  433. v_fp = CVT_RB_V_FPORCH;
  434. v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
  435. v_bp -= vsync + v_fp;
  436. if (v_bp < CVT_RB_MIN_V_BPORCH)
  437. v_bp = CVT_RB_MIN_V_BPORCH;
  438. }
  439. } else {
  440. v_fp = CVT_MIN_V_PORCH_RND;
  441. v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
  442. if (v_bp < CVT_MIN_V_BPORCH)
  443. v_bp = CVT_MIN_V_BPORCH;
  444. }
  445. if (interlaced)
  446. image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
  447. else
  448. image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
  449. if (image_height < 0)
  450. return false;
  451. /* Aspect ratio based on vsync */
  452. switch (vsync) {
  453. case 4:
  454. image_width = (image_height * 4) / 3;
  455. break;
  456. case 5:
  457. image_width = (image_height * 16) / 9;
  458. break;
  459. case 6:
  460. image_width = (image_height * 16) / 10;
  461. break;
  462. case 7:
  463. /* special case */
  464. if (image_height == 1024)
  465. image_width = (image_height * 5) / 4;
  466. else if (image_height == 768)
  467. image_width = (image_height * 15) / 9;
  468. else
  469. return false;
  470. break;
  471. case 8:
  472. image_width = active_width;
  473. break;
  474. default:
  475. return false;
  476. }
  477. if (!rb_v2)
  478. image_width = image_width & ~7;
  479. /* Horizontal */
  480. if (reduced_blanking) {
  481. int h_blank;
  482. int clk_gran;
  483. h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
  484. clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
  485. pix_clk = (image_width + h_blank) * hfreq;
  486. pix_clk = (pix_clk / clk_gran) * clk_gran;
  487. h_bp = h_blank / 2;
  488. hsync = CVT_RB_H_SYNC;
  489. h_fp = h_blank - h_bp - hsync;
  490. frame_width = image_width + h_blank;
  491. } else {
  492. unsigned ideal_duty_cycle_per_myriad =
  493. 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
  494. int h_blank;
  495. if (ideal_duty_cycle_per_myriad < 2000)
  496. ideal_duty_cycle_per_myriad = 2000;
  497. h_blank = image_width * ideal_duty_cycle_per_myriad /
  498. (10000 - ideal_duty_cycle_per_myriad);
  499. h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
  500. pix_clk = (image_width + h_blank) * hfreq;
  501. pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
  502. h_bp = h_blank / 2;
  503. frame_width = image_width + h_blank;
  504. hsync = frame_width * CVT_HSYNC_PERCENT / 100;
  505. hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
  506. h_fp = h_blank - hsync - h_bp;
  507. }
  508. fmt->type = V4L2_DV_BT_656_1120;
  509. fmt->bt.polarities = polarities;
  510. fmt->bt.width = image_width;
  511. fmt->bt.height = image_height;
  512. fmt->bt.hfrontporch = h_fp;
  513. fmt->bt.vfrontporch = v_fp;
  514. fmt->bt.hsync = hsync;
  515. fmt->bt.vsync = vsync;
  516. fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
  517. if (!interlaced) {
  518. fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
  519. fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
  520. } else {
  521. fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
  522. 2 * vsync) / 2;
  523. fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
  524. 2 * vsync - fmt->bt.vbackporch;
  525. fmt->bt.il_vfrontporch = v_fp;
  526. fmt->bt.il_vsync = vsync;
  527. fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
  528. fmt->bt.interlaced = V4L2_DV_INTERLACED;
  529. }
  530. fmt->bt.pixelclock = pix_clk;
  531. fmt->bt.standards = V4L2_DV_BT_STD_CVT;
  532. if (reduced_blanking)
  533. fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
  534. return true;
  535. }
  536. EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
  537. /*
  538. * GTF defines
  539. * Based on Generalized Timing Formula Standard
  540. * Version 1.1 September 2, 1999
  541. */
  542. #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
  543. #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
  544. #define GTF_V_FP 1 /* vertical front porch (lines) */
  545. #define GTF_CELL_GRAN 8 /* character cell granularity */
  546. /* Default */
  547. #define GTF_D_M 600 /* blanking formula gradient */
  548. #define GTF_D_C 40 /* blanking formula offset */
  549. #define GTF_D_K 128 /* blanking formula scaling factor */
  550. #define GTF_D_J 20 /* blanking formula scaling factor */
  551. #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
  552. #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
  553. /* Secondary */
  554. #define GTF_S_M 3600 /* blanking formula gradient */
  555. #define GTF_S_C 40 /* blanking formula offset */
  556. #define GTF_S_K 128 /* blanking formula scaling factor */
  557. #define GTF_S_J 35 /* blanking formula scaling factor */
  558. #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
  559. #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
  560. /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
  561. * @frame_height - the total height of the frame (including blanking) in lines.
  562. * @hfreq - the horizontal frequency in Hz.
  563. * @vsync - the height of the vertical sync in lines.
  564. * @polarities - the horizontal and vertical polarities (same as struct
  565. * v4l2_bt_timings polarities).
  566. * @interlaced - if this flag is true, it indicates interlaced format
  567. * @aspect - preferred aspect ratio. GTF has no method of determining the
  568. * aspect ratio in order to derive the image width from the
  569. * image height, so it has to be passed explicitly. Usually
  570. * the native screen aspect ratio is used for this. If it
  571. * is not filled in correctly, then 16:9 will be assumed.
  572. * @fmt - the resulting timings.
  573. *
  574. * This function will attempt to detect if the given values correspond to a
  575. * valid GTF format. If so, then it will return true, and fmt will be filled
  576. * in with the found GTF timings.
  577. */
  578. bool v4l2_detect_gtf(unsigned frame_height,
  579. unsigned hfreq,
  580. unsigned vsync,
  581. u32 polarities,
  582. bool interlaced,
  583. struct v4l2_fract aspect,
  584. struct v4l2_dv_timings *fmt)
  585. {
  586. int pix_clk;
  587. int v_fp, v_bp, h_fp, hsync;
  588. int frame_width, image_height, image_width;
  589. bool default_gtf;
  590. int h_blank;
  591. if (vsync != 3)
  592. return false;
  593. if (polarities == V4L2_DV_VSYNC_POS_POL)
  594. default_gtf = true;
  595. else if (polarities == V4L2_DV_HSYNC_POS_POL)
  596. default_gtf = false;
  597. else
  598. return false;
  599. if (hfreq == 0)
  600. return false;
  601. /* Vertical */
  602. v_fp = GTF_V_FP;
  603. v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
  604. if (interlaced)
  605. image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
  606. else
  607. image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
  608. if (image_height < 0)
  609. return false;
  610. if (aspect.numerator == 0 || aspect.denominator == 0) {
  611. aspect.numerator = 16;
  612. aspect.denominator = 9;
  613. }
  614. image_width = ((image_height * aspect.numerator) / aspect.denominator);
  615. image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
  616. /* Horizontal */
  617. if (default_gtf) {
  618. u64 num;
  619. u32 den;
  620. num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
  621. ((u64)image_width * GTF_D_M_PRIME * 1000));
  622. den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
  623. (2 * GTF_CELL_GRAN);
  624. h_blank = div_u64((num + (den >> 1)), den);
  625. h_blank *= (2 * GTF_CELL_GRAN);
  626. } else {
  627. u64 num;
  628. u32 den;
  629. num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
  630. ((u64)image_width * GTF_S_M_PRIME * 1000));
  631. den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
  632. (2 * GTF_CELL_GRAN);
  633. h_blank = div_u64((num + (den >> 1)), den);
  634. h_blank *= (2 * GTF_CELL_GRAN);
  635. }
  636. frame_width = image_width + h_blank;
  637. pix_clk = (image_width + h_blank) * hfreq;
  638. pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
  639. hsync = (frame_width * 8 + 50) / 100;
  640. hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
  641. h_fp = h_blank / 2 - hsync;
  642. fmt->type = V4L2_DV_BT_656_1120;
  643. fmt->bt.polarities = polarities;
  644. fmt->bt.width = image_width;
  645. fmt->bt.height = image_height;
  646. fmt->bt.hfrontporch = h_fp;
  647. fmt->bt.vfrontporch = v_fp;
  648. fmt->bt.hsync = hsync;
  649. fmt->bt.vsync = vsync;
  650. fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
  651. if (!interlaced) {
  652. fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
  653. fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
  654. } else {
  655. fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
  656. 2 * vsync) / 2;
  657. fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
  658. 2 * vsync - fmt->bt.vbackporch;
  659. fmt->bt.il_vfrontporch = v_fp;
  660. fmt->bt.il_vsync = vsync;
  661. fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
  662. fmt->bt.interlaced = V4L2_DV_INTERLACED;
  663. }
  664. fmt->bt.pixelclock = pix_clk;
  665. fmt->bt.standards = V4L2_DV_BT_STD_GTF;
  666. if (!default_gtf)
  667. fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
  668. return true;
  669. }
  670. EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
  671. /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
  672. * 0x15 and 0x16 from the EDID.
  673. * @hor_landscape - byte 0x15 from the EDID.
  674. * @vert_portrait - byte 0x16 from the EDID.
  675. *
  676. * Determines the aspect ratio from the EDID.
  677. * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
  678. * "Horizontal and Vertical Screen Size or Aspect Ratio"
  679. */
  680. struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
  681. {
  682. struct v4l2_fract aspect = { 16, 9 };
  683. u8 ratio;
  684. /* Nothing filled in, fallback to 16:9 */
  685. if (!hor_landscape && !vert_portrait)
  686. return aspect;
  687. /* Both filled in, so they are interpreted as the screen size in cm */
  688. if (hor_landscape && vert_portrait) {
  689. aspect.numerator = hor_landscape;
  690. aspect.denominator = vert_portrait;
  691. return aspect;
  692. }
  693. /* Only one is filled in, so interpret them as a ratio:
  694. (val + 99) / 100 */
  695. ratio = hor_landscape | vert_portrait;
  696. /* Change some rounded values into the exact aspect ratio */
  697. if (ratio == 79) {
  698. aspect.numerator = 16;
  699. aspect.denominator = 9;
  700. } else if (ratio == 34) {
  701. aspect.numerator = 4;
  702. aspect.denominator = 3;
  703. } else if (ratio == 68) {
  704. aspect.numerator = 15;
  705. aspect.denominator = 9;
  706. } else {
  707. aspect.numerator = hor_landscape + 99;
  708. aspect.denominator = 100;
  709. }
  710. if (hor_landscape)
  711. return aspect;
  712. /* The aspect ratio is for portrait, so swap numerator and denominator */
  713. swap(aspect.denominator, aspect.numerator);
  714. return aspect;
  715. }
  716. EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
  717. /** v4l2_hdmi_rx_colorimetry - determine HDMI colorimetry information
  718. * based on various InfoFrames.
  719. * @avi: the AVI InfoFrame
  720. * @hdmi: the HDMI Vendor InfoFrame, may be NULL
  721. * @height: the frame height
  722. *
  723. * Determines the HDMI colorimetry information, i.e. how the HDMI
  724. * pixel color data should be interpreted.
  725. *
  726. * Note that some of the newer features (DCI-P3, HDR) are not yet
  727. * implemented: the hdmi.h header needs to be updated to the HDMI 2.0
  728. * and CTA-861-G standards.
  729. */
  730. struct v4l2_hdmi_colorimetry
  731. v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
  732. const struct hdmi_vendor_infoframe *hdmi,
  733. unsigned int height)
  734. {
  735. struct v4l2_hdmi_colorimetry c = {
  736. V4L2_COLORSPACE_SRGB,
  737. V4L2_YCBCR_ENC_DEFAULT,
  738. V4L2_QUANTIZATION_FULL_RANGE,
  739. V4L2_XFER_FUNC_SRGB
  740. };
  741. bool is_ce = avi->video_code || (hdmi && hdmi->vic);
  742. bool is_sdtv = height <= 576;
  743. bool default_is_lim_range_rgb = avi->video_code > 1;
  744. switch (avi->colorspace) {
  745. case HDMI_COLORSPACE_RGB:
  746. /* RGB pixel encoding */
  747. switch (avi->colorimetry) {
  748. case HDMI_COLORIMETRY_EXTENDED:
  749. switch (avi->extended_colorimetry) {
  750. case HDMI_EXTENDED_COLORIMETRY_OPRGB:
  751. c.colorspace = V4L2_COLORSPACE_OPRGB;
  752. c.xfer_func = V4L2_XFER_FUNC_OPRGB;
  753. break;
  754. case HDMI_EXTENDED_COLORIMETRY_BT2020:
  755. c.colorspace = V4L2_COLORSPACE_BT2020;
  756. c.xfer_func = V4L2_XFER_FUNC_709;
  757. break;
  758. default:
  759. break;
  760. }
  761. break;
  762. default:
  763. break;
  764. }
  765. switch (avi->quantization_range) {
  766. case HDMI_QUANTIZATION_RANGE_LIMITED:
  767. c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
  768. break;
  769. case HDMI_QUANTIZATION_RANGE_FULL:
  770. break;
  771. default:
  772. if (default_is_lim_range_rgb)
  773. c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
  774. break;
  775. }
  776. break;
  777. default:
  778. /* YCbCr pixel encoding */
  779. c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
  780. switch (avi->colorimetry) {
  781. case HDMI_COLORIMETRY_NONE:
  782. if (!is_ce)
  783. break;
  784. if (is_sdtv) {
  785. c.colorspace = V4L2_COLORSPACE_SMPTE170M;
  786. c.ycbcr_enc = V4L2_YCBCR_ENC_601;
  787. } else {
  788. c.colorspace = V4L2_COLORSPACE_REC709;
  789. c.ycbcr_enc = V4L2_YCBCR_ENC_709;
  790. }
  791. c.xfer_func = V4L2_XFER_FUNC_709;
  792. break;
  793. case HDMI_COLORIMETRY_ITU_601:
  794. c.colorspace = V4L2_COLORSPACE_SMPTE170M;
  795. c.ycbcr_enc = V4L2_YCBCR_ENC_601;
  796. c.xfer_func = V4L2_XFER_FUNC_709;
  797. break;
  798. case HDMI_COLORIMETRY_ITU_709:
  799. c.colorspace = V4L2_COLORSPACE_REC709;
  800. c.ycbcr_enc = V4L2_YCBCR_ENC_709;
  801. c.xfer_func = V4L2_XFER_FUNC_709;
  802. break;
  803. case HDMI_COLORIMETRY_EXTENDED:
  804. switch (avi->extended_colorimetry) {
  805. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
  806. c.colorspace = V4L2_COLORSPACE_REC709;
  807. c.ycbcr_enc = V4L2_YCBCR_ENC_XV709;
  808. c.xfer_func = V4L2_XFER_FUNC_709;
  809. break;
  810. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
  811. c.colorspace = V4L2_COLORSPACE_REC709;
  812. c.ycbcr_enc = V4L2_YCBCR_ENC_XV601;
  813. c.xfer_func = V4L2_XFER_FUNC_709;
  814. break;
  815. case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
  816. c.colorspace = V4L2_COLORSPACE_SRGB;
  817. c.ycbcr_enc = V4L2_YCBCR_ENC_601;
  818. c.xfer_func = V4L2_XFER_FUNC_SRGB;
  819. break;
  820. case HDMI_EXTENDED_COLORIMETRY_OPYCC_601:
  821. c.colorspace = V4L2_COLORSPACE_OPRGB;
  822. c.ycbcr_enc = V4L2_YCBCR_ENC_601;
  823. c.xfer_func = V4L2_XFER_FUNC_OPRGB;
  824. break;
  825. case HDMI_EXTENDED_COLORIMETRY_BT2020:
  826. c.colorspace = V4L2_COLORSPACE_BT2020;
  827. c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020;
  828. c.xfer_func = V4L2_XFER_FUNC_709;
  829. break;
  830. case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
  831. c.colorspace = V4L2_COLORSPACE_BT2020;
  832. c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020_CONST_LUM;
  833. c.xfer_func = V4L2_XFER_FUNC_709;
  834. break;
  835. default: /* fall back to ITU_709 */
  836. c.colorspace = V4L2_COLORSPACE_REC709;
  837. c.ycbcr_enc = V4L2_YCBCR_ENC_709;
  838. c.xfer_func = V4L2_XFER_FUNC_709;
  839. break;
  840. }
  841. break;
  842. default:
  843. break;
  844. }
  845. /*
  846. * YCC Quantization Range signaling is more-or-less broken,
  847. * let's just ignore this.
  848. */
  849. break;
  850. }
  851. return c;
  852. }
  853. EXPORT_SYMBOL_GPL(v4l2_hdmi_rx_colorimetry);
  854. /**
  855. * v4l2_get_edid_phys_addr() - find and return the physical address
  856. *
  857. * @edid: pointer to the EDID data
  858. * @size: size in bytes of the EDID data
  859. * @offset: If not %NULL then the location of the physical address
  860. * bytes in the EDID will be returned here. This is set to 0
  861. * if there is no physical address found.
  862. *
  863. * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
  864. */
  865. u16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,
  866. unsigned int *offset)
  867. {
  868. unsigned int loc = cec_get_edid_spa_location(edid, size);
  869. if (offset)
  870. *offset = loc;
  871. if (loc == 0)
  872. return CEC_PHYS_ADDR_INVALID;
  873. return (edid[loc] << 8) | edid[loc + 1];
  874. }
  875. EXPORT_SYMBOL_GPL(v4l2_get_edid_phys_addr);
  876. /**
  877. * v4l2_set_edid_phys_addr() - find and set the physical address
  878. *
  879. * @edid: pointer to the EDID data
  880. * @size: size in bytes of the EDID data
  881. * @phys_addr: the new physical address
  882. *
  883. * This function finds the location of the physical address in the EDID
  884. * and fills in the given physical address and updates the checksum
  885. * at the end of the EDID block. It does nothing if the EDID doesn't
  886. * contain a physical address.
  887. */
  888. void v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
  889. {
  890. unsigned int loc = cec_get_edid_spa_location(edid, size);
  891. u8 sum = 0;
  892. unsigned int i;
  893. if (loc == 0)
  894. return;
  895. edid[loc] = phys_addr >> 8;
  896. edid[loc + 1] = phys_addr & 0xff;
  897. loc &= ~0x7f;
  898. /* update the checksum */
  899. for (i = loc; i < loc + 127; i++)
  900. sum += edid[i];
  901. edid[i] = 256 - sum;
  902. }
  903. EXPORT_SYMBOL_GPL(v4l2_set_edid_phys_addr);
  904. /**
  905. * v4l2_phys_addr_for_input() - calculate the PA for an input
  906. *
  907. * @phys_addr: the physical address of the parent
  908. * @input: the number of the input port, must be between 1 and 15
  909. *
  910. * This function calculates a new physical address based on the input
  911. * port number. For example:
  912. *
  913. * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
  914. *
  915. * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
  916. *
  917. * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
  918. *
  919. * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
  920. *
  921. * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
  922. */
  923. u16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input)
  924. {
  925. /* Check if input is sane */
  926. if (WARN_ON(input == 0 || input > 0xf))
  927. return CEC_PHYS_ADDR_INVALID;
  928. if (phys_addr == 0)
  929. return input << 12;
  930. if ((phys_addr & 0x0fff) == 0)
  931. return phys_addr | (input << 8);
  932. if ((phys_addr & 0x00ff) == 0)
  933. return phys_addr | (input << 4);
  934. if ((phys_addr & 0x000f) == 0)
  935. return phys_addr | input;
  936. /*
  937. * All nibbles are used so no valid physical addresses can be assigned
  938. * to the input.
  939. */
  940. return CEC_PHYS_ADDR_INVALID;
  941. }
  942. EXPORT_SYMBOL_GPL(v4l2_phys_addr_for_input);
  943. /**
  944. * v4l2_phys_addr_validate() - validate a physical address from an EDID
  945. *
  946. * @phys_addr: the physical address to validate
  947. * @parent: if not %NULL, then this is filled with the parents PA.
  948. * @port: if not %NULL, then this is filled with the input port.
  949. *
  950. * This validates a physical address as read from an EDID. If the
  951. * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
  952. * then it will return -EINVAL.
  953. *
  954. * The parent PA is passed into %parent and the input port is passed into
  955. * %port. For example:
  956. *
  957. * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
  958. *
  959. * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
  960. *
  961. * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
  962. *
  963. * PA = f.f.f.f: has parent f.f.f.f and input port 0.
  964. *
  965. * Return: 0 if the PA is valid, -EINVAL if not.
  966. */
  967. int v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  968. {
  969. int i;
  970. if (parent)
  971. *parent = phys_addr;
  972. if (port)
  973. *port = 0;
  974. if (phys_addr == CEC_PHYS_ADDR_INVALID)
  975. return 0;
  976. for (i = 0; i < 16; i += 4)
  977. if (phys_addr & (0xf << i))
  978. break;
  979. if (i == 16)
  980. return 0;
  981. if (parent)
  982. *parent = phys_addr & (0xfff0 << i);
  983. if (port)
  984. *port = (phys_addr >> i) & 0xf;
  985. for (i += 4; i < 16; i += 4)
  986. if ((phys_addr & (0xf << i)) == 0)
  987. return -EINVAL;
  988. return 0;
  989. }
  990. EXPORT_SYMBOL_GPL(v4l2_phys_addr_validate);