drm/nouveau: set encoder for lvds
[linux-2.6-block.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;
d17f395c 588 struct drm_display_mode mode;
6ee73861
BS
589 int ret = 0;
590
d17f395c 591 /* destroy the native mode, the attached monitor could have changed.
6ee73861 592 */
d17f395c 593 if (nv_connector->native_mode) {
6ee73861
BS
594 drm_mode_destroy(dev, nv_connector->native_mode);
595 nv_connector->native_mode = NULL;
596 }
597
598 if (nv_connector->edid)
599 ret = drm_add_edid_modes(connector, nv_connector->edid);
d17f395c
BS
600 else
601 if (nv_encoder->dcb->type == OUTPUT_LVDS &&
602 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
603 dev_priv->vbios.fp_no_ddc) &&
604 nouveau_bios_fp_mode(dev, &mode)) {
605 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
606 }
6ee73861
BS
607
608 /* Find the native mode if this is a digital panel, if we didn't
609 * find any modes through DDC previously add the native mode to
610 * the list of modes.
611 */
612 if (!nv_connector->native_mode)
613 nv_connector->native_mode =
26099a74 614 nouveau_connector_native_mode(connector);
6ee73861
BS
615 if (ret == 0 && nv_connector->native_mode) {
616 struct drm_display_mode *mode;
617
618 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
619 drm_mode_probed_add(connector, mode);
620 ret = 1;
621 }
622
623 if (nv_encoder->dcb->type == OUTPUT_TV)
624 ret = get_slave_funcs(nv_encoder)->
625 get_modes(to_drm_encoder(nv_encoder), connector);
626
be079e97 627 if (nv_encoder->dcb->type == OUTPUT_LVDS)
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
701static const struct drm_connector_helper_funcs
702nouveau_connector_helper_funcs = {
703 .get_modes = nouveau_connector_get_modes,
704 .mode_valid = nouveau_connector_mode_valid,
705 .best_encoder = nouveau_connector_best_encoder,
706};
707
708static const struct drm_connector_funcs
709nouveau_connector_funcs = {
710 .dpms = drm_helper_connector_dpms,
711 .save = NULL,
712 .restore = NULL,
713 .detect = nouveau_connector_detect,
714 .destroy = nouveau_connector_destroy,
715 .fill_modes = drm_helper_probe_single_connector_modes,
716 .set_property = nouveau_connector_set_property,
717 .force = nouveau_connector_force
718};
719
d17f395c
BS
720static const struct drm_connector_funcs
721nouveau_connector_funcs_lvds = {
722 .dpms = drm_helper_connector_dpms,
723 .save = NULL,
724 .restore = NULL,
725 .detect = nouveau_connector_detect_lvds,
726 .destroy = nouveau_connector_destroy,
727 .fill_modes = drm_helper_probe_single_connector_modes,
728 .set_property = nouveau_connector_set_property,
729 .force = nouveau_connector_force
730};
6ee73861
BS
731
732int
7f612d87
BS
733nouveau_connector_create(struct drm_device *dev,
734 struct dcb_connector_table_entry *dcb)
6ee73861 735{
d17f395c 736 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
6ee73861
BS
737 struct drm_nouveau_private *dev_priv = dev->dev_private;
738 struct nouveau_connector *nv_connector = NULL;
739 struct drm_connector *connector;
740 struct drm_encoder *encoder;
2fa67f12 741 int type, ret = 0;
6ee73861 742
ef2bb506 743 NV_DEBUG_KMS(dev, "\n");
6ee73861 744
7f612d87
BS
745 switch (dcb->type) {
746 case DCB_CONNECTOR_NONE:
747 return 0;
748 case DCB_CONNECTOR_VGA:
6ee73861 749 NV_INFO(dev, "Detected a VGA connector\n");
7f612d87 750 type = DRM_MODE_CONNECTOR_VGA;
6ee73861 751 break;
7f612d87
BS
752 case DCB_CONNECTOR_TV_0:
753 case DCB_CONNECTOR_TV_1:
754 case DCB_CONNECTOR_TV_3:
755 NV_INFO(dev, "Detected a TV connector\n");
756 type = DRM_MODE_CONNECTOR_TV;
6ee73861 757 break;
7f612d87 758 case DCB_CONNECTOR_DVI_I:
6ee73861 759 NV_INFO(dev, "Detected a DVI-I connector\n");
7f612d87 760 type = DRM_MODE_CONNECTOR_DVII;
6ee73861 761 break;
7f612d87 762 case DCB_CONNECTOR_DVI_D:
7f612d87
BS
763 NV_INFO(dev, "Detected a DVI-D connector\n");
764 type = DRM_MODE_CONNECTOR_DVID;
6ee73861 765 break;
be079e97
BS
766 case DCB_CONNECTOR_HDMI_0:
767 case DCB_CONNECTOR_HDMI_1:
768 NV_INFO(dev, "Detected a HDMI connector\n");
769 type = DRM_MODE_CONNECTOR_HDMIA;
770 break;
7f612d87
BS
771 case DCB_CONNECTOR_LVDS:
772 NV_INFO(dev, "Detected a LVDS connector\n");
773 type = DRM_MODE_CONNECTOR_LVDS;
d17f395c 774 funcs = &nouveau_connector_funcs_lvds;
6ee73861 775 break;
7f612d87 776 case DCB_CONNECTOR_DP:
6ee73861 777 NV_INFO(dev, "Detected a DisplayPort connector\n");
7f612d87 778 type = DRM_MODE_CONNECTOR_DisplayPort;
6ee73861 779 break;
be079e97
BS
780 case DCB_CONNECTOR_eDP:
781 NV_INFO(dev, "Detected an eDP connector\n");
782 type = DRM_MODE_CONNECTOR_eDP;
783 break;
6ee73861 784 default:
7f612d87
BS
785 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
786 return -EINVAL;
6ee73861
BS
787 }
788
7f612d87
BS
789 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
790 if (!nv_connector)
791 return -ENOMEM;
792 nv_connector->dcb = dcb;
793 connector = &nv_connector->base;
794
6ee73861
BS
795 /* defaults, will get overridden in detect() */
796 connector->interlace_allowed = false;
797 connector->doublescan_allowed = false;
798
d17f395c 799 drm_connector_init(dev, connector, funcs, type);
6ee73861
BS
800 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
801
7f612d87
BS
802 /* attach encoders */
803 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
804 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
805
806 if (nv_encoder->dcb->connector != dcb->index)
807 continue;
808
809 if (get_slave_funcs(nv_encoder))
810 get_slave_funcs(nv_encoder)->create_resources(encoder, connector);
811
812 drm_mode_connector_attach_encoder(connector, encoder);
813 }
814
815 if (!connector->encoder_ids[0]) {
816 NV_WARN(dev, " no encoders, ignoring\n");
2fa67f12
FJ
817 goto fail;
818 }
819
820 /* Check if we need dithering enabled */
821 if (dcb->type == DCB_CONNECTOR_LVDS) {
822 bool dummy, is_24bit = false;
823
824 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
825 if (ret) {
826 NV_ERROR(dev, "Error parsing LVDS table, disabling "
827 "LVDS\n");
828 goto fail;
829 }
830
831 nv_connector->use_dithering = !is_24bit;
7f612d87
BS
832 }
833
6ee73861 834 /* Init DVI-I specific properties */
be079e97 835 if (dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
836 drm_mode_create_dvi_i_properties(dev);
837 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
838 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
839 }
840
be079e97
BS
841 switch (dcb->type) {
842 case DCB_CONNECTOR_VGA:
eb1f8e4f 843 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
be079e97 844 if (dev_priv->card_type >= NV_50) {
6ee73861
BS
845 drm_connector_attach_property(connector,
846 dev->mode_config.scaling_mode_property,
847 nv_connector->scaling_mode);
848 }
be079e97
BS
849 /* fall-through */
850 case DCB_CONNECTOR_TV_0:
851 case DCB_CONNECTOR_TV_1:
852 case DCB_CONNECTOR_TV_3:
853 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
854 break;
eb1f8e4f
DA
855 case DCB_CONNECTOR_DP:
856 case DCB_CONNECTOR_eDP:
857 case DCB_CONNECTOR_HDMI_0:
858 case DCB_CONNECTOR_HDMI_1:
859 case DCB_CONNECTOR_DVI_I:
860 case DCB_CONNECTOR_DVI_D:
861 if (dev_priv->card_type >= NV_50)
862 connector->polled = DRM_CONNECTOR_POLL_HPD;
863 else
864 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
865 /* fall-through */
be079e97
BS
866 default:
867 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
868
869 drm_connector_attach_property(connector,
870 dev->mode_config.scaling_mode_property,
871 nv_connector->scaling_mode);
872 drm_connector_attach_property(connector,
873 dev->mode_config.dithering_mode_property,
874 nv_connector->use_dithering ?
875 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
876 break;
6ee73861
BS
877 }
878
6ee73861 879 drm_sysfs_connector_add(connector);
6ee73861 880 return 0;
2fa67f12
FJ
881
882fail:
883 drm_connector_cleanup(connector);
884 kfree(connector);
885 return ret;
886
6ee73861 887}