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