Home | History | Annotate | Line # | Download | only in drm
drm_edid.c revision 1.1.1.1.2.11
      1  1.1.1.1.2.2  riastrad /*
      2  1.1.1.1.2.2  riastrad  * Copyright (c) 2006 Luc Verhaegen (quirks list)
      3  1.1.1.1.2.2  riastrad  * Copyright (c) 2007-2008 Intel Corporation
      4  1.1.1.1.2.2  riastrad  *   Jesse Barnes <jesse.barnes (at) intel.com>
      5  1.1.1.1.2.2  riastrad  * Copyright 2010 Red Hat, Inc.
      6  1.1.1.1.2.2  riastrad  *
      7  1.1.1.1.2.2  riastrad  * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
      8  1.1.1.1.2.2  riastrad  * FB layer.
      9  1.1.1.1.2.2  riastrad  *   Copyright (C) 2006 Dennis Munsie <dmunsie (at) cecropia.com>
     10  1.1.1.1.2.2  riastrad  *
     11  1.1.1.1.2.2  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     12  1.1.1.1.2.2  riastrad  * copy of this software and associated documentation files (the "Software"),
     13  1.1.1.1.2.2  riastrad  * to deal in the Software without restriction, including without limitation
     14  1.1.1.1.2.2  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sub license,
     15  1.1.1.1.2.2  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     16  1.1.1.1.2.2  riastrad  * Software is furnished to do so, subject to the following conditions:
     17  1.1.1.1.2.2  riastrad  *
     18  1.1.1.1.2.2  riastrad  * The above copyright notice and this permission notice (including the
     19  1.1.1.1.2.2  riastrad  * next paragraph) shall be included in all copies or substantial portions
     20  1.1.1.1.2.2  riastrad  * of the Software.
     21  1.1.1.1.2.2  riastrad  *
     22  1.1.1.1.2.2  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23  1.1.1.1.2.2  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  1.1.1.1.2.2  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     25  1.1.1.1.2.2  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26  1.1.1.1.2.2  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27  1.1.1.1.2.2  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     28  1.1.1.1.2.2  riastrad  * DEALINGS IN THE SOFTWARE.
     29  1.1.1.1.2.2  riastrad  */
     30  1.1.1.1.2.2  riastrad #include <linux/kernel.h>
     31  1.1.1.1.2.2  riastrad #include <linux/slab.h>
     32  1.1.1.1.2.2  riastrad #include <linux/i2c.h>
     33  1.1.1.1.2.2  riastrad #include <linux/module.h>
     34  1.1.1.1.2.3  riastrad #include <linux/moduleparam.h>
     35  1.1.1.1.2.3  riastrad #include <linux/export.h>
     36  1.1.1.1.2.3  riastrad #include <linux/printk.h>
     37  1.1.1.1.2.6  riastrad #include <linux/device.h>
     38  1.1.1.1.2.9  riastrad #include <linux/string.h>
     39  1.1.1.1.2.3  riastrad #include <asm/byteorder.h>
     40  1.1.1.1.2.2  riastrad #include <drm/drmP.h>
     41  1.1.1.1.2.2  riastrad #include <drm/drm_edid.h>
     42  1.1.1.1.2.2  riastrad #include "drm_edid_modes.h"
     43  1.1.1.1.2.2  riastrad 
     44  1.1.1.1.2.2  riastrad #define version_greater(edid, maj, min) \
     45  1.1.1.1.2.2  riastrad 	(((edid)->version > (maj)) || \
     46  1.1.1.1.2.2  riastrad 	 ((edid)->version == (maj) && (edid)->revision > (min)))
     47  1.1.1.1.2.2  riastrad 
     48  1.1.1.1.2.2  riastrad #define EDID_EST_TIMINGS 16
     49  1.1.1.1.2.2  riastrad #define EDID_STD_TIMINGS 8
     50  1.1.1.1.2.2  riastrad #define EDID_DETAILED_TIMINGS 4
     51  1.1.1.1.2.2  riastrad 
     52  1.1.1.1.2.2  riastrad /*
     53  1.1.1.1.2.2  riastrad  * EDID blocks out in the wild have a variety of bugs, try to collect
     54  1.1.1.1.2.2  riastrad  * them here (note that userspace may work around broken monitors first,
     55  1.1.1.1.2.2  riastrad  * but fixes should make their way here so that the kernel "just works"
     56  1.1.1.1.2.2  riastrad  * on as many displays as possible).
     57  1.1.1.1.2.2  riastrad  */
     58  1.1.1.1.2.2  riastrad 
     59  1.1.1.1.2.2  riastrad /* First detailed mode wrong, use largest 60Hz mode */
     60  1.1.1.1.2.2  riastrad #define EDID_QUIRK_PREFER_LARGE_60		(1 << 0)
     61  1.1.1.1.2.2  riastrad /* Reported 135MHz pixel clock is too high, needs adjustment */
     62  1.1.1.1.2.2  riastrad #define EDID_QUIRK_135_CLOCK_TOO_HIGH		(1 << 1)
     63  1.1.1.1.2.2  riastrad /* Prefer the largest mode at 75 Hz */
     64  1.1.1.1.2.2  riastrad #define EDID_QUIRK_PREFER_LARGE_75		(1 << 2)
     65  1.1.1.1.2.2  riastrad /* Detail timing is in cm not mm */
     66  1.1.1.1.2.2  riastrad #define EDID_QUIRK_DETAILED_IN_CM		(1 << 3)
     67  1.1.1.1.2.2  riastrad /* Detailed timing descriptors have bogus size values, so just take the
     68  1.1.1.1.2.2  riastrad  * maximum size and use that.
     69  1.1.1.1.2.2  riastrad  */
     70  1.1.1.1.2.2  riastrad #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE	(1 << 4)
     71  1.1.1.1.2.2  riastrad /* Monitor forgot to set the first detailed is preferred bit. */
     72  1.1.1.1.2.2  riastrad #define EDID_QUIRK_FIRST_DETAILED_PREFERRED	(1 << 5)
     73  1.1.1.1.2.2  riastrad /* use +hsync +vsync for detailed mode */
     74  1.1.1.1.2.2  riastrad #define EDID_QUIRK_DETAILED_SYNC_PP		(1 << 6)
     75  1.1.1.1.2.2  riastrad /* Force reduced-blanking timings for detailed modes */
     76  1.1.1.1.2.2  riastrad #define EDID_QUIRK_FORCE_REDUCED_BLANKING	(1 << 7)
     77  1.1.1.1.2.2  riastrad 
     78  1.1.1.1.2.2  riastrad struct detailed_mode_closure {
     79  1.1.1.1.2.2  riastrad 	struct drm_connector *connector;
     80  1.1.1.1.2.2  riastrad 	struct edid *edid;
     81  1.1.1.1.2.2  riastrad 	bool preferred;
     82  1.1.1.1.2.2  riastrad 	u32 quirks;
     83  1.1.1.1.2.2  riastrad 	int modes;
     84  1.1.1.1.2.2  riastrad };
     85  1.1.1.1.2.2  riastrad 
     86  1.1.1.1.2.2  riastrad #define LEVEL_DMT	0
     87  1.1.1.1.2.2  riastrad #define LEVEL_GTF	1
     88  1.1.1.1.2.2  riastrad #define LEVEL_GTF2	2
     89  1.1.1.1.2.2  riastrad #define LEVEL_CVT	3
     90  1.1.1.1.2.2  riastrad 
     91  1.1.1.1.2.2  riastrad static struct edid_quirk {
     92  1.1.1.1.2.2  riastrad 	char vendor[4];
     93  1.1.1.1.2.2  riastrad 	int product_id;
     94  1.1.1.1.2.2  riastrad 	u32 quirks;
     95  1.1.1.1.2.2  riastrad } edid_quirk_list[] = {
     96  1.1.1.1.2.2  riastrad 	/* ASUS VW222S */
     97  1.1.1.1.2.2  riastrad 	{ "ACI", 0x22a2, EDID_QUIRK_FORCE_REDUCED_BLANKING },
     98  1.1.1.1.2.2  riastrad 
     99  1.1.1.1.2.2  riastrad 	/* Acer AL1706 */
    100  1.1.1.1.2.2  riastrad 	{ "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
    101  1.1.1.1.2.2  riastrad 	/* Acer F51 */
    102  1.1.1.1.2.2  riastrad 	{ "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
    103  1.1.1.1.2.2  riastrad 	/* Unknown Acer */
    104  1.1.1.1.2.2  riastrad 	{ "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
    105  1.1.1.1.2.2  riastrad 
    106  1.1.1.1.2.2  riastrad 	/* Belinea 10 15 55 */
    107  1.1.1.1.2.2  riastrad 	{ "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
    108  1.1.1.1.2.2  riastrad 	{ "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
    109  1.1.1.1.2.2  riastrad 
    110  1.1.1.1.2.2  riastrad 	/* Envision Peripherals, Inc. EN-7100e */
    111  1.1.1.1.2.2  riastrad 	{ "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
    112  1.1.1.1.2.2  riastrad 	/* Envision EN2028 */
    113  1.1.1.1.2.2  riastrad 	{ "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
    114  1.1.1.1.2.2  riastrad 
    115  1.1.1.1.2.2  riastrad 	/* Funai Electronics PM36B */
    116  1.1.1.1.2.2  riastrad 	{ "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
    117  1.1.1.1.2.2  riastrad 	  EDID_QUIRK_DETAILED_IN_CM },
    118  1.1.1.1.2.2  riastrad 
    119  1.1.1.1.2.2  riastrad 	/* LG Philips LCD LP154W01-A5 */
    120  1.1.1.1.2.2  riastrad 	{ "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
    121  1.1.1.1.2.2  riastrad 	{ "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
    122  1.1.1.1.2.2  riastrad 
    123  1.1.1.1.2.2  riastrad 	/* Philips 107p5 CRT */
    124  1.1.1.1.2.2  riastrad 	{ "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
    125  1.1.1.1.2.2  riastrad 
    126  1.1.1.1.2.2  riastrad 	/* Proview AY765C */
    127  1.1.1.1.2.2  riastrad 	{ "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
    128  1.1.1.1.2.2  riastrad 
    129  1.1.1.1.2.2  riastrad 	/* Samsung SyncMaster 205BW.  Note: irony */
    130  1.1.1.1.2.2  riastrad 	{ "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
    131  1.1.1.1.2.2  riastrad 	/* Samsung SyncMaster 22[5-6]BW */
    132  1.1.1.1.2.2  riastrad 	{ "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
    133  1.1.1.1.2.2  riastrad 	{ "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
    134  1.1.1.1.2.2  riastrad 
    135  1.1.1.1.2.2  riastrad 	/* ViewSonic VA2026w */
    136  1.1.1.1.2.2  riastrad 	{ "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
    137  1.1.1.1.2.2  riastrad };
    138  1.1.1.1.2.2  riastrad 
    139  1.1.1.1.2.2  riastrad /*** DDC fetch and block validation ***/
    140  1.1.1.1.2.2  riastrad 
    141  1.1.1.1.2.2  riastrad static const u8 edid_header[] = {
    142  1.1.1.1.2.2  riastrad 	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
    143  1.1.1.1.2.2  riastrad };
    144  1.1.1.1.2.2  riastrad 
    145  1.1.1.1.2.2  riastrad  /*
    146  1.1.1.1.2.2  riastrad  * Sanity check the header of the base EDID block.  Return 8 if the header
    147  1.1.1.1.2.2  riastrad  * is perfect, down to 0 if it's totally wrong.
    148  1.1.1.1.2.2  riastrad  */
    149  1.1.1.1.2.2  riastrad int drm_edid_header_is_valid(const u8 *raw_edid)
    150  1.1.1.1.2.2  riastrad {
    151  1.1.1.1.2.2  riastrad 	int i, score = 0;
    152  1.1.1.1.2.2  riastrad 
    153  1.1.1.1.2.2  riastrad 	for (i = 0; i < sizeof(edid_header); i++)
    154  1.1.1.1.2.2  riastrad 		if (raw_edid[i] == edid_header[i])
    155  1.1.1.1.2.2  riastrad 			score++;
    156  1.1.1.1.2.2  riastrad 
    157  1.1.1.1.2.2  riastrad 	return score;
    158  1.1.1.1.2.2  riastrad }
    159  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_edid_header_is_valid);
    160  1.1.1.1.2.2  riastrad 
    161  1.1.1.1.2.2  riastrad static int edid_fixup __read_mostly = 6;
    162  1.1.1.1.2.2  riastrad module_param_named(edid_fixup, edid_fixup, int, 0400);
    163  1.1.1.1.2.2  riastrad MODULE_PARM_DESC(edid_fixup,
    164  1.1.1.1.2.2  riastrad 		 "Minimum number of valid EDID header bytes (0-8, default 6)");
    165  1.1.1.1.2.2  riastrad 
    166  1.1.1.1.2.2  riastrad /*
    167  1.1.1.1.2.2  riastrad  * Sanity check the EDID block (base or extension).  Return 0 if the block
    168  1.1.1.1.2.2  riastrad  * doesn't check out, or 1 if it's valid.
    169  1.1.1.1.2.2  riastrad  */
    170  1.1.1.1.2.2  riastrad bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid)
    171  1.1.1.1.2.2  riastrad {
    172  1.1.1.1.2.2  riastrad 	int i;
    173  1.1.1.1.2.2  riastrad 	u8 csum = 0;
    174  1.1.1.1.2.2  riastrad 	struct edid *edid = (struct edid *)raw_edid;
    175  1.1.1.1.2.2  riastrad 
    176  1.1.1.1.2.2  riastrad 	if (edid_fixup > 8 || edid_fixup < 0)
    177  1.1.1.1.2.2  riastrad 		edid_fixup = 6;
    178  1.1.1.1.2.2  riastrad 
    179  1.1.1.1.2.2  riastrad 	if (block == 0) {
    180  1.1.1.1.2.2  riastrad 		int score = drm_edid_header_is_valid(raw_edid);
    181  1.1.1.1.2.2  riastrad 		if (score == 8) ;
    182  1.1.1.1.2.2  riastrad 		else if (score >= edid_fixup) {
    183  1.1.1.1.2.2  riastrad 			DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
    184  1.1.1.1.2.2  riastrad 			memcpy(raw_edid, edid_header, sizeof(edid_header));
    185  1.1.1.1.2.2  riastrad 		} else {
    186  1.1.1.1.2.2  riastrad 			goto bad;
    187  1.1.1.1.2.2  riastrad 		}
    188  1.1.1.1.2.2  riastrad 	}
    189  1.1.1.1.2.2  riastrad 
    190  1.1.1.1.2.2  riastrad 	for (i = 0; i < EDID_LENGTH; i++)
    191  1.1.1.1.2.2  riastrad 		csum += raw_edid[i];
    192  1.1.1.1.2.2  riastrad 	if (csum) {
    193  1.1.1.1.2.2  riastrad 		if (print_bad_edid) {
    194  1.1.1.1.2.2  riastrad 			DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
    195  1.1.1.1.2.2  riastrad 		}
    196  1.1.1.1.2.2  riastrad 
    197  1.1.1.1.2.2  riastrad 		/* allow CEA to slide through, switches mangle this */
    198  1.1.1.1.2.2  riastrad 		if (raw_edid[0] != 0x02)
    199  1.1.1.1.2.2  riastrad 			goto bad;
    200  1.1.1.1.2.2  riastrad 	}
    201  1.1.1.1.2.2  riastrad 
    202  1.1.1.1.2.2  riastrad 	/* per-block-type checks */
    203  1.1.1.1.2.2  riastrad 	switch (raw_edid[0]) {
    204  1.1.1.1.2.2  riastrad 	case 0: /* base */
    205  1.1.1.1.2.2  riastrad 		if (edid->version != 1) {
    206  1.1.1.1.2.2  riastrad 			DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
    207  1.1.1.1.2.2  riastrad 			goto bad;
    208  1.1.1.1.2.2  riastrad 		}
    209  1.1.1.1.2.2  riastrad 
    210  1.1.1.1.2.2  riastrad 		if (edid->revision > 4)
    211  1.1.1.1.2.2  riastrad 			DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
    212  1.1.1.1.2.2  riastrad 		break;
    213  1.1.1.1.2.2  riastrad 
    214  1.1.1.1.2.2  riastrad 	default:
    215  1.1.1.1.2.2  riastrad 		break;
    216  1.1.1.1.2.2  riastrad 	}
    217  1.1.1.1.2.2  riastrad 
    218  1.1.1.1.2.2  riastrad 	return 1;
    219  1.1.1.1.2.2  riastrad 
    220  1.1.1.1.2.2  riastrad bad:
    221  1.1.1.1.2.2  riastrad 	if (raw_edid && print_bad_edid) {
    222  1.1.1.1.2.2  riastrad 		printk(KERN_ERR "Raw EDID:\n");
    223  1.1.1.1.2.2  riastrad 		print_hex_dump(KERN_ERR, " \t", DUMP_PREFIX_NONE, 16, 1,
    224  1.1.1.1.2.2  riastrad 			       raw_edid, EDID_LENGTH, false);
    225  1.1.1.1.2.2  riastrad 	}
    226  1.1.1.1.2.2  riastrad 	return 0;
    227  1.1.1.1.2.2  riastrad }
    228  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_edid_block_valid);
    229  1.1.1.1.2.2  riastrad 
    230  1.1.1.1.2.2  riastrad /**
    231  1.1.1.1.2.2  riastrad  * drm_edid_is_valid - sanity check EDID data
    232  1.1.1.1.2.2  riastrad  * @edid: EDID data
    233  1.1.1.1.2.2  riastrad  *
    234  1.1.1.1.2.2  riastrad  * Sanity-check an entire EDID record (including extensions)
    235  1.1.1.1.2.2  riastrad  */
    236  1.1.1.1.2.2  riastrad bool drm_edid_is_valid(struct edid *edid)
    237  1.1.1.1.2.2  riastrad {
    238  1.1.1.1.2.2  riastrad 	int i;
    239  1.1.1.1.2.2  riastrad 	u8 *raw = (u8 *)edid;
    240  1.1.1.1.2.2  riastrad 
    241  1.1.1.1.2.2  riastrad 	if (!edid)
    242  1.1.1.1.2.2  riastrad 		return false;
    243  1.1.1.1.2.2  riastrad 
    244  1.1.1.1.2.2  riastrad 	for (i = 0; i <= edid->extensions; i++)
    245  1.1.1.1.2.2  riastrad 		if (!drm_edid_block_valid(raw + i * EDID_LENGTH, i, true))
    246  1.1.1.1.2.2  riastrad 			return false;
    247  1.1.1.1.2.2  riastrad 
    248  1.1.1.1.2.2  riastrad 	return true;
    249  1.1.1.1.2.2  riastrad }
    250  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_edid_is_valid);
    251  1.1.1.1.2.2  riastrad 
    252  1.1.1.1.2.2  riastrad #define DDC_SEGMENT_ADDR 0x30
    253  1.1.1.1.2.2  riastrad /**
    254  1.1.1.1.2.2  riastrad  * Get EDID information via I2C.
    255  1.1.1.1.2.2  riastrad  *
    256  1.1.1.1.2.2  riastrad  * \param adapter : i2c device adaptor
    257  1.1.1.1.2.2  riastrad  * \param buf     : EDID data buffer to be filled
    258  1.1.1.1.2.2  riastrad  * \param len     : EDID data buffer length
    259  1.1.1.1.2.2  riastrad  * \return 0 on success or -1 on failure.
    260  1.1.1.1.2.2  riastrad  *
    261  1.1.1.1.2.2  riastrad  * Try to fetch EDID information by calling i2c driver function.
    262  1.1.1.1.2.2  riastrad  */
    263  1.1.1.1.2.2  riastrad static int
    264  1.1.1.1.2.2  riastrad drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
    265  1.1.1.1.2.2  riastrad 		      int block, int len)
    266  1.1.1.1.2.2  riastrad {
    267  1.1.1.1.2.2  riastrad 	unsigned char start = block * EDID_LENGTH;
    268  1.1.1.1.2.2  riastrad 	unsigned char segment = block >> 1;
    269  1.1.1.1.2.2  riastrad 	unsigned char xfers = segment ? 3 : 2;
    270  1.1.1.1.2.2  riastrad 	int ret, retries = 5;
    271  1.1.1.1.2.2  riastrad 
    272  1.1.1.1.2.2  riastrad 	/* The core i2c driver will automatically retry the transfer if the
    273  1.1.1.1.2.2  riastrad 	 * adapter reports EAGAIN. However, we find that bit-banging transfers
    274  1.1.1.1.2.2  riastrad 	 * are susceptible to errors under a heavily loaded machine and
    275  1.1.1.1.2.2  riastrad 	 * generate spurious NAKs and timeouts. Retrying the transfer
    276  1.1.1.1.2.2  riastrad 	 * of the individual block a few times seems to overcome this.
    277  1.1.1.1.2.2  riastrad 	 */
    278  1.1.1.1.2.2  riastrad 	do {
    279  1.1.1.1.2.2  riastrad 		struct i2c_msg msgs[] = {
    280  1.1.1.1.2.2  riastrad 			{
    281  1.1.1.1.2.2  riastrad 				.addr	= DDC_SEGMENT_ADDR,
    282  1.1.1.1.2.2  riastrad 				.flags	= 0,
    283  1.1.1.1.2.2  riastrad 				.len	= 1,
    284  1.1.1.1.2.2  riastrad 				.buf	= &segment,
    285  1.1.1.1.2.2  riastrad 			}, {
    286  1.1.1.1.2.2  riastrad 				.addr	= DDC_ADDR,
    287  1.1.1.1.2.2  riastrad 				.flags	= 0,
    288  1.1.1.1.2.2  riastrad 				.len	= 1,
    289  1.1.1.1.2.2  riastrad 				.buf	= &start,
    290  1.1.1.1.2.2  riastrad 			}, {
    291  1.1.1.1.2.2  riastrad 				.addr	= DDC_ADDR,
    292  1.1.1.1.2.2  riastrad 				.flags	= I2C_M_RD,
    293  1.1.1.1.2.2  riastrad 				.len	= len,
    294  1.1.1.1.2.2  riastrad 				.buf	= buf,
    295  1.1.1.1.2.2  riastrad 			}
    296  1.1.1.1.2.2  riastrad 		};
    297  1.1.1.1.2.2  riastrad 
    298  1.1.1.1.2.2  riastrad 	/*
    299  1.1.1.1.2.2  riastrad 	 * Avoid sending the segment addr to not upset non-compliant ddc
    300  1.1.1.1.2.2  riastrad 	 * monitors.
    301  1.1.1.1.2.2  riastrad 	 */
    302  1.1.1.1.2.2  riastrad 		ret = i2c_transfer(adapter, &msgs[3 - xfers], xfers);
    303  1.1.1.1.2.2  riastrad 
    304  1.1.1.1.2.2  riastrad 		if (ret == -ENXIO) {
    305  1.1.1.1.2.2  riastrad 			DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
    306  1.1.1.1.2.2  riastrad 					adapter->name);
    307  1.1.1.1.2.2  riastrad 			break;
    308  1.1.1.1.2.2  riastrad 		}
    309  1.1.1.1.2.2  riastrad 	} while (ret != xfers && --retries);
    310  1.1.1.1.2.2  riastrad 
    311  1.1.1.1.2.2  riastrad 	return ret == xfers ? 0 : -1;
    312  1.1.1.1.2.2  riastrad }
    313  1.1.1.1.2.2  riastrad 
    314  1.1.1.1.2.2  riastrad static bool drm_edid_is_zero(u8 *in_edid, int length)
    315  1.1.1.1.2.2  riastrad {
    316  1.1.1.1.2.2  riastrad 	if (memchr_inv(in_edid, 0, length))
    317  1.1.1.1.2.2  riastrad 		return false;
    318  1.1.1.1.2.2  riastrad 
    319  1.1.1.1.2.2  riastrad 	return true;
    320  1.1.1.1.2.2  riastrad }
    321  1.1.1.1.2.2  riastrad 
    322  1.1.1.1.2.2  riastrad static u8 *
    323  1.1.1.1.2.2  riastrad drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
    324  1.1.1.1.2.2  riastrad {
    325  1.1.1.1.2.2  riastrad 	int i, j = 0, valid_extensions = 0;
    326  1.1.1.1.2.2  riastrad 	u8 *block, *new;
    327  1.1.1.1.2.2  riastrad 	bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
    328  1.1.1.1.2.2  riastrad 
    329  1.1.1.1.2.2  riastrad 	if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
    330  1.1.1.1.2.2  riastrad 		return NULL;
    331  1.1.1.1.2.2  riastrad 
    332  1.1.1.1.2.2  riastrad 	/* base block fetch */
    333  1.1.1.1.2.2  riastrad 	for (i = 0; i < 4; i++) {
    334  1.1.1.1.2.2  riastrad 		if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
    335  1.1.1.1.2.2  riastrad 			goto out;
    336  1.1.1.1.2.2  riastrad 		if (drm_edid_block_valid(block, 0, print_bad_edid))
    337  1.1.1.1.2.2  riastrad 			break;
    338  1.1.1.1.2.2  riastrad 		if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
    339  1.1.1.1.2.2  riastrad 			connector->null_edid_counter++;
    340  1.1.1.1.2.2  riastrad 			goto carp;
    341  1.1.1.1.2.2  riastrad 		}
    342  1.1.1.1.2.2  riastrad 	}
    343  1.1.1.1.2.2  riastrad 	if (i == 4)
    344  1.1.1.1.2.2  riastrad 		goto carp;
    345  1.1.1.1.2.2  riastrad 
    346  1.1.1.1.2.2  riastrad 	/* if there's no extensions, we're done */
    347  1.1.1.1.2.2  riastrad 	if (block[0x7e] == 0)
    348  1.1.1.1.2.2  riastrad 		return block;
    349  1.1.1.1.2.2  riastrad 
    350  1.1.1.1.2.2  riastrad 	new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
    351  1.1.1.1.2.2  riastrad 	if (!new)
    352  1.1.1.1.2.2  riastrad 		goto out;
    353  1.1.1.1.2.2  riastrad 	block = new;
    354  1.1.1.1.2.2  riastrad 
    355  1.1.1.1.2.2  riastrad 	for (j = 1; j <= block[0x7e]; j++) {
    356  1.1.1.1.2.2  riastrad 		for (i = 0; i < 4; i++) {
    357  1.1.1.1.2.2  riastrad 			if (drm_do_probe_ddc_edid(adapter,
    358  1.1.1.1.2.2  riastrad 				  block + (valid_extensions + 1) * EDID_LENGTH,
    359  1.1.1.1.2.2  riastrad 				  j, EDID_LENGTH))
    360  1.1.1.1.2.2  riastrad 				goto out;
    361  1.1.1.1.2.2  riastrad 			if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH, j, print_bad_edid)) {
    362  1.1.1.1.2.2  riastrad 				valid_extensions++;
    363  1.1.1.1.2.2  riastrad 				break;
    364  1.1.1.1.2.2  riastrad 			}
    365  1.1.1.1.2.2  riastrad 		}
    366  1.1.1.1.2.2  riastrad 		if (i == 4)
    367  1.1.1.1.2.2  riastrad 			dev_warn(connector->dev->dev,
    368  1.1.1.1.2.2  riastrad 			 "%s: Ignoring invalid EDID block %d.\n",
    369  1.1.1.1.2.2  riastrad 			 drm_get_connector_name(connector), j);
    370  1.1.1.1.2.2  riastrad 	}
    371  1.1.1.1.2.2  riastrad 
    372  1.1.1.1.2.2  riastrad 	if (valid_extensions != block[0x7e]) {
    373  1.1.1.1.2.2  riastrad 		block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
    374  1.1.1.1.2.2  riastrad 		block[0x7e] = valid_extensions;
    375  1.1.1.1.2.2  riastrad 		new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
    376  1.1.1.1.2.2  riastrad 		if (!new)
    377  1.1.1.1.2.2  riastrad 			goto out;
    378  1.1.1.1.2.2  riastrad 		block = new;
    379  1.1.1.1.2.2  riastrad 	}
    380  1.1.1.1.2.2  riastrad 
    381  1.1.1.1.2.2  riastrad 	return block;
    382  1.1.1.1.2.2  riastrad 
    383  1.1.1.1.2.2  riastrad carp:
    384  1.1.1.1.2.2  riastrad 	if (print_bad_edid) {
    385  1.1.1.1.2.2  riastrad 		dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
    386  1.1.1.1.2.2  riastrad 			 drm_get_connector_name(connector), j);
    387  1.1.1.1.2.2  riastrad 	}
    388  1.1.1.1.2.2  riastrad 	connector->bad_edid_counter++;
    389  1.1.1.1.2.2  riastrad 
    390  1.1.1.1.2.2  riastrad out:
    391  1.1.1.1.2.2  riastrad 	kfree(block);
    392  1.1.1.1.2.2  riastrad 	return NULL;
    393  1.1.1.1.2.2  riastrad }
    394  1.1.1.1.2.2  riastrad 
    395  1.1.1.1.2.2  riastrad /**
    396  1.1.1.1.2.2  riastrad  * Probe DDC presence.
    397  1.1.1.1.2.2  riastrad  *
    398  1.1.1.1.2.2  riastrad  * \param adapter : i2c device adaptor
    399  1.1.1.1.2.2  riastrad  * \return 1 on success
    400  1.1.1.1.2.2  riastrad  */
    401  1.1.1.1.2.2  riastrad bool
    402  1.1.1.1.2.2  riastrad drm_probe_ddc(struct i2c_adapter *adapter)
    403  1.1.1.1.2.2  riastrad {
    404  1.1.1.1.2.2  riastrad 	unsigned char out;
    405  1.1.1.1.2.2  riastrad 
    406  1.1.1.1.2.2  riastrad 	return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
    407  1.1.1.1.2.2  riastrad }
    408  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_probe_ddc);
    409  1.1.1.1.2.2  riastrad 
    410  1.1.1.1.2.2  riastrad /**
    411  1.1.1.1.2.2  riastrad  * drm_get_edid - get EDID data, if available
    412  1.1.1.1.2.2  riastrad  * @connector: connector we're probing
    413  1.1.1.1.2.2  riastrad  * @adapter: i2c adapter to use for DDC
    414  1.1.1.1.2.2  riastrad  *
    415  1.1.1.1.2.2  riastrad  * Poke the given i2c channel to grab EDID data if possible.  If found,
    416  1.1.1.1.2.2  riastrad  * attach it to the connector.
    417  1.1.1.1.2.2  riastrad  *
    418  1.1.1.1.2.2  riastrad  * Return edid data or NULL if we couldn't find any.
    419  1.1.1.1.2.2  riastrad  */
    420  1.1.1.1.2.2  riastrad struct edid *drm_get_edid(struct drm_connector *connector,
    421  1.1.1.1.2.2  riastrad 			  struct i2c_adapter *adapter)
    422  1.1.1.1.2.2  riastrad {
    423  1.1.1.1.2.2  riastrad 	struct edid *edid = NULL;
    424  1.1.1.1.2.2  riastrad 
    425  1.1.1.1.2.2  riastrad 	if (drm_probe_ddc(adapter))
    426  1.1.1.1.2.2  riastrad 		edid = (struct edid *)drm_do_get_edid(connector, adapter);
    427  1.1.1.1.2.2  riastrad 
    428  1.1.1.1.2.2  riastrad 	return edid;
    429  1.1.1.1.2.2  riastrad }
    430  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_get_edid);
    431  1.1.1.1.2.2  riastrad 
    432  1.1.1.1.2.2  riastrad /*** EDID parsing ***/
    433  1.1.1.1.2.2  riastrad 
    434  1.1.1.1.2.2  riastrad /**
    435  1.1.1.1.2.2  riastrad  * edid_vendor - match a string against EDID's obfuscated vendor field
    436  1.1.1.1.2.2  riastrad  * @edid: EDID to match
    437  1.1.1.1.2.2  riastrad  * @vendor: vendor string
    438  1.1.1.1.2.2  riastrad  *
    439  1.1.1.1.2.2  riastrad  * Returns true if @vendor is in @edid, false otherwise
    440  1.1.1.1.2.2  riastrad  */
    441  1.1.1.1.2.2  riastrad static bool edid_vendor(struct edid *edid, char *vendor)
    442  1.1.1.1.2.2  riastrad {
    443  1.1.1.1.2.2  riastrad 	char edid_vendor[3];
    444  1.1.1.1.2.2  riastrad 
    445  1.1.1.1.2.2  riastrad 	edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
    446  1.1.1.1.2.2  riastrad 	edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
    447  1.1.1.1.2.2  riastrad 			  ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
    448  1.1.1.1.2.2  riastrad 	edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
    449  1.1.1.1.2.2  riastrad 
    450  1.1.1.1.2.2  riastrad 	return !strncmp(edid_vendor, vendor, 3);
    451  1.1.1.1.2.2  riastrad }
    452  1.1.1.1.2.2  riastrad 
    453  1.1.1.1.2.2  riastrad /**
    454  1.1.1.1.2.2  riastrad  * edid_get_quirks - return quirk flags for a given EDID
    455  1.1.1.1.2.2  riastrad  * @edid: EDID to process
    456  1.1.1.1.2.2  riastrad  *
    457  1.1.1.1.2.2  riastrad  * This tells subsequent routines what fixes they need to apply.
    458  1.1.1.1.2.2  riastrad  */
    459  1.1.1.1.2.2  riastrad static u32 edid_get_quirks(struct edid *edid)
    460  1.1.1.1.2.2  riastrad {
    461  1.1.1.1.2.2  riastrad 	struct edid_quirk *quirk;
    462  1.1.1.1.2.2  riastrad 	int i;
    463  1.1.1.1.2.2  riastrad 
    464  1.1.1.1.2.2  riastrad 	for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
    465  1.1.1.1.2.2  riastrad 		quirk = &edid_quirk_list[i];
    466  1.1.1.1.2.2  riastrad 
    467  1.1.1.1.2.2  riastrad 		if (edid_vendor(edid, quirk->vendor) &&
    468  1.1.1.1.2.2  riastrad 		    (EDID_PRODUCT_ID(edid) == quirk->product_id))
    469  1.1.1.1.2.2  riastrad 			return quirk->quirks;
    470  1.1.1.1.2.2  riastrad 	}
    471  1.1.1.1.2.2  riastrad 
    472  1.1.1.1.2.2  riastrad 	return 0;
    473  1.1.1.1.2.2  riastrad }
    474  1.1.1.1.2.2  riastrad 
    475  1.1.1.1.2.2  riastrad #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
    476  1.1.1.1.2.2  riastrad #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
    477  1.1.1.1.2.2  riastrad 
    478  1.1.1.1.2.2  riastrad /**
    479  1.1.1.1.2.2  riastrad  * edid_fixup_preferred - set preferred modes based on quirk list
    480  1.1.1.1.2.2  riastrad  * @connector: has mode list to fix up
    481  1.1.1.1.2.2  riastrad  * @quirks: quirks list
    482  1.1.1.1.2.2  riastrad  *
    483  1.1.1.1.2.2  riastrad  * Walk the mode list for @connector, clearing the preferred status
    484  1.1.1.1.2.2  riastrad  * on existing modes and setting it anew for the right mode ala @quirks.
    485  1.1.1.1.2.2  riastrad  */
    486  1.1.1.1.2.2  riastrad static void edid_fixup_preferred(struct drm_connector *connector,
    487  1.1.1.1.2.2  riastrad 				 u32 quirks)
    488  1.1.1.1.2.2  riastrad {
    489  1.1.1.1.2.2  riastrad 	struct drm_display_mode *t, *cur_mode, *preferred_mode;
    490  1.1.1.1.2.2  riastrad 	int target_refresh = 0;
    491  1.1.1.1.2.2  riastrad 
    492  1.1.1.1.2.2  riastrad 	if (list_empty(&connector->probed_modes))
    493  1.1.1.1.2.2  riastrad 		return;
    494  1.1.1.1.2.2  riastrad 
    495  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_PREFER_LARGE_60)
    496  1.1.1.1.2.2  riastrad 		target_refresh = 60;
    497  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_PREFER_LARGE_75)
    498  1.1.1.1.2.2  riastrad 		target_refresh = 75;
    499  1.1.1.1.2.2  riastrad 
    500  1.1.1.1.2.2  riastrad 	preferred_mode = list_first_entry(&connector->probed_modes,
    501  1.1.1.1.2.2  riastrad 					  struct drm_display_mode, head);
    502  1.1.1.1.2.2  riastrad 
    503  1.1.1.1.2.2  riastrad 	list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
    504  1.1.1.1.2.2  riastrad 		cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
    505  1.1.1.1.2.2  riastrad 
    506  1.1.1.1.2.2  riastrad 		if (cur_mode == preferred_mode)
    507  1.1.1.1.2.2  riastrad 			continue;
    508  1.1.1.1.2.2  riastrad 
    509  1.1.1.1.2.2  riastrad 		/* Largest mode is preferred */
    510  1.1.1.1.2.2  riastrad 		if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
    511  1.1.1.1.2.2  riastrad 			preferred_mode = cur_mode;
    512  1.1.1.1.2.2  riastrad 
    513  1.1.1.1.2.2  riastrad 		/* At a given size, try to get closest to target refresh */
    514  1.1.1.1.2.2  riastrad 		if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
    515  1.1.1.1.2.2  riastrad 		    MODE_REFRESH_DIFF(cur_mode, target_refresh) <
    516  1.1.1.1.2.2  riastrad 		    MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
    517  1.1.1.1.2.2  riastrad 			preferred_mode = cur_mode;
    518  1.1.1.1.2.2  riastrad 		}
    519  1.1.1.1.2.2  riastrad 	}
    520  1.1.1.1.2.2  riastrad 
    521  1.1.1.1.2.2  riastrad 	preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
    522  1.1.1.1.2.2  riastrad }
    523  1.1.1.1.2.2  riastrad 
    524  1.1.1.1.2.2  riastrad static bool
    525  1.1.1.1.2.2  riastrad mode_is_rb(const struct drm_display_mode *mode)
    526  1.1.1.1.2.2  riastrad {
    527  1.1.1.1.2.2  riastrad 	return (mode->htotal - mode->hdisplay == 160) &&
    528  1.1.1.1.2.2  riastrad 	       (mode->hsync_end - mode->hdisplay == 80) &&
    529  1.1.1.1.2.2  riastrad 	       (mode->hsync_end - mode->hsync_start == 32) &&
    530  1.1.1.1.2.2  riastrad 	       (mode->vsync_start - mode->vdisplay == 3);
    531  1.1.1.1.2.2  riastrad }
    532  1.1.1.1.2.2  riastrad 
    533  1.1.1.1.2.2  riastrad /*
    534  1.1.1.1.2.2  riastrad  * drm_mode_find_dmt - Create a copy of a mode if present in DMT
    535  1.1.1.1.2.2  riastrad  * @dev: Device to duplicate against
    536  1.1.1.1.2.2  riastrad  * @hsize: Mode width
    537  1.1.1.1.2.2  riastrad  * @vsize: Mode height
    538  1.1.1.1.2.2  riastrad  * @fresh: Mode refresh rate
    539  1.1.1.1.2.2  riastrad  * @rb: Mode reduced-blanking-ness
    540  1.1.1.1.2.2  riastrad  *
    541  1.1.1.1.2.2  riastrad  * Walk the DMT mode list looking for a match for the given parameters.
    542  1.1.1.1.2.2  riastrad  * Return a newly allocated copy of the mode, or NULL if not found.
    543  1.1.1.1.2.2  riastrad  */
    544  1.1.1.1.2.2  riastrad struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
    545  1.1.1.1.2.2  riastrad 					   int hsize, int vsize, int fresh,
    546  1.1.1.1.2.2  riastrad 					   bool rb)
    547  1.1.1.1.2.2  riastrad {
    548  1.1.1.1.2.2  riastrad 	int i;
    549  1.1.1.1.2.2  riastrad 
    550  1.1.1.1.2.2  riastrad 	for (i = 0; i < drm_num_dmt_modes; i++) {
    551  1.1.1.1.2.2  riastrad 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
    552  1.1.1.1.2.2  riastrad 		if (hsize != ptr->hdisplay)
    553  1.1.1.1.2.2  riastrad 			continue;
    554  1.1.1.1.2.2  riastrad 		if (vsize != ptr->vdisplay)
    555  1.1.1.1.2.2  riastrad 			continue;
    556  1.1.1.1.2.2  riastrad 		if (fresh != drm_mode_vrefresh(ptr))
    557  1.1.1.1.2.2  riastrad 			continue;
    558  1.1.1.1.2.2  riastrad 		if (rb != mode_is_rb(ptr))
    559  1.1.1.1.2.2  riastrad 			continue;
    560  1.1.1.1.2.2  riastrad 
    561  1.1.1.1.2.2  riastrad 		return drm_mode_duplicate(dev, ptr);
    562  1.1.1.1.2.2  riastrad 	}
    563  1.1.1.1.2.2  riastrad 
    564  1.1.1.1.2.2  riastrad 	return NULL;
    565  1.1.1.1.2.2  riastrad }
    566  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_mode_find_dmt);
    567  1.1.1.1.2.2  riastrad 
    568  1.1.1.1.2.2  riastrad typedef void detailed_cb(struct detailed_timing *timing, void *closure);
    569  1.1.1.1.2.2  riastrad 
    570  1.1.1.1.2.2  riastrad static void
    571  1.1.1.1.2.2  riastrad cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
    572  1.1.1.1.2.2  riastrad {
    573  1.1.1.1.2.2  riastrad 	int i, n = 0;
    574  1.1.1.1.2.2  riastrad 	u8 d = ext[0x02];
    575  1.1.1.1.2.2  riastrad 	u8 *det_base = ext + d;
    576  1.1.1.1.2.2  riastrad 
    577  1.1.1.1.2.2  riastrad 	n = (127 - d) / 18;
    578  1.1.1.1.2.2  riastrad 	for (i = 0; i < n; i++)
    579  1.1.1.1.2.2  riastrad 		cb((struct detailed_timing *)(det_base + 18 * i), closure);
    580  1.1.1.1.2.2  riastrad }
    581  1.1.1.1.2.2  riastrad 
    582  1.1.1.1.2.2  riastrad static void
    583  1.1.1.1.2.2  riastrad vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
    584  1.1.1.1.2.2  riastrad {
    585  1.1.1.1.2.2  riastrad 	unsigned int i, n = min((int)ext[0x02], 6);
    586  1.1.1.1.2.2  riastrad 	u8 *det_base = ext + 5;
    587  1.1.1.1.2.2  riastrad 
    588  1.1.1.1.2.2  riastrad 	if (ext[0x01] != 1)
    589  1.1.1.1.2.2  riastrad 		return; /* unknown version */
    590  1.1.1.1.2.2  riastrad 
    591  1.1.1.1.2.2  riastrad 	for (i = 0; i < n; i++)
    592  1.1.1.1.2.2  riastrad 		cb((struct detailed_timing *)(det_base + 18 * i), closure);
    593  1.1.1.1.2.2  riastrad }
    594  1.1.1.1.2.2  riastrad 
    595  1.1.1.1.2.2  riastrad static void
    596  1.1.1.1.2.2  riastrad drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
    597  1.1.1.1.2.2  riastrad {
    598  1.1.1.1.2.2  riastrad 	int i;
    599  1.1.1.1.2.2  riastrad 	struct edid *edid = (struct edid *)raw_edid;
    600  1.1.1.1.2.2  riastrad 
    601  1.1.1.1.2.2  riastrad 	if (edid == NULL)
    602  1.1.1.1.2.2  riastrad 		return;
    603  1.1.1.1.2.2  riastrad 
    604  1.1.1.1.2.2  riastrad 	for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
    605  1.1.1.1.2.2  riastrad 		cb(&(edid->detailed_timings[i]), closure);
    606  1.1.1.1.2.2  riastrad 
    607  1.1.1.1.2.2  riastrad 	for (i = 1; i <= raw_edid[0x7e]; i++) {
    608  1.1.1.1.2.2  riastrad 		u8 *ext = raw_edid + (i * EDID_LENGTH);
    609  1.1.1.1.2.2  riastrad 		switch (*ext) {
    610  1.1.1.1.2.2  riastrad 		case CEA_EXT:
    611  1.1.1.1.2.2  riastrad 			cea_for_each_detailed_block(ext, cb, closure);
    612  1.1.1.1.2.2  riastrad 			break;
    613  1.1.1.1.2.2  riastrad 		case VTB_EXT:
    614  1.1.1.1.2.2  riastrad 			vtb_for_each_detailed_block(ext, cb, closure);
    615  1.1.1.1.2.2  riastrad 			break;
    616  1.1.1.1.2.2  riastrad 		default:
    617  1.1.1.1.2.2  riastrad 			break;
    618  1.1.1.1.2.2  riastrad 		}
    619  1.1.1.1.2.2  riastrad 	}
    620  1.1.1.1.2.2  riastrad }
    621  1.1.1.1.2.2  riastrad 
    622  1.1.1.1.2.2  riastrad static void
    623  1.1.1.1.2.2  riastrad is_rb(struct detailed_timing *t, void *data)
    624  1.1.1.1.2.2  riastrad {
    625  1.1.1.1.2.2  riastrad 	u8 *r = (u8 *)t;
    626  1.1.1.1.2.2  riastrad 	if (r[3] == EDID_DETAIL_MONITOR_RANGE)
    627  1.1.1.1.2.2  riastrad 		if (r[15] & 0x10)
    628  1.1.1.1.2.2  riastrad 			*(bool *)data = true;
    629  1.1.1.1.2.2  riastrad }
    630  1.1.1.1.2.2  riastrad 
    631  1.1.1.1.2.2  riastrad /* EDID 1.4 defines this explicitly.  For EDID 1.3, we guess, badly. */
    632  1.1.1.1.2.2  riastrad static bool
    633  1.1.1.1.2.2  riastrad drm_monitor_supports_rb(struct edid *edid)
    634  1.1.1.1.2.2  riastrad {
    635  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4) {
    636  1.1.1.1.2.2  riastrad 		bool ret = false;
    637  1.1.1.1.2.2  riastrad 		drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
    638  1.1.1.1.2.2  riastrad 		return ret;
    639  1.1.1.1.2.2  riastrad 	}
    640  1.1.1.1.2.2  riastrad 
    641  1.1.1.1.2.2  riastrad 	return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
    642  1.1.1.1.2.2  riastrad }
    643  1.1.1.1.2.2  riastrad 
    644  1.1.1.1.2.2  riastrad static void
    645  1.1.1.1.2.2  riastrad find_gtf2(struct detailed_timing *t, void *data)
    646  1.1.1.1.2.2  riastrad {
    647  1.1.1.1.2.2  riastrad 	u8 *r = (u8 *)t;
    648  1.1.1.1.2.2  riastrad 	if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
    649  1.1.1.1.2.2  riastrad 		*(u8 **)data = r;
    650  1.1.1.1.2.2  riastrad }
    651  1.1.1.1.2.2  riastrad 
    652  1.1.1.1.2.2  riastrad /* Secondary GTF curve kicks in above some break frequency */
    653  1.1.1.1.2.2  riastrad static int
    654  1.1.1.1.2.2  riastrad drm_gtf2_hbreak(struct edid *edid)
    655  1.1.1.1.2.2  riastrad {
    656  1.1.1.1.2.2  riastrad 	u8 *r = NULL;
    657  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    658  1.1.1.1.2.2  riastrad 	return r ? (r[12] * 2) : 0;
    659  1.1.1.1.2.2  riastrad }
    660  1.1.1.1.2.2  riastrad 
    661  1.1.1.1.2.2  riastrad static int
    662  1.1.1.1.2.2  riastrad drm_gtf2_2c(struct edid *edid)
    663  1.1.1.1.2.2  riastrad {
    664  1.1.1.1.2.2  riastrad 	u8 *r = NULL;
    665  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    666  1.1.1.1.2.2  riastrad 	return r ? r[13] : 0;
    667  1.1.1.1.2.2  riastrad }
    668  1.1.1.1.2.2  riastrad 
    669  1.1.1.1.2.2  riastrad static int
    670  1.1.1.1.2.2  riastrad drm_gtf2_m(struct edid *edid)
    671  1.1.1.1.2.2  riastrad {
    672  1.1.1.1.2.2  riastrad 	u8 *r = NULL;
    673  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    674  1.1.1.1.2.2  riastrad 	return r ? (r[15] << 8) + r[14] : 0;
    675  1.1.1.1.2.2  riastrad }
    676  1.1.1.1.2.2  riastrad 
    677  1.1.1.1.2.2  riastrad static int
    678  1.1.1.1.2.2  riastrad drm_gtf2_k(struct edid *edid)
    679  1.1.1.1.2.2  riastrad {
    680  1.1.1.1.2.2  riastrad 	u8 *r = NULL;
    681  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    682  1.1.1.1.2.2  riastrad 	return r ? r[16] : 0;
    683  1.1.1.1.2.2  riastrad }
    684  1.1.1.1.2.2  riastrad 
    685  1.1.1.1.2.2  riastrad static int
    686  1.1.1.1.2.2  riastrad drm_gtf2_2j(struct edid *edid)
    687  1.1.1.1.2.2  riastrad {
    688  1.1.1.1.2.2  riastrad 	u8 *r = NULL;
    689  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    690  1.1.1.1.2.2  riastrad 	return r ? r[17] : 0;
    691  1.1.1.1.2.2  riastrad }
    692  1.1.1.1.2.2  riastrad 
    693  1.1.1.1.2.2  riastrad /**
    694  1.1.1.1.2.2  riastrad  * standard_timing_level - get std. timing level(CVT/GTF/DMT)
    695  1.1.1.1.2.2  riastrad  * @edid: EDID block to scan
    696  1.1.1.1.2.2  riastrad  */
    697  1.1.1.1.2.2  riastrad static int standard_timing_level(struct edid *edid)
    698  1.1.1.1.2.2  riastrad {
    699  1.1.1.1.2.2  riastrad 	if (edid->revision >= 2) {
    700  1.1.1.1.2.2  riastrad 		if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
    701  1.1.1.1.2.2  riastrad 			return LEVEL_CVT;
    702  1.1.1.1.2.2  riastrad 		if (drm_gtf2_hbreak(edid))
    703  1.1.1.1.2.2  riastrad 			return LEVEL_GTF2;
    704  1.1.1.1.2.2  riastrad 		return LEVEL_GTF;
    705  1.1.1.1.2.2  riastrad 	}
    706  1.1.1.1.2.2  riastrad 	return LEVEL_DMT;
    707  1.1.1.1.2.2  riastrad }
    708  1.1.1.1.2.2  riastrad 
    709  1.1.1.1.2.2  riastrad /*
    710  1.1.1.1.2.2  riastrad  * 0 is reserved.  The spec says 0x01 fill for unused timings.  Some old
    711  1.1.1.1.2.2  riastrad  * monitors fill with ascii space (0x20) instead.
    712  1.1.1.1.2.2  riastrad  */
    713  1.1.1.1.2.2  riastrad static int
    714  1.1.1.1.2.2  riastrad bad_std_timing(u8 a, u8 b)
    715  1.1.1.1.2.2  riastrad {
    716  1.1.1.1.2.2  riastrad 	return (a == 0x00 && b == 0x00) ||
    717  1.1.1.1.2.2  riastrad 	       (a == 0x01 && b == 0x01) ||
    718  1.1.1.1.2.2  riastrad 	       (a == 0x20 && b == 0x20);
    719  1.1.1.1.2.2  riastrad }
    720  1.1.1.1.2.2  riastrad 
    721  1.1.1.1.2.2  riastrad /**
    722  1.1.1.1.2.2  riastrad  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
    723  1.1.1.1.2.2  riastrad  * @t: standard timing params
    724  1.1.1.1.2.2  riastrad  * @timing_level: standard timing level
    725  1.1.1.1.2.2  riastrad  *
    726  1.1.1.1.2.2  riastrad  * Take the standard timing params (in this case width, aspect, and refresh)
    727  1.1.1.1.2.2  riastrad  * and convert them into a real mode using CVT/GTF/DMT.
    728  1.1.1.1.2.2  riastrad  */
    729  1.1.1.1.2.2  riastrad static struct drm_display_mode *
    730  1.1.1.1.2.2  riastrad drm_mode_std(struct drm_connector *connector, struct edid *edid,
    731  1.1.1.1.2.2  riastrad 	     struct std_timing *t, int revision)
    732  1.1.1.1.2.2  riastrad {
    733  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
    734  1.1.1.1.2.2  riastrad 	struct drm_display_mode *m, *mode = NULL;
    735  1.1.1.1.2.2  riastrad 	int hsize, vsize;
    736  1.1.1.1.2.2  riastrad 	int vrefresh_rate;
    737  1.1.1.1.2.2  riastrad 	unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
    738  1.1.1.1.2.2  riastrad 		>> EDID_TIMING_ASPECT_SHIFT;
    739  1.1.1.1.2.2  riastrad 	unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
    740  1.1.1.1.2.2  riastrad 		>> EDID_TIMING_VFREQ_SHIFT;
    741  1.1.1.1.2.2  riastrad 	int timing_level = standard_timing_level(edid);
    742  1.1.1.1.2.2  riastrad 
    743  1.1.1.1.2.2  riastrad 	if (bad_std_timing(t->hsize, t->vfreq_aspect))
    744  1.1.1.1.2.2  riastrad 		return NULL;
    745  1.1.1.1.2.2  riastrad 
    746  1.1.1.1.2.2  riastrad 	/* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
    747  1.1.1.1.2.2  riastrad 	hsize = t->hsize * 8 + 248;
    748  1.1.1.1.2.2  riastrad 	/* vrefresh_rate = vfreq + 60 */
    749  1.1.1.1.2.2  riastrad 	vrefresh_rate = vfreq + 60;
    750  1.1.1.1.2.2  riastrad 	/* the vdisplay is calculated based on the aspect ratio */
    751  1.1.1.1.2.2  riastrad 	if (aspect_ratio == 0) {
    752  1.1.1.1.2.2  riastrad 		if (revision < 3)
    753  1.1.1.1.2.2  riastrad 			vsize = hsize;
    754  1.1.1.1.2.2  riastrad 		else
    755  1.1.1.1.2.2  riastrad 			vsize = (hsize * 10) / 16;
    756  1.1.1.1.2.2  riastrad 	} else if (aspect_ratio == 1)
    757  1.1.1.1.2.2  riastrad 		vsize = (hsize * 3) / 4;
    758  1.1.1.1.2.2  riastrad 	else if (aspect_ratio == 2)
    759  1.1.1.1.2.2  riastrad 		vsize = (hsize * 4) / 5;
    760  1.1.1.1.2.2  riastrad 	else
    761  1.1.1.1.2.2  riastrad 		vsize = (hsize * 9) / 16;
    762  1.1.1.1.2.2  riastrad 
    763  1.1.1.1.2.2  riastrad 	/* HDTV hack, part 1 */
    764  1.1.1.1.2.2  riastrad 	if (vrefresh_rate == 60 &&
    765  1.1.1.1.2.2  riastrad 	    ((hsize == 1360 && vsize == 765) ||
    766  1.1.1.1.2.2  riastrad 	     (hsize == 1368 && vsize == 769))) {
    767  1.1.1.1.2.2  riastrad 		hsize = 1366;
    768  1.1.1.1.2.2  riastrad 		vsize = 768;
    769  1.1.1.1.2.2  riastrad 	}
    770  1.1.1.1.2.2  riastrad 
    771  1.1.1.1.2.2  riastrad 	/*
    772  1.1.1.1.2.2  riastrad 	 * If this connector already has a mode for this size and refresh
    773  1.1.1.1.2.2  riastrad 	 * rate (because it came from detailed or CVT info), use that
    774  1.1.1.1.2.2  riastrad 	 * instead.  This way we don't have to guess at interlace or
    775  1.1.1.1.2.2  riastrad 	 * reduced blanking.
    776  1.1.1.1.2.2  riastrad 	 */
    777  1.1.1.1.2.2  riastrad 	list_for_each_entry(m, &connector->probed_modes, head)
    778  1.1.1.1.2.2  riastrad 		if (m->hdisplay == hsize && m->vdisplay == vsize &&
    779  1.1.1.1.2.2  riastrad 		    drm_mode_vrefresh(m) == vrefresh_rate)
    780  1.1.1.1.2.2  riastrad 			return NULL;
    781  1.1.1.1.2.2  riastrad 
    782  1.1.1.1.2.2  riastrad 	/* HDTV hack, part 2 */
    783  1.1.1.1.2.2  riastrad 	if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
    784  1.1.1.1.2.2  riastrad 		mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
    785  1.1.1.1.2.2  riastrad 				    false);
    786  1.1.1.1.2.2  riastrad 		mode->hdisplay = 1366;
    787  1.1.1.1.2.2  riastrad 		mode->hsync_start = mode->hsync_start - 1;
    788  1.1.1.1.2.2  riastrad 		mode->hsync_end = mode->hsync_end - 1;
    789  1.1.1.1.2.2  riastrad 		return mode;
    790  1.1.1.1.2.2  riastrad 	}
    791  1.1.1.1.2.2  riastrad 
    792  1.1.1.1.2.2  riastrad 	/* check whether it can be found in default mode table */
    793  1.1.1.1.2.2  riastrad 	if (drm_monitor_supports_rb(edid)) {
    794  1.1.1.1.2.2  riastrad 		mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
    795  1.1.1.1.2.2  riastrad 					 true);
    796  1.1.1.1.2.2  riastrad 		if (mode)
    797  1.1.1.1.2.2  riastrad 			return mode;
    798  1.1.1.1.2.2  riastrad 	}
    799  1.1.1.1.2.2  riastrad 	mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
    800  1.1.1.1.2.2  riastrad 	if (mode)
    801  1.1.1.1.2.2  riastrad 		return mode;
    802  1.1.1.1.2.2  riastrad 
    803  1.1.1.1.2.2  riastrad 	/* okay, generate it */
    804  1.1.1.1.2.2  riastrad 	switch (timing_level) {
    805  1.1.1.1.2.2  riastrad 	case LEVEL_DMT:
    806  1.1.1.1.2.2  riastrad 		break;
    807  1.1.1.1.2.2  riastrad 	case LEVEL_GTF:
    808  1.1.1.1.2.2  riastrad 		mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
    809  1.1.1.1.2.2  riastrad 		break;
    810  1.1.1.1.2.2  riastrad 	case LEVEL_GTF2:
    811  1.1.1.1.2.2  riastrad 		/*
    812  1.1.1.1.2.2  riastrad 		 * This is potentially wrong if there's ever a monitor with
    813  1.1.1.1.2.2  riastrad 		 * more than one ranges section, each claiming a different
    814  1.1.1.1.2.2  riastrad 		 * secondary GTF curve.  Please don't do that.
    815  1.1.1.1.2.2  riastrad 		 */
    816  1.1.1.1.2.2  riastrad 		mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
    817  1.1.1.1.2.2  riastrad 		if (!mode)
    818  1.1.1.1.2.2  riastrad 			return NULL;
    819  1.1.1.1.2.2  riastrad 		if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
    820  1.1.1.1.2.2  riastrad 			drm_mode_destroy(dev, mode);
    821  1.1.1.1.2.2  riastrad 			mode = drm_gtf_mode_complex(dev, hsize, vsize,
    822  1.1.1.1.2.2  riastrad 						    vrefresh_rate, 0, 0,
    823  1.1.1.1.2.2  riastrad 						    drm_gtf2_m(edid),
    824  1.1.1.1.2.2  riastrad 						    drm_gtf2_2c(edid),
    825  1.1.1.1.2.2  riastrad 						    drm_gtf2_k(edid),
    826  1.1.1.1.2.2  riastrad 						    drm_gtf2_2j(edid));
    827  1.1.1.1.2.2  riastrad 		}
    828  1.1.1.1.2.2  riastrad 		break;
    829  1.1.1.1.2.2  riastrad 	case LEVEL_CVT:
    830  1.1.1.1.2.2  riastrad 		mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
    831  1.1.1.1.2.2  riastrad 				    false);
    832  1.1.1.1.2.2  riastrad 		break;
    833  1.1.1.1.2.2  riastrad 	}
    834  1.1.1.1.2.2  riastrad 	return mode;
    835  1.1.1.1.2.2  riastrad }
    836  1.1.1.1.2.2  riastrad 
    837  1.1.1.1.2.2  riastrad /*
    838  1.1.1.1.2.2  riastrad  * EDID is delightfully ambiguous about how interlaced modes are to be
    839  1.1.1.1.2.2  riastrad  * encoded.  Our internal representation is of frame height, but some
    840  1.1.1.1.2.2  riastrad  * HDTV detailed timings are encoded as field height.
    841  1.1.1.1.2.2  riastrad  *
    842  1.1.1.1.2.2  riastrad  * The format list here is from CEA, in frame size.  Technically we
    843  1.1.1.1.2.2  riastrad  * should be checking refresh rate too.  Whatever.
    844  1.1.1.1.2.2  riastrad  */
    845  1.1.1.1.2.2  riastrad static void
    846  1.1.1.1.2.2  riastrad drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
    847  1.1.1.1.2.2  riastrad 			    struct detailed_pixel_timing *pt)
    848  1.1.1.1.2.2  riastrad {
    849  1.1.1.1.2.2  riastrad 	int i;
    850  1.1.1.1.2.2  riastrad 	static const struct {
    851  1.1.1.1.2.2  riastrad 		int w, h;
    852  1.1.1.1.2.2  riastrad 	} cea_interlaced[] = {
    853  1.1.1.1.2.2  riastrad 		{ 1920, 1080 },
    854  1.1.1.1.2.2  riastrad 		{  720,  480 },
    855  1.1.1.1.2.2  riastrad 		{ 1440,  480 },
    856  1.1.1.1.2.2  riastrad 		{ 2880,  480 },
    857  1.1.1.1.2.2  riastrad 		{  720,  576 },
    858  1.1.1.1.2.2  riastrad 		{ 1440,  576 },
    859  1.1.1.1.2.2  riastrad 		{ 2880,  576 },
    860  1.1.1.1.2.2  riastrad 	};
    861  1.1.1.1.2.2  riastrad 
    862  1.1.1.1.2.2  riastrad 	if (!(pt->misc & DRM_EDID_PT_INTERLACED))
    863  1.1.1.1.2.2  riastrad 		return;
    864  1.1.1.1.2.2  riastrad 
    865  1.1.1.1.2.2  riastrad 	for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
    866  1.1.1.1.2.2  riastrad 		if ((mode->hdisplay == cea_interlaced[i].w) &&
    867  1.1.1.1.2.2  riastrad 		    (mode->vdisplay == cea_interlaced[i].h / 2)) {
    868  1.1.1.1.2.2  riastrad 			mode->vdisplay *= 2;
    869  1.1.1.1.2.2  riastrad 			mode->vsync_start *= 2;
    870  1.1.1.1.2.2  riastrad 			mode->vsync_end *= 2;
    871  1.1.1.1.2.2  riastrad 			mode->vtotal *= 2;
    872  1.1.1.1.2.2  riastrad 			mode->vtotal |= 1;
    873  1.1.1.1.2.2  riastrad 		}
    874  1.1.1.1.2.2  riastrad 	}
    875  1.1.1.1.2.2  riastrad 
    876  1.1.1.1.2.2  riastrad 	mode->flags |= DRM_MODE_FLAG_INTERLACE;
    877  1.1.1.1.2.2  riastrad }
    878  1.1.1.1.2.2  riastrad 
    879  1.1.1.1.2.2  riastrad /**
    880  1.1.1.1.2.2  riastrad  * drm_mode_detailed - create a new mode from an EDID detailed timing section
    881  1.1.1.1.2.2  riastrad  * @dev: DRM device (needed to create new mode)
    882  1.1.1.1.2.2  riastrad  * @edid: EDID block
    883  1.1.1.1.2.2  riastrad  * @timing: EDID detailed timing info
    884  1.1.1.1.2.2  riastrad  * @quirks: quirks to apply
    885  1.1.1.1.2.2  riastrad  *
    886  1.1.1.1.2.2  riastrad  * An EDID detailed timing block contains enough info for us to create and
    887  1.1.1.1.2.2  riastrad  * return a new struct drm_display_mode.
    888  1.1.1.1.2.2  riastrad  */
    889  1.1.1.1.2.2  riastrad static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
    890  1.1.1.1.2.2  riastrad 						  struct edid *edid,
    891  1.1.1.1.2.2  riastrad 						  struct detailed_timing *timing,
    892  1.1.1.1.2.2  riastrad 						  u32 quirks)
    893  1.1.1.1.2.2  riastrad {
    894  1.1.1.1.2.2  riastrad 	struct drm_display_mode *mode;
    895  1.1.1.1.2.2  riastrad 	struct detailed_pixel_timing *pt = &timing->data.pixel_data;
    896  1.1.1.1.2.2  riastrad 	unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
    897  1.1.1.1.2.2  riastrad 	unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
    898  1.1.1.1.2.2  riastrad 	unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
    899  1.1.1.1.2.2  riastrad 	unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
    900  1.1.1.1.2.2  riastrad 	unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
    901  1.1.1.1.2.2  riastrad 	unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
    902  1.1.1.1.2.2  riastrad 	unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
    903  1.1.1.1.2.2  riastrad 	unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
    904  1.1.1.1.2.2  riastrad 
    905  1.1.1.1.2.2  riastrad 	/* ignore tiny modes */
    906  1.1.1.1.2.2  riastrad 	if (hactive < 64 || vactive < 64)
    907  1.1.1.1.2.2  riastrad 		return NULL;
    908  1.1.1.1.2.2  riastrad 
    909  1.1.1.1.2.2  riastrad 	if (pt->misc & DRM_EDID_PT_STEREO) {
    910  1.1.1.1.2.2  riastrad 		printk(KERN_WARNING "stereo mode not supported\n");
    911  1.1.1.1.2.2  riastrad 		return NULL;
    912  1.1.1.1.2.2  riastrad 	}
    913  1.1.1.1.2.2  riastrad 	if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
    914  1.1.1.1.2.2  riastrad 		printk(KERN_WARNING "composite sync not supported\n");
    915  1.1.1.1.2.2  riastrad 	}
    916  1.1.1.1.2.2  riastrad 
    917  1.1.1.1.2.2  riastrad 	/* it is incorrect if hsync/vsync width is zero */
    918  1.1.1.1.2.2  riastrad 	if (!hsync_pulse_width || !vsync_pulse_width) {
    919  1.1.1.1.2.2  riastrad 		DRM_DEBUG_KMS("Incorrect Detailed timing. "
    920  1.1.1.1.2.2  riastrad 				"Wrong Hsync/Vsync pulse width\n");
    921  1.1.1.1.2.2  riastrad 		return NULL;
    922  1.1.1.1.2.2  riastrad 	}
    923  1.1.1.1.2.2  riastrad 
    924  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
    925  1.1.1.1.2.2  riastrad 		mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
    926  1.1.1.1.2.2  riastrad 		if (!mode)
    927  1.1.1.1.2.2  riastrad 			return NULL;
    928  1.1.1.1.2.2  riastrad 
    929  1.1.1.1.2.2  riastrad 		goto set_size;
    930  1.1.1.1.2.2  riastrad 	}
    931  1.1.1.1.2.2  riastrad 
    932  1.1.1.1.2.2  riastrad 	mode = drm_mode_create(dev);
    933  1.1.1.1.2.2  riastrad 	if (!mode)
    934  1.1.1.1.2.2  riastrad 		return NULL;
    935  1.1.1.1.2.2  riastrad 
    936  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
    937  1.1.1.1.2.2  riastrad 		timing->pixel_clock = cpu_to_le16(1088);
    938  1.1.1.1.2.2  riastrad 
    939  1.1.1.1.2.2  riastrad 	mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
    940  1.1.1.1.2.2  riastrad 
    941  1.1.1.1.2.2  riastrad 	mode->hdisplay = hactive;
    942  1.1.1.1.2.2  riastrad 	mode->hsync_start = mode->hdisplay + hsync_offset;
    943  1.1.1.1.2.2  riastrad 	mode->hsync_end = mode->hsync_start + hsync_pulse_width;
    944  1.1.1.1.2.2  riastrad 	mode->htotal = mode->hdisplay + hblank;
    945  1.1.1.1.2.2  riastrad 
    946  1.1.1.1.2.2  riastrad 	mode->vdisplay = vactive;
    947  1.1.1.1.2.2  riastrad 	mode->vsync_start = mode->vdisplay + vsync_offset;
    948  1.1.1.1.2.2  riastrad 	mode->vsync_end = mode->vsync_start + vsync_pulse_width;
    949  1.1.1.1.2.2  riastrad 	mode->vtotal = mode->vdisplay + vblank;
    950  1.1.1.1.2.2  riastrad 
    951  1.1.1.1.2.2  riastrad 	/* Some EDIDs have bogus h/vtotal values */
    952  1.1.1.1.2.2  riastrad 	if (mode->hsync_end > mode->htotal)
    953  1.1.1.1.2.2  riastrad 		mode->htotal = mode->hsync_end + 1;
    954  1.1.1.1.2.2  riastrad 	if (mode->vsync_end > mode->vtotal)
    955  1.1.1.1.2.2  riastrad 		mode->vtotal = mode->vsync_end + 1;
    956  1.1.1.1.2.2  riastrad 
    957  1.1.1.1.2.2  riastrad 	drm_mode_do_interlace_quirk(mode, pt);
    958  1.1.1.1.2.2  riastrad 
    959  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
    960  1.1.1.1.2.2  riastrad 		pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
    961  1.1.1.1.2.2  riastrad 	}
    962  1.1.1.1.2.2  riastrad 
    963  1.1.1.1.2.2  riastrad 	mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
    964  1.1.1.1.2.2  riastrad 		DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
    965  1.1.1.1.2.2  riastrad 	mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
    966  1.1.1.1.2.2  riastrad 		DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
    967  1.1.1.1.2.2  riastrad 
    968  1.1.1.1.2.2  riastrad set_size:
    969  1.1.1.1.2.2  riastrad 	mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
    970  1.1.1.1.2.2  riastrad 	mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
    971  1.1.1.1.2.2  riastrad 
    972  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
    973  1.1.1.1.2.2  riastrad 		mode->width_mm *= 10;
    974  1.1.1.1.2.2  riastrad 		mode->height_mm *= 10;
    975  1.1.1.1.2.2  riastrad 	}
    976  1.1.1.1.2.2  riastrad 
    977  1.1.1.1.2.2  riastrad 	if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
    978  1.1.1.1.2.2  riastrad 		mode->width_mm = edid->width_cm * 10;
    979  1.1.1.1.2.2  riastrad 		mode->height_mm = edid->height_cm * 10;
    980  1.1.1.1.2.2  riastrad 	}
    981  1.1.1.1.2.2  riastrad 
    982  1.1.1.1.2.2  riastrad 	mode->type = DRM_MODE_TYPE_DRIVER;
    983  1.1.1.1.2.2  riastrad 	drm_mode_set_name(mode);
    984  1.1.1.1.2.2  riastrad 
    985  1.1.1.1.2.2  riastrad 	return mode;
    986  1.1.1.1.2.2  riastrad }
    987  1.1.1.1.2.2  riastrad 
    988  1.1.1.1.2.2  riastrad static bool
    989  1.1.1.1.2.2  riastrad mode_in_hsync_range(const struct drm_display_mode *mode,
    990  1.1.1.1.2.2  riastrad 		    struct edid *edid, u8 *t)
    991  1.1.1.1.2.2  riastrad {
    992  1.1.1.1.2.2  riastrad 	int hsync, hmin, hmax;
    993  1.1.1.1.2.2  riastrad 
    994  1.1.1.1.2.2  riastrad 	hmin = t[7];
    995  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4)
    996  1.1.1.1.2.2  riastrad 	    hmin += ((t[4] & 0x04) ? 255 : 0);
    997  1.1.1.1.2.2  riastrad 	hmax = t[8];
    998  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4)
    999  1.1.1.1.2.2  riastrad 	    hmax += ((t[4] & 0x08) ? 255 : 0);
   1000  1.1.1.1.2.2  riastrad 	hsync = drm_mode_hsync(mode);
   1001  1.1.1.1.2.2  riastrad 
   1002  1.1.1.1.2.2  riastrad 	return (hsync <= hmax && hsync >= hmin);
   1003  1.1.1.1.2.2  riastrad }
   1004  1.1.1.1.2.2  riastrad 
   1005  1.1.1.1.2.2  riastrad static bool
   1006  1.1.1.1.2.2  riastrad mode_in_vsync_range(const struct drm_display_mode *mode,
   1007  1.1.1.1.2.2  riastrad 		    struct edid *edid, u8 *t)
   1008  1.1.1.1.2.2  riastrad {
   1009  1.1.1.1.2.2  riastrad 	int vsync, vmin, vmax;
   1010  1.1.1.1.2.2  riastrad 
   1011  1.1.1.1.2.2  riastrad 	vmin = t[5];
   1012  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4)
   1013  1.1.1.1.2.2  riastrad 	    vmin += ((t[4] & 0x01) ? 255 : 0);
   1014  1.1.1.1.2.2  riastrad 	vmax = t[6];
   1015  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4)
   1016  1.1.1.1.2.2  riastrad 	    vmax += ((t[4] & 0x02) ? 255 : 0);
   1017  1.1.1.1.2.2  riastrad 	vsync = drm_mode_vrefresh(mode);
   1018  1.1.1.1.2.2  riastrad 
   1019  1.1.1.1.2.2  riastrad 	return (vsync <= vmax && vsync >= vmin);
   1020  1.1.1.1.2.2  riastrad }
   1021  1.1.1.1.2.2  riastrad 
   1022  1.1.1.1.2.2  riastrad static u32
   1023  1.1.1.1.2.2  riastrad range_pixel_clock(struct edid *edid, u8 *t)
   1024  1.1.1.1.2.2  riastrad {
   1025  1.1.1.1.2.2  riastrad 	/* unspecified */
   1026  1.1.1.1.2.2  riastrad 	if (t[9] == 0 || t[9] == 255)
   1027  1.1.1.1.2.2  riastrad 		return 0;
   1028  1.1.1.1.2.2  riastrad 
   1029  1.1.1.1.2.2  riastrad 	/* 1.4 with CVT support gives us real precision, yay */
   1030  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4 && t[10] == 0x04)
   1031  1.1.1.1.2.2  riastrad 		return (t[9] * 10000) - ((t[12] >> 2) * 250);
   1032  1.1.1.1.2.2  riastrad 
   1033  1.1.1.1.2.2  riastrad 	/* 1.3 is pathetic, so fuzz up a bit */
   1034  1.1.1.1.2.2  riastrad 	return t[9] * 10000 + 5001;
   1035  1.1.1.1.2.2  riastrad }
   1036  1.1.1.1.2.2  riastrad 
   1037  1.1.1.1.2.2  riastrad static bool
   1038  1.1.1.1.2.2  riastrad mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
   1039  1.1.1.1.2.2  riastrad 	      struct detailed_timing *timing)
   1040  1.1.1.1.2.2  riastrad {
   1041  1.1.1.1.2.2  riastrad 	u32 max_clock;
   1042  1.1.1.1.2.2  riastrad 	u8 *t = (u8 *)timing;
   1043  1.1.1.1.2.2  riastrad 
   1044  1.1.1.1.2.2  riastrad 	if (!mode_in_hsync_range(mode, edid, t))
   1045  1.1.1.1.2.2  riastrad 		return false;
   1046  1.1.1.1.2.2  riastrad 
   1047  1.1.1.1.2.2  riastrad 	if (!mode_in_vsync_range(mode, edid, t))
   1048  1.1.1.1.2.2  riastrad 		return false;
   1049  1.1.1.1.2.2  riastrad 
   1050  1.1.1.1.2.2  riastrad 	if ((max_clock = range_pixel_clock(edid, t)))
   1051  1.1.1.1.2.2  riastrad 		if (mode->clock > max_clock)
   1052  1.1.1.1.2.2  riastrad 			return false;
   1053  1.1.1.1.2.2  riastrad 
   1054  1.1.1.1.2.2  riastrad 	/* 1.4 max horizontal check */
   1055  1.1.1.1.2.2  riastrad 	if (edid->revision >= 4 && t[10] == 0x04)
   1056  1.1.1.1.2.2  riastrad 		if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
   1057  1.1.1.1.2.2  riastrad 			return false;
   1058  1.1.1.1.2.2  riastrad 
   1059  1.1.1.1.2.2  riastrad 	if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
   1060  1.1.1.1.2.2  riastrad 		return false;
   1061  1.1.1.1.2.2  riastrad 
   1062  1.1.1.1.2.2  riastrad 	return true;
   1063  1.1.1.1.2.2  riastrad }
   1064  1.1.1.1.2.2  riastrad 
   1065  1.1.1.1.2.2  riastrad static bool valid_inferred_mode(const struct drm_connector *connector,
   1066  1.1.1.1.2.2  riastrad 				const struct drm_display_mode *mode)
   1067  1.1.1.1.2.2  riastrad {
   1068  1.1.1.1.2.2  riastrad 	struct drm_display_mode *m;
   1069  1.1.1.1.2.2  riastrad 	bool ok = false;
   1070  1.1.1.1.2.2  riastrad 
   1071  1.1.1.1.2.2  riastrad 	list_for_each_entry(m, &connector->probed_modes, head) {
   1072  1.1.1.1.2.2  riastrad 		if (mode->hdisplay == m->hdisplay &&
   1073  1.1.1.1.2.2  riastrad 		    mode->vdisplay == m->vdisplay &&
   1074  1.1.1.1.2.2  riastrad 		    drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
   1075  1.1.1.1.2.2  riastrad 			return false; /* duplicated */
   1076  1.1.1.1.2.2  riastrad 		if (mode->hdisplay <= m->hdisplay &&
   1077  1.1.1.1.2.2  riastrad 		    mode->vdisplay <= m->vdisplay)
   1078  1.1.1.1.2.2  riastrad 			ok = true;
   1079  1.1.1.1.2.2  riastrad 	}
   1080  1.1.1.1.2.2  riastrad 	return ok;
   1081  1.1.1.1.2.2  riastrad }
   1082  1.1.1.1.2.2  riastrad 
   1083  1.1.1.1.2.2  riastrad static int
   1084  1.1.1.1.2.2  riastrad drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
   1085  1.1.1.1.2.2  riastrad 			struct detailed_timing *timing)
   1086  1.1.1.1.2.2  riastrad {
   1087  1.1.1.1.2.2  riastrad 	int i, modes = 0;
   1088  1.1.1.1.2.2  riastrad 	struct drm_display_mode *newmode;
   1089  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   1090  1.1.1.1.2.2  riastrad 
   1091  1.1.1.1.2.2  riastrad 	for (i = 0; i < drm_num_dmt_modes; i++) {
   1092  1.1.1.1.2.2  riastrad 		if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
   1093  1.1.1.1.2.2  riastrad 		    valid_inferred_mode(connector, drm_dmt_modes + i)) {
   1094  1.1.1.1.2.2  riastrad 			newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
   1095  1.1.1.1.2.2  riastrad 			if (newmode) {
   1096  1.1.1.1.2.2  riastrad 				drm_mode_probed_add(connector, newmode);
   1097  1.1.1.1.2.2  riastrad 				modes++;
   1098  1.1.1.1.2.2  riastrad 			}
   1099  1.1.1.1.2.2  riastrad 		}
   1100  1.1.1.1.2.2  riastrad 	}
   1101  1.1.1.1.2.2  riastrad 
   1102  1.1.1.1.2.2  riastrad 	return modes;
   1103  1.1.1.1.2.2  riastrad }
   1104  1.1.1.1.2.2  riastrad 
   1105  1.1.1.1.2.2  riastrad /* fix up 1366x768 mode from 1368x768;
   1106  1.1.1.1.2.2  riastrad  * GFT/CVT can't express 1366 width which isn't dividable by 8
   1107  1.1.1.1.2.2  riastrad  */
   1108  1.1.1.1.2.2  riastrad static void fixup_mode_1366x768(struct drm_display_mode *mode)
   1109  1.1.1.1.2.2  riastrad {
   1110  1.1.1.1.2.2  riastrad 	if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
   1111  1.1.1.1.2.2  riastrad 		mode->hdisplay = 1366;
   1112  1.1.1.1.2.2  riastrad 		mode->hsync_start--;
   1113  1.1.1.1.2.2  riastrad 		mode->hsync_end--;
   1114  1.1.1.1.2.2  riastrad 		drm_mode_set_name(mode);
   1115  1.1.1.1.2.2  riastrad 	}
   1116  1.1.1.1.2.2  riastrad }
   1117  1.1.1.1.2.2  riastrad 
   1118  1.1.1.1.2.2  riastrad static int
   1119  1.1.1.1.2.2  riastrad drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
   1120  1.1.1.1.2.2  riastrad 			struct detailed_timing *timing)
   1121  1.1.1.1.2.2  riastrad {
   1122  1.1.1.1.2.2  riastrad 	int i, modes = 0;
   1123  1.1.1.1.2.2  riastrad 	struct drm_display_mode *newmode;
   1124  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   1125  1.1.1.1.2.2  riastrad 
   1126  1.1.1.1.2.2  riastrad 	for (i = 0; i < num_extra_modes; i++) {
   1127  1.1.1.1.2.2  riastrad 		const struct minimode *m = &extra_modes[i];
   1128  1.1.1.1.2.2  riastrad 		newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
   1129  1.1.1.1.2.2  riastrad 		if (!newmode)
   1130  1.1.1.1.2.2  riastrad 			return modes;
   1131  1.1.1.1.2.2  riastrad 
   1132  1.1.1.1.2.2  riastrad 		fixup_mode_1366x768(newmode);
   1133  1.1.1.1.2.2  riastrad 		if (!mode_in_range(newmode, edid, timing) ||
   1134  1.1.1.1.2.2  riastrad 		    !valid_inferred_mode(connector, newmode)) {
   1135  1.1.1.1.2.2  riastrad 			drm_mode_destroy(dev, newmode);
   1136  1.1.1.1.2.2  riastrad 			continue;
   1137  1.1.1.1.2.2  riastrad 		}
   1138  1.1.1.1.2.2  riastrad 
   1139  1.1.1.1.2.2  riastrad 		drm_mode_probed_add(connector, newmode);
   1140  1.1.1.1.2.2  riastrad 		modes++;
   1141  1.1.1.1.2.2  riastrad 	}
   1142  1.1.1.1.2.2  riastrad 
   1143  1.1.1.1.2.2  riastrad 	return modes;
   1144  1.1.1.1.2.2  riastrad }
   1145  1.1.1.1.2.2  riastrad 
   1146  1.1.1.1.2.2  riastrad static int
   1147  1.1.1.1.2.2  riastrad drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
   1148  1.1.1.1.2.2  riastrad 			struct detailed_timing *timing)
   1149  1.1.1.1.2.2  riastrad {
   1150  1.1.1.1.2.2  riastrad 	int i, modes = 0;
   1151  1.1.1.1.2.2  riastrad 	struct drm_display_mode *newmode;
   1152  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   1153  1.1.1.1.2.2  riastrad 	bool rb = drm_monitor_supports_rb(edid);
   1154  1.1.1.1.2.2  riastrad 
   1155  1.1.1.1.2.2  riastrad 	for (i = 0; i < num_extra_modes; i++) {
   1156  1.1.1.1.2.2  riastrad 		const struct minimode *m = &extra_modes[i];
   1157  1.1.1.1.2.2  riastrad 		newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
   1158  1.1.1.1.2.2  riastrad 		if (!newmode)
   1159  1.1.1.1.2.2  riastrad 			return modes;
   1160  1.1.1.1.2.2  riastrad 
   1161  1.1.1.1.2.2  riastrad 		fixup_mode_1366x768(newmode);
   1162  1.1.1.1.2.2  riastrad 		if (!mode_in_range(newmode, edid, timing) ||
   1163  1.1.1.1.2.2  riastrad 		    !valid_inferred_mode(connector, newmode)) {
   1164  1.1.1.1.2.2  riastrad 			drm_mode_destroy(dev, newmode);
   1165  1.1.1.1.2.2  riastrad 			continue;
   1166  1.1.1.1.2.2  riastrad 		}
   1167  1.1.1.1.2.2  riastrad 
   1168  1.1.1.1.2.2  riastrad 		drm_mode_probed_add(connector, newmode);
   1169  1.1.1.1.2.2  riastrad 		modes++;
   1170  1.1.1.1.2.2  riastrad 	}
   1171  1.1.1.1.2.2  riastrad 
   1172  1.1.1.1.2.2  riastrad 	return modes;
   1173  1.1.1.1.2.2  riastrad }
   1174  1.1.1.1.2.2  riastrad 
   1175  1.1.1.1.2.2  riastrad static void
   1176  1.1.1.1.2.2  riastrad do_inferred_modes(struct detailed_timing *timing, void *c)
   1177  1.1.1.1.2.2  riastrad {
   1178  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure *closure = c;
   1179  1.1.1.1.2.2  riastrad 	struct detailed_non_pixel *data = &timing->data.other_data;
   1180  1.1.1.1.2.2  riastrad 	struct detailed_data_monitor_range *range = &data->data.range;
   1181  1.1.1.1.2.2  riastrad 
   1182  1.1.1.1.2.2  riastrad 	if (data->type != EDID_DETAIL_MONITOR_RANGE)
   1183  1.1.1.1.2.2  riastrad 		return;
   1184  1.1.1.1.2.2  riastrad 
   1185  1.1.1.1.2.2  riastrad 	closure->modes += drm_dmt_modes_for_range(closure->connector,
   1186  1.1.1.1.2.2  riastrad 						  closure->edid,
   1187  1.1.1.1.2.2  riastrad 						  timing);
   1188  1.1.1.1.2.2  riastrad 
   1189  1.1.1.1.2.2  riastrad 	if (!version_greater(closure->edid, 1, 1))
   1190  1.1.1.1.2.2  riastrad 		return; /* GTF not defined yet */
   1191  1.1.1.1.2.2  riastrad 
   1192  1.1.1.1.2.2  riastrad 	switch (range->flags) {
   1193  1.1.1.1.2.2  riastrad 	case 0x02: /* secondary gtf, XXX could do more */
   1194  1.1.1.1.2.2  riastrad 	case 0x00: /* default gtf */
   1195  1.1.1.1.2.2  riastrad 		closure->modes += drm_gtf_modes_for_range(closure->connector,
   1196  1.1.1.1.2.2  riastrad 							  closure->edid,
   1197  1.1.1.1.2.2  riastrad 							  timing);
   1198  1.1.1.1.2.2  riastrad 		break;
   1199  1.1.1.1.2.2  riastrad 	case 0x04: /* cvt, only in 1.4+ */
   1200  1.1.1.1.2.2  riastrad 		if (!version_greater(closure->edid, 1, 3))
   1201  1.1.1.1.2.2  riastrad 			break;
   1202  1.1.1.1.2.2  riastrad 
   1203  1.1.1.1.2.2  riastrad 		closure->modes += drm_cvt_modes_for_range(closure->connector,
   1204  1.1.1.1.2.2  riastrad 							  closure->edid,
   1205  1.1.1.1.2.2  riastrad 							  timing);
   1206  1.1.1.1.2.2  riastrad 		break;
   1207  1.1.1.1.2.2  riastrad 	case 0x01: /* just the ranges, no formula */
   1208  1.1.1.1.2.2  riastrad 	default:
   1209  1.1.1.1.2.2  riastrad 		break;
   1210  1.1.1.1.2.2  riastrad 	}
   1211  1.1.1.1.2.2  riastrad }
   1212  1.1.1.1.2.2  riastrad 
   1213  1.1.1.1.2.2  riastrad static int
   1214  1.1.1.1.2.2  riastrad add_inferred_modes(struct drm_connector *connector, struct edid *edid)
   1215  1.1.1.1.2.2  riastrad {
   1216  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure closure = {
   1217  1.1.1.1.2.2  riastrad 		connector, edid, 0, 0, 0
   1218  1.1.1.1.2.2  riastrad 	};
   1219  1.1.1.1.2.2  riastrad 
   1220  1.1.1.1.2.2  riastrad 	if (version_greater(edid, 1, 0))
   1221  1.1.1.1.2.2  riastrad 		drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
   1222  1.1.1.1.2.2  riastrad 					    &closure);
   1223  1.1.1.1.2.2  riastrad 
   1224  1.1.1.1.2.2  riastrad 	return closure.modes;
   1225  1.1.1.1.2.2  riastrad }
   1226  1.1.1.1.2.2  riastrad 
   1227  1.1.1.1.2.2  riastrad static int
   1228  1.1.1.1.2.2  riastrad drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
   1229  1.1.1.1.2.2  riastrad {
   1230  1.1.1.1.2.2  riastrad 	int i, j, m, modes = 0;
   1231  1.1.1.1.2.2  riastrad 	struct drm_display_mode *mode;
   1232  1.1.1.1.2.2  riastrad 	u8 *est = ((u8 *)timing) + 5;
   1233  1.1.1.1.2.2  riastrad 
   1234  1.1.1.1.2.2  riastrad 	for (i = 0; i < 6; i++) {
   1235  1.1.1.1.2.2  riastrad 		for (j = 7; j > 0; j--) {
   1236  1.1.1.1.2.2  riastrad 			m = (i * 8) + (7 - j);
   1237  1.1.1.1.2.2  riastrad 			if (m >= ARRAY_SIZE(est3_modes))
   1238  1.1.1.1.2.2  riastrad 				break;
   1239  1.1.1.1.2.2  riastrad 			if (est[i] & (1 << j)) {
   1240  1.1.1.1.2.2  riastrad 				mode = drm_mode_find_dmt(connector->dev,
   1241  1.1.1.1.2.2  riastrad 							 est3_modes[m].w,
   1242  1.1.1.1.2.2  riastrad 							 est3_modes[m].h,
   1243  1.1.1.1.2.2  riastrad 							 est3_modes[m].r,
   1244  1.1.1.1.2.2  riastrad 							 est3_modes[m].rb);
   1245  1.1.1.1.2.2  riastrad 				if (mode) {
   1246  1.1.1.1.2.2  riastrad 					drm_mode_probed_add(connector, mode);
   1247  1.1.1.1.2.2  riastrad 					modes++;
   1248  1.1.1.1.2.2  riastrad 				}
   1249  1.1.1.1.2.2  riastrad 			}
   1250  1.1.1.1.2.2  riastrad 		}
   1251  1.1.1.1.2.2  riastrad 	}
   1252  1.1.1.1.2.2  riastrad 
   1253  1.1.1.1.2.2  riastrad 	return modes;
   1254  1.1.1.1.2.2  riastrad }
   1255  1.1.1.1.2.2  riastrad 
   1256  1.1.1.1.2.2  riastrad static void
   1257  1.1.1.1.2.2  riastrad do_established_modes(struct detailed_timing *timing, void *c)
   1258  1.1.1.1.2.2  riastrad {
   1259  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure *closure = c;
   1260  1.1.1.1.2.2  riastrad 	struct detailed_non_pixel *data = &timing->data.other_data;
   1261  1.1.1.1.2.2  riastrad 
   1262  1.1.1.1.2.2  riastrad 	if (data->type == EDID_DETAIL_EST_TIMINGS)
   1263  1.1.1.1.2.2  riastrad 		closure->modes += drm_est3_modes(closure->connector, timing);
   1264  1.1.1.1.2.2  riastrad }
   1265  1.1.1.1.2.2  riastrad 
   1266  1.1.1.1.2.2  riastrad /**
   1267  1.1.1.1.2.2  riastrad  * add_established_modes - get est. modes from EDID and add them
   1268  1.1.1.1.2.2  riastrad  * @edid: EDID block to scan
   1269  1.1.1.1.2.2  riastrad  *
   1270  1.1.1.1.2.2  riastrad  * Each EDID block contains a bitmap of the supported "established modes" list
   1271  1.1.1.1.2.2  riastrad  * (defined above).  Tease them out and add them to the global modes list.
   1272  1.1.1.1.2.2  riastrad  */
   1273  1.1.1.1.2.2  riastrad static int
   1274  1.1.1.1.2.2  riastrad add_established_modes(struct drm_connector *connector, struct edid *edid)
   1275  1.1.1.1.2.2  riastrad {
   1276  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   1277  1.1.1.1.2.2  riastrad 	unsigned long est_bits = edid->established_timings.t1 |
   1278  1.1.1.1.2.2  riastrad 		(edid->established_timings.t2 << 8) |
   1279  1.1.1.1.2.2  riastrad 		((edid->established_timings.mfg_rsvd & 0x80) << 9);
   1280  1.1.1.1.2.2  riastrad 	int i, modes = 0;
   1281  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure closure = {
   1282  1.1.1.1.2.2  riastrad 		connector, edid, 0, 0, 0
   1283  1.1.1.1.2.2  riastrad 	};
   1284  1.1.1.1.2.2  riastrad 
   1285  1.1.1.1.2.2  riastrad 	for (i = 0; i <= EDID_EST_TIMINGS; i++) {
   1286  1.1.1.1.2.2  riastrad 		if (est_bits & (1<<i)) {
   1287  1.1.1.1.2.2  riastrad 			struct drm_display_mode *newmode;
   1288  1.1.1.1.2.2  riastrad 			newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
   1289  1.1.1.1.2.2  riastrad 			if (newmode) {
   1290  1.1.1.1.2.2  riastrad 				drm_mode_probed_add(connector, newmode);
   1291  1.1.1.1.2.2  riastrad 				modes++;
   1292  1.1.1.1.2.2  riastrad 			}
   1293  1.1.1.1.2.2  riastrad 		}
   1294  1.1.1.1.2.2  riastrad 	}
   1295  1.1.1.1.2.2  riastrad 
   1296  1.1.1.1.2.2  riastrad 	if (version_greater(edid, 1, 0))
   1297  1.1.1.1.2.2  riastrad 		    drm_for_each_detailed_block((u8 *)edid,
   1298  1.1.1.1.2.2  riastrad 						do_established_modes, &closure);
   1299  1.1.1.1.2.2  riastrad 
   1300  1.1.1.1.2.2  riastrad 	return modes + closure.modes;
   1301  1.1.1.1.2.2  riastrad }
   1302  1.1.1.1.2.2  riastrad 
   1303  1.1.1.1.2.2  riastrad static void
   1304  1.1.1.1.2.2  riastrad do_standard_modes(struct detailed_timing *timing, void *c)
   1305  1.1.1.1.2.2  riastrad {
   1306  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure *closure = c;
   1307  1.1.1.1.2.2  riastrad 	struct detailed_non_pixel *data = &timing->data.other_data;
   1308  1.1.1.1.2.2  riastrad 	struct drm_connector *connector = closure->connector;
   1309  1.1.1.1.2.2  riastrad 	struct edid *edid = closure->edid;
   1310  1.1.1.1.2.2  riastrad 
   1311  1.1.1.1.2.2  riastrad 	if (data->type == EDID_DETAIL_STD_MODES) {
   1312  1.1.1.1.2.2  riastrad 		int i;
   1313  1.1.1.1.2.2  riastrad 		for (i = 0; i < 6; i++) {
   1314  1.1.1.1.2.2  riastrad 			struct std_timing *std;
   1315  1.1.1.1.2.2  riastrad 			struct drm_display_mode *newmode;
   1316  1.1.1.1.2.2  riastrad 
   1317  1.1.1.1.2.2  riastrad 			std = &data->data.timings[i];
   1318  1.1.1.1.2.2  riastrad 			newmode = drm_mode_std(connector, edid, std,
   1319  1.1.1.1.2.2  riastrad 					       edid->revision);
   1320  1.1.1.1.2.2  riastrad 			if (newmode) {
   1321  1.1.1.1.2.2  riastrad 				drm_mode_probed_add(connector, newmode);
   1322  1.1.1.1.2.2  riastrad 				closure->modes++;
   1323  1.1.1.1.2.2  riastrad 			}
   1324  1.1.1.1.2.2  riastrad 		}
   1325  1.1.1.1.2.2  riastrad 	}
   1326  1.1.1.1.2.2  riastrad }
   1327  1.1.1.1.2.2  riastrad 
   1328  1.1.1.1.2.2  riastrad /**
   1329  1.1.1.1.2.2  riastrad  * add_standard_modes - get std. modes from EDID and add them
   1330  1.1.1.1.2.2  riastrad  * @edid: EDID block to scan
   1331  1.1.1.1.2.2  riastrad  *
   1332  1.1.1.1.2.2  riastrad  * Standard modes can be calculated using the appropriate standard (DMT,
   1333  1.1.1.1.2.2  riastrad  * GTF or CVT. Grab them from @edid and add them to the list.
   1334  1.1.1.1.2.2  riastrad  */
   1335  1.1.1.1.2.2  riastrad static int
   1336  1.1.1.1.2.2  riastrad add_standard_modes(struct drm_connector *connector, struct edid *edid)
   1337  1.1.1.1.2.2  riastrad {
   1338  1.1.1.1.2.2  riastrad 	int i, modes = 0;
   1339  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure closure = {
   1340  1.1.1.1.2.2  riastrad 		connector, edid, 0, 0, 0
   1341  1.1.1.1.2.2  riastrad 	};
   1342  1.1.1.1.2.2  riastrad 
   1343  1.1.1.1.2.2  riastrad 	for (i = 0; i < EDID_STD_TIMINGS; i++) {
   1344  1.1.1.1.2.2  riastrad 		struct drm_display_mode *newmode;
   1345  1.1.1.1.2.2  riastrad 
   1346  1.1.1.1.2.2  riastrad 		newmode = drm_mode_std(connector, edid,
   1347  1.1.1.1.2.2  riastrad 				       &edid->standard_timings[i],
   1348  1.1.1.1.2.2  riastrad 				       edid->revision);
   1349  1.1.1.1.2.2  riastrad 		if (newmode) {
   1350  1.1.1.1.2.2  riastrad 			drm_mode_probed_add(connector, newmode);
   1351  1.1.1.1.2.2  riastrad 			modes++;
   1352  1.1.1.1.2.2  riastrad 		}
   1353  1.1.1.1.2.2  riastrad 	}
   1354  1.1.1.1.2.2  riastrad 
   1355  1.1.1.1.2.2  riastrad 	if (version_greater(edid, 1, 0))
   1356  1.1.1.1.2.2  riastrad 		drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
   1357  1.1.1.1.2.2  riastrad 					    &closure);
   1358  1.1.1.1.2.2  riastrad 
   1359  1.1.1.1.2.2  riastrad 	/* XXX should also look for standard codes in VTB blocks */
   1360  1.1.1.1.2.2  riastrad 
   1361  1.1.1.1.2.2  riastrad 	return modes + closure.modes;
   1362  1.1.1.1.2.2  riastrad }
   1363  1.1.1.1.2.2  riastrad 
   1364  1.1.1.1.2.2  riastrad static int drm_cvt_modes(struct drm_connector *connector,
   1365  1.1.1.1.2.2  riastrad 			 struct detailed_timing *timing)
   1366  1.1.1.1.2.2  riastrad {
   1367  1.1.1.1.2.2  riastrad 	int i, j, modes = 0;
   1368  1.1.1.1.2.2  riastrad 	struct drm_display_mode *newmode;
   1369  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   1370  1.1.1.1.2.2  riastrad 	struct cvt_timing *cvt;
   1371  1.1.1.1.2.2  riastrad 	const int rates[] = { 60, 85, 75, 60, 50 };
   1372  1.1.1.1.2.2  riastrad 	const u8 empty[3] = { 0, 0, 0 };
   1373  1.1.1.1.2.2  riastrad 
   1374  1.1.1.1.2.2  riastrad 	for (i = 0; i < 4; i++) {
   1375  1.1.1.1.2.2  riastrad 		int uninitialized_var(width), height;
   1376  1.1.1.1.2.2  riastrad 		cvt = &(timing->data.other_data.data.cvt[i]);
   1377  1.1.1.1.2.2  riastrad 
   1378  1.1.1.1.2.2  riastrad 		if (!memcmp(cvt->code, empty, 3))
   1379  1.1.1.1.2.2  riastrad 			continue;
   1380  1.1.1.1.2.2  riastrad 
   1381  1.1.1.1.2.2  riastrad 		height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
   1382  1.1.1.1.2.2  riastrad 		switch (cvt->code[1] & 0x0c) {
   1383  1.1.1.1.2.2  riastrad 		case 0x00:
   1384  1.1.1.1.2.2  riastrad 			width = height * 4 / 3;
   1385  1.1.1.1.2.2  riastrad 			break;
   1386  1.1.1.1.2.2  riastrad 		case 0x04:
   1387  1.1.1.1.2.2  riastrad 			width = height * 16 / 9;
   1388  1.1.1.1.2.2  riastrad 			break;
   1389  1.1.1.1.2.2  riastrad 		case 0x08:
   1390  1.1.1.1.2.2  riastrad 			width = height * 16 / 10;
   1391  1.1.1.1.2.2  riastrad 			break;
   1392  1.1.1.1.2.2  riastrad 		case 0x0c:
   1393  1.1.1.1.2.2  riastrad 			width = height * 15 / 9;
   1394  1.1.1.1.2.2  riastrad 			break;
   1395  1.1.1.1.2.2  riastrad 		}
   1396  1.1.1.1.2.2  riastrad 
   1397  1.1.1.1.2.2  riastrad 		for (j = 1; j < 5; j++) {
   1398  1.1.1.1.2.2  riastrad 			if (cvt->code[2] & (1 << j)) {
   1399  1.1.1.1.2.2  riastrad 				newmode = drm_cvt_mode(dev, width, height,
   1400  1.1.1.1.2.2  riastrad 						       rates[j], j == 0,
   1401  1.1.1.1.2.2  riastrad 						       false, false);
   1402  1.1.1.1.2.2  riastrad 				if (newmode) {
   1403  1.1.1.1.2.2  riastrad 					drm_mode_probed_add(connector, newmode);
   1404  1.1.1.1.2.2  riastrad 					modes++;
   1405  1.1.1.1.2.2  riastrad 				}
   1406  1.1.1.1.2.2  riastrad 			}
   1407  1.1.1.1.2.2  riastrad 		}
   1408  1.1.1.1.2.2  riastrad 	}
   1409  1.1.1.1.2.2  riastrad 
   1410  1.1.1.1.2.2  riastrad 	return modes;
   1411  1.1.1.1.2.2  riastrad }
   1412  1.1.1.1.2.2  riastrad 
   1413  1.1.1.1.2.2  riastrad static void
   1414  1.1.1.1.2.2  riastrad do_cvt_mode(struct detailed_timing *timing, void *c)
   1415  1.1.1.1.2.2  riastrad {
   1416  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure *closure = c;
   1417  1.1.1.1.2.2  riastrad 	struct detailed_non_pixel *data = &timing->data.other_data;
   1418  1.1.1.1.2.2  riastrad 
   1419  1.1.1.1.2.2  riastrad 	if (data->type == EDID_DETAIL_CVT_3BYTE)
   1420  1.1.1.1.2.2  riastrad 		closure->modes += drm_cvt_modes(closure->connector, timing);
   1421  1.1.1.1.2.2  riastrad }
   1422  1.1.1.1.2.2  riastrad 
   1423  1.1.1.1.2.2  riastrad static int
   1424  1.1.1.1.2.2  riastrad add_cvt_modes(struct drm_connector *connector, struct edid *edid)
   1425  1.1.1.1.2.2  riastrad {
   1426  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure closure = {
   1427  1.1.1.1.2.2  riastrad 		connector, edid, 0, 0, 0
   1428  1.1.1.1.2.2  riastrad 	};
   1429  1.1.1.1.2.2  riastrad 
   1430  1.1.1.1.2.2  riastrad 	if (version_greater(edid, 1, 2))
   1431  1.1.1.1.2.2  riastrad 		drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
   1432  1.1.1.1.2.2  riastrad 
   1433  1.1.1.1.2.2  riastrad 	/* XXX should also look for CVT codes in VTB blocks */
   1434  1.1.1.1.2.2  riastrad 
   1435  1.1.1.1.2.2  riastrad 	return closure.modes;
   1436  1.1.1.1.2.2  riastrad }
   1437  1.1.1.1.2.2  riastrad 
   1438  1.1.1.1.2.2  riastrad static void
   1439  1.1.1.1.2.2  riastrad do_detailed_mode(struct detailed_timing *timing, void *c)
   1440  1.1.1.1.2.2  riastrad {
   1441  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure *closure = c;
   1442  1.1.1.1.2.2  riastrad 	struct drm_display_mode *newmode;
   1443  1.1.1.1.2.2  riastrad 
   1444  1.1.1.1.2.2  riastrad 	if (timing->pixel_clock) {
   1445  1.1.1.1.2.2  riastrad 		newmode = drm_mode_detailed(closure->connector->dev,
   1446  1.1.1.1.2.2  riastrad 					    closure->edid, timing,
   1447  1.1.1.1.2.2  riastrad 					    closure->quirks);
   1448  1.1.1.1.2.2  riastrad 		if (!newmode)
   1449  1.1.1.1.2.2  riastrad 			return;
   1450  1.1.1.1.2.2  riastrad 
   1451  1.1.1.1.2.2  riastrad 		if (closure->preferred)
   1452  1.1.1.1.2.2  riastrad 			newmode->type |= DRM_MODE_TYPE_PREFERRED;
   1453  1.1.1.1.2.2  riastrad 
   1454  1.1.1.1.2.2  riastrad 		drm_mode_probed_add(closure->connector, newmode);
   1455  1.1.1.1.2.2  riastrad 		closure->modes++;
   1456  1.1.1.1.2.2  riastrad 		closure->preferred = 0;
   1457  1.1.1.1.2.2  riastrad 	}
   1458  1.1.1.1.2.2  riastrad }
   1459  1.1.1.1.2.2  riastrad 
   1460  1.1.1.1.2.2  riastrad /*
   1461  1.1.1.1.2.2  riastrad  * add_detailed_modes - Add modes from detailed timings
   1462  1.1.1.1.2.2  riastrad  * @connector: attached connector
   1463  1.1.1.1.2.2  riastrad  * @edid: EDID block to scan
   1464  1.1.1.1.2.2  riastrad  * @quirks: quirks to apply
   1465  1.1.1.1.2.2  riastrad  */
   1466  1.1.1.1.2.2  riastrad static int
   1467  1.1.1.1.2.2  riastrad add_detailed_modes(struct drm_connector *connector, struct edid *edid,
   1468  1.1.1.1.2.2  riastrad 		   u32 quirks)
   1469  1.1.1.1.2.2  riastrad {
   1470  1.1.1.1.2.2  riastrad 	struct detailed_mode_closure closure = {
   1471  1.1.1.1.2.2  riastrad 		connector,
   1472  1.1.1.1.2.2  riastrad 		edid,
   1473  1.1.1.1.2.2  riastrad 		1,
   1474  1.1.1.1.2.2  riastrad 		quirks,
   1475  1.1.1.1.2.2  riastrad 		0
   1476  1.1.1.1.2.2  riastrad 	};
   1477  1.1.1.1.2.2  riastrad 
   1478  1.1.1.1.2.2  riastrad 	if (closure.preferred && !version_greater(edid, 1, 3))
   1479  1.1.1.1.2.2  riastrad 		closure.preferred =
   1480  1.1.1.1.2.2  riastrad 		    (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
   1481  1.1.1.1.2.2  riastrad 
   1482  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
   1483  1.1.1.1.2.2  riastrad 
   1484  1.1.1.1.2.2  riastrad 	return closure.modes;
   1485  1.1.1.1.2.2  riastrad }
   1486  1.1.1.1.2.2  riastrad 
   1487  1.1.1.1.2.2  riastrad #define HDMI_IDENTIFIER 0x000C03
   1488  1.1.1.1.2.2  riastrad #define AUDIO_BLOCK	0x01
   1489  1.1.1.1.2.2  riastrad #define VIDEO_BLOCK     0x02
   1490  1.1.1.1.2.2  riastrad #define VENDOR_BLOCK    0x03
   1491  1.1.1.1.2.2  riastrad #define SPEAKER_BLOCK	0x04
   1492  1.1.1.1.2.2  riastrad #define EDID_BASIC_AUDIO	(1 << 6)
   1493  1.1.1.1.2.2  riastrad #define EDID_CEA_YCRCB444	(1 << 5)
   1494  1.1.1.1.2.2  riastrad #define EDID_CEA_YCRCB422	(1 << 4)
   1495  1.1.1.1.2.2  riastrad 
   1496  1.1.1.1.2.2  riastrad /**
   1497  1.1.1.1.2.2  riastrad  * Search EDID for CEA extension block.
   1498  1.1.1.1.2.2  riastrad  */
   1499  1.1.1.1.2.2  riastrad u8 *drm_find_cea_extension(struct edid *edid)
   1500  1.1.1.1.2.2  riastrad {
   1501  1.1.1.1.2.2  riastrad 	u8 *edid_ext = NULL;
   1502  1.1.1.1.2.2  riastrad 	int i;
   1503  1.1.1.1.2.2  riastrad 
   1504  1.1.1.1.2.2  riastrad 	/* No EDID or EDID extensions */
   1505  1.1.1.1.2.2  riastrad 	if (edid == NULL || edid->extensions == 0)
   1506  1.1.1.1.2.2  riastrad 		return NULL;
   1507  1.1.1.1.2.2  riastrad 
   1508  1.1.1.1.2.2  riastrad 	/* Find CEA extension */
   1509  1.1.1.1.2.2  riastrad 	for (i = 0; i < edid->extensions; i++) {
   1510  1.1.1.1.2.2  riastrad 		edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
   1511  1.1.1.1.2.2  riastrad 		if (edid_ext[0] == CEA_EXT)
   1512  1.1.1.1.2.2  riastrad 			break;
   1513  1.1.1.1.2.2  riastrad 	}
   1514  1.1.1.1.2.2  riastrad 
   1515  1.1.1.1.2.2  riastrad 	if (i == edid->extensions)
   1516  1.1.1.1.2.2  riastrad 		return NULL;
   1517  1.1.1.1.2.2  riastrad 
   1518  1.1.1.1.2.2  riastrad 	return edid_ext;
   1519  1.1.1.1.2.2  riastrad }
   1520  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_find_cea_extension);
   1521  1.1.1.1.2.2  riastrad 
   1522  1.1.1.1.2.2  riastrad /*
   1523  1.1.1.1.2.2  riastrad  * Looks for a CEA mode matching given drm_display_mode.
   1524  1.1.1.1.2.2  riastrad  * Returns its CEA Video ID code, or 0 if not found.
   1525  1.1.1.1.2.2  riastrad  */
   1526  1.1.1.1.2.2  riastrad u8 drm_match_cea_mode(struct drm_display_mode *to_match)
   1527  1.1.1.1.2.2  riastrad {
   1528  1.1.1.1.2.8  riastrad 	const struct drm_display_mode *cea_mode;
   1529  1.1.1.1.2.2  riastrad 	u8 mode;
   1530  1.1.1.1.2.2  riastrad 
   1531  1.1.1.1.2.2  riastrad 	for (mode = 0; mode < drm_num_cea_modes; mode++) {
   1532  1.1.1.1.2.8  riastrad 		cea_mode = &edid_cea_modes[mode];
   1533  1.1.1.1.2.2  riastrad 
   1534  1.1.1.1.2.2  riastrad 		if (drm_mode_equal(to_match, cea_mode))
   1535  1.1.1.1.2.2  riastrad 			return mode + 1;
   1536  1.1.1.1.2.2  riastrad 	}
   1537  1.1.1.1.2.2  riastrad 	return 0;
   1538  1.1.1.1.2.2  riastrad }
   1539  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_match_cea_mode);
   1540  1.1.1.1.2.2  riastrad 
   1541  1.1.1.1.2.2  riastrad 
   1542  1.1.1.1.2.2  riastrad static int
   1543  1.1.1.1.2.2  riastrad do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
   1544  1.1.1.1.2.2  riastrad {
   1545  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   1546  1.1.1.1.2.2  riastrad 	u8 * mode, cea_mode;
   1547  1.1.1.1.2.2  riastrad 	int modes = 0;
   1548  1.1.1.1.2.2  riastrad 
   1549  1.1.1.1.2.2  riastrad 	for (mode = db; mode < db + len; mode++) {
   1550  1.1.1.1.2.2  riastrad 		cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
   1551  1.1.1.1.2.2  riastrad 		if (cea_mode < drm_num_cea_modes) {
   1552  1.1.1.1.2.2  riastrad 			struct drm_display_mode *newmode;
   1553  1.1.1.1.2.2  riastrad 			newmode = drm_mode_duplicate(dev,
   1554  1.1.1.1.2.2  riastrad 						     &edid_cea_modes[cea_mode]);
   1555  1.1.1.1.2.2  riastrad 			if (newmode) {
   1556  1.1.1.1.2.2  riastrad 				drm_mode_probed_add(connector, newmode);
   1557  1.1.1.1.2.2  riastrad 				modes++;
   1558  1.1.1.1.2.2  riastrad 			}
   1559  1.1.1.1.2.2  riastrad 		}
   1560  1.1.1.1.2.2  riastrad 	}
   1561  1.1.1.1.2.2  riastrad 
   1562  1.1.1.1.2.2  riastrad 	return modes;
   1563  1.1.1.1.2.2  riastrad }
   1564  1.1.1.1.2.2  riastrad 
   1565  1.1.1.1.2.2  riastrad static int
   1566  1.1.1.1.2.2  riastrad cea_db_payload_len(const u8 *db)
   1567  1.1.1.1.2.2  riastrad {
   1568  1.1.1.1.2.2  riastrad 	return db[0] & 0x1f;
   1569  1.1.1.1.2.2  riastrad }
   1570  1.1.1.1.2.2  riastrad 
   1571  1.1.1.1.2.2  riastrad static int
   1572  1.1.1.1.2.2  riastrad cea_db_tag(const u8 *db)
   1573  1.1.1.1.2.2  riastrad {
   1574  1.1.1.1.2.2  riastrad 	return db[0] >> 5;
   1575  1.1.1.1.2.2  riastrad }
   1576  1.1.1.1.2.2  riastrad 
   1577  1.1.1.1.2.2  riastrad static int
   1578  1.1.1.1.2.2  riastrad cea_revision(const u8 *cea)
   1579  1.1.1.1.2.2  riastrad {
   1580  1.1.1.1.2.2  riastrad 	return cea[1];
   1581  1.1.1.1.2.2  riastrad }
   1582  1.1.1.1.2.2  riastrad 
   1583  1.1.1.1.2.2  riastrad static int
   1584  1.1.1.1.2.2  riastrad cea_db_offsets(const u8 *cea, int *start, int *end)
   1585  1.1.1.1.2.2  riastrad {
   1586  1.1.1.1.2.2  riastrad 	/* Data block offset in CEA extension block */
   1587  1.1.1.1.2.2  riastrad 	*start = 4;
   1588  1.1.1.1.2.2  riastrad 	*end = cea[2];
   1589  1.1.1.1.2.2  riastrad 	if (*end == 0)
   1590  1.1.1.1.2.2  riastrad 		*end = 127;
   1591  1.1.1.1.2.2  riastrad 	if (*end < 4 || *end > 127)
   1592  1.1.1.1.2.2  riastrad 		return -ERANGE;
   1593  1.1.1.1.2.2  riastrad 	return 0;
   1594  1.1.1.1.2.2  riastrad }
   1595  1.1.1.1.2.2  riastrad 
   1596  1.1.1.1.2.2  riastrad #define for_each_cea_db(cea, i, start, end) \
   1597  1.1.1.1.2.2  riastrad 	for ((i) = (start); (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
   1598  1.1.1.1.2.2  riastrad 
   1599  1.1.1.1.2.2  riastrad static int
   1600  1.1.1.1.2.2  riastrad add_cea_modes(struct drm_connector *connector, struct edid *edid)
   1601  1.1.1.1.2.2  riastrad {
   1602  1.1.1.1.2.2  riastrad 	u8 * cea = drm_find_cea_extension(edid);
   1603  1.1.1.1.2.2  riastrad 	u8 * db, dbl;
   1604  1.1.1.1.2.2  riastrad 	int modes = 0;
   1605  1.1.1.1.2.2  riastrad 
   1606  1.1.1.1.2.2  riastrad 	if (cea && cea_revision(cea) >= 3) {
   1607  1.1.1.1.2.2  riastrad 		int i, start, end;
   1608  1.1.1.1.2.2  riastrad 
   1609  1.1.1.1.2.2  riastrad 		if (cea_db_offsets(cea, &start, &end))
   1610  1.1.1.1.2.2  riastrad 			return 0;
   1611  1.1.1.1.2.2  riastrad 
   1612  1.1.1.1.2.2  riastrad 		for_each_cea_db(cea, i, start, end) {
   1613  1.1.1.1.2.2  riastrad 			db = &cea[i];
   1614  1.1.1.1.2.2  riastrad 			dbl = cea_db_payload_len(db);
   1615  1.1.1.1.2.2  riastrad 
   1616  1.1.1.1.2.2  riastrad 			if (cea_db_tag(db) == VIDEO_BLOCK)
   1617  1.1.1.1.2.2  riastrad 				modes += do_cea_modes (connector, db+1, dbl);
   1618  1.1.1.1.2.2  riastrad 		}
   1619  1.1.1.1.2.2  riastrad 	}
   1620  1.1.1.1.2.2  riastrad 
   1621  1.1.1.1.2.2  riastrad 	return modes;
   1622  1.1.1.1.2.2  riastrad }
   1623  1.1.1.1.2.2  riastrad 
   1624  1.1.1.1.2.2  riastrad static void
   1625  1.1.1.1.2.2  riastrad parse_hdmi_vsdb(struct drm_connector *connector, const u8 *db)
   1626  1.1.1.1.2.2  riastrad {
   1627  1.1.1.1.2.2  riastrad 	u8 len = cea_db_payload_len(db);
   1628  1.1.1.1.2.2  riastrad 
   1629  1.1.1.1.2.2  riastrad 	if (len >= 6) {
   1630  1.1.1.1.2.2  riastrad 		connector->eld[5] |= (db[6] >> 7) << 1;  /* Supports_AI */
   1631  1.1.1.1.2.2  riastrad 		connector->dvi_dual = db[6] & 1;
   1632  1.1.1.1.2.2  riastrad 	}
   1633  1.1.1.1.2.2  riastrad 	if (len >= 7)
   1634  1.1.1.1.2.2  riastrad 		connector->max_tmds_clock = db[7] * 5;
   1635  1.1.1.1.2.2  riastrad 	if (len >= 8) {
   1636  1.1.1.1.2.2  riastrad 		connector->latency_present[0] = db[8] >> 7;
   1637  1.1.1.1.2.2  riastrad 		connector->latency_present[1] = (db[8] >> 6) & 1;
   1638  1.1.1.1.2.2  riastrad 	}
   1639  1.1.1.1.2.2  riastrad 	if (len >= 9)
   1640  1.1.1.1.2.2  riastrad 		connector->video_latency[0] = db[9];
   1641  1.1.1.1.2.2  riastrad 	if (len >= 10)
   1642  1.1.1.1.2.2  riastrad 		connector->audio_latency[0] = db[10];
   1643  1.1.1.1.2.2  riastrad 	if (len >= 11)
   1644  1.1.1.1.2.2  riastrad 		connector->video_latency[1] = db[11];
   1645  1.1.1.1.2.2  riastrad 	if (len >= 12)
   1646  1.1.1.1.2.2  riastrad 		connector->audio_latency[1] = db[12];
   1647  1.1.1.1.2.2  riastrad 
   1648  1.1.1.1.2.2  riastrad 	DRM_DEBUG_KMS("HDMI: DVI dual %d, "
   1649  1.1.1.1.2.2  riastrad 		    "max TMDS clock %d, "
   1650  1.1.1.1.2.2  riastrad 		    "latency present %d %d, "
   1651  1.1.1.1.2.2  riastrad 		    "video latency %d %d, "
   1652  1.1.1.1.2.2  riastrad 		    "audio latency %d %d\n",
   1653  1.1.1.1.2.2  riastrad 		    connector->dvi_dual,
   1654  1.1.1.1.2.2  riastrad 		    connector->max_tmds_clock,
   1655  1.1.1.1.2.2  riastrad 	      (int) connector->latency_present[0],
   1656  1.1.1.1.2.2  riastrad 	      (int) connector->latency_present[1],
   1657  1.1.1.1.2.2  riastrad 		    connector->video_latency[0],
   1658  1.1.1.1.2.2  riastrad 		    connector->video_latency[1],
   1659  1.1.1.1.2.2  riastrad 		    connector->audio_latency[0],
   1660  1.1.1.1.2.2  riastrad 		    connector->audio_latency[1]);
   1661  1.1.1.1.2.2  riastrad }
   1662  1.1.1.1.2.2  riastrad 
   1663  1.1.1.1.2.2  riastrad static void
   1664  1.1.1.1.2.2  riastrad monitor_name(struct detailed_timing *t, void *data)
   1665  1.1.1.1.2.2  riastrad {
   1666  1.1.1.1.2.2  riastrad 	if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
   1667  1.1.1.1.2.2  riastrad 		*(u8 **)data = t->data.other_data.data.str.str;
   1668  1.1.1.1.2.2  riastrad }
   1669  1.1.1.1.2.2  riastrad 
   1670  1.1.1.1.2.2  riastrad static bool cea_db_is_hdmi_vsdb(const u8 *db)
   1671  1.1.1.1.2.2  riastrad {
   1672  1.1.1.1.2.2  riastrad 	int hdmi_id;
   1673  1.1.1.1.2.2  riastrad 
   1674  1.1.1.1.2.2  riastrad 	if (cea_db_tag(db) != VENDOR_BLOCK)
   1675  1.1.1.1.2.2  riastrad 		return false;
   1676  1.1.1.1.2.2  riastrad 
   1677  1.1.1.1.2.2  riastrad 	if (cea_db_payload_len(db) < 5)
   1678  1.1.1.1.2.2  riastrad 		return false;
   1679  1.1.1.1.2.2  riastrad 
   1680  1.1.1.1.2.2  riastrad 	hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
   1681  1.1.1.1.2.2  riastrad 
   1682  1.1.1.1.2.2  riastrad 	return hdmi_id == HDMI_IDENTIFIER;
   1683  1.1.1.1.2.2  riastrad }
   1684  1.1.1.1.2.2  riastrad 
   1685  1.1.1.1.2.2  riastrad /**
   1686  1.1.1.1.2.2  riastrad  * drm_edid_to_eld - build ELD from EDID
   1687  1.1.1.1.2.2  riastrad  * @connector: connector corresponding to the HDMI/DP sink
   1688  1.1.1.1.2.2  riastrad  * @edid: EDID to parse
   1689  1.1.1.1.2.2  riastrad  *
   1690  1.1.1.1.2.2  riastrad  * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
   1691  1.1.1.1.2.2  riastrad  * Some ELD fields are left to the graphics driver caller:
   1692  1.1.1.1.2.2  riastrad  * - Conn_Type
   1693  1.1.1.1.2.2  riastrad  * - HDCP
   1694  1.1.1.1.2.2  riastrad  * - Port_ID
   1695  1.1.1.1.2.2  riastrad  */
   1696  1.1.1.1.2.2  riastrad void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
   1697  1.1.1.1.2.2  riastrad {
   1698  1.1.1.1.2.2  riastrad 	uint8_t *eld = connector->eld;
   1699  1.1.1.1.2.2  riastrad 	u8 *cea;
   1700  1.1.1.1.2.2  riastrad 	u8 *name;
   1701  1.1.1.1.2.2  riastrad 	u8 *db;
   1702  1.1.1.1.2.2  riastrad 	int sad_count = 0;
   1703  1.1.1.1.2.2  riastrad 	int mnl;
   1704  1.1.1.1.2.2  riastrad 	int dbl;
   1705  1.1.1.1.2.2  riastrad 
   1706  1.1.1.1.2.2  riastrad 	memset(eld, 0, sizeof(connector->eld));
   1707  1.1.1.1.2.2  riastrad 
   1708  1.1.1.1.2.2  riastrad 	cea = drm_find_cea_extension(edid);
   1709  1.1.1.1.2.2  riastrad 	if (!cea) {
   1710  1.1.1.1.2.2  riastrad 		DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
   1711  1.1.1.1.2.2  riastrad 		return;
   1712  1.1.1.1.2.2  riastrad 	}
   1713  1.1.1.1.2.2  riastrad 
   1714  1.1.1.1.2.2  riastrad 	name = NULL;
   1715  1.1.1.1.2.2  riastrad 	drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
   1716  1.1.1.1.2.2  riastrad 	for (mnl = 0; name && mnl < 13; mnl++) {
   1717  1.1.1.1.2.2  riastrad 		if (name[mnl] == 0x0a)
   1718  1.1.1.1.2.2  riastrad 			break;
   1719  1.1.1.1.2.2  riastrad 		eld[20 + mnl] = name[mnl];
   1720  1.1.1.1.2.2  riastrad 	}
   1721  1.1.1.1.2.2  riastrad 	eld[4] = (cea[1] << 5) | mnl;
   1722  1.1.1.1.2.2  riastrad 	DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
   1723  1.1.1.1.2.2  riastrad 
   1724  1.1.1.1.2.2  riastrad 	eld[0] = 2 << 3;		/* ELD version: 2 */
   1725  1.1.1.1.2.2  riastrad 
   1726  1.1.1.1.2.2  riastrad 	eld[16] = edid->mfg_id[0];
   1727  1.1.1.1.2.2  riastrad 	eld[17] = edid->mfg_id[1];
   1728  1.1.1.1.2.2  riastrad 	eld[18] = edid->prod_code[0];
   1729  1.1.1.1.2.2  riastrad 	eld[19] = edid->prod_code[1];
   1730  1.1.1.1.2.2  riastrad 
   1731  1.1.1.1.2.2  riastrad 	if (cea_revision(cea) >= 3) {
   1732  1.1.1.1.2.2  riastrad 		int i, start, end;
   1733  1.1.1.1.2.2  riastrad 
   1734  1.1.1.1.2.2  riastrad 		if (cea_db_offsets(cea, &start, &end)) {
   1735  1.1.1.1.2.2  riastrad 			start = 0;
   1736  1.1.1.1.2.2  riastrad 			end = 0;
   1737  1.1.1.1.2.2  riastrad 		}
   1738  1.1.1.1.2.2  riastrad 
   1739  1.1.1.1.2.2  riastrad 		for_each_cea_db(cea, i, start, end) {
   1740  1.1.1.1.2.2  riastrad 			db = &cea[i];
   1741  1.1.1.1.2.2  riastrad 			dbl = cea_db_payload_len(db);
   1742  1.1.1.1.2.2  riastrad 
   1743  1.1.1.1.2.2  riastrad 			switch (cea_db_tag(db)) {
   1744  1.1.1.1.2.2  riastrad 			case AUDIO_BLOCK:
   1745  1.1.1.1.2.2  riastrad 				/* Audio Data Block, contains SADs */
   1746  1.1.1.1.2.2  riastrad 				sad_count = dbl / 3;
   1747  1.1.1.1.2.2  riastrad 				if (dbl >= 1)
   1748  1.1.1.1.2.2  riastrad 					memcpy(eld + 20 + mnl, &db[1], dbl);
   1749  1.1.1.1.2.2  riastrad 				break;
   1750  1.1.1.1.2.2  riastrad 			case SPEAKER_BLOCK:
   1751  1.1.1.1.2.2  riastrad 				/* Speaker Allocation Data Block */
   1752  1.1.1.1.2.2  riastrad 				if (dbl >= 1)
   1753  1.1.1.1.2.2  riastrad 					eld[7] = db[1];
   1754  1.1.1.1.2.2  riastrad 				break;
   1755  1.1.1.1.2.2  riastrad 			case VENDOR_BLOCK:
   1756  1.1.1.1.2.2  riastrad 				/* HDMI Vendor-Specific Data Block */
   1757  1.1.1.1.2.2  riastrad 				if (cea_db_is_hdmi_vsdb(db))
   1758  1.1.1.1.2.2  riastrad 					parse_hdmi_vsdb(connector, db);
   1759  1.1.1.1.2.2  riastrad 				break;
   1760  1.1.1.1.2.2  riastrad 			default:
   1761  1.1.1.1.2.2  riastrad 				break;
   1762  1.1.1.1.2.2  riastrad 			}
   1763  1.1.1.1.2.2  riastrad 		}
   1764  1.1.1.1.2.2  riastrad 	}
   1765  1.1.1.1.2.2  riastrad 	eld[5] |= sad_count << 4;
   1766  1.1.1.1.2.2  riastrad 	eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
   1767  1.1.1.1.2.2  riastrad 
   1768  1.1.1.1.2.2  riastrad 	DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
   1769  1.1.1.1.2.2  riastrad }
   1770  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_edid_to_eld);
   1771  1.1.1.1.2.2  riastrad 
   1772  1.1.1.1.2.2  riastrad /**
   1773  1.1.1.1.2.2  riastrad  * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
   1774  1.1.1.1.2.2  riastrad  * @connector: connector associated with the HDMI/DP sink
   1775  1.1.1.1.2.2  riastrad  * @mode: the display mode
   1776  1.1.1.1.2.2  riastrad  */
   1777  1.1.1.1.2.2  riastrad int drm_av_sync_delay(struct drm_connector *connector,
   1778  1.1.1.1.2.2  riastrad 		      struct drm_display_mode *mode)
   1779  1.1.1.1.2.2  riastrad {
   1780  1.1.1.1.2.2  riastrad 	int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
   1781  1.1.1.1.2.2  riastrad 	int a, v;
   1782  1.1.1.1.2.2  riastrad 
   1783  1.1.1.1.2.2  riastrad 	if (!connector->latency_present[0])
   1784  1.1.1.1.2.2  riastrad 		return 0;
   1785  1.1.1.1.2.2  riastrad 	if (!connector->latency_present[1])
   1786  1.1.1.1.2.2  riastrad 		i = 0;
   1787  1.1.1.1.2.2  riastrad 
   1788  1.1.1.1.2.2  riastrad 	a = connector->audio_latency[i];
   1789  1.1.1.1.2.2  riastrad 	v = connector->video_latency[i];
   1790  1.1.1.1.2.2  riastrad 
   1791  1.1.1.1.2.2  riastrad 	/*
   1792  1.1.1.1.2.2  riastrad 	 * HDMI/DP sink doesn't support audio or video?
   1793  1.1.1.1.2.2  riastrad 	 */
   1794  1.1.1.1.2.2  riastrad 	if (a == 255 || v == 255)
   1795  1.1.1.1.2.2  riastrad 		return 0;
   1796  1.1.1.1.2.2  riastrad 
   1797  1.1.1.1.2.2  riastrad 	/*
   1798  1.1.1.1.2.2  riastrad 	 * Convert raw EDID values to millisecond.
   1799  1.1.1.1.2.2  riastrad 	 * Treat unknown latency as 0ms.
   1800  1.1.1.1.2.2  riastrad 	 */
   1801  1.1.1.1.2.2  riastrad 	if (a)
   1802  1.1.1.1.2.2  riastrad 		a = min(2 * (a - 1), 500);
   1803  1.1.1.1.2.2  riastrad 	if (v)
   1804  1.1.1.1.2.2  riastrad 		v = min(2 * (v - 1), 500);
   1805  1.1.1.1.2.2  riastrad 
   1806  1.1.1.1.2.2  riastrad 	return max(v - a, 0);
   1807  1.1.1.1.2.2  riastrad }
   1808  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_av_sync_delay);
   1809  1.1.1.1.2.2  riastrad 
   1810  1.1.1.1.2.2  riastrad /**
   1811  1.1.1.1.2.2  riastrad  * drm_select_eld - select one ELD from multiple HDMI/DP sinks
   1812  1.1.1.1.2.2  riastrad  * @encoder: the encoder just changed display mode
   1813  1.1.1.1.2.2  riastrad  * @mode: the adjusted display mode
   1814  1.1.1.1.2.2  riastrad  *
   1815  1.1.1.1.2.2  riastrad  * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
   1816  1.1.1.1.2.2  riastrad  * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
   1817  1.1.1.1.2.2  riastrad  */
   1818  1.1.1.1.2.2  riastrad struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
   1819  1.1.1.1.2.2  riastrad 				     struct drm_display_mode *mode)
   1820  1.1.1.1.2.2  riastrad {
   1821  1.1.1.1.2.2  riastrad 	struct drm_connector *connector;
   1822  1.1.1.1.2.2  riastrad 	struct drm_device *dev = encoder->dev;
   1823  1.1.1.1.2.2  riastrad 
   1824  1.1.1.1.2.2  riastrad 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
   1825  1.1.1.1.2.2  riastrad 		if (connector->encoder == encoder && connector->eld[0])
   1826  1.1.1.1.2.2  riastrad 			return connector;
   1827  1.1.1.1.2.2  riastrad 
   1828  1.1.1.1.2.2  riastrad 	return NULL;
   1829  1.1.1.1.2.2  riastrad }
   1830  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_select_eld);
   1831  1.1.1.1.2.2  riastrad 
   1832  1.1.1.1.2.2  riastrad /**
   1833  1.1.1.1.2.2  riastrad  * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
   1834  1.1.1.1.2.2  riastrad  * @edid: monitor EDID information
   1835  1.1.1.1.2.2  riastrad  *
   1836  1.1.1.1.2.2  riastrad  * Parse the CEA extension according to CEA-861-B.
   1837  1.1.1.1.2.2  riastrad  * Return true if HDMI, false if not or unknown.
   1838  1.1.1.1.2.2  riastrad  */
   1839  1.1.1.1.2.2  riastrad bool drm_detect_hdmi_monitor(struct edid *edid)
   1840  1.1.1.1.2.2  riastrad {
   1841  1.1.1.1.2.2  riastrad 	u8 *edid_ext;
   1842  1.1.1.1.2.2  riastrad 	int i;
   1843  1.1.1.1.2.2  riastrad 	int start_offset, end_offset;
   1844  1.1.1.1.2.2  riastrad 
   1845  1.1.1.1.2.2  riastrad 	edid_ext = drm_find_cea_extension(edid);
   1846  1.1.1.1.2.2  riastrad 	if (!edid_ext)
   1847  1.1.1.1.2.2  riastrad 		return false;
   1848  1.1.1.1.2.2  riastrad 
   1849  1.1.1.1.2.2  riastrad 	if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
   1850  1.1.1.1.2.2  riastrad 		return false;
   1851  1.1.1.1.2.2  riastrad 
   1852  1.1.1.1.2.2  riastrad 	/*
   1853  1.1.1.1.2.2  riastrad 	 * Because HDMI identifier is in Vendor Specific Block,
   1854  1.1.1.1.2.2  riastrad 	 * search it from all data blocks of CEA extension.
   1855  1.1.1.1.2.2  riastrad 	 */
   1856  1.1.1.1.2.2  riastrad 	for_each_cea_db(edid_ext, i, start_offset, end_offset) {
   1857  1.1.1.1.2.2  riastrad 		if (cea_db_is_hdmi_vsdb(&edid_ext[i]))
   1858  1.1.1.1.2.2  riastrad 			return true;
   1859  1.1.1.1.2.2  riastrad 	}
   1860  1.1.1.1.2.2  riastrad 
   1861  1.1.1.1.2.2  riastrad 	return false;
   1862  1.1.1.1.2.2  riastrad }
   1863  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_detect_hdmi_monitor);
   1864  1.1.1.1.2.2  riastrad 
   1865  1.1.1.1.2.2  riastrad /**
   1866  1.1.1.1.2.2  riastrad  * drm_detect_monitor_audio - check monitor audio capability
   1867  1.1.1.1.2.2  riastrad  *
   1868  1.1.1.1.2.2  riastrad  * Monitor should have CEA extension block.
   1869  1.1.1.1.2.2  riastrad  * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
   1870  1.1.1.1.2.2  riastrad  * audio' only. If there is any audio extension block and supported
   1871  1.1.1.1.2.2  riastrad  * audio format, assume at least 'basic audio' support, even if 'basic
   1872  1.1.1.1.2.2  riastrad  * audio' is not defined in EDID.
   1873  1.1.1.1.2.2  riastrad  *
   1874  1.1.1.1.2.2  riastrad  */
   1875  1.1.1.1.2.2  riastrad bool drm_detect_monitor_audio(struct edid *edid)
   1876  1.1.1.1.2.2  riastrad {
   1877  1.1.1.1.2.2  riastrad 	u8 *edid_ext;
   1878  1.1.1.1.2.2  riastrad 	int i, j;
   1879  1.1.1.1.2.2  riastrad 	bool has_audio = false;
   1880  1.1.1.1.2.2  riastrad 	int start_offset, end_offset;
   1881  1.1.1.1.2.2  riastrad 
   1882  1.1.1.1.2.2  riastrad 	edid_ext = drm_find_cea_extension(edid);
   1883  1.1.1.1.2.2  riastrad 	if (!edid_ext)
   1884  1.1.1.1.2.2  riastrad 		goto end;
   1885  1.1.1.1.2.2  riastrad 
   1886  1.1.1.1.2.2  riastrad 	has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
   1887  1.1.1.1.2.2  riastrad 
   1888  1.1.1.1.2.2  riastrad 	if (has_audio) {
   1889  1.1.1.1.2.2  riastrad 		DRM_DEBUG_KMS("Monitor has basic audio support\n");
   1890  1.1.1.1.2.2  riastrad 		goto end;
   1891  1.1.1.1.2.2  riastrad 	}
   1892  1.1.1.1.2.2  riastrad 
   1893  1.1.1.1.2.2  riastrad 	if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
   1894  1.1.1.1.2.2  riastrad 		goto end;
   1895  1.1.1.1.2.2  riastrad 
   1896  1.1.1.1.2.2  riastrad 	for_each_cea_db(edid_ext, i, start_offset, end_offset) {
   1897  1.1.1.1.2.2  riastrad 		if (cea_db_tag(&edid_ext[i]) == AUDIO_BLOCK) {
   1898  1.1.1.1.2.2  riastrad 			has_audio = true;
   1899  1.1.1.1.2.2  riastrad 			for (j = 1; j < cea_db_payload_len(&edid_ext[i]) + 1; j += 3)
   1900  1.1.1.1.2.2  riastrad 				DRM_DEBUG_KMS("CEA audio format %d\n",
   1901  1.1.1.1.2.2  riastrad 					      (edid_ext[i + j] >> 3) & 0xf);
   1902  1.1.1.1.2.2  riastrad 			goto end;
   1903  1.1.1.1.2.2  riastrad 		}
   1904  1.1.1.1.2.2  riastrad 	}
   1905  1.1.1.1.2.2  riastrad end:
   1906  1.1.1.1.2.2  riastrad 	return has_audio;
   1907  1.1.1.1.2.2  riastrad }
   1908  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_detect_monitor_audio);
   1909  1.1.1.1.2.2  riastrad 
   1910  1.1.1.1.2.2  riastrad /**
   1911  1.1.1.1.2.2  riastrad  * drm_add_display_info - pull display info out if present
   1912  1.1.1.1.2.2  riastrad  * @edid: EDID data
   1913  1.1.1.1.2.2  riastrad  * @info: display info (attached to connector)
   1914  1.1.1.1.2.2  riastrad  *
   1915  1.1.1.1.2.2  riastrad  * Grab any available display info and stuff it into the drm_display_info
   1916  1.1.1.1.2.2  riastrad  * structure that's part of the connector.  Useful for tracking bpp and
   1917  1.1.1.1.2.2  riastrad  * color spaces.
   1918  1.1.1.1.2.2  riastrad  */
   1919  1.1.1.1.2.2  riastrad static void drm_add_display_info(struct edid *edid,
   1920  1.1.1.1.2.2  riastrad 				 struct drm_display_info *info)
   1921  1.1.1.1.2.2  riastrad {
   1922  1.1.1.1.2.2  riastrad 	u8 *edid_ext;
   1923  1.1.1.1.2.2  riastrad 
   1924  1.1.1.1.2.2  riastrad 	info->width_mm = edid->width_cm * 10;
   1925  1.1.1.1.2.2  riastrad 	info->height_mm = edid->height_cm * 10;
   1926  1.1.1.1.2.2  riastrad 
   1927  1.1.1.1.2.2  riastrad 	/* driver figures it out in this case */
   1928  1.1.1.1.2.2  riastrad 	info->bpc = 0;
   1929  1.1.1.1.2.2  riastrad 	info->color_formats = 0;
   1930  1.1.1.1.2.2  riastrad 
   1931  1.1.1.1.2.2  riastrad 	if (edid->revision < 3)
   1932  1.1.1.1.2.2  riastrad 		return;
   1933  1.1.1.1.2.2  riastrad 
   1934  1.1.1.1.2.2  riastrad 	if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
   1935  1.1.1.1.2.2  riastrad 		return;
   1936  1.1.1.1.2.2  riastrad 
   1937  1.1.1.1.2.2  riastrad 	/* Get data from CEA blocks if present */
   1938  1.1.1.1.2.2  riastrad 	edid_ext = drm_find_cea_extension(edid);
   1939  1.1.1.1.2.2  riastrad 	if (edid_ext) {
   1940  1.1.1.1.2.2  riastrad 		info->cea_rev = edid_ext[1];
   1941  1.1.1.1.2.2  riastrad 
   1942  1.1.1.1.2.2  riastrad 		/* The existence of a CEA block should imply RGB support */
   1943  1.1.1.1.2.2  riastrad 		info->color_formats = DRM_COLOR_FORMAT_RGB444;
   1944  1.1.1.1.2.2  riastrad 		if (edid_ext[3] & EDID_CEA_YCRCB444)
   1945  1.1.1.1.2.2  riastrad 			info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
   1946  1.1.1.1.2.2  riastrad 		if (edid_ext[3] & EDID_CEA_YCRCB422)
   1947  1.1.1.1.2.2  riastrad 			info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
   1948  1.1.1.1.2.2  riastrad 	}
   1949  1.1.1.1.2.2  riastrad 
   1950  1.1.1.1.2.2  riastrad 	/* Only defined for 1.4 with digital displays */
   1951  1.1.1.1.2.2  riastrad 	if (edid->revision < 4)
   1952  1.1.1.1.2.2  riastrad 		return;
   1953  1.1.1.1.2.2  riastrad 
   1954  1.1.1.1.2.2  riastrad 	switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
   1955  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_6:
   1956  1.1.1.1.2.2  riastrad 		info->bpc = 6;
   1957  1.1.1.1.2.2  riastrad 		break;
   1958  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_8:
   1959  1.1.1.1.2.2  riastrad 		info->bpc = 8;
   1960  1.1.1.1.2.2  riastrad 		break;
   1961  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_10:
   1962  1.1.1.1.2.2  riastrad 		info->bpc = 10;
   1963  1.1.1.1.2.2  riastrad 		break;
   1964  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_12:
   1965  1.1.1.1.2.2  riastrad 		info->bpc = 12;
   1966  1.1.1.1.2.2  riastrad 		break;
   1967  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_14:
   1968  1.1.1.1.2.2  riastrad 		info->bpc = 14;
   1969  1.1.1.1.2.2  riastrad 		break;
   1970  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_16:
   1971  1.1.1.1.2.2  riastrad 		info->bpc = 16;
   1972  1.1.1.1.2.2  riastrad 		break;
   1973  1.1.1.1.2.2  riastrad 	case DRM_EDID_DIGITAL_DEPTH_UNDEF:
   1974  1.1.1.1.2.2  riastrad 	default:
   1975  1.1.1.1.2.2  riastrad 		info->bpc = 0;
   1976  1.1.1.1.2.2  riastrad 		break;
   1977  1.1.1.1.2.2  riastrad 	}
   1978  1.1.1.1.2.2  riastrad 
   1979  1.1.1.1.2.2  riastrad 	info->color_formats |= DRM_COLOR_FORMAT_RGB444;
   1980  1.1.1.1.2.2  riastrad 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
   1981  1.1.1.1.2.2  riastrad 		info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
   1982  1.1.1.1.2.2  riastrad 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
   1983  1.1.1.1.2.2  riastrad 		info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
   1984  1.1.1.1.2.2  riastrad }
   1985  1.1.1.1.2.2  riastrad 
   1986  1.1.1.1.2.2  riastrad /**
   1987  1.1.1.1.2.2  riastrad  * drm_add_edid_modes - add modes from EDID data, if available
   1988  1.1.1.1.2.2  riastrad  * @connector: connector we're probing
   1989  1.1.1.1.2.2  riastrad  * @edid: edid data
   1990  1.1.1.1.2.2  riastrad  *
   1991  1.1.1.1.2.2  riastrad  * Add the specified modes to the connector's mode list.
   1992  1.1.1.1.2.2  riastrad  *
   1993  1.1.1.1.2.2  riastrad  * Return number of modes added or 0 if we couldn't find any.
   1994  1.1.1.1.2.2  riastrad  */
   1995  1.1.1.1.2.2  riastrad int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
   1996  1.1.1.1.2.2  riastrad {
   1997  1.1.1.1.2.2  riastrad 	int num_modes = 0;
   1998  1.1.1.1.2.2  riastrad 	u32 quirks;
   1999  1.1.1.1.2.2  riastrad 
   2000  1.1.1.1.2.2  riastrad 	if (edid == NULL) {
   2001  1.1.1.1.2.2  riastrad 		return 0;
   2002  1.1.1.1.2.2  riastrad 	}
   2003  1.1.1.1.2.2  riastrad 	if (!drm_edid_is_valid(edid)) {
   2004  1.1.1.1.2.2  riastrad 		dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
   2005  1.1.1.1.2.2  riastrad 			 drm_get_connector_name(connector));
   2006  1.1.1.1.2.2  riastrad 		return 0;
   2007  1.1.1.1.2.2  riastrad 	}
   2008  1.1.1.1.2.2  riastrad 
   2009  1.1.1.1.2.2  riastrad 	quirks = edid_get_quirks(edid);
   2010  1.1.1.1.2.2  riastrad 
   2011  1.1.1.1.2.2  riastrad 	/*
   2012  1.1.1.1.2.2  riastrad 	 * EDID spec says modes should be preferred in this order:
   2013  1.1.1.1.2.2  riastrad 	 * - preferred detailed mode
   2014  1.1.1.1.2.2  riastrad 	 * - other detailed modes from base block
   2015  1.1.1.1.2.2  riastrad 	 * - detailed modes from extension blocks
   2016  1.1.1.1.2.2  riastrad 	 * - CVT 3-byte code modes
   2017  1.1.1.1.2.2  riastrad 	 * - standard timing codes
   2018  1.1.1.1.2.2  riastrad 	 * - established timing codes
   2019  1.1.1.1.2.2  riastrad 	 * - modes inferred from GTF or CVT range information
   2020  1.1.1.1.2.2  riastrad 	 *
   2021  1.1.1.1.2.2  riastrad 	 * We get this pretty much right.
   2022  1.1.1.1.2.2  riastrad 	 *
   2023  1.1.1.1.2.2  riastrad 	 * XXX order for additional mode types in extension blocks?
   2024  1.1.1.1.2.2  riastrad 	 */
   2025  1.1.1.1.2.2  riastrad 	num_modes += add_detailed_modes(connector, edid, quirks);
   2026  1.1.1.1.2.2  riastrad 	num_modes += add_cvt_modes(connector, edid);
   2027  1.1.1.1.2.2  riastrad 	num_modes += add_standard_modes(connector, edid);
   2028  1.1.1.1.2.2  riastrad 	num_modes += add_established_modes(connector, edid);
   2029  1.1.1.1.2.2  riastrad 	num_modes += add_inferred_modes(connector, edid);
   2030  1.1.1.1.2.2  riastrad 	num_modes += add_cea_modes(connector, edid);
   2031  1.1.1.1.2.2  riastrad 
   2032  1.1.1.1.2.2  riastrad 	if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
   2033  1.1.1.1.2.2  riastrad 		edid_fixup_preferred(connector, quirks);
   2034  1.1.1.1.2.2  riastrad 
   2035  1.1.1.1.2.2  riastrad 	drm_add_display_info(edid, &connector->display_info);
   2036  1.1.1.1.2.2  riastrad 
   2037  1.1.1.1.2.2  riastrad 	return num_modes;
   2038  1.1.1.1.2.2  riastrad }
   2039  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_add_edid_modes);
   2040  1.1.1.1.2.2  riastrad 
   2041  1.1.1.1.2.2  riastrad /**
   2042  1.1.1.1.2.2  riastrad  * drm_add_modes_noedid - add modes for the connectors without EDID
   2043  1.1.1.1.2.2  riastrad  * @connector: connector we're probing
   2044  1.1.1.1.2.2  riastrad  * @hdisplay: the horizontal display limit
   2045  1.1.1.1.2.2  riastrad  * @vdisplay: the vertical display limit
   2046  1.1.1.1.2.2  riastrad  *
   2047  1.1.1.1.2.2  riastrad  * Add the specified modes to the connector's mode list. Only when the
   2048  1.1.1.1.2.2  riastrad  * hdisplay/vdisplay is not beyond the given limit, it will be added.
   2049  1.1.1.1.2.2  riastrad  *
   2050  1.1.1.1.2.2  riastrad  * Return number of modes added or 0 if we couldn't find any.
   2051  1.1.1.1.2.2  riastrad  */
   2052  1.1.1.1.2.2  riastrad int drm_add_modes_noedid(struct drm_connector *connector,
   2053  1.1.1.1.2.2  riastrad 			int hdisplay, int vdisplay)
   2054  1.1.1.1.2.2  riastrad {
   2055  1.1.1.1.2.2  riastrad 	int i, count, num_modes = 0;
   2056  1.1.1.1.2.2  riastrad 	struct drm_display_mode *mode;
   2057  1.1.1.1.2.2  riastrad 	struct drm_device *dev = connector->dev;
   2058  1.1.1.1.2.2  riastrad 
   2059  1.1.1.1.2.2  riastrad 	count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
   2060  1.1.1.1.2.2  riastrad 	if (hdisplay < 0)
   2061  1.1.1.1.2.2  riastrad 		hdisplay = 0;
   2062  1.1.1.1.2.2  riastrad 	if (vdisplay < 0)
   2063  1.1.1.1.2.2  riastrad 		vdisplay = 0;
   2064  1.1.1.1.2.2  riastrad 
   2065  1.1.1.1.2.2  riastrad 	for (i = 0; i < count; i++) {
   2066  1.1.1.1.2.2  riastrad 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
   2067  1.1.1.1.2.2  riastrad 		if (hdisplay && vdisplay) {
   2068  1.1.1.1.2.2  riastrad 			/*
   2069  1.1.1.1.2.2  riastrad 			 * Only when two are valid, they will be used to check
   2070  1.1.1.1.2.2  riastrad 			 * whether the mode should be added to the mode list of
   2071  1.1.1.1.2.2  riastrad 			 * the connector.
   2072  1.1.1.1.2.2  riastrad 			 */
   2073  1.1.1.1.2.2  riastrad 			if (ptr->hdisplay > hdisplay ||
   2074  1.1.1.1.2.2  riastrad 					ptr->vdisplay > vdisplay)
   2075  1.1.1.1.2.2  riastrad 				continue;
   2076  1.1.1.1.2.2  riastrad 		}
   2077  1.1.1.1.2.2  riastrad 		if (drm_mode_vrefresh(ptr) > 61)
   2078  1.1.1.1.2.2  riastrad 			continue;
   2079  1.1.1.1.2.2  riastrad 		mode = drm_mode_duplicate(dev, ptr);
   2080  1.1.1.1.2.2  riastrad 		if (mode) {
   2081  1.1.1.1.2.2  riastrad 			drm_mode_probed_add(connector, mode);
   2082  1.1.1.1.2.2  riastrad 			num_modes++;
   2083  1.1.1.1.2.2  riastrad 		}
   2084  1.1.1.1.2.2  riastrad 	}
   2085  1.1.1.1.2.2  riastrad 	return num_modes;
   2086  1.1.1.1.2.2  riastrad }
   2087  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_add_modes_noedid);
   2088  1.1.1.1.2.2  riastrad 
   2089  1.1.1.1.2.2  riastrad /**
   2090  1.1.1.1.2.2  riastrad  * drm_mode_cea_vic - return the CEA-861 VIC of a given mode
   2091  1.1.1.1.2.2  riastrad  * @mode: mode
   2092  1.1.1.1.2.2  riastrad  *
   2093  1.1.1.1.2.2  riastrad  * RETURNS:
   2094  1.1.1.1.2.2  riastrad  * The VIC number, 0 in case it's not a CEA-861 mode.
   2095  1.1.1.1.2.2  riastrad  */
   2096  1.1.1.1.2.2  riastrad uint8_t drm_mode_cea_vic(const struct drm_display_mode *mode)
   2097  1.1.1.1.2.2  riastrad {
   2098  1.1.1.1.2.2  riastrad 	uint8_t i;
   2099  1.1.1.1.2.2  riastrad 
   2100  1.1.1.1.2.2  riastrad 	for (i = 0; i < drm_num_cea_modes; i++)
   2101  1.1.1.1.2.2  riastrad 		if (drm_mode_equal(mode, &edid_cea_modes[i]))
   2102  1.1.1.1.2.2  riastrad 			return i + 1;
   2103  1.1.1.1.2.2  riastrad 
   2104  1.1.1.1.2.2  riastrad 	return 0;
   2105  1.1.1.1.2.2  riastrad }
   2106  1.1.1.1.2.2  riastrad EXPORT_SYMBOL(drm_mode_cea_vic);
   2107