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