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