drm/nv50: obey dcb->duallink_possible
[safe/jmp/linux-2.6] / drivers / gpu / drm / nouveau / nouveau_connector.c
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 <acpi/button.h>
28
29 #include "drmP.h"
30 #include "drm_edid.h"
31 #include "drm_crtc_helper.h"
32
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
40 static inline struct drm_encoder_slave_funcs *
41 get_slave_funcs(struct nouveau_encoder *enc)
42 {
43         return to_encoder_slave(to_drm_encoder(enc))->slave_funcs;
44 }
45
46 static struct nouveau_encoder *
47 find_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
71 struct nouveau_connector *
72 nouveau_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
86 static void
87 nouveau_connector_destroy(struct drm_connector *drm_connector)
88 {
89         struct nouveau_connector *nv_connector =
90                 nouveau_connector(drm_connector);
91         struct drm_device *dev;
92
93         if (!nv_connector)
94                 return;
95
96         dev = nv_connector->base.dev;
97         NV_DEBUG_KMS(dev, "\n");
98
99         kfree(nv_connector->edid);
100         drm_sysfs_connector_remove(drm_connector);
101         drm_connector_cleanup(drm_connector);
102         kfree(drm_connector);
103 }
104
105 static void
106 nouveau_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
123 static void
124 nouveau_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
137 static struct nouveau_i2c_chan *
138 nouveau_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
193 static void
194 nouveau_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
221         if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
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
230 static enum drm_connector_status
231 nouveau_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
239         if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS)
240                 nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
241         if (nv_encoder && nv_connector->native_mode) {
242                 unsigned status = connector_status_connected;
243
244 #ifdef CONFIG_ACPI
245                 if (!nouveau_ignorelid && !acpi_lid_open())
246                         status = connector_status_unknown;
247 #endif
248                 nouveau_connector_set_encoder(connector, nv_encoder);
249                 return status;
250         }
251
252         /* Cleanup the previous EDID block. */
253         if (nv_connector->edid) {
254                 drm_mode_connector_update_edid_property(connector, NULL);
255                 kfree(nv_connector->edid);
256                 nv_connector->edid = NULL;
257         }
258
259         i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
260         if (i2c) {
261                 nouveau_connector_ddc_prepare(connector, &flags);
262                 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
263                 nouveau_connector_ddc_finish(connector, flags);
264                 drm_mode_connector_update_edid_property(connector,
265                                                         nv_connector->edid);
266                 if (!nv_connector->edid) {
267                         NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
268                                  drm_get_connector_name(connector));
269                         goto detect_analog;
270                 }
271
272                 if (nv_encoder->dcb->type == OUTPUT_DP &&
273                     !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
274                         NV_ERROR(dev, "Detected %s, but failed init\n",
275                                  drm_get_connector_name(connector));
276                         return connector_status_disconnected;
277                 }
278
279                 /* Override encoder type for DVI-I based on whether EDID
280                  * says the display is digital or analog, both use the
281                  * same i2c channel so the value returned from ddc_detect
282                  * isn't necessarily correct.
283                  */
284                 if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
285                         if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
286                                 type = OUTPUT_TMDS;
287                         else
288                                 type = OUTPUT_ANALOG;
289
290                         nv_encoder = find_encoder_by_type(connector, type);
291                         if (!nv_encoder) {
292                                 NV_ERROR(dev, "Detected %d encoder on %s, "
293                                               "but no object!\n", type,
294                                          drm_get_connector_name(connector));
295                                 return connector_status_disconnected;
296                         }
297                 }
298
299                 nouveau_connector_set_encoder(connector, nv_encoder);
300                 return connector_status_connected;
301         }
302
303 detect_analog:
304         nv_encoder = find_encoder_by_type(connector, OUTPUT_ANALOG);
305         if (!nv_encoder && !nouveau_tv_disable)
306                 nv_encoder = find_encoder_by_type(connector, OUTPUT_TV);
307         if (nv_encoder) {
308                 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
309                 struct drm_encoder_helper_funcs *helper =
310                                                 encoder->helper_private;
311
312                 if (helper->detect(encoder, connector) ==
313                                                 connector_status_connected) {
314                         nouveau_connector_set_encoder(connector, nv_encoder);
315                         return connector_status_connected;
316                 }
317
318         }
319
320         return connector_status_disconnected;
321 }
322
323 static void
324 nouveau_connector_force(struct drm_connector *connector)
325 {
326         struct nouveau_connector *nv_connector = nouveau_connector(connector);
327         struct nouveau_encoder *nv_encoder;
328         int type;
329
330         if (nv_connector->dcb->type == DCB_CONNECTOR_DVI_I) {
331                 if (connector->force == DRM_FORCE_ON_DIGITAL)
332                         type = OUTPUT_TMDS;
333                 else
334                         type = OUTPUT_ANALOG;
335         } else
336                 type = OUTPUT_ANY;
337
338         nv_encoder = find_encoder_by_type(connector, type);
339         if (!nv_encoder) {
340                 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
341                          drm_get_connector_name(connector));
342                 connector->status = connector_status_disconnected;
343                 return;
344         }
345
346         nouveau_connector_set_encoder(connector, nv_encoder);
347 }
348
349 static int
350 nouveau_connector_set_property(struct drm_connector *connector,
351                                struct drm_property *property, uint64_t value)
352 {
353         struct nouveau_connector *nv_connector = nouveau_connector(connector);
354         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
355         struct drm_device *dev = connector->dev;
356         int ret;
357
358         /* Scaling mode */
359         if (property == dev->mode_config.scaling_mode_property) {
360                 struct nouveau_crtc *nv_crtc = NULL;
361                 bool modeset = false;
362
363                 switch (value) {
364                 case DRM_MODE_SCALE_NONE:
365                 case DRM_MODE_SCALE_FULLSCREEN:
366                 case DRM_MODE_SCALE_CENTER:
367                 case DRM_MODE_SCALE_ASPECT:
368                         break;
369                 default:
370                         return -EINVAL;
371                 }
372
373                 /* LVDS always needs gpu scaling */
374                 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS &&
375                     value == DRM_MODE_SCALE_NONE)
376                         return -EINVAL;
377
378                 /* Changing between GPU and panel scaling requires a full
379                  * modeset
380                  */
381                 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
382                     (value == DRM_MODE_SCALE_NONE))
383                         modeset = true;
384                 nv_connector->scaling_mode = value;
385
386                 if (connector->encoder && connector->encoder->crtc)
387                         nv_crtc = nouveau_crtc(connector->encoder->crtc);
388                 if (!nv_crtc)
389                         return 0;
390
391                 if (modeset || !nv_crtc->set_scale) {
392                         ret = drm_crtc_helper_set_mode(&nv_crtc->base,
393                                                         &nv_crtc->base.mode,
394                                                         nv_crtc->base.x,
395                                                         nv_crtc->base.y, NULL);
396                         if (!ret)
397                                 return -EINVAL;
398                 } else {
399                         ret = nv_crtc->set_scale(nv_crtc, value, true);
400                         if (ret)
401                                 return ret;
402                 }
403
404                 return 0;
405         }
406
407         /* Dithering */
408         if (property == dev->mode_config.dithering_mode_property) {
409                 struct nouveau_crtc *nv_crtc = NULL;
410
411                 if (value == DRM_MODE_DITHERING_ON)
412                         nv_connector->use_dithering = true;
413                 else
414                         nv_connector->use_dithering = false;
415
416                 if (connector->encoder && connector->encoder->crtc)
417                         nv_crtc = nouveau_crtc(connector->encoder->crtc);
418
419                 if (!nv_crtc || !nv_crtc->set_dither)
420                         return 0;
421
422                 return nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering,
423                                            true);
424         }
425
426         if (nv_encoder && nv_encoder->dcb->type == OUTPUT_TV)
427                 return get_slave_funcs(nv_encoder)->
428                         set_property(to_drm_encoder(nv_encoder), connector, property, value);
429
430         return -EINVAL;
431 }
432
433 static struct drm_display_mode *
434 nouveau_connector_native_mode(struct nouveau_connector *connector)
435 {
436         struct drm_device *dev = connector->base.dev;
437         struct drm_display_mode *mode, *largest = NULL;
438         int high_w = 0, high_h = 0, high_v = 0;
439
440         /* Use preferred mode if there is one.. */
441         list_for_each_entry(mode, &connector->base.probed_modes, head) {
442                 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
443                         NV_DEBUG_KMS(dev, "native mode from preferred\n");
444                         return drm_mode_duplicate(dev, mode);
445                 }
446         }
447
448         /* Otherwise, take the resolution with the largest width, then height,
449          * then vertical refresh
450          */
451         list_for_each_entry(mode, &connector->base.probed_modes, head) {
452                 if (mode->hdisplay < high_w)
453                         continue;
454
455                 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
456                         continue;
457
458                 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
459                     mode->vrefresh < high_v)
460                         continue;
461
462                 high_w = mode->hdisplay;
463                 high_h = mode->vdisplay;
464                 high_v = mode->vrefresh;
465                 largest = mode;
466         }
467
468         NV_DEBUG_KMS(dev, "native mode from largest: %dx%d@%d\n",
469                       high_w, high_h, high_v);
470         return largest ? drm_mode_duplicate(dev, largest) : NULL;
471 }
472
473 struct moderec {
474         int hdisplay;
475         int vdisplay;
476 };
477
478 static struct moderec scaler_modes[] = {
479         { 1920, 1200 },
480         { 1920, 1080 },
481         { 1680, 1050 },
482         { 1600, 1200 },
483         { 1400, 1050 },
484         { 1280, 1024 },
485         { 1280, 960 },
486         { 1152, 864 },
487         { 1024, 768 },
488         { 800, 600 },
489         { 720, 400 },
490         { 640, 480 },
491         { 640, 400 },
492         { 640, 350 },
493         {}
494 };
495
496 static int
497 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
498 {
499         struct nouveau_connector *nv_connector = nouveau_connector(connector);
500         struct drm_display_mode *native = nv_connector->native_mode, *m;
501         struct drm_device *dev = connector->dev;
502         struct moderec *mode = &scaler_modes[0];
503         int modes = 0;
504
505         if (!native)
506                 return 0;
507
508         while (mode->hdisplay) {
509                 if (mode->hdisplay <= native->hdisplay &&
510                     mode->vdisplay <= native->vdisplay) {
511                         m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
512                                          drm_mode_vrefresh(native), false,
513                                          false, false);
514                         if (!m)
515                                 continue;
516
517                         m->type |= DRM_MODE_TYPE_DRIVER;
518
519                         drm_mode_probed_add(connector, m);
520                         modes++;
521                 }
522
523                 mode++;
524         }
525
526         return modes;
527 }
528
529 static int
530 nouveau_connector_get_modes(struct drm_connector *connector)
531 {
532         struct drm_device *dev = connector->dev;
533         struct nouveau_connector *nv_connector = nouveau_connector(connector);
534         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
535         int ret = 0;
536
537         /* If we're not LVDS, destroy the previous native mode, the attached
538          * monitor could have changed.
539          */
540         if (nv_connector->dcb->type != DCB_CONNECTOR_LVDS &&
541             nv_connector->native_mode) {
542                 drm_mode_destroy(dev, nv_connector->native_mode);
543                 nv_connector->native_mode = NULL;
544         }
545
546         if (nv_connector->edid)
547                 ret = drm_add_edid_modes(connector, nv_connector->edid);
548
549         /* Find the native mode if this is a digital panel, if we didn't
550          * find any modes through DDC previously add the native mode to
551          * the list of modes.
552          */
553         if (!nv_connector->native_mode)
554                 nv_connector->native_mode =
555                         nouveau_connector_native_mode(nv_connector);
556         if (ret == 0 && nv_connector->native_mode) {
557                 struct drm_display_mode *mode;
558
559                 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
560                 drm_mode_probed_add(connector, mode);
561                 ret = 1;
562         }
563
564         if (nv_encoder->dcb->type == OUTPUT_TV)
565                 ret = get_slave_funcs(nv_encoder)->
566                         get_modes(to_drm_encoder(nv_encoder), connector);
567
568         if (nv_encoder->dcb->type == OUTPUT_LVDS)
569                 ret += nouveau_connector_scaler_modes_add(connector);
570
571         return ret;
572 }
573
574 static int
575 nouveau_connector_mode_valid(struct drm_connector *connector,
576                              struct drm_display_mode *mode)
577 {
578         struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
579         struct nouveau_connector *nv_connector = nouveau_connector(connector);
580         struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
581         unsigned min_clock = 25000, max_clock = min_clock;
582         unsigned clock = mode->clock;
583
584         switch (nv_encoder->dcb->type) {
585         case OUTPUT_LVDS:
586                 BUG_ON(!nv_connector->native_mode);
587                 if (mode->hdisplay > nv_connector->native_mode->hdisplay ||
588                     mode->vdisplay > nv_connector->native_mode->vdisplay)
589                         return MODE_PANEL;
590
591                 min_clock = 0;
592                 max_clock = 400000;
593                 break;
594         case OUTPUT_TMDS:
595                 if ((dev_priv->card_type >= NV_50 && !nouveau_duallink) ||
596                     !nv_encoder->dcb->duallink_possible)
597                         max_clock = 165000;
598                 else
599                         max_clock = 330000;
600                 break;
601         case OUTPUT_ANALOG:
602                 max_clock = nv_encoder->dcb->crtconf.maxfreq;
603                 if (!max_clock)
604                         max_clock = 350000;
605                 break;
606         case OUTPUT_TV:
607                 return get_slave_funcs(nv_encoder)->
608                         mode_valid(to_drm_encoder(nv_encoder), mode);
609         case OUTPUT_DP:
610                 if (nv_encoder->dp.link_bw == DP_LINK_BW_2_7)
611                         max_clock = nv_encoder->dp.link_nr * 270000;
612                 else
613                         max_clock = nv_encoder->dp.link_nr * 162000;
614
615                 clock *= 3;
616                 break;
617         default:
618                 BUG_ON(1);
619                 return MODE_BAD;
620         }
621
622         if (clock < min_clock)
623                 return MODE_CLOCK_LOW;
624
625         if (clock > max_clock)
626                 return MODE_CLOCK_HIGH;
627
628         return MODE_OK;
629 }
630
631 static struct drm_encoder *
632 nouveau_connector_best_encoder(struct drm_connector *connector)
633 {
634         struct nouveau_connector *nv_connector = nouveau_connector(connector);
635
636         if (nv_connector->detected_encoder)
637                 return to_drm_encoder(nv_connector->detected_encoder);
638
639         return NULL;
640 }
641
642 static const struct drm_connector_helper_funcs
643 nouveau_connector_helper_funcs = {
644         .get_modes = nouveau_connector_get_modes,
645         .mode_valid = nouveau_connector_mode_valid,
646         .best_encoder = nouveau_connector_best_encoder,
647 };
648
649 static const struct drm_connector_funcs
650 nouveau_connector_funcs = {
651         .dpms = drm_helper_connector_dpms,
652         .save = NULL,
653         .restore = NULL,
654         .detect = nouveau_connector_detect,
655         .destroy = nouveau_connector_destroy,
656         .fill_modes = drm_helper_probe_single_connector_modes,
657         .set_property = nouveau_connector_set_property,
658         .force = nouveau_connector_force
659 };
660
661 static int
662 nouveau_connector_create_lvds(struct drm_device *dev,
663                               struct drm_connector *connector)
664 {
665         struct nouveau_connector *nv_connector = nouveau_connector(connector);
666         struct drm_nouveau_private *dev_priv = dev->dev_private;
667         struct nouveau_i2c_chan *i2c = NULL;
668         struct nouveau_encoder *nv_encoder;
669         struct drm_display_mode native, *mode, *temp;
670         bool dummy, if_is_24bit = false;
671         int ret, flags;
672
673         nv_encoder = find_encoder_by_type(connector, OUTPUT_LVDS);
674         if (!nv_encoder)
675                 return -ENODEV;
676
677         ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &if_is_24bit);
678         if (ret) {
679                 NV_ERROR(dev, "Error parsing LVDS table, disabling LVDS\n");
680                 return ret;
681         }
682         nv_connector->use_dithering = !if_is_24bit;
683
684         /* Firstly try getting EDID over DDC, if allowed and I2C channel
685          * is available.
686          */
687         if (!dev_priv->vbios.fp_no_ddc && nv_encoder->dcb->i2c_index < 0xf)
688                 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
689
690         if (i2c) {
691                 nouveau_connector_ddc_prepare(connector, &flags);
692                 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
693                 nouveau_connector_ddc_finish(connector, flags);
694         }
695
696         /* If no EDID found above, and the VBIOS indicates a hardcoded
697          * modeline is avalilable for the panel, set it as the panel's
698          * native mode and exit.
699          */
700         if (!nv_connector->edid && nouveau_bios_fp_mode(dev, &native) &&
701              (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
702               dev_priv->vbios.fp_no_ddc)) {
703                 nv_connector->native_mode = drm_mode_duplicate(dev, &native);
704                 goto out;
705         }
706
707         /* Still nothing, some VBIOS images have a hardcoded EDID block
708          * stored for the panel stored in them.
709          */
710         if (!nv_connector->edid && !nv_connector->native_mode &&
711             !dev_priv->vbios.fp_no_ddc) {
712                 struct edid *edid =
713                         (struct edid *)nouveau_bios_embedded_edid(dev);
714                 if (edid) {
715                         nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
716                         *(nv_connector->edid) = *edid;
717                 }
718         }
719
720         if (!nv_connector->edid)
721                 goto out;
722
723         /* We didn't find/use a panel mode from the VBIOS, so parse the EDID
724          * block and look for the preferred mode there.
725          */
726         ret = drm_add_edid_modes(connector, nv_connector->edid);
727         if (ret == 0)
728                 goto out;
729         nv_connector->detected_encoder = nv_encoder;
730         nv_connector->native_mode = nouveau_connector_native_mode(nv_connector);
731         list_for_each_entry_safe(mode, temp, &connector->probed_modes, head)
732                 drm_mode_remove(connector, mode);
733
734 out:
735         if (!nv_connector->native_mode) {
736                 NV_ERROR(dev, "LVDS present in DCB table, but couldn't "
737                               "determine its native mode.  Disabling.\n");
738                 return -ENODEV;
739         }
740
741         drm_mode_connector_update_edid_property(connector, nv_connector->edid);
742         return 0;
743 }
744
745 int
746 nouveau_connector_create(struct drm_device *dev,
747                          struct dcb_connector_table_entry *dcb)
748 {
749         struct drm_nouveau_private *dev_priv = dev->dev_private;
750         struct nouveau_connector *nv_connector = NULL;
751         struct drm_connector *connector;
752         struct drm_encoder *encoder;
753         int ret, type;
754
755         NV_DEBUG_KMS(dev, "\n");
756
757         switch (dcb->type) {
758         case DCB_CONNECTOR_NONE:
759                 return 0;
760         case DCB_CONNECTOR_VGA:
761                 NV_INFO(dev, "Detected a VGA connector\n");
762                 type = DRM_MODE_CONNECTOR_VGA;
763                 break;
764         case DCB_CONNECTOR_TV_0:
765         case DCB_CONNECTOR_TV_1:
766         case DCB_CONNECTOR_TV_3:
767                 NV_INFO(dev, "Detected a TV connector\n");
768                 type = DRM_MODE_CONNECTOR_TV;
769                 break;
770         case DCB_CONNECTOR_DVI_I:
771                 NV_INFO(dev, "Detected a DVI-I connector\n");
772                 type = DRM_MODE_CONNECTOR_DVII;
773                 break;
774         case DCB_CONNECTOR_DVI_D:
775                 NV_INFO(dev, "Detected a DVI-D connector\n");
776                 type = DRM_MODE_CONNECTOR_DVID;
777                 break;
778         case DCB_CONNECTOR_HDMI_0:
779         case DCB_CONNECTOR_HDMI_1:
780                 NV_INFO(dev, "Detected a HDMI connector\n");
781                 type = DRM_MODE_CONNECTOR_HDMIA;
782                 break;
783         case DCB_CONNECTOR_LVDS:
784                 NV_INFO(dev, "Detected a LVDS connector\n");
785                 type = DRM_MODE_CONNECTOR_LVDS;
786                 break;
787         case DCB_CONNECTOR_DP:
788                 NV_INFO(dev, "Detected a DisplayPort connector\n");
789                 type = DRM_MODE_CONNECTOR_DisplayPort;
790                 break;
791         case DCB_CONNECTOR_eDP:
792                 NV_INFO(dev, "Detected an eDP connector\n");
793                 type = DRM_MODE_CONNECTOR_eDP;
794                 break;
795         default:
796                 NV_ERROR(dev, "unknown connector type: 0x%02x!!\n", dcb->type);
797                 return -EINVAL;
798         }
799
800         nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
801         if (!nv_connector)
802                 return -ENOMEM;
803         nv_connector->dcb = dcb;
804         connector = &nv_connector->base;
805
806         /* defaults, will get overridden in detect() */
807         connector->interlace_allowed = false;
808         connector->doublescan_allowed = false;
809
810         drm_connector_init(dev, connector, &nouveau_connector_funcs, type);
811         drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
812
813         /* attach encoders */
814         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
815                 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
816
817                 if (nv_encoder->dcb->connector != dcb->index)
818                         continue;
819
820                 if (get_slave_funcs(nv_encoder))
821                         get_slave_funcs(nv_encoder)->create_resources(encoder, connector);
822
823                 drm_mode_connector_attach_encoder(connector, encoder);
824         }
825
826         if (!connector->encoder_ids[0]) {
827                 NV_WARN(dev, "  no encoders, ignoring\n");
828                 drm_connector_cleanup(connector);
829                 kfree(connector);
830                 return 0;
831         }
832
833         /* Init DVI-I specific properties */
834         if (dcb->type == DCB_CONNECTOR_DVI_I) {
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
840         if (dcb->type != DCB_CONNECTOR_LVDS)
841                 nv_connector->use_dithering = false;
842
843         switch (dcb->type) {
844         case DCB_CONNECTOR_VGA:
845                 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
846                 if (dev_priv->card_type >= NV_50) {
847                         drm_connector_attach_property(connector,
848                                         dev->mode_config.scaling_mode_property,
849                                         nv_connector->scaling_mode);
850                 }
851                 /* fall-through */
852         case DCB_CONNECTOR_TV_0:
853         case DCB_CONNECTOR_TV_1:
854         case DCB_CONNECTOR_TV_3:
855                 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
856                 break;
857         case DCB_CONNECTOR_DP:
858         case DCB_CONNECTOR_eDP:
859         case DCB_CONNECTOR_HDMI_0:
860         case DCB_CONNECTOR_HDMI_1:
861         case DCB_CONNECTOR_DVI_I:
862         case DCB_CONNECTOR_DVI_D:
863                 if (dev_priv->card_type >= NV_50)
864                         connector->polled = DRM_CONNECTOR_POLL_HPD;
865                 else
866                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
867                 /* fall-through */
868         default:
869                 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
870
871                 drm_connector_attach_property(connector,
872                                 dev->mode_config.scaling_mode_property,
873                                 nv_connector->scaling_mode);
874                 drm_connector_attach_property(connector,
875                                 dev->mode_config.dithering_mode_property,
876                                 nv_connector->use_dithering ?
877                                 DRM_MODE_DITHERING_ON : DRM_MODE_DITHERING_OFF);
878                 break;
879         }
880
881         drm_sysfs_connector_add(connector);
882
883         if (dcb->type == DCB_CONNECTOR_LVDS) {
884                 ret = nouveau_connector_create_lvds(dev, connector);
885                 if (ret) {
886                         connector->funcs->destroy(connector);
887                         return ret;
888                 }
889         }
890
891         return 0;
892 }