Subversion Repositories Kolibri OS

Rev

Rev 2160 | Rev 3120 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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