Merge branch 'torvalds/master'
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / hdmi / rockchip-hdmi-edid.c
1 #include "rockchip-hdmi.h"
2 #include "../../edid.h"
3
4 #ifdef EDIDDEBUG
5 #define EDBG    DBG
6 #else
7 #define EDBG(format, ...)
8 #endif
9
10 enum {
11         E_HDMI_EDID_SUCCESS = 0,
12         E_HDMI_EDID_PARAM,
13         E_HDMI_EDID_HEAD,
14         E_HDMI_EDID_CHECKSUM,
15         E_HDMI_EDID_VERSION,
16         E_HDMI_EDID_UNKOWNDATA,
17         E_HDMI_EDID_NOMEMORY
18 };
19
20 static int hdmi_edid_checksum(unsigned char *buf)
21 {
22         int i;
23         int checksum = 0;
24
25         for (i = 0; i < HDMI_EDID_BLOCK_SIZE; i++)
26                 checksum += buf[i];
27
28         checksum &= 0xff;
29
30         if (checksum == 0)
31                 return E_HDMI_EDID_SUCCESS;
32         else
33                 return E_HDMI_EDID_CHECKSUM;
34 }
35
36 /*
37         @Des    Parse Detail Timing Descriptor.
38         @Param  buf     :       pointer to DTD data.
39         @Param  pvic:   VIC of DTD descripted.
40  */
41 static int hdmi_edid_parse_dtd(unsigned char *block, struct fb_videomode *mode)
42 {
43         mode->xres = H_ACTIVE;
44         mode->yres = V_ACTIVE;
45         mode->pixclock = PIXEL_CLOCK;
46 /*      mode->pixclock /= 1000;
47         mode->pixclock = KHZ2PICOS(mode->pixclock);
48 */      mode->right_margin = H_SYNC_OFFSET;
49         mode->left_margin = (H_ACTIVE + H_BLANKING) -
50                 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
51         mode->upper_margin = V_BLANKING - V_SYNC_OFFSET -
52                 V_SYNC_WIDTH;
53         mode->lower_margin = V_SYNC_OFFSET;
54         mode->hsync_len = H_SYNC_WIDTH;
55         mode->vsync_len = V_SYNC_WIDTH;
56         if (HSYNC_POSITIVE)
57                 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
58         if (VSYNC_POSITIVE)
59                 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
60         mode->refresh = PIXEL_CLOCK/((H_ACTIVE + H_BLANKING) *
61                                      (V_ACTIVE + V_BLANKING));
62         if (INTERLACED) {
63                 mode->yres *= 2;
64                 mode->upper_margin *= 2;
65                 mode->lower_margin *= 2;
66                 mode->vsync_len *= 2;
67                 mode->vmode |= FB_VMODE_INTERLACED;
68         }
69         mode->flag = FB_MODE_IS_DETAILED;
70
71         EDBG("<<<<<<<<Detailed Time>>>>>>>>>\n");
72         EDBG("%d KHz Refresh %d Hz",
73              PIXEL_CLOCK/1000, mode->refresh);
74         EDBG("%d %d %d %d ", H_ACTIVE, H_ACTIVE + H_SYNC_OFFSET,
75              H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH, H_ACTIVE + H_BLANKING);
76         EDBG("%d %d %d %d ", V_ACTIVE, V_ACTIVE + V_SYNC_OFFSET,
77              V_ACTIVE + V_SYNC_OFFSET + V_SYNC_WIDTH, V_ACTIVE + V_BLANKING);
78         EDBG("%sHSync %sVSync\n\n", (HSYNC_POSITIVE) ? "+" : "-",
79              (VSYNC_POSITIVE) ? "+" : "-");
80         return E_HDMI_EDID_SUCCESS;
81 }
82
83 int hdmi_edid_parse_base(unsigned char *buf,
84                          int *extend_num, struct hdmi_edid *pedid)
85 {
86         int rc = E_HDMI_EDID_SUCCESS;
87
88         if (buf == NULL || extend_num == NULL)
89                 return E_HDMI_EDID_PARAM;
90
91         *extend_num = buf[0x7e];
92         #ifdef DEBUG
93         EDBG("[EDID] extend block num is %d\n", buf[0x7e]);
94         #endif
95
96         /* Check first 8 byte to ensure it is an edid base block. */
97         if (buf[0] != 0x00 ||
98             buf[1] != 0xFF ||
99             buf[2] != 0xFF ||
100             buf[3] != 0xFF ||
101             buf[4] != 0xFF ||
102             buf[5] != 0xFF ||
103             buf[6] != 0xFF ||
104             buf[7] != 0x00) {
105                 pr_err("[EDID] check header error\n");
106                 rc = E_HDMI_EDID_HEAD;
107                 goto out;
108         }
109
110         /* Checksum */
111         rc = hdmi_edid_checksum(buf);
112         if (rc != E_HDMI_EDID_SUCCESS) {
113                 pr_err("[EDID] base block checksum error\n");
114                 rc = E_HDMI_EDID_CHECKSUM;
115                 goto out;
116         }
117
118         pedid->specs = kzalloc(sizeof(*pedid->specs), GFP_KERNEL);
119         if (pedid->specs == NULL)
120                 return E_HDMI_EDID_NOMEMORY;
121
122         fb_edid_to_monspecs(buf, pedid->specs);
123
124 out:
125         if ((rc != E_HDMI_EDID_SUCCESS) &&
126             (*extend_num < 1 && *extend_num > 4))
127                 return rc;
128         else
129                 return E_HDMI_EDID_SUCCESS;
130 }
131
132 /* Parse CEA Short Video Descriptor */
133 static int hdmi_edid_get_cea_svd(unsigned char *buf, struct hdmi_edid *pedid)
134 {
135         int count, i, vic;
136
137         count = buf[0] & 0x1F;
138         for (i = 0; i < count; i++) {
139                 EDBG("[CEA] %02x VID %d native %d\n",
140                      buf[1 + i], buf[1 + i] & 0x7f, buf[1 + i] >> 7);
141                 vic = buf[1 + i] & 0x7f;
142                 hdmi_add_vic(vic, &pedid->modelist);
143         }
144 /*
145         struct list_head *pos;
146         struct display_modelist *modelist;
147
148         list_for_each(pos, &pedid->modelist) {
149                 modelist = list_entry(pos, struct display_modelist, list);
150                 pr_info("%s vic %d\n", __FUNCTION__, modelist->vic);
151         }
152 */      return 0;
153 }
154
155 /* Parse CEA Short Audio Descriptor */
156 static int hdmi_edid_parse_cea_sad(unsigned char *buf, struct hdmi_edid *pedid)
157 {
158         int i, count;
159
160         count = buf[0] & 0x1F;
161         pedid->audio = kmalloc((count/3)*sizeof(struct hdmi_audio), GFP_KERNEL);
162         if (pedid->audio == NULL)
163                 return E_HDMI_EDID_NOMEMORY;
164
165         pedid->audio_num = count/3;
166         for (i = 0; i < pedid->audio_num; i++) {
167                 pedid->audio[i].type = (buf[1 + i*3] >> 3) & 0x0F;
168                 pedid->audio[i].channel = (buf[1 + i*3] & 0x07) + 1;
169                 pedid->audio[i].rate = buf[1 + i*3 + 1];
170                 if (pedid->audio[i].type == HDMI_AUDIO_LPCM)
171                         pedid->audio[i].word_length = buf[1 + i*3 + 2];
172
173 /*              pr_info("type %d channel %d rate %d word length %d\n",
174                         pedid->audio[i].type, pedid->audio[i].channel,
175                         pedid->audio[i].rate, pedid->audio[i].word_length);
176 */      }
177         return E_HDMI_EDID_SUCCESS;
178 }
179
180 static int hdmi_edid_parse_3dinfo(unsigned char *buf, struct list_head *head)
181 {
182         int i, j, len = 0, format_3d, vic_mask;
183         unsigned char offset = 2, vic_2d, structure_3d;
184         struct list_head *pos;
185         struct display_modelist *modelist;
186
187         if (buf[1] & 0xe0) {
188                 len = (buf[1] & 0xe0) >> 5;
189                 for (i = 0; i < len; i++) {
190                         if (buf[offset]) {
191                                 vic_2d = (buf[offset] == 4) ?
192                                          98 : (96 - buf[offset]);
193                                 hdmi_add_vic(vic_2d, head);
194                         }
195                         offset++;
196                 }
197         }
198
199         if (buf[0] & 0x80) {
200                 /* 3d supported */
201                 len += (buf[1] & 0x1F) + 2;
202                 if (((buf[0] & 0x60) == 0x40) || ((buf[0] & 0x60) == 0x20)) {
203                         format_3d = buf[offset++] << 8;
204                         format_3d |= buf[offset++];
205                         if ((buf[0] & 0x60) == 0x20) {
206                                 vic_mask = 0xFFFF;
207                         } else {
208                                 vic_mask  = buf[offset++] << 8;
209                                 vic_mask |= buf[offset++];
210                         }
211                 } else {
212                         format_3d = 0;
213                         vic_mask = 0;
214                 }
215
216                 for (i = 0; i < 16; i++) {
217                         if (vic_mask & (1 << i)) {
218                                 j = 0;
219                                 for (pos = (head)->next; pos != (head);
220                                         pos = pos->next) {
221                                         if (j++ == i) {
222                                                 modelist =
223                         list_entry(pos, struct display_modelist, list);
224                                                 modelist->format_3d = format_3d;
225                                                 break;
226                                         }
227                                 }
228                         }
229                 }
230                 while (offset < len) {
231                         vic_2d = (buf[offset] & 0xF0) >> 4;
232                         structure_3d = (buf[offset++] & 0x0F);
233                         j = 0;
234                         for (pos = (head)->next; pos != (head);
235                                 pos = pos->next) {
236                                 j++;
237                                 if (j == vic_2d) {
238                                         modelist =
239                                 list_entry(pos, struct display_modelist, list);
240                                         modelist->format_3d |=
241                                                 (1 << structure_3d);
242                                         if (structure_3d & 0x08)
243                                                 modelist->detail_3d =
244                                                 (buf[offset++] & 0xF0) >> 4;
245                                         break;
246                                 }
247                         }
248                 }
249                 /* mandatory formats */
250                 for (pos = (head)->next; pos != (head); pos = pos->next) {
251                         modelist = list_entry(pos,
252                                               struct display_modelist,
253                                               list);
254                         if (modelist->vic == HDMI_1920X1080P_24HZ ||
255                             modelist->vic == HDMI_1280X720P_60HZ ||
256                             modelist->vic == HDMI_1280X720P_50HZ) {
257                                 modelist->format_3d |=
258                                         (1 << HDMI_3D_FRAME_PACKING) |
259                                         (1 << HDMI_3D_TOP_BOOTOM);
260                         } else if (modelist->vic == HDMI_1920X1080I_60HZ ||
261                                    modelist->vic == HDMI_1920X1080I_50HZ) {
262                                 modelist->format_3d |=
263                                         (1 << HDMI_3D_SIDE_BY_SIDE_HALF);
264                         }
265                 }
266         }
267
268         return 0;
269 }
270 static int hdmi_edmi_parse_vsdb(unsigned char *buf, struct hdmi_edid *pedid,
271                                 int cur_offset, int IEEEOUI)
272 {
273         int count, buf_offset;
274
275         count = buf[cur_offset] & 0x1F;
276         switch (IEEEOUI) {
277         case 0x0c03:
278                 pedid->sink_hdmi = 1;
279                 pedid->cecaddress = buf[cur_offset + 5];
280                 pedid->cecaddress |= buf[cur_offset + 4] << 8;
281                 EDBG("[CEA] CEC Physical addres is 0x%08x.\n",
282                      pedid->cecaddress);
283                 if (count > 6)
284                         pedid->deepcolor = (buf[cur_offset + 6] >> 3) & 0x0F;
285                 if (count > 7) {
286                         pedid->maxtmdsclock = buf[cur_offset + 7] * 5000000;
287                         EDBG("[CEA] maxtmdsclock is %d.\n",
288                              pedid->maxtmdsclock);
289                 }
290                 if (count > 8) {
291                         pedid->fields_present = buf[cur_offset + 8];
292                         EDBG("[CEA] fields_present is 0x%02x.\n",
293                              pedid->fields_present);
294                 }
295                 buf_offset = cur_offset + 9;
296                 if (pedid->fields_present & 0x80) {
297                         pedid->video_latency = buf[buf_offset++];
298                         pedid->audio_latency = buf[buf_offset++];
299                 }
300                 if (pedid->fields_present & 0x40) {
301                         pedid->interlaced_video_latency = buf[buf_offset++];
302                         pedid->interlaced_audio_latency = buf[buf_offset++];
303                 }
304                 if (pedid->fields_present & 0x20) {
305                         hdmi_edid_parse_3dinfo(buf + buf_offset,
306                                                &pedid->modelist);
307                 }
308                 break;
309         case 0xc45dd8:
310                 pedid->sink_hdmi = 1;
311                 pedid->hf_vsdb_version = buf[cur_offset + 4];
312                 switch (pedid->hf_vsdb_version) {
313                 case 1:/*compliant with HDMI Specification 2.0*/
314                         pedid->maxtmdsclock =
315                                 buf[cur_offset + 5] * 5000000;
316                         EDBG("[CEA] maxtmdsclock is %d.\n",
317                              pedid->maxtmdsclock);
318                         pedid->scdc_present = buf[cur_offset+6] >> 7;
319                         pedid->rr_capable =
320                                 (buf[cur_offset+6]&0x40) >> 6;
321                         pedid->lte_340mcsc_scramble =
322                                 (buf[cur_offset+6]&0x08) >> 3;
323                         pedid->independent_view =
324                                 (buf[cur_offset+6]&0x04) >> 2;
325                         pedid->dual_view =
326                                 (buf[cur_offset+6]&0x02) >> 1;
327                         pedid->osd_disparity_3d =
328                                 buf[cur_offset+6] & 0x01;
329                         pedid->deepcolor_420 =
330                                 (buf[cur_offset+7] & 0x7) << 1;
331                         break;
332                 default:
333                         pr_info("hf_vsdb_version = %d\n",
334                                 pedid->hf_vsdb_version);
335                         break;
336                 }
337                 break;
338         default:
339                 pr_info("IEEOUT = 0x%x\n", IEEEOUI);
340                 break;
341         }
342         return 0;
343 }
344
345 static void hdmi_edid_parse_yuv420cmdb(unsigned char *buf, int count,
346                                        struct list_head *head)
347 {
348         struct list_head *pos;
349         struct display_modelist *modelist;
350         int i, j, yuv420_mask, vic;
351
352         for (i = 0; i < count - 1; i++) {
353                 EDBG("vic which support yuv420 mode is %x\n", buf[i]);
354                 yuv420_mask |= buf[i] << (8 * i);
355         }
356         for (i = 0; i < 32; i++) {
357                 if (yuv420_mask & (1 << i)) {
358                         j = 0;
359                         for (pos = head->next; pos != (head); pos = pos->next) {
360                                 if (j++ == i) {
361                                         modelist =
362                                 list_entry(pos, struct display_modelist, list);
363                                         vic = modelist->vic |
364                                               HDMI_VIDEO_YUV420;
365                                         hdmi_add_vic(vic, head);
366                                         break;
367                                 }
368                         }
369                 }
370         }
371 }
372
373 /* Parse CEA 861 Serial Extension. */
374 static int hdmi_edid_parse_extensions_cea(unsigned char *buf,
375                                           struct hdmi_edid *pedid)
376 {
377         unsigned int ddc_offset, native_dtd_num, cur_offset = 4;
378         unsigned int tag, IEEEOUI = 0, count, i;
379 /*      unsigned int underscan_support, baseaudio_support; */
380
381         if (buf == NULL)
382                 return E_HDMI_EDID_PARAM;
383
384         /* Check ces extension version */
385         if (buf[1] != 3) {
386                 pr_err("[CEA] error version.\n");
387                 return E_HDMI_EDID_VERSION;
388         }
389
390         ddc_offset = buf[2];
391 /*      underscan_support = (buf[3] >> 7) & 0x01;
392 */      pedid->baseaudio_support = (buf[3] >> 6) & 0x01;
393         pedid->ycbcr444 = (buf[3] >> 5) & 0x01;
394         pedid->ycbcr422 = (buf[3] >> 4) & 0x01;
395         native_dtd_num = buf[3] & 0x0F;
396 /*      EDBG("[CEA] ddc_offset %d underscan_support %d
397             baseaudio_support %d yuv_support %d
398             native_dtd_num %d\n",
399             ddc_offset, underscan_support, baseaudio_support,
400             yuv_support, native_dtd_num);
401 */      /* Parse data block */
402         while (cur_offset < ddc_offset) {
403                 tag = buf[cur_offset] >> 5;
404                 count = buf[cur_offset] & 0x1F;
405                 switch (tag) {
406                 case 0x02:      /* Video Data Block */
407                         EDBG("[CEA] Video Data Block.\n");
408                         hdmi_edid_get_cea_svd(buf + cur_offset, pedid);
409                         break;
410                 case 0x01:      /* Audio Data Block */
411                         EDBG("[CEA] Audio Data Block.\n");
412                         hdmi_edid_parse_cea_sad(buf + cur_offset, pedid);
413                         break;
414                 case 0x04:      /* Speaker Allocation Data Block */
415                         EDBG("[CEA] Speaker Allocatio Data Block.\n");
416                         break;
417                 case 0x03:      /* Vendor Specific Data Block */
418                         EDBG("[CEA] Vendor Specific Data Block.\n");
419
420                         IEEEOUI = buf[cur_offset + 3];
421                         IEEEOUI <<= 8;
422                         IEEEOUI += buf[cur_offset + 2];
423                         IEEEOUI <<= 8;
424                         IEEEOUI += buf[cur_offset + 1];
425                         EDBG("[CEA] IEEEOUI is 0x%08x.\n", IEEEOUI);
426
427                         hdmi_edmi_parse_vsdb(buf, pedid,
428                                              cur_offset, IEEEOUI);
429                         break;
430                 case 0x05:      /* VESA DTC Data Block */
431                         EDBG("[CEA] VESA DTC Data Block.\n");
432                         break;
433                 case 0x07:      /* Use Extended Tag */
434                         EDBG("[CEA] Use Extended Tag Data Block %02x.\n",
435                              buf[cur_offset + 1]);
436                         switch (buf[cur_offset + 1]) {
437                         case 0x00:
438                                 EDBG("[CEA] Video Capability Data Block\n");
439                                 EDBG("value is %02x\n", buf[cur_offset + 2]);
440                                 break;
441                         case 0x05:
442                                 EDBG("[CEA] Colorimetry Data Block\n");
443                                 EDBG("value is %02x\n", buf[cur_offset + 2]);
444                                 pedid->colorimetry = buf[cur_offset + 2];
445                                 break;
446                         case 0x0e:
447                                 EDBG("[CEA] YCBCR 4:2:0 Video Data Block\n");
448                                 for (i = 0; i < count - 1; i++) {
449                                         EDBG("mode is %d\n",
450                                              buf[cur_offset + 2 + i]);
451                                         pedid->ycbcr420 = 1;
452                                         IEEEOUI = buf[cur_offset + 2 + i] |
453                                                   HDMI_VIDEO_YUV420;
454                                         hdmi_add_vic(IEEEOUI,
455                                                      &pedid->modelist);
456                                 }
457                                 break;
458                         case 0x0f:
459                                 EDBG("[CEA] YCBCR 4:2:0 Capability Map Data\n");
460                                 hdmi_edid_parse_yuv420cmdb(&buf[cur_offset+2],
461                                                            count,
462                                                            &pedid->modelist);
463                                 pedid->ycbcr420 = 1;
464                                 break;
465                         }
466                         break;
467                 default:
468                         pr_err("[CEA] unkowned data block tag.\n");
469                         break;
470                 }
471                 cur_offset += (buf[cur_offset] & 0x1F) + 1;
472         }
473 #if 1
474 {
475         /* Parse DTD */
476         struct fb_videomode *vmode =
477                 kmalloc(sizeof(struct fb_videomode), GFP_KERNEL);
478
479         if (vmode == NULL)
480                 return E_HDMI_EDID_SUCCESS;
481         while (ddc_offset < HDMI_EDID_BLOCK_SIZE - 2) {
482                 if (!buf[ddc_offset] && !buf[ddc_offset + 1])
483                         break;
484                 memset(vmode, 0, sizeof(struct fb_videomode));
485                 hdmi_edid_parse_dtd(buf + ddc_offset, vmode);
486                 hdmi_add_vic(hdmi_videomode_to_vic(vmode), &pedid->modelist);
487                 ddc_offset += 18;
488         }
489         kfree(vmode);
490 }
491 #endif
492         return E_HDMI_EDID_SUCCESS;
493 }
494
495 int hdmi_edid_parse_extensions(unsigned char *buf, struct hdmi_edid *pedid)
496 {
497         int rc;
498
499         if (buf == NULL || pedid == NULL)
500                 return E_HDMI_EDID_PARAM;
501
502         /* Checksum */
503         rc = hdmi_edid_checksum(buf);
504         if (rc != E_HDMI_EDID_SUCCESS) {
505                 pr_err("[EDID] extensions block checksum error\n");
506                 return E_HDMI_EDID_CHECKSUM;
507         }
508
509         switch (buf[0]) {
510         case 0xF0:
511                 EDBG("[EDID-EXTEND] Iextensions block map.\n");
512                 break;
513         case 0x02:
514                 EDBG("[EDID-EXTEND] CEA 861 Series Extension.\n");
515                 hdmi_edid_parse_extensions_cea(buf, pedid);
516                 break;
517         case 0x10:
518                 EDBG("[EDID-EXTEND] Video Timing Block Extension.\n");
519                 break;
520         case 0x40:
521                 EDBG("[EDID-EXTEND] Display Information Extension.\n");
522                 break;
523         case 0x50:
524                 EDBG("[EDID-EXTEND] Localized String Extension.\n");
525                 break;
526         case 0x60:
527                 EDBG("[EDID-EXTEND] Digital Packet Video Link Extension.\n");
528                 break;
529         default:
530                 pr_err("[EDID-EXTEND] Unkowned Extension.\n");
531                 return E_HDMI_EDID_UNKOWNDATA;
532         }
533
534         return E_HDMI_EDID_SUCCESS;
535 }