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