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