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