1 /* $NetBSD: nouveau_connector.c,v 1.3 2016/04/22 20:17:59 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2008 Maarten Maathuis.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nouveau_connector.c,v 1.3 2016/04/22 20:17:59 riastradh Exp $");
31
32 #include <acpi/button.h>
33
34 #include <linux/err.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/string.h>
37
38 #include <drm/drmP.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_crtc_helper.h>
41
42 #include "nouveau_reg.h"
43 #include "nouveau_drm.h"
44 #include "dispnv04/hw.h"
45 #include "nouveau_acpi.h"
46
47 #include "nouveau_display.h"
48 #include "nouveau_connector.h"
49 #include "nouveau_encoder.h"
50 #include "nouveau_crtc.h"
51
52 #include <subdev/i2c.h>
53 #include <subdev/gpio.h>
54
55 MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
56 static int nouveau_tv_disable = 0;
57 module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
58
59 #if defined(CONFIG_ACPI_BUTTON) || \
60 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
61 MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
62 static int nouveau_ignorelid = 0;
63 module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
64 #endif
65
66 MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (default: enabled)");
67 static int nouveau_duallink = 1;
68 module_param_named(duallink, nouveau_duallink, int, 0400);
69
70 struct nouveau_encoder *
find_encoder(struct drm_connector * connector,int type)71 find_encoder(struct drm_connector *connector, int type)
72 {
73 struct drm_device *dev = connector->dev;
74 struct nouveau_encoder *nv_encoder;
75 struct drm_mode_object *obj;
76 int i, id;
77
78 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
79 id = connector->encoder_ids[i];
80 if (!id)
81 break;
82
83 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
84 if (!obj)
85 continue;
86 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
87
88 if (type == DCB_OUTPUT_ANY || nv_encoder->dcb->type == type)
89 return nv_encoder;
90 }
91
92 return NULL;
93 }
94
95 struct nouveau_connector *
nouveau_encoder_connector_get(struct nouveau_encoder * encoder)96 nouveau_encoder_connector_get(struct nouveau_encoder *encoder)
97 {
98 struct drm_device *dev = to_drm_encoder(encoder)->dev;
99 struct drm_connector *drm_connector;
100
101 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head) {
102 if (drm_connector->encoder == to_drm_encoder(encoder))
103 return nouveau_connector(drm_connector);
104 }
105
106 return NULL;
107 }
108
109 static void
nouveau_connector_destroy(struct drm_connector * connector)110 nouveau_connector_destroy(struct drm_connector *connector)
111 {
112 struct nouveau_connector *nv_connector = nouveau_connector(connector);
113 nouveau_event_ref(NULL, &nv_connector->hpd_func);
114 kfree(nv_connector->edid);
115 drm_sysfs_connector_remove(connector);
116 drm_connector_cleanup(connector);
117 kfree(connector);
118 }
119
120 static struct nouveau_i2c_port *
nouveau_connector_ddc_detect(struct drm_connector * connector,struct nouveau_encoder ** pnv_encoder)121 nouveau_connector_ddc_detect(struct drm_connector *connector,
122 struct nouveau_encoder **pnv_encoder)
123 {
124 struct drm_device *dev = connector->dev;
125 struct nouveau_connector *nv_connector = nouveau_connector(connector);
126 struct nouveau_drm *drm = nouveau_drm(dev);
127 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
128 struct nouveau_i2c_port *port = NULL;
129 int i, panel = -ENODEV;
130
131 /* eDP panels need powering on by us (if the VBIOS doesn't default it
132 * to on) before doing any AUX channel transactions. LVDS panel power
133 * is handled by the SOR itself, and not required for LVDS DDC.
134 */
135 if (nv_connector->type == DCB_CONNECTOR_eDP) {
136 panel = gpio->get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
137 if (panel == 0) {
138 gpio->set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
139 msleep(300);
140 }
141 }
142
143 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
144 struct nouveau_encoder *nv_encoder;
145 struct drm_mode_object *obj;
146 int id;
147
148 id = connector->encoder_ids[i];
149 if (!id)
150 break;
151
152 obj = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
153 if (!obj)
154 continue;
155 nv_encoder = nouveau_encoder(obj_to_encoder(obj));
156
157 port = nv_encoder->i2c;
158 if (port && nv_probe_i2c(port, 0x50)) {
159 *pnv_encoder = nv_encoder;
160 break;
161 }
162
163 port = NULL;
164 }
165
166 /* eDP panel not detected, restore panel power GPIO to previous
167 * state to avoid confusing the SOR for other output types.
168 */
169 if (!port && panel == 0)
170 gpio->set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, panel);
171
172 return port;
173 }
174
175 static struct nouveau_encoder *
nouveau_connector_of_detect(struct drm_connector * connector)176 nouveau_connector_of_detect(struct drm_connector *connector)
177 {
178 #ifdef __powerpc__
179 struct drm_device *dev = connector->dev;
180 struct nouveau_connector *nv_connector = nouveau_connector(connector);
181 struct nouveau_encoder *nv_encoder;
182 struct device_node *cn, *dn = pci_device_to_OF_node(dev->pdev);
183
184 if (!dn ||
185 !((nv_encoder = find_encoder(connector, DCB_OUTPUT_TMDS)) ||
186 (nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG))))
187 return NULL;
188
189 for_each_child_of_node(dn, cn) {
190 const char *name = of_get_property(cn, "name", NULL);
191 const void *edid = of_get_property(cn, "EDID", NULL);
192 int idx = name ? name[strlen(name) - 1] - 'A' : 0;
193
194 if (nv_encoder->dcb->i2c_index == idx && edid) {
195 nv_connector->edid =
196 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
197 of_node_put(cn);
198 return nv_encoder;
199 }
200 }
201 #endif
202 return NULL;
203 }
204
205 static void
nouveau_connector_set_encoder(struct drm_connector * connector,struct nouveau_encoder * nv_encoder)206 nouveau_connector_set_encoder(struct drm_connector *connector,
207 struct nouveau_encoder *nv_encoder)
208 {
209 struct nouveau_connector *nv_connector = nouveau_connector(connector);
210 struct nouveau_drm *drm = nouveau_drm(connector->dev);
211 struct drm_device *dev = connector->dev;
212
213 if (nv_connector->detected_encoder == nv_encoder)
214 return;
215 nv_connector->detected_encoder = nv_encoder;
216
217 if (nv_device(drm->device)->card_type >= NV_50) {
218 connector->interlace_allowed = true;
219 connector->doublescan_allowed = true;
220 } else
221 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
222 nv_encoder->dcb->type == DCB_OUTPUT_TMDS) {
223 connector->doublescan_allowed = false;
224 connector->interlace_allowed = false;
225 } else {
226 connector->doublescan_allowed = true;
227 if (nv_device(drm->device)->card_type == NV_20 ||
228 ((nv_device(drm->device)->card_type == NV_10 ||
229 nv_device(drm->device)->card_type == NV_11) &&
230 (dev->pdev->device & 0x0ff0) != 0x0100 &&
231 (dev->pdev->device & 0x0ff0) != 0x0150))
232 /* HW is broken */
233 connector->interlace_allowed = false;
234 else
235 connector->interlace_allowed = true;
236 }
237
238 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
239 drm_object_property_set_value(&connector->base,
240 dev->mode_config.dvi_i_subconnector_property,
241 nv_encoder->dcb->type == DCB_OUTPUT_TMDS ?
242 DRM_MODE_SUBCONNECTOR_DVID :
243 DRM_MODE_SUBCONNECTOR_DVIA);
244 }
245 }
246
247 static enum drm_connector_status
nouveau_connector_detect(struct drm_connector * connector,bool force)248 nouveau_connector_detect(struct drm_connector *connector, bool force)
249 {
250 struct drm_device *dev = connector->dev;
251 struct nouveau_drm *drm = nouveau_drm(dev);
252 struct nouveau_connector *nv_connector = nouveau_connector(connector);
253 struct nouveau_encoder *nv_encoder = NULL;
254 struct nouveau_encoder *nv_partner;
255 struct nouveau_i2c_port *i2c;
256 int type;
257 int ret;
258 enum drm_connector_status conn_status = connector_status_disconnected;
259
260 /* Cleanup the previous EDID block. */
261 if (nv_connector->edid) {
262 drm_mode_connector_update_edid_property(connector, NULL);
263 kfree(nv_connector->edid);
264 nv_connector->edid = NULL;
265 }
266
267 ret = pm_runtime_get_sync(connector->dev->dev);
268 if (ret < 0 && ret != -EACCES)
269 return conn_status;
270
271 i2c = nouveau_connector_ddc_detect(connector, &nv_encoder);
272 if (i2c) {
273 nv_connector->edid = drm_get_edid(connector, &i2c->adapter);
274 drm_mode_connector_update_edid_property(connector,
275 nv_connector->edid);
276 if (!nv_connector->edid) {
277 NV_ERROR(drm, "DDC responded, but no EDID for %s\n",
278 drm_get_connector_name(connector));
279 goto detect_analog;
280 }
281
282 if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
283 !nouveau_dp_detect(to_drm_encoder(nv_encoder))) {
284 NV_ERROR(drm, "Detected %s, but failed init\n",
285 drm_get_connector_name(connector));
286 conn_status = connector_status_disconnected;
287 goto out;
288 }
289
290 /* Override encoder type for DVI-I based on whether EDID
291 * says the display is digital or analog, both use the
292 * same i2c channel so the value returned from ddc_detect
293 * isn't necessarily correct.
294 */
295 nv_partner = NULL;
296 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
297 nv_partner = find_encoder(connector, DCB_OUTPUT_ANALOG);
298 if (nv_encoder->dcb->type == DCB_OUTPUT_ANALOG)
299 nv_partner = find_encoder(connector, DCB_OUTPUT_TMDS);
300
301 if (nv_partner && ((nv_encoder->dcb->type == DCB_OUTPUT_ANALOG &&
302 nv_partner->dcb->type == DCB_OUTPUT_TMDS) ||
303 (nv_encoder->dcb->type == DCB_OUTPUT_TMDS &&
304 nv_partner->dcb->type == DCB_OUTPUT_ANALOG))) {
305 if (nv_connector->edid->input & DRM_EDID_INPUT_DIGITAL)
306 type = DCB_OUTPUT_TMDS;
307 else
308 type = DCB_OUTPUT_ANALOG;
309
310 nv_encoder = find_encoder(connector, type);
311 BUG_ON(nv_encoder == NULL);
312 }
313
314 nouveau_connector_set_encoder(connector, nv_encoder);
315 conn_status = connector_status_connected;
316 goto out;
317 }
318
319 nv_encoder = nouveau_connector_of_detect(connector);
320 if (nv_encoder) {
321 nouveau_connector_set_encoder(connector, nv_encoder);
322 conn_status = connector_status_connected;
323 goto out;
324 }
325
326 detect_analog:
327 nv_encoder = find_encoder(connector, DCB_OUTPUT_ANALOG);
328 if (!nv_encoder && !nouveau_tv_disable)
329 nv_encoder = find_encoder(connector, DCB_OUTPUT_TV);
330 if (nv_encoder && force) {
331 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
332 struct drm_encoder_helper_funcs *helper =
333 encoder->helper_private;
334
335 if (helper->detect(encoder, connector) ==
336 connector_status_connected) {
337 nouveau_connector_set_encoder(connector, nv_encoder);
338 conn_status = connector_status_connected;
339 goto out;
340 }
341
342 }
343
344 out:
345
346 pm_runtime_mark_last_busy(connector->dev->dev);
347 pm_runtime_put_autosuspend(connector->dev->dev);
348
349 return conn_status;
350 }
351
352 static enum drm_connector_status
nouveau_connector_detect_lvds(struct drm_connector * connector,bool force)353 nouveau_connector_detect_lvds(struct drm_connector *connector, bool force)
354 {
355 struct drm_device *dev = connector->dev;
356 struct nouveau_drm *drm = nouveau_drm(dev);
357 struct nouveau_connector *nv_connector = nouveau_connector(connector);
358 struct nouveau_encoder *nv_encoder = NULL;
359 enum drm_connector_status status = connector_status_disconnected;
360
361 /* Cleanup the previous EDID block. */
362 if (nv_connector->edid) {
363 drm_mode_connector_update_edid_property(connector, NULL);
364 kfree(nv_connector->edid);
365 nv_connector->edid = NULL;
366 }
367
368 nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
369 if (!nv_encoder)
370 return connector_status_disconnected;
371
372 /* Try retrieving EDID via DDC */
373 if (!drm->vbios.fp_no_ddc) {
374 status = nouveau_connector_detect(connector, force);
375 if (status == connector_status_connected)
376 goto out;
377 }
378
379 /* On some laptops (Sony, i'm looking at you) there appears to
380 * be no direct way of accessing the panel's EDID. The only
381 * option available to us appears to be to ask ACPI for help..
382 *
383 * It's important this check's before trying straps, one of the
384 * said manufacturer's laptops are configured in such a way
385 * the nouveau decides an entry in the VBIOS FP mode table is
386 * valid - it's not (rh#613284)
387 */
388 if (nv_encoder->dcb->lvdsconf.use_acpi_for_edid) {
389 if ((nv_connector->edid = nouveau_acpi_edid(dev, connector))) {
390 status = connector_status_connected;
391 goto out;
392 }
393 }
394
395 /* If no EDID found above, and the VBIOS indicates a hardcoded
396 * modeline is avalilable for the panel, set it as the panel's
397 * native mode and exit.
398 */
399 if (nouveau_bios_fp_mode(dev, NULL) && (drm->vbios.fp_no_ddc ||
400 nv_encoder->dcb->lvdsconf.use_straps_for_mode)) {
401 status = connector_status_connected;
402 goto out;
403 }
404
405 /* Still nothing, some VBIOS images have a hardcoded EDID block
406 * stored for the panel stored in them.
407 */
408 if (!drm->vbios.fp_no_ddc) {
409 struct edid *edid =
410 (struct edid *)nouveau_bios_embedded_edid(dev);
411 if (edid) {
412 nv_connector->edid =
413 kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
414 if (nv_connector->edid)
415 status = connector_status_connected;
416 }
417 }
418
419 out:
420 #if defined(CONFIG_ACPI_BUTTON) || \
421 (defined(CONFIG_ACPI_BUTTON_MODULE) && defined(MODULE))
422 if (status == connector_status_connected &&
423 !nouveau_ignorelid && !acpi_lid_open())
424 status = connector_status_unknown;
425 #endif
426
427 drm_mode_connector_update_edid_property(connector, nv_connector->edid);
428 nouveau_connector_set_encoder(connector, nv_encoder);
429 return status;
430 }
431
432 static void
nouveau_connector_force(struct drm_connector * connector)433 nouveau_connector_force(struct drm_connector *connector)
434 {
435 struct nouveau_drm *drm = nouveau_drm(connector->dev);
436 struct nouveau_connector *nv_connector = nouveau_connector(connector);
437 struct nouveau_encoder *nv_encoder;
438 int type;
439
440 if (nv_connector->type == DCB_CONNECTOR_DVI_I) {
441 if (connector->force == DRM_FORCE_ON_DIGITAL)
442 type = DCB_OUTPUT_TMDS;
443 else
444 type = DCB_OUTPUT_ANALOG;
445 } else
446 type = DCB_OUTPUT_ANY;
447
448 nv_encoder = find_encoder(connector, type);
449 if (!nv_encoder) {
450 NV_ERROR(drm, "can't find encoder to force %s on!\n",
451 drm_get_connector_name(connector));
452 connector->status = connector_status_disconnected;
453 return;
454 }
455
456 nouveau_connector_set_encoder(connector, nv_encoder);
457 }
458
459 static int
nouveau_connector_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t value)460 nouveau_connector_set_property(struct drm_connector *connector,
461 struct drm_property *property, uint64_t value)
462 {
463 struct nouveau_display *disp = nouveau_display(connector->dev);
464 struct nouveau_connector *nv_connector = nouveau_connector(connector);
465 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
466 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
467 struct drm_device *dev = connector->dev;
468 struct nouveau_crtc *nv_crtc;
469 int ret;
470
471 nv_crtc = NULL;
472 if (connector->encoder && connector->encoder->crtc)
473 nv_crtc = nouveau_crtc(connector->encoder->crtc);
474
475 /* Scaling mode */
476 if (property == dev->mode_config.scaling_mode_property) {
477 bool modeset = false;
478
479 switch (value) {
480 case DRM_MODE_SCALE_NONE:
481 case DRM_MODE_SCALE_FULLSCREEN:
482 case DRM_MODE_SCALE_CENTER:
483 case DRM_MODE_SCALE_ASPECT:
484 break;
485 default:
486 return -EINVAL;
487 }
488
489 /* LVDS always needs gpu scaling */
490 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS &&
491 value == DRM_MODE_SCALE_NONE)
492 return -EINVAL;
493
494 /* Changing between GPU and panel scaling requires a full
495 * modeset
496 */
497 if ((nv_connector->scaling_mode == DRM_MODE_SCALE_NONE) ||
498 (value == DRM_MODE_SCALE_NONE))
499 modeset = true;
500 nv_connector->scaling_mode = value;
501
502 if (!nv_crtc)
503 return 0;
504
505 if (modeset || !nv_crtc->set_scale) {
506 ret = drm_crtc_helper_set_mode(&nv_crtc->base,
507 &nv_crtc->base.mode,
508 nv_crtc->base.x,
509 nv_crtc->base.y, NULL);
510 if (!ret)
511 return -EINVAL;
512 } else {
513 ret = nv_crtc->set_scale(nv_crtc, true);
514 if (ret)
515 return ret;
516 }
517
518 return 0;
519 }
520
521 /* Underscan */
522 if (property == disp->underscan_property) {
523 if (nv_connector->underscan != value) {
524 nv_connector->underscan = value;
525 if (!nv_crtc || !nv_crtc->set_scale)
526 return 0;
527
528 return nv_crtc->set_scale(nv_crtc, true);
529 }
530
531 return 0;
532 }
533
534 if (property == disp->underscan_hborder_property) {
535 if (nv_connector->underscan_hborder != value) {
536 nv_connector->underscan_hborder = value;
537 if (!nv_crtc || !nv_crtc->set_scale)
538 return 0;
539
540 return nv_crtc->set_scale(nv_crtc, true);
541 }
542
543 return 0;
544 }
545
546 if (property == disp->underscan_vborder_property) {
547 if (nv_connector->underscan_vborder != value) {
548 nv_connector->underscan_vborder = value;
549 if (!nv_crtc || !nv_crtc->set_scale)
550 return 0;
551
552 return nv_crtc->set_scale(nv_crtc, true);
553 }
554
555 return 0;
556 }
557
558 /* Dithering */
559 if (property == disp->dithering_mode) {
560 nv_connector->dithering_mode = value;
561 if (!nv_crtc || !nv_crtc->set_dither)
562 return 0;
563
564 return nv_crtc->set_dither(nv_crtc, true);
565 }
566
567 if (property == disp->dithering_depth) {
568 nv_connector->dithering_depth = value;
569 if (!nv_crtc || !nv_crtc->set_dither)
570 return 0;
571
572 return nv_crtc->set_dither(nv_crtc, true);
573 }
574
575 if (nv_crtc && nv_crtc->set_color_vibrance) {
576 /* Hue */
577 if (property == disp->vibrant_hue_property) {
578 nv_crtc->vibrant_hue = value - 90;
579 return nv_crtc->set_color_vibrance(nv_crtc, true);
580 }
581 /* Saturation */
582 if (property == disp->color_vibrance_property) {
583 nv_crtc->color_vibrance = value - 100;
584 return nv_crtc->set_color_vibrance(nv_crtc, true);
585 }
586 }
587
588 if (nv_encoder && nv_encoder->dcb->type == DCB_OUTPUT_TV)
589 return get_slave_funcs(encoder)->set_property(
590 encoder, connector, property, value);
591
592 return -EINVAL;
593 }
594
595 static struct drm_display_mode *
nouveau_connector_native_mode(struct drm_connector * connector)596 nouveau_connector_native_mode(struct drm_connector *connector)
597 {
598 struct drm_connector_helper_funcs *helper = connector->helper_private;
599 struct nouveau_drm *drm = nouveau_drm(connector->dev);
600 struct nouveau_connector *nv_connector = nouveau_connector(connector);
601 struct drm_device *dev = connector->dev;
602 struct drm_display_mode *mode, *largest = NULL;
603 int high_w = 0, high_h = 0, high_v = 0;
604
605 list_for_each_entry(mode, &nv_connector->base.probed_modes, head) {
606 mode->vrefresh = drm_mode_vrefresh(mode);
607 if (helper->mode_valid(connector, mode) != MODE_OK ||
608 (mode->flags & DRM_MODE_FLAG_INTERLACE))
609 continue;
610
611 /* Use preferred mode if there is one.. */
612 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
613 NV_DEBUG(drm, "native mode from preferred\n");
614 return drm_mode_duplicate(dev, mode);
615 }
616
617 /* Otherwise, take the resolution with the largest width, then
618 * height, then vertical refresh
619 */
620 if (mode->hdisplay < high_w)
621 continue;
622
623 if (mode->hdisplay == high_w && mode->vdisplay < high_h)
624 continue;
625
626 if (mode->hdisplay == high_w && mode->vdisplay == high_h &&
627 mode->vrefresh < high_v)
628 continue;
629
630 high_w = mode->hdisplay;
631 high_h = mode->vdisplay;
632 high_v = mode->vrefresh;
633 largest = mode;
634 }
635
636 NV_DEBUG(drm, "native mode from largest: %dx%d@%d\n",
637 high_w, high_h, high_v);
638 return largest ? drm_mode_duplicate(dev, largest) : NULL;
639 }
640
641 struct moderec {
642 int hdisplay;
643 int vdisplay;
644 };
645
646 static struct moderec scaler_modes[] = {
647 { 1920, 1200 },
648 { 1920, 1080 },
649 { 1680, 1050 },
650 { 1600, 1200 },
651 { 1400, 1050 },
652 { 1280, 1024 },
653 { 1280, 960 },
654 { 1152, 864 },
655 { 1024, 768 },
656 { 800, 600 },
657 { 720, 400 },
658 { 640, 480 },
659 { 640, 400 },
660 { 640, 350 },
661 {}
662 };
663
664 static int
nouveau_connector_scaler_modes_add(struct drm_connector * connector)665 nouveau_connector_scaler_modes_add(struct drm_connector *connector)
666 {
667 struct nouveau_connector *nv_connector = nouveau_connector(connector);
668 struct drm_display_mode *native = nv_connector->native_mode, *m;
669 struct drm_device *dev = connector->dev;
670 struct moderec *mode = &scaler_modes[0];
671 int modes = 0;
672
673 if (!native)
674 return 0;
675
676 while (mode->hdisplay) {
677 if (mode->hdisplay <= native->hdisplay &&
678 mode->vdisplay <= native->vdisplay) {
679 m = drm_cvt_mode(dev, mode->hdisplay, mode->vdisplay,
680 drm_mode_vrefresh(native), false,
681 false, false);
682 if (!m)
683 continue;
684
685 m->type |= DRM_MODE_TYPE_DRIVER;
686
687 drm_mode_probed_add(connector, m);
688 modes++;
689 }
690
691 mode++;
692 }
693
694 return modes;
695 }
696
697 static void
nouveau_connector_detect_depth(struct drm_connector * connector)698 nouveau_connector_detect_depth(struct drm_connector *connector)
699 {
700 struct nouveau_drm *drm = nouveau_drm(connector->dev);
701 struct nouveau_connector *nv_connector = nouveau_connector(connector);
702 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
703 struct nvbios *bios = &drm->vbios;
704 struct drm_display_mode *mode = nv_connector->native_mode;
705 bool duallink;
706
707 /* if the edid is feeling nice enough to provide this info, use it */
708 if (nv_connector->edid && connector->display_info.bpc)
709 return;
710
711 /* EDID 1.4 is *supposed* to be supported on eDP, but, Apple... */
712 if (nv_connector->type == DCB_CONNECTOR_eDP) {
713 connector->display_info.bpc = 6;
714 return;
715 }
716
717 /* we're out of options unless we're LVDS, default to 8bpc */
718 if (nv_encoder->dcb->type != DCB_OUTPUT_LVDS) {
719 connector->display_info.bpc = 8;
720 return;
721 }
722
723 connector->display_info.bpc = 6;
724
725 /* LVDS: panel straps */
726 if (bios->fp_no_ddc) {
727 if (bios->fp.if_is_24bit)
728 connector->display_info.bpc = 8;
729 return;
730 }
731
732 /* LVDS: DDC panel, need to first determine the number of links to
733 * know which if_is_24bit flag to check...
734 */
735 if (nv_connector->edid &&
736 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG)
737 duallink = ((u8 *)nv_connector->edid)[121] == 2;
738 else
739 duallink = mode->clock >= bios->fp.duallink_transition_clk;
740
741 if ((!duallink && (bios->fp.strapless_is_24bit & 1)) ||
742 ( duallink && (bios->fp.strapless_is_24bit & 2)))
743 connector->display_info.bpc = 8;
744 }
745
746 static int
nouveau_connector_get_modes(struct drm_connector * connector)747 nouveau_connector_get_modes(struct drm_connector *connector)
748 {
749 struct drm_device *dev = connector->dev;
750 struct nouveau_drm *drm = nouveau_drm(dev);
751 struct nouveau_connector *nv_connector = nouveau_connector(connector);
752 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
753 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
754 int ret = 0;
755
756 /* destroy the native mode, the attached monitor could have changed.
757 */
758 if (nv_connector->native_mode) {
759 drm_mode_destroy(dev, nv_connector->native_mode);
760 nv_connector->native_mode = NULL;
761 }
762
763 if (nv_connector->edid)
764 ret = drm_add_edid_modes(connector, nv_connector->edid);
765 else
766 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS &&
767 (nv_encoder->dcb->lvdsconf.use_straps_for_mode ||
768 drm->vbios.fp_no_ddc) && nouveau_bios_fp_mode(dev, NULL)) {
769 struct drm_display_mode mode;
770
771 nouveau_bios_fp_mode(dev, &mode);
772 nv_connector->native_mode = drm_mode_duplicate(dev, &mode);
773 }
774
775 /* Determine display colour depth for everything except LVDS now,
776 * DP requires this before mode_valid() is called.
777 */
778 if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
779 nouveau_connector_detect_depth(connector);
780
781 /* Find the native mode if this is a digital panel, if we didn't
782 * find any modes through DDC previously add the native mode to
783 * the list of modes.
784 */
785 if (!nv_connector->native_mode)
786 nv_connector->native_mode =
787 nouveau_connector_native_mode(connector);
788 if (ret == 0 && nv_connector->native_mode) {
789 struct drm_display_mode *mode;
790
791 mode = drm_mode_duplicate(dev, nv_connector->native_mode);
792 drm_mode_probed_add(connector, mode);
793 ret = 1;
794 }
795
796 /* Determine LVDS colour depth, must happen after determining
797 * "native" mode as some VBIOS tables require us to use the
798 * pixel clock as part of the lookup...
799 */
800 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
801 nouveau_connector_detect_depth(connector);
802
803 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
804 ret = get_slave_funcs(encoder)->get_modes(encoder, connector);
805
806 if (nv_connector->type == DCB_CONNECTOR_LVDS ||
807 nv_connector->type == DCB_CONNECTOR_LVDS_SPWG ||
808 nv_connector->type == DCB_CONNECTOR_eDP)
809 ret += nouveau_connector_scaler_modes_add(connector);
810
811 return ret;
812 }
813
814 static unsigned
get_tmds_link_bandwidth(struct drm_connector * connector)815 get_tmds_link_bandwidth(struct drm_connector *connector)
816 {
817 struct nouveau_connector *nv_connector = nouveau_connector(connector);
818 struct nouveau_drm *drm = nouveau_drm(connector->dev);
819 struct dcb_output *dcb = nv_connector->detected_encoder->dcb;
820
821 if (dcb->location != DCB_LOC_ON_CHIP ||
822 nv_device(drm->device)->chipset >= 0x46)
823 return 165000;
824 else if (nv_device(drm->device)->chipset >= 0x40)
825 return 155000;
826 else if (nv_device(drm->device)->chipset >= 0x18)
827 return 135000;
828 else
829 return 112000;
830 }
831
832 static int
nouveau_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)833 nouveau_connector_mode_valid(struct drm_connector *connector,
834 struct drm_display_mode *mode)
835 {
836 struct nouveau_connector *nv_connector = nouveau_connector(connector);
837 struct nouveau_encoder *nv_encoder = nv_connector->detected_encoder;
838 struct drm_encoder *encoder = to_drm_encoder(nv_encoder);
839 unsigned min_clock = 25000, max_clock = min_clock;
840 unsigned clock = mode->clock;
841
842 switch (nv_encoder->dcb->type) {
843 case DCB_OUTPUT_LVDS:
844 if (nv_connector->native_mode &&
845 (mode->hdisplay > nv_connector->native_mode->hdisplay ||
846 mode->vdisplay > nv_connector->native_mode->vdisplay))
847 return MODE_PANEL;
848
849 min_clock = 0;
850 max_clock = 400000;
851 break;
852 case DCB_OUTPUT_TMDS:
853 max_clock = get_tmds_link_bandwidth(connector);
854 if (nouveau_duallink && nv_encoder->dcb->duallink_possible)
855 max_clock *= 2;
856 break;
857 case DCB_OUTPUT_ANALOG:
858 max_clock = nv_encoder->dcb->crtconf.maxfreq;
859 if (!max_clock)
860 max_clock = 350000;
861 break;
862 case DCB_OUTPUT_TV:
863 return get_slave_funcs(encoder)->mode_valid(encoder, mode);
864 case DCB_OUTPUT_DP:
865 max_clock = nv_encoder->dp.link_nr;
866 max_clock *= nv_encoder->dp.link_bw;
867 clock = clock * (connector->display_info.bpc * 3) / 10;
868 break;
869 default:
870 BUG_ON(1);
871 return MODE_BAD;
872 }
873
874 if (clock < min_clock)
875 return MODE_CLOCK_LOW;
876
877 if (clock > max_clock)
878 return MODE_CLOCK_HIGH;
879
880 return MODE_OK;
881 }
882
883 static struct drm_encoder *
nouveau_connector_best_encoder(struct drm_connector * connector)884 nouveau_connector_best_encoder(struct drm_connector *connector)
885 {
886 struct nouveau_connector *nv_connector = nouveau_connector(connector);
887
888 if (nv_connector->detected_encoder)
889 return to_drm_encoder(nv_connector->detected_encoder);
890
891 return NULL;
892 }
893
894 static const struct drm_connector_helper_funcs
895 nouveau_connector_helper_funcs = {
896 .get_modes = nouveau_connector_get_modes,
897 .mode_valid = nouveau_connector_mode_valid,
898 .best_encoder = nouveau_connector_best_encoder,
899 };
900
901 static const struct drm_connector_funcs
902 nouveau_connector_funcs = {
903 .dpms = drm_helper_connector_dpms,
904 .save = NULL,
905 .restore = NULL,
906 .detect = nouveau_connector_detect,
907 .destroy = nouveau_connector_destroy,
908 .fill_modes = drm_helper_probe_single_connector_modes,
909 .set_property = nouveau_connector_set_property,
910 .force = nouveau_connector_force
911 };
912
913 static const struct drm_connector_funcs
914 nouveau_connector_funcs_lvds = {
915 .dpms = drm_helper_connector_dpms,
916 .save = NULL,
917 .restore = NULL,
918 .detect = nouveau_connector_detect_lvds,
919 .destroy = nouveau_connector_destroy,
920 .fill_modes = drm_helper_probe_single_connector_modes,
921 .set_property = nouveau_connector_set_property,
922 .force = nouveau_connector_force
923 };
924
925 static void
nouveau_connector_hotplug_work(struct work_struct * work)926 nouveau_connector_hotplug_work(struct work_struct *work)
927 {
928 struct nouveau_connector *nv_connector =
929 container_of(work, struct nouveau_connector, hpd_work);
930 struct drm_connector *connector = &nv_connector->base;
931 struct drm_device *dev = connector->dev;
932 struct nouveau_drm *drm = nouveau_drm(dev);
933 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
934 bool plugged = gpio->get(gpio, 0, nv_connector->hpd.func, 0xff);
935
936 NV_DEBUG(drm, "%splugged %s\n", plugged ? "" : "un",
937 drm_get_connector_name(connector));
938
939 if (plugged)
940 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
941 else
942 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
943
944 drm_helper_hpd_irq_event(dev);
945 }
946
947 static int
nouveau_connector_hotplug(void * data,int index)948 nouveau_connector_hotplug(void *data, int index)
949 {
950 struct nouveau_connector *nv_connector = data;
951 schedule_work(&nv_connector->hpd_work);
952 return NVKM_EVENT_KEEP;
953 }
954
955 static int
drm_conntype_from_dcb(enum dcb_connector_type dcb)956 drm_conntype_from_dcb(enum dcb_connector_type dcb)
957 {
958 switch (dcb) {
959 case DCB_CONNECTOR_VGA : return DRM_MODE_CONNECTOR_VGA;
960 case DCB_CONNECTOR_TV_0 :
961 case DCB_CONNECTOR_TV_1 :
962 case DCB_CONNECTOR_TV_3 : return DRM_MODE_CONNECTOR_TV;
963 case DCB_CONNECTOR_DMS59_0 :
964 case DCB_CONNECTOR_DMS59_1 :
965 case DCB_CONNECTOR_DVI_I : return DRM_MODE_CONNECTOR_DVII;
966 case DCB_CONNECTOR_DVI_D : return DRM_MODE_CONNECTOR_DVID;
967 case DCB_CONNECTOR_LVDS :
968 case DCB_CONNECTOR_LVDS_SPWG: return DRM_MODE_CONNECTOR_LVDS;
969 case DCB_CONNECTOR_DMS59_DP0:
970 case DCB_CONNECTOR_DMS59_DP1:
971 case DCB_CONNECTOR_DP : return DRM_MODE_CONNECTOR_DisplayPort;
972 case DCB_CONNECTOR_eDP : return DRM_MODE_CONNECTOR_eDP;
973 case DCB_CONNECTOR_HDMI_0 :
974 case DCB_CONNECTOR_HDMI_1 :
975 case DCB_CONNECTOR_HDMI_C : return DRM_MODE_CONNECTOR_HDMIA;
976 default:
977 break;
978 }
979
980 return DRM_MODE_CONNECTOR_Unknown;
981 }
982
983 struct drm_connector *
nouveau_connector_create(struct drm_device * dev,int index)984 nouveau_connector_create(struct drm_device *dev, int index)
985 {
986 const struct drm_connector_funcs *funcs = &nouveau_connector_funcs;
987 struct nouveau_drm *drm = nouveau_drm(dev);
988 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
989 struct nouveau_display *disp = nouveau_display(dev);
990 struct nouveau_connector *nv_connector = NULL;
991 struct drm_connector *connector;
992 int type, ret = 0;
993 bool dummy;
994
995 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
996 nv_connector = nouveau_connector(connector);
997 if (nv_connector->index == index)
998 return connector;
999 }
1000
1001 nv_connector = kzalloc(sizeof(*nv_connector), GFP_KERNEL);
1002 if (!nv_connector)
1003 return ERR_PTR(-ENOMEM);
1004
1005 connector = &nv_connector->base;
1006 INIT_WORK(&nv_connector->hpd_work, nouveau_connector_hotplug_work);
1007 nv_connector->index = index;
1008
1009 /* attempt to parse vbios connector type and hotplug gpio */
1010 nv_connector->dcb = olddcb_conn(dev, index);
1011 if (nv_connector->dcb) {
1012 static const u8 hpd[16] = {
1013 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
1014 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
1015 };
1016
1017 u32 entry = ROM16(nv_connector->dcb[0]);
1018 if (olddcb_conntab(dev)[3] >= 4)
1019 entry |= (u32)ROM16(nv_connector->dcb[2]) << 16;
1020
1021 ret = gpio->find(gpio, 0, hpd[ffs((entry & 0x07033000) >> 12)],
1022 DCB_GPIO_UNUSED, &nv_connector->hpd);
1023 if (ret)
1024 nv_connector->hpd.func = DCB_GPIO_UNUSED;
1025
1026 if (nv_connector->hpd.func != DCB_GPIO_UNUSED) {
1027 nouveau_event_new(gpio->events, nv_connector->hpd.line,
1028 nouveau_connector_hotplug,
1029 nv_connector,
1030 &nv_connector->hpd_func);
1031 }
1032
1033 nv_connector->type = nv_connector->dcb[0];
1034 if (drm_conntype_from_dcb(nv_connector->type) ==
1035 DRM_MODE_CONNECTOR_Unknown) {
1036 NV_WARN(drm, "unknown connector type %02x\n",
1037 nv_connector->type);
1038 nv_connector->type = DCB_CONNECTOR_NONE;
1039 }
1040
1041 /* Gigabyte NX85T */
1042 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
1043 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1044 nv_connector->type = DCB_CONNECTOR_DVI_I;
1045 }
1046
1047 /* Gigabyte GV-NX86T512H */
1048 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
1049 if (nv_connector->type == DCB_CONNECTOR_HDMI_1)
1050 nv_connector->type = DCB_CONNECTOR_DVI_I;
1051 }
1052 } else {
1053 nv_connector->type = DCB_CONNECTOR_NONE;
1054 nv_connector->hpd.func = DCB_GPIO_UNUSED;
1055 }
1056
1057 /* no vbios data, or an unknown dcb connector type - attempt to
1058 * figure out something suitable ourselves
1059 */
1060 if (nv_connector->type == DCB_CONNECTOR_NONE) {
1061 struct dcb_table *dcbt = &drm->vbios.dcb;
1062 u32 encoders = 0;
1063 int i;
1064
1065 for (i = 0; i < dcbt->entries; i++) {
1066 if (dcbt->entry[i].connector == nv_connector->index)
1067 encoders |= (1 << dcbt->entry[i].type);
1068 }
1069
1070 if (encoders & (1 << DCB_OUTPUT_DP)) {
1071 if (encoders & (1 << DCB_OUTPUT_TMDS))
1072 nv_connector->type = DCB_CONNECTOR_DP;
1073 else
1074 nv_connector->type = DCB_CONNECTOR_eDP;
1075 } else
1076 if (encoders & (1 << DCB_OUTPUT_TMDS)) {
1077 if (encoders & (1 << DCB_OUTPUT_ANALOG))
1078 nv_connector->type = DCB_CONNECTOR_DVI_I;
1079 else
1080 nv_connector->type = DCB_CONNECTOR_DVI_D;
1081 } else
1082 if (encoders & (1 << DCB_OUTPUT_ANALOG)) {
1083 nv_connector->type = DCB_CONNECTOR_VGA;
1084 } else
1085 if (encoders & (1 << DCB_OUTPUT_LVDS)) {
1086 nv_connector->type = DCB_CONNECTOR_LVDS;
1087 } else
1088 if (encoders & (1 << DCB_OUTPUT_TV)) {
1089 nv_connector->type = DCB_CONNECTOR_TV_0;
1090 }
1091 }
1092
1093 type = drm_conntype_from_dcb(nv_connector->type);
1094 if (type == DRM_MODE_CONNECTOR_LVDS) {
1095 ret = nouveau_bios_parse_lvds_table(dev, 0, &dummy, &dummy);
1096 if (ret) {
1097 NV_ERROR(drm, "Error parsing LVDS table, disabling\n");
1098 kfree(nv_connector);
1099 return ERR_PTR(ret);
1100 }
1101
1102 funcs = &nouveau_connector_funcs_lvds;
1103 } else {
1104 funcs = &nouveau_connector_funcs;
1105 }
1106
1107 /* defaults, will get overridden in detect() */
1108 connector->interlace_allowed = false;
1109 connector->doublescan_allowed = false;
1110
1111 drm_connector_init(dev, connector, funcs, type);
1112 drm_connector_helper_add(connector, &nouveau_connector_helper_funcs);
1113
1114 /* Init DVI-I specific properties */
1115 if (nv_connector->type == DCB_CONNECTOR_DVI_I)
1116 drm_object_attach_property(&connector->base, dev->mode_config.dvi_i_subconnector_property, 0);
1117
1118 /* Add overscan compensation options to digital outputs */
1119 if (disp->underscan_property &&
1120 (type == DRM_MODE_CONNECTOR_DVID ||
1121 type == DRM_MODE_CONNECTOR_DVII ||
1122 type == DRM_MODE_CONNECTOR_HDMIA ||
1123 type == DRM_MODE_CONNECTOR_DisplayPort)) {
1124 drm_object_attach_property(&connector->base,
1125 disp->underscan_property,
1126 UNDERSCAN_OFF);
1127 drm_object_attach_property(&connector->base,
1128 disp->underscan_hborder_property,
1129 0);
1130 drm_object_attach_property(&connector->base,
1131 disp->underscan_vborder_property,
1132 0);
1133 }
1134
1135 /* Add hue and saturation options */
1136 if (disp->vibrant_hue_property)
1137 drm_object_attach_property(&connector->base,
1138 disp->vibrant_hue_property,
1139 90);
1140 if (disp->color_vibrance_property)
1141 drm_object_attach_property(&connector->base,
1142 disp->color_vibrance_property,
1143 150);
1144
1145 switch (nv_connector->type) {
1146 case DCB_CONNECTOR_VGA:
1147 if (nv_device(drm->device)->card_type >= NV_50) {
1148 drm_object_attach_property(&connector->base,
1149 dev->mode_config.scaling_mode_property,
1150 nv_connector->scaling_mode);
1151 }
1152 /* fall-through */
1153 case DCB_CONNECTOR_TV_0:
1154 case DCB_CONNECTOR_TV_1:
1155 case DCB_CONNECTOR_TV_3:
1156 nv_connector->scaling_mode = DRM_MODE_SCALE_NONE;
1157 break;
1158 default:
1159 nv_connector->scaling_mode = DRM_MODE_SCALE_FULLSCREEN;
1160
1161 drm_object_attach_property(&connector->base,
1162 dev->mode_config.scaling_mode_property,
1163 nv_connector->scaling_mode);
1164 if (disp->dithering_mode) {
1165 nv_connector->dithering_mode = DITHERING_MODE_AUTO;
1166 drm_object_attach_property(&connector->base,
1167 disp->dithering_mode,
1168 nv_connector->dithering_mode);
1169 }
1170 if (disp->dithering_depth) {
1171 nv_connector->dithering_depth = DITHERING_DEPTH_AUTO;
1172 drm_object_attach_property(&connector->base,
1173 disp->dithering_depth,
1174 nv_connector->dithering_depth);
1175 }
1176 break;
1177 }
1178
1179 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1180 if (nv_connector->hpd.func != DCB_GPIO_UNUSED)
1181 connector->polled = DRM_CONNECTOR_POLL_HPD;
1182
1183 drm_sysfs_connector_add(connector);
1184 return connector;
1185 }
1186