Subversion Repositories Kolibri OS

Rev

Rev 2004 | 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 "drmP.h"
  34. #include "drm_edid.h"
  35. #include "drm_edid_modes.h"
  36.  
  37. #define version_greater(edid, maj, min) \
  38.         (((edid)->version > (maj)) || \
  39.          ((edid)->version == (maj) && (edid)->revision > (min)))
  40.  
  41. #define EDID_EST_TIMINGS 16
  42. #define EDID_STD_TIMINGS 8
  43. #define EDID_DETAILED_TIMINGS 4
  44.  
  45. /*
  46.  * EDID blocks out in the wild have a variety of bugs, try to collect
  47.  * them here (note that userspace may work around broken monitors first,
  48.  * but fixes should make their way here so that the kernel "just works"
  49.  * on as many displays as possible).
  50.  */
  51.  
  52. /* First detailed mode wrong, use largest 60Hz mode */
  53. #define EDID_QUIRK_PREFER_LARGE_60              (1 << 0)
  54. /* Reported 135MHz pixel clock is too high, needs adjustment */
  55. #define EDID_QUIRK_135_CLOCK_TOO_HIGH           (1 << 1)
  56. /* Prefer the largest mode at 75 Hz */
  57. #define EDID_QUIRK_PREFER_LARGE_75              (1 << 2)
  58. /* Detail timing is in cm not mm */
  59. #define EDID_QUIRK_DETAILED_IN_CM               (1 << 3)
  60. /* Detailed timing descriptors have bogus size values, so just take the
  61.  * maximum size and use that.
  62.  */
  63. #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE    (1 << 4)
  64. /* Monitor forgot to set the first detailed is preferred bit. */
  65. #define EDID_QUIRK_FIRST_DETAILED_PREFERRED     (1 << 5)
  66. /* use +hsync +vsync for detailed mode */
  67. #define EDID_QUIRK_DETAILED_SYNC_PP             (1 << 6)
  68.  
  69. struct detailed_mode_closure {
  70.         struct drm_connector *connector;
  71.         struct edid *edid;
  72.         bool preferred;
  73.         u32 quirks;
  74.         int modes;
  75. };
  76.  
  77. #define LEVEL_DMT       0
  78. #define LEVEL_GTF       1
  79. #define LEVEL_GTF2      2
  80. #define LEVEL_CVT       3
  81.  
  82. static struct edid_quirk {
  83.         char *vendor;
  84.         int product_id;
  85.         u32 quirks;
  86. } edid_quirk_list[] = {
  87.         /* Acer AL1706 */
  88.         { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
  89.         /* Acer F51 */
  90.         { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
  91.         /* Unknown Acer */
  92.         { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  93.  
  94.         /* Belinea 10 15 55 */
  95.         { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
  96.         { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
  97.  
  98.         /* Envision Peripherals, Inc. EN-7100e */
  99.         { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
  100.         /* Envision EN2028 */
  101.         { "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
  102.  
  103.         /* Funai Electronics PM36B */
  104.         { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
  105.           EDID_QUIRK_DETAILED_IN_CM },
  106.  
  107.         /* LG Philips LCD LP154W01-A5 */
  108.         { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
  109.         { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
  110.  
  111.         /* Philips 107p5 CRT */
  112.         { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  113.  
  114.         /* Proview AY765C */
  115.         { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  116.  
  117.         /* Samsung SyncMaster 205BW.  Note: irony */
  118.         { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
  119.         /* Samsung SyncMaster 22[5-6]BW */
  120.         { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
  121.         { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
  122. };
  123.  
  124. /*** DDC fetch and block validation ***/
  125.  
  126. static const u8 edid_header[] = {
  127.         0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
  128. };
  129.  
  130.  /*
  131.  * Sanity check the header of the base EDID block.  Return 8 if the header
  132.  * is perfect, down to 0 if it's totally wrong.
  133.  */
  134. int drm_edid_header_is_valid(const u8 *raw_edid)
  135. {
  136.         int i, score = 0;
  137.  
  138.         for (i = 0; i < sizeof(edid_header); i++)
  139.                 if (raw_edid[i] == edid_header[i])
  140.                         score++;
  141.  
  142.         return score;
  143. }
  144. EXPORT_SYMBOL(drm_edid_header_is_valid);
  145.  
  146.  
  147. /*
  148.  * Sanity check the EDID block (base or extension).  Return 0 if the block
  149.  * doesn't check out, or 1 if it's valid.
  150.  */
  151. static bool
  152. drm_edid_block_valid(u8 *raw_edid)
  153. {
  154.         int i;
  155.         u8 csum = 0;
  156.         struct edid *edid = (struct edid *)raw_edid;
  157.  
  158.         if (raw_edid[0] == 0x00) {
  159.                 int score = drm_edid_header_is_valid(raw_edid);
  160.         if (score == 8) ;
  161.         else if (score >= 6) {
  162.                 DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
  163.                 memcpy(raw_edid, edid_header, sizeof(edid_header));
  164.                 } else {
  165.                 goto bad;
  166.                 }
  167.         }
  168.  
  169.         for (i = 0; i < EDID_LENGTH; i++)
  170.                 csum += raw_edid[i];
  171.         if (csum) {
  172.                 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
  173.  
  174.                 /* allow CEA to slide through, switches mangle this */
  175.                 if (raw_edid[0] != 0x02)
  176.                 goto bad;
  177.         }
  178.  
  179.         /* per-block-type checks */
  180.         switch (raw_edid[0]) {
  181.         case 0: /* base */
  182.         if (edid->version != 1) {
  183.                 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
  184.                 goto bad;
  185.         }
  186.  
  187.         if (edid->revision > 4)
  188.                 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
  189.                 break;
  190.  
  191.         default:
  192.                 break;
  193.         }
  194.  
  195.         return 1;
  196.  
  197. bad:
  198.         if (raw_edid) {
  199.                 printk(KERN_ERR "Raw EDID:\n");
  200. //              print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
  201.         }
  202.         return 0;
  203. }
  204.  
  205. /**
  206.  * drm_edid_is_valid - sanity check EDID data
  207.  * @edid: EDID data
  208.  *
  209.  * Sanity-check an entire EDID record (including extensions)
  210.  */
  211. bool drm_edid_is_valid(struct edid *edid)
  212. {
  213.         int i;
  214.         u8 *raw = (u8 *)edid;
  215.  
  216.         if (!edid)
  217.                 return false;
  218.  
  219.         for (i = 0; i <= edid->extensions; i++)
  220.                 if (!drm_edid_block_valid(raw + i * EDID_LENGTH))
  221.                         return false;
  222.  
  223.         return true;
  224. }
  225. EXPORT_SYMBOL(drm_edid_is_valid);
  226.  
  227. #define DDC_ADDR 0x50
  228. #define DDC_SEGMENT_ADDR 0x30
  229. /**
  230.  * Get EDID information via I2C.
  231.  *
  232.  * \param adapter : i2c device adaptor
  233.  * \param buf     : EDID data buffer to be filled
  234.  * \param len     : EDID data buffer length
  235.  * \return 0 on success or -1 on failure.
  236.  *
  237.  * Try to fetch EDID information by calling i2c driver function.
  238.  */
  239. static int
  240. drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
  241.                       int block, int len)
  242. {
  243.         unsigned char start = block * EDID_LENGTH;
  244.         int ret, retries = 5;
  245.  
  246.         /* The core i2c driver will automatically retry the transfer if the
  247.          * adapter reports EAGAIN. However, we find that bit-banging transfers
  248.          * are susceptible to errors under a heavily loaded machine and
  249.          * generate spurious NAKs and timeouts. Retrying the transfer
  250.          * of the individual block a few times seems to overcome this.
  251.          */
  252.         do {
  253.         struct i2c_msg msgs[] = {
  254.                 {
  255.                         .addr   = DDC_ADDR,
  256.                         .flags  = 0,
  257.                         .len    = 1,
  258.                         .buf    = &start,
  259.                 }, {
  260.                         .addr   = DDC_ADDR,
  261.                         .flags  = I2C_M_RD,
  262.                         .len    = len,
  263.                         .buf    = buf,
  264.                 }
  265.         };
  266.                 ret = i2c_transfer(adapter, msgs, 2);
  267.         } while (ret != 2 && --retries);
  268.  
  269.         return ret == 2 ? 0 : -1;
  270. }
  271.  
  272. static bool drm_edid_is_zero(u8 *in_edid, int length)
  273. {
  274.         int i;
  275.         u32 *raw_edid = (u32 *)in_edid;
  276.  
  277.         for (i = 0; i < length / 4; i++)
  278.                 if (*(raw_edid + i) != 0)
  279.                         return false;
  280.         return true;
  281. }
  282.  
  283. static u8 *
  284. drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
  285. {
  286.         int i, j = 0, valid_extensions = 0;
  287.         u8 *block, *new;
  288.     size_t alloc_size;
  289.  
  290.         if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
  291.                 return NULL;
  292.  
  293.         /* base block fetch */
  294.         for (i = 0; i < 4; i++) {
  295.                 if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
  296.                         goto out;
  297.                 if (drm_edid_block_valid(block))
  298.                         break;
  299.                 if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
  300.                         connector->null_edid_counter++;
  301.                         goto carp;
  302.                 }
  303.         }
  304.         if (i == 4)
  305.                 goto carp;
  306.  
  307.         /* if there's no extensions, we're done */
  308.         if (block[0x7e] == 0)
  309.                 return block;
  310.  
  311.     alloc_size = (block[0x7e] + 1) * EDID_LENGTH ;
  312.  
  313.     new = kmalloc(alloc_size, GFP_KERNEL);
  314.  
  315.         if (!new)
  316.                 goto out;
  317.  
  318.     memcpy(new, block, EDID_LENGTH);
  319.     kfree(block);
  320.  
  321.         block = new;
  322.  
  323.         for (j = 1; j <= block[0x7e]; j++) {
  324.                 for (i = 0; i < 4; i++) {
  325.                         if (drm_do_probe_ddc_edid(adapter,
  326.                                   block + (valid_extensions + 1) * EDID_LENGTH,
  327.                                   j, EDID_LENGTH))
  328.                                 goto out;
  329.                         if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH)) {
  330.                                 valid_extensions++;
  331.                                 break;
  332.                 }
  333.                 }
  334.                 if (i == 4)
  335.                         dev_warn(connector->dev->dev,
  336.                          "%s: Ignoring invalid EDID block %d.\n",
  337.                          drm_get_connector_name(connector), j);
  338.         }
  339.  
  340.         if (valid_extensions != block[0x7e]) {
  341.                 block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
  342.                 block[0x7e] = valid_extensions;
  343.         new = kmalloc((valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
  344.         if (!new)
  345.                         goto out;
  346.         memcpy(new, block, alloc_size);
  347.         kfree(block);
  348.                 block = new;
  349.         }
  350.  
  351.         return block;
  352.  
  353. carp:
  354.         dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
  355.                  drm_get_connector_name(connector), j);
  356.  
  357. out:
  358.         kfree(block);
  359.         return NULL;
  360. }
  361.  
  362. /**
  363.  * Probe DDC presence.
  364.  *
  365.  * \param adapter : i2c device adaptor
  366.  * \return 1 on success
  367.  */
  368. static bool
  369. drm_probe_ddc(struct i2c_adapter *adapter)
  370. {
  371.         unsigned char out;
  372.  
  373.         return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
  374. }
  375.  
  376. /**
  377.  * drm_get_edid - get EDID data, if available
  378.  * @connector: connector we're probing
  379.  * @adapter: i2c adapter to use for DDC
  380.  *
  381.  * Poke the given i2c channel to grab EDID data if possible.  If found,
  382.  * attach it to the connector.
  383.  *
  384.  * Return edid data or NULL if we couldn't find any.
  385.  */
  386. struct edid *drm_get_edid(struct drm_connector *connector,
  387.                           struct i2c_adapter *adapter)
  388. {
  389.         struct edid *edid = NULL;
  390.  
  391.         if (drm_probe_ddc(adapter))
  392.                 edid = (struct edid *)drm_do_get_edid(connector, adapter);
  393.  
  394.         connector->display_info.raw_edid = (char *)edid;
  395.  
  396.         return edid;
  397.  
  398. }
  399. EXPORT_SYMBOL(drm_get_edid);
  400.  
  401. /*** EDID parsing ***/
  402.  
  403. /**
  404.  * edid_vendor - match a string against EDID's obfuscated vendor field
  405.  * @edid: EDID to match
  406.  * @vendor: vendor string
  407.  *
  408.  * Returns true if @vendor is in @edid, false otherwise
  409.  */
  410. static bool edid_vendor(struct edid *edid, char *vendor)
  411. {
  412.         char edid_vendor[3];
  413.  
  414.         edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
  415.         edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
  416.                           ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
  417.         edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
  418.  
  419.         return !strncmp(edid_vendor, vendor, 3);
  420. }
  421.  
  422. /**
  423.  * edid_get_quirks - return quirk flags for a given EDID
  424.  * @edid: EDID to process
  425.  *
  426.  * This tells subsequent routines what fixes they need to apply.
  427.  */
  428. static u32 edid_get_quirks(struct edid *edid)
  429. {
  430.         struct edid_quirk *quirk;
  431.         int i;
  432.  
  433.         for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
  434.                 quirk = &edid_quirk_list[i];
  435.  
  436.                 if (edid_vendor(edid, quirk->vendor) &&
  437.                     (EDID_PRODUCT_ID(edid) == quirk->product_id))
  438.                         return quirk->quirks;
  439.         }
  440.  
  441.         return 0;
  442. }
  443.  
  444. #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
  445. #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
  446.  
  447. /**
  448.  * edid_fixup_preferred - set preferred modes based on quirk list
  449.  * @connector: has mode list to fix up
  450.  * @quirks: quirks list
  451.  *
  452.  * Walk the mode list for @connector, clearing the preferred status
  453.  * on existing modes and setting it anew for the right mode ala @quirks.
  454.  */
  455. static void edid_fixup_preferred(struct drm_connector *connector,
  456.                                  u32 quirks)
  457. {
  458.         struct drm_display_mode *t, *cur_mode, *preferred_mode;
  459.         int target_refresh = 0;
  460.  
  461.         if (list_empty(&connector->probed_modes))
  462.                 return;
  463.  
  464.         if (quirks & EDID_QUIRK_PREFER_LARGE_60)
  465.                 target_refresh = 60;
  466.         if (quirks & EDID_QUIRK_PREFER_LARGE_75)
  467.                 target_refresh = 75;
  468.  
  469.         preferred_mode = list_first_entry(&connector->probed_modes,
  470.                                           struct drm_display_mode, head);
  471.  
  472.         list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
  473.                 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  474.  
  475.                 if (cur_mode == preferred_mode)
  476.                         continue;
  477.  
  478.                 /* Largest mode is preferred */
  479.                 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
  480.                         preferred_mode = cur_mode;
  481.  
  482.                 /* At a given size, try to get closest to target refresh */
  483.                 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
  484.                     MODE_REFRESH_DIFF(cur_mode, target_refresh) <
  485.                     MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
  486.                         preferred_mode = cur_mode;
  487.                 }
  488.         }
  489.  
  490.         preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
  491. }
  492.  
  493. struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
  494.                         int hsize, int vsize, int fresh)
  495. {
  496.         struct drm_display_mode *mode = NULL;
  497.         int i;
  498.  
  499.         for (i = 0; i < drm_num_dmt_modes; i++) {
  500.                 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
  501.                 if (hsize == ptr->hdisplay &&
  502.                         vsize == ptr->vdisplay &&
  503.                         fresh == drm_mode_vrefresh(ptr)) {
  504.                         /* get the expected default mode */
  505.                         mode = drm_mode_duplicate(dev, ptr);
  506.                         break;
  507.                 }
  508.         }
  509.         return mode;
  510. }
  511. EXPORT_SYMBOL(drm_mode_find_dmt);
  512.  
  513. typedef void detailed_cb(struct detailed_timing *timing, void *closure);
  514.  
  515. static void
  516. cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
  517. {
  518.         int i, n = 0;
  519.         u8 rev = ext[0x01], d = ext[0x02];
  520.         u8 *det_base = ext + d;
  521.  
  522.         switch (rev) {
  523.         case 0:
  524.                 /* can't happen */
  525.                 return;
  526.         case 1:
  527.                 /* have to infer how many blocks we have, check pixel clock */
  528.                 for (i = 0; i < 6; i++)
  529.                         if (det_base[18*i] || det_base[18*i+1])
  530.                                 n++;
  531.                 break;
  532.         default:
  533.                 /* explicit count */
  534.                 n = min(ext[0x03] & 0x0f, 6);
  535.                 break;
  536.         }
  537.  
  538.         for (i = 0; i < n; i++)
  539.                 cb((struct detailed_timing *)(det_base + 18 * i), closure);
  540. }
  541.  
  542. static void
  543. vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
  544. {
  545.         unsigned int i, n = min((int)ext[0x02], 6);
  546.         u8 *det_base = ext + 5;
  547.  
  548.         if (ext[0x01] != 1)
  549.                 return; /* unknown version */
  550.  
  551.         for (i = 0; i < n; i++)
  552.                 cb((struct detailed_timing *)(det_base + 18 * i), closure);
  553. }
  554.  
  555. static void
  556. drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
  557. {
  558.         int i;
  559.         struct edid *edid = (struct edid *)raw_edid;
  560.  
  561.         if (edid == NULL)
  562.                 return;
  563.  
  564.         for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
  565.                 cb(&(edid->detailed_timings[i]), closure);
  566.  
  567.         for (i = 1; i <= raw_edid[0x7e]; i++) {
  568.                 u8 *ext = raw_edid + (i * EDID_LENGTH);
  569.                 switch (*ext) {
  570.                 case CEA_EXT:
  571.                         cea_for_each_detailed_block(ext, cb, closure);
  572.                         break;
  573.                 case VTB_EXT:
  574.                         vtb_for_each_detailed_block(ext, cb, closure);
  575.                         break;
  576.                 default:
  577.                         break;
  578.                 }
  579.         }
  580. }
  581.  
  582. static void
  583. is_rb(struct detailed_timing *t, void *data)
  584. {
  585.         u8 *r = (u8 *)t;
  586.         if (r[3] == EDID_DETAIL_MONITOR_RANGE)
  587.                 if (r[15] & 0x10)
  588.                         *(bool *)data = true;
  589. }
  590.  
  591. /* EDID 1.4 defines this explicitly.  For EDID 1.3, we guess, badly. */
  592. static bool
  593. drm_monitor_supports_rb(struct edid *edid)
  594. {
  595.         if (edid->revision >= 4) {
  596.                 bool ret;
  597.                 drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
  598.                 return ret;
  599.         }
  600.  
  601.         return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
  602. }
  603.  
  604. static void
  605. find_gtf2(struct detailed_timing *t, void *data)
  606. {
  607.         u8 *r = (u8 *)t;
  608.         if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
  609.                 *(u8 **)data = r;
  610. }
  611.  
  612. /* Secondary GTF curve kicks in above some break frequency */
  613. static int
  614. drm_gtf2_hbreak(struct edid *edid)
  615. {
  616.         u8 *r = NULL;
  617.         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  618.         return r ? (r[12] * 2) : 0;
  619. }
  620.  
  621. static int
  622. drm_gtf2_2c(struct edid *edid)
  623. {
  624.         u8 *r = NULL;
  625.         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  626.         return r ? r[13] : 0;
  627. }
  628.  
  629. static int
  630. drm_gtf2_m(struct edid *edid)
  631. {
  632.         u8 *r = NULL;
  633.         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  634.         return r ? (r[15] << 8) + r[14] : 0;
  635. }
  636.  
  637. static int
  638. drm_gtf2_k(struct edid *edid)
  639. {
  640.         u8 *r = NULL;
  641.         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  642.         return r ? r[16] : 0;
  643. }
  644.  
  645. static int
  646. drm_gtf2_2j(struct edid *edid)
  647. {
  648.         u8 *r = NULL;
  649.         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  650.         return r ? r[17] : 0;
  651. }
  652.  
  653. /**
  654.  * standard_timing_level - get std. timing level(CVT/GTF/DMT)
  655.  * @edid: EDID block to scan
  656.  */
  657. static int standard_timing_level(struct edid *edid)
  658. {
  659.         if (edid->revision >= 2) {
  660.                 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
  661.                         return LEVEL_CVT;
  662.                 if (drm_gtf2_hbreak(edid))
  663.                         return LEVEL_GTF2;
  664.                 return LEVEL_GTF;
  665.         }
  666.         return LEVEL_DMT;
  667. }
  668.  
  669. /*
  670.  * 0 is reserved.  The spec says 0x01 fill for unused timings.  Some old
  671.  * monitors fill with ascii space (0x20) instead.
  672.  */
  673. static int
  674. bad_std_timing(u8 a, u8 b)
  675. {
  676.         return (a == 0x00 && b == 0x00) ||
  677.                (a == 0x01 && b == 0x01) ||
  678.                (a == 0x20 && b == 0x20);
  679. }
  680.  
  681. /**
  682.  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
  683.  * @t: standard timing params
  684.  * @timing_level: standard timing level
  685.  *
  686.  * Take the standard timing params (in this case width, aspect, and refresh)
  687.  * and convert them into a real mode using CVT/GTF/DMT.
  688.  */
  689. static struct drm_display_mode *
  690. drm_mode_std(struct drm_connector *connector, struct edid *edid,
  691.              struct std_timing *t, int revision)
  692. {
  693.         struct drm_device *dev = connector->dev;
  694.         struct drm_display_mode *m, *mode = NULL;
  695.         int hsize, vsize;
  696.         int vrefresh_rate;
  697.         unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
  698.                 >> EDID_TIMING_ASPECT_SHIFT;
  699.         unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
  700.                 >> EDID_TIMING_VFREQ_SHIFT;
  701.         int timing_level = standard_timing_level(edid);
  702.  
  703.         if (bad_std_timing(t->hsize, t->vfreq_aspect))
  704.                 return NULL;
  705.  
  706.         /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
  707.         hsize = t->hsize * 8 + 248;
  708.         /* vrefresh_rate = vfreq + 60 */
  709.         vrefresh_rate = vfreq + 60;
  710.         /* the vdisplay is calculated based on the aspect ratio */
  711.         if (aspect_ratio == 0) {
  712.                 if (revision < 3)
  713.                         vsize = hsize;
  714.                 else
  715.                 vsize = (hsize * 10) / 16;
  716.         } else if (aspect_ratio == 1)
  717.                 vsize = (hsize * 3) / 4;
  718.         else if (aspect_ratio == 2)
  719.                 vsize = (hsize * 4) / 5;
  720.         else
  721.                 vsize = (hsize * 9) / 16;
  722.  
  723.         /* HDTV hack, part 1 */
  724.         if (vrefresh_rate == 60 &&
  725.             ((hsize == 1360 && vsize == 765) ||
  726.              (hsize == 1368 && vsize == 769))) {
  727.                 hsize = 1366;
  728.                 vsize = 768;
  729.         }
  730.  
  731.         /*
  732.          * If this connector already has a mode for this size and refresh
  733.          * rate (because it came from detailed or CVT info), use that
  734.          * instead.  This way we don't have to guess at interlace or
  735.          * reduced blanking.
  736.          */
  737.         list_for_each_entry(m, &connector->probed_modes, head)
  738.                 if (m->hdisplay == hsize && m->vdisplay == vsize &&
  739.                     drm_mode_vrefresh(m) == vrefresh_rate)
  740.                         return NULL;
  741.  
  742.         /* HDTV hack, part 2 */
  743.         if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
  744.                 mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
  745.                                     false);
  746.                 mode->hdisplay = 1366;
  747.                 mode->hsync_start = mode->hsync_start - 1;
  748.                 mode->hsync_end = mode->hsync_end - 1;
  749.                 return mode;
  750.         }
  751.  
  752.         /* check whether it can be found in default mode table */
  753.         mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate);
  754.         if (mode)
  755.                 return mode;
  756.  
  757.         switch (timing_level) {
  758.         case LEVEL_DMT:
  759.                 break;
  760.         case LEVEL_GTF:
  761.                 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  762.                 break;
  763.         case LEVEL_GTF2:
  764.                 /*
  765.                  * This is potentially wrong if there's ever a monitor with
  766.                  * more than one ranges section, each claiming a different
  767.                  * secondary GTF curve.  Please don't do that.
  768.                  */
  769.                 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  770.                 if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
  771.                         kfree(mode);
  772.                         mode = drm_gtf_mode_complex(dev, hsize, vsize,
  773.                                                     vrefresh_rate, 0, 0,
  774.                                                     drm_gtf2_m(edid),
  775.                                                     drm_gtf2_2c(edid),
  776.                                                     drm_gtf2_k(edid),
  777.                                                     drm_gtf2_2j(edid));
  778.                 }
  779.                 break;
  780.         case LEVEL_CVT:
  781.                 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
  782.                                     false);
  783.                 break;
  784.         }
  785.         return mode;
  786. }
  787.  
  788. /*
  789.  * EDID is delightfully ambiguous about how interlaced modes are to be
  790.  * encoded.  Our internal representation is of frame height, but some
  791.  * HDTV detailed timings are encoded as field height.
  792.  *
  793.  * The format list here is from CEA, in frame size.  Technically we
  794.  * should be checking refresh rate too.  Whatever.
  795.  */
  796. static void
  797. drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
  798.                             struct detailed_pixel_timing *pt)
  799. {
  800.         int i;
  801.         static const struct {
  802.                 int w, h;
  803.         } cea_interlaced[] = {
  804.                 { 1920, 1080 },
  805.                 {  720,  480 },
  806.                 { 1440,  480 },
  807.                 { 2880,  480 },
  808.                 {  720,  576 },
  809.                 { 1440,  576 },
  810.                 { 2880,  576 },
  811.         };
  812.  
  813.         if (!(pt->misc & DRM_EDID_PT_INTERLACED))
  814.                 return;
  815.  
  816.         for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
  817.                 if ((mode->hdisplay == cea_interlaced[i].w) &&
  818.                     (mode->vdisplay == cea_interlaced[i].h / 2)) {
  819.                         mode->vdisplay *= 2;
  820.                         mode->vsync_start *= 2;
  821.                         mode->vsync_end *= 2;
  822.                         mode->vtotal *= 2;
  823.                         mode->vtotal |= 1;
  824.                 }
  825.         }
  826.  
  827.         mode->flags |= DRM_MODE_FLAG_INTERLACE;
  828. }
  829.  
  830. /**
  831.  * drm_mode_detailed - create a new mode from an EDID detailed timing section
  832.  * @dev: DRM device (needed to create new mode)
  833.  * @edid: EDID block
  834.  * @timing: EDID detailed timing info
  835.  * @quirks: quirks to apply
  836.  *
  837.  * An EDID detailed timing block contains enough info for us to create and
  838.  * return a new struct drm_display_mode.
  839.  */
  840. static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
  841.                                                   struct edid *edid,
  842.                                                   struct detailed_timing *timing,
  843.                                                   u32 quirks)
  844. {
  845.         struct drm_display_mode *mode;
  846.         struct detailed_pixel_timing *pt = &timing->data.pixel_data;
  847.         unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
  848.         unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
  849.         unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
  850.         unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
  851.         unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
  852.         unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
  853.         unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
  854.         unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
  855.  
  856.         /* ignore tiny modes */
  857.         if (hactive < 64 || vactive < 64)
  858.                 return NULL;
  859.  
  860.         if (pt->misc & DRM_EDID_PT_STEREO) {
  861.                 printk(KERN_WARNING "stereo mode not supported\n");
  862.                 return NULL;
  863.         }
  864.         if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
  865.                 printk(KERN_WARNING "composite sync not supported\n");
  866.         }
  867.  
  868.         /* it is incorrect if hsync/vsync width is zero */
  869.         if (!hsync_pulse_width || !vsync_pulse_width) {
  870.                 DRM_DEBUG_KMS("Incorrect Detailed timing. "
  871.                                 "Wrong Hsync/Vsync pulse width\n");
  872.                 return NULL;
  873.         }
  874.         mode = drm_mode_create(dev);
  875.         if (!mode)
  876.                 return NULL;
  877.  
  878.         mode->type = DRM_MODE_TYPE_DRIVER;
  879.  
  880.         if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
  881.                 timing->pixel_clock = cpu_to_le16(1088);
  882.  
  883.         mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
  884.  
  885.         mode->hdisplay = hactive;
  886.         mode->hsync_start = mode->hdisplay + hsync_offset;
  887.         mode->hsync_end = mode->hsync_start + hsync_pulse_width;
  888.         mode->htotal = mode->hdisplay + hblank;
  889.  
  890.         mode->vdisplay = vactive;
  891.         mode->vsync_start = mode->vdisplay + vsync_offset;
  892.         mode->vsync_end = mode->vsync_start + vsync_pulse_width;
  893.         mode->vtotal = mode->vdisplay + vblank;
  894.  
  895.         /* Some EDIDs have bogus h/vtotal values */
  896.         if (mode->hsync_end > mode->htotal)
  897.                 mode->htotal = mode->hsync_end + 1;
  898.         if (mode->vsync_end > mode->vtotal)
  899.                 mode->vtotal = mode->vsync_end + 1;
  900.  
  901.         drm_mode_do_interlace_quirk(mode, pt);
  902.  
  903.         drm_mode_set_name(mode);
  904.  
  905.         if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
  906.                 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
  907.         }
  908.  
  909.         mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
  910.                 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  911.         mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
  912.                 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  913.  
  914.         mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
  915.         mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
  916.  
  917.         if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
  918.                 mode->width_mm *= 10;
  919.                 mode->height_mm *= 10;
  920.         }
  921.  
  922.         if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
  923.                 mode->width_mm = edid->width_cm * 10;
  924.                 mode->height_mm = edid->height_cm * 10;
  925.         }
  926.  
  927.         return mode;
  928. }
  929.  
  930. static bool
  931. mode_is_rb(const struct drm_display_mode *mode)
  932. {
  933.         return (mode->htotal - mode->hdisplay == 160) &&
  934.                (mode->hsync_end - mode->hdisplay == 80) &&
  935.                (mode->hsync_end - mode->hsync_start == 32) &&
  936.                (mode->vsync_start - mode->vdisplay == 3);
  937. }
  938.  
  939. static bool
  940. mode_in_hsync_range(const struct drm_display_mode *mode,
  941.                     struct edid *edid, u8 *t)
  942. {
  943.         int hsync, hmin, hmax;
  944.  
  945.         hmin = t[7];
  946.         if (edid->revision >= 4)
  947.             hmin += ((t[4] & 0x04) ? 255 : 0);
  948.         hmax = t[8];
  949.         if (edid->revision >= 4)
  950.             hmax += ((t[4] & 0x08) ? 255 : 0);
  951.         hsync = drm_mode_hsync(mode);
  952.  
  953.         return (hsync <= hmax && hsync >= hmin);
  954. }
  955.  
  956. static bool
  957. mode_in_vsync_range(const struct drm_display_mode *mode,
  958.                     struct edid *edid, u8 *t)
  959. {
  960.         int vsync, vmin, vmax;
  961.  
  962.         vmin = t[5];
  963.         if (edid->revision >= 4)
  964.             vmin += ((t[4] & 0x01) ? 255 : 0);
  965.         vmax = t[6];
  966.         if (edid->revision >= 4)
  967.             vmax += ((t[4] & 0x02) ? 255 : 0);
  968.         vsync = drm_mode_vrefresh(mode);
  969.  
  970.         return (vsync <= vmax && vsync >= vmin);
  971. }
  972.  
  973. static u32
  974. range_pixel_clock(struct edid *edid, u8 *t)
  975. {
  976.         /* unspecified */
  977.         if (t[9] == 0 || t[9] == 255)
  978.                 return 0;
  979.  
  980.         /* 1.4 with CVT support gives us real precision, yay */
  981.         if (edid->revision >= 4 && t[10] == 0x04)
  982.                 return (t[9] * 10000) - ((t[12] >> 2) * 250);
  983.  
  984.         /* 1.3 is pathetic, so fuzz up a bit */
  985.         return t[9] * 10000 + 5001;
  986. }
  987.  
  988. static bool
  989. mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
  990.               struct detailed_timing *timing)
  991. {
  992.         u32 max_clock;
  993.         u8 *t = (u8 *)timing;
  994.  
  995.         if (!mode_in_hsync_range(mode, edid, t))
  996.                 return false;
  997.  
  998.         if (!mode_in_vsync_range(mode, edid, t))
  999.                 return false;
  1000.  
  1001.         if ((max_clock = range_pixel_clock(edid, t)))
  1002.                 if (mode->clock > max_clock)
  1003.                         return false;
  1004.  
  1005.         /* 1.4 max horizontal check */
  1006.         if (edid->revision >= 4 && t[10] == 0x04)
  1007.                 if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
  1008.                         return false;
  1009.  
  1010.         if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
  1011.                 return false;
  1012.  
  1013.         return true;
  1014. }
  1015.  
  1016. /*
  1017.  * XXX If drm_dmt_modes ever regrows the CVT-R modes (and it will) this will
  1018.  * need to account for them.
  1019.  */
  1020. static int
  1021. drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
  1022.                                    struct detailed_timing *timing)
  1023. {
  1024.         int i, modes = 0;
  1025.         struct drm_display_mode *newmode;
  1026.         struct drm_device *dev = connector->dev;
  1027.  
  1028.         for (i = 0; i < drm_num_dmt_modes; i++) {
  1029.                 if (mode_in_range(drm_dmt_modes + i, edid, timing)) {
  1030.                         newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
  1031.                         if (newmode) {
  1032.                                 drm_mode_probed_add(connector, newmode);
  1033.                                 modes++;
  1034.                         }
  1035.                 }
  1036.         }
  1037.  
  1038.         return modes;
  1039. }
  1040.  
  1041. static void
  1042. do_inferred_modes(struct detailed_timing *timing, void *c)
  1043. {
  1044.         struct detailed_mode_closure *closure = c;
  1045.         struct detailed_non_pixel *data = &timing->data.other_data;
  1046.         int gtf = (closure->edid->features & DRM_EDID_FEATURE_DEFAULT_GTF);
  1047.  
  1048.         if (gtf && data->type == EDID_DETAIL_MONITOR_RANGE)
  1049.                 closure->modes += drm_gtf_modes_for_range(closure->connector,
  1050.                                                           closure->edid,
  1051.                                                           timing);
  1052. }
  1053.  
  1054. static int
  1055. add_inferred_modes(struct drm_connector *connector, struct edid *edid)
  1056. {
  1057.         struct detailed_mode_closure closure = {
  1058.                 connector, edid, 0, 0, 0
  1059.         };
  1060.  
  1061.         if (version_greater(edid, 1, 0))
  1062.                 drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
  1063.                                             &closure);
  1064.  
  1065.         return closure.modes;
  1066. }
  1067.  
  1068. static int
  1069. drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
  1070. {
  1071.         int i, j, m, modes = 0;
  1072.         struct drm_display_mode *mode;
  1073.         u8 *est = ((u8 *)timing) + 5;
  1074.  
  1075.         for (i = 0; i < 6; i++) {
  1076.                 for (j = 7; j > 0; j--) {
  1077.                         m = (i * 8) + (7 - j);
  1078.                         if (m >= ARRAY_SIZE(est3_modes))
  1079.                                 break;
  1080.                         if (est[i] & (1 << j)) {
  1081.                                 mode = drm_mode_find_dmt(connector->dev,
  1082.                                                          est3_modes[m].w,
  1083.                                                          est3_modes[m].h,
  1084.                                                          est3_modes[m].r
  1085.                                                          /*, est3_modes[m].rb */);
  1086.                                 if (mode) {
  1087.                                         drm_mode_probed_add(connector, mode);
  1088.                                         modes++;
  1089.                                 }
  1090.                         }
  1091.                 }
  1092.         }
  1093.  
  1094.         return modes;
  1095. }
  1096.  
  1097. static void
  1098. do_established_modes(struct detailed_timing *timing, void *c)
  1099. {
  1100.         struct detailed_mode_closure *closure = c;
  1101.                 struct detailed_non_pixel *data = &timing->data.other_data;
  1102.  
  1103.         if (data->type == EDID_DETAIL_EST_TIMINGS)
  1104.                 closure->modes += drm_est3_modes(closure->connector, timing);
  1105. }
  1106.  
  1107. /**
  1108.  * add_established_modes - get est. modes from EDID and add them
  1109.  * @edid: EDID block to scan
  1110.  *
  1111.  * Each EDID block contains a bitmap of the supported "established modes" list
  1112.  * (defined above).  Tease them out and add them to the global modes list.
  1113.  */
  1114. static int
  1115. add_established_modes(struct drm_connector *connector, struct edid *edid)
  1116. {
  1117.         struct drm_device *dev = connector->dev;
  1118.         unsigned long est_bits = edid->established_timings.t1 |
  1119.                 (edid->established_timings.t2 << 8) |
  1120.                 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
  1121.         int i, modes = 0;
  1122.         struct detailed_mode_closure closure = {
  1123.                 connector, edid, 0, 0, 0
  1124.         };
  1125.  
  1126.         for (i = 0; i <= EDID_EST_TIMINGS; i++) {
  1127.                 if (est_bits & (1<<i)) {
  1128.                         struct drm_display_mode *newmode;
  1129.                         newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
  1130.                         if (newmode) {
  1131.                 drm_mode_probed_add(connector, newmode);
  1132.                                 modes++;
  1133.                         }
  1134.                 }
  1135.         }
  1136.  
  1137.         if (version_greater(edid, 1, 0))
  1138.                     drm_for_each_detailed_block((u8 *)edid,
  1139.                                                 do_established_modes, &closure);
  1140.  
  1141.         return modes + closure.modes;
  1142. }
  1143.  
  1144. static void
  1145. do_standard_modes(struct detailed_timing *timing, void *c)
  1146. {
  1147.         struct detailed_mode_closure *closure = c;
  1148.         struct detailed_non_pixel *data = &timing->data.other_data;
  1149.         struct drm_connector *connector = closure->connector;
  1150.         struct edid *edid = closure->edid;
  1151.  
  1152.         if (data->type == EDID_DETAIL_STD_MODES) {
  1153.                 int i;
  1154.                 for (i = 0; i < 6; i++) {
  1155.                                 struct std_timing *std;
  1156.                                 struct drm_display_mode *newmode;
  1157.  
  1158.                         std = &data->data.timings[i];
  1159.                         newmode = drm_mode_std(connector, edid, std,
  1160.                                                edid->revision);
  1161.                                 if (newmode) {
  1162.                                         drm_mode_probed_add(connector, newmode);
  1163.                                 closure->modes++;
  1164.                                 }
  1165.                         }
  1166.                 }
  1167. }
  1168.  
  1169. /**
  1170.  * add_standard_modes - get std. modes from EDID and add them
  1171.  * @edid: EDID block to scan
  1172.  *
  1173.  * Standard modes can be calculated using the appropriate standard (DMT,
  1174.  * GTF or CVT. Grab them from @edid and add them to the list.
  1175.  */
  1176. static int
  1177. add_standard_modes(struct drm_connector *connector, struct edid *edid)
  1178. {
  1179.         int i, modes = 0;
  1180.         struct detailed_mode_closure closure = {
  1181.                 connector, edid, 0, 0, 0
  1182.         };
  1183.  
  1184.         for (i = 0; i < EDID_STD_TIMINGS; i++) {
  1185.                 struct drm_display_mode *newmode;
  1186.  
  1187.                 newmode = drm_mode_std(connector, edid,
  1188.                                        &edid->standard_timings[i],
  1189.                                        edid->revision);
  1190.                 if (newmode) {
  1191.                         drm_mode_probed_add(connector, newmode);
  1192.                         modes++;
  1193.                 }
  1194.         }
  1195.  
  1196.         if (version_greater(edid, 1, 0))
  1197.                 drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
  1198.                                             &closure);
  1199.  
  1200.         /* XXX should also look for standard codes in VTB blocks */
  1201.  
  1202.         return modes + closure.modes;
  1203. }
  1204.  
  1205. static int drm_cvt_modes(struct drm_connector *connector,
  1206.                          struct detailed_timing *timing)
  1207. {
  1208.         int i, j, modes = 0;
  1209.         struct drm_display_mode *newmode;
  1210.         struct drm_device *dev = connector->dev;
  1211.         struct cvt_timing *cvt;
  1212.         const int rates[] = { 60, 85, 75, 60, 50 };
  1213.         const u8 empty[3] = { 0, 0, 0 };
  1214.  
  1215.         for (i = 0; i < 4; i++) {
  1216.                 int uninitialized_var(width), height;
  1217.                 cvt = &(timing->data.other_data.data.cvt[i]);
  1218.  
  1219.                 if (!memcmp(cvt->code, empty, 3))
  1220.                                 continue;
  1221.  
  1222.                 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
  1223.                 switch (cvt->code[1] & 0x0c) {
  1224.                 case 0x00:
  1225.                         width = height * 4 / 3;
  1226.                         break;
  1227.                 case 0x04:
  1228.                         width = height * 16 / 9;
  1229.                         break;
  1230.                 case 0x08:
  1231.                         width = height * 16 / 10;
  1232.                         break;
  1233.                 case 0x0c:
  1234.                         width = height * 15 / 9;
  1235.                         break;
  1236.                 }
  1237.  
  1238.                 for (j = 1; j < 5; j++) {
  1239.                         if (cvt->code[2] & (1 << j)) {
  1240.                                 newmode = drm_cvt_mode(dev, width, height,
  1241.                                                        rates[j], j == 0,
  1242.                                                        false, false);
  1243.                                 if (newmode) {
  1244.                                         drm_mode_probed_add(connector, newmode);
  1245.                                         modes++;
  1246.                                 }
  1247.                         }
  1248.                 }
  1249.                 }
  1250.  
  1251.         return modes;
  1252. }
  1253.  
  1254. static void
  1255. do_cvt_mode(struct detailed_timing *timing, void *c)
  1256. {
  1257.         struct detailed_mode_closure *closure = c;
  1258.         struct detailed_non_pixel *data = &timing->data.other_data;
  1259.  
  1260.         if (data->type == EDID_DETAIL_CVT_3BYTE)
  1261.                 closure->modes += drm_cvt_modes(closure->connector, timing);
  1262. }
  1263.  
  1264. static int
  1265. add_cvt_modes(struct drm_connector *connector, struct edid *edid)
  1266. {      
  1267.         struct detailed_mode_closure closure = {
  1268.                 connector, edid, 0, 0, 0
  1269.         };
  1270.  
  1271.         if (version_greater(edid, 1, 2))
  1272.                 drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
  1273.  
  1274.         /* XXX should also look for CVT codes in VTB blocks */
  1275.  
  1276.         return closure.modes;
  1277. }
  1278.  
  1279. static void
  1280. do_detailed_mode(struct detailed_timing *timing, void *c)
  1281. {
  1282.         struct detailed_mode_closure *closure = c;
  1283.         struct drm_display_mode *newmode;
  1284.  
  1285.         if (timing->pixel_clock) {
  1286.                 newmode = drm_mode_detailed(closure->connector->dev,
  1287.                                             closure->edid, timing,
  1288.                                             closure->quirks);
  1289.                 if (!newmode)
  1290.                         return;
  1291.  
  1292.                 if (closure->preferred)
  1293.                         newmode->type |= DRM_MODE_TYPE_PREFERRED;
  1294.  
  1295.                 drm_mode_probed_add(closure->connector, newmode);
  1296.                 closure->modes++;
  1297.                 closure->preferred = 0;
  1298.         }
  1299. }
  1300.  
  1301. /*
  1302.  * add_detailed_modes - Add modes from detailed timings
  1303.  * @connector: attached connector
  1304.  * @edid: EDID block to scan
  1305.  * @quirks: quirks to apply
  1306.  */
  1307. static int
  1308. add_detailed_modes(struct drm_connector *connector, struct edid *edid,
  1309.                    u32 quirks)
  1310. {
  1311.         struct detailed_mode_closure closure = {
  1312.                 connector,
  1313.                 edid,
  1314.                 1,
  1315.                 quirks,
  1316.                 0
  1317.         };
  1318.  
  1319.         if (closure.preferred && !version_greater(edid, 1, 3))
  1320.                 closure.preferred =
  1321.                     (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
  1322.  
  1323.         drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
  1324.  
  1325.         return closure.modes;
  1326. }
  1327.  
  1328. #define HDMI_IDENTIFIER 0x000C03
  1329. #define AUDIO_BLOCK     0x01
  1330. #define VENDOR_BLOCK    0x03
  1331. #define EDID_BASIC_AUDIO        (1 << 6)
  1332.  
  1333. /**
  1334.  * Search EDID for CEA extension block.
  1335.  */
  1336. u8 *drm_find_cea_extension(struct edid *edid)
  1337. {
  1338.         u8 *edid_ext = NULL;
  1339.         int i;
  1340.  
  1341.         /* No EDID or EDID extensions */
  1342.         if (edid == NULL || edid->extensions == 0)
  1343.                 return NULL;
  1344.  
  1345.         /* Find CEA extension */
  1346.         for (i = 0; i < edid->extensions; i++) {
  1347.                 edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
  1348.                 if (edid_ext[0] == CEA_EXT)
  1349.                         break;
  1350.         }
  1351.  
  1352.         if (i == edid->extensions)
  1353.                 return NULL;
  1354.  
  1355.         return edid_ext;
  1356. }
  1357. EXPORT_SYMBOL(drm_find_cea_extension);
  1358.  
  1359. /**
  1360.  * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
  1361.  * @edid: monitor EDID information
  1362.  *
  1363.  * Parse the CEA extension according to CEA-861-B.
  1364.  * Return true if HDMI, false if not or unknown.
  1365.  */
  1366. bool drm_detect_hdmi_monitor(struct edid *edid)
  1367. {
  1368.         u8 *edid_ext;
  1369.         int i, hdmi_id;
  1370.         int start_offset, end_offset;
  1371.         bool is_hdmi = false;
  1372.  
  1373.         edid_ext = drm_find_cea_extension(edid);
  1374.         if (!edid_ext)
  1375.                 goto end;
  1376.  
  1377.         /* Data block offset in CEA extension block */
  1378.         start_offset = 4;
  1379.         end_offset = edid_ext[2];
  1380.  
  1381.         /*
  1382.          * Because HDMI identifier is in Vendor Specific Block,
  1383.          * search it from all data blocks of CEA extension.
  1384.          */
  1385.         for (i = start_offset; i < end_offset;
  1386.                 /* Increased by data block len */
  1387.                 i += ((edid_ext[i] & 0x1f) + 1)) {
  1388.                 /* Find vendor specific block */
  1389.                 if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
  1390.                         hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
  1391.                                   edid_ext[i + 3] << 16;
  1392.                         /* Find HDMI identifier */
  1393.                         if (hdmi_id == HDMI_IDENTIFIER)
  1394.                                 is_hdmi = true;
  1395.                         break;
  1396.                 }
  1397.         }
  1398.  
  1399. end:
  1400.         return is_hdmi;
  1401. }
  1402. EXPORT_SYMBOL(drm_detect_hdmi_monitor);
  1403.  
  1404. /**
  1405.  * drm_detect_monitor_audio - check monitor audio capability
  1406.  *
  1407.  * Monitor should have CEA extension block.
  1408.  * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
  1409.  * audio' only. If there is any audio extension block and supported
  1410.  * audio format, assume at least 'basic audio' support, even if 'basic
  1411.  * audio' is not defined in EDID.
  1412.  *
  1413.  */
  1414. bool drm_detect_monitor_audio(struct edid *edid)
  1415. {
  1416.         u8 *edid_ext;
  1417.         int i, j;
  1418.         bool has_audio = false;
  1419.         int start_offset, end_offset;
  1420.  
  1421.         edid_ext = drm_find_cea_extension(edid);
  1422.         if (!edid_ext)
  1423.                 goto end;
  1424.  
  1425.         has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
  1426.  
  1427.         if (has_audio) {
  1428.                 DRM_DEBUG_KMS("Monitor has basic audio support\n");
  1429.                 goto end;
  1430.         }
  1431.  
  1432.         /* Data block offset in CEA extension block */
  1433.         start_offset = 4;
  1434.         end_offset = edid_ext[2];
  1435.  
  1436.         for (i = start_offset; i < end_offset;
  1437.                         i += ((edid_ext[i] & 0x1f) + 1)) {
  1438.                 if ((edid_ext[i] >> 5) == AUDIO_BLOCK) {
  1439.                         has_audio = true;
  1440.                         for (j = 1; j < (edid_ext[i] & 0x1f); j += 3)
  1441.                                 DRM_DEBUG_KMS("CEA audio format %d\n",
  1442.                                               (edid_ext[i + j] >> 3) & 0xf);
  1443.                         goto end;
  1444.                 }
  1445.         }
  1446. end:
  1447.         return has_audio;
  1448. }
  1449. EXPORT_SYMBOL(drm_detect_monitor_audio);
  1450.  
  1451. /**
  1452.  * drm_add_display_info - pull display info out if present
  1453.  * @edid: EDID data
  1454.  * @info: display info (attached to connector)
  1455.  *
  1456.  * Grab any available display info and stuff it into the drm_display_info
  1457.  * structure that's part of the connector.  Useful for tracking bpp and
  1458.  * color spaces.
  1459.  */
  1460. static void drm_add_display_info(struct edid *edid,
  1461.                                  struct drm_display_info *info)
  1462. {
  1463.         u8 *edid_ext;
  1464.  
  1465.         info->width_mm = edid->width_cm * 10;
  1466.         info->height_mm = edid->height_cm * 10;
  1467.  
  1468.         /* driver figures it out in this case */
  1469.         info->bpc = 0;
  1470.         info->color_formats = 0;
  1471.  
  1472.         /* Only defined for 1.4 with digital displays */
  1473.         if (edid->revision < 4)
  1474.                 return;
  1475.  
  1476.         if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
  1477.                 return;
  1478.  
  1479.         switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
  1480.         case DRM_EDID_DIGITAL_DEPTH_6:
  1481.                 info->bpc = 6;
  1482.                 break;
  1483.         case DRM_EDID_DIGITAL_DEPTH_8:
  1484.                 info->bpc = 8;
  1485.                 break;
  1486.         case DRM_EDID_DIGITAL_DEPTH_10:
  1487.                 info->bpc = 10;
  1488.                 break;
  1489.         case DRM_EDID_DIGITAL_DEPTH_12:
  1490.                 info->bpc = 12;
  1491.                 break;
  1492.         case DRM_EDID_DIGITAL_DEPTH_14:
  1493.                 info->bpc = 14;
  1494.                 break;
  1495.         case DRM_EDID_DIGITAL_DEPTH_16:
  1496.                 info->bpc = 16;
  1497.                 break;
  1498.         case DRM_EDID_DIGITAL_DEPTH_UNDEF:
  1499.         default:
  1500.                 info->bpc = 0;
  1501.                 break;
  1502.         }
  1503.  
  1504.         info->color_formats = DRM_COLOR_FORMAT_RGB444;
  1505.         if (info->color_formats & DRM_EDID_FEATURE_RGB_YCRCB444)
  1506.                 info->color_formats = DRM_COLOR_FORMAT_YCRCB444;
  1507.         if (info->color_formats & DRM_EDID_FEATURE_RGB_YCRCB422)
  1508.                 info->color_formats = DRM_COLOR_FORMAT_YCRCB422;
  1509.  
  1510.         /* Get data from CEA blocks if present */
  1511.         edid_ext = drm_find_cea_extension(edid);
  1512.         if (!edid_ext)
  1513.                 return;
  1514.  
  1515.         info->cea_rev = edid_ext[1];
  1516. }
  1517.  
  1518. /**
  1519.  * drm_add_edid_modes - add modes from EDID data, if available
  1520.  * @connector: connector we're probing
  1521.  * @edid: edid data
  1522.  *
  1523.  * Add the specified modes to the connector's mode list.
  1524.  *
  1525.  * Return number of modes added or 0 if we couldn't find any.
  1526.  */
  1527. int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
  1528. {
  1529.         int num_modes = 0;
  1530.         u32 quirks;
  1531.  
  1532.         if (edid == NULL) {
  1533.                 return 0;
  1534.         }
  1535.         if (!drm_edid_is_valid(edid)) {
  1536.                 dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
  1537.                          drm_get_connector_name(connector));
  1538.                 return 0;
  1539.         }
  1540.  
  1541.         quirks = edid_get_quirks(edid);
  1542.  
  1543.         /*
  1544.          * EDID spec says modes should be preferred in this order:
  1545.          * - preferred detailed mode
  1546.          * - other detailed modes from base block
  1547.          * - detailed modes from extension blocks
  1548.          * - CVT 3-byte code modes
  1549.          * - standard timing codes
  1550.          * - established timing codes
  1551.          * - modes inferred from GTF or CVT range information
  1552.          *
  1553.          * We get this pretty much right.
  1554.          *
  1555.          * XXX order for additional mode types in extension blocks?
  1556.          */
  1557.         num_modes += add_detailed_modes(connector, edid, quirks);
  1558.         num_modes += add_cvt_modes(connector, edid);
  1559.         num_modes += add_standard_modes(connector, edid);
  1560.         num_modes += add_established_modes(connector, edid);
  1561.         num_modes += add_inferred_modes(connector, edid);
  1562.  
  1563.         if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
  1564.                 edid_fixup_preferred(connector, quirks);
  1565.  
  1566.         drm_add_display_info(edid, &connector->display_info);
  1567.  
  1568.         return num_modes;
  1569. }
  1570. EXPORT_SYMBOL(drm_add_edid_modes);
  1571.  
  1572. /**
  1573.  * drm_add_modes_noedid - add modes for the connectors without EDID
  1574.  * @connector: connector we're probing
  1575.  * @hdisplay: the horizontal display limit
  1576.  * @vdisplay: the vertical display limit
  1577.  *
  1578.  * Add the specified modes to the connector's mode list. Only when the
  1579.  * hdisplay/vdisplay is not beyond the given limit, it will be added.
  1580.  *
  1581.  * Return number of modes added or 0 if we couldn't find any.
  1582.  */
  1583. int drm_add_modes_noedid(struct drm_connector *connector,
  1584.                         int hdisplay, int vdisplay)
  1585. {
  1586.         int i, count, num_modes = 0;
  1587.         struct drm_display_mode *mode;
  1588.         struct drm_device *dev = connector->dev;
  1589.  
  1590.         count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
  1591.         if (hdisplay < 0)
  1592.                 hdisplay = 0;
  1593.         if (vdisplay < 0)
  1594.                 vdisplay = 0;
  1595.  
  1596.         for (i = 0; i < count; i++) {
  1597.                 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
  1598.                 if (hdisplay && vdisplay) {
  1599.                         /*
  1600.                          * Only when two are valid, they will be used to check
  1601.                          * whether the mode should be added to the mode list of
  1602.                          * the connector.
  1603.                          */
  1604.                         if (ptr->hdisplay > hdisplay ||
  1605.                                         ptr->vdisplay > vdisplay)
  1606.                                 continue;
  1607.                 }
  1608.                 if (drm_mode_vrefresh(ptr) > 61)
  1609.                         continue;
  1610.                 mode = drm_mode_duplicate(dev, ptr);
  1611.                 if (mode) {
  1612.                         drm_mode_probed_add(connector, mode);
  1613.                         num_modes++;
  1614.                 }
  1615.         }
  1616.         return num_modes;
  1617. }
  1618. EXPORT_SYMBOL(drm_add_modes_noedid);
  1619.