drivers: video: tegra: Add blacklist for a user TV
[linux-3.10.git] / drivers / video / tegra / dc / edid.c
1 /*
2  * drivers/video/tegra/dc/edid.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Author: Erik Gilling <konkers@android.com>
6  *
7  * Copyright (c) 2010-2015, NVIDIA CORPORATION, All rights reserved.
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20
21 #include <linux/debugfs.h>
22 #include <linux/fb.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/vmalloc.h>
27 #include <linux/delay.h>
28
29 #include "edid.h"
30 #include "dc_priv.h"
31
32 struct tegra_edid_pvt {
33         struct kref                     refcnt;
34         struct tegra_edid_hdmi_eld      eld;
35         bool                            support_stereo;
36         bool                            support_underscan;
37         bool                            support_audio;
38         bool                            scdc_present;
39         bool                            db420_present;
40         bool                            hfvsdb_present;
41         int                             hdmi_vic_len;
42         u8                              hdmi_vic[7];
43         u16                     color_depth_flag;
44         u16                     max_tmds_char_rate_hf_mhz;
45         u16                     max_tmds_char_rate_hllc_mhz;
46         u16                     colorimetry;
47         u16                     min_vrr_fps;
48         bool                    hdr_present;
49         u8                      hdr_pckt_len;
50         u8                      hdr_eotf;
51         u8                      hdr_static_metadata;
52         u16                     hdr_desired_max_luma;
53         u16                     hdr_desired_max_frame_avg_luma;
54         u16                     hdr_desired_min_luma;
55         u32                     quirks;
56         /* Note: dc_edid must remain the last member */
57         struct tegra_dc_edid            dc_edid;
58 };
59
60 /* 720p 60Hz EDID */
61 static const char default_720p_edid[256] = {
62         0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
63         0x4c, 0x2d, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00,
64         0x2c, 0x0e, 0x01, 0x03, 0x80, 0x59, 0x32, 0x8c,
65         0x0a, 0xe2, 0xbd, 0xa1, 0x5b, 0x4a, 0x98, 0x24,
66         0x15, 0x47, 0x4a, 0x20, 0x00, 0x00, 0x01, 0x01,
67         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
68         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1d,
69         0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28,
70         0x55, 0x00, 0x75, 0xf2, 0x31, 0x00, 0x00, 0x1e,
71         0x01, 0x1d, 0x00, 0xbc, 0x52, 0xd0, 0x1e, 0x20,
72         0xb8, 0x28, 0x55, 0x40, 0x75, 0xf2, 0x31, 0x00,
73         0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x32,
74         0x3d, 0x0f, 0x2e, 0x08, 0x00, 0x0a, 0x20, 0x20,
75         0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
76         0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
77         0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0x81,
78         0x02, 0x03, 0x19, 0x71, 0x46, 0x84, 0x13, 0x05,
79         0x14, 0x03, 0x12, 0x23, 0x09, 0x07, 0x07, 0x83,
80         0x01, 0x00, 0x00, 0x65, 0x03, 0x0c, 0x00, 0x10,
81         0x00, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16,
82         0x20, 0x58, 0x2c, 0x25, 0x00, 0x75, 0xf2, 0x31,
83         0x00, 0x00, 0x9e, 0x01, 0x1d, 0x80, 0xd0, 0x72,
84         0x1c, 0x16, 0x20, 0x10, 0x2c, 0x25, 0x80, 0x75,
85         0xf2, 0x31, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0,
86         0x8a, 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96,
87         0x00, 0x75, 0xf2, 0x31, 0x00, 0x00, 0x18, 0x8c,
88         0x0a, 0xd0, 0x90, 0x20, 0x40, 0x31, 0x20, 0x0c,
89         0x40, 0x55, 0x00, 0x75, 0xf2, 0x31, 0x00, 0x00,
90         0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca,
94 };
95
96 #ifdef DEBUG
97 static char tegra_edid_dump_buff[16 * 1024];
98
99 static void tegra_edid_dump(struct tegra_edid *edid)
100 {
101         struct seq_file s;
102         int i;
103         char c;
104
105         memset(&s, 0x0, sizeof(s));
106
107         s.buf = tegra_edid_dump_buff;
108         s.size = sizeof(tegra_edid_dump_buff);
109         s.private = edid;
110
111         tegra_edid_show(&s, NULL);
112
113         i = 0;
114         while (i < s.count ) {
115                 if ((s.count - i) > 256) {
116                         c = s.buf[i + 256];
117                         s.buf[i + 256] = 0;
118                         printk("%s", s.buf + i);
119                         s.buf[i + 256] = c;
120                 } else {
121                         printk("%s", s.buf + i);
122                 }
123                 i += 256;
124         }
125 }
126 #else
127 static void tegra_edid_dump(struct tegra_edid *edid)
128 {
129 }
130 #endif
131
132
133 int tegra_edid_i2c_adap_change_rate(struct i2c_adapter *i2c_adap, int rate)
134 {
135         const int MIN_RATE = 5000, MAX_RATE = 4000000;
136         int err = 0, cur_rate = 0;
137         if (rate < MIN_RATE || rate > MAX_RATE) {
138                 pr_warn("Cannot change the i2c_ddc rate, the rate:%d cannot"
139 "be below minimum rate:%d or above maximum rate:%d", rate, MIN_RATE, MAX_RATE);
140                 return -1;
141         }
142
143         if (i2c_adap) {
144                 cur_rate = i2c_get_adapter_bus_clk_rate(i2c_adap);
145                 if (cur_rate == rate)
146                         return 0;
147
148                 err = i2c_set_adapter_bus_clk_rate(i2c_adap, rate);
149                 if (err)
150                         pr_warn("Could not change i2c_ddc sclk rate\n");
151                 else
152                         pr_warn("Switching i2c_ddc sclk rate: from %d, "
153 "to %d\n", cur_rate, rate);
154         } else {
155                 pr_warn("ddc i2c adapter NULL\n");
156                 err = -1;
157         }
158         return err;
159 }
160
161 static int tegra_edid_i2c_divide_rate(struct tegra_edid *edid)
162 {
163         struct i2c_adapter *i2c_adap = i2c_get_adapter(edid->dc->out->ddc_bus);
164         int new_rate = 0, old_rate = 0, err = 0;
165
166         if (i2c_adap) {
167                 old_rate = i2c_get_adapter_bus_clk_rate(i2c_adap);
168                 new_rate = old_rate >> 1;
169                 err = tegra_edid_i2c_adap_change_rate(i2c_adap, new_rate);
170         } else
171                 err = -1;
172         return err;
173 }
174
175 int tegra_edid_read_block(struct tegra_edid *edid, int block, u8 *data)
176 {
177         u8 block_buf[] = {block >> 1};
178         u8 cmd_buf[] = {(block % 0x2) * 128};
179         u8 i;
180         u8 last_checksum = 0;
181         size_t attempt_cnt = 0;
182         struct i2c_msg msg[] = {
183                 {
184                         .addr = 0x30,
185                         .flags = 0,
186                         .len = 1,
187                         .buf = block_buf,
188                 },
189                 {
190                         .addr = 0x50,
191                         .flags = 0,
192                         .len = 1,
193                         .buf = cmd_buf,
194                 },
195                 {
196                         .addr = 0x50,
197                         .flags = I2C_M_RD,
198                         .len = 128,
199                         .buf = data,
200                 }};
201         struct i2c_msg *m;
202         int msg_len;
203         if (block > 1) {
204                 msg_len = 3;
205                 m = msg;
206         } else {
207                 msg_len = 2;
208                 m = &msg[1];
209         }
210
211         do {
212                 u8 checksum = 0;
213                 int status = edid->i2c_ops.i2c_transfer(edid->dc, m, msg_len);
214
215                 if (status < 0)
216                         return status;
217
218                 if (status != msg_len)
219                         return -EIO;
220
221                 /* fix base block header if corrupted */
222                 if (!block) {
223                         for (i = 0; i < EDID_BASE_HEADER_SIZE; i++) {
224                                 if (data[i] != edid_base_header[i])
225                                         data[i] = edid_base_header[i];
226                         }
227                 }
228
229                 for (i = 0; i < 128; i++)
230                         checksum += data[i];
231                 if (checksum != 0) {
232                         /*
233                          * It is completely possible that the sink that we are
234                          * reading has a bad EDID checksum (specifically, some
235                          * of the older TVs). These TVs have the modes, etc
236                          * programmed in their EDID correctly, but just have
237                          * a bad checksum. It then becomes hard to distinguish
238                          * between an i2c failure vs bad EDID.
239                          * To get around this, read the EDID multiple times.
240                          * If the calculated checksum is the exact same
241                          * multiple number of times, just print a
242                          * warning and ignore.
243                          */
244
245                         if (attempt_cnt == 0)
246                                 last_checksum = checksum;
247
248                         /* On different checksum remainder, lower i2c speed */
249                         if (last_checksum != checksum) {
250                                 pr_warn("%s: checksum failed and did not match consecutive reads. Previous remainder was %u. New remainder is %u. Failed at attempt %zu\n",
251                                         __func__, last_checksum, checksum, attempt_cnt);
252                                 if (tegra_edid_i2c_divide_rate(edid)) {
253                                         pr_warn("Cannot halve i2c speed giving"
254 "up on trying to change the i2c speed for EDID read\n");
255                                         return -EIO;
256                                 } else {
257                                         attempt_cnt = 0;
258                                         continue;
259                                 }
260                         }
261                         usleep_range(TEGRA_EDID_MIN_RETRY_DELAY_US, TEGRA_EDID_MAX_RETRY_DELAY_US);
262                 }
263         } while (last_checksum != 0 && ++attempt_cnt < TEGRA_EDID_MAX_RETRY);
264
265         /*
266          * Re-calculate the checksum since the standard EDID parser doesn't
267          * like the bad checksum
268          */
269         if (last_checksum != 0) {
270                 u8 checksum = 0;
271
272                 edid->errors |= EDID_ERRORS_CHECKSUM_CORRUPTED;
273
274                 for (i = 0; i < 127; i++)
275                         checksum += data[i];
276
277                 checksum = (u8)(256 - checksum);
278                 data[127] = checksum;
279
280                 pr_warn("%s: remainder is %u for the last %d attempts. Assuming bad sink EDID and ignoring. New checksum is %u\n",
281                                 __func__, last_checksum, TEGRA_EDID_MAX_RETRY,
282                                 checksum);
283         }
284
285         return 0;
286 }
287
288 static int tegra_edid_parse_ext_block(const u8 *raw, int idx,
289                                struct tegra_edid_pvt *edid)
290 {
291         const u8 *ptr;
292         u8 tmp;
293         u8 code;
294         int len;
295         int i;
296         bool basic_audio = false;
297
298         if (!edid) {
299                 pr_err("%s: invalid argument\n", __func__);
300                 return -EINVAL;
301         }
302         ptr = &raw[0];
303
304         /* If CEA 861 block get info for eld struct */
305         if (ptr) {
306                 if (*ptr <= 3)
307                         edid->eld.eld_ver = 0x02;
308                 edid->eld.cea_edid_ver = ptr[1];
309
310                 /* check for basic audio support in CEA 861 block */
311                 if(raw[3] & (1<<6)) {
312                         /* For basic audio, set spk_alloc to Left+Right.
313                          * If there is a Speaker Alloc block this will
314                          * get over written with that value */
315                         basic_audio = true;
316                         edid->support_audio = 1;
317                 }
318         }
319
320         if (raw[3] & 0x80)
321                 edid->support_underscan = 1;
322         else
323                 edid->support_underscan = 0;
324
325         ptr = &raw[4];
326
327         while (ptr < &raw[idx]) {
328                 tmp = *ptr;
329                 len = tmp & 0x1f;
330
331                 /* HDMI Specification v1.4a, section 8.3.2:
332                  * see Table 8-16 for HDMI VSDB format.
333                  * data blocks have tags in top 3 bits:
334                  * tag code 2: video data block
335                  * tag code 3: vendor specific data block
336                  */
337                 code = (tmp >> 5) & 0x7;
338                 switch (code) {
339                 case CEA_DATA_BLOCK_AUDIO:
340                 {
341                         int sad_n = edid->eld.sad_count * 3;
342                         edid->eld.sad_count += len / 3;
343                         pr_debug("%s: incrementing eld.sad_count by %d to %d\n",
344                                  __func__, len / 3, edid->eld.sad_count);
345                         edid->eld.conn_type = 0x00;
346                         edid->eld.support_hdcp = 0x00;
347                         for (i = 0; (i < len) && (sad_n < ELD_MAX_SAD_BYTES);
348                              i++, sad_n++)
349                                 edid->eld.sad[sad_n] = ptr[i + 1];
350                         len++;
351                         ptr += len; /* adding the header */
352                         /* Got an audio data block so enable audio */
353                         if (basic_audio == true)
354                                 edid->eld.spk_alloc = 1;
355                         break;
356                 }
357                 /* case 2 is commented out for now */
358                 case CEA_DATA_BLOCK_VENDOR:
359                 {
360                         int j = 0;
361
362                         /* OUI for hdmi licensing, LLC */
363                         if ((ptr[1] == 0x03) &&
364                                 (ptr[2] == 0x0c) &&
365                                 (ptr[3] == 0)) {
366                                 edid->eld.port_id[0] = ptr[4];
367                                 edid->eld.port_id[1] = ptr[5];
368
369                                 if (len >= 7)
370                                         edid->max_tmds_char_rate_hllc_mhz =
371                                                                 ptr[7] * 5;
372                                 edid->max_tmds_char_rate_hllc_mhz =
373                                         edid->max_tmds_char_rate_hllc_mhz ? :
374                                         165; /* for <=165MHz field may be 0 */
375                         }
376
377                         /* OUI for hdmi forum */
378                         if ((ptr[1] == 0xd8) &&
379                                 (ptr[2] == 0x5d) &&
380                                 (ptr[3] == 0xc4)) {
381                                 edid->hfvsdb_present = true;
382                                 edid->color_depth_flag = ptr[7] &
383                                                         TEGRA_DC_Y420_MASK;
384                                 edid->max_tmds_char_rate_hf_mhz = ptr[5] * 5;
385                                 edid->scdc_present = (ptr[6] >> 7) & 0x1;
386                         }
387
388                         /* OUI for Nvidia */
389                         if ((ptr[1] == 0x4b) &&
390                                 (ptr[2] == 0x04) &&
391                                 (ptr[3] == 0)) {
392                                 /* version 1.0 vrr capabilities */
393                                 if (ptr[4] == 1)
394                                         edid->min_vrr_fps = ptr[5];
395                         }
396
397                         if ((len >= 8) &&
398                                 (ptr[1] == 0x03) &&
399                                 (ptr[2] == 0x0c) &&
400                                 (ptr[3] == 0)) {
401                                 j = 8;
402                                 tmp = ptr[j++];
403                                 /* HDMI_Video_present? */
404                                 if (tmp & 0x20) {
405                                         /* Latency_Fields_present? */
406                                         if (tmp & 0x80)
407                                                 j += 2;
408                                         /* I_Latency_Fields_present? */
409                                         if (tmp & 0x40)
410                                                 j += 2;
411                                         /* 3D_present? */
412                                         if (j <= len && (ptr[j] & 0x80))
413                                                 edid->support_stereo = 1;
414                                         /* HDMI_VIC_LEN */
415                                         if (++j <= len && (ptr[j] & 0xe0)) {
416                                                 int k = 0;
417                                                 edid->hdmi_vic_len = ptr[j] >> 5;
418                                                 for (k = 0; k < edid->hdmi_vic_len; k++)
419                                                     edid->hdmi_vic[k] = ptr[j+k+1];
420                                         }
421                                 }
422                         }
423                         if ((len > 5) &&
424                                 (ptr[1] == 0x03) &&
425                                 (ptr[2] == 0x0c) &&
426                                 (ptr[3] == 0)) {
427
428                                 edid->eld.support_ai = (ptr[6] & 0x80);
429                         }
430
431                         if ((len > 9) &&
432                                 (ptr[1] == 0x03) &&
433                                 (ptr[2] == 0x0c) &&
434                                 (ptr[3] == 0)) {
435
436                                 edid->eld.aud_synch_delay = ptr[10];
437                         }
438                         len++;
439                         ptr += len; /* adding the header */
440                         break;
441                 }
442                 case CEA_DATA_BLOCK_SPEAKER_ALLOC:
443                 {
444                         edid->eld.spk_alloc = ptr[1];
445                         len++;
446                         ptr += len; /* adding the header */
447                         break;
448                 }
449                 case CEA_DATA_BLOCK_EXT:
450                 {
451                         u8 ext_db = ptr[1];
452
453                         switch (ext_db) {
454                         case CEA_DATA_BLOCK_EXT_Y420VDB: /* fall through */
455                         case CEA_DATA_BLOCK_EXT_Y420CMDB:
456                                 edid->db420_present = true;
457                                 break;
458                         case CEA_DATA_BLOCK_EXT_CDB:
459                                 edid->colorimetry = ptr[2];
460                                 break;
461                         case CEA_DATA_BLOCK_EXT_HDR:
462                                 edid->hdr_pckt_len = ptr[0] & 0x0f;
463                                 edid->hdr_present = true;
464                                 edid->hdr_eotf = ptr[2];
465                                 edid->hdr_static_metadata = ptr[3];
466                                 if (edid->hdr_pckt_len > 5) {
467                                         edid->hdr_desired_max_luma = ptr[4];
468                                         edid->hdr_desired_max_frame_avg_luma =
469                                                                         ptr[5];
470                                         edid->hdr_desired_min_luma = ptr[6];
471                                 } else if (edid->hdr_pckt_len > 4) {
472                                         edid->hdr_desired_max_luma = ptr[4];
473                                         edid->hdr_desired_max_frame_avg_luma =
474                                                                         ptr[5];
475                                 } else
476                                         edid->hdr_desired_max_luma = ptr[4];
477                                 break;
478                         };
479
480                         len++;
481                         ptr += len;
482                         break;
483                 }
484                 default:
485                         len++; /* len does not include header */
486                         ptr += len;
487                         break;
488                 }
489         }
490
491         return 0;
492 }
493
494 static int tegra_edid_mode_support_stereo(struct fb_videomode *mode)
495 {
496         if (!mode)
497                 return 0;
498
499         if (mode->xres == 1280 &&
500                 mode->yres == 720 &&
501                 ((mode->refresh == 60) || (mode->refresh == 50)))
502                 return 1;
503
504         if (mode->xres == 1920 && mode->yres == 1080 && mode->refresh == 24)
505                 return 1;
506
507         return 0;
508 }
509
510 static void data_release(struct kref *ref)
511 {
512         struct tegra_edid_pvt *data =
513                 container_of(ref, struct tegra_edid_pvt, refcnt);
514         vfree(data);
515 }
516
517 u16 tegra_edid_get_cd_flag(struct tegra_edid *edid)
518 {
519         if (!edid || !edid->data) {
520                 pr_warn("edid invalid\n");
521                 return -EFAULT;
522         }
523
524         return edid->data->color_depth_flag;
525 }
526
527 u16 tegra_edid_get_ex_hdr_cap(struct tegra_edid *edid)
528 {
529         u16 ret = 0;
530         if (!edid || !edid->data) {
531                 pr_warn("edid invalid\n");
532                 return -EFAULT;
533         }
534
535         if (edid->data->hdr_present)
536                 ret |= FB_CAP_HDR;
537
538         return ret;
539 }
540
541 /* hdmi spec mandates sink to specify correct max_tmds_clk only for >165MHz */
542 u16 tegra_edid_get_max_clk_rate(struct tegra_edid *edid)
543 {
544         u16 tmds_hf, tmds_llc;
545
546         if (!edid || !edid->data) {
547                 pr_warn("edid invalid\n");
548                 return -EFAULT;
549         }
550
551         tmds_hf = edid->data->max_tmds_char_rate_hf_mhz;
552         tmds_llc = edid->data->max_tmds_char_rate_hllc_mhz;
553
554         if (tmds_hf || tmds_llc)
555                 return tmds_hf ? : tmds_llc;
556
557         return 0;
558 }
559
560 bool tegra_edid_is_scdc_present(struct tegra_edid *edid)
561 {
562         if (tegra_platform_is_linsim()) {
563                 return false;
564         }
565         if (!edid || !edid->data) {
566                 pr_warn("edid invalid\n");
567                 return false;
568         }
569
570         if (edid->data->scdc_present &&
571                 !tegra_edid_is_hfvsdb_present(edid)) {
572                 pr_warn("scdc presence incorrectly parsed\n");
573                 dump_stack();
574         }
575
576         return edid->data->scdc_present;
577 }
578
579 bool tegra_edid_is_hfvsdb_present(struct tegra_edid *edid)
580 {
581         if (!edid || !edid->data) {
582                 pr_warn("edid invalid\n");
583                 return false;
584         }
585
586         return edid->data->hfvsdb_present;
587 }
588
589 bool tegra_edid_is_420db_present(struct tegra_edid *edid)
590 {
591         if (!edid || !edid->data) {
592                 pr_warn("edid invalid\n");
593                 return false;
594         }
595
596         return edid->data->db420_present;
597 }
598
599 u32 tegra_edid_get_quirks(struct tegra_edid *edid)
600 {
601         if (!edid || !edid->data) {
602                 pr_warn("edid invalid\n");
603                 return false;
604         }
605
606         return edid->data->quirks;
607 }
608
609 u16 tegra_edid_get_ex_colorimetry(struct tegra_edid *edid)
610 {
611         if (!edid || !edid->data) {
612                 pr_warn("edid invalid\n");
613                 return 0;
614         }
615
616         return edid->data->colorimetry;
617 }
618
619 int tegra_edid_get_monspecs(struct tegra_edid *edid, struct fb_monspecs *specs)
620 {
621         int i;
622         int j;
623         int ret;
624         int extension_blocks;
625         struct tegra_edid_pvt *new_data, *old_data;
626         u8 checksum = 0;
627         u8 *data;
628         bool use_fallback = false;
629
630         new_data = vzalloc(SZ_32K + sizeof(struct tegra_edid_pvt));
631         if (!new_data)
632                 return -ENOMEM;
633
634         kref_init(&new_data->refcnt);
635
636         if (edid->errors & EDID_ERRORS_READ_FAILED)
637                 use_fallback = true;
638
639         edid->errors = 0;
640
641         data = new_data->dc_edid.buf;
642
643         if (edid->dc->vedid) {
644                 memcpy(data, edid->dc->vedid_data, 128);
645                 /* checksum new edid */
646                 for (i = 0; i < 128; i++)
647                         checksum += data[i];
648                 if (checksum != 0) {
649                         pr_err("%s: checksum failed\n", __func__);
650                         ret = -EINVAL;
651                         goto fail;
652                 }
653         } else if (use_fallback) {
654                 memcpy(data, default_720p_edid, 128);
655                 /* no checksum test needed */
656         } else {
657                 ret = tegra_edid_read_block(edid, 0, data);
658                 if (ret)
659                         goto fail;
660         }
661
662         memset(specs, 0x0, sizeof(struct fb_monspecs));
663         memset(&new_data->eld, 0x0, sizeof(new_data->eld));
664         fb_edid_to_monspecs(data, specs);
665         if (specs->modedb == NULL) {
666                 ret = -EINVAL;
667                 goto fail;
668         }
669         memcpy(new_data->eld.monitor_name, specs->monitor, sizeof(specs->monitor));
670         new_data->eld.mnl = strlen(new_data->eld.monitor_name) + 1;
671         new_data->eld.product_id[0] = data[0x8];
672         new_data->eld.product_id[1] = data[0x9];
673         new_data->eld.manufacture_id[0] = data[0xA];
674         new_data->eld.manufacture_id[1] = data[0xB];
675         new_data->quirks = tegra_edid_lookup_quirks(specs->manufacturer,
676                 specs->model, specs->monitor);
677
678         extension_blocks = data[0x7e];
679
680         /* verify only one extension block (to stay in bounds) */
681         if (use_fallback && extension_blocks != 1) {
682                 pr_err("%s: fallback edid parsing failed\n", __func__);
683                 ret = -EINVAL;
684                 goto fail;
685         }
686
687         for (i = 1; i <= extension_blocks; i++) {
688                 if (edid->dc->vedid) {
689                         memcpy(data + i * 128,
690                                 edid->dc->vedid_data + i * 128, 128);
691                         for (j = 0; j < 128; j++)
692                                 checksum += data[i * 128 + j];
693                         if (checksum != 0) {
694                                 pr_err("%s: checksum failed\n", __func__);
695                                 ret = -EINVAL;
696                                 goto fail;
697                         }
698                 } else if (use_fallback) {
699                         /* only one extension block, verified above */
700                         memcpy(data + i * 128,
701                                 default_720p_edid + i * 128, 128);
702                 } else {
703                         ret = tegra_edid_read_block(edid, i, data + i * 128);
704                         if (ret < 0)
705                                 goto fail;
706                 }
707
708                 if (data[i * 128] == 0x2) {
709                         fb_edid_add_monspecs(data + i * 128, specs);
710
711                         tegra_edid_parse_ext_block(data + i * 128,
712                                         data[i * 128 + 2], new_data);
713
714                         if (new_data->support_stereo) {
715                                 for (j = 0; j < specs->modedb_len; j++) {
716                                         if (tegra_edid_mode_support_stereo(
717                                                 &specs->modedb[j]))
718                                                 specs->modedb[j].vmode |=
719 #ifndef CONFIG_TEGRA_HDMI_74MHZ_LIMIT
720                                                 FB_VMODE_STEREO_FRAME_PACK;
721 #else
722                                                 FB_VMODE_STEREO_LEFT_RIGHT;
723 #endif
724                                 }
725                         }
726
727                         if (new_data->hdmi_vic_len > 0) {
728                                 int k;
729                                 int l = specs->modedb_len;
730                                 struct fb_videomode *m;
731                                 m = kzalloc((specs->modedb_len +
732                                     new_data->hdmi_vic_len) *
733                                     sizeof(struct fb_videomode), GFP_KERNEL);
734                                 if (!m)
735                                         break;
736                                 memcpy(m, specs->modedb, specs->modedb_len *
737                                         sizeof(struct fb_videomode));
738                                 for (k = 0; k < new_data->hdmi_vic_len; k++) {
739                                     unsigned vic = new_data->hdmi_vic[k];
740                                     if (vic >= HDMI_EXT_MODEDB_SIZE) {
741                                         pr_warning("Unsupported HDMI VIC %d, ignoring\n", vic);
742                                         continue;
743                                     }
744                                     memcpy(&m[l], &hdmi_ext_modes[vic],
745                                                 sizeof(m[l]));
746                                     m[l].vmode |= FB_VMODE_IS_HDMI_EXT;
747                                     l++;
748                                 }
749                                 kfree(specs->modedb);
750                                 specs->modedb = m;
751                                 specs->modedb_len = specs->modedb_len +
752                                                         new_data->hdmi_vic_len;
753                         }
754                 }
755         }
756
757 #ifdef CONFIG_ARCH_TEGRA_21x_SOC
758         /* T210 supports fractional divider and hence can support the * 1000 / 1001 modes.
759            For now, only enable support for 24, 30 and 60 Hz modes. */
760         {
761                 const int max_modes = 50;
762                 struct fb_videomode *frac_modes, *m;
763                 int frac_n = 0;
764                 frac_modes = kzalloc(sizeof(struct fb_videomode) * max_modes,
765                                      GFP_KERNEL);
766
767                 if (frac_modes) {
768                         for (j = 0; j < specs->modedb_len; ++j) {
769                                 int rate = tegra_dc_calc_fb_refresh(&specs->modedb[j]);
770                                 /*
771                                  * 1000/1001 modes are only supported on CEA
772                                  * SVDs or on HDMI EXT
773                                  * */
774                                 bool supported = ((specs->modedb[j].vmode &
775                                                    FB_VMODE_IS_CEA) &&
776                                                   !(specs->modedb[j].vmode &
777                                                     FB_VMODE_IS_DETAILED)) ||
778                                                  specs->modedb[j].vmode &
779                                                  FB_VMODE_IS_HDMI_EXT;
780                                 if (supported && (rate == 24000 ||
781                                      rate == 30000 ||
782                                     (rate > (60000 - 20) && rate < (60000 + 20))) &&
783                                     frac_n < max_modes) {
784                                         memcpy(&frac_modes[frac_n], &specs->modedb[j], sizeof(struct fb_videomode));
785                                         frac_modes[frac_n].pixclock = frac_modes[frac_n].pixclock * 1000 / 1001;
786                                         frac_modes[frac_n].vmode |= FB_VMODE_1000DIV1001;
787                                         frac_n++;
788                                 }
789                         }
790
791                         if (frac_n == max_modes)
792                                 pr_warn("Hit fractional mode limit %d!\n", frac_n);
793
794                         m = kzalloc((specs->modedb_len + frac_n) * sizeof(struct fb_videomode), GFP_KERNEL);
795                         if (m) {
796                                 memcpy(m, specs->modedb, specs->modedb_len * sizeof(struct fb_videomode));
797                                 memcpy(&m[specs->modedb_len], frac_modes, frac_n * sizeof(struct fb_videomode));
798                                 kfree(specs->modedb);
799                                 specs->modedb = m;
800                                 specs->modedb_len += frac_n;
801                         }
802
803                         kfree(frac_modes);
804                 }
805         }
806 #endif
807         if (use_fallback)
808                 edid->errors |= EDID_ERRORS_USING_FALLBACK;
809
810         new_data->dc_edid.len = i * 128;
811
812         mutex_lock(&edid->lock);
813         old_data = edid->data;
814         edid->data = new_data;
815         mutex_unlock(&edid->lock);
816
817         if (old_data)
818                 kref_put(&old_data->refcnt, data_release);
819
820         tegra_edid_dump(edid);
821         return 0;
822
823 fail:
824         vfree(new_data);
825         return ret;
826 }
827
828 int tegra_edid_audio_supported(struct tegra_edid *edid)
829 {
830         if ((!edid) || (!edid->data))
831                 return 0;
832
833         return edid->data->support_audio;
834 }
835
836 int tegra_edid_underscan_supported(struct tegra_edid *edid)
837 {
838         if ((!edid) || (!edid->data))
839                 return 0;
840
841         return edid->data->support_underscan;
842 }
843
844 int tegra_edid_get_eld(struct tegra_edid *edid, struct tegra_edid_hdmi_eld *elddata)
845 {
846         if (!elddata || !edid->data)
847                 return -EFAULT;
848
849         memcpy(elddata,&edid->data->eld,sizeof(struct tegra_edid_hdmi_eld));
850
851         return 0;
852 }
853
854 struct tegra_edid *tegra_edid_create(struct tegra_dc *dc,
855         i2c_transfer_func_t i2c_func)
856 {
857         struct tegra_edid *edid;
858
859         edid = kzalloc(sizeof(struct tegra_edid), GFP_KERNEL);
860         if (!edid)
861                 return ERR_PTR(-ENOMEM);
862
863         mutex_init(&edid->lock);
864         edid->i2c_ops.i2c_transfer = i2c_func;
865         edid->dc = dc;
866
867         return edid;
868 }
869
870 void tegra_edid_destroy(struct tegra_edid *edid)
871 {
872         if (edid->data)
873                 kref_put(&edid->data->refcnt, data_release);
874         kfree(edid);
875 }
876
877 struct tegra_dc_edid *tegra_edid_get_data(struct tegra_edid *edid)
878 {
879         struct tegra_edid_pvt *data;
880
881         mutex_lock(&edid->lock);
882         data = edid->data;
883         if (data)
884                 kref_get(&data->refcnt);
885         mutex_unlock(&edid->lock);
886
887         return data ? &data->dc_edid : NULL;
888 }
889
890 void tegra_edid_put_data(struct tegra_dc_edid *data)
891 {
892         struct tegra_edid_pvt *pvt;
893
894         if (!data)
895                 return;
896
897         pvt = container_of(data, struct tegra_edid_pvt, dc_edid);
898
899         kref_put(&pvt->refcnt, data_release);
900 }
901
902 int tegra_dc_edid_blob(struct tegra_dc *dc, struct i2c_msg *msgs, int num)
903 {
904         struct i2c_msg *pmsg;
905         int i;
906         int status = 0;
907         u32 len = 0;
908         struct device_node *np_panel = NULL;
909
910         np_panel = tegra_get_panel_node_out_type_check(dc,
911                 dc->pdata->default_out->type);
912
913         if (!np_panel || !of_device_is_available(np_panel))
914                 return -ENOENT;
915
916         for (i = 0; i < num; ++i) {
917                 pmsg = &msgs[i];
918
919                 if (pmsg->flags & I2C_M_RD) { /* Read */
920                         len = pmsg->len;
921                         status = of_property_read_u8_array(np_panel,
922                                 "nvidia,edid", pmsg->buf, len);
923
924                         if (status) {
925                                 dev_err(&dc->ndev->dev,
926                                         "Failed to read EDID blob from DT"
927                                         " addr:%d, size:%d\n",
928                                         pmsg->addr, len);
929                                 return status;
930                         }
931                 }
932         }
933         of_node_put(np_panel);
934         return i;
935 }
936
937 struct tegra_dc_edid *tegra_dc_get_edid(struct tegra_dc *dc)
938 {
939         if (!dc->edid)
940                 return ERR_PTR(-ENODEV);
941
942         return tegra_edid_get_data(dc->edid);
943 }
944 EXPORT_SYMBOL(tegra_dc_get_edid);
945
946 void tegra_dc_put_edid(struct tegra_dc_edid *edid)
947 {
948         tegra_edid_put_data(edid);
949 }
950 EXPORT_SYMBOL(tegra_dc_put_edid);
951
952 static const struct i2c_device_id tegra_edid_id[] = {
953         { "tegra_edid", 0 },
954         { }
955 };
956
957 MODULE_DEVICE_TABLE(i2c, tegra_edid_id);
958
959 static struct i2c_driver tegra_edid_driver = {
960         .id_table = tegra_edid_id,
961         .driver = {
962                 .name = "tegra_edid",
963         },
964 };
965
966 static int __init tegra_edid_init(void)
967 {
968         return i2c_add_driver(&tegra_edid_driver);
969 }
970
971 static void __exit tegra_edid_exit(void)
972 {
973         i2c_del_driver(&tegra_edid_driver);
974 }
975
976 module_init(tegra_edid_init);
977 module_exit(tegra_edid_exit);