]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpu/drm/nouveau/nouveau_connector.c
drm/nouveau: add scaler-only modes for eDP too
[net-next-2.6.git] / drivers / gpu / drm / nouveau / nouveau_connector.c
CommitLineData
6ee73861
BS
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
a1470890
BS
27#include <acpi/button.h>
28
6ee73861
BS
29#include "drmP.h"
30#include "drm_edid.h"
31#include "drm_crtc_helper.h"
a1470890 32
6ee73861
BS
33#include "nouveau_reg.h"
34#include "nouveau_drv.h"
35#include "nouveau_encoder.h"
36#include "nouveau_crtc.h"
37#include "nouveau_connector.h"
38#include "nouveau_hw.h"
39
40static inline struct drm_encoder_slave_funcs *
41get_slave_funcs(struct nouveau_encoder *enc)
42{
43 return to_encoder_slave(to_drm_encoder(enc))->slave_funcs;
44}
45
46static struct nouveau_encoder *
47find_encoder_by_type(struct drm_connector *connector, int type)
48{
49 struct drm_device *dev = connector->dev;
50 struct nouveau_encoder *nv_encoder;
51 struct drm_mode_object *obj;
52 int i, id;
53
54 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
55 id = connector->encoder_ids[i];
56 if (!id)
57 break;
58
59 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
60 if (!obj)
61 continue;
62 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
63
64 if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
65 return nv_encoder;
66 }
67
68 return NULL;
69}
70
71struct nouveau_connector *
72nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
73{
74 struct drm_device *dev = to_drm_encoder(encoder)->dev;
75 struct drm_connector *drm_connector;
76
77 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
78 if (drm_connector->encoder == to_drm_encoder(encoder))
79 return nouveau_connector(drm_connector);
80 }
81
82 return NULL;
83}
84
85
86static void
87nouveau_connector_destroy(struct drm_connector *drm_connector)
88{
c8ebe275
XC
89 struct nouveau_connector *nv_connector =
90 nouveau_connector(drm_connector);
dd19e44b 91 struct drm_device *dev;
6ee73861 92
c8ebe275 93 if (!nv_connector)
6ee73861
BS
94 return;
95
dd19e44b
MS
96 dev = nv_connector->base.dev;
97 NV_DEBUG_KMS(dev, "\n");
98
c8ebe275 99 kfree(nv_connector->edid);
6ee73861
BS
100 drm_sysfs_connector_remove(drm_connector);
101 drm_connector_cleanup(drm_connector);
102 kfree(drm_connector);
103}
104
105static void
106nouveau_connector_ddc_prepare(struct drm_connector *connector, int *flags)
107{
108 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
109
110 if (dev_priv->card_type >= NV_50)
111 return;
112
113 *flags = 0;
114 if (NVLockVgaCrtcs(dev_priv->dev, false))
115 *flags |= 1;
116 if (nv_heads_tied(dev_priv->dev))
117 *flags |= 2;
118
119 if (*flags & 2)
120 NVSetOwner(dev_priv->dev, 0); /* necessary? */
121}
122
123static void
124nouveau_connector_ddc_finish(struct drm_connector *connector, int flags)
125{
126 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
127
128 if (dev_priv->card_type >= NV_50)
129 return;
130
131 if (flags & 2)
132 NVSetOwner(dev_priv->dev, 4);
133 if (flags & 1)
134 NVLockVgaCrtcs(dev_priv->dev, true);
135}
136
137static struct nouveau_i2c_chan *
138nouveau_connector_ddc_detect(struct drm_connector *connector,
139 struct nouveau_encoder **pnv_encoder)
140{
141 struct drm_device *dev = connector->dev;
142 uint8_t out_buf[] = { 0x0, 0x0}, buf[2];
143 int ret, flags, i;
144
145 struct i2c_msg msgs[] = {
146 {
147 .addr = 0x50,
148 .flags = 0,
149 .len = 1,
150 .buf = out_buf,
151 },
152 {
153 .addr = 0x50,
154 .flags = I2C_M_RD,
155 .len = 1,
156 .buf = buf,
157 }
158 };
159
160 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
161 struct nouveau_i2c_chan *i2c = NULL;
162 struct nouveau_encoder *nv_encoder;
163 struct drm_mode_object *obj;
164 int id;
165
166 id = connector->encoder_ids[i];
167 if (!id)
168 break;
169
170 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
171 if (!obj)
172 continue;
173 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
174
175 if (nv_encoder->dcb->i2c_index < 0xf)
176 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
177 if (!i2c)
178 continue;
179
180 nouveau_connector_ddc_prepare(connector, &flags);
181 ret = i2c_transfer(&i2c->adapter, msgs, 2);
182 nouveau_connector_ddc_finish(connector, flags);
183
184 if (ret == 2) {
185 *pnv_encoder = nv_encoder;
186 return i2c;
187 }
188 }
189
190 return NULL;
191}
192
193static void
194nouveau_connector_set_encoder(struct drm_connector *connector,
195 struct nouveau_encoder *nv_encoder)
196{
197 struct nouveau_connector *nv_connector = nouveau_connector(connector);
198 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
199 struct drm_device *dev = connector->dev;
200
201 if (nv_connector->detected_encoder == nv_encoder)
202 return;
203 nv_connector->detected_encoder = nv_encoder;
204
205 if (nv_encoder->dcb->type == OUTPUT_LVDS ||
206 nv_encoder->dcb->type == OUTPUT_TMDS) {
207 connector->doublescan_allowed = false;
208 connector->interlace_allowed = false;
209 } else {
210 connector->doublescan_allowed = true;
211 if (dev_priv->card_type == NV_20 ||
212 (dev_priv->card_type == NV_10 &&
213 (dev->pci_device & 0x0ff0) != 0x0100 &&
214 (dev->pci_device & 0x0ff0) != 0x0150))
215 /* HW is broken */
216 connector->interlace_allowed = false;
217 else
218 connector->interlace_allowed = true;
219 }
220
be079e97 221 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
222 drm_connector_property_set_value(connector,
223 dev->mode_config.dvi_i_subconnector_property,
224 nv_encoder->dcb->type == OUTPUT_TMDS ?
225 DRM_MODE_SUBCONNECTOR_DVID :
226 DRM_MODE_SUBCONNECTOR_DVIA);
227 }
228}
229
230static enum drm_connector_status
231nouveau_connector_detect(struct drm_connector *connector)
232{
233 struct drm_device *dev = connector->dev;
234 struct nouveau_connector *nv_connector = nouveau_connector(connector);
235 struct nouveau_encoder *nv_encoder = NULL;
236 struct nouveau_i2c_chan *i2c;
237 int type, flags;
238
b8780e2a
FJ
239 /* Cleanup the previous EDID block. */
240 if (nv_connector->edid) {
241 drm_mode_connector_update_edid_property(connector, NULL);
242 kfree(nv_connector->edid);
243 nv_connector->edid = NULL;
244 }
c8ebe275 245
6ee73861
BS
246 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
247 if (i2c) {
248 nouveau_connector_ddc_prepare(connector, &flags);
249 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
250 nouveau_connector_ddc_finish(connector, flags);
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));
0ed3165e 256 goto detect_analog;
6ee73861
BS
257 }
258
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;
264 }
265
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.
270 */
be079e97 271 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
272 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
273 type = OUTPUT_TMDS;
274 else
275 type = OUTPUT_ANALOG;
276
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;
283 }
284 }
285
286 nouveau_connector_set_encoder(connector, nv_encoder);
287 return connector_status_connected;
288 }
289
0ed3165e 290detect_analog:
6ee73861 291 nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
f4053509 292 if (!nv_encoder && !nouveau_tv_disable)
6ee73861
BS
293 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
294 if (nv_encoder) {
295 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
296 struct drm_encoder_helper_funcs *helper =
297 encoder->helper_private;
298
299 if (helper->detect(encoder, connector) ==
300 connector_status_connected) {
301 nouveau_connector_set_encoder(connector, nv_encoder);
302 return connector_status_connected;
303 }
304
305 }
306
307 return connector_status_disconnected;
308}
309
d17f395c
BS
310static enum drm_connector_status
311nouveau_connector_detect_lvds(struct drm_connector *connector)
312{
313 struct drm_device *dev = connector->dev;
314 struct drm_nouveau_private *dev_priv = dev->dev_private;
315 struct nouveau_connector *nv_connector = nouveau_connector(connector);
316 struct nouveau_encoder *nv_encoder = NULL;
317 enum drm_connector_status status = connector_status_disconnected;
318
319 /* Cleanup the previous EDID block. */
320 if (nv_connector->edid) {
321 drm_mode_connector_update_edid_property(connector, NULL);
322 kfree(nv_connector->edid);
323 nv_connector->edid = NULL;
324 }
325
326 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
327 if (!nv_encoder)
328 return connector_status_disconnected;
329
330 if (!dev_priv->vbios.fp_no_ddc) {
331 status = nouveau_connector_detect(connector);
332 if (status == connector_status_connected)
333 goto out;
334 }
335
336 /* If no EDID found above, and the VBIOS indicates a hardcoded
337 * modeline is avalilable for the panel, set it as the panel's
338 * native mode and exit.
339 */
340 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
341 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
342 status = connector_status_connected;
343 goto out;
344 }
345
346 /* Still nothing, some VBIOS images have a hardcoded EDID block
347 * stored for the panel stored in them.
348 */
349 if (!dev_priv->vbios.fp_no_ddc) {
350 struct edid *edid =
351 (struct edid *)nouveau_bios_embedded_edid(dev);
352 if (edid) {
353 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
354 *(nv_connector->edid) = *edid;
355 status = connector_status_connected;
356 }
357 }
358
359out:
360#if defined(CONFIG_ACPI_BUTTON) || \
361 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
362 if (status == connector_status_connected &&
363 !nouveau_ignorelid && !acpi_lid_open())
364 status = connector_status_unknown;
365#endif
366
367 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
3195c5f9 368 nouveau_connector_set_encoder(connector, nv_encoder);
d17f395c
BS
369 return status;
370}
371
6ee73861
BS
372static void
373nouveau_connector_force(struct drm_connector *connector)
374{
be079e97 375 struct nouveau_connector *nv_connector = nouveau_connector(connector);
6ee73861
BS
376 struct nouveau_encoder *nv_encoder;
377 int type;
378
be079e97 379 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
380 if (connector->force == DRM_FORCE_ON_DIGITAL)
381 type = OUTPUT_TMDS;
382 else
383 type = OUTPUT_ANALOG;
384 } else
385 type = OUTPUT_ANY;
386
387 nv_encoder = find_encoder_by_type(connector, type);
388 if (!nv_encoder) {
be079e97 389 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
6ee73861
BS
390 drm_get_connector_name(connector));
391 connector->status = connector_status_disconnected;
392 return;
393 }
394
395 nouveau_connector_set_encoder(connector, nv_encoder);
396}
397
398static int
399nouveau_connector_set_property(struct drm_connector *connector,
400 struct drm_property *property, uint64_t value)
401{
402 struct nouveau_connector *nv_connector = nouveau_connector(connector);
403 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
404 struct drm_device *dev = connector->dev;
405 int ret;
406
407 /* Scaling mode */
408 if (property == dev->mode_config.scaling_mode_property) {
409 struct nouveau_crtc *nv_crtc = NULL;
410 bool modeset = false;
411
412 switch (value) {
413 case DRM_MODE_SCALE_NONE:
414 case DRM_MODE_SCALE_FULLSCREEN:
415 case DRM_MODE_SCALE_CENTER:
416 case DRM_MODE_SCALE_ASPECT:
417 break;
418 default:
419 return -EINVAL;
420 }
421
422 /* LVDS always needs gpu scaling */
be079e97 423 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
6ee73861
BS
424 value == DRM_MODE_SCALE_NONE)
425 return -EINVAL;
426
427 /* Changing between GPU and panel scaling requires a full
428 * modeset
429 */
430 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
431 (value == DRM_MODE_SCALE_NONE))
432 modeset = true;
433 nv_connector->scaling_mode = value;
434
435 if (connector->encoder && connector->encoder->crtc)
436 nv_crtc = nouveau_crtc(connector->encoder->crtc);
437 if (!nv_crtc)
438 return 0;
439
440 if (modeset || !nv_crtc->set_scale) {
441 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
442 &nv_crtc->base.mode,
443 nv_crtc->base.x,
444 nv_crtc->base.y, NULL);
445 if (!ret)
446 return -EINVAL;
447 } else {
448 ret = nv_crtc->set_scale(nv_crtc, value, true);
449 if (ret)
450 return ret;
451 }
452
453 return 0;
454 }
455
456 /* Dithering */
457 if (property == dev->mode_config.dithering_mode_property) {
458 struct nouveau_crtc *nv_crtc = NULL;
459
460 if (value == DRM_MODE_DITHERING_ON)
461 nv_connector->use_dithering = true;
462 else
463 nv_connector->use_dithering = false;
464
465 if (connector->encoder && connector->encoder->crtc)
466 nv_crtc = nouveau_crtc(connector->encoder->crtc);
467
468 if (!nv_crtc || !nv_crtc->set_dither)
469 return 0;
470
471 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
472 true);
473 }
474
475 if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
476 return get_slave_funcs(nv_encoder)->
477 set_property(to_drm_encoder(nv_encoder), connector, property, value);
478
479 return -EINVAL;
480}
481
482static struct drm_display_mode *
26099a74 483nouveau_connector_native_mode(struct drm_connector *connector)
6ee73861 484{
26099a74
BS
485 struct drm_connector_helper_funcs *helper = connector->helper_private;
486 struct nouveau_connector *nv_connector = nouveau_connector(connector);
487 struct drm_device *dev = connector->dev;
6ee73861
BS
488 struct drm_display_mode *mode, *largest = NULL;
489 int high_w = 0, high_h = 0, high_v = 0;
490
26099a74
BS
491 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
492 if (helper->mode_valid(connector, mode) != MODE_OK)
493 continue;
494
495 /* Use preferred mode if there is one.. */
6ee73861 496 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
ef2bb506 497 NV_DEBUG_KMS(dev, "native mode from preferred\n");
6ee73861
BS
498 return drm_mode_duplicate(dev, mode);
499 }
6ee73861 500
26099a74
BS
501 /* Otherwise, take the resolution with the largest width, then
502 * height, then vertical refresh
503 */
6ee73861
BS
504 if (mode->hdisplay < high_w)
505 continue;
506
507 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
508 continue;
509
510 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
511 mode->vrefresh < high_v)
512 continue;
513
514 high_w = mode->hdisplay;
515 high_h = mode->vdisplay;
516 high_v = mode->vrefresh;
517 largest = mode;
518 }
519
ef2bb506 520 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
6ee73861
BS
521 high_w, high_h, high_v);
522 return largest ? drm_mode_duplicate(dev, largest) : NULL;
523}
524
525struct moderec {
526 int hdisplay;
527 int vdisplay;
528};
529
530static struct moderec scaler_modes[] = {
531 { 1920, 1200 },
532 { 1920, 1080 },
533 { 1680, 1050 },
534 { 1600, 1200 },
535 { 1400, 1050 },
536 { 1280, 1024 },
537 { 1280, 960 },
538 { 1152, 864 },
539 { 1024, 768 },
540 { 800, 600 },
541 { 720, 400 },
542 { 640, 480 },
543 { 640, 400 },
544 { 640, 350 },
545 {}
546};
547
548static int
549nouveau_connector_scaler_modes_add(struct drm_connector *connector)
550{
551 struct nouveau_connector *nv_connector = nouveau_connector(connector);
552 struct drm_display_mode *native = nv_connector->native_mode, *m;
553 struct drm_device *dev = connector->dev;
554 struct moderec *mode = &scaler_modes[0];
555 int modes = 0;
556
557 if (!native)
558 return 0;
559
560 while (mode->hdisplay) {
561 if (mode->hdisplay <= native->hdisplay &&
562 mode->vdisplay <= native->vdisplay) {
563 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
564 drm_mode_vrefresh(native), false,
565 false, false);
566 if (!m)
567 continue;
568
569 m->type |= DRM_MODE_TYPE_DRIVER;
570
571 drm_mode_probed_add(connector, m);
572 modes++;
573 }
574
575 mode++;
576 }
577
578 return modes;
579}
580
581static int
582nouveau_connector_get_modes(struct drm_connector *connector)
583{
584 struct drm_device *dev = connector->dev;
d17f395c 585 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861
BS
586 struct nouveau_connector *nv_connector = nouveau_connector(connector);
587 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
588 int ret = 0;
589
d17f395c 590 /* destroy the native mode, the attached monitor could have changed.
6ee73861 591 */
d17f395c 592 if (nv_connector->native_mode) {
6ee73861
BS
593 drm_mode_destroy(dev, nv_connector->native_mode);
594 nv_connector->native_mode = NULL;
595 }
596
597 if (nv_connector->edid)
598 ret = drm_add_edid_modes(connector, nv_connector->edid);
d17f395c
BS
599 else
600 if (nv_encoder->dcb->type == OUTPUT_LVDS &&
601 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
6e86e041
FJ
602 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
603 nv_connector->native_mode = drm_mode_create(dev);
604 nouveau_bios_fp_mode(dev, nv_connector->native_mode);
d17f395c 605 }
6ee73861
BS
606
607 /* Find the native mode if this is a digital panel, if we didn't
608 * find any modes through DDC previously add the native mode to
609 * the list of modes.
610 */
611 if (!nv_connector->native_mode)
612 nv_connector->native_mode =
26099a74 613 nouveau_connector_native_mode(connector);
6ee73861
BS
614 if (ret == 0 && nv_connector->native_mode) {
615 struct drm_display_mode *mode;
616
617 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
618 drm_mode_probed_add(connector, mode);
619 ret = 1;
620 }
621
622 if (nv_encoder->dcb->type == OUTPUT_TV)
623 ret = get_slave_funcs(nv_encoder)->
624 get_modes(to_drm_encoder(nv_encoder), connector);
625
646bef2d
BS
626 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS ||
627 nv_connector->dcb->type == DCB_CONNECTOR_eDP)
6ee73861
BS
628 ret += nouveau_connector_scaler_modes_add(connector);
629
630 return ret;
631}
632
633static int
634nouveau_connector_mode_valid(struct drm_connector *connector,
635 struct drm_display_mode *mode)
636{
637 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
638 struct nouveau_connector *nv_connector = nouveau_connector(connector);
639 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
640 unsigned min_clock = 25000, max_clock = min_clock;
641 unsigned clock = mode->clock;
642
643 switch (nv_encoder->dcb->type) {
644 case OUTPUT_LVDS:
26099a74
BS
645 if (nv_connector->native_mode &&
646 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
647 mode->vdisplay > nv_connector->native_mode->vdisplay))
6ee73861
BS
648 return MODE_PANEL;
649
650 min_clock = 0;
651 max_clock = 400000;
652 break;
653 case OUTPUT_TMDS:
654 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
2c580775 655 !nv_encoder->dcb->duallink_possible)
6ee73861
BS
656 max_clock = 165000;
657 else
658 max_clock = 330000;
659 break;
660 case OUTPUT_ANALOG:
661 max_clock = nv_encoder->dcb->crtconf.maxfreq;
662 if (!max_clock)
663 max_clock = 350000;
664 break;
665 case OUTPUT_TV:
666 return get_slave_funcs(nv_encoder)->
667 mode_valid(to_drm_encoder(nv_encoder), mode);
668 case OUTPUT_DP:
669 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
670 max_clock = nv_encoder->dp.link_nr * 270000;
671 else
672 max_clock = nv_encoder->dp.link_nr * 162000;
673
674 clock *= 3;
675 break;
e7cc51c5
BS
676 default:
677 BUG_ON(1);
678 return MODE_BAD;
6ee73861
BS
679 }
680
681 if (clock < min_clock)
682 return MODE_CLOCK_LOW;
683
684 if (clock > max_clock)
685 return MODE_CLOCK_HIGH;
686
687 return MODE_OK;
688}
689
690static struct drm_encoder *
691nouveau_connector_best_encoder(struct drm_connector *connector)
692{
693 struct nouveau_connector *nv_connector = nouveau_connector(connector);
694
695 if (nv_connector->detected_encoder)
696 return to_drm_encoder(nv_connector->detected_encoder);
697
698 return NULL;
699}
700
2ed06b7d
FJ
701void
702nouveau_connector_set_polling(struct drm_connector *connector)
703{
704 struct drm_device *dev = connector->dev;
705 struct drm_nouveau_private *dev_priv = dev->dev_private;
706 struct drm_crtc *crtc;
707 bool spare_crtc = false;
708
709 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
710 spare_crtc |= !crtc->enabled;
711
712 connector->polled = 0;
713
714 switch (connector->connector_type) {
715 case DRM_MODE_CONNECTOR_VGA:
716 case DRM_MODE_CONNECTOR_TV:
717 if (dev_priv->card_type >= NV_50 ||
718 (nv_gf4_disp_arch(dev) && spare_crtc))
719 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
720 break;
721
722 case DRM_MODE_CONNECTOR_DVII:
723 case DRM_MODE_CONNECTOR_DVID:
724 case DRM_MODE_CONNECTOR_HDMIA:
725 case DRM_MODE_CONNECTOR_DisplayPort:
726 case DRM_MODE_CONNECTOR_eDP:
727 if (dev_priv->card_type >= NV_50)
728 connector->polled = DRM_CONNECTOR_POLL_HPD;
729 else if (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
730 spare_crtc)
731 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
732 break;
733
734 default:
735 break;
736 }
737}
738
6ee73861
BS
739static const struct drm_connector_helper_funcs
740nouveau_connector_helper_funcs = {
741 .get_modes = nouveau_connector_get_modes,
742 .mode_valid = nouveau_connector_mode_valid,
743 .best_encoder = nouveau_connector_best_encoder,
744};
745
746static const struct drm_connector_funcs
747nouveau_connector_funcs = {
748 .dpms = drm_helper_connector_dpms,
749 .save = NULL,
750 .restore = NULL,
751 .detect = nouveau_connector_detect,
752 .destroy = nouveau_connector_destroy,
753 .fill_modes = drm_helper_probe_single_connector_modes,
754 .set_property = nouveau_connector_set_property,
755 .force = nouveau_connector_force
756};
757
d17f395c
BS
758static const struct drm_connector_funcs
759nouveau_connector_funcs_lvds = {
760 .dpms = drm_helper_connector_dpms,
761 .save = NULL,
762 .restore = NULL,
763 .detect = nouveau_connector_detect_lvds,
764 .destroy = nouveau_connector_destroy,
765 .fill_modes = drm_helper_probe_single_connector_modes,
766 .set_property = nouveau_connector_set_property,
767 .force = nouveau_connector_force
768};
6ee73861 769
8f1a6086
BS
770struct drm_connector *
771nouveau_connector_create(struct drm_device *dev, int index)
6ee73861 772{
d17f395c 773 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
6ee73861
BS
774 struct drm_nouveau_private *dev_priv = dev->dev_private;
775 struct nouveau_connector *nv_connector = NULL;
8f1a6086 776 struct dcb_connector_table_entry *dcb = NULL;
6ee73861 777 struct drm_connector *connector;
2fa67f12 778 int type, ret = 0;
6ee73861 779
ef2bb506 780 NV_DEBUG_KMS(dev, "\n");
6ee73861 781
8f1a6086
BS
782 if (index >= dev_priv->vbios.dcb.connector.entries)
783 return ERR_PTR(-EINVAL);
784
785 dcb = &dev_priv->vbios.dcb.connector.entry[index];
786 if (dcb->drm)
787 return dcb->drm;
788
7f612d87 789 switch (dcb->type) {
7f612d87 790 case DCB_CONNECTOR_VGA:
7f612d87 791 type = DRM_MODE_CONNECTOR_VGA;
6ee73861 792 break;
7f612d87
BS
793 case DCB_CONNECTOR_TV_0:
794 case DCB_CONNECTOR_TV_1:
795 case DCB_CONNECTOR_TV_3:
7f612d87 796 type = DRM_MODE_CONNECTOR_TV;
6ee73861 797 break;
7f612d87 798 case DCB_CONNECTOR_DVI_I:
7f612d87 799 type = DRM_MODE_CONNECTOR_DVII;
6ee73861 800 break;
7f612d87 801 case DCB_CONNECTOR_DVI_D:
7f612d87 802 type = DRM_MODE_CONNECTOR_DVID;
6ee73861 803 break;
be079e97
BS
804 case DCB_CONNECTOR_HDMI_0:
805 case DCB_CONNECTOR_HDMI_1:
be079e97
BS
806 type = DRM_MODE_CONNECTOR_HDMIA;
807 break;
7f612d87 808 case DCB_CONNECTOR_LVDS:
7f612d87 809 type = DRM_MODE_CONNECTOR_LVDS;
d17f395c 810 funcs = &nouveau_connector_funcs_lvds;
6ee73861 811 break;
7f612d87 812 case DCB_CONNECTOR_DP:
7f612d87 813 type = DRM_MODE_CONNECTOR_DisplayPort;
6ee73861 814 break;
be079e97 815 case DCB_CONNECTOR_eDP:
be079e97
BS
816 type = DRM_MODE_CONNECTOR_eDP;
817 break;
6ee73861 818 default:
7f612d87 819 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
8f1a6086 820 return ERR_PTR(-EINVAL);
6ee73861
BS
821 }
822
7f612d87
BS
823 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
824 if (!nv_connector)
8f1a6086 825 return ERR_PTR(-ENOMEM);
7f612d87
BS
826 nv_connector->dcb = dcb;
827 connector = &nv_connector->base;
828
6ee73861
BS
829 /* defaults, will get overridden in detect() */
830 connector->interlace_allowed = false;
831 connector->doublescan_allowed = false;
832
d17f395c 833 drm_connector_init(dev, connector, funcs, type);
6ee73861
BS
834 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
835
2fa67f12
FJ
836 /* Check if we need dithering enabled */
837 if (dcb->type == DCB_CONNECTOR_LVDS) {
838 bool dummy, is_24bit = false;
839
840 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
841 if (ret) {
842 NV_ERROR(dev, "Error parsing LVDS table, disabling "
843 "LVDS\n");
844 goto fail;
845 }
846
847 nv_connector->use_dithering = !is_24bit;
7f612d87
BS
848 }
849
6ee73861 850 /* Init DVI-I specific properties */
be079e97 851 if (dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
852 drm_mode_create_dvi_i_properties(dev);
853 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
854 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
855 }
856
be079e97
BS
857 switch (dcb->type) {
858 case DCB_CONNECTOR_VGA:
859 if (dev_priv->card_type >= NV_50) {
6ee73861
BS
860 drm_connector_attach_property(connector,
861 dev->mode_config.scaling_mode_property,
862 nv_connector->scaling_mode);
863 }
be079e97
BS
864 /* fall-through */
865 case DCB_CONNECTOR_TV_0:
866 case DCB_CONNECTOR_TV_1:
867 case DCB_CONNECTOR_TV_3:
868 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
869 break;
870 default:
871 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
872
873 drm_connector_attach_property(connector,
874 dev->mode_config.scaling_mode_property,
875 nv_connector->scaling_mode);
876 drm_connector_attach_property(connector,
877 dev->mode_config.dithering_mode_property,
878 nv_connector->use_dithering ?
879 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
880 break;
6ee73861
BS
881 }
882
2ed06b7d
FJ
883 nouveau_connector_set_polling(connector);
884
6ee73861 885 drm_sysfs_connector_add(connector);
8f1a6086
BS
886 dcb->drm = connector;
887 return dcb->drm;
2fa67f12
FJ
888
889fail:
890 drm_connector_cleanup(connector);
891 kfree(connector);
8f1a6086 892 return ERR_PTR(ret);
2fa67f12 893
6ee73861 894}