2 * Copyright (c) 2006 Luc Verhaegen (quirks list)
3 * Copyright (c) 2007-2008 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
5 * Copyright 2010 Red Hat, Inc.
7 * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
9 * Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sub license,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice (including the
19 * next paragraph) shall be included in all copies or substantial portions
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
31 #include <linux/export.h>
32 #include <linux/kernel.h>
33 #include <linux/i2c.h>
35 #include <drm/drm_edid.h>
36 #include "drm_edid_modes.h"
37 #include <bus/iicbus/iic.h>
38 #include <bus/iicbus/iiconf.h>
39 #include "iicbus_if.h"
41 #define version_greater(edid, maj, min) \
42 (((edid)->version > (maj)) || \
43 ((edid)->version == (maj) && (edid)->revision > (min)))
45 #define EDID_EST_TIMINGS 16
46 #define EDID_STD_TIMINGS 8
47 #define EDID_DETAILED_TIMINGS 4
50 * EDID blocks out in the wild have a variety of bugs, try to collect
51 * them here (note that userspace may work around broken monitors first,
52 * but fixes should make their way here so that the kernel "just works"
53 * on as many displays as possible).
56 /* First detailed mode wrong, use largest 60Hz mode */
57 #define EDID_QUIRK_PREFER_LARGE_60 (1 << 0)
58 /* Reported 135MHz pixel clock is too high, needs adjustment */
59 #define EDID_QUIRK_135_CLOCK_TOO_HIGH (1 << 1)
60 /* Prefer the largest mode at 75 Hz */
61 #define EDID_QUIRK_PREFER_LARGE_75 (1 << 2)
62 /* Detail timing is in cm not mm */
63 #define EDID_QUIRK_DETAILED_IN_CM (1 << 3)
64 /* Detailed timing descriptors have bogus size values, so just take the
65 * maximum size and use that.
67 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE (1 << 4)
68 /* Monitor forgot to set the first detailed is preferred bit. */
69 #define EDID_QUIRK_FIRST_DETAILED_PREFERRED (1 << 5)
70 /* use +hsync +vsync for detailed mode */
71 #define EDID_QUIRK_DETAILED_SYNC_PP (1 << 6)
72 /* Force reduced-blanking timings for detailed modes */
73 #define EDID_QUIRK_FORCE_REDUCED_BLANKING (1 << 7)
75 struct detailed_mode_closure {
76 struct drm_connector *connector;
88 static struct edid_quirk {
92 } edid_quirk_list[] = {
94 { "ACI", 0x22a2, EDID_QUIRK_FORCE_REDUCED_BLANKING },
97 { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
99 { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
101 { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
103 /* Belinea 10 15 55 */
104 { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
105 { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
107 /* Envision Peripherals, Inc. EN-7100e */
108 { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
109 /* Envision EN2028 */
110 { "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
112 /* Funai Electronics PM36B */
113 { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
114 EDID_QUIRK_DETAILED_IN_CM },
116 /* LG Philips LCD LP154W01-A5 */
117 { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
118 { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
120 /* Philips 107p5 CRT */
121 { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
124 { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
126 /* Samsung SyncMaster 205BW. Note: irony */
127 { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
128 /* Samsung SyncMaster 22[5-6]BW */
129 { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
130 { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
132 /* ViewSonic VA2026w */
133 { "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
136 /*** DDC fetch and block validation ***/
138 static const u8 edid_header[] = {
139 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
143 * Sanity check the header of the base EDID block. Return 8 if the header
144 * is perfect, down to 0 if it's totally wrong.
146 int drm_edid_header_is_valid(const u8 *raw_edid)
150 for (i = 0; i < sizeof(edid_header); i++)
151 if (raw_edid[i] == edid_header[i])
156 EXPORT_SYMBOL(drm_edid_header_is_valid);
158 static int edid_fixup __read_mostly = 6;
161 * Sanity check the EDID block (base or extension). Return 0 if the block
162 * doesn't check out, or 1 if it's valid.
164 bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid)
168 struct edid *edid = (struct edid *)raw_edid;
170 if (edid_fixup > 8 || edid_fixup < 0)
174 int score = drm_edid_header_is_valid(raw_edid);
176 else if (score >= edid_fixup) {
177 DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
178 memcpy(raw_edid, edid_header, sizeof(edid_header));
184 for (i = 0; i < EDID_LENGTH; i++)
187 if (print_bad_edid) {
188 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
191 /* allow CEA to slide through, switches mangle this */
192 if (raw_edid[0] != 0x02)
196 /* per-block-type checks */
197 switch (raw_edid[0]) {
199 if (edid->version != 1) {
200 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
204 if (edid->revision > 4)
205 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
215 if (raw_edid && print_bad_edid) {
216 DRM_DEBUG_KMS("Raw EDID:\n");
217 if ((drm_debug & DRM_DEBUGBITS_KMS) != 0) {
218 for (i = 0; i < EDID_LENGTH; ) {
219 kprintf("%02x", raw_edid[i]);
221 if (i % 16 == 0 || i == EDID_LENGTH)
232 EXPORT_SYMBOL(drm_edid_block_valid);
235 * drm_edid_is_valid - sanity check EDID data
238 * Sanity-check an entire EDID record (including extensions)
240 bool drm_edid_is_valid(struct edid *edid)
243 u8 *raw = (u8 *)edid;
248 for (i = 0; i <= edid->extensions; i++)
249 if (!drm_edid_block_valid(raw + i * EDID_LENGTH, i, true))
254 EXPORT_SYMBOL(drm_edid_is_valid);
256 #define DDC_SEGMENT_ADDR 0x30
258 * Get EDID information via I2C.
260 * \param adapter : i2c device adaptor
261 * \param buf : EDID data buffer to be filled
262 * \param len : EDID data buffer length
263 * \return 0 on success or -1 on failure.
265 * Try to fetch EDID information by calling i2c driver function.
268 drm_do_probe_ddc_edid(device_t adapter, unsigned char *buf,
271 unsigned char start = block * EDID_LENGTH;
272 unsigned char segment = block >> 1;
273 unsigned char xfers = segment ? 3 : 2;
274 int ret, retries = 5;
276 /* The core i2c driver will automatically retry the transfer if the
277 * adapter reports EAGAIN. However, we find that bit-banging transfers
278 * are susceptible to errors under a heavily loaded machine and
279 * generate spurious NAKs and timeouts. Retrying the transfer
280 * of the individual block a few times seems to overcome this.
283 struct i2c_msg msgs[] = {
285 .slave = DDC_SEGMENT_ADDR << 1,
290 .slave = DDC_ADDR << 1,
295 .slave = DDC_ADDR << 1,
303 * Avoid sending the segment addr to not upset non-compliant ddc
306 ret = iicbus_transfer(adapter, &msgs[3 - xfers], xfers);
309 DRM_DEBUG_KMS("iicbus_transfer countdown %d error %d\n",
311 } while (ret != 0 && --retries);
313 return (ret == 0 ? 0 : -1);
316 static bool drm_edid_is_zero(u8 *in_edid, int length)
319 u32 *raw_edid = (u32 *)in_edid;
321 for (i = 0; i < length / 4; i++)
322 if (*(raw_edid + i) != 0)
328 drm_do_get_edid(struct drm_connector *connector, device_t adapter)
330 int i, j = 0, valid_extensions = 0;
332 bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
334 block = kmalloc(EDID_LENGTH, M_DRM, M_WAITOK | M_ZERO);
336 /* base block fetch */
337 for (i = 0; i < 4; i++) {
338 if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
340 if (drm_edid_block_valid(block, 0, print_bad_edid))
342 if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
343 connector->null_edid_counter++;
350 /* if there's no extensions, we're done */
351 if (block[0x7e] == 0)
354 new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, M_DRM,
360 for (j = 1; j <= block[0x7e]; j++) {
361 for (i = 0; i < 4; i++) {
362 if (drm_do_probe_ddc_edid(adapter,
363 block + (valid_extensions + 1) * EDID_LENGTH,
366 if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH, j, print_bad_edid)) {
372 dev_warn(connector->dev->dev,
373 "%s: Ignoring invalid EDID block %d.\n",
374 drm_get_connector_name(connector), j);
377 if (valid_extensions != block[0x7e]) {
378 block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
379 block[0x7e] = valid_extensions;
380 new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH,
390 if (print_bad_edid) {
391 dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
392 drm_get_connector_name(connector), j);
394 connector->bad_edid_counter++;
402 * Probe DDC presence.
404 * \param adapter : i2c device adaptor
405 * \return 1 on success
408 drm_probe_ddc(device_t adapter)
412 return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
414 EXPORT_SYMBOL(drm_probe_ddc);
417 * drm_get_edid - get EDID data, if available
418 * @connector: connector we're probing
419 * @adapter: i2c adapter to use for DDC
421 * Poke the given i2c channel to grab EDID data if possible. If found,
422 * attach it to the connector.
424 * Return edid data or NULL if we couldn't find any.
426 struct edid *drm_get_edid(struct drm_connector *connector,
429 struct edid *edid = NULL;
431 if (drm_probe_ddc(adapter))
432 edid = (struct edid *)drm_do_get_edid(connector, adapter);
436 EXPORT_SYMBOL(drm_get_edid);
438 /*** EDID parsing ***/
441 * edid_vendor - match a string against EDID's obfuscated vendor field
442 * @edid: EDID to match
443 * @vendor: vendor string
445 * Returns true if @vendor is in @edid, false otherwise
447 static bool edid_vendor(struct edid *edid, char *vendor)
451 edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
452 edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
453 ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
454 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
456 return !strncmp(edid_vendor, vendor, 3);
460 * edid_get_quirks - return quirk flags for a given EDID
461 * @edid: EDID to process
463 * This tells subsequent routines what fixes they need to apply.
465 static u32 edid_get_quirks(struct edid *edid)
467 struct edid_quirk *quirk;
470 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
471 quirk = &edid_quirk_list[i];
473 if (edid_vendor(edid, quirk->vendor) &&
474 (EDID_PRODUCT_ID(edid) == quirk->product_id))
475 return quirk->quirks;
481 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
482 #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
485 * edid_fixup_preferred - set preferred modes based on quirk list
486 * @connector: has mode list to fix up
487 * @quirks: quirks list
489 * Walk the mode list for @connector, clearing the preferred status
490 * on existing modes and setting it anew for the right mode ala @quirks.
492 static void edid_fixup_preferred(struct drm_connector *connector,
495 struct drm_display_mode *t, *cur_mode, *preferred_mode;
496 int target_refresh = 0;
498 if (list_empty(&connector->probed_modes))
501 if (quirks & EDID_QUIRK_PREFER_LARGE_60)
503 if (quirks & EDID_QUIRK_PREFER_LARGE_75)
506 preferred_mode = list_first_entry(&connector->probed_modes,
507 struct drm_display_mode, head);
509 list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
510 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
512 if (cur_mode == preferred_mode)
515 /* Largest mode is preferred */
516 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
517 preferred_mode = cur_mode;
519 /* At a given size, try to get closest to target refresh */
520 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
521 MODE_REFRESH_DIFF(cur_mode, target_refresh) <
522 MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
523 preferred_mode = cur_mode;
527 preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
531 mode_is_rb(const struct drm_display_mode *mode)
533 return (mode->htotal - mode->hdisplay == 160) &&
534 (mode->hsync_end - mode->hdisplay == 80) &&
535 (mode->hsync_end - mode->hsync_start == 32) &&
536 (mode->vsync_start - mode->vdisplay == 3);
540 * drm_mode_find_dmt - Create a copy of a mode if present in DMT
541 * @dev: Device to duplicate against
543 * @vsize: Mode height
544 * @fresh: Mode refresh rate
545 * @rb: Mode reduced-blanking-ness
547 * Walk the DMT mode list looking for a match for the given parameters.
548 * Return a newly allocated copy of the mode, or NULL if not found.
550 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
551 int hsize, int vsize, int fresh,
556 for (i = 0; i < drm_num_dmt_modes; i++) {
557 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
558 if (hsize != ptr->hdisplay)
560 if (vsize != ptr->vdisplay)
562 if (fresh != drm_mode_vrefresh(ptr))
564 if (rb != mode_is_rb(ptr))
567 return drm_mode_duplicate(dev, ptr);
572 EXPORT_SYMBOL(drm_mode_find_dmt);
574 typedef void detailed_cb(struct detailed_timing *timing, void *closure);
577 cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
581 u8 *det_base = ext + d;
584 for (i = 0; i < n; i++)
585 cb((struct detailed_timing *)(det_base + 18 * i), closure);
589 vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
591 unsigned int i, n = min((int)ext[0x02], 6);
592 u8 *det_base = ext + 5;
595 return; /* unknown version */
597 for (i = 0; i < n; i++)
598 cb((struct detailed_timing *)(det_base + 18 * i), closure);
602 drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
605 struct edid *edid = (struct edid *)raw_edid;
610 for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
611 cb(&(edid->detailed_timings[i]), closure);
613 for (i = 1; i <= raw_edid[0x7e]; i++) {
614 u8 *ext = raw_edid + (i * EDID_LENGTH);
617 cea_for_each_detailed_block(ext, cb, closure);
620 vtb_for_each_detailed_block(ext, cb, closure);
629 is_rb(struct detailed_timing *t, void *data)
632 if (r[3] == EDID_DETAIL_MONITOR_RANGE)
634 *(bool *)data = true;
637 /* EDID 1.4 defines this explicitly. For EDID 1.3, we guess, badly. */
639 drm_monitor_supports_rb(struct edid *edid)
641 if (edid->revision >= 4) {
643 drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
647 return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
651 find_gtf2(struct detailed_timing *t, void *data)
654 if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
658 /* Secondary GTF curve kicks in above some break frequency */
660 drm_gtf2_hbreak(struct edid *edid)
663 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
664 return r ? (r[12] * 2) : 0;
668 drm_gtf2_2c(struct edid *edid)
671 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
672 return r ? r[13] : 0;
676 drm_gtf2_m(struct edid *edid)
679 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
680 return r ? (r[15] << 8) + r[14] : 0;
684 drm_gtf2_k(struct edid *edid)
687 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
688 return r ? r[16] : 0;
692 drm_gtf2_2j(struct edid *edid)
695 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
696 return r ? r[17] : 0;
700 * standard_timing_level - get std. timing level(CVT/GTF/DMT)
701 * @edid: EDID block to scan
703 static int standard_timing_level(struct edid *edid)
705 if (edid->revision >= 2) {
706 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
708 if (drm_gtf2_hbreak(edid))
716 * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
717 * monitors fill with ascii space (0x20) instead.
720 bad_std_timing(u8 a, u8 b)
722 return (a == 0x00 && b == 0x00) ||
723 (a == 0x01 && b == 0x01) ||
724 (a == 0x20 && b == 0x20);
728 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
729 * @t: standard timing params
730 * @timing_level: standard timing level
732 * Take the standard timing params (in this case width, aspect, and refresh)
733 * and convert them into a real mode using CVT/GTF/DMT.
735 static struct drm_display_mode *
736 drm_mode_std(struct drm_connector *connector, struct edid *edid,
737 struct std_timing *t, int revision)
739 struct drm_device *dev = connector->dev;
740 struct drm_display_mode *m, *mode = NULL;
743 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
744 >> EDID_TIMING_ASPECT_SHIFT;
745 unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
746 >> EDID_TIMING_VFREQ_SHIFT;
747 int timing_level = standard_timing_level(edid);
749 if (bad_std_timing(t->hsize, t->vfreq_aspect))
752 /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
753 hsize = t->hsize * 8 + 248;
754 /* vrefresh_rate = vfreq + 60 */
755 vrefresh_rate = vfreq + 60;
756 /* the vdisplay is calculated based on the aspect ratio */
757 if (aspect_ratio == 0) {
761 vsize = (hsize * 10) / 16;
762 } else if (aspect_ratio == 1)
763 vsize = (hsize * 3) / 4;
764 else if (aspect_ratio == 2)
765 vsize = (hsize * 4) / 5;
767 vsize = (hsize * 9) / 16;
769 /* HDTV hack, part 1 */
770 if (vrefresh_rate == 60 &&
771 ((hsize == 1360 && vsize == 765) ||
772 (hsize == 1368 && vsize == 769))) {
778 * If this connector already has a mode for this size and refresh
779 * rate (because it came from detailed or CVT info), use that
780 * instead. This way we don't have to guess at interlace or
783 list_for_each_entry(m, &connector->probed_modes, head)
784 if (m->hdisplay == hsize && m->vdisplay == vsize &&
785 drm_mode_vrefresh(m) == vrefresh_rate)
788 /* HDTV hack, part 2 */
789 if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
790 mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
792 mode->hdisplay = 1366;
793 mode->hsync_start = mode->hsync_start - 1;
794 mode->hsync_end = mode->hsync_end - 1;
798 /* check whether it can be found in default mode table */
799 if (drm_monitor_supports_rb(edid)) {
800 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
805 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
809 /* okay, generate it */
810 switch (timing_level) {
814 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
818 * This is potentially wrong if there's ever a monitor with
819 * more than one ranges section, each claiming a different
820 * secondary GTF curve. Please don't do that.
822 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
825 if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
826 drm_mode_destroy(dev, mode);
827 mode = drm_gtf_mode_complex(dev, hsize, vsize,
836 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
844 * EDID is delightfully ambiguous about how interlaced modes are to be
845 * encoded. Our internal representation is of frame height, but some
846 * HDTV detailed timings are encoded as field height.
848 * The format list here is from CEA, in frame size. Technically we
849 * should be checking refresh rate too. Whatever.
852 drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
853 struct detailed_pixel_timing *pt)
856 static const struct {
858 } cea_interlaced[] = {
868 if (!(pt->misc & DRM_EDID_PT_INTERLACED))
871 for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
872 if ((mode->hdisplay == cea_interlaced[i].w) &&
873 (mode->vdisplay == cea_interlaced[i].h / 2)) {
875 mode->vsync_start *= 2;
876 mode->vsync_end *= 2;
882 mode->flags |= DRM_MODE_FLAG_INTERLACE;
886 * drm_mode_detailed - create a new mode from an EDID detailed timing section
887 * @dev: DRM device (needed to create new mode)
889 * @timing: EDID detailed timing info
890 * @quirks: quirks to apply
892 * An EDID detailed timing block contains enough info for us to create and
893 * return a new struct drm_display_mode.
895 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
897 struct detailed_timing *timing,
900 struct drm_display_mode *mode;
901 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
902 unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
903 unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
904 unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
905 unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
906 unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
907 unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
908 unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
909 unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
911 /* ignore tiny modes */
912 if (hactive < 64 || vactive < 64)
915 if (pt->misc & DRM_EDID_PT_STEREO) {
916 kprintf("stereo mode not supported\n");
919 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
920 kprintf("composite sync not supported\n");
923 /* it is incorrect if hsync/vsync width is zero */
924 if (!hsync_pulse_width || !vsync_pulse_width) {
925 DRM_DEBUG_KMS("Incorrect Detailed timing. "
926 "Wrong Hsync/Vsync pulse width\n");
930 if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
931 mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
938 mode = drm_mode_create(dev);
942 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
943 timing->pixel_clock = cpu_to_le16(1088);
945 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
947 mode->hdisplay = hactive;
948 mode->hsync_start = mode->hdisplay + hsync_offset;
949 mode->hsync_end = mode->hsync_start + hsync_pulse_width;
950 mode->htotal = mode->hdisplay + hblank;
952 mode->vdisplay = vactive;
953 mode->vsync_start = mode->vdisplay + vsync_offset;
954 mode->vsync_end = mode->vsync_start + vsync_pulse_width;
955 mode->vtotal = mode->vdisplay + vblank;
957 /* Some EDIDs have bogus h/vtotal values */
958 if (mode->hsync_end > mode->htotal)
959 mode->htotal = mode->hsync_end + 1;
960 if (mode->vsync_end > mode->vtotal)
961 mode->vtotal = mode->vsync_end + 1;
963 drm_mode_do_interlace_quirk(mode, pt);
965 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
966 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
969 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
970 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
971 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
972 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
975 mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
976 mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
978 if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
979 mode->width_mm *= 10;
980 mode->height_mm *= 10;
983 if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
984 mode->width_mm = edid->width_cm * 10;
985 mode->height_mm = edid->height_cm * 10;
988 mode->type = DRM_MODE_TYPE_DRIVER;
989 drm_mode_set_name(mode);
995 mode_in_hsync_range(const struct drm_display_mode *mode,
996 struct edid *edid, u8 *t)
998 int hsync, hmin, hmax;
1001 if (edid->revision >= 4)
1002 hmin += ((t[4] & 0x04) ? 255 : 0);
1004 if (edid->revision >= 4)
1005 hmax += ((t[4] & 0x08) ? 255 : 0);
1006 hsync = drm_mode_hsync(mode);
1008 return (hsync <= hmax && hsync >= hmin);
1012 mode_in_vsync_range(const struct drm_display_mode *mode,
1013 struct edid *edid, u8 *t)
1015 int vsync, vmin, vmax;
1018 if (edid->revision >= 4)
1019 vmin += ((t[4] & 0x01) ? 255 : 0);
1021 if (edid->revision >= 4)
1022 vmax += ((t[4] & 0x02) ? 255 : 0);
1023 vsync = drm_mode_vrefresh(mode);
1025 return (vsync <= vmax && vsync >= vmin);
1029 range_pixel_clock(struct edid *edid, u8 *t)
1032 if (t[9] == 0 || t[9] == 255)
1035 /* 1.4 with CVT support gives us real precision, yay */
1036 if (edid->revision >= 4 && t[10] == 0x04)
1037 return (t[9] * 10000) - ((t[12] >> 2) * 250);
1039 /* 1.3 is pathetic, so fuzz up a bit */
1040 return t[9] * 10000 + 5001;
1044 mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
1045 struct detailed_timing *timing)
1048 u8 *t = (u8 *)timing;
1050 if (!mode_in_hsync_range(mode, edid, t))
1053 if (!mode_in_vsync_range(mode, edid, t))
1056 if ((max_clock = range_pixel_clock(edid, t)))
1057 if (mode->clock > max_clock)
1060 /* 1.4 max horizontal check */
1061 if (edid->revision >= 4 && t[10] == 0x04)
1062 if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
1065 if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
1071 static bool valid_inferred_mode(const struct drm_connector *connector,
1072 const struct drm_display_mode *mode)
1074 struct drm_display_mode *m;
1077 list_for_each_entry(m, &connector->probed_modes, head) {
1078 if (mode->hdisplay == m->hdisplay &&
1079 mode->vdisplay == m->vdisplay &&
1080 drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
1081 return false; /* duplicated */
1082 if (mode->hdisplay <= m->hdisplay &&
1083 mode->vdisplay <= m->vdisplay)
1090 drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
1091 struct detailed_timing *timing)
1094 struct drm_display_mode *newmode;
1095 struct drm_device *dev = connector->dev;
1097 for (i = 0; i < drm_num_dmt_modes; i++) {
1098 if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
1099 valid_inferred_mode(connector, drm_dmt_modes + i)) {
1100 newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
1102 drm_mode_probed_add(connector, newmode);
1111 /* fix up 1366x768 mode from 1368x768;
1112 * GFT/CVT can't express 1366 width which isn't dividable by 8
1114 static void fixup_mode_1366x768(struct drm_display_mode *mode)
1116 if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
1117 mode->hdisplay = 1366;
1118 mode->hsync_start--;
1120 drm_mode_set_name(mode);
1125 drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
1126 struct detailed_timing *timing)
1129 struct drm_display_mode *newmode;
1130 struct drm_device *dev = connector->dev;
1132 for (i = 0; i < num_extra_modes; i++) {
1133 const struct minimode *m = &extra_modes[i];
1134 newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
1138 fixup_mode_1366x768(newmode);
1139 if (!mode_in_range(newmode, edid, timing) ||
1140 !valid_inferred_mode(connector, newmode)) {
1141 drm_mode_destroy(dev, newmode);
1145 drm_mode_probed_add(connector, newmode);
1153 drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
1154 struct detailed_timing *timing)
1157 struct drm_display_mode *newmode;
1158 struct drm_device *dev = connector->dev;
1159 bool rb = drm_monitor_supports_rb(edid);
1161 for (i = 0; i < num_extra_modes; i++) {
1162 const struct minimode *m = &extra_modes[i];
1163 newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
1167 fixup_mode_1366x768(newmode);
1168 if (!mode_in_range(newmode, edid, timing) ||
1169 !valid_inferred_mode(connector, newmode)) {
1170 drm_mode_destroy(dev, newmode);
1174 drm_mode_probed_add(connector, newmode);
1182 do_inferred_modes(struct detailed_timing *timing, void *c)
1184 struct detailed_mode_closure *closure = c;
1185 struct detailed_non_pixel *data = &timing->data.other_data;
1186 struct detailed_data_monitor_range *range = &data->data.range;
1188 if (data->type != EDID_DETAIL_MONITOR_RANGE)
1191 closure->modes += drm_dmt_modes_for_range(closure->connector,
1195 if (!version_greater(closure->edid, 1, 1))
1196 return; /* GTF not defined yet */
1198 switch (range->flags) {
1199 case 0x02: /* secondary gtf, XXX could do more */
1200 case 0x00: /* default gtf */
1201 closure->modes += drm_gtf_modes_for_range(closure->connector,
1205 case 0x04: /* cvt, only in 1.4+ */
1206 if (!version_greater(closure->edid, 1, 3))
1209 closure->modes += drm_cvt_modes_for_range(closure->connector,
1213 case 0x01: /* just the ranges, no formula */
1220 add_inferred_modes(struct drm_connector *connector, struct edid *edid)
1222 struct detailed_mode_closure closure = {
1223 connector, edid, 0, 0, 0
1226 if (version_greater(edid, 1, 0))
1227 drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
1230 return closure.modes;
1234 drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
1236 int i, j, m, modes = 0;
1237 struct drm_display_mode *mode;
1238 u8 *est = ((u8 *)timing) + 5;
1240 for (i = 0; i < 6; i++) {
1241 for (j = 7; j > 0; j--) {
1242 m = (i * 8) + (7 - j);
1243 if (m >= ARRAY_SIZE(est3_modes))
1245 if (est[i] & (1 << j)) {
1246 mode = drm_mode_find_dmt(connector->dev,
1252 drm_mode_probed_add(connector, mode);
1263 do_established_modes(struct detailed_timing *timing, void *c)
1265 struct detailed_mode_closure *closure = c;
1266 struct detailed_non_pixel *data = &timing->data.other_data;
1268 if (data->type == EDID_DETAIL_EST_TIMINGS)
1269 closure->modes += drm_est3_modes(closure->connector, timing);
1273 * add_established_modes - get est. modes from EDID and add them
1274 * @edid: EDID block to scan
1276 * Each EDID block contains a bitmap of the supported "established modes" list
1277 * (defined above). Tease them out and add them to the global modes list.
1280 add_established_modes(struct drm_connector *connector, struct edid *edid)
1282 struct drm_device *dev = connector->dev;
1283 unsigned long est_bits = edid->established_timings.t1 |
1284 (edid->established_timings.t2 << 8) |
1285 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
1287 struct detailed_mode_closure closure = {
1288 connector, edid, 0, 0, 0
1291 for (i = 0; i <= EDID_EST_TIMINGS; i++) {
1292 if (est_bits & (1<<i)) {
1293 struct drm_display_mode *newmode;
1294 newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
1296 drm_mode_probed_add(connector, newmode);
1302 if (version_greater(edid, 1, 0))
1303 drm_for_each_detailed_block((u8 *)edid,
1304 do_established_modes, &closure);
1306 return modes + closure.modes;
1310 do_standard_modes(struct detailed_timing *timing, void *c)
1312 struct detailed_mode_closure *closure = c;
1313 struct detailed_non_pixel *data = &timing->data.other_data;
1314 struct drm_connector *connector = closure->connector;
1315 struct edid *edid = closure->edid;
1317 if (data->type == EDID_DETAIL_STD_MODES) {
1319 for (i = 0; i < 6; i++) {
1320 struct std_timing *std;
1321 struct drm_display_mode *newmode;
1323 std = &data->data.timings[i];
1324 newmode = drm_mode_std(connector, edid, std,
1327 drm_mode_probed_add(connector, newmode);
1335 * add_standard_modes - get std. modes from EDID and add them
1336 * @edid: EDID block to scan
1338 * Standard modes can be calculated using the appropriate standard (DMT,
1339 * GTF or CVT. Grab them from @edid and add them to the list.
1342 add_standard_modes(struct drm_connector *connector, struct edid *edid)
1345 struct detailed_mode_closure closure = {
1346 connector, edid, 0, 0, 0
1349 for (i = 0; i < EDID_STD_TIMINGS; i++) {
1350 struct drm_display_mode *newmode;
1352 newmode = drm_mode_std(connector, edid,
1353 &edid->standard_timings[i],
1356 drm_mode_probed_add(connector, newmode);
1361 if (version_greater(edid, 1, 0))
1362 drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
1365 /* XXX should also look for standard codes in VTB blocks */
1367 return modes + closure.modes;
1370 static int drm_cvt_modes(struct drm_connector *connector,
1371 struct detailed_timing *timing)
1373 int i, j, modes = 0;
1374 struct drm_display_mode *newmode;
1375 struct drm_device *dev = connector->dev;
1376 struct cvt_timing *cvt;
1377 const int rates[] = { 60, 85, 75, 60, 50 };
1378 const u8 empty[3] = { 0, 0, 0 };
1380 for (i = 0; i < 4; i++) {
1381 int width = 0, height;
1382 cvt = &(timing->data.other_data.data.cvt[i]);
1384 if (!memcmp(cvt->code, empty, 3))
1387 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
1388 switch (cvt->code[1] & 0x0c) {
1390 width = height * 4 / 3;
1393 width = height * 16 / 9;
1396 width = height * 16 / 10;
1399 width = height * 15 / 9;
1403 for (j = 1; j < 5; j++) {
1404 if (cvt->code[2] & (1 << j)) {
1405 newmode = drm_cvt_mode(dev, width, height,
1409 drm_mode_probed_add(connector, newmode);
1420 do_cvt_mode(struct detailed_timing *timing, void *c)
1422 struct detailed_mode_closure *closure = c;
1423 struct detailed_non_pixel *data = &timing->data.other_data;
1425 if (data->type == EDID_DETAIL_CVT_3BYTE)
1426 closure->modes += drm_cvt_modes(closure->connector, timing);
1430 add_cvt_modes(struct drm_connector *connector, struct edid *edid)
1432 struct detailed_mode_closure closure = {
1433 connector, edid, 0, 0, 0
1436 if (version_greater(edid, 1, 2))
1437 drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
1439 /* XXX should also look for CVT codes in VTB blocks */
1441 return closure.modes;
1445 do_detailed_mode(struct detailed_timing *timing, void *c)
1447 struct detailed_mode_closure *closure = c;
1448 struct drm_display_mode *newmode;
1450 if (timing->pixel_clock) {
1451 newmode = drm_mode_detailed(closure->connector->dev,
1452 closure->edid, timing,
1457 if (closure->preferred)
1458 newmode->type |= DRM_MODE_TYPE_PREFERRED;
1460 drm_mode_probed_add(closure->connector, newmode);
1462 closure->preferred = 0;
1467 * add_detailed_modes - Add modes from detailed timings
1468 * @connector: attached connector
1469 * @edid: EDID block to scan
1470 * @quirks: quirks to apply
1473 add_detailed_modes(struct drm_connector *connector, struct edid *edid,
1476 struct detailed_mode_closure closure = {
1484 if (closure.preferred && !version_greater(edid, 1, 3))
1486 (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
1488 drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
1490 return closure.modes;
1493 #define HDMI_IDENTIFIER 0x000C03
1494 #define AUDIO_BLOCK 0x01
1495 #define VIDEO_BLOCK 0x02
1496 #define VENDOR_BLOCK 0x03
1497 #define SPEAKER_BLOCK 0x04
1498 #define VIDEO_CAPABILITY_BLOCK 0x07
1499 #define EDID_BASIC_AUDIO (1 << 6)
1500 #define EDID_CEA_YCRCB444 (1 << 5)
1501 #define EDID_CEA_YCRCB422 (1 << 4)
1502 #define EDID_CEA_VCDB_QS (1 << 6)
1505 * Search EDID for CEA extension block.
1507 u8 *drm_find_cea_extension(struct edid *edid)
1509 u8 *edid_ext = NULL;
1512 /* No EDID or EDID extensions */
1513 if (edid == NULL || edid->extensions == 0)
1516 /* Find CEA extension */
1517 for (i = 0; i < edid->extensions; i++) {
1518 edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
1519 if (edid_ext[0] == CEA_EXT)
1523 if (i == edid->extensions)
1528 EXPORT_SYMBOL(drm_find_cea_extension);
1531 * Looks for a CEA mode matching given drm_display_mode.
1532 * Returns its CEA Video ID code, or 0 if not found.
1534 u8 drm_match_cea_mode(const struct drm_display_mode *to_match)
1538 for (mode = 0; mode < drm_num_cea_modes; mode++) {
1539 const struct drm_display_mode *cea_mode = &edid_cea_modes[mode];
1541 if (drm_mode_equal(to_match, cea_mode))
1546 EXPORT_SYMBOL(drm_match_cea_mode);
1550 do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
1552 struct drm_device *dev = connector->dev;
1553 u8 * mode, cea_mode;
1556 for (mode = db; mode < db + len; mode++) {
1557 cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
1558 if (cea_mode < drm_num_cea_modes) {
1559 struct drm_display_mode *newmode;
1560 newmode = drm_mode_duplicate(dev,
1561 &edid_cea_modes[cea_mode]);
1563 drm_mode_probed_add(connector, newmode);
1573 cea_db_payload_len(const u8 *db)
1575 return db[0] & 0x1f;
1579 cea_db_tag(const u8 *db)
1585 cea_revision(const u8 *cea)
1591 cea_db_offsets(const u8 *cea, int *start, int *end)
1593 /* Data block offset in CEA extension block */
1598 if (*end < 4 || *end > 127)
1603 #define for_each_cea_db(cea, i, start, end) \
1604 for ((i) = (start); (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
1607 add_cea_modes(struct drm_connector *connector, struct edid *edid)
1609 u8 * cea = drm_find_cea_extension(edid);
1613 if (cea && cea_revision(cea) >= 3) {
1616 if (cea_db_offsets(cea, &start, &end))
1619 for_each_cea_db(cea, i, start, end) {
1621 dbl = cea_db_payload_len(db);
1623 if (cea_db_tag(db) == VIDEO_BLOCK)
1624 modes += do_cea_modes (connector, db+1, dbl);
1632 parse_hdmi_vsdb(struct drm_connector *connector, const u8 *db)
1634 u8 len = cea_db_payload_len(db);
1637 connector->eld[5] |= (db[6] >> 7) << 1; /* Supports_AI */
1638 connector->dvi_dual = db[6] & 1;
1641 connector->max_tmds_clock = db[7] * 5;
1643 connector->latency_present[0] = db[8] >> 7;
1644 connector->latency_present[1] = (db[8] >> 6) & 1;
1647 connector->video_latency[0] = db[9];
1649 connector->audio_latency[0] = db[10];
1651 connector->video_latency[1] = db[11];
1653 connector->audio_latency[1] = db[12];
1655 DRM_DEBUG_KMS("HDMI: DVI dual %d, "
1656 "max TMDS clock %d, "
1657 "latency present %d %d, "
1658 "video latency %d %d, "
1659 "audio latency %d %d\n",
1660 connector->dvi_dual,
1661 connector->max_tmds_clock,
1662 (int) connector->latency_present[0],
1663 (int) connector->latency_present[1],
1664 connector->video_latency[0],
1665 connector->video_latency[1],
1666 connector->audio_latency[0],
1667 connector->audio_latency[1]);
1671 monitor_name(struct detailed_timing *t, void *data)
1673 if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
1674 *(u8 **)data = t->data.other_data.data.str.str;
1677 static bool cea_db_is_hdmi_vsdb(const u8 *db)
1681 if (cea_db_tag(db) != VENDOR_BLOCK)
1684 if (cea_db_payload_len(db) < 5)
1687 hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
1689 return hdmi_id == HDMI_IDENTIFIER;
1693 * drm_edid_to_eld - build ELD from EDID
1694 * @connector: connector corresponding to the HDMI/DP sink
1695 * @edid: EDID to parse
1697 * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
1698 * Some ELD fields are left to the graphics driver caller:
1703 void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
1705 uint8_t *eld = connector->eld;
1713 memset(eld, 0, sizeof(connector->eld));
1715 cea = drm_find_cea_extension(edid);
1717 DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
1722 drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
1723 for (mnl = 0; name && mnl < 13; mnl++) {
1724 if (name[mnl] == 0x0a)
1726 eld[20 + mnl] = name[mnl];
1728 eld[4] = (cea[1] << 5) | mnl;
1729 DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
1731 eld[0] = 2 << 3; /* ELD version: 2 */
1733 eld[16] = edid->mfg_id[0];
1734 eld[17] = edid->mfg_id[1];
1735 eld[18] = edid->prod_code[0];
1736 eld[19] = edid->prod_code[1];
1738 if (cea_revision(cea) >= 3) {
1741 if (cea_db_offsets(cea, &start, &end)) {
1746 for_each_cea_db(cea, i, start, end) {
1748 dbl = cea_db_payload_len(db);
1750 switch (cea_db_tag(db)) {
1752 /* Audio Data Block, contains SADs */
1753 sad_count = dbl / 3;
1755 memcpy(eld + 20 + mnl, &db[1], dbl);
1758 /* Speaker Allocation Data Block */
1763 /* HDMI Vendor-Specific Data Block */
1764 if (cea_db_is_hdmi_vsdb(db))
1765 parse_hdmi_vsdb(connector, db);
1772 eld[5] |= sad_count << 4;
1773 eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
1775 DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
1777 EXPORT_SYMBOL(drm_edid_to_eld);
1780 * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
1781 * @connector: connector associated with the HDMI/DP sink
1782 * @mode: the display mode
1784 int drm_av_sync_delay(struct drm_connector *connector,
1785 struct drm_display_mode *mode)
1787 int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1790 if (!connector->latency_present[0])
1792 if (!connector->latency_present[1])
1795 a = connector->audio_latency[i];
1796 v = connector->video_latency[i];
1799 * HDMI/DP sink doesn't support audio or video?
1801 if (a == 255 || v == 255)
1805 * Convert raw EDID values to millisecond.
1806 * Treat unknown latency as 0ms.
1809 a = min(2 * (a - 1), 500);
1811 v = min(2 * (v - 1), 500);
1813 return max(v - a, 0);
1815 EXPORT_SYMBOL(drm_av_sync_delay);
1818 * drm_select_eld - select one ELD from multiple HDMI/DP sinks
1819 * @encoder: the encoder just changed display mode
1820 * @mode: the adjusted display mode
1822 * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
1823 * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
1825 struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
1826 struct drm_display_mode *mode)
1828 struct drm_connector *connector;
1829 struct drm_device *dev = encoder->dev;
1831 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1832 if (connector->encoder == encoder && connector->eld[0])
1837 EXPORT_SYMBOL(drm_select_eld);
1840 * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
1841 * @edid: monitor EDID information
1843 * Parse the CEA extension according to CEA-861-B.
1844 * Return true if HDMI, false if not or unknown.
1846 bool drm_detect_hdmi_monitor(struct edid *edid)
1850 int start_offset, end_offset;
1852 edid_ext = drm_find_cea_extension(edid);
1856 if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
1860 * Because HDMI identifier is in Vendor Specific Block,
1861 * search it from all data blocks of CEA extension.
1863 for_each_cea_db(edid_ext, i, start_offset, end_offset) {
1864 if (cea_db_is_hdmi_vsdb(&edid_ext[i]))
1870 EXPORT_SYMBOL(drm_detect_hdmi_monitor);
1873 * drm_detect_monitor_audio - check monitor audio capability
1875 * Monitor should have CEA extension block.
1876 * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
1877 * audio' only. If there is any audio extension block and supported
1878 * audio format, assume at least 'basic audio' support, even if 'basic
1879 * audio' is not defined in EDID.
1882 bool drm_detect_monitor_audio(struct edid *edid)
1886 bool has_audio = false;
1887 int start_offset, end_offset;
1889 edid_ext = drm_find_cea_extension(edid);
1893 has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
1896 DRM_DEBUG_KMS("Monitor has basic audio support\n");
1900 if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
1903 for_each_cea_db(edid_ext, i, start_offset, end_offset) {
1904 if (cea_db_tag(&edid_ext[i]) == AUDIO_BLOCK) {
1906 for (j = 1; j < cea_db_payload_len(&edid_ext[i]) + 1; j += 3)
1907 DRM_DEBUG_KMS("CEA audio format %d\n",
1908 (edid_ext[i + j] >> 3) & 0xf);
1915 EXPORT_SYMBOL(drm_detect_monitor_audio);
1918 * drm_rgb_quant_range_selectable - is RGB quantization range selectable?
1920 * Check whether the monitor reports the RGB quantization range selection
1921 * as supported. The AVI infoframe can then be used to inform the monitor
1922 * which quantization range (full or limited) is used.
1924 bool drm_rgb_quant_range_selectable(struct edid *edid)
1929 edid_ext = drm_find_cea_extension(edid);
1933 if (cea_db_offsets(edid_ext, &start, &end))
1936 for_each_cea_db(edid_ext, i, start, end) {
1937 if (cea_db_tag(&edid_ext[i]) == VIDEO_CAPABILITY_BLOCK &&
1938 cea_db_payload_len(&edid_ext[i]) == 2) {
1939 DRM_DEBUG_KMS("CEA VCDB 0x%02x\n", edid_ext[i + 2]);
1940 return edid_ext[i + 2] & EDID_CEA_VCDB_QS;
1946 EXPORT_SYMBOL(drm_rgb_quant_range_selectable);
1949 * drm_add_display_info - pull display info out if present
1951 * @info: display info (attached to connector)
1953 * Grab any available display info and stuff it into the drm_display_info
1954 * structure that's part of the connector. Useful for tracking bpp and
1957 static void drm_add_display_info(struct edid *edid,
1958 struct drm_display_info *info)
1962 info->width_mm = edid->width_cm * 10;
1963 info->height_mm = edid->height_cm * 10;
1965 /* driver figures it out in this case */
1967 info->color_formats = 0;
1969 if (edid->revision < 3)
1972 if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
1975 /* Get data from CEA blocks if present */
1976 edid_ext = drm_find_cea_extension(edid);
1978 info->cea_rev = edid_ext[1];
1980 /* The existence of a CEA block should imply RGB support */
1981 info->color_formats = DRM_COLOR_FORMAT_RGB444;
1982 if (edid_ext[3] & EDID_CEA_YCRCB444)
1983 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
1984 if (edid_ext[3] & EDID_CEA_YCRCB422)
1985 info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
1988 /* Only defined for 1.4 with digital displays */
1989 if (edid->revision < 4)
1992 switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
1993 case DRM_EDID_DIGITAL_DEPTH_6:
1996 case DRM_EDID_DIGITAL_DEPTH_8:
1999 case DRM_EDID_DIGITAL_DEPTH_10:
2002 case DRM_EDID_DIGITAL_DEPTH_12:
2005 case DRM_EDID_DIGITAL_DEPTH_14:
2008 case DRM_EDID_DIGITAL_DEPTH_16:
2011 case DRM_EDID_DIGITAL_DEPTH_UNDEF:
2017 info->color_formats |= DRM_COLOR_FORMAT_RGB444;
2018 if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
2019 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
2020 if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
2021 info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
2025 * drm_add_edid_modes - add modes from EDID data, if available
2026 * @connector: connector we're probing
2029 * Add the specified modes to the connector's mode list.
2031 * Return number of modes added or 0 if we couldn't find any.
2033 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
2041 if (!drm_edid_is_valid(edid)) {
2042 dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
2043 drm_get_connector_name(connector));
2047 quirks = edid_get_quirks(edid);
2050 * EDID spec says modes should be preferred in this order:
2051 * - preferred detailed mode
2052 * - other detailed modes from base block
2053 * - detailed modes from extension blocks
2054 * - CVT 3-byte code modes
2055 * - standard timing codes
2056 * - established timing codes
2057 * - modes inferred from GTF or CVT range information
2059 * We get this pretty much right.
2061 * XXX order for additional mode types in extension blocks?
2063 num_modes += add_detailed_modes(connector, edid, quirks);
2064 num_modes += add_cvt_modes(connector, edid);
2065 num_modes += add_standard_modes(connector, edid);
2066 num_modes += add_established_modes(connector, edid);
2067 num_modes += add_inferred_modes(connector, edid);
2068 num_modes += add_cea_modes(connector, edid);
2070 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
2071 edid_fixup_preferred(connector, quirks);
2073 drm_add_display_info(edid, &connector->display_info);
2077 EXPORT_SYMBOL(drm_add_edid_modes);
2080 * drm_add_modes_noedid - add modes for the connectors without EDID
2081 * @connector: connector we're probing
2082 * @hdisplay: the horizontal display limit
2083 * @vdisplay: the vertical display limit
2085 * Add the specified modes to the connector's mode list. Only when the
2086 * hdisplay/vdisplay is not beyond the given limit, it will be added.
2088 * Return number of modes added or 0 if we couldn't find any.
2090 int drm_add_modes_noedid(struct drm_connector *connector,
2091 int hdisplay, int vdisplay)
2093 int i, count, num_modes = 0;
2094 struct drm_display_mode *mode;
2095 struct drm_device *dev = connector->dev;
2097 count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
2103 for (i = 0; i < count; i++) {
2104 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
2105 if (hdisplay && vdisplay) {
2107 * Only when two are valid, they will be used to check
2108 * whether the mode should be added to the mode list of
2111 if (ptr->hdisplay > hdisplay ||
2112 ptr->vdisplay > vdisplay)
2115 if (drm_mode_vrefresh(ptr) > 61)
2117 mode = drm_mode_duplicate(dev, ptr);
2119 drm_mode_probed_add(connector, mode);
2125 EXPORT_SYMBOL(drm_add_modes_noedid);