drm/dp_mst: Enable registration of AUX devices for MST ports
[linux-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
5addcf0a 29#include <linux/pm_runtime.h>
39c1c901 30#include <linux/vga_switcheroo.h>
5addcf0a 31
760285e7 32#include <drm/drmP.h>
616915ec 33#include <drm/drm_atomic_helper.h>
760285e7
DH
34#include <drm/drm_edid.h>
35#include <drm/drm_crtc_helper.h>
fcd70cd3 36#include <drm/drm_probe_helper.h>
a743d758 37#include <drm/drm_atomic.h>
a1470890 38
6ee73861 39#include "nouveau_reg.h"
4dc28134 40#include "nouveau_drv.h"
1a646342 41#include "dispnv04/hw.h"
c0077061 42#include "nouveau_acpi.h"
6ee73861 43
77145f1c
BS
44#include "nouveau_display.h"
45#include "nouveau_connector.h"
6ee73861
BS
46#include "nouveau_encoder.h"
47#include "nouveau_crtc.h"
77145f1c 48
923bc416 49#include <nvif/class.h>
7568b106 50#include <nvif/cl0046.h>
79ca2770
BS
51#include <nvif/event.h>
52
616915ec
BS
53struct drm_display_mode *
54nouveau_conn_native_mode(struct drm_connector *connector)
55{
56 const struct drm_connector_helper_funcs *helper = connector->helper_private;
57 struct nouveau_drm *drm = nouveau_drm(connector->dev);
58 struct drm_device *dev = connector->dev;
59 struct drm_display_mode *mode, *largest = NULL;
60 int high_w = 0, high_h = 0, high_v = 0;
61
62 list_for_each_entry(mode, &connector->probed_modes, head) {
63 mode->vrefresh = drm_mode_vrefresh(mode);
64 if (helper->mode_valid(connector, mode) != MODE_OK ||
65 (mode->flags & DRM_MODE_FLAG_INTERLACE))
66 continue;
67
68 /* Use preferred mode if there is one.. */
69 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
70 NV_DEBUG(drm, "native mode from preferred\n");
71 return drm_mode_duplicate(dev, mode);
72 }
73
74 /* Otherwise, take the resolution with the largest width, then
75 * height, then vertical refresh
76 */
77 if (mode->hdisplay < high_w)
78 continue;
79
80 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
81 continue;
82
83 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
84 mode->vrefresh < high_v)
85 continue;
86
87 high_w = mode->hdisplay;
88 high_h = mode->vdisplay;
89 high_v = mode->vrefresh;
90 largest = mode;
91 }
92
93 NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
94 high_w, high_h, high_v);
95 return largest ? drm_mode_duplicate(dev, largest) : NULL;
96}
97
98int
99nouveau_conn_atomic_get_property(struct drm_connector *connector,
100 const struct drm_connector_state *state,
101 struct drm_property *property, u64 *val)
102{
103 struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
104 struct nouveau_display *disp = nouveau_display(connector->dev);
105 struct drm_device *dev = connector->dev;
106
107 if (property == dev->mode_config.scaling_mode_property)
108 *val = asyc->scaler.mode;
109 else if (property == disp->underscan_property)
110 *val = asyc->scaler.underscan.mode;
111 else if (property == disp->underscan_hborder_property)
112 *val = asyc->scaler.underscan.hborder;
113 else if (property == disp->underscan_vborder_property)
114 *val = asyc->scaler.underscan.vborder;
115 else if (property == disp->dithering_mode)
116 *val = asyc->dither.mode;
117 else if (property == disp->dithering_depth)
118 *val = asyc->dither.depth;
119 else if (property == disp->vibrant_hue_property)
120 *val = asyc->procamp.vibrant_hue;
121 else if (property == disp->color_vibrance_property)
122 *val = asyc->procamp.color_vibrance;
123 else
124 return -EINVAL;
125
126 return 0;
127}
128
129int
130nouveau_conn_atomic_set_property(struct drm_connector *connector,
131 struct drm_connector_state *state,
132 struct drm_property *property, u64 val)
133{
134 struct drm_device *dev = connector->dev;
135 struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
136 struct nouveau_display *disp = nouveau_display(dev);
137
138 if (property == dev->mode_config.scaling_mode_property) {
139 switch (val) {
140 case DRM_MODE_SCALE_NONE:
141 /* We allow 'None' for EDID modes, even on a fixed
142 * panel (some exist with support for lower refresh
143 * rates, which people might want to use for power-
144 * saving purposes).
145 *
146 * Non-EDID modes will force the use of GPU scaling
147 * to the native mode regardless of this setting.
148 */
149 switch (connector->connector_type) {
150 case DRM_MODE_CONNECTOR_LVDS:
151 case DRM_MODE_CONNECTOR_eDP:
152 /* ... except prior to G80, where the code
153 * doesn't support such things.
154 */
0d4a2c57 155 if (disp->disp.object.oclass < NV50_DISP)
616915ec
BS
156 return -EINVAL;
157 break;
158 default:
159 break;
160 }
161 case DRM_MODE_SCALE_FULLSCREEN:
162 case DRM_MODE_SCALE_CENTER:
163 case DRM_MODE_SCALE_ASPECT:
164 break;
165 default:
166 return -EINVAL;
167 }
168
169 if (asyc->scaler.mode != val) {
170 asyc->scaler.mode = val;
171 asyc->set.scaler = true;
172 }
173 } else
174 if (property == disp->underscan_property) {
175 if (asyc->scaler.underscan.mode != val) {
176 asyc->scaler.underscan.mode = val;
177 asyc->set.scaler = true;
178 }
179 } else
180 if (property == disp->underscan_hborder_property) {
181 if (asyc->scaler.underscan.hborder != val) {
182 asyc->scaler.underscan.hborder = val;
183 asyc->set.scaler = true;
184 }
185 } else
186 if (property == disp->underscan_vborder_property) {
187 if (asyc->scaler.underscan.vborder != val) {
188 asyc->scaler.underscan.vborder = val;
189 asyc->set.scaler = true;
190 }
191 } else
192 if (property == disp->dithering_mode) {
193 if (asyc->dither.mode != val) {
194 asyc->dither.mode = val;
195 asyc->set.dither = true;
196 }
197 } else
198 if (property == disp->dithering_depth) {
199 if (asyc->dither.mode != val) {
200 asyc->dither.depth = val;
201 asyc->set.dither = true;
202 }
203 } else
204 if (property == disp->vibrant_hue_property) {
205 if (asyc->procamp.vibrant_hue != val) {
206 asyc->procamp.vibrant_hue = val;
207 asyc->set.procamp = true;
208 }
209 } else
210 if (property == disp->color_vibrance_property) {
211 if (asyc->procamp.color_vibrance != val) {
212 asyc->procamp.color_vibrance = val;
213 asyc->set.procamp = true;
214 }
215 } else {
216 return -EINVAL;
217 }
218
219 return 0;
220}
221
222void
223nouveau_conn_atomic_destroy_state(struct drm_connector *connector,
224 struct drm_connector_state *state)
225{
226 struct nouveau_conn_atom *asyc = nouveau_conn_atom(state);
227 __drm_atomic_helper_connector_destroy_state(&asyc->state);
228 kfree(asyc);
229}
230
231struct drm_connector_state *
232nouveau_conn_atomic_duplicate_state(struct drm_connector *connector)
233{
234 struct nouveau_conn_atom *armc = nouveau_conn_atom(connector->state);
235 struct nouveau_conn_atom *asyc;
236 if (!(asyc = kmalloc(sizeof(*asyc), GFP_KERNEL)))
237 return NULL;
238 __drm_atomic_helper_connector_duplicate_state(connector, &asyc->state);
239 asyc->dither = armc->dither;
240 asyc->scaler = armc->scaler;
241 asyc->procamp = armc->procamp;
242 asyc->set.mask = 0;
243 return &asyc->state;
244}
245
246void
247nouveau_conn_reset(struct drm_connector *connector)
248{
249 struct nouveau_conn_atom *asyc;
250
251 if (WARN_ON(!(asyc = kzalloc(sizeof(*asyc), GFP_KERNEL))))
252 return;
253
254 if (connector->state)
09b90e2f 255 nouveau_conn_atomic_destroy_state(connector, connector->state);
616915ec
BS
256 __drm_atomic_helper_connector_reset(connector, &asyc->state);
257 asyc->dither.mode = DITHERING_MODE_AUTO;
258 asyc->dither.depth = DITHERING_DEPTH_AUTO;
259 asyc->scaler.mode = DRM_MODE_SCALE_NONE;
260 asyc->scaler.underscan.mode = UNDERSCAN_OFF;
261 asyc->procamp.color_vibrance = 150;
262 asyc->procamp.vibrant_hue = 90;
263
0d4a2c57 264 if (nouveau_display(connector->dev)->disp.object.oclass < NV50_DISP) {
616915ec
BS
265 switch (connector->connector_type) {
266 case DRM_MODE_CONNECTOR_LVDS:
267 /* See note in nouveau_conn_atomic_set_property(). */
268 asyc->scaler.mode = DRM_MODE_SCALE_FULLSCREEN;
269 break;
270 default:
271 break;
272 }
273 }
274}
275
56182b8b
BS
276void
277nouveau_conn_attach_properties(struct drm_connector *connector)
278{
279 struct drm_device *dev = connector->dev;
280 struct nouveau_conn_atom *armc = nouveau_conn_atom(connector->state);
281 struct nouveau_display *disp = nouveau_display(dev);
282
283 /* Init DVI-I specific properties. */
284 if (connector->connector_type == DRM_MODE_CONNECTOR_DVII)
285 drm_object_attach_property(&connector->base, dev->mode_config.
286 dvi_i_subconnector_property, 0);
287
288 /* Add overscan compensation options to digital outputs. */
289 if (disp->underscan_property &&
290 (connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
291 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
292 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
293 connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)) {
294 drm_object_attach_property(&connector->base,
295 disp->underscan_property,
296 UNDERSCAN_OFF);
297 drm_object_attach_property(&connector->base,
298 disp->underscan_hborder_property, 0);
299 drm_object_attach_property(&connector->base,
300 disp->underscan_vborder_property, 0);
301 }
302
303 /* Add hue and saturation options. */
304 if (disp->vibrant_hue_property)
305 drm_object_attach_property(&connector->base,
306 disp->vibrant_hue_property,
307 armc->procamp.vibrant_hue);
308 if (disp->color_vibrance_property)
309 drm_object_attach_property(&connector->base,
310 disp->color_vibrance_property,
311 armc->procamp.color_vibrance);
312
313 /* Scaling mode property. */
314 switch (connector->connector_type) {
315 case DRM_MODE_CONNECTOR_TV:
316 break;
317 case DRM_MODE_CONNECTOR_VGA:
0d4a2c57 318 if (disp->disp.object.oclass < NV50_DISP)
56182b8b
BS
319 break; /* Can only scale on DFPs. */
320 /* Fall-through. */
321 default:
322 drm_object_attach_property(&connector->base, dev->mode_config.
323 scaling_mode_property,
324 armc->scaler.mode);
325 break;
326 }
327
328 /* Dithering properties. */
329 switch (connector->connector_type) {
330 case DRM_MODE_CONNECTOR_TV:
331 case DRM_MODE_CONNECTOR_VGA:
332 break;
333 default:
334 if (disp->dithering_mode) {
335 drm_object_attach_property(&connector->base,
336 disp->dithering_mode,
337 armc->dither.mode);
338 }
339 if (disp->dithering_depth) {
340 drm_object_attach_property(&connector->base,
341 disp->dithering_depth,
342 armc->dither.depth);
343 }
344 break;
345 }
346}
347
77145f1c 348MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
703fa264 349int nouveau_tv_disable = 0;
77145f1c
BS
350module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
351
352MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
703fa264 353int nouveau_ignorelid = 0;
77145f1c
BS
354module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
355
356MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
703fa264 357int nouveau_duallink = 1;
77145f1c 358module_param_named(duallink, nouveau_duallink, int, 0400);
6ee73861 359
1a0c96c0
IM
360MODULE_PARM_DESC(hdmimhz, "Force a maximum HDMI pixel clock (in MHz)");
361int nouveau_hdmimhz = 0;
362module_param_named(hdmimhz, nouveau_hdmimhz, int, 0400);
363
10b461e4 364struct nouveau_encoder *
e19b20bb 365find_encoder(struct drm_connector *connector, int type)
6ee73861 366{
6ee73861 367 struct nouveau_encoder *nv_encoder;
6d385c0a 368 struct drm_encoder *enc;
ddba766d 369 int i;
6ee73861 370
ddba766d 371 drm_connector_for_each_possible_encoder(connector, enc, i) {
6d385c0a 372 nv_encoder = nouveau_encoder(enc);
6ee73861 373
4874322e
BS
374 if (type == DCB_OUTPUT_ANY ||
375 (nv_encoder->dcb && nv_encoder->dcb->type == type))
6ee73861
BS
376 return nv_encoder;
377 }
378
379 return NULL;
380}
381
382struct nouveau_connector *
383nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
384{
385 struct drm_device *dev = to_drm_encoder(encoder)->dev;
386 struct drm_connector *drm_connector;
387
388 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
389 if (drm_connector->encoder == to_drm_encoder(encoder))
390 return nouveau_connector(drm_connector);
391 }
392
393 return NULL;
394}
395
6ee73861 396static void
fce2bad0 397nouveau_connector_destroy(struct drm_connector *connector)
6ee73861 398{
fce2bad0 399 struct nouveau_connector *nv_connector = nouveau_connector(connector);
80bc340b 400 nvif_notify_fini(&nv_connector->hpd);
c8ebe275 401 kfree(nv_connector->edid);
34ea3d38 402 drm_connector_unregister(connector);
fce2bad0 403 drm_connector_cleanup(connector);
46094b2b
HV
404 if (nv_connector->aux.transfer) {
405 drm_dp_cec_unregister_connector(&nv_connector->aux);
8894f491 406 drm_dp_aux_unregister(&nv_connector->aux);
3c7fc252 407 kfree(nv_connector->aux.name);
46094b2b 408 }
fce2bad0 409 kfree(connector);
6ee73861
BS
410}
411
8777c5c1
BS
412static struct nouveau_encoder *
413nouveau_connector_ddc_detect(struct drm_connector *connector)
6ee73861
BS
414{
415 struct drm_device *dev = connector->dev;
d5986a1c 416 struct nouveau_encoder *nv_encoder = NULL, *found = NULL;
6d385c0a 417 struct drm_encoder *encoder;
f6d52b21 418 int i, ret;
d5986a1c 419 bool switcheroo_ddc = false;
6ee73861 420
ddba766d 421 drm_connector_for_each_possible_encoder(connector, encoder, i) {
6d385c0a 422 nv_encoder = nouveau_encoder(encoder);
4ca2b712 423
d5986a1c
LP
424 switch (nv_encoder->dcb->type) {
425 case DCB_OUTPUT_DP:
426 ret = nouveau_dp_detect(nv_encoder);
52aa30f2
BS
427 if (ret == NOUVEAU_DP_MST)
428 return NULL;
d5986a1c
LP
429 else if (ret == NOUVEAU_DP_SST)
430 found = nv_encoder;
431
432 break;
433 case DCB_OUTPUT_LVDS:
434 switcheroo_ddc = !!(vga_switcheroo_handler_flags() &
435 VGA_SWITCHEROO_CAN_SWITCH_DDC);
436 /* fall-through */
437 default:
438 if (!nv_encoder->i2c)
39c1c901 439 break;
d5986a1c
LP
440
441 if (switcheroo_ddc)
442 vga_switcheroo_lock_ddc(dev->pdev);
2aa5eac5 443 if (nvkm_probe_i2c(nv_encoder->i2c, 0x50))
d5986a1c
LP
444 found = nv_encoder;
445 if (switcheroo_ddc)
446 vga_switcheroo_unlock_ddc(dev->pdev);
447
448 break;
6ee73861 449 }
d5986a1c
LP
450 if (found)
451 break;
6ee73861
BS
452 }
453
d5986a1c 454 return found;
6ee73861
BS
455}
456
c16c5707
FJ
457static struct nouveau_encoder *
458nouveau_connector_of_detect(struct drm_connector *connector)
459{
460#ifdef __powerpc__
461 struct drm_device *dev = connector->dev;
462 struct nouveau_connector *nv_connector = nouveau_connector(connector);
463 struct nouveau_encoder *nv_encoder;
464 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
465
466 if (!dn ||
cb75d97e
BS
467 !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
468 (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
c16c5707
FJ
469 return NULL;
470
471 for_each_child_of_node(dn, cn) {
472 const char *name = of_get_property(cn, "name", NULL);
473 const void *edid = of_get_property(cn, "EDID", NULL);
474 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
475
476 if (nv_encoder->dcb->i2c_index == idx && edid) {
477 nv_connector->edid =
478 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
479 of_node_put(cn);
480 return nv_encoder;
481 }
482 }
483#endif
484 return NULL;
485}
486
6ee73861
BS
487static void
488nouveau_connector_set_encoder(struct drm_connector *connector,
489 struct nouveau_encoder *nv_encoder)
490{
491 struct nouveau_connector *nv_connector = nouveau_connector(connector);
77145f1c 492 struct nouveau_drm *drm = nouveau_drm(connector->dev);
6ee73861
BS
493 struct drm_device *dev = connector->dev;
494
495 if (nv_connector->detected_encoder == nv_encoder)
496 return;
497 nv_connector->detected_encoder = nv_encoder;
498
1167c6bc 499 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
c8334423
BS
500 connector->interlace_allowed = true;
501 connector->doublescan_allowed = true;
502 } else
cb75d97e
BS
503 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
504 nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
6ee73861
BS
505 connector->doublescan_allowed = false;
506 connector->interlace_allowed = false;
507 } else {
508 connector->doublescan_allowed = true;
1167c6bc
BS
509 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_KELVIN ||
510 (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
4a0ff754
IM
511 (dev->pdev->device & 0x0ff0) != 0x0100 &&
512 (dev->pdev->device & 0x0ff0) != 0x0150))
6ee73861
BS
513 /* HW is broken */
514 connector->interlace_allowed = false;
515 else
516 connector->interlace_allowed = true;
517 }
518
befb51e9 519 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
2db83827 520 drm_object_property_set_value(&connector->base,
6ee73861 521 dev->mode_config.dvi_i_subconnector_property,
cb75d97e 522 nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
6ee73861
BS
523 DRM_MODE_SUBCONNECTOR_DVID :
524 DRM_MODE_SUBCONNECTOR_DVIA);
525 }
526}
527
528static enum drm_connector_status
930a9e28 529nouveau_connector_detect(struct drm_connector *connector, bool force)
6ee73861
BS
530{
531 struct drm_device *dev = connector->dev;
77145f1c 532 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861
BS
533 struct nouveau_connector *nv_connector = nouveau_connector(connector);
534 struct nouveau_encoder *nv_encoder = NULL;
e19b20bb 535 struct nouveau_encoder *nv_partner;
2aa5eac5 536 struct i2c_adapter *i2c;
03cd06ca 537 int type;
5addcf0a
DA
538 int ret;
539 enum drm_connector_status conn_status = connector_status_disconnected;
6ee73861 540
b8780e2a
FJ
541 /* Cleanup the previous EDID block. */
542 if (nv_connector->edid) {
c555f023 543 drm_connector_update_edid_property(connector, NULL);
b8780e2a
FJ
544 kfree(nv_connector->edid);
545 nv_connector->edid = NULL;
546 }
c8ebe275 547
6833fb1e
LP
548 /* Outputs are only polled while runtime active, so resuming the
549 * device here is unnecessary (and would deadlock upon runtime suspend
550 * because it waits for polling to finish). We do however, want to
551 * prevent the autosuspend timer from elapsing during this operation
552 * if possible.
d61a5c10 553 */
6833fb1e
LP
554 if (drm_kms_helper_is_poll_worker()) {
555 pm_runtime_get_noresume(dev->dev);
556 } else {
557 ret = pm_runtime_get_sync(dev->dev);
d61a5c10
LW
558 if (ret < 0 && ret != -EACCES)
559 return conn_status;
560 }
5addcf0a 561
8777c5c1
BS
562 nv_encoder = nouveau_connector_ddc_detect(connector);
563 if (nv_encoder && (i2c = nv_encoder->i2c) != NULL) {
39c1c901
LW
564 if ((vga_switcheroo_handler_flags() &
565 VGA_SWITCHEROO_CAN_SWITCH_DDC) &&
566 nv_connector->type == DCB_CONNECTOR_LVDS)
567 nv_connector->edid = drm_get_edid_switcheroo(connector,
568 i2c);
569 else
570 nv_connector->edid = drm_get_edid(connector, i2c);
571
c555f023 572 drm_connector_update_edid_property(connector,
6ee73861
BS
573 nv_connector->edid);
574 if (!nv_connector->edid) {
77145f1c 575 NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
8c6c361a 576 connector->name);
0ed3165e 577 goto detect_analog;
6ee73861
BS
578 }
579
6ee73861
BS
580 /* Override encoder type for DVI-I based on whether EDID
581 * says the display is digital or analog, both use the
582 * same i2c channel so the value returned from ddc_detect
583 * isn't necessarily correct.
584 */
e19b20bb 585 nv_partner = NULL;
cb75d97e
BS
586 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
587 nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
588 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
589 nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
590
591 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
592 nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
593 (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
594 nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
6ee73861 595 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
cb75d97e 596 type = DCB_OUTPUT_TMDS;
6ee73861 597 else
cb75d97e 598 type = DCB_OUTPUT_ANALOG;
6ee73861 599
e19b20bb 600 nv_encoder = find_encoder(connector, type);
6ee73861
BS
601 }
602
603 nouveau_connector_set_encoder(connector, nv_encoder);
5addcf0a 604 conn_status = connector_status_connected;
46094b2b 605 drm_dp_cec_set_edid(&nv_connector->aux, nv_connector->edid);
5addcf0a 606 goto out;
6ee73861
BS
607 }
608
c16c5707
FJ
609 nv_encoder = nouveau_connector_of_detect(connector);
610 if (nv_encoder) {
611 nouveau_connector_set_encoder(connector, nv_encoder);
5addcf0a
DA
612 conn_status = connector_status_connected;
613 goto out;
c16c5707
FJ
614 }
615
0ed3165e 616detect_analog:
cb75d97e 617 nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
f4053509 618 if (!nv_encoder && !nouveau_tv_disable)
cb75d97e 619 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
84b8081c 620 if (nv_encoder && force) {
6ee73861 621 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
d58ded76 622 const struct drm_encoder_helper_funcs *helper =
6ee73861
BS
623 encoder->helper_private;
624
625 if (helper->detect(encoder, connector) ==
626 connector_status_connected) {
627 nouveau_connector_set_encoder(connector, nv_encoder);
5addcf0a
DA
628 conn_status = connector_status_connected;
629 goto out;
6ee73861
BS
630 }
631
632 }
633
5addcf0a
DA
634 out:
635
6833fb1e
LP
636 pm_runtime_mark_last_busy(dev->dev);
637 pm_runtime_put_autosuspend(dev->dev);
5addcf0a
DA
638
639 return conn_status;
6ee73861
BS
640}
641
d17f395c 642static enum drm_connector_status
930a9e28 643nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
d17f395c
BS
644{
645 struct drm_device *dev = connector->dev;
77145f1c 646 struct nouveau_drm *drm = nouveau_drm(dev);
d17f395c
BS
647 struct nouveau_connector *nv_connector = nouveau_connector(connector);
648 struct nouveau_encoder *nv_encoder = NULL;
649 enum drm_connector_status status = connector_status_disconnected;
650
651 /* Cleanup the previous EDID block. */
652 if (nv_connector->edid) {
c555f023 653 drm_connector_update_edid_property(connector, NULL);
d17f395c
BS
654 kfree(nv_connector->edid);
655 nv_connector->edid = NULL;
656 }
657
cb75d97e 658 nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
d17f395c
BS
659 if (!nv_encoder)
660 return connector_status_disconnected;
661
a6ed76d7 662 /* Try retrieving EDID via DDC */
77145f1c 663 if (!drm->vbios.fp_no_ddc) {
930a9e28 664 status = nouveau_connector_detect(connector, force);
d17f395c
BS
665 if (status == connector_status_connected)
666 goto out;
667 }
668
a6ed76d7
BS
669 /* On some laptops (Sony, i'm looking at you) there appears to
670 * be no direct way of accessing the panel's EDID. The only
671 * option available to us appears to be to ask ACPI for help..
672 *
673 * It's important this check's before trying straps, one of the
674 * said manufacturer's laptops are configured in such a way
675 * the nouveau decides an entry in the VBIOS FP mode table is
676 * valid - it's not (rh#613284)
677 */
678 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
df285500 679 if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
a6ed76d7
BS
680 status = connector_status_connected;
681 goto out;
682 }
683 }
684
d17f395c
BS
685 /* If no EDID found above, and the VBIOS indicates a hardcoded
686 * modeline is avalilable for the panel, set it as the panel's
687 * native mode and exit.
688 */
77145f1c 689 if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
d17f395c
BS
690 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
691 status = connector_status_connected;
692 goto out;
693 }
694
695 /* Still nothing, some VBIOS images have a hardcoded EDID block
696 * stored for the panel stored in them.
697 */
77145f1c 698 if (!drm->vbios.fp_no_ddc) {
d17f395c
BS
699 struct edid *edid =
700 (struct edid *)nouveau_bios_embedded_edid(dev);
701 if (edid) {
a441dbb1
MS
702 nv_connector->edid =
703 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
704 if (nv_connector->edid)
705 status = connector_status_connected;
d17f395c
BS
706 }
707 }
708
709out:
710#if defined(CONFIG_ACPI_BUTTON) || \
711 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
712 if (status == connector_status_connected &&
713 !nouveau_ignorelid && !acpi_lid_open())
714 status = connector_status_unknown;
715#endif
716
c555f023 717 drm_connector_update_edid_property(connector, nv_connector->edid);
3195c5f9 718 nouveau_connector_set_encoder(connector, nv_encoder);
d17f395c
BS
719 return status;
720}
721
6ee73861
BS
722static void
723nouveau_connector_force(struct drm_connector *connector)
724{
77145f1c 725 struct nouveau_drm *drm = nouveau_drm(connector->dev);
be079e97 726 struct nouveau_connector *nv_connector = nouveau_connector(connector);
6ee73861
BS
727 struct nouveau_encoder *nv_encoder;
728 int type;
729
befb51e9 730 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
6ee73861 731 if (connector->force == DRM_FORCE_ON_DIGITAL)
cb75d97e 732 type = DCB_OUTPUT_TMDS;
6ee73861 733 else
cb75d97e 734 type = DCB_OUTPUT_ANALOG;
6ee73861 735 } else
cb75d97e 736 type = DCB_OUTPUT_ANY;
6ee73861 737
e19b20bb 738 nv_encoder = find_encoder(connector, type);
6ee73861 739 if (!nv_encoder) {
77145f1c 740 NV_ERROR(drm, "can't find encoder to force %s on!\n",
8c6c361a 741 connector->name);
6ee73861
BS
742 connector->status = connector_status_disconnected;
743 return;
744 }
745
746 nouveau_connector_set_encoder(connector, nv_encoder);
747}
748
749static int
750nouveau_connector_set_property(struct drm_connector *connector,
751 struct drm_property *property, uint64_t value)
752{
616915ec 753 struct nouveau_conn_atom *asyc = nouveau_conn_atom(connector->state);
6ee73861
BS
754 struct nouveau_connector *nv_connector = nouveau_connector(connector);
755 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
4a9f822f 756 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
6ee73861
BS
757 int ret;
758
616915ec
BS
759 ret = connector->funcs->atomic_set_property(&nv_connector->base,
760 &asyc->state,
761 property, value);
762 if (ret) {
763 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
764 return get_slave_funcs(encoder)->set_property(
765 encoder, connector, property, value);
766 return ret;
b29caa58
BS
767 }
768
616915ec 769 nv_connector->scaling_mode = asyc->scaler.mode;
616915ec 770 nv_connector->dithering_mode = asyc->dither.mode;
b29caa58 771
c2d926aa
BS
772 if (connector->encoder && connector->encoder->crtc) {
773 ret = drm_crtc_helper_set_mode(connector->encoder->crtc,
774 &connector->encoder->crtc->mode,
775 connector->encoder->crtc->x,
776 connector->encoder->crtc->y,
777 NULL);
778 if (!ret)
779 return -EINVAL;
780 }
6ee73861 781
616915ec 782 return 0;
6ee73861
BS
783}
784
785struct moderec {
786 int hdisplay;
787 int vdisplay;
788};
789
790static struct moderec scaler_modes[] = {
791 { 1920, 1200 },
792 { 1920, 1080 },
793 { 1680, 1050 },
794 { 1600, 1200 },
795 { 1400, 1050 },
796 { 1280, 1024 },
797 { 1280, 960 },
798 { 1152, 864 },
799 { 1024, 768 },
800 { 800, 600 },
801 { 720, 400 },
802 { 640, 480 },
803 { 640, 400 },
804 { 640, 350 },
805 {}
806};
807
808static int
809nouveau_connector_scaler_modes_add(struct drm_connector *connector)
810{
811 struct nouveau_connector *nv_connector = nouveau_connector(connector);
812 struct drm_display_mode *native = nv_connector->native_mode, *m;
813 struct drm_device *dev = connector->dev;
814 struct moderec *mode = &scaler_modes[0];
815 int modes = 0;
816
817 if (!native)
818 return 0;
819
820 while (mode->hdisplay) {
821 if (mode->hdisplay <= native->hdisplay &&
f0d15402
BS
822 mode->vdisplay <= native->vdisplay &&
823 (mode->hdisplay != native->hdisplay ||
824 mode->vdisplay != native->vdisplay)) {
6ee73861
BS
825 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
826 drm_mode_vrefresh(native), false,
827 false, false);
828 if (!m)
829 continue;
830
6ee73861
BS
831 drm_mode_probed_add(connector, m);
832 modes++;
833 }
834
835 mode++;
836 }
837
838 return modes;
839}
840
63221755
BS
841static void
842nouveau_connector_detect_depth(struct drm_connector *connector)
843{
77145f1c 844 struct nouveau_drm *drm = nouveau_drm(connector->dev);
63221755
BS
845 struct nouveau_connector *nv_connector = nouveau_connector(connector);
846 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
77145f1c 847 struct nvbios *bios = &drm->vbios;
63221755
BS
848 struct drm_display_mode *mode = nv_connector->native_mode;
849 bool duallink;
850
851 /* if the edid is feeling nice enough to provide this info, use it */
852 if (nv_connector->edid && connector->display_info.bpc)
853 return;
854
a6a17859
BS
855 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
856 if (nv_connector->type == DCB_CONNECTOR_eDP) {
857 connector->display_info.bpc = 6;
858 return;
859 }
860
861 /* we're out of options unless we're LVDS, default to 8bpc */
cb75d97e 862 if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
c8435362 863 connector->display_info.bpc = 8;
63221755 864 return;
c8435362
BS
865 }
866
867 connector->display_info.bpc = 6;
63221755
BS
868
869 /* LVDS: panel straps */
870 if (bios->fp_no_ddc) {
871 if (bios->fp.if_is_24bit)
872 connector->display_info.bpc = 8;
873 return;
874 }
875
876 /* LVDS: DDC panel, need to first determine the number of links to
877 * know which if_is_24bit flag to check...
878 */
879 if (nv_connector->edid &&
befb51e9 880 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
63221755
BS
881 duallink = ((u8 *)nv_connector->edid)[121] == 2;
882 else
883 duallink = mode->clock >= bios->fp.duallink_transition_clk;
884
885 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
886 ( duallink && (bios->fp.strapless_is_24bit & 2)))
887 connector->display_info.bpc = 8;
888}
889
6d757753
LP
890static int
891nouveau_connector_late_register(struct drm_connector *connector)
892{
893 int ret;
894
895 ret = nouveau_backlight_init(connector);
896
897 return ret;
898}
899
900static void
901nouveau_connector_early_unregister(struct drm_connector *connector)
902{
a4e05f41 903 nouveau_backlight_fini(connector);
6d757753
LP
904}
905
6ee73861
BS
906static int
907nouveau_connector_get_modes(struct drm_connector *connector)
908{
909 struct drm_device *dev = connector->dev;
77145f1c 910 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861
BS
911 struct nouveau_connector *nv_connector = nouveau_connector(connector);
912 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
4a9f822f 913 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
6ee73861
BS
914 int ret = 0;
915
d17f395c 916 /* destroy the native mode, the attached monitor could have changed.
6ee73861 917 */
d17f395c 918 if (nv_connector->native_mode) {
6ee73861
BS
919 drm_mode_destroy(dev, nv_connector->native_mode);
920 nv_connector->native_mode = NULL;
921 }
922
923 if (nv_connector->edid)
924 ret = drm_add_edid_modes(connector, nv_connector->edid);
d17f395c 925 else
cb75d97e 926 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
d17f395c 927 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
77145f1c 928 drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
80dad869
BS
929 struct drm_display_mode mode;
930
931 nouveau_bios_fp_mode(dev, &mode);
932 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
d17f395c 933 }
6ee73861 934
d4c2c99b
BS
935 /* Determine display colour depth for everything except LVDS now,
936 * DP requires this before mode_valid() is called.
937 */
938 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
939 nouveau_connector_detect_depth(connector);
940
6ee73861
BS
941 /* Find the native mode if this is a digital panel, if we didn't
942 * find any modes through DDC previously add the native mode to
943 * the list of modes.
944 */
945 if (!nv_connector->native_mode)
616915ec 946 nv_connector->native_mode = nouveau_conn_native_mode(connector);
6ee73861
BS
947 if (ret == 0 && nv_connector->native_mode) {
948 struct drm_display_mode *mode;
949
950 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
951 drm_mode_probed_add(connector, mode);
952 ret = 1;
953 }
954
d4c2c99b
BS
955 /* Determine LVDS colour depth, must happen after determining
956 * "native" mode as some VBIOS tables require us to use the
957 * pixel clock as part of the lookup...
63221755 958 */
d4c2c99b
BS
959 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
960 nouveau_connector_detect_depth(connector);
63221755 961
cb75d97e 962 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
4a9f822f 963 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
6ee73861 964
befb51e9
BS
965 if (nv_connector->type == DCB_CONNECTOR_LVDS ||
966 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
967 nv_connector->type == DCB_CONNECTOR_eDP)
6ee73861
BS
968 ret += nouveau_connector_scaler_modes_add(connector);
969
970 return ret;
971}
972
1f5bd443 973static unsigned
9340d77f 974get_tmds_link_bandwidth(struct drm_connector *connector)
1f5bd443
FJ
975{
976 struct nouveau_connector *nv_connector = nouveau_connector(connector);
9340d77f 977 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
77145f1c 978 struct nouveau_drm *drm = nouveau_drm(connector->dev);
cb75d97e 979 struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
9340d77f 980 struct drm_display_info *info = NULL;
d1084184 981 unsigned duallink_scale =
9340d77f
IM
982 nouveau_duallink && nv_encoder->dcb->duallink_possible ? 2 : 1;
983
d1084184 984 if (drm_detect_hdmi_monitor(nv_connector->edid)) {
9340d77f 985 info = &nv_connector->base.display_info;
d1084184
BS
986 duallink_scale = 1;
987 }
1f5bd443 988
9340d77f 989 if (info) {
1a0c96c0
IM
990 if (nouveau_hdmimhz > 0)
991 return nouveau_hdmimhz * 1000;
992 /* Note: these limits are conservative, some Fermi's
993 * can do 297 MHz. Unclear how this can be determined.
994 */
9340d77f
IM
995 if (drm->client.device.info.chipset >= 0x120) {
996 const int max_tmds_clock =
997 info->hdmi.scdc.scrambling.supported ?
998 594000 : 340000;
999 return info->max_tmds_clock ?
1000 min(info->max_tmds_clock, max_tmds_clock) :
1001 max_tmds_clock;
1002 }
1167c6bc 1003 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KEPLER)
1a0c96c0 1004 return 297000;
1167c6bc 1005 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
1a0c96c0
IM
1006 return 225000;
1007 }
d1084184 1008
1f5bd443 1009 if (dcb->location != DCB_LOC_ON_CHIP ||
1167c6bc 1010 drm->client.device.info.chipset >= 0x46)
9340d77f 1011 return 165000 * duallink_scale;
1167c6bc 1012 else if (drm->client.device.info.chipset >= 0x40)
9340d77f 1013 return 155000 * duallink_scale;
1167c6bc 1014 else if (drm->client.device.info.chipset >= 0x18)
9340d77f 1015 return 135000 * duallink_scale;
1f5bd443 1016 else
9340d77f 1017 return 112000 * duallink_scale;
1f5bd443
FJ
1018}
1019
54b202f1 1020static enum drm_mode_status
6ee73861
BS
1021nouveau_connector_mode_valid(struct drm_connector *connector,
1022 struct drm_display_mode *mode)
1023{
6ee73861
BS
1024 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1025 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
4a9f822f 1026 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
6ee73861
BS
1027 unsigned min_clock = 25000, max_clock = min_clock;
1028 unsigned clock = mode->clock;
1029
1030 switch (nv_encoder->dcb->type) {
cb75d97e 1031 case DCB_OUTPUT_LVDS:
26099a74
BS
1032 if (nv_connector->native_mode &&
1033 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
1034 mode->vdisplay > nv_connector->native_mode->vdisplay))
6ee73861
BS
1035 return MODE_PANEL;
1036
1037 min_clock = 0;
1038 max_clock = 400000;
1039 break;
cb75d97e 1040 case DCB_OUTPUT_TMDS:
9340d77f 1041 max_clock = get_tmds_link_bandwidth(connector);
6ee73861 1042 break;
cb75d97e 1043 case DCB_OUTPUT_ANALOG:
6ee73861
BS
1044 max_clock = nv_encoder->dcb->crtconf.maxfreq;
1045 if (!max_clock)
1046 max_clock = 350000;
1047 break;
cb75d97e 1048 case DCB_OUTPUT_TV:
4a9f822f 1049 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
cb75d97e 1050 case DCB_OUTPUT_DP:
75a1fccf
BS
1051 max_clock = nv_encoder->dp.link_nr;
1052 max_clock *= nv_encoder->dp.link_bw;
d4c2c99b 1053 clock = clock * (connector->display_info.bpc * 3) / 10;
6ee73861 1054 break;
e7cc51c5 1055 default:
af7db03e 1056 BUG();
e7cc51c5 1057 return MODE_BAD;
6ee73861
BS
1058 }
1059
37aa2243
AB
1060 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1061 clock *= 2;
1062
6ee73861
BS
1063 if (clock < min_clock)
1064 return MODE_CLOCK_LOW;
1065
1066 if (clock > max_clock)
1067 return MODE_CLOCK_HIGH;
1068
1069 return MODE_OK;
1070}
1071
1072static struct drm_encoder *
1073nouveau_connector_best_encoder(struct drm_connector *connector)
1074{
1075 struct nouveau_connector *nv_connector = nouveau_connector(connector);
1076
1077 if (nv_connector->detected_encoder)
1078 return to_drm_encoder(nv_connector->detected_encoder);
1079
1080 return NULL;
1081}
1082
1083static const struct drm_connector_helper_funcs
1084nouveau_connector_helper_funcs = {
1085 .get_modes = nouveau_connector_get_modes,
1086 .mode_valid = nouveau_connector_mode_valid,
1087 .best_encoder = nouveau_connector_best_encoder,
1088};
1089
1090static const struct drm_connector_funcs
1091nouveau_connector_funcs = {
7d902c05 1092 .dpms = drm_helper_connector_dpms,
616915ec 1093 .reset = nouveau_conn_reset,
6ee73861 1094 .detect = nouveau_connector_detect,
616915ec 1095 .force = nouveau_connector_force,
6ee73861
BS
1096 .fill_modes = drm_helper_probe_single_connector_modes,
1097 .set_property = nouveau_connector_set_property,
616915ec
BS
1098 .destroy = nouveau_connector_destroy,
1099 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1100 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1101 .atomic_set_property = nouveau_conn_atomic_set_property,
1102 .atomic_get_property = nouveau_conn_atomic_get_property,
6d757753
LP
1103 .late_register = nouveau_connector_late_register,
1104 .early_unregister = nouveau_connector_early_unregister,
6ee73861
BS
1105};
1106
d17f395c
BS
1107static const struct drm_connector_funcs
1108nouveau_connector_funcs_lvds = {
7d902c05 1109 .dpms = drm_helper_connector_dpms,
616915ec 1110 .reset = nouveau_conn_reset,
d17f395c 1111 .detect = nouveau_connector_detect_lvds,
616915ec 1112 .force = nouveau_connector_force,
d17f395c
BS
1113 .fill_modes = drm_helper_probe_single_connector_modes,
1114 .set_property = nouveau_connector_set_property,
616915ec
BS
1115 .destroy = nouveau_connector_destroy,
1116 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1117 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1118 .atomic_set_property = nouveau_conn_atomic_set_property,
1119 .atomic_get_property = nouveau_conn_atomic_get_property,
6d757753
LP
1120 .late_register = nouveau_connector_late_register,
1121 .early_unregister = nouveau_connector_early_unregister,
d17f395c 1122};
6ee73861 1123
79ca2770 1124static int
80bc340b 1125nouveau_connector_hotplug(struct nvif_notify *notify)
4f47643d
BS
1126{
1127 struct nouveau_connector *nv_connector =
79ca2770 1128 container_of(notify, typeof(*nv_connector), hpd);
4f47643d 1129 struct drm_connector *connector = &nv_connector->base;
456b0579 1130 struct nouveau_drm *drm = nouveau_drm(connector->dev);
79ca2770 1131 const struct nvif_notify_conn_rep_v0 *rep = notify->data;
456b0579 1132 const char *name = connector->name;
f479c0ba 1133 struct nouveau_encoder *nv_encoder;
3e1a1275
LP
1134 int ret;
1135
1136 ret = pm_runtime_get(drm->dev->dev);
1137 if (ret == 0) {
1138 /* We can't block here if there's a pending PM request
1139 * running, as we'll deadlock nouveau_display_fini() when it
1140 * calls nvif_put() on our nvif_notify struct. So, simply
1141 * defer the hotplug event until the device finishes resuming
1142 */
1143 NV_DEBUG(drm, "Deferring HPD on %s until runtime resume\n",
1144 name);
1145 schedule_work(&drm->hpd_work);
1146
1147 pm_runtime_put_noidle(drm->dev->dev);
1148 return NVIF_NOTIFY_KEEP;
1149 } else if (ret != 1 && ret != -EACCES) {
1150 NV_WARN(drm, "HPD on %s dropped due to RPM failure: %d\n",
1151 name, ret);
1152 return NVIF_NOTIFY_DROP;
1153 }
4f47643d 1154
79ca2770 1155 if (rep->mask & NVIF_NOTIFY_CONN_V0_IRQ) {
f479c0ba 1156 NV_DEBUG(drm, "service %s\n", name);
46094b2b 1157 drm_dp_cec_irq(&nv_connector->aux);
f479c0ba
BS
1158 if ((nv_encoder = find_encoder(connector, DCB_OUTPUT_DP)))
1159 nv50_mstm_service(nv_encoder->dp.mstm);
456b0579 1160 } else {
79ca2770 1161 bool plugged = (rep->mask != NVIF_NOTIFY_CONN_V0_UNPLUG);
4f47643d 1162
46094b2b
HV
1163 if (!plugged)
1164 drm_dp_cec_unset_edid(&nv_connector->aux);
456b0579 1165 NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un", name);
f479c0ba
BS
1166 if ((nv_encoder = find_encoder(connector, DCB_OUTPUT_DP))) {
1167 if (!plugged)
1168 nv50_mstm_remove(nv_encoder->dp.mstm);
1169 }
1170
456b0579
BS
1171 drm_helper_hpd_irq_event(connector->dev);
1172 }
1173
3e1a1275
LP
1174 pm_runtime_mark_last_busy(drm->dev->dev);
1175 pm_runtime_put_autosuspend(drm->dev->dev);
80bc340b 1176 return NVIF_NOTIFY_KEEP;
4f47643d
BS
1177}
1178
8894f491 1179static ssize_t
2aa5eac5 1180nouveau_connector_aux_xfer(struct drm_dp_aux *obj, struct drm_dp_aux_msg *msg)
8894f491
BS
1181{
1182 struct nouveau_connector *nv_connector =
2aa5eac5 1183 container_of(obj, typeof(*nv_connector), aux);
8894f491 1184 struct nouveau_encoder *nv_encoder;
2aa5eac5 1185 struct nvkm_i2c_aux *aux;
1af5c410 1186 u8 size = msg->size;
8894f491
BS
1187 int ret;
1188
1189 nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
2aa5eac5 1190 if (!nv_encoder || !(aux = nv_encoder->aux))
8894f491
BS
1191 return -ENODEV;
1192 if (WARN_ON(msg->size > 16))
1193 return -E2BIG;
8894f491 1194
2aa5eac5 1195 ret = nvkm_i2c_aux_acquire(aux);
8894f491
BS
1196 if (ret)
1197 return ret;
1198
2aa5eac5 1199 ret = nvkm_i2c_aux_xfer(aux, false, msg->request, msg->address,
1af5c410 1200 msg->buffer, &size);
2aa5eac5 1201 nvkm_i2c_aux_release(aux);
8894f491
BS
1202 if (ret >= 0) {
1203 msg->reply = ret;
1af5c410 1204 return size;
8894f491
BS
1205 }
1206
1207 return ret;
1208}
1209
befb51e9
BS
1210static int
1211drm_conntype_from_dcb(enum dcb_connector_type dcb)
1212{
1213 switch (dcb) {
1214 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
1215 case DCB_CONNECTOR_TV_0 :
1216 case DCB_CONNECTOR_TV_1 :
1217 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
fa2c113a
BS
1218 case DCB_CONNECTOR_DMS59_0 :
1219 case DCB_CONNECTOR_DMS59_1 :
befb51e9
BS
1220 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
1221 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
1222 case DCB_CONNECTOR_LVDS :
1223 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
4abb410a
BS
1224 case DCB_CONNECTOR_DMS59_DP0:
1225 case DCB_CONNECTOR_DMS59_DP1:
7919faab
BS
1226 case DCB_CONNECTOR_DP :
1227 case DCB_CONNECTOR_USB_C : return DRM_MODE_CONNECTOR_DisplayPort;
befb51e9
BS
1228 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
1229 case DCB_CONNECTOR_HDMI_0 :
6bd9293e
BS
1230 case DCB_CONNECTOR_HDMI_1 :
1231 case DCB_CONNECTOR_HDMI_C : return DRM_MODE_CONNECTOR_HDMIA;
df00d5da 1232 case DCB_CONNECTOR_WFD : return DRM_MODE_CONNECTOR_VIRTUAL;
befb51e9
BS
1233 default:
1234 break;
1235 }
1236
1237 return DRM_MODE_CONNECTOR_Unknown;
1238}
1239
8f1a6086 1240struct drm_connector *
3c7fc252
LP
1241nouveau_connector_create(struct drm_device *dev,
1242 const struct dcb_output *dcbe)
6ee73861 1243{
d17f395c 1244 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
77145f1c 1245 struct nouveau_drm *drm = nouveau_drm(dev);
77145f1c 1246 struct nouveau_display *disp = nouveau_display(dev);
6ee73861
BS
1247 struct nouveau_connector *nv_connector = NULL;
1248 struct drm_connector *connector;
22b76bbe 1249 struct drm_connector_list_iter conn_iter;
3c7fc252
LP
1250 char aux_name[48] = {0};
1251 int index = dcbe->connector;
2fa67f12 1252 int type, ret = 0;
befb51e9 1253 bool dummy;
6ee73861 1254
22b76bbe 1255 drm_connector_list_iter_begin(dev, &conn_iter);
37afe55b 1256 nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
befb51e9 1257 nv_connector = nouveau_connector(connector);
22b76bbe
LP
1258 if (nv_connector->index == index) {
1259 drm_connector_list_iter_end(&conn_iter);
befb51e9 1260 return connector;
22b76bbe 1261 }
6ee73861 1262 }
22b76bbe 1263 drm_connector_list_iter_end(&conn_iter);
6ee73861 1264
7f612d87
BS
1265 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
1266 if (!nv_connector)
8f1a6086 1267 return ERR_PTR(-ENOMEM);
befb51e9 1268
7f612d87 1269 connector = &nv_connector->base;
befb51e9
BS
1270 nv_connector->index = index;
1271
1272 /* attempt to parse vbios connector type and hotplug gpio */
cb75d97e 1273 nv_connector->dcb = olddcb_conn(dev, index);
befb51e9 1274 if (nv_connector->dcb) {
befb51e9 1275 u32 entry = ROM16(nv_connector->dcb[0]);
cb75d97e 1276 if (olddcb_conntab(dev)[3] >= 4)
befb51e9
BS
1277 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
1278
befb51e9
BS
1279 nv_connector->type = nv_connector->dcb[0];
1280 if (drm_conntype_from_dcb(nv_connector->type) ==
1281 DRM_MODE_CONNECTOR_Unknown) {
77145f1c 1282 NV_WARN(drm, "unknown connector type %02x\n",
befb51e9
BS
1283 nv_connector->type);
1284 nv_connector->type = DCB_CONNECTOR_NONE;
1285 }
7f612d87 1286
befb51e9
BS
1287 /* Gigabyte NX85T */
1288 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
1289 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1290 nv_connector->type = DCB_CONNECTOR_DVI_I;
1291 }
6ee73861 1292
befb51e9
BS
1293 /* Gigabyte GV-NX86T512H */
1294 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
1295 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1296 nv_connector->type = DCB_CONNECTOR_DVI_I;
1297 }
1298 } else {
1299 nv_connector->type = DCB_CONNECTOR_NONE;
befb51e9
BS
1300 }
1301
1302 /* no vbios data, or an unknown dcb connector type - attempt to
1303 * figure out something suitable ourselves
1304 */
1305 if (nv_connector->type == DCB_CONNECTOR_NONE) {
77145f1c
BS
1306 struct nouveau_drm *drm = nouveau_drm(dev);
1307 struct dcb_table *dcbt = &drm->vbios.dcb;
befb51e9
BS
1308 u32 encoders = 0;
1309 int i;
1310
1311 for (i = 0; i < dcbt->entries; i++) {
1312 if (dcbt->entry[i].connector == nv_connector->index)
1313 encoders |= (1 << dcbt->entry[i].type);
1314 }
6ee73861 1315
cb75d97e
BS
1316 if (encoders & (1 << DCB_OUTPUT_DP)) {
1317 if (encoders & (1 << DCB_OUTPUT_TMDS))
befb51e9
BS
1318 nv_connector->type = DCB_CONNECTOR_DP;
1319 else
1320 nv_connector->type = DCB_CONNECTOR_eDP;
1321 } else
cb75d97e
BS
1322 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
1323 if (encoders & (1 << DCB_OUTPUT_ANALOG))
befb51e9
BS
1324 nv_connector->type = DCB_CONNECTOR_DVI_I;
1325 else
1326 nv_connector->type = DCB_CONNECTOR_DVI_D;
1327 } else
cb75d97e 1328 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
befb51e9
BS
1329 nv_connector->type = DCB_CONNECTOR_VGA;
1330 } else
cb75d97e 1331 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
befb51e9
BS
1332 nv_connector->type = DCB_CONNECTOR_LVDS;
1333 } else
cb75d97e 1334 if (encoders & (1 << DCB_OUTPUT_TV)) {
befb51e9
BS
1335 nv_connector->type = DCB_CONNECTOR_TV_0;
1336 }
1337 }
2fa67f12 1338
8894f491
BS
1339 switch ((type = drm_conntype_from_dcb(nv_connector->type))) {
1340 case DRM_MODE_CONNECTOR_LVDS:
befb51e9 1341 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
2fa67f12 1342 if (ret) {
77145f1c 1343 NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
befb51e9
BS
1344 kfree(nv_connector);
1345 return ERR_PTR(ret);
2fa67f12 1346 }
befb51e9
BS
1347
1348 funcs = &nouveau_connector_funcs_lvds;
8894f491
BS
1349 break;
1350 case DRM_MODE_CONNECTOR_DisplayPort:
1351 case DRM_MODE_CONNECTOR_eDP:
1352 nv_connector->aux.dev = dev->dev;
1353 nv_connector->aux.transfer = nouveau_connector_aux_xfer;
3c7fc252
LP
1354 snprintf(aux_name, sizeof(aux_name), "sor-%04x-%04x",
1355 dcbe->hasht, dcbe->hashm);
1356 nv_connector->aux.name = kstrdup(aux_name, GFP_KERNEL);
8894f491
BS
1357 ret = drm_dp_aux_register(&nv_connector->aux);
1358 if (ret) {
1359 NV_ERROR(drm, "failed to register aux channel\n");
1360 kfree(nv_connector);
1361 return ERR_PTR(ret);
1362 }
8896ceef 1363 funcs = &nouveau_connector_funcs;
8894f491
BS
1364 break;
1365 default:
1366 funcs = &nouveau_connector_funcs;
1367 break;
7f612d87
BS
1368 }
1369
0f18d276 1370 /* HDMI 3D support */
0d4a2c57 1371 if ((disp->disp.object.oclass >= G82_DISP)
0f18d276
AB
1372 && ((type == DRM_MODE_CONNECTOR_DisplayPort)
1373 || (type == DRM_MODE_CONNECTOR_eDP)
1374 || (type == DRM_MODE_CONNECTOR_HDMIA)))
1375 connector->stereo_allowed = true;
1376
befb51e9
BS
1377 /* defaults, will get overridden in detect() */
1378 connector->interlace_allowed = false;
1379 connector->doublescan_allowed = false;
1380
1381 drm_connector_init(dev, connector, funcs, type);
1382 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1383
56182b8b
BS
1384 connector->funcs->reset(connector);
1385 nouveau_conn_attach_properties(connector);
df26bc9c 1386
56182b8b 1387 /* Default scaling mode */
befb51e9 1388 switch (nv_connector->type) {
0ea5fe8a
BS
1389 case DCB_CONNECTOR_LVDS:
1390 case DCB_CONNECTOR_LVDS_SPWG:
1391 case DCB_CONNECTOR_eDP:
1392 /* see note in nouveau_connector_set_property() */
0d4a2c57 1393 if (disp->disp.object.oclass < NV50_DISP) {
0ea5fe8a
BS
1394 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1395 break;
1396 }
be079e97
BS
1397 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1398 break;
1399 default:
0ea5fe8a 1400 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
7d95216e
BS
1401 break;
1402 }
be079e97 1403
7d95216e
BS
1404 /* dithering properties */
1405 switch (nv_connector->type) {
1406 case DCB_CONNECTOR_TV_0:
1407 case DCB_CONNECTOR_TV_1:
1408 case DCB_CONNECTOR_TV_3:
1409 case DCB_CONNECTOR_VGA:
1410 break;
1411 default:
56182b8b 1412 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
be079e97 1413 break;
6ee73861
BS
1414 }
1415
46094b2b
HV
1416 switch (type) {
1417 case DRM_MODE_CONNECTOR_DisplayPort:
1418 case DRM_MODE_CONNECTOR_eDP:
1419 drm_dp_cec_register_connector(&nv_connector->aux,
1420 connector->name, dev->dev);
1421 break;
1422 }
1423
0d4a2c57
BS
1424 ret = nvif_notify_init(&disp->disp.object, nouveau_connector_hotplug,
1425 true, NV04_DISP_NTFY_CONN,
79ca2770
BS
1426 &(struct nvif_notify_conn_req_v0) {
1427 .mask = NVIF_NOTIFY_CONN_V0_ANY,
1428 .conn = index,
1429 },
1430 sizeof(struct nvif_notify_conn_req_v0),
1431 sizeof(struct nvif_notify_conn_rep_v0),
1432 &nv_connector->hpd);
456b0579
BS
1433 if (ret)
1434 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1435 else
4f47643d 1436 connector->polled = DRM_CONNECTOR_POLL_HPD;
fce2bad0 1437
34ea3d38 1438 drm_connector_register(connector);
befb51e9 1439 return connector;
6ee73861 1440}