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