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