sde_edid_parser.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  7. #include <drm/drm_edid.h>
  8. #include <linux/hdmi.h>
  9. #include "sde_kms.h"
  10. #include "sde_edid_parser.h"
  11. #include "sde/sde_connector.h"
  12. #define DBC_START_OFFSET 4
  13. #define EDID_DTD_LEN 18
  14. enum data_block_types {
  15. RESERVED,
  16. AUDIO_DATA_BLOCK,
  17. VIDEO_DATA_BLOCK,
  18. VENDOR_SPECIFIC_DATA_BLOCK,
  19. SPEAKER_ALLOCATION_DATA_BLOCK,
  20. VESA_DTC_DATA_BLOCK,
  21. RESERVED2,
  22. USE_EXTENDED_TAG
  23. };
  24. static u8 *sde_find_edid_extension(struct edid *edid, int ext_id)
  25. {
  26. u8 *edid_ext = NULL;
  27. int i;
  28. /* No EDID or EDID extensions */
  29. if (edid == NULL || edid->extensions == 0)
  30. return NULL;
  31. /* Find CEA extension */
  32. for (i = 0; i < edid->extensions; i++) {
  33. edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
  34. if (edid_ext[0] == ext_id)
  35. break;
  36. }
  37. if (i == edid->extensions)
  38. return NULL;
  39. return edid_ext;
  40. }
  41. static u8 *sde_find_cea_extension(struct edid *edid)
  42. {
  43. return sde_find_edid_extension(edid, SDE_CEA_EXT);
  44. }
  45. static int
  46. sde_cea_db_payload_len(const u8 *db)
  47. {
  48. return db[0] & 0x1f;
  49. }
  50. static int
  51. sde_cea_db_tag(const u8 *db)
  52. {
  53. return db[0] >> 5;
  54. }
  55. static int
  56. sde_cea_revision(const u8 *cea)
  57. {
  58. return cea[1];
  59. }
  60. static int
  61. sde_cea_db_offsets(const u8 *cea, int *start, int *end)
  62. {
  63. /* Data block offset in CEA extension block */
  64. *start = 4;
  65. *end = cea[2];
  66. if (*end == 0)
  67. *end = 127;
  68. if (*end < 4 || *end > 127)
  69. return -ERANGE;
  70. return 0;
  71. }
  72. #define sde_for_each_cea_db(cea, i, start, end) \
  73. for ((i) = (start); \
  74. (i) < (end) && (i) + sde_cea_db_payload_len(&(cea)[(i)]) < (end); \
  75. (i) += sde_cea_db_payload_len(&(cea)[(i)]) + 1)
  76. static const u8 *_sde_edid_find_block(const u8 *in_buf, u32 start_offset,
  77. u8 type, u8 *len)
  78. {
  79. /* the start of data block collection, start of Video Data Block */
  80. u32 offset = start_offset;
  81. u32 dbc_offset = in_buf[2];
  82. SDE_EDID_DEBUG("%s +", __func__);
  83. /*
  84. * * edid buffer 1, byte 2 being 4 means no non-DTD/Data block
  85. * collection present.
  86. * * edid buffer 1, byte 2 being 0 means no non-DTD/DATA block
  87. * collection present and no DTD data present.
  88. */
  89. if ((dbc_offset == 0) || (dbc_offset == 4)) {
  90. SDE_EDID_DEBUG("EDID: no DTD or non-DTD data present\n");
  91. return NULL;
  92. }
  93. while (offset < dbc_offset) {
  94. u8 block_len = in_buf[offset] & 0x1F;
  95. if ((offset + block_len <= dbc_offset) &&
  96. (in_buf[offset] >> 5) == type) {
  97. *len = block_len;
  98. SDE_EDID_DEBUG("block=%d found @ 0x%x w/ len=%d\n",
  99. type, offset, block_len);
  100. return in_buf + offset;
  101. }
  102. offset += 1 + block_len;
  103. }
  104. return NULL;
  105. }
  106. static void sde_edid_extract_vendor_id(struct sde_edid_ctrl *edid_ctrl)
  107. {
  108. char *vendor_id;
  109. u32 id_codes;
  110. SDE_EDID_DEBUG("%s +", __func__);
  111. if (!edid_ctrl) {
  112. SDE_ERROR("%s: invalid input\n", __func__);
  113. return;
  114. }
  115. vendor_id = edid_ctrl->vendor_id;
  116. id_codes = ((u32)edid_ctrl->edid->mfg_id[0] << 8) +
  117. edid_ctrl->edid->mfg_id[1];
  118. vendor_id[0] = 'A' - 1 + ((id_codes >> 10) & 0x1F);
  119. vendor_id[1] = 'A' - 1 + ((id_codes >> 5) & 0x1F);
  120. vendor_id[2] = 'A' - 1 + (id_codes & 0x1F);
  121. vendor_id[3] = 0;
  122. SDE_EDID_DEBUG("vendor id is %s ", vendor_id);
  123. SDE_EDID_DEBUG("%s -", __func__);
  124. }
  125. static void _sde_edid_extract_audio_data_blocks(
  126. struct sde_edid_ctrl *edid_ctrl)
  127. {
  128. u8 len = 0;
  129. u8 adb_max = 0;
  130. const u8 *adb = NULL;
  131. u32 offset = DBC_START_OFFSET;
  132. u8 *cea = NULL;
  133. if (!edid_ctrl) {
  134. SDE_ERROR("invalid edid_ctrl\n");
  135. return;
  136. }
  137. SDE_EDID_DEBUG("%s +", __func__);
  138. cea = sde_find_cea_extension(edid_ctrl->edid);
  139. if (!cea) {
  140. SDE_DEBUG("CEA extension not found\n");
  141. return;
  142. }
  143. edid_ctrl->adb_size = 0;
  144. memset(edid_ctrl->audio_data_block, 0,
  145. sizeof(edid_ctrl->audio_data_block));
  146. do {
  147. len = 0;
  148. adb = _sde_edid_find_block(cea, offset, AUDIO_DATA_BLOCK,
  149. &len);
  150. if ((adb == NULL) || (len > MAX_AUDIO_DATA_BLOCK_SIZE ||
  151. adb_max >= MAX_NUMBER_ADB)) {
  152. if (!edid_ctrl->adb_size) {
  153. SDE_DEBUG("No/Invalid Audio Data Block\n");
  154. return;
  155. }
  156. continue;
  157. }
  158. memcpy(edid_ctrl->audio_data_block + edid_ctrl->adb_size,
  159. adb + 1, len);
  160. offset = (adb - cea) + 1 + len;
  161. edid_ctrl->adb_size += len;
  162. adb_max++;
  163. } while (adb);
  164. SDE_EDID_DEBUG("%s -", __func__);
  165. }
  166. static void sde_edid_parse_hdr_plus_info(struct drm_connector *connector,
  167. const u8 *db)
  168. {
  169. struct sde_connector *c_conn;
  170. c_conn = to_sde_connector(connector);
  171. c_conn->hdr_plus_app_ver = db[5] & VSVDB_HDR10_PLUS_APP_VER_MASK;
  172. }
  173. static void sde_edid_parse_vsvdb_info(struct drm_connector *connector,
  174. const u8 *db)
  175. {
  176. u8 db_len = 0;
  177. u32 ieee_code = 0;
  178. SDE_EDID_DEBUG("%s +\n", __func__);
  179. db_len = sde_cea_db_payload_len(db);
  180. if (db_len < 5)
  181. return;
  182. /* Bytes 2-4: IEEE 24-bit code, LSB first */
  183. ieee_code = db[2] | (db[3] << 8) | (db[4] << 16);
  184. if (ieee_code == VSVDB_HDR10_PLUS_IEEE_CODE)
  185. sde_edid_parse_hdr_plus_info(connector, db);
  186. SDE_EDID_DEBUG("%s -\n", __func__);
  187. }
  188. static bool sde_edid_is_luminance_value_present(u32 block_length,
  189. enum luminance_value value)
  190. {
  191. return block_length > NO_LUMINANCE_DATA && value <= block_length;
  192. }
  193. /*
  194. * sde_edid_parse_hdr_db - Parse the HDR extended block
  195. * @connector: connector for the external sink
  196. * @db: start of the HDR extended block
  197. *
  198. * Parses the HDR extended block to extract sink info for @connector.
  199. */
  200. static void
  201. sde_edid_parse_hdr_db(struct drm_connector *connector, const u8 *db)
  202. {
  203. u8 len = 0;
  204. struct sde_connector *c_conn;
  205. c_conn = to_sde_connector(connector);
  206. if (!db)
  207. return;
  208. len = db[0] & 0x1f;
  209. /* Byte 3: Electro-Optical Transfer Functions */
  210. c_conn->hdr_eotf = db[2] & 0x3F;
  211. /* Byte 4: Static Metadata Descriptor Type 1 */
  212. c_conn->hdr_metadata_type_one = (db[3] & BIT(0));
  213. /* Byte 5: Desired Content Maximum Luminance */
  214. if (sde_edid_is_luminance_value_present(len, MAXIMUM_LUMINANCE))
  215. c_conn->hdr_max_luminance = db[MAXIMUM_LUMINANCE];
  216. /* Byte 6: Desired Content Max Frame-average Luminance */
  217. if (sde_edid_is_luminance_value_present(len, FRAME_AVERAGE_LUMINANCE))
  218. c_conn->hdr_avg_luminance = db[FRAME_AVERAGE_LUMINANCE];
  219. /* Byte 7: Desired Content Min Luminance */
  220. if (sde_edid_is_luminance_value_present(len, MINIMUM_LUMINANCE))
  221. c_conn->hdr_min_luminance = db[MINIMUM_LUMINANCE];
  222. c_conn->hdr_supported = true;
  223. SDE_EDID_DEBUG("HDR electro-optical %d\n", c_conn->hdr_eotf);
  224. SDE_EDID_DEBUG("metadata desc 1 %d\n", c_conn->hdr_metadata_type_one);
  225. SDE_EDID_DEBUG("max luminance %d\n", c_conn->hdr_max_luminance);
  226. SDE_EDID_DEBUG("avg luminance %d\n", c_conn->hdr_avg_luminance);
  227. SDE_EDID_DEBUG("min luminance %d\n", c_conn->hdr_min_luminance);
  228. }
  229. /*
  230. * drm_extract_clrmetry_db - Parse the HDMI colorimetry extended block
  231. * @connector: connector corresponding to the HDMI sink
  232. * @db: start of the HDMI colorimetry extended block
  233. *
  234. * Parses the HDMI colorimetry block to extract sink info for @connector.
  235. */
  236. static void
  237. sde_parse_clrmetry_db(struct drm_connector *connector, const u8 *db)
  238. {
  239. struct sde_connector *c_conn;
  240. c_conn = to_sde_connector(connector);
  241. if (!db) {
  242. DRM_ERROR("invalid db\n");
  243. return;
  244. }
  245. /* Byte 3 Bit 0: xvYCC_601 */
  246. if (db[2] & BIT(0))
  247. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_xvYCC_601;
  248. /* Byte 3 Bit 1: xvYCC_709 */
  249. if (db[2] & BIT(1))
  250. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_xvYCC_709;
  251. /* Byte 3 Bit 2: sYCC_601 */
  252. if (db[2] & BIT(2))
  253. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_sYCC_601;
  254. /* Byte 3 Bit 3: ADOBE_YCC_601 */
  255. if (db[2] & BIT(3))
  256. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_ADOBE_YCC_601;
  257. /* Byte 3 Bit 4: ADOBE_RGB */
  258. if (db[2] & BIT(4))
  259. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_ADOBE_RGB;
  260. /* Byte 3 Bit 5: BT2020_CYCC */
  261. if (db[2] & BIT(5))
  262. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_BT2020_CYCC;
  263. /* Byte 3 Bit 6: BT2020_YCC */
  264. if (db[2] & BIT(6))
  265. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_BT2020_YCC;
  266. /* Byte 3 Bit 7: BT2020_RGB */
  267. if (db[2] & BIT(7))
  268. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_BT2020_RGB;
  269. /* Byte 4 Bit 7: DCI-P3 */
  270. if (db[3] & BIT(7))
  271. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_DCI_P3;
  272. DRM_DEBUG_KMS("colorimetry fmts = 0x%x\n", c_conn->color_enc_fmt);
  273. }
  274. /*
  275. * sde_edid_parse_extended_blk_info - Parse the HDMI extended tag blocks
  276. * @connector: connector corresponding to external sink
  277. * @edid: handle to the EDID structure
  278. * Parses the all extended tag blocks extract sink info for @connector.
  279. */
  280. static void
  281. sde_edid_parse_extended_blk_info(struct drm_connector *connector,
  282. struct edid *edid)
  283. {
  284. const u8 *cea = sde_find_cea_extension(edid);
  285. const u8 *db = NULL;
  286. if (cea && sde_cea_revision(cea) >= 3) {
  287. int i, start, end;
  288. if (sde_cea_db_offsets(cea, &start, &end))
  289. return;
  290. sde_for_each_cea_db(cea, i, start, end) {
  291. db = &cea[i];
  292. if (sde_cea_db_tag(db) == USE_EXTENDED_TAG) {
  293. SDE_EDID_DEBUG("found ext tag block = %d\n",
  294. db[1]);
  295. switch (db[1]) {
  296. case VENDOR_SPECIFIC_VIDEO_DATA_BLOCK:
  297. sde_edid_parse_vsvdb_info(connector,
  298. db);
  299. break;
  300. case HDR_STATIC_METADATA_DATA_BLOCK:
  301. sde_edid_parse_hdr_db(connector, db);
  302. break;
  303. case COLORIMETRY_EXTENDED_DATA_BLOCK:
  304. sde_parse_clrmetry_db(connector, db);
  305. default:
  306. break;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. static void _sde_edid_extract_speaker_allocation_data(
  313. struct sde_edid_ctrl *edid_ctrl)
  314. {
  315. u8 len;
  316. const u8 *sadb = NULL;
  317. u8 *cea = NULL;
  318. if (!edid_ctrl) {
  319. SDE_ERROR("invalid edid_ctrl\n");
  320. return;
  321. }
  322. SDE_EDID_DEBUG("%s +", __func__);
  323. cea = sde_find_cea_extension(edid_ctrl->edid);
  324. if (!cea) {
  325. SDE_DEBUG("CEA extension not found\n");
  326. return;
  327. }
  328. sadb = _sde_edid_find_block(cea, DBC_START_OFFSET,
  329. SPEAKER_ALLOCATION_DATA_BLOCK, &len);
  330. if ((sadb == NULL) || (len != MAX_SPKR_ALLOC_DATA_BLOCK_SIZE)) {
  331. SDE_DEBUG("No/Invalid Speaker Allocation Data Block\n");
  332. return;
  333. }
  334. memcpy(edid_ctrl->spkr_alloc_data_block, sadb + 1, len);
  335. edid_ctrl->sadb_size = len;
  336. SDE_EDID_DEBUG("speaker alloc data SP byte = %08x %s%s%s%s%s%s%s\n",
  337. sadb[1],
  338. (sadb[1] & BIT(0)) ? "FL/FR," : "",
  339. (sadb[1] & BIT(1)) ? "LFE," : "",
  340. (sadb[1] & BIT(2)) ? "FC," : "",
  341. (sadb[1] & BIT(3)) ? "RL/RR," : "",
  342. (sadb[1] & BIT(4)) ? "RC," : "",
  343. (sadb[1] & BIT(5)) ? "FLC/FRC," : "",
  344. (sadb[1] & BIT(6)) ? "RLC/RRC," : "");
  345. SDE_EDID_DEBUG("%s -", __func__);
  346. }
  347. struct sde_edid_ctrl *sde_edid_init(void)
  348. {
  349. struct sde_edid_ctrl *edid_ctrl = NULL;
  350. SDE_EDID_DEBUG("%s +\n", __func__);
  351. edid_ctrl = kzalloc(sizeof(*edid_ctrl), GFP_KERNEL);
  352. if (!edid_ctrl) {
  353. SDE_ERROR("edid_ctrl alloc failed\n");
  354. return NULL;
  355. }
  356. memset((edid_ctrl), 0, sizeof(*edid_ctrl));
  357. SDE_EDID_DEBUG("%s -\n", __func__);
  358. return edid_ctrl;
  359. }
  360. void sde_free_edid(void **input)
  361. {
  362. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
  363. SDE_EDID_DEBUG("%s +", __func__);
  364. kfree(edid_ctrl->edid);
  365. edid_ctrl->edid = NULL;
  366. }
  367. void sde_edid_deinit(void **input)
  368. {
  369. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
  370. SDE_EDID_DEBUG("%s +", __func__);
  371. sde_free_edid((void *)&edid_ctrl);
  372. kfree(edid_ctrl);
  373. SDE_EDID_DEBUG("%s -", __func__);
  374. }
  375. int _sde_edid_update_modes(struct drm_connector *connector,
  376. void *input)
  377. {
  378. int rc = 0;
  379. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
  380. SDE_EDID_DEBUG("%s +", __func__);
  381. if (edid_ctrl->edid) {
  382. drm_connector_update_edid_property(connector,
  383. edid_ctrl->edid);
  384. rc = drm_add_edid_modes(connector, edid_ctrl->edid);
  385. sde_edid_parse_extended_blk_info(connector,
  386. edid_ctrl->edid);
  387. SDE_EDID_DEBUG("%s -", __func__);
  388. return rc;
  389. }
  390. drm_connector_update_edid_property(connector, NULL);
  391. SDE_EDID_DEBUG("%s null edid -", __func__);
  392. return rc;
  393. }
  394. u8 sde_get_edid_checksum(void *input)
  395. {
  396. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
  397. struct edid *edid = NULL, *last_block = NULL;
  398. u8 *raw_edid = NULL;
  399. if (!edid_ctrl || !edid_ctrl->edid) {
  400. SDE_ERROR("invalid edid input\n");
  401. return 0;
  402. }
  403. edid = edid_ctrl->edid;
  404. raw_edid = (u8 *)edid;
  405. raw_edid += (edid->extensions * EDID_LENGTH);
  406. last_block = (struct edid *)raw_edid;
  407. if (last_block)
  408. return last_block->checksum;
  409. SDE_ERROR("Invalid block, no checksum\n");
  410. return 0;
  411. }
  412. bool sde_detect_hdmi_monitor(void *input)
  413. {
  414. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
  415. return drm_detect_hdmi_monitor(edid_ctrl->edid);
  416. }
  417. void sde_parse_edid(void *input)
  418. {
  419. struct sde_edid_ctrl *edid_ctrl;
  420. if (!input) {
  421. SDE_ERROR("Invalid input\n");
  422. return;
  423. }
  424. edid_ctrl = (struct sde_edid_ctrl *)(input);
  425. if (edid_ctrl->edid) {
  426. sde_edid_extract_vendor_id(edid_ctrl);
  427. _sde_edid_extract_audio_data_blocks(edid_ctrl);
  428. _sde_edid_extract_speaker_allocation_data(edid_ctrl);
  429. } else {
  430. SDE_ERROR("edid not present\n");
  431. }
  432. }
  433. void sde_get_edid(struct drm_connector *connector,
  434. struct i2c_adapter *adapter, void **input)
  435. {
  436. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
  437. edid_ctrl->edid = drm_get_edid(connector, adapter);
  438. SDE_EDID_DEBUG("%s +\n", __func__);
  439. if (!edid_ctrl->edid)
  440. SDE_ERROR("EDID read failed\n");
  441. if (edid_ctrl->edid)
  442. sde_parse_edid(edid_ctrl);
  443. SDE_EDID_DEBUG("%s -\n", __func__);
  444. };