drm/nouveau/clock: pull in the implementation from all over the place
[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
e0996aea
BS
40#include <subdev/bios/gpio.h>
41
fce2bad0
BS
42static void nouveau_connector_hotplug(void *, int);
43
10b461e4 44struct nouveau_encoder *
e19b20bb 45find_encoder(struct drm_connector *connector, int type)
6ee73861
BS
46{
47 struct drm_device *dev = connector->dev;
48 struct nouveau_encoder *nv_encoder;
49 struct drm_mode_object *obj;
50 int i, id;
51
52 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
53 id = connector->encoder_ids[i];
54 if (!id)
55 break;
56
57 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
58 if (!obj)
59 continue;
60 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
61
62 if (type == OUTPUT_ANY || nv_encoder->dcb->type == type)
63 return nv_encoder;
64 }
65
66 return NULL;
67}
68
69struct nouveau_connector *
70nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
71{
72 struct drm_device *dev = to_drm_encoder(encoder)->dev;
73 struct drm_connector *drm_connector;
74
75 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
76 if (drm_connector->encoder == to_drm_encoder(encoder))
77 return nouveau_connector(drm_connector);
78 }
79
80 return NULL;
81}
82
6ee73861 83static void
fce2bad0 84nouveau_connector_destroy(struct drm_connector *connector)
6ee73861 85{
fce2bad0
BS
86 struct nouveau_connector *nv_connector = nouveau_connector(connector);
87 struct drm_nouveau_private *dev_priv;
dd19e44b 88 struct drm_device *dev;
6ee73861 89
c8ebe275 90 if (!nv_connector)
6ee73861
BS
91 return;
92
dd19e44b 93 dev = nv_connector->base.dev;
fce2bad0 94 dev_priv = dev->dev_private;
dd19e44b
MS
95 NV_DEBUG_KMS(dev, "\n");
96
a0b25635
BS
97 if (nv_connector->hpd != DCB_GPIO_UNUSED) {
98 nouveau_gpio_isr_del(dev, 0, nv_connector->hpd, 0xff,
99 nouveau_connector_hotplug, connector);
fce2bad0
BS
100 }
101
c8ebe275 102 kfree(nv_connector->edid);
fce2bad0
BS
103 drm_sysfs_connector_remove(connector);
104 drm_connector_cleanup(connector);
105 kfree(connector);
6ee73861
BS
106}
107
4196faa8 108static struct nouveau_i2c_port *
6ee73861
BS
109nouveau_connector_ddc_detect(struct drm_connector *connector,
110 struct nouveau_encoder **pnv_encoder)
111{
112 struct drm_device *dev = connector->dev;
03cd06ca 113 int i;
6ee73861 114
6ee73861 115 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4196faa8 116 struct nouveau_i2c_port *i2c = NULL;
6ee73861
BS
117 struct nouveau_encoder *nv_encoder;
118 struct drm_mode_object *obj;
119 int id;
120
121 id = connector->encoder_ids[i];
122 if (!id)
123 break;
124
125 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
126 if (!obj)
127 continue;
128 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
4ca2b712
FJ
129
130 if (nv_encoder->dcb->i2c_index < 0xf)
131 i2c = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
6ee73861 132
03cd06ca 133 if (i2c && nouveau_probe_i2c_addr(i2c, 0x50)) {
6ee73861
BS
134 *pnv_encoder = nv_encoder;
135 return i2c;
136 }
137 }
138
139 return NULL;
140}
141
c16c5707
FJ
142static struct nouveau_encoder *
143nouveau_connector_of_detect(struct drm_connector *connector)
144{
145#ifdef __powerpc__
146 struct drm_device *dev = connector->dev;
147 struct nouveau_connector *nv_connector = nouveau_connector(connector);
148 struct nouveau_encoder *nv_encoder;
149 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
150
151 if (!dn ||
e19b20bb
BS
152 !((nv_encoder = find_encoder(connector, OUTPUT_TMDS)) ||
153 (nv_encoder = find_encoder(connector, OUTPUT_ANALOG))))
c16c5707
FJ
154 return NULL;
155
156 for_each_child_of_node(dn, cn) {
157 const char *name = of_get_property(cn, "name", NULL);
158 const void *edid = of_get_property(cn, "EDID", NULL);
159 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
160
161 if (nv_encoder->dcb->i2c_index == idx && edid) {
162 nv_connector->edid =
163 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
164 of_node_put(cn);
165 return nv_encoder;
166 }
167 }
168#endif
169 return NULL;
170}
171
6ee73861
BS
172static void
173nouveau_connector_set_encoder(struct drm_connector *connector,
174 struct nouveau_encoder *nv_encoder)
175{
176 struct nouveau_connector *nv_connector = nouveau_connector(connector);
177 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
178 struct drm_device *dev = connector->dev;
179
180 if (nv_connector->detected_encoder == nv_encoder)
181 return;
182 nv_connector->detected_encoder = nv_encoder;
183
c8334423
BS
184 if (dev_priv->card_type >= NV_50) {
185 connector->interlace_allowed = true;
186 connector->doublescan_allowed = true;
187 } else
6ee73861
BS
188 if (nv_encoder->dcb->type == OUTPUT_LVDS ||
189 nv_encoder->dcb->type == OUTPUT_TMDS) {
190 connector->doublescan_allowed = false;
191 connector->interlace_allowed = false;
192 } else {
193 connector->doublescan_allowed = true;
194 if (dev_priv->card_type == NV_20 ||
195 (dev_priv->card_type == NV_10 &&
196 (dev->pci_device & 0x0ff0) != 0x0100 &&
197 (dev->pci_device & 0x0ff0) != 0x0150))
198 /* HW is broken */
199 connector->interlace_allowed = false;
200 else
201 connector->interlace_allowed = true;
202 }
203
befb51e9 204 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
205 drm_connector_property_set_value(connector,
206 dev->mode_config.dvi_i_subconnector_property,
207 nv_encoder->dcb->type == OUTPUT_TMDS ?
208 DRM_MODE_SUBCONNECTOR_DVID :
209 DRM_MODE_SUBCONNECTOR_DVIA);
210 }
211}
212
213static enum drm_connector_status
930a9e28 214nouveau_connector_detect(struct drm_connector *connector, bool force)
6ee73861
BS
215{
216 struct drm_device *dev = connector->dev;
217 struct nouveau_connector *nv_connector = nouveau_connector(connector);
218 struct nouveau_encoder *nv_encoder = NULL;
e19b20bb 219 struct nouveau_encoder *nv_partner;
4196faa8 220 struct nouveau_i2c_port *i2c;
03cd06ca 221 int type;
6ee73861 222
b8780e2a
FJ
223 /* Cleanup the previous EDID block. */
224 if (nv_connector->edid) {
225 drm_mode_connector_update_edid_property(connector, NULL);
226 kfree(nv_connector->edid);
227 nv_connector->edid = NULL;
228 }
c8ebe275 229
6ee73861
BS
230 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
231 if (i2c) {
4196faa8 232 nv_connector->edid = drm_get_edid(connector, nouveau_i2c_adapter(i2c));
6ee73861
BS
233 drm_mode_connector_update_edid_property(connector,
234 nv_connector->edid);
235 if (!nv_connector->edid) {
236 NV_ERROR(dev, "DDC responded, but no EDID for %s\n",
237 drm_get_connector_name(connector));
0ed3165e 238 goto detect_analog;
6ee73861
BS
239 }
240
241 if (nv_encoder->dcb->type == OUTPUT_DP &&
242 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
243 NV_ERROR(dev, "Detected %s, but failed init\n",
244 drm_get_connector_name(connector));
245 return connector_status_disconnected;
246 }
247
248 /* Override encoder type for DVI-I based on whether EDID
249 * says the display is digital or analog, both use the
250 * same i2c channel so the value returned from ddc_detect
251 * isn't necessarily correct.
252 */
e19b20bb
BS
253 nv_partner = NULL;
254 if (nv_encoder->dcb->type == OUTPUT_TMDS)
255 nv_partner = find_encoder(connector, OUTPUT_ANALOG);
256 if (nv_encoder->dcb->type == OUTPUT_ANALOG)
257 nv_partner = find_encoder(connector, OUTPUT_TMDS);
258
259 if (nv_partner && ((nv_encoder->dcb->type == OUTPUT_ANALOG &&
260 nv_partner->dcb->type == OUTPUT_TMDS) ||
261 (nv_encoder->dcb->type == OUTPUT_TMDS &&
262 nv_partner->dcb->type == OUTPUT_ANALOG))) {
6ee73861
BS
263 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
264 type = OUTPUT_TMDS;
265 else
266 type = OUTPUT_ANALOG;
267
e19b20bb 268 nv_encoder = find_encoder(connector, type);
6ee73861
BS
269 }
270
271 nouveau_connector_set_encoder(connector, nv_encoder);
272 return connector_status_connected;
273 }
274
c16c5707
FJ
275 nv_encoder = nouveau_connector_of_detect(connector);
276 if (nv_encoder) {
277 nouveau_connector_set_encoder(connector, nv_encoder);
278 return connector_status_connected;
279 }
280
0ed3165e 281detect_analog:
e19b20bb 282 nv_encoder = find_encoder(connector, OUTPUT_ANALOG);
f4053509 283 if (!nv_encoder && !nouveau_tv_disable)
e19b20bb 284 nv_encoder = find_encoder(connector, OUTPUT_TV);
84b8081c 285 if (nv_encoder && force) {
6ee73861
BS
286 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
287 struct drm_encoder_helper_funcs *helper =
288 encoder->helper_private;
289
290 if (helper->detect(encoder, connector) ==
291 connector_status_connected) {
292 nouveau_connector_set_encoder(connector, nv_encoder);
293 return connector_status_connected;
294 }
295
296 }
297
298 return connector_status_disconnected;
299}
300
d17f395c 301static enum drm_connector_status
930a9e28 302nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
d17f395c
BS
303{
304 struct drm_device *dev = connector->dev;
305 struct drm_nouveau_private *dev_priv = dev->dev_private;
306 struct nouveau_connector *nv_connector = nouveau_connector(connector);
307 struct nouveau_encoder *nv_encoder = NULL;
308 enum drm_connector_status status = connector_status_disconnected;
309
310 /* Cleanup the previous EDID block. */
311 if (nv_connector->edid) {
312 drm_mode_connector_update_edid_property(connector, NULL);
313 kfree(nv_connector->edid);
314 nv_connector->edid = NULL;
315 }
316
e19b20bb 317 nv_encoder = find_encoder(connector, OUTPUT_LVDS);
d17f395c
BS
318 if (!nv_encoder)
319 return connector_status_disconnected;
320
a6ed76d7 321 /* Try retrieving EDID via DDC */
d17f395c 322 if (!dev_priv->vbios.fp_no_ddc) {
930a9e28 323 status = nouveau_connector_detect(connector, force);
d17f395c
BS
324 if (status == connector_status_connected)
325 goto out;
326 }
327
a6ed76d7
BS
328 /* On some laptops (Sony, i'm looking at you) there appears to
329 * be no direct way of accessing the panel's EDID. The only
330 * option available to us appears to be to ask ACPI for help..
331 *
332 * It's important this check's before trying straps, one of the
333 * said manufacturer's laptops are configured in such a way
334 * the nouveau decides an entry in the VBIOS FP mode table is
335 * valid - it's not (rh#613284)
336 */
337 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
338 if (!nouveau_acpi_edid(dev, connector)) {
339 status = connector_status_connected;
340 goto out;
341 }
342 }
343
d17f395c
BS
344 /* If no EDID found above, and the VBIOS indicates a hardcoded
345 * modeline is avalilable for the panel, set it as the panel's
346 * native mode and exit.
347 */
348 if (nouveau_bios_fp_mode(dev, NULL) && (dev_priv->vbios.fp_no_ddc ||
349 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
350 status = connector_status_connected;
351 goto out;
352 }
353
354 /* Still nothing, some VBIOS images have a hardcoded EDID block
355 * stored for the panel stored in them.
356 */
357 if (!dev_priv->vbios.fp_no_ddc) {
358 struct edid *edid =
359 (struct edid *)nouveau_bios_embedded_edid(dev);
360 if (edid) {
361 nv_connector->edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
362 *(nv_connector->edid) = *edid;
363 status = connector_status_connected;
364 }
365 }
366
367out:
368#if defined(CONFIG_ACPI_BUTTON) || \
369 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
370 if (status == connector_status_connected &&
371 !nouveau_ignorelid && !acpi_lid_open())
372 status = connector_status_unknown;
373#endif
374
375 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
3195c5f9 376 nouveau_connector_set_encoder(connector, nv_encoder);
d17f395c
BS
377 return status;
378}
379
6ee73861
BS
380static void
381nouveau_connector_force(struct drm_connector *connector)
382{
be079e97 383 struct nouveau_connector *nv_connector = nouveau_connector(connector);
6ee73861
BS
384 struct nouveau_encoder *nv_encoder;
385 int type;
386
befb51e9 387 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
6ee73861
BS
388 if (connector->force == DRM_FORCE_ON_DIGITAL)
389 type = OUTPUT_TMDS;
390 else
391 type = OUTPUT_ANALOG;
392 } else
393 type = OUTPUT_ANY;
394
e19b20bb 395 nv_encoder = find_encoder(connector, type);
6ee73861 396 if (!nv_encoder) {
be079e97 397 NV_ERROR(connector->dev, "can't find encoder to force %s on!\n",
6ee73861
BS
398 drm_get_connector_name(connector));
399 connector->status = connector_status_disconnected;
400 return;
401 }
402
403 nouveau_connector_set_encoder(connector, nv_encoder);
404}
405
406static int
407nouveau_connector_set_property(struct drm_connector *connector,
408 struct drm_property *property, uint64_t value)
409{
b29caa58
BS
410 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
411 struct nouveau_display_engine *disp = &dev_priv->engine.display;
6ee73861
BS
412 struct nouveau_connector *nv_connector = nouveau_connector(connector);
413 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
4a9f822f 414 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
6ee73861 415 struct drm_device *dev = connector->dev;
b29caa58 416 struct nouveau_crtc *nv_crtc;
6ee73861
BS
417 int ret;
418
b29caa58
BS
419 nv_crtc = NULL;
420 if (connector->encoder && connector->encoder->crtc)
421 nv_crtc = nouveau_crtc(connector->encoder->crtc);
422
6ee73861
BS
423 /* Scaling mode */
424 if (property == dev->mode_config.scaling_mode_property) {
6ee73861
BS
425 bool modeset = false;
426
427 switch (value) {
428 case DRM_MODE_SCALE_NONE:
429 case DRM_MODE_SCALE_FULLSCREEN:
430 case DRM_MODE_SCALE_CENTER:
431 case DRM_MODE_SCALE_ASPECT:
432 break;
433 default:
434 return -EINVAL;
435 }
436
437 /* LVDS always needs gpu scaling */
8c3f6bb9 438 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
6ee73861
BS
439 value == DRM_MODE_SCALE_NONE)
440 return -EINVAL;
441
442 /* Changing between GPU and panel scaling requires a full
443 * modeset
444 */
445 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
446 (value == DRM_MODE_SCALE_NONE))
447 modeset = true;
448 nv_connector->scaling_mode = value;
449
6ee73861
BS
450 if (!nv_crtc)
451 return 0;
452
453 if (modeset || !nv_crtc->set_scale) {
454 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
455 &nv_crtc->base.mode,
456 nv_crtc->base.x,
457 nv_crtc->base.y, NULL);
458 if (!ret)
459 return -EINVAL;
460 } else {
488ff207 461 ret = nv_crtc->set_scale(nv_crtc, true);
6ee73861
BS
462 if (ret)
463 return ret;
464 }
465
466 return 0;
467 }
468
b29caa58
BS
469 /* Underscan */
470 if (property == disp->underscan_property) {
471 if (nv_connector->underscan != value) {
472 nv_connector->underscan = value;
473 if (!nv_crtc || !nv_crtc->set_scale)
474 return 0;
475
488ff207 476 return nv_crtc->set_scale(nv_crtc, true);
b29caa58
BS
477 }
478
479 return 0;
480 }
481
482 if (property == disp->underscan_hborder_property) {
483 if (nv_connector->underscan_hborder != value) {
484 nv_connector->underscan_hborder = value;
485 if (!nv_crtc || !nv_crtc->set_scale)
486 return 0;
487
488ff207 488 return nv_crtc->set_scale(nv_crtc, true);
b29caa58
BS
489 }
490
491 return 0;
492 }
493
494 if (property == disp->underscan_vborder_property) {
495 if (nv_connector->underscan_vborder != value) {
496 nv_connector->underscan_vborder = value;
497 if (!nv_crtc || !nv_crtc->set_scale)
498 return 0;
499
488ff207 500 return nv_crtc->set_scale(nv_crtc, true);
b29caa58
BS
501 }
502
503 return 0;
504 }
505
6ee73861 506 /* Dithering */
de691855
BS
507 if (property == disp->dithering_mode) {
508 nv_connector->dithering_mode = value;
509 if (!nv_crtc || !nv_crtc->set_dither)
510 return 0;
511
512 return nv_crtc->set_dither(nv_crtc, true);
513 }
6ee73861 514
de691855
BS
515 if (property == disp->dithering_depth) {
516 nv_connector->dithering_depth = value;
6ee73861
BS
517 if (!nv_crtc || !nv_crtc->set_dither)
518 return 0;
519
488ff207 520 return nv_crtc->set_dither(nv_crtc, true);
6ee73861
BS
521 }
522
df26bc9c
CB
523 if (nv_crtc && nv_crtc->set_color_vibrance) {
524 /* Hue */
525 if (property == disp->vibrant_hue_property) {
526 nv_crtc->vibrant_hue = value - 90;
527 return nv_crtc->set_color_vibrance(nv_crtc, true);
528 }
529 /* Saturation */
530 if (property == disp->color_vibrance_property) {
531 nv_crtc->color_vibrance = value - 100;
532 return nv_crtc->set_color_vibrance(nv_crtc, true);
533 }
534 }
535
6ee73861 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
a6a17859
BS
658 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
659 if (nv_connector->type == DCB_CONNECTOR_eDP) {
660 connector->display_info.bpc = 6;
661 return;
662 }
663
664 /* we're out of options unless we're LVDS, default to 8bpc */
c8435362
BS
665 if (nv_encoder->dcb->type != OUTPUT_LVDS) {
666 connector->display_info.bpc = 8;
63221755 667 return;
c8435362
BS
668 }
669
670 connector->display_info.bpc = 6;
63221755
BS
671
672 /* LVDS: panel straps */
673 if (bios->fp_no_ddc) {
674 if (bios->fp.if_is_24bit)
675 connector->display_info.bpc = 8;
676 return;
677 }
678
679 /* LVDS: DDC panel, need to first determine the number of links to
680 * know which if_is_24bit flag to check...
681 */
682 if (nv_connector->edid &&
befb51e9 683 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
63221755
BS
684 duallink = ((u8 *)nv_connector->edid)[121] == 2;
685 else
686 duallink = mode->clock >= bios->fp.duallink_transition_clk;
687
688 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
689 ( duallink && (bios->fp.strapless_is_24bit & 2)))
690 connector->display_info.bpc = 8;
691}
692
6ee73861
BS
693static int
694nouveau_connector_get_modes(struct drm_connector *connector)
695{
696 struct drm_device *dev = connector->dev;
d17f395c 697 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861
BS
698 struct nouveau_connector *nv_connector = nouveau_connector(connector);
699 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
4a9f822f 700 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
6ee73861
BS
701 int ret = 0;
702
d17f395c 703 /* destroy the native mode, the attached monitor could have changed.
6ee73861 704 */
d17f395c 705 if (nv_connector->native_mode) {
6ee73861
BS
706 drm_mode_destroy(dev, nv_connector->native_mode);
707 nv_connector->native_mode = NULL;
708 }
709
710 if (nv_connector->edid)
711 ret = drm_add_edid_modes(connector, nv_connector->edid);
d17f395c
BS
712 else
713 if (nv_encoder->dcb->type == OUTPUT_LVDS &&
714 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
6e86e041 715 dev_priv->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
80dad869
BS
716 struct drm_display_mode mode;
717
718 nouveau_bios_fp_mode(dev, &mode);
719 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
d17f395c 720 }
6ee73861 721
d4c2c99b
BS
722 /* Determine display colour depth for everything except LVDS now,
723 * DP requires this before mode_valid() is called.
724 */
725 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
726 nouveau_connector_detect_depth(connector);
727
6ee73861
BS
728 /* Find the native mode if this is a digital panel, if we didn't
729 * find any modes through DDC previously add the native mode to
730 * the list of modes.
731 */
732 if (!nv_connector->native_mode)
733 nv_connector->native_mode =
26099a74 734 nouveau_connector_native_mode(connector);
6ee73861
BS
735 if (ret == 0 && nv_connector->native_mode) {
736 struct drm_display_mode *mode;
737
738 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
739 drm_mode_probed_add(connector, mode);
740 ret = 1;
741 }
742
d4c2c99b
BS
743 /* Determine LVDS colour depth, must happen after determining
744 * "native" mode as some VBIOS tables require us to use the
745 * pixel clock as part of the lookup...
63221755 746 */
d4c2c99b
BS
747 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
748 nouveau_connector_detect_depth(connector);
63221755 749
6ee73861 750 if (nv_encoder->dcb->type == OUTPUT_TV)
4a9f822f 751 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
6ee73861 752
befb51e9
BS
753 if (nv_connector->type == DCB_CONNECTOR_LVDS ||
754 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
755 nv_connector->type == DCB_CONNECTOR_eDP)
6ee73861
BS
756 ret += nouveau_connector_scaler_modes_add(connector);
757
758 return ret;
759}
760
1f5bd443
FJ
761static unsigned
762get_tmds_link_bandwidth(struct drm_connector *connector)
763{
764 struct nouveau_connector *nv_connector = nouveau_connector(connector);
765 struct drm_nouveau_private *dev_priv = connector->dev->dev_private;
766 struct dcb_entry *dcb = nv_connector->detected_encoder->dcb;
767
768 if (dcb->location != DCB_LOC_ON_CHIP ||
769 dev_priv->chipset >= 0x46)
770 return 165000;
771 else if (dev_priv->chipset >= 0x40)
772 return 155000;
773 else if (dev_priv->chipset >= 0x18)
774 return 135000;
775 else
776 return 112000;
777}
778
6ee73861
BS
779static int
780nouveau_connector_mode_valid(struct drm_connector *connector,
781 struct drm_display_mode *mode)
782{
6ee73861
BS
783 struct nouveau_connector *nv_connector = nouveau_connector(connector);
784 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
4a9f822f 785 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
6ee73861
BS
786 unsigned min_clock = 25000, max_clock = min_clock;
787 unsigned clock = mode->clock;
788
789 switch (nv_encoder->dcb->type) {
790 case OUTPUT_LVDS:
26099a74
BS
791 if (nv_connector->native_mode &&
792 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
793 mode->vdisplay > nv_connector->native_mode->vdisplay))
6ee73861
BS
794 return MODE_PANEL;
795
796 min_clock = 0;
797 max_clock = 400000;
798 break;
799 case OUTPUT_TMDS:
1f5bd443
FJ
800 max_clock = get_tmds_link_bandwidth(connector);
801 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
802 max_clock *= 2;
6ee73861
BS
803 break;
804 case OUTPUT_ANALOG:
805 max_clock = nv_encoder->dcb->crtconf.maxfreq;
806 if (!max_clock)
807 max_clock = 350000;
808 break;
809 case OUTPUT_TV:
4a9f822f 810 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
6ee73861 811 case OUTPUT_DP:
75a1fccf
BS
812 max_clock = nv_encoder->dp.link_nr;
813 max_clock *= nv_encoder->dp.link_bw;
d4c2c99b 814 clock = clock * (connector->display_info.bpc * 3) / 10;
6ee73861 815 break;
e7cc51c5
BS
816 default:
817 BUG_ON(1);
818 return MODE_BAD;
6ee73861
BS
819 }
820
821 if (clock < min_clock)
822 return MODE_CLOCK_LOW;
823
824 if (clock > max_clock)
825 return MODE_CLOCK_HIGH;
826
827 return MODE_OK;
828}
829
830static struct drm_encoder *
831nouveau_connector_best_encoder(struct drm_connector *connector)
832{
833 struct nouveau_connector *nv_connector = nouveau_connector(connector);
834
835 if (nv_connector->detected_encoder)
836 return to_drm_encoder(nv_connector->detected_encoder);
837
838 return NULL;
839}
840
841static const struct drm_connector_helper_funcs
842nouveau_connector_helper_funcs = {
843 .get_modes = nouveau_connector_get_modes,
844 .mode_valid = nouveau_connector_mode_valid,
845 .best_encoder = nouveau_connector_best_encoder,
846};
847
848static const struct drm_connector_funcs
849nouveau_connector_funcs = {
850 .dpms = drm_helper_connector_dpms,
851 .save = NULL,
852 .restore = NULL,
853 .detect = nouveau_connector_detect,
854 .destroy = nouveau_connector_destroy,
855 .fill_modes = drm_helper_probe_single_connector_modes,
856 .set_property = nouveau_connector_set_property,
857 .force = nouveau_connector_force
858};
859
d17f395c
BS
860static const struct drm_connector_funcs
861nouveau_connector_funcs_lvds = {
862 .dpms = drm_helper_connector_dpms,
863 .save = NULL,
864 .restore = NULL,
865 .detect = nouveau_connector_detect_lvds,
866 .destroy = nouveau_connector_destroy,
867 .fill_modes = drm_helper_probe_single_connector_modes,
868 .set_property = nouveau_connector_set_property,
869 .force = nouveau_connector_force
870};
6ee73861 871
befb51e9
BS
872static int
873drm_conntype_from_dcb(enum dcb_connector_type dcb)
874{
875 switch (dcb) {
876 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
877 case DCB_CONNECTOR_TV_0 :
878 case DCB_CONNECTOR_TV_1 :
879 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
fa2c113a
BS
880 case DCB_CONNECTOR_DMS59_0 :
881 case DCB_CONNECTOR_DMS59_1 :
befb51e9
BS
882 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
883 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
884 case DCB_CONNECTOR_LVDS :
885 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
4abb410a
BS
886 case DCB_CONNECTOR_DMS59_DP0:
887 case DCB_CONNECTOR_DMS59_DP1:
befb51e9
BS
888 case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
889 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
890 case DCB_CONNECTOR_HDMI_0 :
891 case DCB_CONNECTOR_HDMI_1 : return DRM_MODE_CONNECTOR_HDMIA;
892 default:
893 break;
894 }
895
896 return DRM_MODE_CONNECTOR_Unknown;
897}
898
8f1a6086
BS
899struct drm_connector *
900nouveau_connector_create(struct drm_device *dev, int index)
6ee73861 901{
d17f395c 902 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
6ee73861 903 struct drm_nouveau_private *dev_priv = dev->dev_private;
b29caa58 904 struct nouveau_display_engine *disp = &dev_priv->engine.display;
6ee73861
BS
905 struct nouveau_connector *nv_connector = NULL;
906 struct drm_connector *connector;
2fa67f12 907 int type, ret = 0;
befb51e9 908 bool dummy;
6ee73861 909
ef2bb506 910 NV_DEBUG_KMS(dev, "\n");
6ee73861 911
befb51e9
BS
912 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
913 nv_connector = nouveau_connector(connector);
914 if (nv_connector->index == index)
915 return connector;
6ee73861
BS
916 }
917
7f612d87
BS
918 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
919 if (!nv_connector)
8f1a6086 920 return ERR_PTR(-ENOMEM);
befb51e9 921
7f612d87 922 connector = &nv_connector->base;
befb51e9
BS
923 nv_connector->index = index;
924
925 /* attempt to parse vbios connector type and hotplug gpio */
926 nv_connector->dcb = dcb_conn(dev, index);
927 if (nv_connector->dcb) {
928 static const u8 hpd[16] = {
929 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
930 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
931 };
932
933 u32 entry = ROM16(nv_connector->dcb[0]);
934 if (dcb_conntab(dev)[3] >= 4)
935 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
936
937 nv_connector->hpd = ffs((entry & 0x07033000) >> 12);
938 nv_connector->hpd = hpd[nv_connector->hpd];
939
940 nv_connector->type = nv_connector->dcb[0];
941 if (drm_conntype_from_dcb(nv_connector->type) ==
942 DRM_MODE_CONNECTOR_Unknown) {
943 NV_WARN(dev, "unknown connector type %02x\n",
944 nv_connector->type);
945 nv_connector->type = DCB_CONNECTOR_NONE;
946 }
7f612d87 947
befb51e9
BS
948 /* Gigabyte NX85T */
949 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
950 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
951 nv_connector->type = DCB_CONNECTOR_DVI_I;
952 }
6ee73861 953
befb51e9
BS
954 /* Gigabyte GV-NX86T512H */
955 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
956 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
957 nv_connector->type = DCB_CONNECTOR_DVI_I;
958 }
959 } else {
960 nv_connector->type = DCB_CONNECTOR_NONE;
961 nv_connector->hpd = DCB_GPIO_UNUSED;
962 }
963
964 /* no vbios data, or an unknown dcb connector type - attempt to
965 * figure out something suitable ourselves
966 */
967 if (nv_connector->type == DCB_CONNECTOR_NONE) {
968 struct drm_nouveau_private *dev_priv = dev->dev_private;
969 struct dcb_table *dcbt = &dev_priv->vbios.dcb;
970 u32 encoders = 0;
971 int i;
972
973 for (i = 0; i < dcbt->entries; i++) {
974 if (dcbt->entry[i].connector == nv_connector->index)
975 encoders |= (1 << dcbt->entry[i].type);
976 }
6ee73861 977
befb51e9
BS
978 if (encoders & (1 << OUTPUT_DP)) {
979 if (encoders & (1 << OUTPUT_TMDS))
980 nv_connector->type = DCB_CONNECTOR_DP;
981 else
982 nv_connector->type = DCB_CONNECTOR_eDP;
983 } else
984 if (encoders & (1 << OUTPUT_TMDS)) {
985 if (encoders & (1 << OUTPUT_ANALOG))
986 nv_connector->type = DCB_CONNECTOR_DVI_I;
987 else
988 nv_connector->type = DCB_CONNECTOR_DVI_D;
989 } else
990 if (encoders & (1 << OUTPUT_ANALOG)) {
991 nv_connector->type = DCB_CONNECTOR_VGA;
992 } else
993 if (encoders & (1 << OUTPUT_LVDS)) {
994 nv_connector->type = DCB_CONNECTOR_LVDS;
995 } else
996 if (encoders & (1 << OUTPUT_TV)) {
997 nv_connector->type = DCB_CONNECTOR_TV_0;
998 }
999 }
2fa67f12 1000
befb51e9
BS
1001 type = drm_conntype_from_dcb(nv_connector->type);
1002 if (type == DRM_MODE_CONNECTOR_LVDS) {
1003 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
2fa67f12 1004 if (ret) {
befb51e9
BS
1005 NV_ERROR(dev, "Error parsing LVDS table, disabling\n");
1006 kfree(nv_connector);
1007 return ERR_PTR(ret);
2fa67f12 1008 }
befb51e9
BS
1009
1010 funcs = &nouveau_connector_funcs_lvds;
1011 } else {
1012 funcs = &nouveau_connector_funcs;
7f612d87
BS
1013 }
1014
befb51e9
BS
1015 /* defaults, will get overridden in detect() */
1016 connector->interlace_allowed = false;
1017 connector->doublescan_allowed = false;
1018
1019 drm_connector_init(dev, connector, funcs, type);
1020 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1021
6ee73861 1022 /* Init DVI-I specific properties */
befb51e9 1023 if (nv_connector->type == DCB_CONNECTOR_DVI_I)
6ee73861 1024 drm_connector_attach_property(connector, dev->mode_config.dvi_i_subconnector_property, 0);
6ee73861 1025
b29caa58 1026 /* Add overscan compensation options to digital outputs */
de691855 1027 if (disp->underscan_property &&
fa2c113a
BS
1028 (type == DRM_MODE_CONNECTOR_DVID ||
1029 type == DRM_MODE_CONNECTOR_DVII ||
1030 type == DRM_MODE_CONNECTOR_HDMIA ||
1031 type == DRM_MODE_CONNECTOR_DisplayPort)) {
b29caa58
BS
1032 drm_connector_attach_property(connector,
1033 disp->underscan_property,
1034 UNDERSCAN_OFF);
1035 drm_connector_attach_property(connector,
1036 disp->underscan_hborder_property,
1037 0);
1038 drm_connector_attach_property(connector,
1039 disp->underscan_vborder_property,
1040 0);
1041 }
1042
df26bc9c
CB
1043 /* Add hue and saturation options */
1044 if (disp->vibrant_hue_property)
1045 drm_connector_attach_property(connector,
1046 disp->vibrant_hue_property,
1047 90);
1048 if (disp->color_vibrance_property)
1049 drm_connector_attach_property(connector,
1050 disp->color_vibrance_property,
1051 150);
1052
befb51e9 1053 switch (nv_connector->type) {
be079e97
BS
1054 case DCB_CONNECTOR_VGA:
1055 if (dev_priv->card_type >= NV_50) {
6ee73861
BS
1056 drm_connector_attach_property(connector,
1057 dev->mode_config.scaling_mode_property,
1058 nv_connector->scaling_mode);
1059 }
be079e97
BS
1060 /* fall-through */
1061 case DCB_CONNECTOR_TV_0:
1062 case DCB_CONNECTOR_TV_1:
1063 case DCB_CONNECTOR_TV_3:
1064 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1065 break;
1066 default:
1067 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1068
1069 drm_connector_attach_property(connector,
1070 dev->mode_config.scaling_mode_property,
1071 nv_connector->scaling_mode);
de691855
BS
1072 if (disp->dithering_mode) {
1073 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1074 drm_connector_attach_property(connector,
1075 disp->dithering_mode,
1076 nv_connector->dithering_mode);
1077 }
1078 if (disp->dithering_depth) {
1079 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
1080 drm_connector_attach_property(connector,
1081 disp->dithering_depth,
1082 nv_connector->dithering_depth);
1083 }
be079e97 1084 break;
6ee73861
BS
1085 }
1086
a0b25635
BS
1087 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1088 if (nv_connector->hpd != DCB_GPIO_UNUSED) {
1089 ret = nouveau_gpio_isr_add(dev, 0, nv_connector->hpd, 0xff,
1090 nouveau_connector_hotplug,
1091 connector);
1092 if (ret == 0)
1093 connector->polled = DRM_CONNECTOR_POLL_HPD;
fce2bad0
BS
1094 }
1095
6ee73861 1096 drm_sysfs_connector_add(connector);
befb51e9 1097 return connector;
6ee73861 1098}
fce2bad0
BS
1099
1100static void
1101nouveau_connector_hotplug(void *data, int plugged)
1102{
1103 struct drm_connector *connector = data;
1104 struct drm_device *dev = connector->dev;
1105
68c99184
BS
1106 NV_DEBUG(dev, "%splugged %s\n", plugged ? "" : "un",
1107 drm_get_connector_name(connector));
fce2bad0 1108
68c99184
BS
1109 if (plugged)
1110 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1111 else
1112 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
fce2bad0
BS
1113
1114 drm_helper_hpd_irq_event(dev);
1115}