xref: /openbmc/linux/drivers/gpu/drm/drm_edid.c (revision 78c99ba1)
1 /*
2  * Copyright (c) 2006 Luc Verhaegen (quirks list)
3  * Copyright (c) 2007-2008 Intel Corporation
4  *   Jesse Barnes <jesse.barnes@intel.com>
5  *
6  * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
7  * FB layer.
8  *   Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sub license,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the
18  * next paragraph) shall be included in all copies or substantial portions
19  * of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 #include <linux/kernel.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-algo-bit.h>
32 #include "drmP.h"
33 #include "drm_edid.h"
34 
35 /*
36  * TODO:
37  *   - support EDID 1.4 (incl. CE blocks)
38  */
39 
40 /*
41  * EDID blocks out in the wild have a variety of bugs, try to collect
42  * them here (note that userspace may work around broken monitors first,
43  * but fixes should make their way here so that the kernel "just works"
44  * on as many displays as possible).
45  */
46 
47 /* First detailed mode wrong, use largest 60Hz mode */
48 #define EDID_QUIRK_PREFER_LARGE_60		(1 << 0)
49 /* Reported 135MHz pixel clock is too high, needs adjustment */
50 #define EDID_QUIRK_135_CLOCK_TOO_HIGH		(1 << 1)
51 /* Prefer the largest mode at 75 Hz */
52 #define EDID_QUIRK_PREFER_LARGE_75		(1 << 2)
53 /* Detail timing is in cm not mm */
54 #define EDID_QUIRK_DETAILED_IN_CM		(1 << 3)
55 /* Detailed timing descriptors have bogus size values, so just take the
56  * maximum size and use that.
57  */
58 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE	(1 << 4)
59 /* Monitor forgot to set the first detailed is preferred bit. */
60 #define EDID_QUIRK_FIRST_DETAILED_PREFERRED	(1 << 5)
61 /* use +hsync +vsync for detailed mode */
62 #define EDID_QUIRK_DETAILED_SYNC_PP		(1 << 6)
63 
64 static struct edid_quirk {
65 	char *vendor;
66 	int product_id;
67 	u32 quirks;
68 } edid_quirk_list[] = {
69 	/* Acer AL1706 */
70 	{ "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
71 	/* Acer F51 */
72 	{ "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
73 	/* Unknown Acer */
74 	{ "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
75 
76 	/* Belinea 10 15 55 */
77 	{ "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
78 	{ "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
79 
80 	/* Envision Peripherals, Inc. EN-7100e */
81 	{ "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
82 
83 	/* Funai Electronics PM36B */
84 	{ "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
85 	  EDID_QUIRK_DETAILED_IN_CM },
86 
87 	/* LG Philips LCD LP154W01-A5 */
88 	{ "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
89 	{ "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
90 
91 	/* Philips 107p5 CRT */
92 	{ "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
93 
94 	/* Proview AY765C */
95 	{ "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
96 
97 	/* Samsung SyncMaster 205BW.  Note: irony */
98 	{ "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
99 	/* Samsung SyncMaster 22[5-6]BW */
100 	{ "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
101 	{ "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
102 };
103 
104 
105 /* Valid EDID header has these bytes */
106 static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
107 
108 /**
109  * edid_is_valid - sanity check EDID data
110  * @edid: EDID data
111  *
112  * Sanity check the EDID block by looking at the header, the version number
113  * and the checksum.  Return 0 if the EDID doesn't check out, or 1 if it's
114  * valid.
115  */
116 static bool edid_is_valid(struct edid *edid)
117 {
118 	int i;
119 	u8 csum = 0;
120 	u8 *raw_edid = (u8 *)edid;
121 
122 	if (memcmp(edid->header, edid_header, sizeof(edid_header)))
123 		goto bad;
124 	if (edid->version != 1) {
125 		DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
126 		goto bad;
127 	}
128 	if (edid->revision > 4)
129 		DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
130 
131 	for (i = 0; i < EDID_LENGTH; i++)
132 		csum += raw_edid[i];
133 	if (csum) {
134 		DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
135 		goto bad;
136 	}
137 
138 	return 1;
139 
140 bad:
141 	if (raw_edid) {
142 		DRM_ERROR("Raw EDID:\n");
143 		print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
144 		printk("\n");
145 	}
146 	return 0;
147 }
148 
149 /**
150  * edid_vendor - match a string against EDID's obfuscated vendor field
151  * @edid: EDID to match
152  * @vendor: vendor string
153  *
154  * Returns true if @vendor is in @edid, false otherwise
155  */
156 static bool edid_vendor(struct edid *edid, char *vendor)
157 {
158 	char edid_vendor[3];
159 
160 	edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
161 	edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
162 			  ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
163 	edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
164 
165 	return !strncmp(edid_vendor, vendor, 3);
166 }
167 
168 /**
169  * edid_get_quirks - return quirk flags for a given EDID
170  * @edid: EDID to process
171  *
172  * This tells subsequent routines what fixes they need to apply.
173  */
174 static u32 edid_get_quirks(struct edid *edid)
175 {
176 	struct edid_quirk *quirk;
177 	int i;
178 
179 	for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
180 		quirk = &edid_quirk_list[i];
181 
182 		if (edid_vendor(edid, quirk->vendor) &&
183 		    (EDID_PRODUCT_ID(edid) == quirk->product_id))
184 			return quirk->quirks;
185 	}
186 
187 	return 0;
188 }
189 
190 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
191 #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
192 
193 
194 /**
195  * edid_fixup_preferred - set preferred modes based on quirk list
196  * @connector: has mode list to fix up
197  * @quirks: quirks list
198  *
199  * Walk the mode list for @connector, clearing the preferred status
200  * on existing modes and setting it anew for the right mode ala @quirks.
201  */
202 static void edid_fixup_preferred(struct drm_connector *connector,
203 				 u32 quirks)
204 {
205 	struct drm_display_mode *t, *cur_mode, *preferred_mode;
206 	int target_refresh = 0;
207 
208 	if (list_empty(&connector->probed_modes))
209 		return;
210 
211 	if (quirks & EDID_QUIRK_PREFER_LARGE_60)
212 		target_refresh = 60;
213 	if (quirks & EDID_QUIRK_PREFER_LARGE_75)
214 		target_refresh = 75;
215 
216 	preferred_mode = list_first_entry(&connector->probed_modes,
217 					  struct drm_display_mode, head);
218 
219 	list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
220 		cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
221 
222 		if (cur_mode == preferred_mode)
223 			continue;
224 
225 		/* Largest mode is preferred */
226 		if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
227 			preferred_mode = cur_mode;
228 
229 		/* At a given size, try to get closest to target refresh */
230 		if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
231 		    MODE_REFRESH_DIFF(cur_mode, target_refresh) <
232 		    MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
233 			preferred_mode = cur_mode;
234 		}
235 	}
236 
237 	preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
238 }
239 
240 /**
241  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
242  * @t: standard timing params
243  *
244  * Take the standard timing params (in this case width, aspect, and refresh)
245  * and convert them into a real mode using CVT.
246  *
247  * Punts for now, but should eventually use the FB layer's CVT based mode
248  * generation code.
249  */
250 struct drm_display_mode *drm_mode_std(struct drm_device *dev,
251 				      struct std_timing *t)
252 {
253 	struct drm_display_mode *mode;
254 	int hsize = t->hsize * 8 + 248, vsize;
255 
256 	mode = drm_mode_create(dev);
257 	if (!mode)
258 		return NULL;
259 
260 	if (t->aspect_ratio == 0)
261 		vsize = (hsize * 10) / 16;
262 	else if (t->aspect_ratio == 1)
263 		vsize = (hsize * 3) / 4;
264 	else if (t->aspect_ratio == 2)
265 		vsize = (hsize * 4) / 5;
266 	else
267 		vsize = (hsize * 9) / 16;
268 
269 	drm_mode_set_name(mode);
270 
271 	return mode;
272 }
273 
274 /**
275  * drm_mode_detailed - create a new mode from an EDID detailed timing section
276  * @dev: DRM device (needed to create new mode)
277  * @edid: EDID block
278  * @timing: EDID detailed timing info
279  * @quirks: quirks to apply
280  *
281  * An EDID detailed timing block contains enough info for us to create and
282  * return a new struct drm_display_mode.
283  */
284 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
285 						  struct edid *edid,
286 						  struct detailed_timing *timing,
287 						  u32 quirks)
288 {
289 	struct drm_display_mode *mode;
290 	struct detailed_pixel_timing *pt = &timing->data.pixel_data;
291 
292 	/* ignore tiny modes */
293 	if (((pt->hactive_hi << 8) | pt->hactive_lo) < 64 ||
294 	    ((pt->vactive_hi << 8) | pt->hactive_lo) < 64)
295 		return NULL;
296 
297 	if (pt->stereo) {
298 		printk(KERN_WARNING "stereo mode not supported\n");
299 		return NULL;
300 	}
301 	if (!pt->separate_sync) {
302 		printk(KERN_WARNING "integrated sync not supported\n");
303 		return NULL;
304 	}
305 
306 	mode = drm_mode_create(dev);
307 	if (!mode)
308 		return NULL;
309 
310 	mode->type = DRM_MODE_TYPE_DRIVER;
311 
312 	if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
313 		timing->pixel_clock = 1088;
314 
315 	mode->clock = timing->pixel_clock * 10;
316 
317 	mode->hdisplay = (pt->hactive_hi << 8) | pt->hactive_lo;
318 	mode->hsync_start = mode->hdisplay + ((pt->hsync_offset_hi << 8) |
319 					      pt->hsync_offset_lo);
320 	mode->hsync_end = mode->hsync_start +
321 		((pt->hsync_pulse_width_hi << 8) |
322 		 pt->hsync_pulse_width_lo);
323 	mode->htotal = mode->hdisplay + ((pt->hblank_hi << 8) | pt->hblank_lo);
324 
325 	mode->vdisplay = (pt->vactive_hi << 8) | pt->vactive_lo;
326 	mode->vsync_start = mode->vdisplay + ((pt->vsync_offset_hi << 4) |
327 					      pt->vsync_offset_lo);
328 	mode->vsync_end = mode->vsync_start +
329 		((pt->vsync_pulse_width_hi << 4) |
330 		 pt->vsync_pulse_width_lo);
331 	mode->vtotal = mode->vdisplay + ((pt->vblank_hi << 8) | pt->vblank_lo);
332 
333 	drm_mode_set_name(mode);
334 
335 	if (pt->interlaced)
336 		mode->flags |= DRM_MODE_FLAG_INTERLACE;
337 
338 	if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
339 		pt->hsync_positive = 1;
340 		pt->vsync_positive = 1;
341 	}
342 
343 	mode->flags |= pt->hsync_positive ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
344 	mode->flags |= pt->vsync_positive ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
345 
346 	mode->width_mm = pt->width_mm_lo | (pt->width_mm_hi << 8);
347 	mode->height_mm = pt->height_mm_lo | (pt->height_mm_hi << 8);
348 
349 	if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
350 		mode->width_mm *= 10;
351 		mode->height_mm *= 10;
352 	}
353 
354 	if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
355 		mode->width_mm = edid->width_cm * 10;
356 		mode->height_mm = edid->height_cm * 10;
357 	}
358 
359 	return mode;
360 }
361 
362 /*
363  * Detailed mode info for the EDID "established modes" data to use.
364  */
365 static struct drm_display_mode edid_est_modes[] = {
366 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
367 		   968, 1056, 0, 600, 601, 605, 628, 0,
368 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
369 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
370 		   896, 1024, 0, 600, 601, 603,  625, 0,
371 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
372 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
373 		   720, 840, 0, 480, 481, 484, 500, 0,
374 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
375 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
376 		   704,  832, 0, 480, 489, 491, 520, 0,
377 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
378 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
379 		   768,  864, 0, 480, 483, 486, 525, 0,
380 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
381 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
382 		   752, 800, 0, 480, 490, 492, 525, 0,
383 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
384 	{ DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
385 		   846, 900, 0, 400, 421, 423,  449, 0,
386 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
387 	{ DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
388 		   846,  900, 0, 400, 412, 414, 449, 0,
389 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
390 	{ DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
391 		   1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
392 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
393 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
394 		   1136, 1312, 0,  768, 769, 772, 800, 0,
395 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
396 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
397 		   1184, 1328, 0,  768, 771, 777, 806, 0,
398 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
399 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
400 		   1184, 1344, 0,  768, 771, 777, 806, 0,
401 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
402 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
403 		   1208, 1264, 0, 768, 768, 776, 817, 0,
404 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
405 	{ DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
406 		   928, 1152, 0, 624, 625, 628, 667, 0,
407 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
408 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
409 		   896, 1056, 0, 600, 601, 604,  625, 0,
410 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
411 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
412 		   976, 1040, 0, 600, 637, 643, 666, 0,
413 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
414 	{ DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
415 		   1344, 1600, 0,  864, 865, 868, 900, 0,
416 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
417 };
418 
419 #define EDID_EST_TIMINGS 16
420 #define EDID_STD_TIMINGS 8
421 #define EDID_DETAILED_TIMINGS 4
422 
423 /**
424  * add_established_modes - get est. modes from EDID and add them
425  * @edid: EDID block to scan
426  *
427  * Each EDID block contains a bitmap of the supported "established modes" list
428  * (defined above).  Tease them out and add them to the global modes list.
429  */
430 static int add_established_modes(struct drm_connector *connector, struct edid *edid)
431 {
432 	struct drm_device *dev = connector->dev;
433 	unsigned long est_bits = edid->established_timings.t1 |
434 		(edid->established_timings.t2 << 8) |
435 		((edid->established_timings.mfg_rsvd & 0x80) << 9);
436 	int i, modes = 0;
437 
438 	for (i = 0; i <= EDID_EST_TIMINGS; i++)
439 		if (est_bits & (1<<i)) {
440 			struct drm_display_mode *newmode;
441 			newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
442 			if (newmode) {
443 				drm_mode_probed_add(connector, newmode);
444 				modes++;
445 			}
446 		}
447 
448 	return modes;
449 }
450 
451 /**
452  * add_standard_modes - get std. modes from EDID and add them
453  * @edid: EDID block to scan
454  *
455  * Standard modes can be calculated using the CVT standard.  Grab them from
456  * @edid, calculate them, and add them to the list.
457  */
458 static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
459 {
460 	struct drm_device *dev = connector->dev;
461 	int i, modes = 0;
462 
463 	for (i = 0; i < EDID_STD_TIMINGS; i++) {
464 		struct std_timing *t = &edid->standard_timings[i];
465 		struct drm_display_mode *newmode;
466 
467 		/* If std timings bytes are 1, 1 it's empty */
468 		if (t->hsize == 1 && (t->aspect_ratio | t->vfreq) == 1)
469 			continue;
470 
471 		newmode = drm_mode_std(dev, &edid->standard_timings[i]);
472 		if (newmode) {
473 			drm_mode_probed_add(connector, newmode);
474 			modes++;
475 		}
476 	}
477 
478 	return modes;
479 }
480 
481 /**
482  * add_detailed_modes - get detailed mode info from EDID data
483  * @connector: attached connector
484  * @edid: EDID block to scan
485  * @quirks: quirks to apply
486  *
487  * Some of the detailed timing sections may contain mode information.  Grab
488  * it and add it to the list.
489  */
490 static int add_detailed_info(struct drm_connector *connector,
491 			     struct edid *edid, u32 quirks)
492 {
493 	struct drm_device *dev = connector->dev;
494 	int i, j, modes = 0;
495 
496 	for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
497 		struct detailed_timing *timing = &edid->detailed_timings[i];
498 		struct detailed_non_pixel *data = &timing->data.other_data;
499 		struct drm_display_mode *newmode;
500 
501 		/* EDID up to and including 1.2 may put monitor info here */
502 		if (edid->version == 1 && edid->revision < 3)
503 			continue;
504 
505 		/* Detailed mode timing */
506 		if (timing->pixel_clock) {
507 			newmode = drm_mode_detailed(dev, edid, timing, quirks);
508 			if (!newmode)
509 				continue;
510 
511 			/* First detailed mode is preferred */
512 			if (i == 0 && edid->preferred_timing)
513 				newmode->type |= DRM_MODE_TYPE_PREFERRED;
514 			drm_mode_probed_add(connector, newmode);
515 
516 			modes++;
517 			continue;
518 		}
519 
520 		/* Other timing or info */
521 		switch (data->type) {
522 		case EDID_DETAIL_MONITOR_SERIAL:
523 			break;
524 		case EDID_DETAIL_MONITOR_STRING:
525 			break;
526 		case EDID_DETAIL_MONITOR_RANGE:
527 			/* Get monitor range data */
528 			break;
529 		case EDID_DETAIL_MONITOR_NAME:
530 			break;
531 		case EDID_DETAIL_MONITOR_CPDATA:
532 			break;
533 		case EDID_DETAIL_STD_MODES:
534 			/* Five modes per detailed section */
535 			for (j = 0; j < 5; i++) {
536 				struct std_timing *std;
537 				struct drm_display_mode *newmode;
538 
539 				std = &data->data.timings[j];
540 				newmode = drm_mode_std(dev, std);
541 				if (newmode) {
542 					drm_mode_probed_add(connector, newmode);
543 					modes++;
544 				}
545 			}
546 			break;
547 		default:
548 			break;
549 		}
550 	}
551 
552 	return modes;
553 }
554 
555 #define DDC_ADDR 0x50
556 /**
557  * Get EDID information via I2C.
558  *
559  * \param adapter : i2c device adaptor
560  * \param buf     : EDID data buffer to be filled
561  * \param len     : EDID data buffer length
562  * \return 0 on success or -1 on failure.
563  *
564  * Try to fetch EDID information by calling i2c driver function.
565  */
566 int drm_do_probe_ddc_edid(struct i2c_adapter *adapter,
567 			  unsigned char *buf, int len)
568 {
569 	unsigned char start = 0x0;
570 	struct i2c_msg msgs[] = {
571 		{
572 			.addr	= DDC_ADDR,
573 			.flags	= 0,
574 			.len	= 1,
575 			.buf	= &start,
576 		}, {
577 			.addr	= DDC_ADDR,
578 			.flags	= I2C_M_RD,
579 			.len	= len,
580 			.buf	= buf,
581 		}
582 	};
583 
584 	if (i2c_transfer(adapter, msgs, 2) == 2)
585 		return 0;
586 
587 	dev_info(&adapter->dev, "unable to read EDID block.\n");
588 	return -1;
589 }
590 EXPORT_SYMBOL(drm_do_probe_ddc_edid);
591 
592 /**
593  * Get EDID information.
594  *
595  * \param adapter : i2c device adaptor.
596  * \param buf     : EDID data buffer to be filled
597  * \param len     : EDID data buffer length
598  * \return 0 on success or -1 on failure.
599  *
600  * Initialize DDC, then fetch EDID information
601  * by calling drm_do_probe_ddc_edid function.
602  */
603 static int drm_ddc_read(struct i2c_adapter *adapter,
604 			unsigned char *buf, int len)
605 {
606 	struct i2c_algo_bit_data *algo_data = adapter->algo_data;
607 	int i, j;
608 	int ret = -1;
609 
610 	algo_data->setscl(algo_data->data, 1);
611 
612 	for (i = 0; i < 1; i++) {
613 		/* For some old monitors we need the
614 		 * following process to initialize/stop DDC
615 		 */
616 		algo_data->setsda(algo_data->data, 1);
617 		msleep(13);
618 
619 		algo_data->setscl(algo_data->data, 1);
620 		for (j = 0; j < 5; j++) {
621 			msleep(10);
622 			if (algo_data->getscl(algo_data->data))
623 				break;
624 		}
625 		if (j == 5)
626 			continue;
627 
628 		algo_data->setsda(algo_data->data, 0);
629 		msleep(15);
630 		algo_data->setscl(algo_data->data, 0);
631 		msleep(15);
632 		algo_data->setsda(algo_data->data, 1);
633 		msleep(15);
634 
635 		/* Do the real work */
636 		ret = drm_do_probe_ddc_edid(adapter, buf, len);
637 		algo_data->setsda(algo_data->data, 0);
638 		algo_data->setscl(algo_data->data, 0);
639 		msleep(15);
640 
641 		algo_data->setscl(algo_data->data, 1);
642 		for (j = 0; j < 10; j++) {
643 			msleep(10);
644 			if (algo_data->getscl(algo_data->data))
645 				break;
646 		}
647 
648 		algo_data->setsda(algo_data->data, 1);
649 		msleep(15);
650 		algo_data->setscl(algo_data->data, 0);
651 		algo_data->setsda(algo_data->data, 0);
652 		if (ret == 0)
653 			break;
654 	}
655 	/* Release the DDC lines when done or the Apple Cinema HD display
656 	 * will switch off
657 	 */
658 	algo_data->setsda(algo_data->data, 1);
659 	algo_data->setscl(algo_data->data, 1);
660 
661 	return ret;
662 }
663 
664 static int drm_ddc_read_edid(struct drm_connector *connector,
665 			     struct i2c_adapter *adapter,
666 			     char *buf, int len)
667 {
668 	int ret;
669 
670 	ret = drm_ddc_read(adapter, buf, len);
671 	if (ret != 0) {
672 		dev_info(&connector->dev->pdev->dev, "%s: no EDID data\n",
673 			 drm_get_connector_name(connector));
674 		goto end;
675 	}
676 	if (!edid_is_valid((struct edid *)buf)) {
677 		dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
678 			 drm_get_connector_name(connector));
679 		ret = -1;
680 	}
681 end:
682 	return ret;
683 }
684 
685 #define MAX_EDID_EXT_NUM 4
686 /**
687  * drm_get_edid - get EDID data, if available
688  * @connector: connector we're probing
689  * @adapter: i2c adapter to use for DDC
690  *
691  * Poke the given connector's i2c channel to grab EDID data if possible.
692  *
693  * Return edid data or NULL if we couldn't find any.
694  */
695 struct edid *drm_get_edid(struct drm_connector *connector,
696 			  struct i2c_adapter *adapter)
697 {
698 	int ret;
699 	struct edid *edid;
700 
701 	edid = kmalloc(EDID_LENGTH * (MAX_EDID_EXT_NUM + 1),
702 		       GFP_KERNEL);
703 	if (edid == NULL) {
704 		dev_warn(&connector->dev->pdev->dev,
705 			 "Failed to allocate EDID\n");
706 		goto end;
707 	}
708 
709 	/* Read first EDID block */
710 	ret = drm_ddc_read_edid(connector, adapter,
711 				(unsigned char *)edid, EDID_LENGTH);
712 	if (ret != 0)
713 		goto clean_up;
714 
715 	/* There are EDID extensions to be read */
716 	if (edid->extensions != 0) {
717 		int edid_ext_num = edid->extensions;
718 
719 		if (edid_ext_num > MAX_EDID_EXT_NUM) {
720 			dev_warn(&connector->dev->pdev->dev,
721 				 "The number of extension(%d) is "
722 				 "over max (%d), actually read number (%d)\n",
723 				 edid_ext_num, MAX_EDID_EXT_NUM,
724 				 MAX_EDID_EXT_NUM);
725 			/* Reset EDID extension number to be read */
726 			edid_ext_num = MAX_EDID_EXT_NUM;
727 		}
728 		/* Read EDID including extensions too */
729 		ret = drm_ddc_read_edid(connector, adapter, (char *)edid,
730 					EDID_LENGTH * (edid_ext_num + 1));
731 		if (ret != 0)
732 			goto clean_up;
733 
734 	}
735 
736 	connector->display_info.raw_edid = (char *)edid;
737 	goto end;
738 
739 clean_up:
740 	kfree(edid);
741 	edid = NULL;
742 end:
743 	return edid;
744 
745 }
746 EXPORT_SYMBOL(drm_get_edid);
747 
748 #define HDMI_IDENTIFIER 0x000C03
749 #define VENDOR_BLOCK    0x03
750 /**
751  * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
752  * @edid: monitor EDID information
753  *
754  * Parse the CEA extension according to CEA-861-B.
755  * Return true if HDMI, false if not or unknown.
756  */
757 bool drm_detect_hdmi_monitor(struct edid *edid)
758 {
759 	char *edid_ext = NULL;
760 	int i, hdmi_id, edid_ext_num;
761 	int start_offset, end_offset;
762 	bool is_hdmi = false;
763 
764 	/* No EDID or EDID extensions */
765 	if (edid == NULL || edid->extensions == 0)
766 		goto end;
767 
768 	/* Chose real EDID extension number */
769 	edid_ext_num = edid->extensions > MAX_EDID_EXT_NUM ?
770 		       MAX_EDID_EXT_NUM : edid->extensions;
771 
772 	/* Find CEA extension */
773 	for (i = 0; i < edid_ext_num; i++) {
774 		edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
775 		/* This block is CEA extension */
776 		if (edid_ext[0] == 0x02)
777 			break;
778 	}
779 
780 	if (i == edid_ext_num)
781 		goto end;
782 
783 	/* Data block offset in CEA extension block */
784 	start_offset = 4;
785 	end_offset = edid_ext[2];
786 
787 	/*
788 	 * Because HDMI identifier is in Vendor Specific Block,
789 	 * search it from all data blocks of CEA extension.
790 	 */
791 	for (i = start_offset; i < end_offset;
792 		/* Increased by data block len */
793 		i += ((edid_ext[i] & 0x1f) + 1)) {
794 		/* Find vendor specific block */
795 		if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
796 			hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
797 				  edid_ext[i + 3] << 16;
798 			/* Find HDMI identifier */
799 			if (hdmi_id == HDMI_IDENTIFIER)
800 				is_hdmi = true;
801 			break;
802 		}
803 	}
804 
805 end:
806 	return is_hdmi;
807 }
808 EXPORT_SYMBOL(drm_detect_hdmi_monitor);
809 
810 /**
811  * drm_add_edid_modes - add modes from EDID data, if available
812  * @connector: connector we're probing
813  * @edid: edid data
814  *
815  * Add the specified modes to the connector's mode list.
816  *
817  * Return number of modes added or 0 if we couldn't find any.
818  */
819 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
820 {
821 	int num_modes = 0;
822 	u32 quirks;
823 
824 	if (edid == NULL) {
825 		return 0;
826 	}
827 	if (!edid_is_valid(edid)) {
828 		dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
829 			 drm_get_connector_name(connector));
830 		return 0;
831 	}
832 
833 	quirks = edid_get_quirks(edid);
834 
835 	num_modes += add_established_modes(connector, edid);
836 	num_modes += add_standard_modes(connector, edid);
837 	num_modes += add_detailed_info(connector, edid, quirks);
838 
839 	if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
840 		edid_fixup_preferred(connector, quirks);
841 
842 	connector->display_info.serration_vsync = edid->serration_vsync;
843 	connector->display_info.sync_on_green = edid->sync_on_green;
844 	connector->display_info.composite_sync = edid->composite_sync;
845 	connector->display_info.separate_syncs = edid->separate_syncs;
846 	connector->display_info.blank_to_black = edid->blank_to_black;
847 	connector->display_info.video_level = edid->video_level;
848 	connector->display_info.digital = edid->digital;
849 	connector->display_info.width_mm = edid->width_cm * 10;
850 	connector->display_info.height_mm = edid->height_cm * 10;
851 	connector->display_info.gamma = edid->gamma;
852 	connector->display_info.gtf_supported = edid->default_gtf;
853 	connector->display_info.standard_color = edid->standard_color;
854 	connector->display_info.display_type = edid->display_type;
855 	connector->display_info.active_off_supported = edid->pm_active_off;
856 	connector->display_info.suspend_supported = edid->pm_suspend;
857 	connector->display_info.standby_supported = edid->pm_standby;
858 	connector->display_info.gamma = edid->gamma;
859 
860 	return num_modes;
861 }
862 EXPORT_SYMBOL(drm_add_edid_modes);
863