sde_edid_parser.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2024 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. if (!c_conn->hdr_supported) {
  209. SDE_INFO("connected receiver does not support HDR\n");
  210. return;
  211. }
  212. len = db[0] & 0x1f;
  213. /* Byte 3: Electro-Optical Transfer Functions */
  214. c_conn->hdr_eotf = db[2] & 0x3F;
  215. /* Byte 4: Static Metadata Descriptor Type 1 */
  216. c_conn->hdr_metadata_type_one = (db[3] & BIT(0));
  217. /* Byte 5: Desired Content Maximum Luminance */
  218. if (sde_edid_is_luminance_value_present(len, MAXIMUM_LUMINANCE))
  219. c_conn->hdr_max_luminance = db[MAXIMUM_LUMINANCE];
  220. /* Byte 6: Desired Content Max Frame-average Luminance */
  221. if (sde_edid_is_luminance_value_present(len, FRAME_AVERAGE_LUMINANCE))
  222. c_conn->hdr_avg_luminance = db[FRAME_AVERAGE_LUMINANCE];
  223. /* Byte 7: Desired Content Min Luminance */
  224. if (sde_edid_is_luminance_value_present(len, MINIMUM_LUMINANCE))
  225. c_conn->hdr_min_luminance = db[MINIMUM_LUMINANCE];
  226. c_conn->hdr_supported = true;
  227. SDE_EDID_DEBUG("HDR electro-optical %d\n", c_conn->hdr_eotf);
  228. SDE_EDID_DEBUG("metadata desc 1 %d\n", c_conn->hdr_metadata_type_one);
  229. SDE_EDID_DEBUG("max luminance %d\n", c_conn->hdr_max_luminance);
  230. SDE_EDID_DEBUG("avg luminance %d\n", c_conn->hdr_avg_luminance);
  231. SDE_EDID_DEBUG("min luminance %d\n", c_conn->hdr_min_luminance);
  232. }
  233. /*
  234. * drm_extract_clrmetry_db - Parse the HDMI colorimetry extended block
  235. * @connector: connector corresponding to the HDMI sink
  236. * @db: start of the HDMI colorimetry extended block
  237. *
  238. * Parses the HDMI colorimetry block to extract sink info for @connector.
  239. */
  240. static void
  241. sde_parse_clrmetry_db(struct drm_connector *connector, const u8 *db)
  242. {
  243. struct sde_connector *c_conn;
  244. c_conn = to_sde_connector(connector);
  245. if (!db) {
  246. DRM_ERROR("invalid db\n");
  247. return;
  248. }
  249. /* Byte 3 Bit 0: xvYCC_601 */
  250. if (db[2] & BIT(0))
  251. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_xvYCC_601;
  252. /* Byte 3 Bit 1: xvYCC_709 */
  253. if (db[2] & BIT(1))
  254. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_xvYCC_709;
  255. /* Byte 3 Bit 2: sYCC_601 */
  256. if (db[2] & BIT(2))
  257. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_sYCC_601;
  258. /* Byte 3 Bit 3: ADOBE_YCC_601 */
  259. if (db[2] & BIT(3))
  260. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_ADOBE_YCC_601;
  261. /* Byte 3 Bit 4: ADOBE_RGB */
  262. if (db[2] & BIT(4))
  263. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_ADOBE_RGB;
  264. /* Byte 3 Bit 5: BT2020_CYCC */
  265. if (db[2] & BIT(5))
  266. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_BT2020_CYCC;
  267. /* Byte 3 Bit 6: BT2020_YCC */
  268. if (db[2] & BIT(6))
  269. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_BT2020_YCC;
  270. /* Byte 3 Bit 7: BT2020_RGB */
  271. if (db[2] & BIT(7))
  272. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_BT2020_RGB;
  273. /* Byte 4 Bit 7: DCI-P3 */
  274. if (db[3] & BIT(7))
  275. c_conn->color_enc_fmt |= DRM_EDID_CLRMETRY_DCI_P3;
  276. DRM_DEBUG_KMS("colorimetry fmts = 0x%x\n", c_conn->color_enc_fmt);
  277. }
  278. /*
  279. * sde_edid_parse_extended_blk_info - Parse the HDMI extended tag blocks
  280. * @connector: connector corresponding to external sink
  281. * @edid: handle to the EDID structure
  282. * Parses the all extended tag blocks extract sink info for @connector.
  283. */
  284. static void
  285. sde_edid_parse_extended_blk_info(struct drm_connector *connector,
  286. struct edid *edid)
  287. {
  288. const u8 *cea = sde_find_cea_extension(edid);
  289. const u8 *db = NULL;
  290. if (cea && sde_cea_revision(cea) >= 3) {
  291. int i, start, end;
  292. if (sde_cea_db_offsets(cea, &start, &end))
  293. return;
  294. sde_for_each_cea_db(cea, i, start, end) {
  295. db = &cea[i];
  296. if (sde_cea_db_tag(db) == USE_EXTENDED_TAG) {
  297. SDE_EDID_DEBUG("found ext tag block = %d\n",
  298. db[1]);
  299. switch (db[1]) {
  300. case VENDOR_SPECIFIC_VIDEO_DATA_BLOCK:
  301. sde_edid_parse_vsvdb_info(connector,
  302. db);
  303. break;
  304. case HDR_STATIC_METADATA_DATA_BLOCK:
  305. sde_edid_parse_hdr_db(connector, db);
  306. break;
  307. case COLORIMETRY_EXTENDED_DATA_BLOCK:
  308. sde_parse_clrmetry_db(connector, db);
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. static void _sde_edid_extract_speaker_allocation_data(
  318. struct sde_edid_ctrl *edid_ctrl)
  319. {
  320. u8 len;
  321. const u8 *sadb = NULL;
  322. u8 *cea = NULL;
  323. if (!edid_ctrl) {
  324. SDE_ERROR("invalid edid_ctrl\n");
  325. return;
  326. }
  327. SDE_EDID_DEBUG("%s +", __func__);
  328. cea = sde_find_cea_extension(edid_ctrl->edid);
  329. if (!cea) {
  330. SDE_DEBUG("CEA extension not found\n");
  331. return;
  332. }
  333. sadb = _sde_edid_find_block(cea, DBC_START_OFFSET,
  334. SPEAKER_ALLOCATION_DATA_BLOCK, &len);
  335. if ((sadb == NULL) || (len != MAX_SPKR_ALLOC_DATA_BLOCK_SIZE)) {
  336. SDE_DEBUG("No/Invalid Speaker Allocation Data Block\n");
  337. return;
  338. }
  339. memcpy(edid_ctrl->spkr_alloc_data_block, sadb + 1, len);
  340. edid_ctrl->sadb_size = len;
  341. SDE_EDID_DEBUG("speaker alloc data SP byte = %08x %s%s%s%s%s%s%s\n",
  342. sadb[1],
  343. (sadb[1] & BIT(0)) ? "FL/FR," : "",
  344. (sadb[1] & BIT(1)) ? "LFE," : "",
  345. (sadb[1] & BIT(2)) ? "FC," : "",
  346. (sadb[1] & BIT(3)) ? "RL/RR," : "",
  347. (sadb[1] & BIT(4)) ? "RC," : "",
  348. (sadb[1] & BIT(5)) ? "FLC/FRC," : "",
  349. (sadb[1] & BIT(6)) ? "RLC/RRC," : "");
  350. SDE_EDID_DEBUG("%s -", __func__);
  351. }
  352. struct sde_edid_ctrl *sde_edid_init(void)
  353. {
  354. struct sde_edid_ctrl *edid_ctrl = NULL;
  355. SDE_EDID_DEBUG("%s +\n", __func__);
  356. edid_ctrl = kzalloc(sizeof(*edid_ctrl), GFP_KERNEL);
  357. if (!edid_ctrl) {
  358. SDE_ERROR("edid_ctrl alloc failed\n");
  359. return NULL;
  360. }
  361. memset((edid_ctrl), 0, sizeof(*edid_ctrl));
  362. SDE_EDID_DEBUG("%s -\n", __func__);
  363. return edid_ctrl;
  364. }
  365. void sde_free_edid(void **input)
  366. {
  367. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
  368. SDE_EDID_DEBUG("%s +", __func__);
  369. kfree(edid_ctrl->edid);
  370. edid_ctrl->edid = NULL;
  371. }
  372. void sde_edid_deinit(void **input)
  373. {
  374. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
  375. SDE_EDID_DEBUG("%s +", __func__);
  376. sde_free_edid((void *)&edid_ctrl);
  377. kfree(edid_ctrl);
  378. SDE_EDID_DEBUG("%s -", __func__);
  379. }
  380. int _sde_edid_update_modes(struct drm_connector *connector,
  381. void *input)
  382. {
  383. int rc = 0;
  384. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
  385. SDE_EDID_DEBUG("%s +", __func__);
  386. if (edid_ctrl->edid) {
  387. drm_connector_update_edid_property(connector,
  388. edid_ctrl->edid);
  389. rc = drm_add_edid_modes(connector, edid_ctrl->edid);
  390. sde_edid_parse_extended_blk_info(connector,
  391. edid_ctrl->edid);
  392. SDE_EDID_DEBUG("%s -", __func__);
  393. return rc;
  394. }
  395. drm_connector_update_edid_property(connector, NULL);
  396. SDE_EDID_DEBUG("%s null edid -", __func__);
  397. return rc;
  398. }
  399. u8 sde_get_edid_checksum(void *input)
  400. {
  401. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
  402. struct edid *edid = NULL, *last_block = NULL;
  403. u8 *raw_edid = NULL;
  404. if (!edid_ctrl || !edid_ctrl->edid) {
  405. SDE_ERROR("invalid edid input\n");
  406. return 0;
  407. }
  408. edid = edid_ctrl->edid;
  409. raw_edid = (u8 *)edid;
  410. raw_edid += (edid->extensions * EDID_LENGTH);
  411. last_block = (struct edid *)raw_edid;
  412. if (last_block)
  413. return last_block->checksum;
  414. SDE_ERROR("Invalid block, no checksum\n");
  415. return 0;
  416. }
  417. bool sde_detect_hdmi_monitor(void *input)
  418. {
  419. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
  420. return drm_detect_hdmi_monitor(edid_ctrl->edid);
  421. }
  422. void sde_parse_edid(void *input)
  423. {
  424. struct sde_edid_ctrl *edid_ctrl;
  425. if (!input) {
  426. SDE_ERROR("Invalid input\n");
  427. return;
  428. }
  429. edid_ctrl = (struct sde_edid_ctrl *)(input);
  430. if (edid_ctrl->edid) {
  431. sde_edid_extract_vendor_id(edid_ctrl);
  432. _sde_edid_extract_audio_data_blocks(edid_ctrl);
  433. _sde_edid_extract_speaker_allocation_data(edid_ctrl);
  434. } else {
  435. SDE_ERROR("edid not present\n");
  436. }
  437. }
  438. void sde_get_edid(struct drm_connector *connector,
  439. struct i2c_adapter *adapter, void **input)
  440. {
  441. struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
  442. edid_ctrl->edid = drm_get_edid(connector, adapter);
  443. SDE_EDID_DEBUG("%s +\n", __func__);
  444. if (!edid_ctrl->edid)
  445. SDE_ERROR("EDID read failed\n");
  446. if (edid_ctrl->edid)
  447. sde_parse_edid(edid_ctrl);
  448. SDE_EDID_DEBUG("%s -\n", __func__);
  449. };