Set memory attributes on page
[pscnv.git] / pscnv / nouveau_connector.c
blobef31dda744b1158c8354c1a412f92fd1bf2084b4
1 /*
2 * Copyright (C) 2008 Maarten Maathuis.
3 * All Rights Reserved.
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:
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.
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.
27 #ifdef __linux__
28 #include <acpi/button.h>
29 #endif
31 #include "nouveau_drv.h"
32 #include "drm_edid.h"
33 #include "drm_crtc_helper.h"
35 #include "nouveau_reg.h"
36 #include "nouveau_encoder.h"
37 #include "nouveau_crtc.h"
38 #include "nouveau_connector.h"
39 #include "nouveau_hw.h"
40 #include "pscnv_kapi.h"
42 static struct nouveau_encoder *
43 find_encoder_by_type(struct drm_connector *connector, int type)
45 struct drm_device *dev = connector->dev;
46 struct nouveau_encoder *nv_encoder;
47 struct drm_mode_object *obj;
48 int i, id;
50 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
51 id = connector->encoder_ids[i];
52 if (!id)
53 break;
55 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
56 if (!obj)
57 continue;
58 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
60 if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
61 return nv_encoder;
64 return NULL;
67 struct nouveau_connector *
68 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
70 struct drm_device *dev = to_drm_encoder(encoder)->dev;
71 struct drm_connector *drm_connector;
73 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
74 if (drm_connector->encoder == to_drm_encoder(encoder))
75 return nouveau_connector(drm_connector);
78 return NULL;
81 /*TODO: This could use improvement, and learn to handle the fixed
82 * BIOS tables etc. It's fine currently, for its only user.
84 int
85 nouveau_connector_bpp(struct drm_connector *connector)
87 struct nouveau_connector *nv_connector = nouveau_connector(connector);
89 if (nv_connector->edid && nv_connector->edid->revision >= 4) {
90 u8 bpc = ((nv_connector->edid->input & 0x70) >> 3) + 4;
91 if (bpc > 4)
92 return bpc;
95 return 18;
98 static void
99 nouveau_connector_destroy(struct drm_connector *drm_connector)
101 struct nouveau_connector *nv_connector =
102 nouveau_connector(drm_connector);
103 struct drm_device *dev;
105 if (!nv_connector)
106 return;
108 dev = nv_connector->base.dev;
109 NV_DEBUG_KMS(dev, "\n");
111 kfree(nv_connector->edid);
112 #ifdef __linux__
113 drm_sysfs_connector_remove(drm_connector);
114 #endif
115 drm_connector_cleanup(drm_connector);
116 kfree(drm_connector);
119 static struct nouveau_i2c_chan *
120 nouveau_connector_ddc_detect(struct drm_connector *connector,
121 struct nouveau_encoder **pnv_encoder)
123 struct drm_device *dev = connector->dev;
124 int i;
126 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
127 struct nouveau_i2c_chan *i2c = NULL;
128 struct nouveau_encoder *nv_encoder;
129 struct drm_mode_object *obj;
130 int id;
132 id = connector->encoder_ids[i];
133 if (!id)
134 break;
136 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
137 if (!obj)
138 continue;
139 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
141 if (nv_encoder->dcb->i2c_index < 0xf)
142 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
144 if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
145 *pnv_encoder = nv_encoder;
146 return i2c;
150 return NULL;
153 static struct nouveau_encoder *
154 nouveau_connector_of_detect(struct drm_connector *connector)
156 #ifdef __powerpc__
157 struct drm_device *dev = connector->dev;
158 struct nouveau_connector *nv_connector = nouveau_connector(connector);
159 struct nouveau_encoder *nv_encoder;
160 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
162 if (!dn ||
163 !((nv_encoder = find_encoder_by_type(connector, OUTPUT_TMDS)) ||
164 (nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG))))
165 return NULL;
167 for_each_child_of_node(dn, cn) {
168 const char *name = of_get_property(cn, "name", NULL);
169 const void *edid = of_get_property(cn, "EDID", NULL);
170 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
172 if (nv_encoder->dcb->i2c_index == idx && edid) {
173 nv_connector->edid =
174 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
175 of_node_put(cn);
176 return nv_encoder;
179 #endif
180 return NULL;
183 static void
184 nouveau_connector_set_encoder(struct drm_connector *connector,
185 struct nouveau_encoder *nv_encoder)
187 struct nouveau_connector *nv_connector = nouveau_connector(connector);
188 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
189 struct drm_device *dev = connector->dev;
191 if (nv_connector->detected_encoder == nv_encoder)
192 return;
193 nv_connector->detected_encoder = nv_encoder;
195 if (nv_encoder->dcb->type == OUTPUT_LVDS ||
196 nv_encoder->dcb->type == OUTPUT_TMDS) {
197 connector->doublescan_allowed = false;
198 connector->interlace_allowed = false;
199 } else {
200 connector->doublescan_allowed = true;
201 if (dev_priv->card_type == NV_20 ||
202 (dev_priv->card_type == NV_10 &&
203 (dev->pci_device & 0x0ff0) != 0x0100 &&
204 (dev->pci_device & 0x0ff0) != 0x0150))
205 /* HW is broken */
206 connector->interlace_allowed = false;
207 else
208 connector->interlace_allowed = true;
211 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
212 drm_connector_property_set_value(connector,
213 dev->mode_config.dvi_i_subconnector_property,
214 nv_encoder->dcb->type == OUTPUT_TMDS ?
215 DRM_MODE_SUBCONNECTOR_DVID :
216 DRM_MODE_SUBCONNECTOR_DVIA);
220 static enum drm_connector_status
221 #ifdef PSCNV_KAPI_DRM_CONNECTOR_DETECT_1
222 nouveau_connector_detect(struct drm_connector *connector)
223 #else
224 #ifdef PSCNV_KAPI_DRM_CONNECTOR_DETECT_2
225 nouveau_connector_detect(struct drm_connector *connector, bool force)
226 #else
227 #error cannot determine drm_connector.detect API
228 #endif
229 #endif
231 struct drm_device *dev = connector->dev;
232 struct nouveau_connector *nv_connector = nouveau_connector(connector);
233 struct nouveau_encoder *nv_encoder = NULL;
234 struct nouveau_i2c_chan *i2c;
235 int type;
237 /* Cleanup the previous EDID block. */
238 if (nv_connector->edid) {
239 drm_mode_connector_update_edid_property(connector, NULL);
240 kfree(nv_connector->edid);
241 nv_connector->edid = NULL;
244 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
245 if (i2c) {
246 #ifdef __linux__
247 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
248 #else
249 nv_connector->edid = drm_get_edid(connector, i2c->adapter);
250 #endif
251 drm_mode_connector_update_edid_property(connector,
252 nv_connector->edid);
253 if (!nv_connector->edid) {
254 NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
255 drm_get_connector_name(connector));
256 goto detect_analog;
259 if (nv_encoder->dcb->type == OUTPUT_DP &&
260 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
261 NV_ERROR(dev, "Detected %s, but failed init\n",
262 drm_get_connector_name(connector));
263 return connector_status_disconnected;
266 /* Override encoder type for DVI-I based on whether EDID
267 * says the display is digital or analog, both use the
268 * same i2c channel so the value returned from ddc_detect
269 * isn't necessarily correct.
271 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
272 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
273 type = OUTPUT_TMDS;
274 else
275 type = OUTPUT_ANALOG;
277 nv_encoder = find_encoder_by_type(connector, type);
278 if (!nv_encoder) {
279 NV_ERROR(dev, "Detected %d encoder on %s, "
280 "but no object!\n", type,
281 drm_get_connector_name(connector));
282 return connector_status_disconnected;
286 nouveau_connector_set_encoder(connector, nv_encoder);
287 return connector_status_connected;
290 nv_encoder = nouveau_connector_of_detect(connector);
291 if (nv_encoder) {
292 nouveau_connector_set_encoder(connector, nv_encoder);
293 return connector_status_connected;
296 detect_analog:
297 nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
298 if (!nv_encoder && !nouveau_tv_disable)
299 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
300 if (nv_encoder) {
301 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
302 struct drm_encoder_helper_funcs *helper =
303 encoder->helper_private;
305 if (helper->detect(encoder, connector) ==
306 connector_status_connected) {
307 nouveau_connector_set_encoder(connector, nv_encoder);
308 return connector_status_connected;
313 return connector_status_disconnected;
316 static enum drm_connector_status
317 #ifdef PSCNV_KAPI_DRM_CONNECTOR_DETECT_1
318 nouveau_connector_detect_lvds(struct drm_connector *connector)
319 #else
320 #ifdef PSCNV_KAPI_DRM_CONNECTOR_DETECT_2
321 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
322 #else
323 #error cannot determine drm_connector.detect API
324 #endif
325 #endif
327 struct drm_device *dev = connector->dev;
328 struct drm_nouveau_private *dev_priv = dev->dev_private;
329 struct nouveau_connector *nv_connector = nouveau_connector(connector);
330 struct nouveau_encoder *nv_encoder = NULL;
331 enum drm_connector_status status = connector_status_disconnected;
333 /* Cleanup the previous EDID block. */
334 if (nv_connector->edid) {
335 drm_mode_connector_update_edid_property(connector, NULL);
336 kfree(nv_connector->edid);
337 nv_connector->edid = NULL;
340 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
341 if (!nv_encoder)
342 return connector_status_disconnected;
344 /* Try retrieving EDID via DDC */
345 if (!dev_priv->vbios.fp_no_ddc) {
346 #ifdef PSCNV_KAPI_DRM_CONNECTOR_DETECT_1
347 status = nouveau_connector_detect(connector);
348 #else
349 #ifdef PSCNV_KAPI_DRM_CONNECTOR_DETECT_2
350 status = nouveau_connector_detect(connector, force);
351 #else
352 #error cannot determine drm_connector.detect API
353 #endif
354 #endif
355 if (status == connector_status_connected)
356 goto out;
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..
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)
368 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
369 if (!nouveau_acpi_edid(dev, connector)) {
370 status = connector_status_connected;
371 goto out;
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.
379 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
380 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
381 status = connector_status_connected;
382 goto out;
385 /* Still nothing, some VBIOS images have a hardcoded EDID block
386 * stored for the panel stored in them.
388 if (!dev_priv->vbios.fp_no_ddc) {
389 struct edid *edid =
390 (struct edid *)nouveau_bios_embedded_edid(dev);
391 if (edid) {
392 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
393 *(nv_connector->edid) = *edid;
394 status = connector_status_connected;
398 out:
399 #if defined(CONFIG_ACPI_BUTTON) || \
400 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
401 if (status == connector_status_connected &&
402 !nouveau_ignorelid && !acpi_lid_open())
403 status = connector_status_unknown;
404 #endif
406 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
407 nouveau_connector_set_encoder(connector, nv_encoder);
408 return status;
411 static void
412 nouveau_connector_force(struct drm_connector *connector)
414 struct nouveau_connector *nv_connector = nouveau_connector(connector);
415 struct nouveau_encoder *nv_encoder;
416 int type;
418 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
419 if (connector->force == DRM_FORCE_ON_DIGITAL)
420 type = OUTPUT_TMDS;
421 else
422 type = OUTPUT_ANALOG;
423 } else
424 type = OUTPUT_ANY;
426 nv_encoder = find_encoder_by_type(connector, type);
427 if (!nv_encoder) {
428 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
429 drm_get_connector_name(connector));
430 connector->status = connector_status_disconnected;
431 return;
434 nouveau_connector_set_encoder(connector, nv_encoder);
437 static int
438 nouveau_connector_set_property(struct drm_connector *connector,
439 struct drm_property *property, uint64_t value)
441 struct nouveau_connector *nv_connector = nouveau_connector(connector);
442 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
443 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
444 struct drm_device *dev = connector->dev;
445 struct drm_nouveau_private *dev_priv = dev->dev_private;
446 struct nouveau_display_engine *disp = &dev_priv->engine.display;
447 struct nouveau_crtc *nv_crtc = NULL;
448 int ret;
449 if (connector->encoder && connector->encoder->crtc)
450 nv_crtc = nouveau_crtc(connector->encoder->crtc);
452 /* Scaling mode */
453 if (property == dev->mode_config.scaling_mode_property) {
454 bool modeset = false;
456 switch (value) {
457 case DRM_MODE_SCALE_NONE:
458 case DRM_MODE_SCALE_FULLSCREEN:
459 case DRM_MODE_SCALE_CENTER:
460 case DRM_MODE_SCALE_ASPECT:
461 break;
462 default:
463 return -EINVAL;
466 /* LVDS always needs gpu scaling */
467 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
468 value == DRM_MODE_SCALE_NONE)
469 return -EINVAL;
471 /* Changing between GPU and panel scaling requires a full
472 * modeset
474 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
475 (value == DRM_MODE_SCALE_NONE))
476 modeset = true;
477 nv_connector->scaling_mode = value;
479 if (!nv_crtc)
480 return 0;
482 if (modeset || !nv_crtc->set_scale) {
483 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
484 &nv_crtc->base.mode,
485 nv_crtc->base.x,
486 nv_crtc->base.y, NULL);
487 if (!ret)
488 return -EINVAL;
489 } else {
490 ret = nv_crtc->set_scale(nv_crtc, true);
491 if (ret)
492 return ret;
495 return 0;
498 /* Underscan */
499 if (property == disp->underscan_property) {
500 if (nv_connector->underscan != value) {
501 nv_connector->underscan = value;
502 if (!nv_crtc || !nv_crtc->set_scale)
503 return 0;
505 return nv_crtc->set_scale(nv_crtc, true);
508 return 0;
511 if (property == disp->underscan_hborder_property) {
512 if (nv_connector->underscan_hborder != value) {
513 nv_connector->underscan_hborder = value;
514 if (!nv_crtc || !nv_crtc->set_scale)
515 return 0;
517 return nv_crtc->set_scale(nv_crtc, true);
520 return 0;
523 if (property == disp->underscan_vborder_property) {
524 if (nv_connector->underscan_vborder != value) {
525 nv_connector->underscan_vborder = value;
526 if (!nv_crtc || !nv_crtc->set_scale)
527 return 0;
529 return nv_crtc->set_scale(nv_crtc, true);
532 return 0;
535 /* Dithering */
536 if (property == disp->dithering_mode) {
537 nv_connector->dithering_mode = value;
538 if (!nv_crtc || !nv_crtc->set_dither)
539 return 0;
541 return nv_crtc->set_dither(nv_crtc, true);
544 if (property == disp->dithering_depth) {
545 nv_connector->dithering_depth = value;
546 if (!nv_crtc || !nv_crtc->set_dither)
547 return 0;
549 return nv_crtc->set_dither(nv_crtc, true);
552 if (nv_crtc && nv_crtc->set_color_vibrance) {
553 /* Hue */
554 if (property == disp->vibrant_hue_property) {
555 nv_crtc->vibrant_hue = value - 90;
556 return nv_crtc->set_color_vibrance(nv_crtc, true);
558 /* Saturation */
559 if (property == disp->color_vibrance_property) {
560 nv_crtc->color_vibrance = value - 100;
561 return nv_crtc->set_color_vibrance(nv_crtc, true);
565 if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
566 return get_slave_funcs(encoder)->set_property(
567 encoder, connector, property, value);
569 return -EINVAL;
572 static struct drm_display_mode *
573 nouveau_connector_native_mode(struct drm_connector *connector)
575 struct drm_connector_helper_funcs *helper = connector->helper_private;
576 struct nouveau_connector *nv_connector = nouveau_connector(connector);
577 struct drm_device *dev = connector->dev;
578 struct drm_display_mode *mode, *largest = NULL;
579 int high_w = 0, high_h = 0, high_v = 0;
581 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
582 if (helper->mode_valid(connector, mode) != MODE_OK ||
583 (mode->flags & DRM_MODE_FLAG_INTERLACE))
584 continue;
586 /* Use preferred mode if there is one.. */
587 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
588 NV_DEBUG_KMS(dev, "native mode from preferred\n");
589 return drm_mode_duplicate(dev, mode);
592 /* Otherwise, take the resolution with the largest width, then
593 * height, then vertical refresh
595 if (mode->hdisplay < high_w)
596 continue;
598 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
599 continue;
601 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
602 mode->vrefresh < high_v)
603 continue;
605 high_w = mode->hdisplay;
606 high_h = mode->vdisplay;
607 high_v = mode->vrefresh;
608 largest = mode;
611 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
612 high_w, high_h, high_v);
613 return largest ? drm_mode_duplicate(dev, largest) : NULL;
616 struct moderec {
617 int hdisplay;
618 int vdisplay;
621 static struct moderec scaler_modes[] = {
622 { 1920, 1200 },
623 { 1920, 1080 },
624 { 1680, 1050 },
625 { 1600, 1200 },
626 { 1400, 1050 },
627 { 1280, 1024 },
628 { 1280, 960 },
629 { 1152, 864 },
630 { 1024, 768 },
631 { 800, 600 },
632 { 720, 400 },
633 { 640, 480 },
634 { 640, 400 },
635 { 640, 350 },
639 static int
640 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
642 struct nouveau_connector *nv_connector = nouveau_connector(connector);
643 struct drm_display_mode *native = nv_connector->native_mode, *m;
644 struct drm_device *dev = connector->dev;
645 struct moderec *mode = &scaler_modes[0];
646 int modes = 0;
648 if (!native)
649 return 0;
651 while (mode->hdisplay) {
652 if (mode->hdisplay <= native->hdisplay &&
653 mode->vdisplay <= native->vdisplay) {
654 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
655 drm_mode_vrefresh(native), false,
656 false, false);
657 if (!m)
658 continue;
660 m->type |= DRM_MODE_TYPE_DRIVER;
662 drm_mode_probed_add(connector, m);
663 modes++;
666 mode++;
669 return modes;
672 static int
673 nouveau_connector_get_modes(struct drm_connector *connector)
675 struct drm_device *dev = connector->dev;
676 struct drm_nouveau_private *dev_priv = dev->dev_private;
677 struct nouveau_connector *nv_connector = nouveau_connector(connector);
678 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
679 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
680 int ret = 0;
682 /* destroy the native mode, the attached monitor could have changed.
684 if (nv_connector->native_mode) {
685 drm_mode_destroy(dev, nv_connector->native_mode);
686 nv_connector->native_mode = NULL;
689 if (nv_connector->edid)
690 ret = drm_add_edid_modes(connector, nv_connector->edid);
691 else
692 if (nv_encoder->dcb->type == OUTPUT_LVDS &&
693 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
694 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
695 struct drm_display_mode mode;
697 nouveau_bios_fp_mode(dev, &mode);
698 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
701 /* Find the native mode if this is a digital panel, if we didn't
702 * find any modes through DDC previously add the native mode to
703 * the list of modes.
705 if (!nv_connector->native_mode)
706 nv_connector->native_mode =
707 nouveau_connector_native_mode(connector);
708 if (ret == 0 && nv_connector->native_mode) {
709 struct drm_display_mode *mode;
711 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
712 drm_mode_probed_add(connector, mode);
713 ret = 1;
716 if (nv_encoder->dcb->type == OUTPUT_TV)
717 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
719 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
720 nv_connector->dcb->type == DCB_CONNECTOR_eDP)
721 ret += nouveau_connector_scaler_modes_add(connector);
723 return ret;
726 static unsigned
727 get_tmds_link_bandwidth(struct drm_connector *connector)
729 struct nouveau_connector *nv_connector = nouveau_connector(connector);
730 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
731 struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
733 if (dcb->location != DCB_LOC_ON_CHIP ||
734 dev_priv->chipset >= 0x46)
735 return 165000;
736 else if (dev_priv->chipset >= 0x40)
737 return 155000;
738 else if (dev_priv->chipset >= 0x18)
739 return 135000;
740 else
741 return 112000;
744 static int
745 nouveau_connector_mode_valid(struct drm_connector *connector,
746 struct drm_display_mode *mode)
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 unsigned min_clock = 25000, max_clock = min_clock;
752 unsigned clock = mode->clock;
754 switch (nv_encoder->dcb->type) {
755 case OUTPUT_LVDS:
756 if (nv_connector->native_mode &&
757 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
758 mode->vdisplay > nv_connector->native_mode->vdisplay))
759 return MODE_PANEL;
761 min_clock = 0;
762 max_clock = 400000;
763 break;
764 case OUTPUT_TMDS:
765 max_clock = get_tmds_link_bandwidth(connector);
766 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
767 max_clock *= 2;
768 break;
769 case OUTPUT_ANALOG:
770 max_clock = nv_encoder->dcb->crtconf.maxfreq;
771 if (!max_clock)
772 max_clock = 350000;
773 break;
774 case OUTPUT_TV:
775 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
776 case OUTPUT_DP:
777 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
778 max_clock = nv_encoder->dp.link_nr * 270000;
779 else
780 max_clock = nv_encoder->dp.link_nr * 162000;
782 clock = clock * nouveau_connector_bpp(connector) / 8;
783 break;
784 default:
785 BUG_ON(1);
786 return MODE_BAD;
789 if (clock < min_clock)
790 return MODE_CLOCK_LOW;
792 if (clock > max_clock)
793 return MODE_CLOCK_HIGH;
795 return MODE_OK;
798 static struct drm_encoder *
799 nouveau_connector_best_encoder(struct drm_connector *connector)
801 struct nouveau_connector *nv_connector = nouveau_connector(connector);
803 if (nv_connector->detected_encoder)
804 return to_drm_encoder(nv_connector->detected_encoder);
806 return NULL;
809 void
810 nouveau_connector_set_polling(struct drm_connector *connector)
812 struct drm_device *dev = connector->dev;
813 struct drm_nouveau_private *dev_priv = dev->dev_private;
814 struct drm_crtc *crtc;
815 bool spare_crtc = false;
817 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
818 spare_crtc |= !crtc->enabled;
820 connector->polled = 0;
822 switch (connector->connector_type) {
823 case DRM_MODE_CONNECTOR_VGA:
824 case DRM_MODE_CONNECTOR_TV:
825 if (dev_priv->card_type >= NV_50 ||
826 (nv_gf4_disp_arch(dev) && spare_crtc))
827 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
828 break;
830 case DRM_MODE_CONNECTOR_DVII:
831 case DRM_MODE_CONNECTOR_DVID:
832 case DRM_MODE_CONNECTOR_HDMIA:
833 case DRM_MODE_CONNECTOR_DisplayPort:
834 case DRM_MODE_CONNECTOR_eDP:
835 if (dev_priv->card_type >= NV_50)
836 connector->polled = DRM_CONNECTOR_POLL_HPD;
837 else if (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
838 spare_crtc)
839 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
840 break;
842 default:
843 break;
847 static const struct drm_connector_helper_funcs
848 nouveau_connector_helper_funcs = {
849 .get_modes = nouveau_connector_get_modes,
850 .mode_valid = nouveau_connector_mode_valid,
851 .best_encoder = nouveau_connector_best_encoder,
854 static const struct drm_connector_funcs
855 nouveau_connector_funcs = {
856 .dpms = drm_helper_connector_dpms,
857 .save = NULL,
858 .restore = NULL,
859 .detect = nouveau_connector_detect,
860 .destroy = nouveau_connector_destroy,
861 .fill_modes = drm_helper_probe_single_connector_modes,
862 .set_property = nouveau_connector_set_property,
863 .force = nouveau_connector_force
866 static const struct drm_connector_funcs
867 nouveau_connector_funcs_lvds = {
868 .dpms = drm_helper_connector_dpms,
869 .save = NULL,
870 .restore = NULL,
871 .detect = nouveau_connector_detect_lvds,
872 .destroy = nouveau_connector_destroy,
873 .fill_modes = drm_helper_probe_single_connector_modes,
874 .set_property = nouveau_connector_set_property,
875 .force = nouveau_connector_force
878 struct drm_connector *
879 nouveau_connector_create(struct drm_device *dev, int index)
881 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
882 struct drm_nouveau_private *dev_priv = dev->dev_private;
883 struct nouveau_connector *nv_connector = NULL;
884 struct dcb_connector_table_entry *dcb = NULL;
885 struct drm_connector *connector;
886 struct nouveau_display_engine *disp = &dev_priv->engine.display;
887 int type, ret = 0;
889 NV_DEBUG_KMS(dev, "\n");
891 if (index >= dev_priv->vbios.dcb.connector.entries)
892 return ERR_PTR(-EINVAL);
894 dcb = &dev_priv->vbios.dcb.connector.entry[index];
895 if (dcb->drm)
896 return dcb->drm;
898 switch (dcb->type) {
899 case DCB_CONNECTOR_VGA:
900 type = DRM_MODE_CONNECTOR_VGA;
901 break;
902 case DCB_CONNECTOR_TV_0:
903 case DCB_CONNECTOR_TV_1:
904 case DCB_CONNECTOR_TV_3:
905 type = DRM_MODE_CONNECTOR_TV;
906 break;
907 case DCB_CONNECTOR_DVI_I:
908 type = DRM_MODE_CONNECTOR_DVII;
909 break;
910 case DCB_CONNECTOR_DVI_D:
911 type = DRM_MODE_CONNECTOR_DVID;
912 break;
913 case DCB_CONNECTOR_HDMI_0:
914 case DCB_CONNECTOR_HDMI_1:
915 type = DRM_MODE_CONNECTOR_HDMIA;
916 break;
917 case DCB_CONNECTOR_LVDS:
918 type = DRM_MODE_CONNECTOR_LVDS;
919 funcs = &nouveau_connector_funcs_lvds;
920 break;
921 case DCB_CONNECTOR_DP:
922 type = DRM_MODE_CONNECTOR_DisplayPort;
923 break;
924 case DCB_CONNECTOR_eDP:
925 type = DRM_MODE_CONNECTOR_eDP;
926 break;
927 default:
928 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
929 return ERR_PTR(-EINVAL);
932 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
933 if (!nv_connector)
934 return ERR_PTR(-ENOMEM);
935 nv_connector->dcb = dcb;
936 connector = &nv_connector->base;
938 /* defaults, will get overridden in detect() */
939 connector->interlace_allowed = false;
940 connector->doublescan_allowed = false;
942 drm_connector_init(dev, connector, funcs, type);
943 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
945 /* Check if we need dithering enabled */
946 if (dcb->type == DCB_CONNECTOR_LVDS) {
947 bool dummy, is_24bit = false;
949 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
950 if (ret) {
951 NV_ERROR(dev, "Error parsing LVDS table, disabling "
952 "LVDS\n");
953 goto fail;
957 /* Init DVI-I specific properties */
958 if (dcb->type == DCB_CONNECTOR_DVI_I)
959 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
961 /* Add overscan compensation options to digital outputs */
962 if (disp->underscan_property &&
963 (type == DRM_MODE_CONNECTOR_DVID ||
964 type == DRM_MODE_CONNECTOR_DVII ||
965 type == DRM_MODE_CONNECTOR_HDMIA ||
966 type == DRM_MODE_CONNECTOR_DisplayPort)) {
967 drm_connector_attach_property(connector,
968 disp->underscan_property,
969 UNDERSCAN_OFF);
970 drm_connector_attach_property(connector,
971 disp->underscan_hborder_property,
973 drm_connector_attach_property(connector,
974 disp->underscan_vborder_property,
978 /* Add hue and saturation options */
979 if (disp->vibrant_hue_property)
980 drm_connector_attach_property(connector,
981 disp->vibrant_hue_property,
982 90);
983 if (disp->color_vibrance_property)
984 drm_connector_attach_property(connector,
985 disp->color_vibrance_property,
986 150);
989 switch (dcb->type) {
990 case DCB_CONNECTOR_VGA:
991 if (dev_priv->card_type >= NV_50) {
992 drm_connector_attach_property(connector,
993 dev->mode_config.scaling_mode_property,
994 nv_connector->scaling_mode);
996 /* fall-through */
997 case DCB_CONNECTOR_TV_0:
998 case DCB_CONNECTOR_TV_1:
999 case DCB_CONNECTOR_TV_3:
1000 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1001 break;
1002 default:
1003 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1005 drm_connector_attach_property(connector,
1006 dev->mode_config.scaling_mode_property,
1007 nv_connector->scaling_mode);
1008 if (disp->dithering_mode) {
1009 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1010 drm_connector_attach_property(connector,
1011 disp->dithering_mode,
1012 nv_connector->dithering_mode);
1014 if (disp->dithering_depth) {
1015 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
1016 drm_connector_attach_property(connector,
1017 disp->dithering_depth,
1018 nv_connector->dithering_depth);
1020 break;
1023 nouveau_connector_set_polling(connector);
1025 #ifdef __linux__
1026 drm_sysfs_connector_add(connector);
1027 #endif
1028 dcb->drm = connector;
1029 return dcb->drm;
1031 fail:
1032 drm_connector_cleanup(connector);
1033 kfree(connector);
1034 return ERR_PTR(ret);