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