OSDN Git Service

Merge android-4.4.143 (7bbfac1) into msm-4.4
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpu / drm / msm / sde_edid_parser.c
1 /* Copyright (c) 2017, The Linux Foundation. All rights reserved.
2  *
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.
6  *
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.
11  */
12
13 #include <drm/drm_edid.h>
14
15 #include "sde_kms.h"
16 #include "sde_edid_parser.h"
17
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
21
22 enum data_block_types {
23         RESERVED,
24         AUDIO_DATA_BLOCK,
25         VIDEO_DATA_BLOCK,
26         VENDOR_SPECIFIC_DATA_BLOCK,
27         SPEAKER_ALLOCATION_DATA_BLOCK,
28         VESA_DTC_DATA_BLOCK,
29         RESERVED2,
30         USE_EXTENDED_TAG
31 };
32
33 static u8 *sde_find_edid_extension(struct edid *edid, int ext_id)
34 {
35         u8 *edid_ext = NULL;
36         int i;
37
38         /* No EDID or EDID extensions */
39         if (edid == NULL || edid->extensions == 0)
40                 return NULL;
41
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)
46                         break;
47         }
48
49         if (i == edid->extensions)
50                 return NULL;
51
52         return edid_ext;
53 }
54
55 static u8 *sde_find_cea_extension(struct edid *edid)
56 {
57         return sde_find_edid_extension(edid, SDE_CEA_EXT);
58 }
59
60 static int
61 sde_cea_db_payload_len(const u8 *db)
62 {
63         return db[0] & 0x1f;
64 }
65
66 static int
67 sde_cea_db_tag(const u8 *db)
68 {
69         return db[0] >> 5;
70 }
71
72 static int
73 sde_cea_revision(const u8 *cea)
74 {
75         return cea[1];
76 }
77
78 static int
79 sde_cea_db_offsets(const u8 *cea, int *start, int *end)
80 {
81         /* Data block offset in CEA extension block */
82         *start = 4;
83         *end = cea[2];
84         if (*end == 0)
85                 *end = 127;
86         if (*end < 4 || *end > 127)
87                 return -ERANGE;
88         return 0;
89 }
90
91 #define sde_for_each_cea_db(cea, i, start, end) \
92 for ((i) = (start); \
93 (i) < (end) && (i) + sde_cea_db_payload_len(&(cea)[(i)]) < (end); \
94 (i) += sde_cea_db_payload_len(&(cea)[(i)]) + 1)
95
96 static bool sde_cea_db_is_hdmi_hf_vsdb(const u8 *db)
97 {
98         int hdmi_id;
99
100         if (sde_cea_db_tag(db) != VENDOR_SPECIFIC_DATA_BLOCK)
101                 return false;
102
103         if (sde_cea_db_payload_len(db) < 7)
104                 return false;
105
106         hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
107
108         return hdmi_id == HDMI_IEEE_OUI_HF;
109 }
110
111 static u8 *sde_edid_find_extended_tag_block(struct edid *edid, int blk_id)
112 {
113         u8 *db = NULL;
114         u8 *cea = NULL;
115
116         if (!edid) {
117                 pr_err("%s: invalid input\n", __func__);
118                 return NULL;
119         }
120
121         cea = sde_find_cea_extension(edid);
122
123         if (cea && sde_cea_revision(cea) >= 3) {
124                 int i, start, end;
125
126                 if (sde_cea_db_offsets(cea, &start, &end))
127                         return NULL;
128
129                 sde_for_each_cea_db(cea, i, start, end) {
130                         db = &cea[i];
131                         if ((sde_cea_db_tag(db) == SDE_EXTENDED_TAG) &&
132                                 (db[1] == blk_id))
133                                 return db;
134                 }
135         }
136         return NULL;
137 }
138
139 static u8 *
140 sde_edid_find_block(struct edid *edid, int blk_id)
141 {
142         u8 *db = NULL;
143         u8 *cea = NULL;
144
145         if (!edid) {
146                 pr_err("%s: invalid input\n", __func__);
147                 return NULL;
148         }
149
150         cea = sde_find_cea_extension(edid);
151
152         if (cea && sde_cea_revision(cea) >= 3) {
153                 int i, start, end;
154
155                 if (sde_cea_db_offsets(cea, &start, &end))
156                         return 0;
157
158                 sde_for_each_cea_db(cea, i, start, end) {
159                         db = &cea[i];
160                         if (sde_cea_db_tag(db) == blk_id)
161                                 return db;
162                 }
163         }
164         return NULL;
165 }
166
167
168 static const u8 *_sde_edid_find_block(const u8 *in_buf, u32 start_offset,
169         u8 type, u8 *len)
170 {
171         /* the start of data block collection, start of Video Data Block */
172         u32 offset = start_offset;
173         u32 dbc_offset = in_buf[2];
174
175         SDE_EDID_DEBUG("%s +", __func__);
176         /*
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.
181          */
182         if ((dbc_offset == 0) || (dbc_offset == 4)) {
183                 SDE_ERROR("EDID: no DTD or non-DTD data present\n");
184                 return NULL;
185         }
186
187         while (offset < dbc_offset) {
188                 u8 block_len = in_buf[offset] & 0x1F;
189
190                 if ((offset + block_len <= dbc_offset) &&
191                     (in_buf[offset] >> 5) == type) {
192                         *len = block_len;
193                         SDE_EDID_DEBUG("block=%d found @ 0x%x w/ len=%d\n",
194                                 type, offset, block_len);
195
196                         return in_buf + offset;
197                 }
198                 offset += 1 + block_len;
199         }
200
201         return NULL;
202 }
203
204 static void sde_edid_extract_vendor_id(struct sde_edid_ctrl *edid_ctrl)
205 {
206         char *vendor_id;
207         u32 id_codes;
208
209         SDE_EDID_DEBUG("%s +", __func__);
210         if (!edid_ctrl) {
211                 SDE_ERROR("%s: invalid input\n", __func__);
212                 return;
213         }
214
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];
218
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);
222         vendor_id[3] = 0;
223         SDE_EDID_DEBUG("vendor id is %s ", vendor_id);
224         SDE_EDID_DEBUG("%s -", __func__);
225 }
226
227 static void sde_edid_set_y420_support(struct drm_connector *connector,
228 u32 video_format)
229 {
230         u8 cea_mode = 0;
231         struct drm_display_mode *mode;
232         u32 mode_fmt_flags = 0;
233
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__,
245                         video_format);
246                         mode->flags |= DRM_MODE_FLAG_SUPPORTS_YUV;
247                 }
248         }
249 }
250
251 static void sde_edid_parse_Y420CMDB(
252 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl,
253 const u8 *db)
254 {
255         u32 offset = 0;
256         u8 cmdb_len = 0;
257         u8 svd_len = 0;
258         const u8 *svd = NULL;
259         u32 i = 0, j = 0;
260         u32 video_format = 0;
261
262         if (!edid_ctrl) {
263                 DEV_ERR("%s: edid_ctrl is NULL\n", __func__);
264                 return;
265         }
266
267         if (!db) {
268                 DEV_ERR("%s: invalid input\n", __func__);
269                 return;
270         }
271         SDE_EDID_DEBUG("%s +\n", __func__);
272         cmdb_len = db[0] & 0x1f;
273
274         /* Byte 3 to L+1 contain SVDs */
275         offset += 2;
276
277         svd = sde_edid_find_block(edid_ctrl->edid, VIDEO_DATA_BLOCK);
278
279         if (svd) {
280                 /*moving to the next byte as vic info begins there*/
281                 svd_len = svd[0] & 0x1f;
282                 ++svd;
283         }
284
285         for (i = 0; i < svd_len; i++, j++) {
286                 video_format = *(svd + i) & 0x7F;
287                 if (cmdb_len == 1) {
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);
292
293                         if (j & 0x80) {
294                                 j = j/8;
295                                 offset++;
296                                 if (offset >= cmdb_len)
297                                         break;
298                         }
299                 }
300         }
301
302         SDE_EDID_DEBUG("%s -\n", __func__);
303
304 }
305
306 static void sde_edid_parse_Y420VDB(
307 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl,
308 const u8 *db)
309 {
310         u8 len = db[0] & 0x1f;
311         u32 i = 0;
312         u32 video_format = 0;
313
314         if (!edid_ctrl) {
315                 DEV_ERR("%s: invalid input\n", __func__);
316                 return;
317         }
318
319         SDE_EDID_DEBUG("%s +\n", __func__);
320
321         /* Offset to byte 3 */
322         db += 2;
323         for (i = 0; i < len - 1; i++) {
324                 video_format = *(db + i) & 0x7F;
325                 /*
326                  * mode was already added in get_modes()
327                  * only need to set the Y420 support flag
328                  */
329                 sde_edid_set_y420_support(connector, video_format);
330         }
331         SDE_EDID_DEBUG("%s -", __func__);
332 }
333
334 static void sde_edid_set_mode_format(
335 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl)
336 {
337         const u8 *db = NULL;
338         struct drm_display_mode *mode;
339
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);
344         if (db)
345                 sde_edid_parse_Y420VDB(connector, edid_ctrl, db);
346         else
347                 SDE_EDID_DEBUG("YCbCr420 VDB is not present\n");
348
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;
353         }
354
355
356         db = sde_edid_find_extended_tag_block(edid_ctrl->edid,
357                         Y420_CAPABILITY_MAP_DATA_BLOCK);
358         if (db)
359                 sde_edid_parse_Y420CMDB(connector, edid_ctrl, db);
360         else
361                 SDE_EDID_DEBUG("YCbCr420 CMDB is not present\n");
362
363         SDE_EDID_DEBUG("%s -\n", __func__);
364 }
365
366 static void _sde_edid_update_dc_modes(
367 struct drm_connector *connector, struct sde_edid_ctrl *edid_ctrl)
368 {
369         int i, start, end;
370         u8 *edid_ext, *hdmi;
371         struct drm_display_info *disp_info;
372         u32 hdmi_dc_yuv_modes = 0;
373
374         SDE_EDID_DEBUG("%s +\n", __func__);
375
376         if (!connector || !edid_ctrl) {
377                 SDE_ERROR("invalid input\n");
378                 return;
379         }
380
381         disp_info = &connector->display_info;
382
383         edid_ext = sde_find_cea_extension(edid_ctrl->edid);
384
385         if (!edid_ext) {
386                 SDE_ERROR("no cea extension\n");
387                 return;
388         }
389
390         if (sde_cea_db_offsets(edid_ext, &start, &end))
391                 return;
392
393         sde_for_each_cea_db(edid_ext, i, start, end) {
394                 if (sde_cea_db_is_hdmi_hf_vsdb(&edid_ext[i])) {
395
396                         hdmi = &edid_ext[i];
397
398                         if (sde_cea_db_payload_len(hdmi) < 7)
399                                 continue;
400
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");
404                         }
405
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");
409                         }
410
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");
414                         }
415                 }
416         }
417
418         disp_info->edid_hdmi_dc_modes |= hdmi_dc_yuv_modes;
419
420         SDE_EDID_DEBUG("%s -\n", __func__);
421 }
422
423 static void _sde_edid_extract_audio_data_blocks(
424         struct sde_edid_ctrl *edid_ctrl)
425 {
426         u8 len = 0;
427         u8 adb_max = 0;
428         const u8 *adb = NULL;
429         u32 offset = DBC_START_OFFSET;
430         u8 *cea = NULL;
431
432         if (!edid_ctrl) {
433                 SDE_ERROR("invalid edid_ctrl\n");
434                 return;
435         }
436         SDE_EDID_DEBUG("%s +", __func__);
437         cea = sde_find_cea_extension(edid_ctrl->edid);
438         if (!cea) {
439                 SDE_DEBUG("CEA extension not found\n");
440                 return;
441         }
442
443         edid_ctrl->adb_size = 0;
444
445         memset(edid_ctrl->audio_data_block, 0,
446                 sizeof(edid_ctrl->audio_data_block));
447
448         do {
449                 len = 0;
450                 adb = _sde_edid_find_block(cea, offset, AUDIO_DATA_BLOCK,
451                         &len);
452
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");
457                                 return;
458                         }
459
460                         continue;
461                 }
462
463                 memcpy(edid_ctrl->audio_data_block + edid_ctrl->adb_size,
464                         adb + 1, len);
465                 offset = (adb - cea) + 1 + len;
466
467                 edid_ctrl->adb_size += len;
468                 adb_max++;
469         } while (adb);
470         SDE_EDID_DEBUG("%s -", __func__);
471 }
472
473 static void _sde_edid_extract_speaker_allocation_data(
474         struct sde_edid_ctrl *edid_ctrl)
475 {
476         u8 len;
477         const u8 *sadb = NULL;
478         u8 *cea = NULL;
479
480         if (!edid_ctrl) {
481                 SDE_ERROR("invalid edid_ctrl\n");
482                 return;
483         }
484         SDE_EDID_DEBUG("%s +", __func__);
485         cea = sde_find_cea_extension(edid_ctrl->edid);
486         if (!cea) {
487                 SDE_DEBUG("CEA extension not found\n");
488                 return;
489         }
490
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");
495                 return;
496         }
497
498         memcpy(edid_ctrl->spkr_alloc_data_block, sadb + 1, len);
499         edid_ctrl->sadb_size = len;
500
501         SDE_EDID_DEBUG("speaker alloc data SP byte = %08x %s%s%s%s%s%s%s\n",
502                 sadb[1],
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__);
511 }
512
513 struct sde_edid_ctrl *sde_edid_init(void)
514 {
515         struct sde_edid_ctrl *edid_ctrl = NULL;
516
517         SDE_EDID_DEBUG("%s +\n", __func__);
518         edid_ctrl = kzalloc(sizeof(*edid_ctrl), GFP_KERNEL);
519         if (!edid_ctrl) {
520                 SDE_ERROR("edid_ctrl alloc failed\n");
521                 return NULL;
522         }
523         memset((edid_ctrl), 0, sizeof(*edid_ctrl));
524         SDE_EDID_DEBUG("%s -\n", __func__);
525         return edid_ctrl;
526 }
527
528 void sde_free_edid(void **input)
529 {
530         struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
531
532         SDE_EDID_DEBUG("%s +", __func__);
533         kfree(edid_ctrl->edid);
534         edid_ctrl->edid = NULL;
535 }
536
537 void sde_edid_deinit(void **input)
538 {
539         struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
540
541         SDE_EDID_DEBUG("%s +", __func__);
542         sde_free_edid((void *)&edid_ctrl);
543         kfree(edid_ctrl);
544         SDE_EDID_DEBUG("%s -", __func__);
545 }
546
547 int _sde_edid_update_modes(struct drm_connector *connector,
548         void *input)
549 {
550         int rc = 0;
551         struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
552         struct drm_display_info *disp_info;
553
554         disp_info = &connector->display_info;
555
556         if (disp_info)
557                 disp_info->edid_hdmi_dc_modes = 0;
558
559         SDE_EDID_DEBUG("%s +", __func__);
560         if (edid_ctrl->edid) {
561                 drm_mode_connector_update_edid_property(connector,
562                         edid_ctrl->edid);
563
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__);
568                 return rc;
569         }
570
571         drm_mode_connector_update_edid_property(connector, NULL);
572         SDE_EDID_DEBUG("%s null edid -", __func__);
573         return rc;
574 }
575
576 bool sde_detect_hdmi_monitor(void *input)
577 {
578         struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(input);
579
580         return drm_detect_hdmi_monitor(edid_ctrl->edid);
581 }
582
583 void sde_get_edid(struct drm_connector *connector,
584                                   struct i2c_adapter *adapter, void **input)
585 {
586         struct sde_edid_ctrl *edid_ctrl = (struct sde_edid_ctrl *)(*input);
587
588         edid_ctrl->edid = drm_get_edid(connector, adapter);
589         SDE_EDID_DEBUG("%s +\n", __func__);
590
591         if (!edid_ctrl->edid)
592                 SDE_ERROR("EDID read failed\n");
593
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);
598         }
599         SDE_EDID_DEBUG("%s -\n", __func__);
600 };