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