1 /* Copyright (c) 2017, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <drm/drm_edid.h>
16 #include "sde_edid_parser.h"
18 /* TODO: copy from drm_edid.c and mdss_hdmi_edid.c. remove if using ELD */
19 #define DBC_START_OFFSET 4
20 #define EDID_DTD_LEN 18
22 enum data_block_types {
26 VENDOR_SPECIFIC_DATA_BLOCK,
27 SPEAKER_ALLOCATION_DATA_BLOCK,
33 static u8 *sde_find_edid_extension(struct edid *edid, int ext_id)
38 /* No EDID or EDID extensions */
39 if (edid == NULL || edid->extensions == 0)
42 /* Find CEA extension */
43 for (i = 0; i < edid->extensions; i++) {
44 edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
45 if (edid_ext[0] == ext_id)
49 if (i == edid->extensions)
55 static u8 *sde_find_cea_extension(struct edid *edid)
57 return sde_find_edid_extension(edid, SDE_CEA_EXT);
61 sde_cea_db_payload_len(const u8 *db)
67 sde_cea_db_tag(const u8 *db)
73 sde_cea_revision(const u8 *cea)
79 sde_cea_db_offsets(const u8 *cea, int *start, int *end)
81 /* Data block offset in CEA extension block */
86 if (*end < 4 || *end > 127)
91 #define sde_for_each_cea_db(cea, i, start, end) \
93 (i) < (end) && (i) + sde_cea_db_payload_len(&(cea)[(i)]) < (end); \
94 (i) += sde_cea_db_payload_len(&(cea)[(i)]) + 1)
96 static bool sde_cea_db_is_hdmi_hf_vsdb(const u8 *db)
100 if (sde_cea_db_tag(db) != VENDOR_SPECIFIC_DATA_BLOCK)
103 if (sde_cea_db_payload_len(db) < 7)
106 hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
108 return hdmi_id == HDMI_IEEE_OUI_HF;
111 static u8 *sde_edid_find_extended_tag_block(struct edid *edid, int blk_id)
117 pr_err("%s: invalid input\n", __func__);
121 cea = sde_find_cea_extension(edid);
123 if (cea && sde_cea_revision(cea) >= 3) {
126 if (sde_cea_db_offsets(cea, &start, &end))
129 sde_for_each_cea_db(cea, i, start, end) {
131 if ((sde_cea_db_tag(db) == SDE_EXTENDED_TAG) &&
140 sde_edid_find_block(struct edid *edid, int blk_id)
146 pr_err("%s: invalid input\n", __func__);
150 cea = sde_find_cea_extension(edid);
152 if (cea && sde_cea_revision(cea) >= 3) {
155 if (sde_cea_db_offsets(cea, &start, &end))
158 sde_for_each_cea_db(cea, i, start, end) {
160 if (sde_cea_db_tag(db) == blk_id)
168 static const u8 *_sde_edid_find_block(const u8 *in_buf, u32 start_offset,
171 /* the start of data block collection, start of Video Data Block */
172 u32 offset = start_offset;
173 u32 dbc_offset = in_buf[2];
175 SDE_EDID_DEBUG("%s +", __func__);
177 * * edid buffer 1, byte 2 being 4 means no non-DTD/Data block
178 * collection present.
179 * * edid buffer 1, byte 2 being 0 means no non-DTD/DATA block
180 * collection present and no DTD data present.
182 if ((dbc_offset == 0) || (dbc_offset == 4)) {
183 SDE_ERROR("EDID: no DTD or non-DTD data present\n");
187 while (offset < dbc_offset) {
188 u8 block_len = in_buf[offset] & 0x1F;
190 if ((offset + block_len <= dbc_offset) &&
191 (in_buf[offset] >> 5) == type) {
193 SDE_EDID_DEBUG("block=%d found @ 0x%x w/ len=%d\n",
194 type, offset, block_len);
196 return in_buf + offset;
198 offset += 1 + block_len;
204 static void sde_edid_extract_vendor_id(struct sde_edid_ctrl *edid_ctrl)
209 SDE_EDID_DEBUG("%s +", __func__);
211 SDE_ERROR("%s: invalid input\n", __func__);
215 vendor_id = edid_ctrl->vendor_id;
216 id_codes = ((u32)edid_ctrl->edid->mfg_id[0] << 8) +
217 edid_ctrl->edid->mfg_id[1];
219 vendor_id[0] = 'A' - 1 + ((id_codes >> 10) & 0x1F);
220 vendor_id[1] = 'A' - 1 + ((id_codes >> 5) & 0x1F);
221 vendor_id[2] = 'A' - 1 + (id_codes & 0x1F);
223 SDE_EDID_DEBUG("vendor id is %s ", vendor_id);
224 SDE_EDID_DEBUG("%s -", __func__);
227 static void sde_edid_set_y420_support(struct drm_connector *connector,
231 struct drm_display_mode *mode;
232 u32 mode_fmt_flags = 0;
234 /* Need to add Y420 support flag to the modes */
235 list_for_each_entry(mode, &connector->probed_modes, head) {
236 /* Cache the format flags before clearing */
237 mode_fmt_flags = mode->flags;
238 /* Clear the RGB/YUV format flags before calling upstream API */
239 mode->flags &= ~SDE_DRM_MODE_FLAG_FMT_MASK;
240 cea_mode = drm_match_cea_mode(mode);
241 /* Restore the format flags */
242 mode->flags = mode_fmt_flags;
243 if ((cea_mode != 0) && (cea_mode == video_format)) {
244 SDE_EDID_DEBUG("%s found match for %d ", __func__,
246 mode->flags |= DRM_MODE_FLAG_SUPPORTS_YUV;
251 static void sde_edid_parse_Y420CMDB(
252 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl,
258 const u8 *svd = NULL;
260 u32 video_format = 0;
263 DEV_ERR("%s: edid_ctrl is NULL\n", __func__);
268 DEV_ERR("%s: invalid input\n", __func__);
271 SDE_EDID_DEBUG("%s +\n", __func__);
272 cmdb_len = db[0] & 0x1f;
274 /* Byte 3 to L+1 contain SVDs */
277 svd = sde_edid_find_block(edid_ctrl->edid, VIDEO_DATA_BLOCK);
280 /*moving to the next byte as vic info begins there*/
281 svd_len = svd[0] & 0x1f;
285 for (i = 0; i < svd_len; i++, j++) {
286 video_format = *(svd + i) & 0x7F;
288 /* If cmdb_len is 1, it means all SVDs support YUV */
289 sde_edid_set_y420_support(connector, video_format);
290 } else if (db[offset] & (1 << j)) {
291 sde_edid_set_y420_support(connector, video_format);
296 if (offset >= cmdb_len)
302 SDE_EDID_DEBUG("%s -\n", __func__);
306 static void sde_edid_parse_Y420VDB(
307 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl,
310 u8 len = db[0] & 0x1f;
312 u32 video_format = 0;
315 DEV_ERR("%s: invalid input\n", __func__);
319 SDE_EDID_DEBUG("%s +\n", __func__);
321 /* Offset to byte 3 */
323 for (i = 0; i < len - 1; i++) {
324 video_format = *(db + i) & 0x7F;
326 * mode was already added in get_modes()
327 * only need to set the Y420 support flag
329 sde_edid_set_y420_support(connector, video_format);
331 SDE_EDID_DEBUG("%s -", __func__);
334 static void sde_edid_set_mode_format(
335 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl)
338 struct drm_display_mode *mode;
340 SDE_EDID_DEBUG("%s +\n", __func__);
341 /* Set YUV mode support flags for YCbcr420VDB */
342 db = sde_edid_find_extended_tag_block(edid_ctrl->edid,
343 Y420_VIDEO_DATA_BLOCK);
345 sde_edid_parse_Y420VDB(connector, edid_ctrl, db);
347 SDE_EDID_DEBUG("YCbCr420 VDB is not present\n");
349 /* Set RGB supported on all modes where YUV is not set */
350 list_for_each_entry(mode, &connector->probed_modes, head) {
351 if (!(mode->flags & DRM_MODE_FLAG_SUPPORTS_YUV))
352 mode->flags |= DRM_MODE_FLAG_SUPPORTS_RGB;
356 db = sde_edid_find_extended_tag_block(edid_ctrl->edid,
357 Y420_CAPABILITY_MAP_DATA_BLOCK);
359 sde_edid_parse_Y420CMDB(connector, edid_ctrl, db);
361 SDE_EDID_DEBUG("YCbCr420 CMDB is not present\n");
363 SDE_EDID_DEBUG("%s -\n", __func__);
366 static void _sde_edid_update_dc_modes(
367 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl)
371 struct drm_display_info *disp_info;
372 u32 hdmi_dc_yuv_modes = 0;
374 SDE_EDID_DEBUG("%s +\n", __func__);
376 if (!connector || !edid_ctrl) {
377 SDE_ERROR("invalid input\n");
381 disp_info = &connector->display_info;
383 edid_ext = sde_find_cea_extension(edid_ctrl->edid);
386 SDE_ERROR("no cea extension\n");
390 if (sde_cea_db_offsets(edid_ext, &start, &end))
393 sde_for_each_cea_db(edid_ext, i, start, end) {
394 if (sde_cea_db_is_hdmi_hf_vsdb(&edid_ext[i])) {
398 if (sde_cea_db_payload_len(hdmi) < 7)
401 if (hdmi[7] & DRM_EDID_YCBCR420_DC_30) {
402 hdmi_dc_yuv_modes |= DRM_EDID_YCBCR420_DC_30;
403 SDE_EDID_DEBUG("Y420 30-bit supported\n");
406 if (hdmi[7] & DRM_EDID_YCBCR420_DC_36) {
407 hdmi_dc_yuv_modes |= DRM_EDID_YCBCR420_DC_36;
408 SDE_EDID_DEBUG("Y420 36-bit supported\n");
411 if (hdmi[7] & DRM_EDID_YCBCR420_DC_48) {
412 hdmi_dc_yuv_modes |= DRM_EDID_YCBCR420_DC_36;
413 SDE_EDID_DEBUG("Y420 48-bit supported\n");
418 disp_info->edid_hdmi_dc_modes |= hdmi_dc_yuv_modes;
420 SDE_EDID_DEBUG("%s -\n", __func__);
423 static void _sde_edid_extract_audio_data_blocks(
424 struct sde_edid_ctrl *edid_ctrl)
428 const u8 *adb = NULL;
429 u32 offset = DBC_START_OFFSET;
433 SDE_ERROR("invalid edid_ctrl\n");
436 SDE_EDID_DEBUG("%s +", __func__);
437 cea = sde_find_cea_extension(edid_ctrl->edid);
439 SDE_DEBUG("CEA extension not found\n");
443 edid_ctrl->adb_size = 0;
445 memset(edid_ctrl->audio_data_block, 0,
446 sizeof(edid_ctrl->audio_data_block));
450 adb = _sde_edid_find_block(cea, offset, AUDIO_DATA_BLOCK,
453 if ((adb == NULL) || (len > MAX_AUDIO_DATA_BLOCK_SIZE ||
454 adb_max >= MAX_NUMBER_ADB)) {
455 if (!edid_ctrl->adb_size) {
456 SDE_DEBUG("No/Invalid Audio Data Block\n");
463 memcpy(edid_ctrl->audio_data_block + edid_ctrl->adb_size,
465 offset = (adb - cea) + 1 + len;
467 edid_ctrl->adb_size += len;
470 SDE_EDID_DEBUG("%s -", __func__);
473 static void _sde_edid_extract_speaker_allocation_data(
474 struct sde_edid_ctrl *edid_ctrl)
477 const u8 *sadb = NULL;
481 SDE_ERROR("invalid edid_ctrl\n");
484 SDE_EDID_DEBUG("%s +", __func__);
485 cea = sde_find_cea_extension(edid_ctrl->edid);
487 SDE_DEBUG("CEA extension not found\n");
491 sadb = _sde_edid_find_block(cea, DBC_START_OFFSET,
492 SPEAKER_ALLOCATION_DATA_BLOCK, &len);
493 if ((sadb == NULL) || (len != MAX_SPKR_ALLOC_DATA_BLOCK_SIZE)) {
494 SDE_DEBUG("No/Invalid Speaker Allocation Data Block\n");
498 memcpy(edid_ctrl->spkr_alloc_data_block, sadb + 1, len);
499 edid_ctrl->sadb_size = len;
501 SDE_EDID_DEBUG("speaker alloc data SP byte = %08x %s%s%s%s%s%s%s\n",
503 (sadb[1] & BIT(0)) ? "FL/FR," : "",
504 (sadb[1] & BIT(1)) ? "LFE," : "",
505 (sadb[1] & BIT(2)) ? "FC," : "",
506 (sadb[1] & BIT(3)) ? "RL/RR," : "",
507 (sadb[1] & BIT(4)) ? "RC," : "",
508 (sadb[1] & BIT(5)) ? "FLC/FRC," : "",
509 (sadb[1] & BIT(6)) ? "RLC/RRC," : "");
510 SDE_EDID_DEBUG("%s -", __func__);
513 struct sde_edid_ctrl *sde_edid_init(void)
515 struct sde_edid_ctrl *edid_ctrl = NULL;
517 SDE_EDID_DEBUG("%s +\n", __func__);
518 edid_ctrl = kzalloc(sizeof(*edid_ctrl), GFP_KERNEL);
520 SDE_ERROR("edid_ctrl alloc failed\n");
523 memset((edid_ctrl), 0, sizeof(*edid_ctrl));
524 SDE_EDID_DEBUG("%s -\n", __func__);
528 void sde_free_edid(void **input)
530 struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
532 SDE_EDID_DEBUG("%s +", __func__);
533 kfree(edid_ctrl->edid);
534 edid_ctrl->edid = NULL;
537 void sde_edid_deinit(void **input)
539 struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
541 SDE_EDID_DEBUG("%s +", __func__);
542 sde_free_edid((void *)&edid_ctrl);
544 SDE_EDID_DEBUG("%s -", __func__);
547 int _sde_edid_update_modes(struct drm_connector *connector,
551 struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
552 struct drm_display_info *disp_info;
554 disp_info = &connector->display_info;
557 disp_info->edid_hdmi_dc_modes = 0;
559 SDE_EDID_DEBUG("%s +", __func__);
560 if (edid_ctrl->edid) {
561 drm_mode_connector_update_edid_property(connector,
564 rc = drm_add_edid_modes(connector, edid_ctrl->edid);
565 sde_edid_set_mode_format(connector, edid_ctrl);
566 _sde_edid_update_dc_modes(connector, edid_ctrl);
567 SDE_EDID_DEBUG("%s -", __func__);
571 drm_mode_connector_update_edid_property(connector, NULL);
572 SDE_EDID_DEBUG("%s null edid -", __func__);
576 bool sde_detect_hdmi_monitor(void *input)
578 struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
580 return drm_detect_hdmi_monitor(edid_ctrl->edid);
583 void sde_get_edid(struct drm_connector *connector,
584 struct i2c_adapter *adapter, void **input)
586 struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
588 edid_ctrl->edid = drm_get_edid(connector, adapter);
589 SDE_EDID_DEBUG("%s +\n", __func__);
591 if (!edid_ctrl->edid)
592 SDE_ERROR("EDID read failed\n");
594 if (edid_ctrl->edid) {
595 sde_edid_extract_vendor_id(edid_ctrl);
596 _sde_edid_extract_audio_data_blocks(edid_ctrl);
597 _sde_edid_extract_speaker_allocation_data(edid_ctrl);
599 SDE_EDID_DEBUG("%s -\n", __func__);