1/* 2 * Copyright (C) 2008 Maarten Maathuis. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 */ 26 27#include <acpi/button.h> 28 29#include <linux/pm_runtime.h> 30 31#include <drm/drmP.h> 32#include <drm/drm_edid.h> 33#include <drm/drm_crtc_helper.h> 34 35#include "nouveau_reg.h" 36#include "nouveau_drm.h" 37#include "dispnv04/hw.h" 38#include "nouveau_acpi.h" 39 40#include "nouveau_display.h" 41#include "nouveau_connector.h" 42#include "nouveau_encoder.h" 43#include "nouveau_crtc.h" 44 45#include <nvif/event.h> 46 47MODULE_PARM_DESC(tv_disable, "Disable TV-out detection"); 48int nouveau_tv_disable = 0; 49module_param_named(tv_disable, nouveau_tv_disable, int, 0400); 50 51MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status"); 52int nouveau_ignorelid = 0; 53module_param_named(ignorelid, nouveau_ignorelid, int, 0400); 54 55MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)"); 56int nouveau_duallink = 1; 57module_param_named(duallink, nouveau_duallink, int, 0400); 58 59struct nouveau_encoder * 60find_encoder(struct drm_connector *connector, int type) 61{ 62 struct drm_device *dev = connector->dev; 63 struct nouveau_encoder *nv_encoder; 64 struct drm_encoder *enc; 65 int i, id; 66 67 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 68 id = connector->encoder_ids[i]; 69 if (!id) 70 break; 71 72 enc = drm_encoder_find(dev, id); 73 if (!enc) 74 continue; 75 nv_encoder = nouveau_encoder(enc); 76 77 if (type == DCB_OUTPUT_ANY || 78 (nv_encoder->dcb && nv_encoder->dcb->type == type)) 79 return nv_encoder; 80 } 81 82 return NULL; 83} 84 85struct nouveau_connector * 86nouveau_encoder_connector_get(struct nouveau_encoder *encoder) 87{ 88 struct drm_device *dev = to_drm_encoder(encoder)->dev; 89 struct drm_connector *drm_connector; 90 91 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) { 92 if (drm_connector->encoder == to_drm_encoder(encoder)) 93 return nouveau_connector(drm_connector); 94 } 95 96 return NULL; 97} 98 99static void 100nouveau_connector_destroy(struct drm_connector *connector) 101{ 102 struct nouveau_connector *nv_connector = nouveau_connector(connector); 103 nvif_notify_fini(&nv_connector->hpd); 104 kfree(nv_connector->edid); 105 drm_connector_unregister(connector); 106 drm_connector_cleanup(connector); 107 if (nv_connector->aux.transfer) 108 drm_dp_aux_unregister(&nv_connector->aux); 109 kfree(connector); 110} 111 112static struct nouveau_encoder * 113nouveau_connector_ddc_detect(struct drm_connector *connector) 114{ 115 struct drm_device *dev = connector->dev; 116 struct nouveau_connector *nv_connector = nouveau_connector(connector); 117 struct nouveau_drm *drm = nouveau_drm(dev); 118 struct nvkm_gpio *gpio = nvxx_gpio(&drm->device); 119 struct nouveau_encoder *nv_encoder; 120 struct drm_encoder *encoder; 121 int i, panel = -ENODEV; 122 123 /* eDP panels need powering on by us (if the VBIOS doesn't default it 124 * to on) before doing any AUX channel transactions. LVDS panel power 125 * is handled by the SOR itself, and not required for LVDS DDC. 126 */ 127 if (nv_connector->type == DCB_CONNECTOR_eDP) { 128 panel = gpio->get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff); 129 if (panel == 0) { 130 gpio->set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1); 131 msleep(300); 132 } 133 } 134 135 for (i = 0; nv_encoder = NULL, i < DRM_CONNECTOR_MAX_ENCODER; i++) { 136 int id = connector->encoder_ids[i]; 137 if (id == 0) 138 break; 139 140 encoder = drm_encoder_find(dev, id); 141 if (!encoder) 142 continue; 143 nv_encoder = nouveau_encoder(encoder); 144 145 if (nv_encoder->dcb->type == DCB_OUTPUT_DP) { 146 int ret = nouveau_dp_detect(nv_encoder); 147 if (ret == 0) 148 break; 149 } else 150 if (nv_encoder->i2c) { 151 if (nv_probe_i2c(nv_encoder->i2c, 0x50)) 152 break; 153 } 154 } 155 156 /* eDP panel not detected, restore panel power GPIO to previous 157 * state to avoid confusing the SOR for other output types. 158 */ 159 if (!nv_encoder && panel == 0) 160 gpio->set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, panel); 161 162 return nv_encoder; 163} 164 165static struct nouveau_encoder * 166nouveau_connector_of_detect(struct drm_connector *connector) 167{ 168#ifdef __powerpc__ 169 struct drm_device *dev = connector->dev; 170 struct nouveau_connector *nv_connector = nouveau_connector(connector); 171 struct nouveau_encoder *nv_encoder; 172 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev); 173 174 if (!dn || 175 !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) || 176 (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG)))) 177 return NULL; 178 179 for_each_child_of_node(dn, cn) { 180 const char *name = of_get_property(cn, "name", NULL); 181 const void *edid = of_get_property(cn, "EDID", NULL); 182 int idx = name ? name[strlen(name) - 1] - 'A' : 0; 183 184 if (nv_encoder->dcb->i2c_index == idx && edid) { 185 nv_connector->edid = 186 kmemdup(edid, EDID_LENGTH, GFP_KERNEL); 187 of_node_put(cn); 188 return nv_encoder; 189 } 190 } 191#endif 192 return NULL; 193} 194 195static void 196nouveau_connector_set_encoder(struct drm_connector *connector, 197 struct nouveau_encoder *nv_encoder) 198{ 199 struct nouveau_connector *nv_connector = nouveau_connector(connector); 200 struct nouveau_drm *drm = nouveau_drm(connector->dev); 201 struct drm_device *dev = connector->dev; 202 203 if (nv_connector->detected_encoder == nv_encoder) 204 return; 205 nv_connector->detected_encoder = nv_encoder; 206 207 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 208 connector->interlace_allowed = true; 209 connector->doublescan_allowed = true; 210 } else 211 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS || 212 nv_encoder->dcb->type == DCB_OUTPUT_TMDS) { 213 connector->doublescan_allowed = false; 214 connector->interlace_allowed = false; 215 } else { 216 connector->doublescan_allowed = true; 217 if (drm->device.info.family == NV_DEVICE_INFO_V0_KELVIN || 218 (drm->device.info.family == NV_DEVICE_INFO_V0_CELSIUS && 219 (dev->pdev->device & 0x0ff0) != 0x0100 && 220 (dev->pdev->device & 0x0ff0) != 0x0150)) 221 /* HW is broken */ 222 connector->interlace_allowed = false; 223 else 224 connector->interlace_allowed = true; 225 } 226 227 if (nv_connector->type == DCB_CONNECTOR_DVI_I) { 228 drm_object_property_set_value(&connector->base, 229 dev->mode_config.dvi_i_subconnector_property, 230 nv_encoder->dcb->type == DCB_OUTPUT_TMDS ? 231 DRM_MODE_SUBCONNECTOR_DVID : 232 DRM_MODE_SUBCONNECTOR_DVIA); 233 } 234} 235 236static enum drm_connector_status 237nouveau_connector_detect(struct drm_connector *connector, bool force) 238{ 239 struct drm_device *dev = connector->dev; 240 struct nouveau_drm *drm = nouveau_drm(dev); 241 struct nouveau_connector *nv_connector = nouveau_connector(connector); 242 struct nouveau_encoder *nv_encoder = NULL; 243 struct nouveau_encoder *nv_partner; 244 struct nvkm_i2c_port *i2c; 245 int type; 246 int ret; 247 enum drm_connector_status conn_status = connector_status_disconnected; 248 249 /* Cleanup the previous EDID block. */ 250 if (nv_connector->edid) { 251 drm_mode_connector_update_edid_property(connector, NULL); 252 kfree(nv_connector->edid); 253 nv_connector->edid = NULL; 254 } 255 256 ret = pm_runtime_get_sync(connector->dev->dev); 257 if (ret < 0 && ret != -EACCES) 258 return conn_status; 259 260 nv_encoder = nouveau_connector_ddc_detect(connector); 261 if (nv_encoder && (i2c = nv_encoder->i2c) != NULL) { 262 nv_connector->edid = drm_get_edid(connector, &i2c->adapter); 263 drm_mode_connector_update_edid_property(connector, 264 nv_connector->edid); 265 if (!nv_connector->edid) { 266 NV_ERROR(drm, "DDC responded, but no EDID for %s\n", 267 connector->name); 268 goto detect_analog; 269 } 270 271 /* Override encoder type for DVI-I based on whether EDID 272 * says the display is digital or analog, both use the 273 * same i2c channel so the value returned from ddc_detect 274 * isn't necessarily correct. 275 */ 276 nv_partner = NULL; 277 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS) 278 nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG); 279 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG) 280 nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS); 281 282 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG && 283 nv_partner->dcb->type == DCB_OUTPUT_TMDS) || 284 (nv_encoder->dcb->type == DCB_OUTPUT_TMDS && 285 nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) { 286 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL) 287 type = DCB_OUTPUT_TMDS; 288 else 289 type = DCB_OUTPUT_ANALOG; 290 291 nv_encoder = find_encoder(connector, type); 292 } 293 294 nouveau_connector_set_encoder(connector, nv_encoder); 295 conn_status = connector_status_connected; 296 goto out; 297 } 298 299 nv_encoder = nouveau_connector_of_detect(connector); 300 if (nv_encoder) { 301 nouveau_connector_set_encoder(connector, nv_encoder); 302 conn_status = connector_status_connected; 303 goto out; 304 } 305 306detect_analog: 307 nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG); 308 if (!nv_encoder && !nouveau_tv_disable) 309 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV); 310 if (nv_encoder && force) { 311 struct drm_encoder *encoder = to_drm_encoder(nv_encoder); 312 const struct drm_encoder_helper_funcs *helper = 313 encoder->helper_private; 314 315 if (helper->detect(encoder, connector) == 316 connector_status_connected) { 317 nouveau_connector_set_encoder(connector, nv_encoder); 318 conn_status = connector_status_connected; 319 goto out; 320 } 321 322 } 323 324 out: 325 326 pm_runtime_mark_last_busy(connector->dev->dev); 327 pm_runtime_put_autosuspend(connector->dev->dev); 328 329 return conn_status; 330} 331 332static enum drm_connector_status 333nouveau_connector_detect_lvds(struct drm_connector *connector, bool force) 334{ 335 struct drm_device *dev = connector->dev; 336 struct nouveau_drm *drm = nouveau_drm(dev); 337 struct nouveau_connector *nv_connector = nouveau_connector(connector); 338 struct nouveau_encoder *nv_encoder = NULL; 339 enum drm_connector_status status = connector_status_disconnected; 340 341 /* Cleanup the previous EDID block. */ 342 if (nv_connector->edid) { 343 drm_mode_connector_update_edid_property(connector, NULL); 344 kfree(nv_connector->edid); 345 nv_connector->edid = NULL; 346 } 347 348 nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS); 349 if (!nv_encoder) 350 return connector_status_disconnected; 351 352 /* Try retrieving EDID via DDC */ 353 if (!drm->vbios.fp_no_ddc) { 354 status = nouveau_connector_detect(connector, force); 355 if (status == connector_status_connected) 356 goto out; 357 } 358 359 /* On some laptops (Sony, i'm looking at you) there appears to 360 * be no direct way of accessing the panel's EDID. The only 361 * option available to us appears to be to ask ACPI for help.. 362 * 363 * It's important this check's before trying straps, one of the 364 * said manufacturer's laptops are configured in such a way 365 * the nouveau decides an entry in the VBIOS FP mode table is 366 * valid - it's not (rh#613284) 367 */ 368 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) { 369 if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) { 370 status = connector_status_connected; 371 goto out; 372 } 373 } 374 375 /* If no EDID found above, and the VBIOS indicates a hardcoded 376 * modeline is avalilable for the panel, set it as the panel's 377 * native mode and exit. 378 */ 379 if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc || 380 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) { 381 status = connector_status_connected; 382 goto out; 383 } 384 385 /* Still nothing, some VBIOS images have a hardcoded EDID block 386 * stored for the panel stored in them. 387 */ 388 if (!drm->vbios.fp_no_ddc) { 389 struct edid *edid = 390 (struct edid *)nouveau_bios_embedded_edid(dev); 391 if (edid) { 392 nv_connector->edid = 393 kmemdup(edid, EDID_LENGTH, GFP_KERNEL); 394 if (nv_connector->edid) 395 status = connector_status_connected; 396 } 397 } 398 399out: 400#if defined(CONFIG_ACPI_BUTTON) || \ 401 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE)) 402 if (status == connector_status_connected && 403 !nouveau_ignorelid && !acpi_lid_open()) 404 status = connector_status_unknown; 405#endif 406 407 drm_mode_connector_update_edid_property(connector, nv_connector->edid); 408 nouveau_connector_set_encoder(connector, nv_encoder); 409 return status; 410} 411 412static void 413nouveau_connector_force(struct drm_connector *connector) 414{ 415 struct nouveau_drm *drm = nouveau_drm(connector->dev); 416 struct nouveau_connector *nv_connector = nouveau_connector(connector); 417 struct nouveau_encoder *nv_encoder; 418 int type; 419 420 if (nv_connector->type == DCB_CONNECTOR_DVI_I) { 421 if (connector->force == DRM_FORCE_ON_DIGITAL) 422 type = DCB_OUTPUT_TMDS; 423 else 424 type = DCB_OUTPUT_ANALOG; 425 } else 426 type = DCB_OUTPUT_ANY; 427 428 nv_encoder = find_encoder(connector, type); 429 if (!nv_encoder) { 430 NV_ERROR(drm, "can't find encoder to force %s on!\n", 431 connector->name); 432 connector->status = connector_status_disconnected; 433 return; 434 } 435 436 nouveau_connector_set_encoder(connector, nv_encoder); 437} 438 439static int 440nouveau_connector_set_property(struct drm_connector *connector, 441 struct drm_property *property, uint64_t value) 442{ 443 struct nouveau_display *disp = nouveau_display(connector->dev); 444 struct nouveau_connector *nv_connector = nouveau_connector(connector); 445 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder; 446 struct drm_encoder *encoder = to_drm_encoder(nv_encoder); 447 struct drm_device *dev = connector->dev; 448 struct nouveau_crtc *nv_crtc; 449 int ret; 450 451 nv_crtc = NULL; 452 if (connector->encoder && connector->encoder->crtc) 453 nv_crtc = nouveau_crtc(connector->encoder->crtc); 454 455 /* Scaling mode */ 456 if (property == dev->mode_config.scaling_mode_property) { 457 bool modeset = false; 458 459 switch (value) { 460 case DRM_MODE_SCALE_NONE: 461 /* We allow 'None' for EDID modes, even on a fixed 462 * panel (some exist with support for lower refresh 463 * rates, which people might want to use for power 464 * saving purposes). 465 * 466 * Non-EDID modes will force the use of GPU scaling 467 * to the native mode regardless of this setting. 468 */ 469 switch (nv_connector->type) { 470 case DCB_CONNECTOR_LVDS: 471 case DCB_CONNECTOR_LVDS_SPWG: 472 case DCB_CONNECTOR_eDP: 473 /* ... except prior to G80, where the code 474 * doesn't support such things. 475 */ 476 if (disp->disp.oclass < NV50_DISP) 477 return -EINVAL; 478 break; 479 default: 480 break; 481 } 482 break; 483 case DRM_MODE_SCALE_FULLSCREEN: 484 case DRM_MODE_SCALE_CENTER: 485 case DRM_MODE_SCALE_ASPECT: 486 break; 487 default: 488 return -EINVAL; 489 } 490 491 /* Changing between GPU and panel scaling requires a full 492 * modeset 493 */ 494 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) || 495 (value == DRM_MODE_SCALE_NONE)) 496 modeset = true; 497 nv_connector->scaling_mode = value; 498 499 if (!nv_crtc) 500 return 0; 501 502 if (modeset || !nv_crtc->set_scale) { 503 ret = drm_crtc_helper_set_mode(&nv_crtc->base, 504 &nv_crtc->base.mode, 505 nv_crtc->base.x, 506 nv_crtc->base.y, NULL); 507 if (!ret) 508 return -EINVAL; 509 } else { 510 ret = nv_crtc->set_scale(nv_crtc, true); 511 if (ret) 512 return ret; 513 } 514 515 return 0; 516 } 517 518 /* Underscan */ 519 if (property == disp->underscan_property) { 520 if (nv_connector->underscan != value) { 521 nv_connector->underscan = value; 522 if (!nv_crtc || !nv_crtc->set_scale) 523 return 0; 524 525 return nv_crtc->set_scale(nv_crtc, true); 526 } 527 528 return 0; 529 } 530 531 if (property == disp->underscan_hborder_property) { 532 if (nv_connector->underscan_hborder != value) { 533 nv_connector->underscan_hborder = value; 534 if (!nv_crtc || !nv_crtc->set_scale) 535 return 0; 536 537 return nv_crtc->set_scale(nv_crtc, true); 538 } 539 540 return 0; 541 } 542 543 if (property == disp->underscan_vborder_property) { 544 if (nv_connector->underscan_vborder != value) { 545 nv_connector->underscan_vborder = value; 546 if (!nv_crtc || !nv_crtc->set_scale) 547 return 0; 548 549 return nv_crtc->set_scale(nv_crtc, true); 550 } 551 552 return 0; 553 } 554 555 /* Dithering */ 556 if (property == disp->dithering_mode) { 557 nv_connector->dithering_mode = value; 558 if (!nv_crtc || !nv_crtc->set_dither) 559 return 0; 560 561 return nv_crtc->set_dither(nv_crtc, true); 562 } 563 564 if (property == disp->dithering_depth) { 565 nv_connector->dithering_depth = value; 566 if (!nv_crtc || !nv_crtc->set_dither) 567 return 0; 568 569 return nv_crtc->set_dither(nv_crtc, true); 570 } 571 572 if (nv_crtc && nv_crtc->set_color_vibrance) { 573 /* Hue */ 574 if (property == disp->vibrant_hue_property) { 575 nv_crtc->vibrant_hue = value - 90; 576 return nv_crtc->set_color_vibrance(nv_crtc, true); 577 } 578 /* Saturation */ 579 if (property == disp->color_vibrance_property) { 580 nv_crtc->color_vibrance = value - 100; 581 return nv_crtc->set_color_vibrance(nv_crtc, true); 582 } 583 } 584 585 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV) 586 return get_slave_funcs(encoder)->set_property( 587 encoder, connector, property, value); 588 589 return -EINVAL; 590} 591 592static struct drm_display_mode * 593nouveau_connector_native_mode(struct drm_connector *connector) 594{ 595 const struct drm_connector_helper_funcs *helper = connector->helper_private; 596 struct nouveau_drm *drm = nouveau_drm(connector->dev); 597 struct nouveau_connector *nv_connector = nouveau_connector(connector); 598 struct drm_device *dev = connector->dev; 599 struct drm_display_mode *mode, *largest = NULL; 600 int high_w = 0, high_h = 0, high_v = 0; 601 602 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) { 603 mode->vrefresh = drm_mode_vrefresh(mode); 604 if (helper->mode_valid(connector, mode) != MODE_OK || 605 (mode->flags & DRM_MODE_FLAG_INTERLACE)) 606 continue; 607 608 /* Use preferred mode if there is one.. */ 609 if (mode->type & DRM_MODE_TYPE_PREFERRED) { 610 NV_DEBUG(drm, "native mode from preferred\n"); 611 return drm_mode_duplicate(dev, mode); 612 } 613 614 /* Otherwise, take the resolution with the largest width, then 615 * height, then vertical refresh 616 */ 617 if (mode->hdisplay < high_w) 618 continue; 619 620 if (mode->hdisplay == high_w && mode->vdisplay < high_h) 621 continue; 622 623 if (mode->hdisplay == high_w && mode->vdisplay == high_h && 624 mode->vrefresh < high_v) 625 continue; 626 627 high_w = mode->hdisplay; 628 high_h = mode->vdisplay; 629 high_v = mode->vrefresh; 630 largest = mode; 631 } 632 633 NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n", 634 high_w, high_h, high_v); 635 return largest ? drm_mode_duplicate(dev, largest) : NULL; 636} 637 638struct moderec { 639 int hdisplay; 640 int vdisplay; 641}; 642 643static struct moderec scaler_modes[] = { 644 { 1920, 1200 }, 645 { 1920, 1080 }, 646 { 1680, 1050 }, 647 { 1600, 1200 }, 648 { 1400, 1050 }, 649 { 1280, 1024 }, 650 { 1280, 960 }, 651 { 1152, 864 }, 652 { 1024, 768 }, 653 { 800, 600 }, 654 { 720, 400 }, 655 { 640, 480 }, 656 { 640, 400 }, 657 { 640, 350 }, 658 {} 659}; 660 661static int 662nouveau_connector_scaler_modes_add(struct drm_connector *connector) 663{ 664 struct nouveau_connector *nv_connector = nouveau_connector(connector); 665 struct drm_display_mode *native = nv_connector->native_mode, *m; 666 struct drm_device *dev = connector->dev; 667 struct moderec *mode = &scaler_modes[0]; 668 int modes = 0; 669 670 if (!native) 671 return 0; 672 673 while (mode->hdisplay) { 674 if (mode->hdisplay <= native->hdisplay && 675 mode->vdisplay <= native->vdisplay && 676 (mode->hdisplay != native->hdisplay || 677 mode->vdisplay != native->vdisplay)) { 678 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay, 679 drm_mode_vrefresh(native), false, 680 false, false); 681 if (!m) 682 continue; 683 684 drm_mode_probed_add(connector, m); 685 modes++; 686 } 687 688 mode++; 689 } 690 691 return modes; 692} 693 694static void 695nouveau_connector_detect_depth(struct drm_connector *connector) 696{ 697 struct nouveau_drm *drm = nouveau_drm(connector->dev); 698 struct nouveau_connector *nv_connector = nouveau_connector(connector); 699 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder; 700 struct nvbios *bios = &drm->vbios; 701 struct drm_display_mode *mode = nv_connector->native_mode; 702 bool duallink; 703 704 /* if the edid is feeling nice enough to provide this info, use it */ 705 if (nv_connector->edid && connector->display_info.bpc) 706 return; 707 708 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */ 709 if (nv_connector->type == DCB_CONNECTOR_eDP) { 710 connector->display_info.bpc = 6; 711 return; 712 } 713 714 /* we're out of options unless we're LVDS, default to 8bpc */ 715 if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) { 716 connector->display_info.bpc = 8; 717 return; 718 } 719 720 connector->display_info.bpc = 6; 721 722 /* LVDS: panel straps */ 723 if (bios->fp_no_ddc) { 724 if (bios->fp.if_is_24bit) 725 connector->display_info.bpc = 8; 726 return; 727 } 728 729 /* LVDS: DDC panel, need to first determine the number of links to 730 * know which if_is_24bit flag to check... 731 */ 732 if (nv_connector->edid && 733 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) 734 duallink = ((u8 *)nv_connector->edid)[121] == 2; 735 else 736 duallink = mode->clock >= bios->fp.duallink_transition_clk; 737 738 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) || 739 ( duallink && (bios->fp.strapless_is_24bit & 2))) 740 connector->display_info.bpc = 8; 741} 742 743static int 744nouveau_connector_get_modes(struct drm_connector *connector) 745{ 746 struct drm_device *dev = connector->dev; 747 struct nouveau_drm *drm = nouveau_drm(dev); 748 struct nouveau_connector *nv_connector = nouveau_connector(connector); 749 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder; 750 struct drm_encoder *encoder = to_drm_encoder(nv_encoder); 751 int ret = 0; 752 753 /* destroy the native mode, the attached monitor could have changed. 754 */ 755 if (nv_connector->native_mode) { 756 drm_mode_destroy(dev, nv_connector->native_mode); 757 nv_connector->native_mode = NULL; 758 } 759 760 if (nv_connector->edid) 761 ret = drm_add_edid_modes(connector, nv_connector->edid); 762 else 763 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS && 764 (nv_encoder->dcb->lvdsconf.use_straps_for_mode || 765 drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) { 766 struct drm_display_mode mode; 767 768 nouveau_bios_fp_mode(dev, &mode); 769 nv_connector->native_mode = drm_mode_duplicate(dev, &mode); 770 } 771 772 /* Determine display colour depth for everything except LVDS now, 773 * DP requires this before mode_valid() is called. 774 */ 775 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS) 776 nouveau_connector_detect_depth(connector); 777 778 /* Find the native mode if this is a digital panel, if we didn't 779 * find any modes through DDC previously add the native mode to 780 * the list of modes. 781 */ 782 if (!nv_connector->native_mode) 783 nv_connector->native_mode = 784 nouveau_connector_native_mode(connector); 785 if (ret == 0 && nv_connector->native_mode) { 786 struct drm_display_mode *mode; 787 788 mode = drm_mode_duplicate(dev, nv_connector->native_mode); 789 drm_mode_probed_add(connector, mode); 790 ret = 1; 791 } 792 793 /* Determine LVDS colour depth, must happen after determining 794 * "native" mode as some VBIOS tables require us to use the 795 * pixel clock as part of the lookup... 796 */ 797 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS) 798 nouveau_connector_detect_depth(connector); 799 800 if (nv_encoder->dcb->type == DCB_OUTPUT_TV) 801 ret = get_slave_funcs(encoder)->get_modes(encoder, connector); 802 803 if (nv_connector->type == DCB_CONNECTOR_LVDS || 804 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG || 805 nv_connector->type == DCB_CONNECTOR_eDP) 806 ret += nouveau_connector_scaler_modes_add(connector); 807 808 return ret; 809} 810 811static unsigned 812get_tmds_link_bandwidth(struct drm_connector *connector) 813{ 814 struct nouveau_connector *nv_connector = nouveau_connector(connector); 815 struct nouveau_drm *drm = nouveau_drm(connector->dev); 816 struct dcb_output *dcb = nv_connector->detected_encoder->dcb; 817 818 if (dcb->location != DCB_LOC_ON_CHIP || 819 drm->device.info.chipset >= 0x46) 820 return 165000; 821 else if (drm->device.info.chipset >= 0x40) 822 return 155000; 823 else if (drm->device.info.chipset >= 0x18) 824 return 135000; 825 else 826 return 112000; 827} 828 829static int 830nouveau_connector_mode_valid(struct drm_connector *connector, 831 struct drm_display_mode *mode) 832{ 833 struct nouveau_connector *nv_connector = nouveau_connector(connector); 834 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder; 835 struct drm_encoder *encoder = to_drm_encoder(nv_encoder); 836 unsigned min_clock = 25000, max_clock = min_clock; 837 unsigned clock = mode->clock; 838 839 switch (nv_encoder->dcb->type) { 840 case DCB_OUTPUT_LVDS: 841 if (nv_connector->native_mode && 842 (mode->hdisplay > nv_connector->native_mode->hdisplay || 843 mode->vdisplay > nv_connector->native_mode->vdisplay)) 844 return MODE_PANEL; 845 846 min_clock = 0; 847 max_clock = 400000; 848 break; 849 case DCB_OUTPUT_TMDS: 850 max_clock = get_tmds_link_bandwidth(connector); 851 if (nouveau_duallink && nv_encoder->dcb->duallink_possible) 852 max_clock *= 2; 853 break; 854 case DCB_OUTPUT_ANALOG: 855 max_clock = nv_encoder->dcb->crtconf.maxfreq; 856 if (!max_clock) 857 max_clock = 350000; 858 break; 859 case DCB_OUTPUT_TV: 860 return get_slave_funcs(encoder)->mode_valid(encoder, mode); 861 case DCB_OUTPUT_DP: 862 max_clock = nv_encoder->dp.link_nr; 863 max_clock *= nv_encoder->dp.link_bw; 864 clock = clock * (connector->display_info.bpc * 3) / 10; 865 break; 866 default: 867 BUG_ON(1); 868 return MODE_BAD; 869 } 870 871 if (clock < min_clock) 872 return MODE_CLOCK_LOW; 873 874 if (clock > max_clock) 875 return MODE_CLOCK_HIGH; 876 877 return MODE_OK; 878} 879 880static struct drm_encoder * 881nouveau_connector_best_encoder(struct drm_connector *connector) 882{ 883 struct nouveau_connector *nv_connector = nouveau_connector(connector); 884 885 if (nv_connector->detected_encoder) 886 return to_drm_encoder(nv_connector->detected_encoder); 887 888 return NULL; 889} 890 891static const struct drm_connector_helper_funcs 892nouveau_connector_helper_funcs = { 893 .get_modes = nouveau_connector_get_modes, 894 .mode_valid = nouveau_connector_mode_valid, 895 .best_encoder = nouveau_connector_best_encoder, 896}; 897 898static const struct drm_connector_funcs 899nouveau_connector_funcs = { 900 .dpms = drm_helper_connector_dpms, 901 .save = NULL, 902 .restore = NULL, 903 .detect = nouveau_connector_detect, 904 .destroy = nouveau_connector_destroy, 905 .fill_modes = drm_helper_probe_single_connector_modes, 906 .set_property = nouveau_connector_set_property, 907 .force = nouveau_connector_force 908}; 909 910static const struct drm_connector_funcs 911nouveau_connector_funcs_lvds = { 912 .dpms = drm_helper_connector_dpms, 913 .save = NULL, 914 .restore = NULL, 915 .detect = nouveau_connector_detect_lvds, 916 .destroy = nouveau_connector_destroy, 917 .fill_modes = drm_helper_probe_single_connector_modes, 918 .set_property = nouveau_connector_set_property, 919 .force = nouveau_connector_force 920}; 921 922static void 923nouveau_connector_dp_dpms(struct drm_connector *connector, int mode) 924{ 925 struct nouveau_encoder *nv_encoder = NULL; 926 927 if (connector->encoder) 928 nv_encoder = nouveau_encoder(connector->encoder); 929 if (nv_encoder && nv_encoder->dcb && 930 nv_encoder->dcb->type == DCB_OUTPUT_DP) { 931 if (mode == DRM_MODE_DPMS_ON) { 932 u8 data = DP_SET_POWER_D0; 933 nv_wraux(nv_encoder->i2c, DP_SET_POWER, &data, 1); 934 usleep_range(1000, 2000); 935 } else { 936 u8 data = DP_SET_POWER_D3; 937 nv_wraux(nv_encoder->i2c, DP_SET_POWER, &data, 1); 938 } 939 } 940 941 drm_helper_connector_dpms(connector, mode); 942} 943 944static const struct drm_connector_funcs 945nouveau_connector_funcs_dp = { 946 .dpms = nouveau_connector_dp_dpms, 947 .save = NULL, 948 .restore = NULL, 949 .detect = nouveau_connector_detect, 950 .destroy = nouveau_connector_destroy, 951 .fill_modes = drm_helper_probe_single_connector_modes, 952 .set_property = nouveau_connector_set_property, 953 .force = nouveau_connector_force 954}; 955 956static int 957nouveau_connector_hotplug(struct nvif_notify *notify) 958{ 959 struct nouveau_connector *nv_connector = 960 container_of(notify, typeof(*nv_connector), hpd); 961 struct drm_connector *connector = &nv_connector->base; 962 struct nouveau_drm *drm = nouveau_drm(connector->dev); 963 const struct nvif_notify_conn_rep_v0 *rep = notify->data; 964 const char *name = connector->name; 965 966 if (rep->mask & NVIF_NOTIFY_CONN_V0_IRQ) { 967 } else { 968 bool plugged = (rep->mask != NVIF_NOTIFY_CONN_V0_UNPLUG); 969 970 NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un", name); 971 972 mutex_lock(&drm->dev->mode_config.mutex); 973 if (plugged) 974 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); 975 else 976 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); 977 mutex_unlock(&drm->dev->mode_config.mutex); 978 979 drm_helper_hpd_irq_event(connector->dev); 980 } 981 982 return NVIF_NOTIFY_KEEP; 983} 984 985static ssize_t 986nouveau_connector_aux_xfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg) 987{ 988 struct nouveau_connector *nv_connector = 989 container_of(aux, typeof(*nv_connector), aux); 990 struct nouveau_encoder *nv_encoder; 991 struct nvkm_i2c_port *port; 992 int ret; 993 994 nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP); 995 if (!nv_encoder || !(port = nv_encoder->i2c)) 996 return -ENODEV; 997 if (WARN_ON(msg->size > 16)) 998 return -E2BIG; 999 if (msg->size == 0) 1000 return msg->size; 1001 1002 ret = nvkm_i2c(port)->acquire(port, 0); 1003 if (ret) 1004 return ret; 1005 1006 ret = port->func->aux(port, false, msg->request, msg->address, 1007 msg->buffer, msg->size); 1008 nvkm_i2c(port)->release(port); 1009 if (ret >= 0) { 1010 msg->reply = ret; 1011 return msg->size; 1012 } 1013 1014 return ret; 1015} 1016 1017static int 1018drm_conntype_from_dcb(enum dcb_connector_type dcb) 1019{ 1020 switch (dcb) { 1021 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA; 1022 case DCB_CONNECTOR_TV_0 : 1023 case DCB_CONNECTOR_TV_1 : 1024 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV; 1025 case DCB_CONNECTOR_DMS59_0 : 1026 case DCB_CONNECTOR_DMS59_1 : 1027 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII; 1028 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID; 1029 case DCB_CONNECTOR_LVDS : 1030 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS; 1031 case DCB_CONNECTOR_DMS59_DP0: 1032 case DCB_CONNECTOR_DMS59_DP1: 1033 case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort; 1034 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP; 1035 case DCB_CONNECTOR_HDMI_0 : 1036 case DCB_CONNECTOR_HDMI_1 : 1037 case DCB_CONNECTOR_HDMI_C : return DRM_MODE_CONNECTOR_HDMIA; 1038 default: 1039 break; 1040 } 1041 1042 return DRM_MODE_CONNECTOR_Unknown; 1043} 1044 1045struct drm_connector * 1046nouveau_connector_create(struct drm_device *dev, int index) 1047{ 1048 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs; 1049 struct nouveau_drm *drm = nouveau_drm(dev); 1050 struct nouveau_display *disp = nouveau_display(dev); 1051 struct nouveau_connector *nv_connector = NULL; 1052 struct drm_connector *connector; 1053 int type, ret = 0; 1054 bool dummy; 1055 1056 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1057 nv_connector = nouveau_connector(connector); 1058 if (nv_connector->index == index) 1059 return connector; 1060 } 1061 1062 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL); 1063 if (!nv_connector) 1064 return ERR_PTR(-ENOMEM); 1065 1066 connector = &nv_connector->base; 1067 nv_connector->index = index; 1068 1069 /* attempt to parse vbios connector type and hotplug gpio */ 1070 nv_connector->dcb = olddcb_conn(dev, index); 1071 if (nv_connector->dcb) { 1072 u32 entry = ROM16(nv_connector->dcb[0]); 1073 if (olddcb_conntab(dev)[3] >= 4) 1074 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16; 1075 1076 nv_connector->type = nv_connector->dcb[0]; 1077 if (drm_conntype_from_dcb(nv_connector->type) == 1078 DRM_MODE_CONNECTOR_Unknown) { 1079 NV_WARN(drm, "unknown connector type %02x\n", 1080 nv_connector->type); 1081 nv_connector->type = DCB_CONNECTOR_NONE; 1082 } 1083 1084 /* Gigabyte NX85T */ 1085 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) { 1086 if (nv_connector->type == DCB_CONNECTOR_HDMI_1) 1087 nv_connector->type = DCB_CONNECTOR_DVI_I; 1088 } 1089 1090 /* Gigabyte GV-NX86T512H */ 1091 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) { 1092 if (nv_connector->type == DCB_CONNECTOR_HDMI_1) 1093 nv_connector->type = DCB_CONNECTOR_DVI_I; 1094 } 1095 } else { 1096 nv_connector->type = DCB_CONNECTOR_NONE; 1097 } 1098 1099 /* no vbios data, or an unknown dcb connector type - attempt to 1100 * figure out something suitable ourselves 1101 */ 1102 if (nv_connector->type == DCB_CONNECTOR_NONE) { 1103 struct nouveau_drm *drm = nouveau_drm(dev); 1104 struct dcb_table *dcbt = &drm->vbios.dcb; 1105 u32 encoders = 0; 1106 int i; 1107 1108 for (i = 0; i < dcbt->entries; i++) { 1109 if (dcbt->entry[i].connector == nv_connector->index) 1110 encoders |= (1 << dcbt->entry[i].type); 1111 } 1112 1113 if (encoders & (1 << DCB_OUTPUT_DP)) { 1114 if (encoders & (1 << DCB_OUTPUT_TMDS)) 1115 nv_connector->type = DCB_CONNECTOR_DP; 1116 else 1117 nv_connector->type = DCB_CONNECTOR_eDP; 1118 } else 1119 if (encoders & (1 << DCB_OUTPUT_TMDS)) { 1120 if (encoders & (1 << DCB_OUTPUT_ANALOG)) 1121 nv_connector->type = DCB_CONNECTOR_DVI_I; 1122 else 1123 nv_connector->type = DCB_CONNECTOR_DVI_D; 1124 } else 1125 if (encoders & (1 << DCB_OUTPUT_ANALOG)) { 1126 nv_connector->type = DCB_CONNECTOR_VGA; 1127 } else 1128 if (encoders & (1 << DCB_OUTPUT_LVDS)) { 1129 nv_connector->type = DCB_CONNECTOR_LVDS; 1130 } else 1131 if (encoders & (1 << DCB_OUTPUT_TV)) { 1132 nv_connector->type = DCB_CONNECTOR_TV_0; 1133 } 1134 } 1135 1136 switch ((type = drm_conntype_from_dcb(nv_connector->type))) { 1137 case DRM_MODE_CONNECTOR_LVDS: 1138 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy); 1139 if (ret) { 1140 NV_ERROR(drm, "Error parsing LVDS table, disabling\n"); 1141 kfree(nv_connector); 1142 return ERR_PTR(ret); 1143 } 1144 1145 funcs = &nouveau_connector_funcs_lvds; 1146 break; 1147 case DRM_MODE_CONNECTOR_DisplayPort: 1148 case DRM_MODE_CONNECTOR_eDP: 1149 nv_connector->aux.dev = dev->dev; 1150 nv_connector->aux.transfer = nouveau_connector_aux_xfer; 1151 ret = drm_dp_aux_register(&nv_connector->aux); 1152 if (ret) { 1153 NV_ERROR(drm, "failed to register aux channel\n"); 1154 kfree(nv_connector); 1155 return ERR_PTR(ret); 1156 } 1157 1158 funcs = &nouveau_connector_funcs_dp; 1159 break; 1160 default: 1161 funcs = &nouveau_connector_funcs; 1162 break; 1163 } 1164 1165 /* defaults, will get overridden in detect() */ 1166 connector->interlace_allowed = false; 1167 connector->doublescan_allowed = false; 1168 1169 drm_connector_init(dev, connector, funcs, type); 1170 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs); 1171 1172 /* Init DVI-I specific properties */ 1173 if (nv_connector->type == DCB_CONNECTOR_DVI_I) 1174 drm_object_attach_property(&connector->base, dev->mode_config.dvi_i_subconnector_property, 0); 1175 1176 /* Add overscan compensation options to digital outputs */ 1177 if (disp->underscan_property && 1178 (type == DRM_MODE_CONNECTOR_DVID || 1179 type == DRM_MODE_CONNECTOR_DVII || 1180 type == DRM_MODE_CONNECTOR_HDMIA || 1181 type == DRM_MODE_CONNECTOR_DisplayPort)) { 1182 drm_object_attach_property(&connector->base, 1183 disp->underscan_property, 1184 UNDERSCAN_OFF); 1185 drm_object_attach_property(&connector->base, 1186 disp->underscan_hborder_property, 1187 0); 1188 drm_object_attach_property(&connector->base, 1189 disp->underscan_vborder_property, 1190 0); 1191 } 1192 1193 /* Add hue and saturation options */ 1194 if (disp->vibrant_hue_property) 1195 drm_object_attach_property(&connector->base, 1196 disp->vibrant_hue_property, 1197 90); 1198 if (disp->color_vibrance_property) 1199 drm_object_attach_property(&connector->base, 1200 disp->color_vibrance_property, 1201 150); 1202 1203 /* default scaling mode */ 1204 switch (nv_connector->type) { 1205 case DCB_CONNECTOR_LVDS: 1206 case DCB_CONNECTOR_LVDS_SPWG: 1207 case DCB_CONNECTOR_eDP: 1208 /* see note in nouveau_connector_set_property() */ 1209 if (disp->disp.oclass < NV50_DISP) { 1210 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN; 1211 break; 1212 } 1213 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE; 1214 break; 1215 default: 1216 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE; 1217 break; 1218 } 1219 1220 /* scaling mode property */ 1221 switch (nv_connector->type) { 1222 case DCB_CONNECTOR_TV_0: 1223 case DCB_CONNECTOR_TV_1: 1224 case DCB_CONNECTOR_TV_3: 1225 break; 1226 case DCB_CONNECTOR_VGA: 1227 if (disp->disp.oclass < NV50_DISP) 1228 break; /* can only scale on DFPs */ 1229 /* fall-through */ 1230 default: 1231 drm_object_attach_property(&connector->base, dev->mode_config. 1232 scaling_mode_property, 1233 nv_connector->scaling_mode); 1234 break; 1235 } 1236 1237 /* dithering properties */ 1238 switch (nv_connector->type) { 1239 case DCB_CONNECTOR_TV_0: 1240 case DCB_CONNECTOR_TV_1: 1241 case DCB_CONNECTOR_TV_3: 1242 case DCB_CONNECTOR_VGA: 1243 break; 1244 default: 1245 if (disp->dithering_mode) { 1246 drm_object_attach_property(&connector->base, 1247 disp->dithering_mode, 1248 nv_connector-> 1249 dithering_mode); 1250 nv_connector->dithering_mode = DITHERING_MODE_AUTO; 1251 } 1252 if (disp->dithering_depth) { 1253 drm_object_attach_property(&connector->base, 1254 disp->dithering_depth, 1255 nv_connector-> 1256 dithering_depth); 1257 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO; 1258 } 1259 break; 1260 } 1261 1262 ret = nvif_notify_init(&disp->disp, NULL, nouveau_connector_hotplug, 1263 true, NV04_DISP_NTFY_CONN, 1264 &(struct nvif_notify_conn_req_v0) { 1265 .mask = NVIF_NOTIFY_CONN_V0_ANY, 1266 .conn = index, 1267 }, 1268 sizeof(struct nvif_notify_conn_req_v0), 1269 sizeof(struct nvif_notify_conn_rep_v0), 1270 &nv_connector->hpd); 1271 if (ret) 1272 connector->polled = DRM_CONNECTOR_POLL_CONNECT; 1273 else 1274 connector->polled = DRM_CONNECTOR_POLL_HPD; 1275 1276 drm_connector_register(connector); 1277 return connector; 1278} 1279