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