drm/nouveau: Ignore broken legacy I2C entries.
[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);
368 return status;
369}
370
6ee73861
BS
371static void
372nouveau_connector_force(struct drm_connector *connector)
373{
be079e97 374 struct nouveau_connector *nv_connector = nouveau_connector(connector);
6ee73861
BS
375 struct nouveau_encoder *nv_encoder;
376 int type;
377
be079e97 378 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
379 if (connector->force == DRM_FORCE_ON_DIGITAL)
380 type = OUTPUT_TMDS;
381 else
382 type = OUTPUT_ANALOG;
383 } else
384 type = OUTPUT_ANY;
385
386 nv_encoder = find_encoder_by_type(connector, type);
387 if (!nv_encoder) {
be079e97 388 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
6ee73861
BS
389 drm_get_connector_name(connector));
390 connector->status = connector_status_disconnected;
391 return;
392 }
393
394 nouveau_connector_set_encoder(connector, nv_encoder);
395}
396
397static int
398nouveau_connector_set_property(struct drm_connector *connector,
399 struct drm_property *property, uint64_t value)
400{
401 struct nouveau_connector *nv_connector = nouveau_connector(connector);
402 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
403 struct drm_device *dev = connector->dev;
404 int ret;
405
406 /* Scaling mode */
407 if (property == dev->mode_config.scaling_mode_property) {
408 struct nouveau_crtc *nv_crtc = NULL;
409 bool modeset = false;
410
411 switch (value) {
412 case DRM_MODE_SCALE_NONE:
413 case DRM_MODE_SCALE_FULLSCREEN:
414 case DRM_MODE_SCALE_CENTER:
415 case DRM_MODE_SCALE_ASPECT:
416 break;
417 default:
418 return -EINVAL;
419 }
420
421 /* LVDS always needs gpu scaling */
be079e97 422 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
6ee73861
BS
423 value == DRM_MODE_SCALE_NONE)
424 return -EINVAL;
425
426 /* Changing between GPU and panel scaling requires a full
427 * modeset
428 */
429 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
430 (value == DRM_MODE_SCALE_NONE))
431 modeset = true;
432 nv_connector->scaling_mode = value;
433
434 if (connector->encoder && connector->encoder->crtc)
435 nv_crtc = nouveau_crtc(connector->encoder->crtc);
436 if (!nv_crtc)
437 return 0;
438
439 if (modeset || !nv_crtc->set_scale) {
440 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
441 &nv_crtc->base.mode,
442 nv_crtc->base.x,
443 nv_crtc->base.y, NULL);
444 if (!ret)
445 return -EINVAL;
446 } else {
447 ret = nv_crtc->set_scale(nv_crtc, value, true);
448 if (ret)
449 return ret;
450 }
451
452 return 0;
453 }
454
455 /* Dithering */
456 if (property == dev->mode_config.dithering_mode_property) {
457 struct nouveau_crtc *nv_crtc = NULL;
458
459 if (value == DRM_MODE_DITHERING_ON)
460 nv_connector->use_dithering = true;
461 else
462 nv_connector->use_dithering = false;
463
464 if (connector->encoder && connector->encoder->crtc)
465 nv_crtc = nouveau_crtc(connector->encoder->crtc);
466
467 if (!nv_crtc || !nv_crtc->set_dither)
468 return 0;
469
470 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
471 true);
472 }
473
474 if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
475 return get_slave_funcs(nv_encoder)->
476 set_property(to_drm_encoder(nv_encoder), connector, property, value);
477
478 return -EINVAL;
479}
480
481static struct drm_display_mode *
26099a74 482nouveau_connector_native_mode(struct drm_connector *connector)
6ee73861 483{
26099a74
BS
484 struct drm_connector_helper_funcs *helper = connector->helper_private;
485 struct nouveau_connector *nv_connector = nouveau_connector(connector);
486 struct drm_device *dev = connector->dev;
6ee73861
BS
487 struct drm_display_mode *mode, *largest = NULL;
488 int high_w = 0, high_h = 0, high_v = 0;
489
26099a74
BS
490 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
491 if (helper->mode_valid(connector, mode) != MODE_OK)
492 continue;
493
494 /* Use preferred mode if there is one.. */
6ee73861 495 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
ef2bb506 496 NV_DEBUG_KMS(dev, "native mode from preferred\n");
6ee73861
BS
497 return drm_mode_duplicate(dev, mode);
498 }
6ee73861 499
26099a74
BS
500 /* Otherwise, take the resolution with the largest width, then
501 * height, then vertical refresh
502 */
6ee73861
BS
503 if (mode->hdisplay < high_w)
504 continue;
505
506 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
507 continue;
508
509 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
510 mode->vrefresh < high_v)
511 continue;
512
513 high_w = mode->hdisplay;
514 high_h = mode->vdisplay;
515 high_v = mode->vrefresh;
516 largest = mode;
517 }
518
ef2bb506 519 NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
6ee73861
BS
520 high_w, high_h, high_v);
521 return largest ? drm_mode_duplicate(dev, largest) : NULL;
522}
523
524struct moderec {
525 int hdisplay;
526 int vdisplay;
527};
528
529static struct moderec scaler_modes[] = {
530 { 1920, 1200 },
531 { 1920, 1080 },
532 { 1680, 1050 },
533 { 1600, 1200 },
534 { 1400, 1050 },
535 { 1280, 1024 },
536 { 1280, 960 },
537 { 1152, 864 },
538 { 1024, 768 },
539 { 800, 600 },
540 { 720, 400 },
541 { 640, 480 },
542 { 640, 400 },
543 { 640, 350 },
544 {}
545};
546
547static int
548nouveau_connector_scaler_modes_add(struct drm_connector *connector)
549{
550 struct nouveau_connector *nv_connector = nouveau_connector(connector);
551 struct drm_display_mode *native = nv_connector->native_mode, *m;
552 struct drm_device *dev = connector->dev;
553 struct moderec *mode = &scaler_modes[0];
554 int modes = 0;
555
556 if (!native)
557 return 0;
558
559 while (mode->hdisplay) {
560 if (mode->hdisplay <= native->hdisplay &&
561 mode->vdisplay <= native->vdisplay) {
562 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
563 drm_mode_vrefresh(native), false,
564 false, false);
565 if (!m)
566 continue;
567
568 m->type |= DRM_MODE_TYPE_DRIVER;
569
570 drm_mode_probed_add(connector, m);
571 modes++;
572 }
573
574 mode++;
575 }
576
577 return modes;
578}
579
580static int
581nouveau_connector_get_modes(struct drm_connector *connector)
582{
583 struct drm_device *dev = connector->dev;
d17f395c 584 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861
BS
585 struct nouveau_connector *nv_connector = nouveau_connector(connector);
586 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
d17f395c 587 struct drm_display_mode mode;
6ee73861
BS
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 ||
602 dev_priv->vbios.fp_no_ddc) &&
603 nouveau_bios_fp_mode(dev, &mode)) {
604 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
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
be079e97 626 if (nv_encoder->dcb->type == OUTPUT_LVDS)
6ee73861
BS
627 ret += nouveau_connector_scaler_modes_add(connector);
628
629 return ret;
630}
631
632static int
633nouveau_connector_mode_valid(struct drm_connector *connector,
634 struct drm_display_mode *mode)
635{
636 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
637 struct nouveau_connector *nv_connector = nouveau_connector(connector);
638 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
639 unsigned min_clock = 25000, max_clock = min_clock;
640 unsigned clock = mode->clock;
641
642 switch (nv_encoder->dcb->type) {
643 case OUTPUT_LVDS:
26099a74
BS
644 if (nv_connector->native_mode &&
645 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
646 mode->vdisplay > nv_connector->native_mode->vdisplay))
6ee73861
BS
647 return MODE_PANEL;
648
649 min_clock = 0;
650 max_clock = 400000;
651 break;
652 case OUTPUT_TMDS:
653 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
2c580775 654 !nv_encoder->dcb->duallink_possible)
6ee73861
BS
655 max_clock = 165000;
656 else
657 max_clock = 330000;
658 break;
659 case OUTPUT_ANALOG:
660 max_clock = nv_encoder->dcb->crtconf.maxfreq;
661 if (!max_clock)
662 max_clock = 350000;
663 break;
664 case OUTPUT_TV:
665 return get_slave_funcs(nv_encoder)->
666 mode_valid(to_drm_encoder(nv_encoder), mode);
667 case OUTPUT_DP:
668 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
669 max_clock = nv_encoder->dp.link_nr * 270000;
670 else
671 max_clock = nv_encoder->dp.link_nr * 162000;
672
673 clock *= 3;
674 break;
e7cc51c5
BS
675 default:
676 BUG_ON(1);
677 return MODE_BAD;
6ee73861
BS
678 }
679
680 if (clock < min_clock)
681 return MODE_CLOCK_LOW;
682
683 if (clock > max_clock)
684 return MODE_CLOCK_HIGH;
685
686 return MODE_OK;
687}
688
689static struct drm_encoder *
690nouveau_connector_best_encoder(struct drm_connector *connector)
691{
692 struct nouveau_connector *nv_connector = nouveau_connector(connector);
693
694 if (nv_connector->detected_encoder)
695 return to_drm_encoder(nv_connector->detected_encoder);
696
697 return NULL;
698}
699
700static const struct drm_connector_helper_funcs
701nouveau_connector_helper_funcs = {
702 .get_modes = nouveau_connector_get_modes,
703 .mode_valid = nouveau_connector_mode_valid,
704 .best_encoder = nouveau_connector_best_encoder,
705};
706
707static const struct drm_connector_funcs
708nouveau_connector_funcs = {
709 .dpms = drm_helper_connector_dpms,
710 .save = NULL,
711 .restore = NULL,
712 .detect = nouveau_connector_detect,
713 .destroy = nouveau_connector_destroy,
714 .fill_modes = drm_helper_probe_single_connector_modes,
715 .set_property = nouveau_connector_set_property,
716 .force = nouveau_connector_force
717};
718
d17f395c
BS
719static const struct drm_connector_funcs
720nouveau_connector_funcs_lvds = {
721 .dpms = drm_helper_connector_dpms,
722 .save = NULL,
723 .restore = NULL,
724 .detect = nouveau_connector_detect_lvds,
725 .destroy = nouveau_connector_destroy,
726 .fill_modes = drm_helper_probe_single_connector_modes,
727 .set_property = nouveau_connector_set_property,
728 .force = nouveau_connector_force
729};
6ee73861
BS
730
731int
7f612d87
BS
732nouveau_connector_create(struct drm_device *dev,
733 struct dcb_connector_table_entry *dcb)
6ee73861 734{
d17f395c 735 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
6ee73861
BS
736 struct drm_nouveau_private *dev_priv = dev->dev_private;
737 struct nouveau_connector *nv_connector = NULL;
738 struct drm_connector *connector;
739 struct drm_encoder *encoder;
2fa67f12 740 int type, ret = 0;
6ee73861 741
ef2bb506 742 NV_DEBUG_KMS(dev, "\n");
6ee73861 743
7f612d87
BS
744 switch (dcb->type) {
745 case DCB_CONNECTOR_NONE:
746 return 0;
747 case DCB_CONNECTOR_VGA:
6ee73861 748 NV_INFO(dev, "Detected a VGA connector\n");
7f612d87 749 type = DRM_MODE_CONNECTOR_VGA;
6ee73861 750 break;
7f612d87
BS
751 case DCB_CONNECTOR_TV_0:
752 case DCB_CONNECTOR_TV_1:
753 case DCB_CONNECTOR_TV_3:
754 NV_INFO(dev, "Detected a TV connector\n");
755 type = DRM_MODE_CONNECTOR_TV;
6ee73861 756 break;
7f612d87 757 case DCB_CONNECTOR_DVI_I:
6ee73861 758 NV_INFO(dev, "Detected a DVI-I connector\n");
7f612d87 759 type = DRM_MODE_CONNECTOR_DVII;
6ee73861 760 break;
7f612d87 761 case DCB_CONNECTOR_DVI_D:
7f612d87
BS
762 NV_INFO(dev, "Detected a DVI-D connector\n");
763 type = DRM_MODE_CONNECTOR_DVID;
6ee73861 764 break;
be079e97
BS
765 case DCB_CONNECTOR_HDMI_0:
766 case DCB_CONNECTOR_HDMI_1:
767 NV_INFO(dev, "Detected a HDMI connector\n");
768 type = DRM_MODE_CONNECTOR_HDMIA;
769 break;
7f612d87
BS
770 case DCB_CONNECTOR_LVDS:
771 NV_INFO(dev, "Detected a LVDS connector\n");
772 type = DRM_MODE_CONNECTOR_LVDS;
d17f395c 773 funcs = &nouveau_connector_funcs_lvds;
6ee73861 774 break;
7f612d87 775 case DCB_CONNECTOR_DP:
6ee73861 776 NV_INFO(dev, "Detected a DisplayPort connector\n");
7f612d87 777 type = DRM_MODE_CONNECTOR_DisplayPort;
6ee73861 778 break;
be079e97
BS
779 case DCB_CONNECTOR_eDP:
780 NV_INFO(dev, "Detected an eDP connector\n");
781 type = DRM_MODE_CONNECTOR_eDP;
782 break;
6ee73861 783 default:
7f612d87
BS
784 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
785 return -EINVAL;
6ee73861
BS
786 }
787
7f612d87
BS
788 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
789 if (!nv_connector)
790 return -ENOMEM;
791 nv_connector->dcb = dcb;
792 connector = &nv_connector->base;
793
6ee73861
BS
794 /* defaults, will get overridden in detect() */
795 connector->interlace_allowed = false;
796 connector->doublescan_allowed = false;
797
d17f395c 798 drm_connector_init(dev, connector, funcs, type);
6ee73861
BS
799 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
800
7f612d87
BS
801 /* attach encoders */
802 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
803 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
804
805 if (nv_encoder->dcb->connector != dcb->index)
806 continue;
807
808 if (get_slave_funcs(nv_encoder))
809 get_slave_funcs(nv_encoder)->create_resources(encoder, connector);
810
811 drm_mode_connector_attach_encoder(connector, encoder);
812 }
813
814 if (!connector->encoder_ids[0]) {
815 NV_WARN(dev, " no encoders, ignoring\n");
2fa67f12
FJ
816 goto fail;
817 }
818
819 /* Check if we need dithering enabled */
820 if (dcb->type == DCB_CONNECTOR_LVDS) {
821 bool dummy, is_24bit = false;
822
823 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &is_24bit);
824 if (ret) {
825 NV_ERROR(dev, "Error parsing LVDS table, disabling "
826 "LVDS\n");
827 goto fail;
828 }
829
830 nv_connector->use_dithering = !is_24bit;
7f612d87
BS
831 }
832
6ee73861 833 /* Init DVI-I specific properties */
be079e97 834 if (dcb->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
835 drm_mode_create_dvi_i_properties(dev);
836 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
837 drm_connector_attach_property(connector, dev->mode_config.dvi_i_select_subconnector_property, 0);
838 }
839
be079e97
BS
840 switch (dcb->type) {
841 case DCB_CONNECTOR_VGA:
eb1f8e4f 842 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
be079e97 843 if (dev_priv->card_type >= NV_50) {
6ee73861
BS
844 drm_connector_attach_property(connector,
845 dev->mode_config.scaling_mode_property,
846 nv_connector->scaling_mode);
847 }
be079e97
BS
848 /* fall-through */
849 case DCB_CONNECTOR_TV_0:
850 case DCB_CONNECTOR_TV_1:
851 case DCB_CONNECTOR_TV_3:
852 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
853 break;
eb1f8e4f
DA
854 case DCB_CONNECTOR_DP:
855 case DCB_CONNECTOR_eDP:
856 case DCB_CONNECTOR_HDMI_0:
857 case DCB_CONNECTOR_HDMI_1:
858 case DCB_CONNECTOR_DVI_I:
859 case DCB_CONNECTOR_DVI_D:
860 if (dev_priv->card_type >= NV_50)
861 connector->polled = DRM_CONNECTOR_POLL_HPD;
862 else
863 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
864 /* fall-through */
be079e97
BS
865 default:
866 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
867
868 drm_connector_attach_property(connector,
869 dev->mode_config.scaling_mode_property,
870 nv_connector->scaling_mode);
871 drm_connector_attach_property(connector,
872 dev->mode_config.dithering_mode_property,
873 nv_connector->use_dithering ?
874 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
875 break;
6ee73861
BS
876 }
877
6ee73861 878 drm_sysfs_connector_add(connector);
6ee73861 879 return 0;
2fa67f12
FJ
880
881fail:
882 drm_connector_cleanup(connector);
883 kfree(connector);
884 return ret;
885
6ee73861 886}