Loading...
1/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26
27#include <drm/display/drm_dp_helper.h>
28#include <drm/drm_crtc_helper.h>
29#include <drm/drm_edid.h>
30#include <drm/drm_modeset_helper_vtables.h>
31#include <drm/drm_probe_helper.h>
32#include <drm/amdgpu_drm.h>
33#include "amdgpu.h"
34#include "atom.h"
35#include "atombios_encoders.h"
36#include "atombios_dp.h"
37#include "amdgpu_connectors.h"
38#include "amdgpu_i2c.h"
39#include "amdgpu_display.h"
40
41#include <linux/pm_runtime.h>
42
43void amdgpu_connector_hotplug(struct drm_connector *connector)
44{
45 struct drm_device *dev = connector->dev;
46 struct amdgpu_device *adev = drm_to_adev(dev);
47 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
48
49 /* bail if the connector does not have hpd pin, e.g.,
50 * VGA, TV, etc.
51 */
52 if (amdgpu_connector->hpd.hpd == AMDGPU_HPD_NONE)
53 return;
54
55 amdgpu_display_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
56
57 /* if the connector is already off, don't turn it back on */
58 if (connector->dpms != DRM_MODE_DPMS_ON)
59 return;
60
61 /* just deal with DP (not eDP) here. */
62 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
63 struct amdgpu_connector_atom_dig *dig_connector =
64 amdgpu_connector->con_priv;
65
66 /* if existing sink type was not DP no need to retrain */
67 if (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT)
68 return;
69
70 /* first get sink type as it may be reset after (un)plug */
71 dig_connector->dp_sink_type = amdgpu_atombios_dp_get_sinktype(amdgpu_connector);
72 /* don't do anything if sink is not display port, i.e.,
73 * passive dp->(dvi|hdmi) adaptor
74 */
75 if (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT &&
76 amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd) &&
77 amdgpu_atombios_dp_needs_link_train(amdgpu_connector)) {
78 /* Don't start link training before we have the DPCD */
79 if (amdgpu_atombios_dp_get_dpcd(amdgpu_connector))
80 return;
81
82 /* Turn the connector off and back on immediately, which
83 * will trigger link training
84 */
85 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
86 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
87 }
88 }
89}
90
91static void amdgpu_connector_property_change_mode(struct drm_encoder *encoder)
92{
93 struct drm_crtc *crtc = encoder->crtc;
94
95 if (crtc && crtc->enabled) {
96 drm_crtc_helper_set_mode(crtc, &crtc->mode,
97 crtc->x, crtc->y, crtc->primary->fb);
98 }
99}
100
101int amdgpu_connector_get_monitor_bpc(struct drm_connector *connector)
102{
103 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
104 struct amdgpu_connector_atom_dig *dig_connector;
105 int bpc = 8;
106 unsigned int mode_clock, max_tmds_clock;
107
108 switch (connector->connector_type) {
109 case DRM_MODE_CONNECTOR_DVII:
110 case DRM_MODE_CONNECTOR_HDMIB:
111 if (amdgpu_connector->use_digital) {
112 if (connector->display_info.is_hdmi) {
113 if (connector->display_info.bpc)
114 bpc = connector->display_info.bpc;
115 }
116 }
117 break;
118 case DRM_MODE_CONNECTOR_DVID:
119 case DRM_MODE_CONNECTOR_HDMIA:
120 if (connector->display_info.is_hdmi) {
121 if (connector->display_info.bpc)
122 bpc = connector->display_info.bpc;
123 }
124 break;
125 case DRM_MODE_CONNECTOR_DisplayPort:
126 dig_connector = amdgpu_connector->con_priv;
127 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
128 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) ||
129 connector->display_info.is_hdmi) {
130 if (connector->display_info.bpc)
131 bpc = connector->display_info.bpc;
132 }
133 break;
134 case DRM_MODE_CONNECTOR_eDP:
135 case DRM_MODE_CONNECTOR_LVDS:
136 if (connector->display_info.bpc)
137 bpc = connector->display_info.bpc;
138 else {
139 const struct drm_connector_helper_funcs *connector_funcs =
140 connector->helper_private;
141 struct drm_encoder *encoder = connector_funcs->best_encoder(connector);
142 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
143 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
144
145 if (dig->lcd_misc & ATOM_PANEL_MISC_V13_6BIT_PER_COLOR)
146 bpc = 6;
147 else if (dig->lcd_misc & ATOM_PANEL_MISC_V13_8BIT_PER_COLOR)
148 bpc = 8;
149 }
150 break;
151 }
152
153 if (connector->display_info.is_hdmi) {
154 /*
155 * Pre DCE-8 hw can't handle > 12 bpc, and more than 12 bpc doesn't make
156 * much sense without support for > 12 bpc framebuffers. RGB 4:4:4 at
157 * 12 bpc is always supported on hdmi deep color sinks, as this is
158 * required by the HDMI-1.3 spec. Clamp to a safe 12 bpc maximum.
159 */
160 if (bpc > 12) {
161 DRM_DEBUG("%s: HDMI deep color %d bpc unsupported. Using 12 bpc.\n",
162 connector->name, bpc);
163 bpc = 12;
164 }
165
166 /* Any defined maximum tmds clock limit we must not exceed? */
167 if (connector->display_info.max_tmds_clock > 0) {
168 /* mode_clock is clock in kHz for mode to be modeset on this connector */
169 mode_clock = amdgpu_connector->pixelclock_for_modeset;
170
171 /* Maximum allowable input clock in kHz */
172 max_tmds_clock = connector->display_info.max_tmds_clock;
173
174 DRM_DEBUG("%s: hdmi mode dotclock %d kHz, max tmds input clock %d kHz.\n",
175 connector->name, mode_clock, max_tmds_clock);
176
177 /* Check if bpc is within clock limit. Try to degrade gracefully otherwise */
178 if ((bpc == 12) && (mode_clock * 3/2 > max_tmds_clock)) {
179 if ((connector->display_info.edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30) &&
180 (mode_clock * 5/4 <= max_tmds_clock))
181 bpc = 10;
182 else
183 bpc = 8;
184
185 DRM_DEBUG("%s: HDMI deep color 12 bpc exceeds max tmds clock. Using %d bpc.\n",
186 connector->name, bpc);
187 }
188
189 if ((bpc == 10) && (mode_clock * 5/4 > max_tmds_clock)) {
190 bpc = 8;
191 DRM_DEBUG("%s: HDMI deep color 10 bpc exceeds max tmds clock. Using %d bpc.\n",
192 connector->name, bpc);
193 }
194 } else if (bpc > 8) {
195 /* max_tmds_clock missing, but hdmi spec mandates it for deep color. */
196 DRM_DEBUG("%s: Required max tmds clock for HDMI deep color missing. Using 8 bpc.\n",
197 connector->name);
198 bpc = 8;
199 }
200 }
201
202 if ((amdgpu_deep_color == 0) && (bpc > 8)) {
203 DRM_DEBUG("%s: Deep color disabled. Set amdgpu module param deep_color=1 to enable.\n",
204 connector->name);
205 bpc = 8;
206 }
207
208 DRM_DEBUG("%s: Display bpc=%d, returned bpc=%d\n",
209 connector->name, connector->display_info.bpc, bpc);
210
211 return bpc;
212}
213
214static void
215amdgpu_connector_update_scratch_regs(struct drm_connector *connector,
216 enum drm_connector_status status)
217{
218 struct drm_encoder *best_encoder;
219 struct drm_encoder *encoder;
220 const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
221 bool connected;
222
223 best_encoder = connector_funcs->best_encoder(connector);
224
225 drm_connector_for_each_possible_encoder(connector, encoder) {
226 if ((encoder == best_encoder) && (status == connector_status_connected))
227 connected = true;
228 else
229 connected = false;
230
231 amdgpu_atombios_encoder_set_bios_scratch_regs(connector, encoder, connected);
232 }
233}
234
235static struct drm_encoder *
236amdgpu_connector_find_encoder(struct drm_connector *connector,
237 int encoder_type)
238{
239 struct drm_encoder *encoder;
240
241 drm_connector_for_each_possible_encoder(connector, encoder) {
242 if (encoder->encoder_type == encoder_type)
243 return encoder;
244 }
245
246 return NULL;
247}
248
249struct edid *amdgpu_connector_edid(struct drm_connector *connector)
250{
251 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
252 struct drm_property_blob *edid_blob = connector->edid_blob_ptr;
253
254 if (amdgpu_connector->edid) {
255 return amdgpu_connector->edid;
256 } else if (edid_blob) {
257 struct edid *edid = kmemdup(edid_blob->data, edid_blob->length, GFP_KERNEL);
258
259 if (edid)
260 amdgpu_connector->edid = edid;
261 }
262 return amdgpu_connector->edid;
263}
264
265static struct edid *
266amdgpu_connector_get_hardcoded_edid(struct amdgpu_device *adev)
267{
268 if (adev->mode_info.bios_hardcoded_edid) {
269 return kmemdup((unsigned char *)adev->mode_info.bios_hardcoded_edid,
270 adev->mode_info.bios_hardcoded_edid_size, GFP_KERNEL);
271 }
272 return NULL;
273}
274
275static void amdgpu_connector_get_edid(struct drm_connector *connector)
276{
277 struct drm_device *dev = connector->dev;
278 struct amdgpu_device *adev = drm_to_adev(dev);
279 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
280
281 if (amdgpu_connector->edid)
282 return;
283
284 /* on hw with routers, select right port */
285 if (amdgpu_connector->router.ddc_valid)
286 amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
287
288 if ((amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
289 ENCODER_OBJECT_ID_NONE) &&
290 amdgpu_connector->ddc_bus->has_aux) {
291 amdgpu_connector->edid = drm_get_edid(connector,
292 &amdgpu_connector->ddc_bus->aux.ddc);
293 } else if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
294 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
295 struct amdgpu_connector_atom_dig *dig = amdgpu_connector->con_priv;
296
297 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
298 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
299 amdgpu_connector->ddc_bus->has_aux)
300 amdgpu_connector->edid = drm_get_edid(connector,
301 &amdgpu_connector->ddc_bus->aux.ddc);
302 else if (amdgpu_connector->ddc_bus)
303 amdgpu_connector->edid = drm_get_edid(connector,
304 &amdgpu_connector->ddc_bus->adapter);
305 } else if (amdgpu_connector->ddc_bus) {
306 amdgpu_connector->edid = drm_get_edid(connector,
307 &amdgpu_connector->ddc_bus->adapter);
308 }
309
310 if (!amdgpu_connector->edid) {
311 /* some laptops provide a hardcoded edid in rom for LCDs */
312 if (((connector->connector_type == DRM_MODE_CONNECTOR_LVDS) ||
313 (connector->connector_type == DRM_MODE_CONNECTOR_eDP))) {
314 amdgpu_connector->edid = amdgpu_connector_get_hardcoded_edid(adev);
315 drm_connector_update_edid_property(connector, amdgpu_connector->edid);
316 }
317 }
318}
319
320static void amdgpu_connector_free_edid(struct drm_connector *connector)
321{
322 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
323
324 kfree(amdgpu_connector->edid);
325 amdgpu_connector->edid = NULL;
326}
327
328static int amdgpu_connector_ddc_get_modes(struct drm_connector *connector)
329{
330 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
331 int ret;
332
333 if (amdgpu_connector->edid) {
334 drm_connector_update_edid_property(connector, amdgpu_connector->edid);
335 ret = drm_add_edid_modes(connector, amdgpu_connector->edid);
336 return ret;
337 }
338 drm_connector_update_edid_property(connector, NULL);
339 return 0;
340}
341
342static struct drm_encoder *
343amdgpu_connector_best_single_encoder(struct drm_connector *connector)
344{
345 struct drm_encoder *encoder;
346
347 /* pick the first one */
348 drm_connector_for_each_possible_encoder(connector, encoder)
349 return encoder;
350
351 return NULL;
352}
353
354static void amdgpu_get_native_mode(struct drm_connector *connector)
355{
356 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
357 struct amdgpu_encoder *amdgpu_encoder;
358
359 if (encoder == NULL)
360 return;
361
362 amdgpu_encoder = to_amdgpu_encoder(encoder);
363
364 if (!list_empty(&connector->probed_modes)) {
365 struct drm_display_mode *preferred_mode =
366 list_first_entry(&connector->probed_modes,
367 struct drm_display_mode, head);
368
369 amdgpu_encoder->native_mode = *preferred_mode;
370 } else {
371 amdgpu_encoder->native_mode.clock = 0;
372 }
373}
374
375static struct drm_display_mode *
376amdgpu_connector_lcd_native_mode(struct drm_encoder *encoder)
377{
378 struct drm_device *dev = encoder->dev;
379 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
380 struct drm_display_mode *mode = NULL;
381 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
382
383 if (native_mode->hdisplay != 0 &&
384 native_mode->vdisplay != 0 &&
385 native_mode->clock != 0) {
386 mode = drm_mode_duplicate(dev, native_mode);
387 if (!mode)
388 return NULL;
389
390 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
391 drm_mode_set_name(mode);
392
393 DRM_DEBUG_KMS("Adding native panel mode %s\n", mode->name);
394 } else if (native_mode->hdisplay != 0 &&
395 native_mode->vdisplay != 0) {
396 /* mac laptops without an edid */
397 /* Note that this is not necessarily the exact panel mode,
398 * but an approximation based on the cvt formula. For these
399 * systems we should ideally read the mode info out of the
400 * registers or add a mode table, but this works and is much
401 * simpler.
402 */
403 mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
404 if (!mode)
405 return NULL;
406
407 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
408 DRM_DEBUG_KMS("Adding cvt approximation of native panel mode %s\n", mode->name);
409 }
410 return mode;
411}
412
413static void amdgpu_connector_add_common_modes(struct drm_encoder *encoder,
414 struct drm_connector *connector)
415{
416 struct drm_device *dev = encoder->dev;
417 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
418 struct drm_display_mode *mode = NULL;
419 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
420 int i;
421 static const struct mode_size {
422 int w;
423 int h;
424 } common_modes[17] = {
425 { 640, 480},
426 { 720, 480},
427 { 800, 600},
428 { 848, 480},
429 {1024, 768},
430 {1152, 768},
431 {1280, 720},
432 {1280, 800},
433 {1280, 854},
434 {1280, 960},
435 {1280, 1024},
436 {1440, 900},
437 {1400, 1050},
438 {1680, 1050},
439 {1600, 1200},
440 {1920, 1080},
441 {1920, 1200}
442 };
443
444 for (i = 0; i < 17; i++) {
445 if (amdgpu_encoder->devices & (ATOM_DEVICE_TV_SUPPORT)) {
446 if (common_modes[i].w > 1024 ||
447 common_modes[i].h > 768)
448 continue;
449 }
450 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
451 if (common_modes[i].w > native_mode->hdisplay ||
452 common_modes[i].h > native_mode->vdisplay ||
453 (common_modes[i].w == native_mode->hdisplay &&
454 common_modes[i].h == native_mode->vdisplay))
455 continue;
456 }
457 if (common_modes[i].w < 320 || common_modes[i].h < 200)
458 continue;
459
460 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
461 drm_mode_probed_add(connector, mode);
462 }
463}
464
465static int amdgpu_connector_set_property(struct drm_connector *connector,
466 struct drm_property *property,
467 uint64_t val)
468{
469 struct drm_device *dev = connector->dev;
470 struct amdgpu_device *adev = drm_to_adev(dev);
471 struct drm_encoder *encoder;
472 struct amdgpu_encoder *amdgpu_encoder;
473
474 if (property == adev->mode_info.coherent_mode_property) {
475 struct amdgpu_encoder_atom_dig *dig;
476 bool new_coherent_mode;
477
478 /* need to find digital encoder on connector */
479 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
480 if (!encoder)
481 return 0;
482
483 amdgpu_encoder = to_amdgpu_encoder(encoder);
484
485 if (!amdgpu_encoder->enc_priv)
486 return 0;
487
488 dig = amdgpu_encoder->enc_priv;
489 new_coherent_mode = val ? true : false;
490 if (dig->coherent_mode != new_coherent_mode) {
491 dig->coherent_mode = new_coherent_mode;
492 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
493 }
494 }
495
496 if (property == adev->mode_info.audio_property) {
497 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
498 /* need to find digital encoder on connector */
499 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
500 if (!encoder)
501 return 0;
502
503 amdgpu_encoder = to_amdgpu_encoder(encoder);
504
505 if (amdgpu_connector->audio != val) {
506 amdgpu_connector->audio = val;
507 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
508 }
509 }
510
511 if (property == adev->mode_info.dither_property) {
512 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
513 /* need to find digital encoder on connector */
514 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
515 if (!encoder)
516 return 0;
517
518 amdgpu_encoder = to_amdgpu_encoder(encoder);
519
520 if (amdgpu_connector->dither != val) {
521 amdgpu_connector->dither = val;
522 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
523 }
524 }
525
526 if (property == adev->mode_info.underscan_property) {
527 /* need to find digital encoder on connector */
528 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
529 if (!encoder)
530 return 0;
531
532 amdgpu_encoder = to_amdgpu_encoder(encoder);
533
534 if (amdgpu_encoder->underscan_type != val) {
535 amdgpu_encoder->underscan_type = val;
536 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
537 }
538 }
539
540 if (property == adev->mode_info.underscan_hborder_property) {
541 /* need to find digital encoder on connector */
542 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
543 if (!encoder)
544 return 0;
545
546 amdgpu_encoder = to_amdgpu_encoder(encoder);
547
548 if (amdgpu_encoder->underscan_hborder != val) {
549 amdgpu_encoder->underscan_hborder = val;
550 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
551 }
552 }
553
554 if (property == adev->mode_info.underscan_vborder_property) {
555 /* need to find digital encoder on connector */
556 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
557 if (!encoder)
558 return 0;
559
560 amdgpu_encoder = to_amdgpu_encoder(encoder);
561
562 if (amdgpu_encoder->underscan_vborder != val) {
563 amdgpu_encoder->underscan_vborder = val;
564 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
565 }
566 }
567
568 if (property == adev->mode_info.load_detect_property) {
569 struct amdgpu_connector *amdgpu_connector =
570 to_amdgpu_connector(connector);
571
572 if (val == 0)
573 amdgpu_connector->dac_load_detect = false;
574 else
575 amdgpu_connector->dac_load_detect = true;
576 }
577
578 if (property == dev->mode_config.scaling_mode_property) {
579 enum amdgpu_rmx_type rmx_type;
580
581 if (connector->encoder) {
582 amdgpu_encoder = to_amdgpu_encoder(connector->encoder);
583 } else {
584 const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
585
586 amdgpu_encoder = to_amdgpu_encoder(connector_funcs->best_encoder(connector));
587 }
588
589 switch (val) {
590 default:
591 case DRM_MODE_SCALE_NONE:
592 rmx_type = RMX_OFF;
593 break;
594 case DRM_MODE_SCALE_CENTER:
595 rmx_type = RMX_CENTER;
596 break;
597 case DRM_MODE_SCALE_ASPECT:
598 rmx_type = RMX_ASPECT;
599 break;
600 case DRM_MODE_SCALE_FULLSCREEN:
601 rmx_type = RMX_FULL;
602 break;
603 }
604
605 if (amdgpu_encoder->rmx_type == rmx_type)
606 return 0;
607
608 if ((rmx_type != DRM_MODE_SCALE_NONE) &&
609 (amdgpu_encoder->native_mode.clock == 0))
610 return 0;
611
612 amdgpu_encoder->rmx_type = rmx_type;
613
614 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
615 }
616
617 return 0;
618}
619
620static void
621amdgpu_connector_fixup_lcd_native_mode(struct drm_encoder *encoder,
622 struct drm_connector *connector)
623{
624 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
625 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
626 struct drm_display_mode *t, *mode;
627
628 /* If the EDID preferred mode doesn't match the native mode, use it */
629 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
630 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
631 if (mode->hdisplay != native_mode->hdisplay ||
632 mode->vdisplay != native_mode->vdisplay)
633 drm_mode_copy(native_mode, mode);
634 }
635 }
636
637 /* Try to get native mode details from EDID if necessary */
638 if (!native_mode->clock) {
639 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
640 if (mode->hdisplay == native_mode->hdisplay &&
641 mode->vdisplay == native_mode->vdisplay) {
642 drm_mode_copy(native_mode, mode);
643 drm_mode_set_crtcinfo(native_mode, CRTC_INTERLACE_HALVE_V);
644 DRM_DEBUG_KMS("Determined LVDS native mode details from EDID\n");
645 break;
646 }
647 }
648 }
649
650 if (!native_mode->clock) {
651 DRM_DEBUG_KMS("No LVDS native mode details, disabling RMX\n");
652 amdgpu_encoder->rmx_type = RMX_OFF;
653 }
654}
655
656static int amdgpu_connector_lvds_get_modes(struct drm_connector *connector)
657{
658 struct drm_encoder *encoder;
659 int ret = 0;
660 struct drm_display_mode *mode;
661
662 amdgpu_connector_get_edid(connector);
663 ret = amdgpu_connector_ddc_get_modes(connector);
664 if (ret > 0) {
665 encoder = amdgpu_connector_best_single_encoder(connector);
666 if (encoder) {
667 amdgpu_connector_fixup_lcd_native_mode(encoder, connector);
668 /* add scaled modes */
669 amdgpu_connector_add_common_modes(encoder, connector);
670 }
671 return ret;
672 }
673
674 encoder = amdgpu_connector_best_single_encoder(connector);
675 if (!encoder)
676 return 0;
677
678 /* we have no EDID modes */
679 mode = amdgpu_connector_lcd_native_mode(encoder);
680 if (mode) {
681 ret = 1;
682 drm_mode_probed_add(connector, mode);
683 /* add the width/height from vbios tables if available */
684 connector->display_info.width_mm = mode->width_mm;
685 connector->display_info.height_mm = mode->height_mm;
686 /* add scaled modes */
687 amdgpu_connector_add_common_modes(encoder, connector);
688 }
689
690 return ret;
691}
692
693static enum drm_mode_status amdgpu_connector_lvds_mode_valid(struct drm_connector *connector,
694 struct drm_display_mode *mode)
695{
696 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
697
698 if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
699 return MODE_PANEL;
700
701 if (encoder) {
702 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
703 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
704
705 /* AVIVO hardware supports downscaling modes larger than the panel
706 * to the panel size, but I'm not sure this is desirable.
707 */
708 if ((mode->hdisplay > native_mode->hdisplay) ||
709 (mode->vdisplay > native_mode->vdisplay))
710 return MODE_PANEL;
711
712 /* if scaling is disabled, block non-native modes */
713 if (amdgpu_encoder->rmx_type == RMX_OFF) {
714 if ((mode->hdisplay != native_mode->hdisplay) ||
715 (mode->vdisplay != native_mode->vdisplay))
716 return MODE_PANEL;
717 }
718 }
719
720 return MODE_OK;
721}
722
723static enum drm_connector_status
724amdgpu_connector_lvds_detect(struct drm_connector *connector, bool force)
725{
726 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
727 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
728 enum drm_connector_status ret = connector_status_disconnected;
729 int r;
730
731 if (!drm_kms_helper_is_poll_worker()) {
732 r = pm_runtime_get_sync(connector->dev->dev);
733 if (r < 0) {
734 pm_runtime_put_autosuspend(connector->dev->dev);
735 return connector_status_disconnected;
736 }
737 }
738
739 if (encoder) {
740 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
741 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
742
743 /* check if panel is valid */
744 if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
745 ret = connector_status_connected;
746
747 }
748
749 /* check for edid as well */
750 amdgpu_connector_get_edid(connector);
751 if (amdgpu_connector->edid)
752 ret = connector_status_connected;
753 /* check acpi lid status ??? */
754
755 amdgpu_connector_update_scratch_regs(connector, ret);
756
757 if (!drm_kms_helper_is_poll_worker()) {
758 pm_runtime_mark_last_busy(connector->dev->dev);
759 pm_runtime_put_autosuspend(connector->dev->dev);
760 }
761
762 return ret;
763}
764
765static void amdgpu_connector_unregister(struct drm_connector *connector)
766{
767 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
768
769 if (amdgpu_connector->ddc_bus && amdgpu_connector->ddc_bus->has_aux) {
770 drm_dp_aux_unregister(&amdgpu_connector->ddc_bus->aux);
771 amdgpu_connector->ddc_bus->has_aux = false;
772 }
773}
774
775static void amdgpu_connector_destroy(struct drm_connector *connector)
776{
777 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
778
779 amdgpu_connector_free_edid(connector);
780 kfree(amdgpu_connector->con_priv);
781 drm_connector_unregister(connector);
782 drm_connector_cleanup(connector);
783 kfree(connector);
784}
785
786static int amdgpu_connector_set_lcd_property(struct drm_connector *connector,
787 struct drm_property *property,
788 uint64_t value)
789{
790 struct drm_device *dev = connector->dev;
791 struct amdgpu_encoder *amdgpu_encoder;
792 enum amdgpu_rmx_type rmx_type;
793
794 DRM_DEBUG_KMS("\n");
795 if (property != dev->mode_config.scaling_mode_property)
796 return 0;
797
798 if (connector->encoder)
799 amdgpu_encoder = to_amdgpu_encoder(connector->encoder);
800 else {
801 const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
802
803 amdgpu_encoder = to_amdgpu_encoder(connector_funcs->best_encoder(connector));
804 }
805
806 switch (value) {
807 case DRM_MODE_SCALE_NONE:
808 rmx_type = RMX_OFF;
809 break;
810 case DRM_MODE_SCALE_CENTER:
811 rmx_type = RMX_CENTER;
812 break;
813 case DRM_MODE_SCALE_ASPECT:
814 rmx_type = RMX_ASPECT;
815 break;
816 default:
817 case DRM_MODE_SCALE_FULLSCREEN:
818 rmx_type = RMX_FULL;
819 break;
820 }
821
822 if (amdgpu_encoder->rmx_type == rmx_type)
823 return 0;
824
825 amdgpu_encoder->rmx_type = rmx_type;
826
827 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
828 return 0;
829}
830
831
832static const struct drm_connector_helper_funcs amdgpu_connector_lvds_helper_funcs = {
833 .get_modes = amdgpu_connector_lvds_get_modes,
834 .mode_valid = amdgpu_connector_lvds_mode_valid,
835 .best_encoder = amdgpu_connector_best_single_encoder,
836};
837
838static const struct drm_connector_funcs amdgpu_connector_lvds_funcs = {
839 .dpms = drm_helper_connector_dpms,
840 .detect = amdgpu_connector_lvds_detect,
841 .fill_modes = drm_helper_probe_single_connector_modes,
842 .early_unregister = amdgpu_connector_unregister,
843 .destroy = amdgpu_connector_destroy,
844 .set_property = amdgpu_connector_set_lcd_property,
845};
846
847static int amdgpu_connector_vga_get_modes(struct drm_connector *connector)
848{
849 int ret;
850
851 amdgpu_connector_get_edid(connector);
852 ret = amdgpu_connector_ddc_get_modes(connector);
853 amdgpu_get_native_mode(connector);
854
855 return ret;
856}
857
858static enum drm_mode_status amdgpu_connector_vga_mode_valid(struct drm_connector *connector,
859 struct drm_display_mode *mode)
860{
861 struct drm_device *dev = connector->dev;
862 struct amdgpu_device *adev = drm_to_adev(dev);
863
864 /* XXX check mode bandwidth */
865
866 if ((mode->clock / 10) > adev->clock.max_pixel_clock)
867 return MODE_CLOCK_HIGH;
868
869 return MODE_OK;
870}
871
872static enum drm_connector_status
873amdgpu_connector_vga_detect(struct drm_connector *connector, bool force)
874{
875 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
876 struct drm_encoder *encoder;
877 const struct drm_encoder_helper_funcs *encoder_funcs;
878 bool dret = false;
879 enum drm_connector_status ret = connector_status_disconnected;
880 int r;
881
882 if (!drm_kms_helper_is_poll_worker()) {
883 r = pm_runtime_get_sync(connector->dev->dev);
884 if (r < 0) {
885 pm_runtime_put_autosuspend(connector->dev->dev);
886 return connector_status_disconnected;
887 }
888 }
889
890 encoder = amdgpu_connector_best_single_encoder(connector);
891 if (!encoder)
892 ret = connector_status_disconnected;
893
894 if (amdgpu_connector->ddc_bus)
895 dret = amdgpu_display_ddc_probe(amdgpu_connector, false);
896 if (dret) {
897 amdgpu_connector->detected_by_load = false;
898 amdgpu_connector_free_edid(connector);
899 amdgpu_connector_get_edid(connector);
900
901 if (!amdgpu_connector->edid) {
902 DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
903 connector->name);
904 ret = connector_status_connected;
905 } else {
906 amdgpu_connector->use_digital =
907 !!(amdgpu_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
908
909 /* some oems have boards with separate digital and analog connectors
910 * with a shared ddc line (often vga + hdmi)
911 */
912 if (amdgpu_connector->use_digital && amdgpu_connector->shared_ddc) {
913 amdgpu_connector_free_edid(connector);
914 ret = connector_status_disconnected;
915 } else {
916 ret = connector_status_connected;
917 }
918 }
919 } else {
920
921 /* if we aren't forcing don't do destructive polling */
922 if (!force) {
923 /* only return the previous status if we last
924 * detected a monitor via load.
925 */
926 if (amdgpu_connector->detected_by_load)
927 ret = connector->status;
928 goto out;
929 }
930
931 if (amdgpu_connector->dac_load_detect && encoder) {
932 encoder_funcs = encoder->helper_private;
933 ret = encoder_funcs->detect(encoder, connector);
934 if (ret != connector_status_disconnected)
935 amdgpu_connector->detected_by_load = true;
936 }
937 }
938
939 amdgpu_connector_update_scratch_regs(connector, ret);
940
941out:
942 if (!drm_kms_helper_is_poll_worker()) {
943 pm_runtime_mark_last_busy(connector->dev->dev);
944 pm_runtime_put_autosuspend(connector->dev->dev);
945 }
946
947 return ret;
948}
949
950static const struct drm_connector_helper_funcs amdgpu_connector_vga_helper_funcs = {
951 .get_modes = amdgpu_connector_vga_get_modes,
952 .mode_valid = amdgpu_connector_vga_mode_valid,
953 .best_encoder = amdgpu_connector_best_single_encoder,
954};
955
956static const struct drm_connector_funcs amdgpu_connector_vga_funcs = {
957 .dpms = drm_helper_connector_dpms,
958 .detect = amdgpu_connector_vga_detect,
959 .fill_modes = drm_helper_probe_single_connector_modes,
960 .early_unregister = amdgpu_connector_unregister,
961 .destroy = amdgpu_connector_destroy,
962 .set_property = amdgpu_connector_set_property,
963};
964
965static bool
966amdgpu_connector_check_hpd_status_unchanged(struct drm_connector *connector)
967{
968 struct drm_device *dev = connector->dev;
969 struct amdgpu_device *adev = drm_to_adev(dev);
970 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
971 enum drm_connector_status status;
972
973 if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE) {
974 if (amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd))
975 status = connector_status_connected;
976 else
977 status = connector_status_disconnected;
978 if (connector->status == status)
979 return true;
980 }
981
982 return false;
983}
984
985static void amdgpu_connector_shared_ddc(enum drm_connector_status *status,
986 struct drm_connector *connector,
987 struct amdgpu_connector *amdgpu_connector)
988{
989 struct drm_connector *list_connector;
990 struct drm_connector_list_iter iter;
991 struct amdgpu_connector *list_amdgpu_connector;
992 struct drm_device *dev = connector->dev;
993 struct amdgpu_device *adev = drm_to_adev(dev);
994
995 if (amdgpu_connector->shared_ddc && *status == connector_status_connected) {
996 drm_connector_list_iter_begin(dev, &iter);
997 drm_for_each_connector_iter(list_connector,
998 &iter) {
999 if (connector == list_connector)
1000 continue;
1001 list_amdgpu_connector = to_amdgpu_connector(list_connector);
1002 if (list_amdgpu_connector->shared_ddc &&
1003 list_amdgpu_connector->ddc_bus->rec.i2c_id ==
1004 amdgpu_connector->ddc_bus->rec.i2c_id) {
1005 /* cases where both connectors are digital */
1006 if (list_connector->connector_type != DRM_MODE_CONNECTOR_VGA) {
1007 /* hpd is our only option in this case */
1008 if (!amdgpu_display_hpd_sense(adev,
1009 amdgpu_connector->hpd.hpd)) {
1010 amdgpu_connector_free_edid(connector);
1011 *status = connector_status_disconnected;
1012 }
1013 }
1014 }
1015 }
1016 drm_connector_list_iter_end(&iter);
1017 }
1018}
1019
1020/*
1021 * DVI is complicated
1022 * Do a DDC probe, if DDC probe passes, get the full EDID so
1023 * we can do analog/digital monitor detection at this point.
1024 * If the monitor is an analog monitor or we got no DDC,
1025 * we need to find the DAC encoder object for this connector.
1026 * If we got no DDC, we do load detection on the DAC encoder object.
1027 * If we got analog DDC or load detection passes on the DAC encoder
1028 * we have to check if this analog encoder is shared with anyone else (TV)
1029 * if its shared we have to set the other connector to disconnected.
1030 */
1031static enum drm_connector_status
1032amdgpu_connector_dvi_detect(struct drm_connector *connector, bool force)
1033{
1034 struct drm_device *dev = connector->dev;
1035 struct amdgpu_device *adev = drm_to_adev(dev);
1036 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1037 const struct drm_encoder_helper_funcs *encoder_funcs;
1038 int r;
1039 enum drm_connector_status ret = connector_status_disconnected;
1040 bool dret = false, broken_edid = false;
1041
1042 if (!drm_kms_helper_is_poll_worker()) {
1043 r = pm_runtime_get_sync(connector->dev->dev);
1044 if (r < 0) {
1045 pm_runtime_put_autosuspend(connector->dev->dev);
1046 return connector_status_disconnected;
1047 }
1048 }
1049
1050 if (amdgpu_connector->detected_hpd_without_ddc) {
1051 force = true;
1052 amdgpu_connector->detected_hpd_without_ddc = false;
1053 }
1054
1055 if (!force && amdgpu_connector_check_hpd_status_unchanged(connector)) {
1056 ret = connector->status;
1057 goto exit;
1058 }
1059
1060 if (amdgpu_connector->ddc_bus) {
1061 dret = amdgpu_display_ddc_probe(amdgpu_connector, false);
1062
1063 /* Sometimes the pins required for the DDC probe on DVI
1064 * connectors don't make contact at the same time that the ones
1065 * for HPD do. If the DDC probe fails even though we had an HPD
1066 * signal, try again later
1067 */
1068 if (!dret && !force &&
1069 amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd)) {
1070 DRM_DEBUG_KMS("hpd detected without ddc, retrying in 1 second\n");
1071 amdgpu_connector->detected_hpd_without_ddc = true;
1072 schedule_delayed_work(&adev->hotplug_work,
1073 msecs_to_jiffies(1000));
1074 goto exit;
1075 }
1076 }
1077 if (dret) {
1078 amdgpu_connector->detected_by_load = false;
1079 amdgpu_connector_free_edid(connector);
1080 amdgpu_connector_get_edid(connector);
1081
1082 if (!amdgpu_connector->edid) {
1083 DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
1084 connector->name);
1085 ret = connector_status_connected;
1086 broken_edid = true; /* defer use_digital to later */
1087 } else {
1088 amdgpu_connector->use_digital =
1089 !!(amdgpu_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
1090
1091 /* some oems have boards with separate digital and analog connectors
1092 * with a shared ddc line (often vga + hdmi)
1093 */
1094 if ((!amdgpu_connector->use_digital) && amdgpu_connector->shared_ddc) {
1095 amdgpu_connector_free_edid(connector);
1096 ret = connector_status_disconnected;
1097 } else {
1098 ret = connector_status_connected;
1099 }
1100
1101 /* This gets complicated. We have boards with VGA + HDMI with a
1102 * shared DDC line and we have boards with DVI-D + HDMI with a shared
1103 * DDC line. The latter is more complex because with DVI<->HDMI adapters
1104 * you don't really know what's connected to which port as both are digital.
1105 */
1106 amdgpu_connector_shared_ddc(&ret, connector, amdgpu_connector);
1107 }
1108 }
1109
1110 if ((ret == connector_status_connected) && (amdgpu_connector->use_digital == true))
1111 goto out;
1112
1113 /* DVI-D and HDMI-A are digital only */
1114 if ((connector->connector_type == DRM_MODE_CONNECTOR_DVID) ||
1115 (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA))
1116 goto out;
1117
1118 /* if we aren't forcing don't do destructive polling */
1119 if (!force) {
1120 /* only return the previous status if we last
1121 * detected a monitor via load.
1122 */
1123 if (amdgpu_connector->detected_by_load)
1124 ret = connector->status;
1125 goto out;
1126 }
1127
1128 /* find analog encoder */
1129 if (amdgpu_connector->dac_load_detect) {
1130 struct drm_encoder *encoder;
1131
1132 drm_connector_for_each_possible_encoder(connector, encoder) {
1133 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC &&
1134 encoder->encoder_type != DRM_MODE_ENCODER_TVDAC)
1135 continue;
1136
1137 encoder_funcs = encoder->helper_private;
1138 if (encoder_funcs->detect) {
1139 if (!broken_edid) {
1140 if (ret != connector_status_connected) {
1141 /* deal with analog monitors without DDC */
1142 ret = encoder_funcs->detect(encoder, connector);
1143 if (ret == connector_status_connected) {
1144 amdgpu_connector->use_digital = false;
1145 }
1146 if (ret != connector_status_disconnected)
1147 amdgpu_connector->detected_by_load = true;
1148 }
1149 } else {
1150 enum drm_connector_status lret;
1151 /* assume digital unless load detected otherwise */
1152 amdgpu_connector->use_digital = true;
1153 lret = encoder_funcs->detect(encoder, connector);
1154 DRM_DEBUG_KMS("load_detect %x returned: %x\n",
1155 encoder->encoder_type, lret);
1156 if (lret == connector_status_connected)
1157 amdgpu_connector->use_digital = false;
1158 }
1159 break;
1160 }
1161 }
1162 }
1163
1164out:
1165 /* updated in get modes as well since we need to know if it's analog or digital */
1166 amdgpu_connector_update_scratch_regs(connector, ret);
1167
1168exit:
1169 if (!drm_kms_helper_is_poll_worker()) {
1170 pm_runtime_mark_last_busy(connector->dev->dev);
1171 pm_runtime_put_autosuspend(connector->dev->dev);
1172 }
1173
1174 return ret;
1175}
1176
1177/* okay need to be smart in here about which encoder to pick */
1178static struct drm_encoder *
1179amdgpu_connector_dvi_encoder(struct drm_connector *connector)
1180{
1181 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1182 struct drm_encoder *encoder;
1183
1184 drm_connector_for_each_possible_encoder(connector, encoder) {
1185 if (amdgpu_connector->use_digital == true) {
1186 if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
1187 return encoder;
1188 } else {
1189 if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
1190 encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1191 return encoder;
1192 }
1193 }
1194
1195 /* see if we have a default encoder TODO */
1196
1197 /* then check use digitial */
1198 /* pick the first one */
1199 drm_connector_for_each_possible_encoder(connector, encoder)
1200 return encoder;
1201
1202 return NULL;
1203}
1204
1205static void amdgpu_connector_dvi_force(struct drm_connector *connector)
1206{
1207 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1208
1209 if (connector->force == DRM_FORCE_ON)
1210 amdgpu_connector->use_digital = false;
1211 if (connector->force == DRM_FORCE_ON_DIGITAL)
1212 amdgpu_connector->use_digital = true;
1213}
1214
1215static enum drm_mode_status amdgpu_connector_dvi_mode_valid(struct drm_connector *connector,
1216 struct drm_display_mode *mode)
1217{
1218 struct drm_device *dev = connector->dev;
1219 struct amdgpu_device *adev = drm_to_adev(dev);
1220 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1221
1222 /* XXX check mode bandwidth */
1223
1224 if (amdgpu_connector->use_digital && (mode->clock > 165000)) {
1225 if ((amdgpu_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
1226 (amdgpu_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
1227 (amdgpu_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B)) {
1228 return MODE_OK;
1229 } else if (connector->display_info.is_hdmi) {
1230 /* HDMI 1.3+ supports max clock of 340 Mhz */
1231 if (mode->clock > 340000)
1232 return MODE_CLOCK_HIGH;
1233 else
1234 return MODE_OK;
1235 } else {
1236 return MODE_CLOCK_HIGH;
1237 }
1238 }
1239
1240 /* check against the max pixel clock */
1241 if ((mode->clock / 10) > adev->clock.max_pixel_clock)
1242 return MODE_CLOCK_HIGH;
1243
1244 return MODE_OK;
1245}
1246
1247static const struct drm_connector_helper_funcs amdgpu_connector_dvi_helper_funcs = {
1248 .get_modes = amdgpu_connector_vga_get_modes,
1249 .mode_valid = amdgpu_connector_dvi_mode_valid,
1250 .best_encoder = amdgpu_connector_dvi_encoder,
1251};
1252
1253static const struct drm_connector_funcs amdgpu_connector_dvi_funcs = {
1254 .dpms = drm_helper_connector_dpms,
1255 .detect = amdgpu_connector_dvi_detect,
1256 .fill_modes = drm_helper_probe_single_connector_modes,
1257 .set_property = amdgpu_connector_set_property,
1258 .early_unregister = amdgpu_connector_unregister,
1259 .destroy = amdgpu_connector_destroy,
1260 .force = amdgpu_connector_dvi_force,
1261};
1262
1263static int amdgpu_connector_dp_get_modes(struct drm_connector *connector)
1264{
1265 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1266 struct amdgpu_connector_atom_dig *amdgpu_dig_connector = amdgpu_connector->con_priv;
1267 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
1268 int ret;
1269
1270 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1271 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1272 struct drm_display_mode *mode;
1273
1274 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1275 if (!amdgpu_dig_connector->edp_on)
1276 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1277 ATOM_TRANSMITTER_ACTION_POWER_ON);
1278 amdgpu_connector_get_edid(connector);
1279 ret = amdgpu_connector_ddc_get_modes(connector);
1280 if (!amdgpu_dig_connector->edp_on)
1281 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1282 ATOM_TRANSMITTER_ACTION_POWER_OFF);
1283 } else {
1284 /* need to setup ddc on the bridge */
1285 if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1286 ENCODER_OBJECT_ID_NONE) {
1287 if (encoder)
1288 amdgpu_atombios_encoder_setup_ext_encoder_ddc(encoder);
1289 }
1290 amdgpu_connector_get_edid(connector);
1291 ret = amdgpu_connector_ddc_get_modes(connector);
1292 }
1293
1294 if (ret > 0) {
1295 if (encoder) {
1296 amdgpu_connector_fixup_lcd_native_mode(encoder, connector);
1297 /* add scaled modes */
1298 amdgpu_connector_add_common_modes(encoder, connector);
1299 }
1300 return ret;
1301 }
1302
1303 if (!encoder)
1304 return 0;
1305
1306 /* we have no EDID modes */
1307 mode = amdgpu_connector_lcd_native_mode(encoder);
1308 if (mode) {
1309 ret = 1;
1310 drm_mode_probed_add(connector, mode);
1311 /* add the width/height from vbios tables if available */
1312 connector->display_info.width_mm = mode->width_mm;
1313 connector->display_info.height_mm = mode->height_mm;
1314 /* add scaled modes */
1315 amdgpu_connector_add_common_modes(encoder, connector);
1316 }
1317 } else {
1318 /* need to setup ddc on the bridge */
1319 if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1320 ENCODER_OBJECT_ID_NONE) {
1321 if (encoder)
1322 amdgpu_atombios_encoder_setup_ext_encoder_ddc(encoder);
1323 }
1324 amdgpu_connector_get_edid(connector);
1325 ret = amdgpu_connector_ddc_get_modes(connector);
1326
1327 amdgpu_get_native_mode(connector);
1328 }
1329
1330 return ret;
1331}
1332
1333u16 amdgpu_connector_encoder_get_dp_bridge_encoder_id(struct drm_connector *connector)
1334{
1335 struct drm_encoder *encoder;
1336 struct amdgpu_encoder *amdgpu_encoder;
1337
1338 drm_connector_for_each_possible_encoder(connector, encoder) {
1339 amdgpu_encoder = to_amdgpu_encoder(encoder);
1340
1341 switch (amdgpu_encoder->encoder_id) {
1342 case ENCODER_OBJECT_ID_TRAVIS:
1343 case ENCODER_OBJECT_ID_NUTMEG:
1344 return amdgpu_encoder->encoder_id;
1345 default:
1346 break;
1347 }
1348 }
1349
1350 return ENCODER_OBJECT_ID_NONE;
1351}
1352
1353static bool amdgpu_connector_encoder_is_hbr2(struct drm_connector *connector)
1354{
1355 struct drm_encoder *encoder;
1356 struct amdgpu_encoder *amdgpu_encoder;
1357 bool found = false;
1358
1359 drm_connector_for_each_possible_encoder(connector, encoder) {
1360 amdgpu_encoder = to_amdgpu_encoder(encoder);
1361 if (amdgpu_encoder->caps & ATOM_ENCODER_CAP_RECORD_HBR2)
1362 found = true;
1363 }
1364
1365 return found;
1366}
1367
1368bool amdgpu_connector_is_dp12_capable(struct drm_connector *connector)
1369{
1370 struct drm_device *dev = connector->dev;
1371 struct amdgpu_device *adev = drm_to_adev(dev);
1372
1373 if ((adev->clock.default_dispclk >= 53900) &&
1374 amdgpu_connector_encoder_is_hbr2(connector)) {
1375 return true;
1376 }
1377
1378 return false;
1379}
1380
1381static enum drm_connector_status
1382amdgpu_connector_dp_detect(struct drm_connector *connector, bool force)
1383{
1384 struct drm_device *dev = connector->dev;
1385 struct amdgpu_device *adev = drm_to_adev(dev);
1386 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1387 enum drm_connector_status ret = connector_status_disconnected;
1388 struct amdgpu_connector_atom_dig *amdgpu_dig_connector = amdgpu_connector->con_priv;
1389 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
1390 int r;
1391
1392 if (!drm_kms_helper_is_poll_worker()) {
1393 r = pm_runtime_get_sync(connector->dev->dev);
1394 if (r < 0) {
1395 pm_runtime_put_autosuspend(connector->dev->dev);
1396 return connector_status_disconnected;
1397 }
1398 }
1399
1400 if (!force && amdgpu_connector_check_hpd_status_unchanged(connector)) {
1401 ret = connector->status;
1402 goto out;
1403 }
1404
1405 amdgpu_connector_free_edid(connector);
1406
1407 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1408 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1409 if (encoder) {
1410 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1411 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
1412
1413 /* check if panel is valid */
1414 if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
1415 ret = connector_status_connected;
1416 }
1417 /* eDP is always DP */
1418 amdgpu_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1419 if (!amdgpu_dig_connector->edp_on)
1420 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1421 ATOM_TRANSMITTER_ACTION_POWER_ON);
1422 if (!amdgpu_atombios_dp_get_dpcd(amdgpu_connector))
1423 ret = connector_status_connected;
1424 if (!amdgpu_dig_connector->edp_on)
1425 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1426 ATOM_TRANSMITTER_ACTION_POWER_OFF);
1427 } else if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1428 ENCODER_OBJECT_ID_NONE) {
1429 /* DP bridges are always DP */
1430 amdgpu_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1431 /* get the DPCD from the bridge */
1432 amdgpu_atombios_dp_get_dpcd(amdgpu_connector);
1433
1434 if (encoder) {
1435 /* setup ddc on the bridge */
1436 amdgpu_atombios_encoder_setup_ext_encoder_ddc(encoder);
1437 /* bridge chips are always aux */
1438 /* try DDC */
1439 if (amdgpu_display_ddc_probe(amdgpu_connector, true))
1440 ret = connector_status_connected;
1441 else if (amdgpu_connector->dac_load_detect) { /* try load detection */
1442 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1443
1444 ret = encoder_funcs->detect(encoder, connector);
1445 }
1446 }
1447 } else {
1448 amdgpu_dig_connector->dp_sink_type =
1449 amdgpu_atombios_dp_get_sinktype(amdgpu_connector);
1450 if (amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd)) {
1451 ret = connector_status_connected;
1452 if (amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
1453 amdgpu_atombios_dp_get_dpcd(amdgpu_connector);
1454 } else {
1455 if (amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) {
1456 if (!amdgpu_atombios_dp_get_dpcd(amdgpu_connector))
1457 ret = connector_status_connected;
1458 } else {
1459 /* try non-aux ddc (DP to DVI/HDMI/etc. adapter) */
1460 if (amdgpu_display_ddc_probe(amdgpu_connector,
1461 false))
1462 ret = connector_status_connected;
1463 }
1464 }
1465 }
1466
1467 amdgpu_connector_update_scratch_regs(connector, ret);
1468out:
1469 if (!drm_kms_helper_is_poll_worker()) {
1470 pm_runtime_mark_last_busy(connector->dev->dev);
1471 pm_runtime_put_autosuspend(connector->dev->dev);
1472 }
1473
1474 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
1475 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
1476 drm_dp_set_subconnector_property(&amdgpu_connector->base,
1477 ret,
1478 amdgpu_dig_connector->dpcd,
1479 amdgpu_dig_connector->downstream_ports);
1480 return ret;
1481}
1482
1483static enum drm_mode_status amdgpu_connector_dp_mode_valid(struct drm_connector *connector,
1484 struct drm_display_mode *mode)
1485{
1486 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1487 struct amdgpu_connector_atom_dig *amdgpu_dig_connector = amdgpu_connector->con_priv;
1488
1489 /* XXX check mode bandwidth */
1490
1491 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1492 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1493 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
1494
1495 if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
1496 return MODE_PANEL;
1497
1498 if (encoder) {
1499 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1500 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
1501
1502 /* AVIVO hardware supports downscaling modes larger than the panel
1503 * to the panel size, but I'm not sure this is desirable.
1504 */
1505 if ((mode->hdisplay > native_mode->hdisplay) ||
1506 (mode->vdisplay > native_mode->vdisplay))
1507 return MODE_PANEL;
1508
1509 /* if scaling is disabled, block non-native modes */
1510 if (amdgpu_encoder->rmx_type == RMX_OFF) {
1511 if ((mode->hdisplay != native_mode->hdisplay) ||
1512 (mode->vdisplay != native_mode->vdisplay))
1513 return MODE_PANEL;
1514 }
1515 }
1516 return MODE_OK;
1517 } else {
1518 if ((amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1519 (amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
1520 return amdgpu_atombios_dp_mode_valid_helper(connector, mode);
1521 } else {
1522 if (connector->display_info.is_hdmi) {
1523 /* HDMI 1.3+ supports max clock of 340 Mhz */
1524 if (mode->clock > 340000)
1525 return MODE_CLOCK_HIGH;
1526 } else {
1527 if (mode->clock > 165000)
1528 return MODE_CLOCK_HIGH;
1529 }
1530 }
1531 }
1532
1533 return MODE_OK;
1534}
1535
1536static int
1537amdgpu_connector_late_register(struct drm_connector *connector)
1538{
1539 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1540 int r = 0;
1541
1542 if (amdgpu_connector->ddc_bus->has_aux) {
1543 amdgpu_connector->ddc_bus->aux.dev = amdgpu_connector->base.kdev;
1544 r = drm_dp_aux_register(&amdgpu_connector->ddc_bus->aux);
1545 }
1546
1547 return r;
1548}
1549
1550static const struct drm_connector_helper_funcs amdgpu_connector_dp_helper_funcs = {
1551 .get_modes = amdgpu_connector_dp_get_modes,
1552 .mode_valid = amdgpu_connector_dp_mode_valid,
1553 .best_encoder = amdgpu_connector_dvi_encoder,
1554};
1555
1556static const struct drm_connector_funcs amdgpu_connector_dp_funcs = {
1557 .dpms = drm_helper_connector_dpms,
1558 .detect = amdgpu_connector_dp_detect,
1559 .fill_modes = drm_helper_probe_single_connector_modes,
1560 .set_property = amdgpu_connector_set_property,
1561 .early_unregister = amdgpu_connector_unregister,
1562 .destroy = amdgpu_connector_destroy,
1563 .force = amdgpu_connector_dvi_force,
1564 .late_register = amdgpu_connector_late_register,
1565};
1566
1567static const struct drm_connector_funcs amdgpu_connector_edp_funcs = {
1568 .dpms = drm_helper_connector_dpms,
1569 .detect = amdgpu_connector_dp_detect,
1570 .fill_modes = drm_helper_probe_single_connector_modes,
1571 .set_property = amdgpu_connector_set_lcd_property,
1572 .early_unregister = amdgpu_connector_unregister,
1573 .destroy = amdgpu_connector_destroy,
1574 .force = amdgpu_connector_dvi_force,
1575 .late_register = amdgpu_connector_late_register,
1576};
1577
1578void
1579amdgpu_connector_add(struct amdgpu_device *adev,
1580 uint32_t connector_id,
1581 uint32_t supported_device,
1582 int connector_type,
1583 struct amdgpu_i2c_bus_rec *i2c_bus,
1584 uint16_t connector_object_id,
1585 struct amdgpu_hpd *hpd,
1586 struct amdgpu_router *router)
1587{
1588 struct drm_device *dev = adev_to_drm(adev);
1589 struct drm_connector *connector;
1590 struct drm_connector_list_iter iter;
1591 struct amdgpu_connector *amdgpu_connector;
1592 struct amdgpu_connector_atom_dig *amdgpu_dig_connector;
1593 struct drm_encoder *encoder;
1594 struct amdgpu_encoder *amdgpu_encoder;
1595 struct i2c_adapter *ddc = NULL;
1596 uint32_t subpixel_order = SubPixelNone;
1597 bool shared_ddc = false;
1598 bool is_dp_bridge = false;
1599 bool has_aux = false;
1600
1601 if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1602 return;
1603
1604 /* see if we already added it */
1605 drm_connector_list_iter_begin(dev, &iter);
1606 drm_for_each_connector_iter(connector, &iter) {
1607 amdgpu_connector = to_amdgpu_connector(connector);
1608 if (amdgpu_connector->connector_id == connector_id) {
1609 amdgpu_connector->devices |= supported_device;
1610 drm_connector_list_iter_end(&iter);
1611 return;
1612 }
1613 if (amdgpu_connector->ddc_bus && i2c_bus->valid) {
1614 if (amdgpu_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1615 amdgpu_connector->shared_ddc = true;
1616 shared_ddc = true;
1617 }
1618 if (amdgpu_connector->router_bus && router->ddc_valid &&
1619 (amdgpu_connector->router.router_id == router->router_id)) {
1620 amdgpu_connector->shared_ddc = false;
1621 shared_ddc = false;
1622 }
1623 }
1624 }
1625 drm_connector_list_iter_end(&iter);
1626
1627 /* check if it's a dp bridge */
1628 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1629 amdgpu_encoder = to_amdgpu_encoder(encoder);
1630 if (amdgpu_encoder->devices & supported_device) {
1631 switch (amdgpu_encoder->encoder_id) {
1632 case ENCODER_OBJECT_ID_TRAVIS:
1633 case ENCODER_OBJECT_ID_NUTMEG:
1634 is_dp_bridge = true;
1635 break;
1636 default:
1637 break;
1638 }
1639 }
1640 }
1641
1642 amdgpu_connector = kzalloc(sizeof(struct amdgpu_connector), GFP_KERNEL);
1643 if (!amdgpu_connector)
1644 return;
1645
1646 connector = &amdgpu_connector->base;
1647
1648 amdgpu_connector->connector_id = connector_id;
1649 amdgpu_connector->devices = supported_device;
1650 amdgpu_connector->shared_ddc = shared_ddc;
1651 amdgpu_connector->connector_object_id = connector_object_id;
1652 amdgpu_connector->hpd = *hpd;
1653
1654 amdgpu_connector->router = *router;
1655 if (router->ddc_valid || router->cd_valid) {
1656 amdgpu_connector->router_bus = amdgpu_i2c_lookup(adev, &router->i2c_info);
1657 if (!amdgpu_connector->router_bus)
1658 DRM_ERROR("Failed to assign router i2c bus! Check dmesg for i2c errors.\n");
1659 }
1660
1661 if (is_dp_bridge) {
1662 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1663 if (!amdgpu_dig_connector)
1664 goto failed;
1665 amdgpu_connector->con_priv = amdgpu_dig_connector;
1666 if (i2c_bus->valid) {
1667 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1668 if (amdgpu_connector->ddc_bus) {
1669 has_aux = true;
1670 ddc = &amdgpu_connector->ddc_bus->adapter;
1671 } else {
1672 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1673 }
1674 }
1675 switch (connector_type) {
1676 case DRM_MODE_CONNECTOR_VGA:
1677 case DRM_MODE_CONNECTOR_DVIA:
1678 default:
1679 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1680 &amdgpu_connector_dp_funcs,
1681 connector_type,
1682 ddc);
1683 drm_connector_helper_add(&amdgpu_connector->base,
1684 &amdgpu_connector_dp_helper_funcs);
1685 connector->interlace_allowed = true;
1686 connector->doublescan_allowed = true;
1687 amdgpu_connector->dac_load_detect = true;
1688 drm_object_attach_property(&amdgpu_connector->base.base,
1689 adev->mode_info.load_detect_property,
1690 1);
1691 drm_object_attach_property(&amdgpu_connector->base.base,
1692 dev->mode_config.scaling_mode_property,
1693 DRM_MODE_SCALE_NONE);
1694 break;
1695 case DRM_MODE_CONNECTOR_DVII:
1696 case DRM_MODE_CONNECTOR_DVID:
1697 case DRM_MODE_CONNECTOR_HDMIA:
1698 case DRM_MODE_CONNECTOR_HDMIB:
1699 case DRM_MODE_CONNECTOR_DisplayPort:
1700 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1701 &amdgpu_connector_dp_funcs,
1702 connector_type,
1703 ddc);
1704 drm_connector_helper_add(&amdgpu_connector->base,
1705 &amdgpu_connector_dp_helper_funcs);
1706 drm_object_attach_property(&amdgpu_connector->base.base,
1707 adev->mode_info.underscan_property,
1708 UNDERSCAN_OFF);
1709 drm_object_attach_property(&amdgpu_connector->base.base,
1710 adev->mode_info.underscan_hborder_property,
1711 0);
1712 drm_object_attach_property(&amdgpu_connector->base.base,
1713 adev->mode_info.underscan_vborder_property,
1714 0);
1715
1716 drm_object_attach_property(&amdgpu_connector->base.base,
1717 dev->mode_config.scaling_mode_property,
1718 DRM_MODE_SCALE_NONE);
1719
1720 drm_object_attach_property(&amdgpu_connector->base.base,
1721 adev->mode_info.dither_property,
1722 AMDGPU_FMT_DITHER_DISABLE);
1723
1724 if (amdgpu_audio != 0) {
1725 drm_object_attach_property(&amdgpu_connector->base.base,
1726 adev->mode_info.audio_property,
1727 AMDGPU_AUDIO_AUTO);
1728 amdgpu_connector->audio = AMDGPU_AUDIO_AUTO;
1729 }
1730
1731 subpixel_order = SubPixelHorizontalRGB;
1732 connector->interlace_allowed = true;
1733 if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1734 connector->doublescan_allowed = true;
1735 else
1736 connector->doublescan_allowed = false;
1737 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1738 amdgpu_connector->dac_load_detect = true;
1739 drm_object_attach_property(&amdgpu_connector->base.base,
1740 adev->mode_info.load_detect_property,
1741 1);
1742 }
1743 break;
1744 case DRM_MODE_CONNECTOR_LVDS:
1745 case DRM_MODE_CONNECTOR_eDP:
1746 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1747 &amdgpu_connector_edp_funcs,
1748 connector_type,
1749 ddc);
1750 drm_connector_helper_add(&amdgpu_connector->base,
1751 &amdgpu_connector_dp_helper_funcs);
1752 drm_object_attach_property(&amdgpu_connector->base.base,
1753 dev->mode_config.scaling_mode_property,
1754 DRM_MODE_SCALE_FULLSCREEN);
1755 subpixel_order = SubPixelHorizontalRGB;
1756 connector->interlace_allowed = false;
1757 connector->doublescan_allowed = false;
1758 break;
1759 }
1760 } else {
1761 switch (connector_type) {
1762 case DRM_MODE_CONNECTOR_VGA:
1763 if (i2c_bus->valid) {
1764 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1765 if (!amdgpu_connector->ddc_bus)
1766 DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1767 else
1768 ddc = &amdgpu_connector->ddc_bus->adapter;
1769 }
1770 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1771 &amdgpu_connector_vga_funcs,
1772 connector_type,
1773 ddc);
1774 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_vga_helper_funcs);
1775 amdgpu_connector->dac_load_detect = true;
1776 drm_object_attach_property(&amdgpu_connector->base.base,
1777 adev->mode_info.load_detect_property,
1778 1);
1779 drm_object_attach_property(&amdgpu_connector->base.base,
1780 dev->mode_config.scaling_mode_property,
1781 DRM_MODE_SCALE_NONE);
1782 /* no HPD on analog connectors */
1783 amdgpu_connector->hpd.hpd = AMDGPU_HPD_NONE;
1784 connector->interlace_allowed = true;
1785 connector->doublescan_allowed = true;
1786 break;
1787 case DRM_MODE_CONNECTOR_DVIA:
1788 if (i2c_bus->valid) {
1789 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1790 if (!amdgpu_connector->ddc_bus)
1791 DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1792 else
1793 ddc = &amdgpu_connector->ddc_bus->adapter;
1794 }
1795 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1796 &amdgpu_connector_vga_funcs,
1797 connector_type,
1798 ddc);
1799 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_vga_helper_funcs);
1800 amdgpu_connector->dac_load_detect = true;
1801 drm_object_attach_property(&amdgpu_connector->base.base,
1802 adev->mode_info.load_detect_property,
1803 1);
1804 drm_object_attach_property(&amdgpu_connector->base.base,
1805 dev->mode_config.scaling_mode_property,
1806 DRM_MODE_SCALE_NONE);
1807 /* no HPD on analog connectors */
1808 amdgpu_connector->hpd.hpd = AMDGPU_HPD_NONE;
1809 connector->interlace_allowed = true;
1810 connector->doublescan_allowed = true;
1811 break;
1812 case DRM_MODE_CONNECTOR_DVII:
1813 case DRM_MODE_CONNECTOR_DVID:
1814 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1815 if (!amdgpu_dig_connector)
1816 goto failed;
1817 amdgpu_connector->con_priv = amdgpu_dig_connector;
1818 if (i2c_bus->valid) {
1819 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1820 if (!amdgpu_connector->ddc_bus)
1821 DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1822 else
1823 ddc = &amdgpu_connector->ddc_bus->adapter;
1824 }
1825 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1826 &amdgpu_connector_dvi_funcs,
1827 connector_type,
1828 ddc);
1829 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dvi_helper_funcs);
1830 subpixel_order = SubPixelHorizontalRGB;
1831 drm_object_attach_property(&amdgpu_connector->base.base,
1832 adev->mode_info.coherent_mode_property,
1833 1);
1834 drm_object_attach_property(&amdgpu_connector->base.base,
1835 adev->mode_info.underscan_property,
1836 UNDERSCAN_OFF);
1837 drm_object_attach_property(&amdgpu_connector->base.base,
1838 adev->mode_info.underscan_hborder_property,
1839 0);
1840 drm_object_attach_property(&amdgpu_connector->base.base,
1841 adev->mode_info.underscan_vborder_property,
1842 0);
1843 drm_object_attach_property(&amdgpu_connector->base.base,
1844 dev->mode_config.scaling_mode_property,
1845 DRM_MODE_SCALE_NONE);
1846
1847 if (amdgpu_audio != 0) {
1848 drm_object_attach_property(&amdgpu_connector->base.base,
1849 adev->mode_info.audio_property,
1850 AMDGPU_AUDIO_AUTO);
1851 amdgpu_connector->audio = AMDGPU_AUDIO_AUTO;
1852 }
1853 drm_object_attach_property(&amdgpu_connector->base.base,
1854 adev->mode_info.dither_property,
1855 AMDGPU_FMT_DITHER_DISABLE);
1856 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1857 amdgpu_connector->dac_load_detect = true;
1858 drm_object_attach_property(&amdgpu_connector->base.base,
1859 adev->mode_info.load_detect_property,
1860 1);
1861 }
1862 connector->interlace_allowed = true;
1863 if (connector_type == DRM_MODE_CONNECTOR_DVII)
1864 connector->doublescan_allowed = true;
1865 else
1866 connector->doublescan_allowed = false;
1867 break;
1868 case DRM_MODE_CONNECTOR_HDMIA:
1869 case DRM_MODE_CONNECTOR_HDMIB:
1870 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1871 if (!amdgpu_dig_connector)
1872 goto failed;
1873 amdgpu_connector->con_priv = amdgpu_dig_connector;
1874 if (i2c_bus->valid) {
1875 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1876 if (!amdgpu_connector->ddc_bus)
1877 DRM_ERROR("HDMI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1878 else
1879 ddc = &amdgpu_connector->ddc_bus->adapter;
1880 }
1881 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1882 &amdgpu_connector_dvi_funcs,
1883 connector_type,
1884 ddc);
1885 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dvi_helper_funcs);
1886 drm_object_attach_property(&amdgpu_connector->base.base,
1887 adev->mode_info.coherent_mode_property,
1888 1);
1889 drm_object_attach_property(&amdgpu_connector->base.base,
1890 adev->mode_info.underscan_property,
1891 UNDERSCAN_OFF);
1892 drm_object_attach_property(&amdgpu_connector->base.base,
1893 adev->mode_info.underscan_hborder_property,
1894 0);
1895 drm_object_attach_property(&amdgpu_connector->base.base,
1896 adev->mode_info.underscan_vborder_property,
1897 0);
1898 drm_object_attach_property(&amdgpu_connector->base.base,
1899 dev->mode_config.scaling_mode_property,
1900 DRM_MODE_SCALE_NONE);
1901 if (amdgpu_audio != 0) {
1902 drm_object_attach_property(&amdgpu_connector->base.base,
1903 adev->mode_info.audio_property,
1904 AMDGPU_AUDIO_AUTO);
1905 amdgpu_connector->audio = AMDGPU_AUDIO_AUTO;
1906 }
1907 drm_object_attach_property(&amdgpu_connector->base.base,
1908 adev->mode_info.dither_property,
1909 AMDGPU_FMT_DITHER_DISABLE);
1910 subpixel_order = SubPixelHorizontalRGB;
1911 connector->interlace_allowed = true;
1912 if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1913 connector->doublescan_allowed = true;
1914 else
1915 connector->doublescan_allowed = false;
1916 break;
1917 case DRM_MODE_CONNECTOR_DisplayPort:
1918 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1919 if (!amdgpu_dig_connector)
1920 goto failed;
1921 amdgpu_connector->con_priv = amdgpu_dig_connector;
1922 if (i2c_bus->valid) {
1923 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1924 if (amdgpu_connector->ddc_bus) {
1925 has_aux = true;
1926 ddc = &amdgpu_connector->ddc_bus->adapter;
1927 } else {
1928 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1929 }
1930 }
1931 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1932 &amdgpu_connector_dp_funcs,
1933 connector_type,
1934 ddc);
1935 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dp_helper_funcs);
1936 subpixel_order = SubPixelHorizontalRGB;
1937 drm_object_attach_property(&amdgpu_connector->base.base,
1938 adev->mode_info.coherent_mode_property,
1939 1);
1940 drm_object_attach_property(&amdgpu_connector->base.base,
1941 adev->mode_info.underscan_property,
1942 UNDERSCAN_OFF);
1943 drm_object_attach_property(&amdgpu_connector->base.base,
1944 adev->mode_info.underscan_hborder_property,
1945 0);
1946 drm_object_attach_property(&amdgpu_connector->base.base,
1947 adev->mode_info.underscan_vborder_property,
1948 0);
1949 drm_object_attach_property(&amdgpu_connector->base.base,
1950 dev->mode_config.scaling_mode_property,
1951 DRM_MODE_SCALE_NONE);
1952 if (amdgpu_audio != 0) {
1953 drm_object_attach_property(&amdgpu_connector->base.base,
1954 adev->mode_info.audio_property,
1955 AMDGPU_AUDIO_AUTO);
1956 amdgpu_connector->audio = AMDGPU_AUDIO_AUTO;
1957 }
1958 drm_object_attach_property(&amdgpu_connector->base.base,
1959 adev->mode_info.dither_property,
1960 AMDGPU_FMT_DITHER_DISABLE);
1961 connector->interlace_allowed = true;
1962 /* in theory with a DP to VGA converter... */
1963 connector->doublescan_allowed = false;
1964 break;
1965 case DRM_MODE_CONNECTOR_eDP:
1966 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1967 if (!amdgpu_dig_connector)
1968 goto failed;
1969 amdgpu_connector->con_priv = amdgpu_dig_connector;
1970 if (i2c_bus->valid) {
1971 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1972 if (amdgpu_connector->ddc_bus) {
1973 has_aux = true;
1974 ddc = &amdgpu_connector->ddc_bus->adapter;
1975 } else {
1976 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1977 }
1978 }
1979 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
1980 &amdgpu_connector_edp_funcs,
1981 connector_type,
1982 ddc);
1983 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dp_helper_funcs);
1984 drm_object_attach_property(&amdgpu_connector->base.base,
1985 dev->mode_config.scaling_mode_property,
1986 DRM_MODE_SCALE_FULLSCREEN);
1987 subpixel_order = SubPixelHorizontalRGB;
1988 connector->interlace_allowed = false;
1989 connector->doublescan_allowed = false;
1990 break;
1991 case DRM_MODE_CONNECTOR_LVDS:
1992 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1993 if (!amdgpu_dig_connector)
1994 goto failed;
1995 amdgpu_connector->con_priv = amdgpu_dig_connector;
1996 if (i2c_bus->valid) {
1997 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1998 if (!amdgpu_connector->ddc_bus)
1999 DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
2000 else
2001 ddc = &amdgpu_connector->ddc_bus->adapter;
2002 }
2003 drm_connector_init_with_ddc(dev, &amdgpu_connector->base,
2004 &amdgpu_connector_lvds_funcs,
2005 connector_type,
2006 ddc);
2007 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_lvds_helper_funcs);
2008 drm_object_attach_property(&amdgpu_connector->base.base,
2009 dev->mode_config.scaling_mode_property,
2010 DRM_MODE_SCALE_FULLSCREEN);
2011 subpixel_order = SubPixelHorizontalRGB;
2012 connector->interlace_allowed = false;
2013 connector->doublescan_allowed = false;
2014 break;
2015 }
2016 }
2017
2018 if (amdgpu_connector->hpd.hpd == AMDGPU_HPD_NONE) {
2019 if (i2c_bus->valid) {
2020 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
2021 DRM_CONNECTOR_POLL_DISCONNECT;
2022 }
2023 } else
2024 connector->polled = DRM_CONNECTOR_POLL_HPD;
2025
2026 connector->display_info.subpixel_order = subpixel_order;
2027
2028 if (has_aux)
2029 amdgpu_atombios_dp_aux_init(amdgpu_connector);
2030
2031 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
2032 connector_type == DRM_MODE_CONNECTOR_eDP) {
2033 drm_connector_attach_dp_subconnector_property(&amdgpu_connector->base);
2034 }
2035
2036 return;
2037
2038failed:
2039 drm_connector_cleanup(connector);
2040 kfree(connector);
2041}
1/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_edid.h>
28#include <drm/drm_crtc_helper.h>
29#include <drm/drm_fb_helper.h>
30#include <drm/amdgpu_drm.h>
31#include "amdgpu.h"
32#include "atom.h"
33#include "atombios_encoders.h"
34#include "atombios_dp.h"
35#include "amdgpu_connectors.h"
36#include "amdgpu_i2c.h"
37
38#include <linux/pm_runtime.h>
39
40void amdgpu_connector_hotplug(struct drm_connector *connector)
41{
42 struct drm_device *dev = connector->dev;
43 struct amdgpu_device *adev = dev->dev_private;
44 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
45
46 /* bail if the connector does not have hpd pin, e.g.,
47 * VGA, TV, etc.
48 */
49 if (amdgpu_connector->hpd.hpd == AMDGPU_HPD_NONE)
50 return;
51
52 amdgpu_display_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
53
54 /* if the connector is already off, don't turn it back on */
55 if (connector->dpms != DRM_MODE_DPMS_ON)
56 return;
57
58 /* just deal with DP (not eDP) here. */
59 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
60 struct amdgpu_connector_atom_dig *dig_connector =
61 amdgpu_connector->con_priv;
62
63 /* if existing sink type was not DP no need to retrain */
64 if (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT)
65 return;
66
67 /* first get sink type as it may be reset after (un)plug */
68 dig_connector->dp_sink_type = amdgpu_atombios_dp_get_sinktype(amdgpu_connector);
69 /* don't do anything if sink is not display port, i.e.,
70 * passive dp->(dvi|hdmi) adaptor
71 */
72 if (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT &&
73 amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd) &&
74 amdgpu_atombios_dp_needs_link_train(amdgpu_connector)) {
75 /* Don't start link training before we have the DPCD */
76 if (amdgpu_atombios_dp_get_dpcd(amdgpu_connector))
77 return;
78
79 /* Turn the connector off and back on immediately, which
80 * will trigger link training
81 */
82 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
83 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
84 }
85 }
86}
87
88static void amdgpu_connector_property_change_mode(struct drm_encoder *encoder)
89{
90 struct drm_crtc *crtc = encoder->crtc;
91
92 if (crtc && crtc->enabled) {
93 drm_crtc_helper_set_mode(crtc, &crtc->mode,
94 crtc->x, crtc->y, crtc->primary->fb);
95 }
96}
97
98int amdgpu_connector_get_monitor_bpc(struct drm_connector *connector)
99{
100 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
101 struct amdgpu_connector_atom_dig *dig_connector;
102 int bpc = 8;
103 unsigned mode_clock, max_tmds_clock;
104
105 switch (connector->connector_type) {
106 case DRM_MODE_CONNECTOR_DVII:
107 case DRM_MODE_CONNECTOR_HDMIB:
108 if (amdgpu_connector->use_digital) {
109 if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
110 if (connector->display_info.bpc)
111 bpc = connector->display_info.bpc;
112 }
113 }
114 break;
115 case DRM_MODE_CONNECTOR_DVID:
116 case DRM_MODE_CONNECTOR_HDMIA:
117 if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
118 if (connector->display_info.bpc)
119 bpc = connector->display_info.bpc;
120 }
121 break;
122 case DRM_MODE_CONNECTOR_DisplayPort:
123 dig_connector = amdgpu_connector->con_priv;
124 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
125 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) ||
126 drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
127 if (connector->display_info.bpc)
128 bpc = connector->display_info.bpc;
129 }
130 break;
131 case DRM_MODE_CONNECTOR_eDP:
132 case DRM_MODE_CONNECTOR_LVDS:
133 if (connector->display_info.bpc)
134 bpc = connector->display_info.bpc;
135 else {
136 const struct drm_connector_helper_funcs *connector_funcs =
137 connector->helper_private;
138 struct drm_encoder *encoder = connector_funcs->best_encoder(connector);
139 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
140 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
141
142 if (dig->lcd_misc & ATOM_PANEL_MISC_V13_6BIT_PER_COLOR)
143 bpc = 6;
144 else if (dig->lcd_misc & ATOM_PANEL_MISC_V13_8BIT_PER_COLOR)
145 bpc = 8;
146 }
147 break;
148 }
149
150 if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
151 /*
152 * Pre DCE-8 hw can't handle > 12 bpc, and more than 12 bpc doesn't make
153 * much sense without support for > 12 bpc framebuffers. RGB 4:4:4 at
154 * 12 bpc is always supported on hdmi deep color sinks, as this is
155 * required by the HDMI-1.3 spec. Clamp to a safe 12 bpc maximum.
156 */
157 if (bpc > 12) {
158 DRM_DEBUG("%s: HDMI deep color %d bpc unsupported. Using 12 bpc.\n",
159 connector->name, bpc);
160 bpc = 12;
161 }
162
163 /* Any defined maximum tmds clock limit we must not exceed? */
164 if (connector->display_info.max_tmds_clock > 0) {
165 /* mode_clock is clock in kHz for mode to be modeset on this connector */
166 mode_clock = amdgpu_connector->pixelclock_for_modeset;
167
168 /* Maximum allowable input clock in kHz */
169 max_tmds_clock = connector->display_info.max_tmds_clock;
170
171 DRM_DEBUG("%s: hdmi mode dotclock %d kHz, max tmds input clock %d kHz.\n",
172 connector->name, mode_clock, max_tmds_clock);
173
174 /* Check if bpc is within clock limit. Try to degrade gracefully otherwise */
175 if ((bpc == 12) && (mode_clock * 3/2 > max_tmds_clock)) {
176 if ((connector->display_info.edid_hdmi_dc_modes & DRM_EDID_HDMI_DC_30) &&
177 (mode_clock * 5/4 <= max_tmds_clock))
178 bpc = 10;
179 else
180 bpc = 8;
181
182 DRM_DEBUG("%s: HDMI deep color 12 bpc exceeds max tmds clock. Using %d bpc.\n",
183 connector->name, bpc);
184 }
185
186 if ((bpc == 10) && (mode_clock * 5/4 > max_tmds_clock)) {
187 bpc = 8;
188 DRM_DEBUG("%s: HDMI deep color 10 bpc exceeds max tmds clock. Using %d bpc.\n",
189 connector->name, bpc);
190 }
191 } else if (bpc > 8) {
192 /* max_tmds_clock missing, but hdmi spec mandates it for deep color. */
193 DRM_DEBUG("%s: Required max tmds clock for HDMI deep color missing. Using 8 bpc.\n",
194 connector->name);
195 bpc = 8;
196 }
197 }
198
199 if ((amdgpu_deep_color == 0) && (bpc > 8)) {
200 DRM_DEBUG("%s: Deep color disabled. Set amdgpu module param deep_color=1 to enable.\n",
201 connector->name);
202 bpc = 8;
203 }
204
205 DRM_DEBUG("%s: Display bpc=%d, returned bpc=%d\n",
206 connector->name, connector->display_info.bpc, bpc);
207
208 return bpc;
209}
210
211static void
212amdgpu_connector_update_scratch_regs(struct drm_connector *connector,
213 enum drm_connector_status status)
214{
215 struct drm_encoder *best_encoder = NULL;
216 struct drm_encoder *encoder = NULL;
217 const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
218 bool connected;
219 int i;
220
221 best_encoder = connector_funcs->best_encoder(connector);
222
223 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
224 if (connector->encoder_ids[i] == 0)
225 break;
226
227 encoder = drm_encoder_find(connector->dev, NULL,
228 connector->encoder_ids[i]);
229 if (!encoder)
230 continue;
231
232 if ((encoder == best_encoder) && (status == connector_status_connected))
233 connected = true;
234 else
235 connected = false;
236
237 amdgpu_atombios_encoder_set_bios_scratch_regs(connector, encoder, connected);
238
239 }
240}
241
242static struct drm_encoder *
243amdgpu_connector_find_encoder(struct drm_connector *connector,
244 int encoder_type)
245{
246 struct drm_encoder *encoder;
247 int i;
248
249 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
250 if (connector->encoder_ids[i] == 0)
251 break;
252 encoder = drm_encoder_find(connector->dev, NULL,
253 connector->encoder_ids[i]);
254 if (!encoder)
255 continue;
256
257 if (encoder->encoder_type == encoder_type)
258 return encoder;
259 }
260 return NULL;
261}
262
263struct edid *amdgpu_connector_edid(struct drm_connector *connector)
264{
265 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
266 struct drm_property_blob *edid_blob = connector->edid_blob_ptr;
267
268 if (amdgpu_connector->edid) {
269 return amdgpu_connector->edid;
270 } else if (edid_blob) {
271 struct edid *edid = kmemdup(edid_blob->data, edid_blob->length, GFP_KERNEL);
272 if (edid)
273 amdgpu_connector->edid = edid;
274 }
275 return amdgpu_connector->edid;
276}
277
278static struct edid *
279amdgpu_connector_get_hardcoded_edid(struct amdgpu_device *adev)
280{
281 struct edid *edid;
282
283 if (adev->mode_info.bios_hardcoded_edid) {
284 edid = kmalloc(adev->mode_info.bios_hardcoded_edid_size, GFP_KERNEL);
285 if (edid) {
286 memcpy((unsigned char *)edid,
287 (unsigned char *)adev->mode_info.bios_hardcoded_edid,
288 adev->mode_info.bios_hardcoded_edid_size);
289 return edid;
290 }
291 }
292 return NULL;
293}
294
295static void amdgpu_connector_get_edid(struct drm_connector *connector)
296{
297 struct drm_device *dev = connector->dev;
298 struct amdgpu_device *adev = dev->dev_private;
299 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
300
301 if (amdgpu_connector->edid)
302 return;
303
304 /* on hw with routers, select right port */
305 if (amdgpu_connector->router.ddc_valid)
306 amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
307
308 if ((amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
309 ENCODER_OBJECT_ID_NONE) &&
310 amdgpu_connector->ddc_bus->has_aux) {
311 amdgpu_connector->edid = drm_get_edid(connector,
312 &amdgpu_connector->ddc_bus->aux.ddc);
313 } else if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
314 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
315 struct amdgpu_connector_atom_dig *dig = amdgpu_connector->con_priv;
316
317 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
318 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
319 amdgpu_connector->ddc_bus->has_aux)
320 amdgpu_connector->edid = drm_get_edid(connector,
321 &amdgpu_connector->ddc_bus->aux.ddc);
322 else if (amdgpu_connector->ddc_bus)
323 amdgpu_connector->edid = drm_get_edid(connector,
324 &amdgpu_connector->ddc_bus->adapter);
325 } else if (amdgpu_connector->ddc_bus) {
326 amdgpu_connector->edid = drm_get_edid(connector,
327 &amdgpu_connector->ddc_bus->adapter);
328 }
329
330 if (!amdgpu_connector->edid) {
331 /* some laptops provide a hardcoded edid in rom for LCDs */
332 if (((connector->connector_type == DRM_MODE_CONNECTOR_LVDS) ||
333 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)))
334 amdgpu_connector->edid = amdgpu_connector_get_hardcoded_edid(adev);
335 }
336}
337
338static void amdgpu_connector_free_edid(struct drm_connector *connector)
339{
340 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
341
342 kfree(amdgpu_connector->edid);
343 amdgpu_connector->edid = NULL;
344}
345
346static int amdgpu_connector_ddc_get_modes(struct drm_connector *connector)
347{
348 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
349 int ret;
350
351 if (amdgpu_connector->edid) {
352 drm_mode_connector_update_edid_property(connector, amdgpu_connector->edid);
353 ret = drm_add_edid_modes(connector, amdgpu_connector->edid);
354 return ret;
355 }
356 drm_mode_connector_update_edid_property(connector, NULL);
357 return 0;
358}
359
360static struct drm_encoder *
361amdgpu_connector_best_single_encoder(struct drm_connector *connector)
362{
363 int enc_id = connector->encoder_ids[0];
364
365 /* pick the encoder ids */
366 if (enc_id)
367 return drm_encoder_find(connector->dev, NULL, enc_id);
368 return NULL;
369}
370
371static void amdgpu_get_native_mode(struct drm_connector *connector)
372{
373 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
374 struct amdgpu_encoder *amdgpu_encoder;
375
376 if (encoder == NULL)
377 return;
378
379 amdgpu_encoder = to_amdgpu_encoder(encoder);
380
381 if (!list_empty(&connector->probed_modes)) {
382 struct drm_display_mode *preferred_mode =
383 list_first_entry(&connector->probed_modes,
384 struct drm_display_mode, head);
385
386 amdgpu_encoder->native_mode = *preferred_mode;
387 } else {
388 amdgpu_encoder->native_mode.clock = 0;
389 }
390}
391
392static struct drm_display_mode *
393amdgpu_connector_lcd_native_mode(struct drm_encoder *encoder)
394{
395 struct drm_device *dev = encoder->dev;
396 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
397 struct drm_display_mode *mode = NULL;
398 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
399
400 if (native_mode->hdisplay != 0 &&
401 native_mode->vdisplay != 0 &&
402 native_mode->clock != 0) {
403 mode = drm_mode_duplicate(dev, native_mode);
404 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
405 drm_mode_set_name(mode);
406
407 DRM_DEBUG_KMS("Adding native panel mode %s\n", mode->name);
408 } else if (native_mode->hdisplay != 0 &&
409 native_mode->vdisplay != 0) {
410 /* mac laptops without an edid */
411 /* Note that this is not necessarily the exact panel mode,
412 * but an approximation based on the cvt formula. For these
413 * systems we should ideally read the mode info out of the
414 * registers or add a mode table, but this works and is much
415 * simpler.
416 */
417 mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
418 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
419 DRM_DEBUG_KMS("Adding cvt approximation of native panel mode %s\n", mode->name);
420 }
421 return mode;
422}
423
424static void amdgpu_connector_add_common_modes(struct drm_encoder *encoder,
425 struct drm_connector *connector)
426{
427 struct drm_device *dev = encoder->dev;
428 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
429 struct drm_display_mode *mode = NULL;
430 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
431 int i;
432 static const struct mode_size {
433 int w;
434 int h;
435 } common_modes[17] = {
436 { 640, 480},
437 { 720, 480},
438 { 800, 600},
439 { 848, 480},
440 {1024, 768},
441 {1152, 768},
442 {1280, 720},
443 {1280, 800},
444 {1280, 854},
445 {1280, 960},
446 {1280, 1024},
447 {1440, 900},
448 {1400, 1050},
449 {1680, 1050},
450 {1600, 1200},
451 {1920, 1080},
452 {1920, 1200}
453 };
454
455 for (i = 0; i < 17; i++) {
456 if (amdgpu_encoder->devices & (ATOM_DEVICE_TV_SUPPORT)) {
457 if (common_modes[i].w > 1024 ||
458 common_modes[i].h > 768)
459 continue;
460 }
461 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
462 if (common_modes[i].w > native_mode->hdisplay ||
463 common_modes[i].h > native_mode->vdisplay ||
464 (common_modes[i].w == native_mode->hdisplay &&
465 common_modes[i].h == native_mode->vdisplay))
466 continue;
467 }
468 if (common_modes[i].w < 320 || common_modes[i].h < 200)
469 continue;
470
471 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
472 drm_mode_probed_add(connector, mode);
473 }
474}
475
476static int amdgpu_connector_set_property(struct drm_connector *connector,
477 struct drm_property *property,
478 uint64_t val)
479{
480 struct drm_device *dev = connector->dev;
481 struct amdgpu_device *adev = dev->dev_private;
482 struct drm_encoder *encoder;
483 struct amdgpu_encoder *amdgpu_encoder;
484
485 if (property == adev->mode_info.coherent_mode_property) {
486 struct amdgpu_encoder_atom_dig *dig;
487 bool new_coherent_mode;
488
489 /* need to find digital encoder on connector */
490 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
491 if (!encoder)
492 return 0;
493
494 amdgpu_encoder = to_amdgpu_encoder(encoder);
495
496 if (!amdgpu_encoder->enc_priv)
497 return 0;
498
499 dig = amdgpu_encoder->enc_priv;
500 new_coherent_mode = val ? true : false;
501 if (dig->coherent_mode != new_coherent_mode) {
502 dig->coherent_mode = new_coherent_mode;
503 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
504 }
505 }
506
507 if (property == adev->mode_info.audio_property) {
508 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
509 /* need to find digital encoder on connector */
510 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
511 if (!encoder)
512 return 0;
513
514 amdgpu_encoder = to_amdgpu_encoder(encoder);
515
516 if (amdgpu_connector->audio != val) {
517 amdgpu_connector->audio = val;
518 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
519 }
520 }
521
522 if (property == adev->mode_info.dither_property) {
523 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
524 /* need to find digital encoder on connector */
525 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
526 if (!encoder)
527 return 0;
528
529 amdgpu_encoder = to_amdgpu_encoder(encoder);
530
531 if (amdgpu_connector->dither != val) {
532 amdgpu_connector->dither = val;
533 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
534 }
535 }
536
537 if (property == adev->mode_info.underscan_property) {
538 /* need to find digital encoder on connector */
539 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
540 if (!encoder)
541 return 0;
542
543 amdgpu_encoder = to_amdgpu_encoder(encoder);
544
545 if (amdgpu_encoder->underscan_type != val) {
546 amdgpu_encoder->underscan_type = val;
547 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
548 }
549 }
550
551 if (property == adev->mode_info.underscan_hborder_property) {
552 /* need to find digital encoder on connector */
553 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
554 if (!encoder)
555 return 0;
556
557 amdgpu_encoder = to_amdgpu_encoder(encoder);
558
559 if (amdgpu_encoder->underscan_hborder != val) {
560 amdgpu_encoder->underscan_hborder = val;
561 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
562 }
563 }
564
565 if (property == adev->mode_info.underscan_vborder_property) {
566 /* need to find digital encoder on connector */
567 encoder = amdgpu_connector_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
568 if (!encoder)
569 return 0;
570
571 amdgpu_encoder = to_amdgpu_encoder(encoder);
572
573 if (amdgpu_encoder->underscan_vborder != val) {
574 amdgpu_encoder->underscan_vborder = val;
575 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
576 }
577 }
578
579 if (property == adev->mode_info.load_detect_property) {
580 struct amdgpu_connector *amdgpu_connector =
581 to_amdgpu_connector(connector);
582
583 if (val == 0)
584 amdgpu_connector->dac_load_detect = false;
585 else
586 amdgpu_connector->dac_load_detect = true;
587 }
588
589 if (property == dev->mode_config.scaling_mode_property) {
590 enum amdgpu_rmx_type rmx_type;
591
592 if (connector->encoder) {
593 amdgpu_encoder = to_amdgpu_encoder(connector->encoder);
594 } else {
595 const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
596 amdgpu_encoder = to_amdgpu_encoder(connector_funcs->best_encoder(connector));
597 }
598
599 switch (val) {
600 default:
601 case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
602 case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
603 case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
604 case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
605 }
606 if (amdgpu_encoder->rmx_type == rmx_type)
607 return 0;
608
609 if ((rmx_type != DRM_MODE_SCALE_NONE) &&
610 (amdgpu_encoder->native_mode.clock == 0))
611 return 0;
612
613 amdgpu_encoder->rmx_type = rmx_type;
614
615 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
616 }
617
618 return 0;
619}
620
621static void
622amdgpu_connector_fixup_lcd_native_mode(struct drm_encoder *encoder,
623 struct drm_connector *connector)
624{
625 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
626 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
627 struct drm_display_mode *t, *mode;
628
629 /* If the EDID preferred mode doesn't match the native mode, use it */
630 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
631 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
632 if (mode->hdisplay != native_mode->hdisplay ||
633 mode->vdisplay != native_mode->vdisplay)
634 memcpy(native_mode, mode, sizeof(*mode));
635 }
636 }
637
638 /* Try to get native mode details from EDID if necessary */
639 if (!native_mode->clock) {
640 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
641 if (mode->hdisplay == native_mode->hdisplay &&
642 mode->vdisplay == native_mode->vdisplay) {
643 *native_mode = *mode;
644 drm_mode_set_crtcinfo(native_mode, CRTC_INTERLACE_HALVE_V);
645 DRM_DEBUG_KMS("Determined LVDS native mode details from EDID\n");
646 break;
647 }
648 }
649 }
650
651 if (!native_mode->clock) {
652 DRM_DEBUG_KMS("No LVDS native mode details, disabling RMX\n");
653 amdgpu_encoder->rmx_type = RMX_OFF;
654 }
655}
656
657static int amdgpu_connector_lvds_get_modes(struct drm_connector *connector)
658{
659 struct drm_encoder *encoder;
660 int ret = 0;
661 struct drm_display_mode *mode;
662
663 amdgpu_connector_get_edid(connector);
664 ret = amdgpu_connector_ddc_get_modes(connector);
665 if (ret > 0) {
666 encoder = amdgpu_connector_best_single_encoder(connector);
667 if (encoder) {
668 amdgpu_connector_fixup_lcd_native_mode(encoder, connector);
669 /* add scaled modes */
670 amdgpu_connector_add_common_modes(encoder, connector);
671 }
672 return ret;
673 }
674
675 encoder = amdgpu_connector_best_single_encoder(connector);
676 if (!encoder)
677 return 0;
678
679 /* we have no EDID modes */
680 mode = amdgpu_connector_lcd_native_mode(encoder);
681 if (mode) {
682 ret = 1;
683 drm_mode_probed_add(connector, mode);
684 /* add the width/height from vbios tables if available */
685 connector->display_info.width_mm = mode->width_mm;
686 connector->display_info.height_mm = mode->height_mm;
687 /* add scaled modes */
688 amdgpu_connector_add_common_modes(encoder, connector);
689 }
690
691 return ret;
692}
693
694static int amdgpu_connector_lvds_mode_valid(struct drm_connector *connector,
695 struct drm_display_mode *mode)
696{
697 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
698
699 if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
700 return MODE_PANEL;
701
702 if (encoder) {
703 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
704 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
705
706 /* AVIVO hardware supports downscaling modes larger than the panel
707 * to the panel size, but I'm not sure this is desirable.
708 */
709 if ((mode->hdisplay > native_mode->hdisplay) ||
710 (mode->vdisplay > native_mode->vdisplay))
711 return MODE_PANEL;
712
713 /* if scaling is disabled, block non-native modes */
714 if (amdgpu_encoder->rmx_type == RMX_OFF) {
715 if ((mode->hdisplay != native_mode->hdisplay) ||
716 (mode->vdisplay != native_mode->vdisplay))
717 return MODE_PANEL;
718 }
719 }
720
721 return MODE_OK;
722}
723
724static enum drm_connector_status
725amdgpu_connector_lvds_detect(struct drm_connector *connector, bool force)
726{
727 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
728 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
729 enum drm_connector_status ret = connector_status_disconnected;
730 int r;
731
732 if (!drm_kms_helper_is_poll_worker()) {
733 r = pm_runtime_get_sync(connector->dev->dev);
734 if (r < 0)
735 return connector_status_disconnected;
736 }
737
738 if (encoder) {
739 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
740 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
741
742 /* check if panel is valid */
743 if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
744 ret = connector_status_connected;
745
746 }
747
748 /* check for edid as well */
749 amdgpu_connector_get_edid(connector);
750 if (amdgpu_connector->edid)
751 ret = connector_status_connected;
752 /* check acpi lid status ??? */
753
754 amdgpu_connector_update_scratch_regs(connector, ret);
755
756 if (!drm_kms_helper_is_poll_worker()) {
757 pm_runtime_mark_last_busy(connector->dev->dev);
758 pm_runtime_put_autosuspend(connector->dev->dev);
759 }
760
761 return ret;
762}
763
764static void amdgpu_connector_unregister(struct drm_connector *connector)
765{
766 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
767
768 if (amdgpu_connector->ddc_bus && amdgpu_connector->ddc_bus->has_aux) {
769 drm_dp_aux_unregister(&amdgpu_connector->ddc_bus->aux);
770 amdgpu_connector->ddc_bus->has_aux = false;
771 }
772}
773
774static void amdgpu_connector_destroy(struct drm_connector *connector)
775{
776 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
777
778 amdgpu_connector_free_edid(connector);
779 kfree(amdgpu_connector->con_priv);
780 drm_connector_unregister(connector);
781 drm_connector_cleanup(connector);
782 kfree(connector);
783}
784
785static int amdgpu_connector_set_lcd_property(struct drm_connector *connector,
786 struct drm_property *property,
787 uint64_t value)
788{
789 struct drm_device *dev = connector->dev;
790 struct amdgpu_encoder *amdgpu_encoder;
791 enum amdgpu_rmx_type rmx_type;
792
793 DRM_DEBUG_KMS("\n");
794 if (property != dev->mode_config.scaling_mode_property)
795 return 0;
796
797 if (connector->encoder)
798 amdgpu_encoder = to_amdgpu_encoder(connector->encoder);
799 else {
800 const struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
801 amdgpu_encoder = to_amdgpu_encoder(connector_funcs->best_encoder(connector));
802 }
803
804 switch (value) {
805 case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
806 case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
807 case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
808 default:
809 case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
810 }
811 if (amdgpu_encoder->rmx_type == rmx_type)
812 return 0;
813
814 amdgpu_encoder->rmx_type = rmx_type;
815
816 amdgpu_connector_property_change_mode(&amdgpu_encoder->base);
817 return 0;
818}
819
820
821static const struct drm_connector_helper_funcs amdgpu_connector_lvds_helper_funcs = {
822 .get_modes = amdgpu_connector_lvds_get_modes,
823 .mode_valid = amdgpu_connector_lvds_mode_valid,
824 .best_encoder = amdgpu_connector_best_single_encoder,
825};
826
827static const struct drm_connector_funcs amdgpu_connector_lvds_funcs = {
828 .dpms = drm_helper_connector_dpms,
829 .detect = amdgpu_connector_lvds_detect,
830 .fill_modes = drm_helper_probe_single_connector_modes,
831 .early_unregister = amdgpu_connector_unregister,
832 .destroy = amdgpu_connector_destroy,
833 .set_property = amdgpu_connector_set_lcd_property,
834};
835
836static int amdgpu_connector_vga_get_modes(struct drm_connector *connector)
837{
838 int ret;
839
840 amdgpu_connector_get_edid(connector);
841 ret = amdgpu_connector_ddc_get_modes(connector);
842
843 return ret;
844}
845
846static int amdgpu_connector_vga_mode_valid(struct drm_connector *connector,
847 struct drm_display_mode *mode)
848{
849 struct drm_device *dev = connector->dev;
850 struct amdgpu_device *adev = dev->dev_private;
851
852 /* XXX check mode bandwidth */
853
854 if ((mode->clock / 10) > adev->clock.max_pixel_clock)
855 return MODE_CLOCK_HIGH;
856
857 return MODE_OK;
858}
859
860static enum drm_connector_status
861amdgpu_connector_vga_detect(struct drm_connector *connector, bool force)
862{
863 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
864 struct drm_encoder *encoder;
865 const struct drm_encoder_helper_funcs *encoder_funcs;
866 bool dret = false;
867 enum drm_connector_status ret = connector_status_disconnected;
868 int r;
869
870 if (!drm_kms_helper_is_poll_worker()) {
871 r = pm_runtime_get_sync(connector->dev->dev);
872 if (r < 0)
873 return connector_status_disconnected;
874 }
875
876 encoder = amdgpu_connector_best_single_encoder(connector);
877 if (!encoder)
878 ret = connector_status_disconnected;
879
880 if (amdgpu_connector->ddc_bus)
881 dret = amdgpu_display_ddc_probe(amdgpu_connector, false);
882 if (dret) {
883 amdgpu_connector->detected_by_load = false;
884 amdgpu_connector_free_edid(connector);
885 amdgpu_connector_get_edid(connector);
886
887 if (!amdgpu_connector->edid) {
888 DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
889 connector->name);
890 ret = connector_status_connected;
891 } else {
892 amdgpu_connector->use_digital =
893 !!(amdgpu_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
894
895 /* some oems have boards with separate digital and analog connectors
896 * with a shared ddc line (often vga + hdmi)
897 */
898 if (amdgpu_connector->use_digital && amdgpu_connector->shared_ddc) {
899 amdgpu_connector_free_edid(connector);
900 ret = connector_status_disconnected;
901 } else {
902 ret = connector_status_connected;
903 }
904 }
905 } else {
906
907 /* if we aren't forcing don't do destructive polling */
908 if (!force) {
909 /* only return the previous status if we last
910 * detected a monitor via load.
911 */
912 if (amdgpu_connector->detected_by_load)
913 ret = connector->status;
914 goto out;
915 }
916
917 if (amdgpu_connector->dac_load_detect && encoder) {
918 encoder_funcs = encoder->helper_private;
919 ret = encoder_funcs->detect(encoder, connector);
920 if (ret != connector_status_disconnected)
921 amdgpu_connector->detected_by_load = true;
922 }
923 }
924
925 amdgpu_connector_update_scratch_regs(connector, ret);
926
927out:
928 if (!drm_kms_helper_is_poll_worker()) {
929 pm_runtime_mark_last_busy(connector->dev->dev);
930 pm_runtime_put_autosuspend(connector->dev->dev);
931 }
932
933 return ret;
934}
935
936static const struct drm_connector_helper_funcs amdgpu_connector_vga_helper_funcs = {
937 .get_modes = amdgpu_connector_vga_get_modes,
938 .mode_valid = amdgpu_connector_vga_mode_valid,
939 .best_encoder = amdgpu_connector_best_single_encoder,
940};
941
942static const struct drm_connector_funcs amdgpu_connector_vga_funcs = {
943 .dpms = drm_helper_connector_dpms,
944 .detect = amdgpu_connector_vga_detect,
945 .fill_modes = drm_helper_probe_single_connector_modes,
946 .early_unregister = amdgpu_connector_unregister,
947 .destroy = amdgpu_connector_destroy,
948 .set_property = amdgpu_connector_set_property,
949};
950
951static bool
952amdgpu_connector_check_hpd_status_unchanged(struct drm_connector *connector)
953{
954 struct drm_device *dev = connector->dev;
955 struct amdgpu_device *adev = dev->dev_private;
956 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
957 enum drm_connector_status status;
958
959 if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE) {
960 if (amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd))
961 status = connector_status_connected;
962 else
963 status = connector_status_disconnected;
964 if (connector->status == status)
965 return true;
966 }
967
968 return false;
969}
970
971/*
972 * DVI is complicated
973 * Do a DDC probe, if DDC probe passes, get the full EDID so
974 * we can do analog/digital monitor detection at this point.
975 * If the monitor is an analog monitor or we got no DDC,
976 * we need to find the DAC encoder object for this connector.
977 * If we got no DDC, we do load detection on the DAC encoder object.
978 * If we got analog DDC or load detection passes on the DAC encoder
979 * we have to check if this analog encoder is shared with anyone else (TV)
980 * if its shared we have to set the other connector to disconnected.
981 */
982static enum drm_connector_status
983amdgpu_connector_dvi_detect(struct drm_connector *connector, bool force)
984{
985 struct drm_device *dev = connector->dev;
986 struct amdgpu_device *adev = dev->dev_private;
987 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
988 struct drm_encoder *encoder = NULL;
989 const struct drm_encoder_helper_funcs *encoder_funcs;
990 int i, r;
991 enum drm_connector_status ret = connector_status_disconnected;
992 bool dret = false, broken_edid = false;
993
994 if (!drm_kms_helper_is_poll_worker()) {
995 r = pm_runtime_get_sync(connector->dev->dev);
996 if (r < 0)
997 return connector_status_disconnected;
998 }
999
1000 if (!force && amdgpu_connector_check_hpd_status_unchanged(connector)) {
1001 ret = connector->status;
1002 goto exit;
1003 }
1004
1005 if (amdgpu_connector->ddc_bus)
1006 dret = amdgpu_display_ddc_probe(amdgpu_connector, false);
1007 if (dret) {
1008 amdgpu_connector->detected_by_load = false;
1009 amdgpu_connector_free_edid(connector);
1010 amdgpu_connector_get_edid(connector);
1011
1012 if (!amdgpu_connector->edid) {
1013 DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
1014 connector->name);
1015 ret = connector_status_connected;
1016 broken_edid = true; /* defer use_digital to later */
1017 } else {
1018 amdgpu_connector->use_digital =
1019 !!(amdgpu_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
1020
1021 /* some oems have boards with separate digital and analog connectors
1022 * with a shared ddc line (often vga + hdmi)
1023 */
1024 if ((!amdgpu_connector->use_digital) && amdgpu_connector->shared_ddc) {
1025 amdgpu_connector_free_edid(connector);
1026 ret = connector_status_disconnected;
1027 } else {
1028 ret = connector_status_connected;
1029 }
1030
1031 /* This gets complicated. We have boards with VGA + HDMI with a
1032 * shared DDC line and we have boards with DVI-D + HDMI with a shared
1033 * DDC line. The latter is more complex because with DVI<->HDMI adapters
1034 * you don't really know what's connected to which port as both are digital.
1035 */
1036 if (amdgpu_connector->shared_ddc && (ret == connector_status_connected)) {
1037 struct drm_connector *list_connector;
1038 struct amdgpu_connector *list_amdgpu_connector;
1039 list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
1040 if (connector == list_connector)
1041 continue;
1042 list_amdgpu_connector = to_amdgpu_connector(list_connector);
1043 if (list_amdgpu_connector->shared_ddc &&
1044 (list_amdgpu_connector->ddc_bus->rec.i2c_id ==
1045 amdgpu_connector->ddc_bus->rec.i2c_id)) {
1046 /* cases where both connectors are digital */
1047 if (list_connector->connector_type != DRM_MODE_CONNECTOR_VGA) {
1048 /* hpd is our only option in this case */
1049 if (!amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd)) {
1050 amdgpu_connector_free_edid(connector);
1051 ret = connector_status_disconnected;
1052 }
1053 }
1054 }
1055 }
1056 }
1057 }
1058 }
1059
1060 if ((ret == connector_status_connected) && (amdgpu_connector->use_digital == true))
1061 goto out;
1062
1063 /* DVI-D and HDMI-A are digital only */
1064 if ((connector->connector_type == DRM_MODE_CONNECTOR_DVID) ||
1065 (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA))
1066 goto out;
1067
1068 /* if we aren't forcing don't do destructive polling */
1069 if (!force) {
1070 /* only return the previous status if we last
1071 * detected a monitor via load.
1072 */
1073 if (amdgpu_connector->detected_by_load)
1074 ret = connector->status;
1075 goto out;
1076 }
1077
1078 /* find analog encoder */
1079 if (amdgpu_connector->dac_load_detect) {
1080 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1081 if (connector->encoder_ids[i] == 0)
1082 break;
1083
1084 encoder = drm_encoder_find(connector->dev, NULL, connector->encoder_ids[i]);
1085 if (!encoder)
1086 continue;
1087
1088 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC &&
1089 encoder->encoder_type != DRM_MODE_ENCODER_TVDAC)
1090 continue;
1091
1092 encoder_funcs = encoder->helper_private;
1093 if (encoder_funcs->detect) {
1094 if (!broken_edid) {
1095 if (ret != connector_status_connected) {
1096 /* deal with analog monitors without DDC */
1097 ret = encoder_funcs->detect(encoder, connector);
1098 if (ret == connector_status_connected) {
1099 amdgpu_connector->use_digital = false;
1100 }
1101 if (ret != connector_status_disconnected)
1102 amdgpu_connector->detected_by_load = true;
1103 }
1104 } else {
1105 enum drm_connector_status lret;
1106 /* assume digital unless load detected otherwise */
1107 amdgpu_connector->use_digital = true;
1108 lret = encoder_funcs->detect(encoder, connector);
1109 DRM_DEBUG_KMS("load_detect %x returned: %x\n",encoder->encoder_type,lret);
1110 if (lret == connector_status_connected)
1111 amdgpu_connector->use_digital = false;
1112 }
1113 break;
1114 }
1115 }
1116 }
1117
1118out:
1119 /* updated in get modes as well since we need to know if it's analog or digital */
1120 amdgpu_connector_update_scratch_regs(connector, ret);
1121
1122exit:
1123 if (!drm_kms_helper_is_poll_worker()) {
1124 pm_runtime_mark_last_busy(connector->dev->dev);
1125 pm_runtime_put_autosuspend(connector->dev->dev);
1126 }
1127
1128 return ret;
1129}
1130
1131/* okay need to be smart in here about which encoder to pick */
1132static struct drm_encoder *
1133amdgpu_connector_dvi_encoder(struct drm_connector *connector)
1134{
1135 int enc_id = connector->encoder_ids[0];
1136 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1137 struct drm_encoder *encoder;
1138 int i;
1139 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1140 if (connector->encoder_ids[i] == 0)
1141 break;
1142
1143 encoder = drm_encoder_find(connector->dev, NULL, connector->encoder_ids[i]);
1144 if (!encoder)
1145 continue;
1146
1147 if (amdgpu_connector->use_digital == true) {
1148 if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
1149 return encoder;
1150 } else {
1151 if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
1152 encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1153 return encoder;
1154 }
1155 }
1156
1157 /* see if we have a default encoder TODO */
1158
1159 /* then check use digitial */
1160 /* pick the first one */
1161 if (enc_id)
1162 return drm_encoder_find(connector->dev, NULL, enc_id);
1163 return NULL;
1164}
1165
1166static void amdgpu_connector_dvi_force(struct drm_connector *connector)
1167{
1168 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1169 if (connector->force == DRM_FORCE_ON)
1170 amdgpu_connector->use_digital = false;
1171 if (connector->force == DRM_FORCE_ON_DIGITAL)
1172 amdgpu_connector->use_digital = true;
1173}
1174
1175static int amdgpu_connector_dvi_mode_valid(struct drm_connector *connector,
1176 struct drm_display_mode *mode)
1177{
1178 struct drm_device *dev = connector->dev;
1179 struct amdgpu_device *adev = dev->dev_private;
1180 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1181
1182 /* XXX check mode bandwidth */
1183
1184 if (amdgpu_connector->use_digital && (mode->clock > 165000)) {
1185 if ((amdgpu_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
1186 (amdgpu_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
1187 (amdgpu_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B)) {
1188 return MODE_OK;
1189 } else if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
1190 /* HDMI 1.3+ supports max clock of 340 Mhz */
1191 if (mode->clock > 340000)
1192 return MODE_CLOCK_HIGH;
1193 else
1194 return MODE_OK;
1195 } else {
1196 return MODE_CLOCK_HIGH;
1197 }
1198 }
1199
1200 /* check against the max pixel clock */
1201 if ((mode->clock / 10) > adev->clock.max_pixel_clock)
1202 return MODE_CLOCK_HIGH;
1203
1204 return MODE_OK;
1205}
1206
1207static const struct drm_connector_helper_funcs amdgpu_connector_dvi_helper_funcs = {
1208 .get_modes = amdgpu_connector_vga_get_modes,
1209 .mode_valid = amdgpu_connector_dvi_mode_valid,
1210 .best_encoder = amdgpu_connector_dvi_encoder,
1211};
1212
1213static const struct drm_connector_funcs amdgpu_connector_dvi_funcs = {
1214 .dpms = drm_helper_connector_dpms,
1215 .detect = amdgpu_connector_dvi_detect,
1216 .fill_modes = drm_helper_probe_single_connector_modes,
1217 .set_property = amdgpu_connector_set_property,
1218 .early_unregister = amdgpu_connector_unregister,
1219 .destroy = amdgpu_connector_destroy,
1220 .force = amdgpu_connector_dvi_force,
1221};
1222
1223static int amdgpu_connector_dp_get_modes(struct drm_connector *connector)
1224{
1225 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1226 struct amdgpu_connector_atom_dig *amdgpu_dig_connector = amdgpu_connector->con_priv;
1227 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
1228 int ret;
1229
1230 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1231 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1232 struct drm_display_mode *mode;
1233
1234 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1235 if (!amdgpu_dig_connector->edp_on)
1236 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1237 ATOM_TRANSMITTER_ACTION_POWER_ON);
1238 amdgpu_connector_get_edid(connector);
1239 ret = amdgpu_connector_ddc_get_modes(connector);
1240 if (!amdgpu_dig_connector->edp_on)
1241 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1242 ATOM_TRANSMITTER_ACTION_POWER_OFF);
1243 } else {
1244 /* need to setup ddc on the bridge */
1245 if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1246 ENCODER_OBJECT_ID_NONE) {
1247 if (encoder)
1248 amdgpu_atombios_encoder_setup_ext_encoder_ddc(encoder);
1249 }
1250 amdgpu_connector_get_edid(connector);
1251 ret = amdgpu_connector_ddc_get_modes(connector);
1252 }
1253
1254 if (ret > 0) {
1255 if (encoder) {
1256 amdgpu_connector_fixup_lcd_native_mode(encoder, connector);
1257 /* add scaled modes */
1258 amdgpu_connector_add_common_modes(encoder, connector);
1259 }
1260 return ret;
1261 }
1262
1263 if (!encoder)
1264 return 0;
1265
1266 /* we have no EDID modes */
1267 mode = amdgpu_connector_lcd_native_mode(encoder);
1268 if (mode) {
1269 ret = 1;
1270 drm_mode_probed_add(connector, mode);
1271 /* add the width/height from vbios tables if available */
1272 connector->display_info.width_mm = mode->width_mm;
1273 connector->display_info.height_mm = mode->height_mm;
1274 /* add scaled modes */
1275 amdgpu_connector_add_common_modes(encoder, connector);
1276 }
1277 } else {
1278 /* need to setup ddc on the bridge */
1279 if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1280 ENCODER_OBJECT_ID_NONE) {
1281 if (encoder)
1282 amdgpu_atombios_encoder_setup_ext_encoder_ddc(encoder);
1283 }
1284 amdgpu_connector_get_edid(connector);
1285 ret = amdgpu_connector_ddc_get_modes(connector);
1286
1287 amdgpu_get_native_mode(connector);
1288 }
1289
1290 return ret;
1291}
1292
1293u16 amdgpu_connector_encoder_get_dp_bridge_encoder_id(struct drm_connector *connector)
1294{
1295 struct drm_encoder *encoder;
1296 struct amdgpu_encoder *amdgpu_encoder;
1297 int i;
1298
1299 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1300 if (connector->encoder_ids[i] == 0)
1301 break;
1302
1303 encoder = drm_encoder_find(connector->dev, NULL,
1304 connector->encoder_ids[i]);
1305 if (!encoder)
1306 continue;
1307
1308 amdgpu_encoder = to_amdgpu_encoder(encoder);
1309
1310 switch (amdgpu_encoder->encoder_id) {
1311 case ENCODER_OBJECT_ID_TRAVIS:
1312 case ENCODER_OBJECT_ID_NUTMEG:
1313 return amdgpu_encoder->encoder_id;
1314 default:
1315 break;
1316 }
1317 }
1318
1319 return ENCODER_OBJECT_ID_NONE;
1320}
1321
1322static bool amdgpu_connector_encoder_is_hbr2(struct drm_connector *connector)
1323{
1324 struct drm_encoder *encoder;
1325 struct amdgpu_encoder *amdgpu_encoder;
1326 int i;
1327 bool found = false;
1328
1329 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1330 if (connector->encoder_ids[i] == 0)
1331 break;
1332 encoder = drm_encoder_find(connector->dev, NULL,
1333 connector->encoder_ids[i]);
1334 if (!encoder)
1335 continue;
1336
1337 amdgpu_encoder = to_amdgpu_encoder(encoder);
1338 if (amdgpu_encoder->caps & ATOM_ENCODER_CAP_RECORD_HBR2)
1339 found = true;
1340 }
1341
1342 return found;
1343}
1344
1345bool amdgpu_connector_is_dp12_capable(struct drm_connector *connector)
1346{
1347 struct drm_device *dev = connector->dev;
1348 struct amdgpu_device *adev = dev->dev_private;
1349
1350 if ((adev->clock.default_dispclk >= 53900) &&
1351 amdgpu_connector_encoder_is_hbr2(connector)) {
1352 return true;
1353 }
1354
1355 return false;
1356}
1357
1358static enum drm_connector_status
1359amdgpu_connector_dp_detect(struct drm_connector *connector, bool force)
1360{
1361 struct drm_device *dev = connector->dev;
1362 struct amdgpu_device *adev = dev->dev_private;
1363 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1364 enum drm_connector_status ret = connector_status_disconnected;
1365 struct amdgpu_connector_atom_dig *amdgpu_dig_connector = amdgpu_connector->con_priv;
1366 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
1367 int r;
1368
1369 if (!drm_kms_helper_is_poll_worker()) {
1370 r = pm_runtime_get_sync(connector->dev->dev);
1371 if (r < 0)
1372 return connector_status_disconnected;
1373 }
1374
1375 if (!force && amdgpu_connector_check_hpd_status_unchanged(connector)) {
1376 ret = connector->status;
1377 goto out;
1378 }
1379
1380 amdgpu_connector_free_edid(connector);
1381
1382 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1383 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1384 if (encoder) {
1385 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1386 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
1387
1388 /* check if panel is valid */
1389 if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
1390 ret = connector_status_connected;
1391 }
1392 /* eDP is always DP */
1393 amdgpu_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1394 if (!amdgpu_dig_connector->edp_on)
1395 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1396 ATOM_TRANSMITTER_ACTION_POWER_ON);
1397 if (!amdgpu_atombios_dp_get_dpcd(amdgpu_connector))
1398 ret = connector_status_connected;
1399 if (!amdgpu_dig_connector->edp_on)
1400 amdgpu_atombios_encoder_set_edp_panel_power(connector,
1401 ATOM_TRANSMITTER_ACTION_POWER_OFF);
1402 } else if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1403 ENCODER_OBJECT_ID_NONE) {
1404 /* DP bridges are always DP */
1405 amdgpu_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1406 /* get the DPCD from the bridge */
1407 amdgpu_atombios_dp_get_dpcd(amdgpu_connector);
1408
1409 if (encoder) {
1410 /* setup ddc on the bridge */
1411 amdgpu_atombios_encoder_setup_ext_encoder_ddc(encoder);
1412 /* bridge chips are always aux */
1413 /* try DDC */
1414 if (amdgpu_display_ddc_probe(amdgpu_connector, true))
1415 ret = connector_status_connected;
1416 else if (amdgpu_connector->dac_load_detect) { /* try load detection */
1417 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1418 ret = encoder_funcs->detect(encoder, connector);
1419 }
1420 }
1421 } else {
1422 amdgpu_dig_connector->dp_sink_type =
1423 amdgpu_atombios_dp_get_sinktype(amdgpu_connector);
1424 if (amdgpu_display_hpd_sense(adev, amdgpu_connector->hpd.hpd)) {
1425 ret = connector_status_connected;
1426 if (amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
1427 amdgpu_atombios_dp_get_dpcd(amdgpu_connector);
1428 } else {
1429 if (amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) {
1430 if (!amdgpu_atombios_dp_get_dpcd(amdgpu_connector))
1431 ret = connector_status_connected;
1432 } else {
1433 /* try non-aux ddc (DP to DVI/HDMI/etc. adapter) */
1434 if (amdgpu_display_ddc_probe(amdgpu_connector,
1435 false))
1436 ret = connector_status_connected;
1437 }
1438 }
1439 }
1440
1441 amdgpu_connector_update_scratch_regs(connector, ret);
1442out:
1443 if (!drm_kms_helper_is_poll_worker()) {
1444 pm_runtime_mark_last_busy(connector->dev->dev);
1445 pm_runtime_put_autosuspend(connector->dev->dev);
1446 }
1447
1448 return ret;
1449}
1450
1451static int amdgpu_connector_dp_mode_valid(struct drm_connector *connector,
1452 struct drm_display_mode *mode)
1453{
1454 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
1455 struct amdgpu_connector_atom_dig *amdgpu_dig_connector = amdgpu_connector->con_priv;
1456
1457 /* XXX check mode bandwidth */
1458
1459 if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1460 (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1461 struct drm_encoder *encoder = amdgpu_connector_best_single_encoder(connector);
1462
1463 if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
1464 return MODE_PANEL;
1465
1466 if (encoder) {
1467 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1468 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
1469
1470 /* AVIVO hardware supports downscaling modes larger than the panel
1471 * to the panel size, but I'm not sure this is desirable.
1472 */
1473 if ((mode->hdisplay > native_mode->hdisplay) ||
1474 (mode->vdisplay > native_mode->vdisplay))
1475 return MODE_PANEL;
1476
1477 /* if scaling is disabled, block non-native modes */
1478 if (amdgpu_encoder->rmx_type == RMX_OFF) {
1479 if ((mode->hdisplay != native_mode->hdisplay) ||
1480 (mode->vdisplay != native_mode->vdisplay))
1481 return MODE_PANEL;
1482 }
1483 }
1484 return MODE_OK;
1485 } else {
1486 if ((amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1487 (amdgpu_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
1488 return amdgpu_atombios_dp_mode_valid_helper(connector, mode);
1489 } else {
1490 if (drm_detect_hdmi_monitor(amdgpu_connector_edid(connector))) {
1491 /* HDMI 1.3+ supports max clock of 340 Mhz */
1492 if (mode->clock > 340000)
1493 return MODE_CLOCK_HIGH;
1494 } else {
1495 if (mode->clock > 165000)
1496 return MODE_CLOCK_HIGH;
1497 }
1498 }
1499 }
1500
1501 return MODE_OK;
1502}
1503
1504static const struct drm_connector_helper_funcs amdgpu_connector_dp_helper_funcs = {
1505 .get_modes = amdgpu_connector_dp_get_modes,
1506 .mode_valid = amdgpu_connector_dp_mode_valid,
1507 .best_encoder = amdgpu_connector_dvi_encoder,
1508};
1509
1510static const struct drm_connector_funcs amdgpu_connector_dp_funcs = {
1511 .dpms = drm_helper_connector_dpms,
1512 .detect = amdgpu_connector_dp_detect,
1513 .fill_modes = drm_helper_probe_single_connector_modes,
1514 .set_property = amdgpu_connector_set_property,
1515 .early_unregister = amdgpu_connector_unregister,
1516 .destroy = amdgpu_connector_destroy,
1517 .force = amdgpu_connector_dvi_force,
1518};
1519
1520static const struct drm_connector_funcs amdgpu_connector_edp_funcs = {
1521 .dpms = drm_helper_connector_dpms,
1522 .detect = amdgpu_connector_dp_detect,
1523 .fill_modes = drm_helper_probe_single_connector_modes,
1524 .set_property = amdgpu_connector_set_lcd_property,
1525 .early_unregister = amdgpu_connector_unregister,
1526 .destroy = amdgpu_connector_destroy,
1527 .force = amdgpu_connector_dvi_force,
1528};
1529
1530void
1531amdgpu_connector_add(struct amdgpu_device *adev,
1532 uint32_t connector_id,
1533 uint32_t supported_device,
1534 int connector_type,
1535 struct amdgpu_i2c_bus_rec *i2c_bus,
1536 uint16_t connector_object_id,
1537 struct amdgpu_hpd *hpd,
1538 struct amdgpu_router *router)
1539{
1540 struct drm_device *dev = adev->ddev;
1541 struct drm_connector *connector;
1542 struct amdgpu_connector *amdgpu_connector;
1543 struct amdgpu_connector_atom_dig *amdgpu_dig_connector;
1544 struct drm_encoder *encoder;
1545 struct amdgpu_encoder *amdgpu_encoder;
1546 uint32_t subpixel_order = SubPixelNone;
1547 bool shared_ddc = false;
1548 bool is_dp_bridge = false;
1549 bool has_aux = false;
1550
1551 if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1552 return;
1553
1554 /* see if we already added it */
1555 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1556 amdgpu_connector = to_amdgpu_connector(connector);
1557 if (amdgpu_connector->connector_id == connector_id) {
1558 amdgpu_connector->devices |= supported_device;
1559 return;
1560 }
1561 if (amdgpu_connector->ddc_bus && i2c_bus->valid) {
1562 if (amdgpu_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1563 amdgpu_connector->shared_ddc = true;
1564 shared_ddc = true;
1565 }
1566 if (amdgpu_connector->router_bus && router->ddc_valid &&
1567 (amdgpu_connector->router.router_id == router->router_id)) {
1568 amdgpu_connector->shared_ddc = false;
1569 shared_ddc = false;
1570 }
1571 }
1572 }
1573
1574 /* check if it's a dp bridge */
1575 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1576 amdgpu_encoder = to_amdgpu_encoder(encoder);
1577 if (amdgpu_encoder->devices & supported_device) {
1578 switch (amdgpu_encoder->encoder_id) {
1579 case ENCODER_OBJECT_ID_TRAVIS:
1580 case ENCODER_OBJECT_ID_NUTMEG:
1581 is_dp_bridge = true;
1582 break;
1583 default:
1584 break;
1585 }
1586 }
1587 }
1588
1589 amdgpu_connector = kzalloc(sizeof(struct amdgpu_connector), GFP_KERNEL);
1590 if (!amdgpu_connector)
1591 return;
1592
1593 connector = &amdgpu_connector->base;
1594
1595 amdgpu_connector->connector_id = connector_id;
1596 amdgpu_connector->devices = supported_device;
1597 amdgpu_connector->shared_ddc = shared_ddc;
1598 amdgpu_connector->connector_object_id = connector_object_id;
1599 amdgpu_connector->hpd = *hpd;
1600
1601 amdgpu_connector->router = *router;
1602 if (router->ddc_valid || router->cd_valid) {
1603 amdgpu_connector->router_bus = amdgpu_i2c_lookup(adev, &router->i2c_info);
1604 if (!amdgpu_connector->router_bus)
1605 DRM_ERROR("Failed to assign router i2c bus! Check dmesg for i2c errors.\n");
1606 }
1607
1608 if (is_dp_bridge) {
1609 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1610 if (!amdgpu_dig_connector)
1611 goto failed;
1612 amdgpu_connector->con_priv = amdgpu_dig_connector;
1613 if (i2c_bus->valid) {
1614 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1615 if (amdgpu_connector->ddc_bus)
1616 has_aux = true;
1617 else
1618 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1619 }
1620 switch (connector_type) {
1621 case DRM_MODE_CONNECTOR_VGA:
1622 case DRM_MODE_CONNECTOR_DVIA:
1623 default:
1624 drm_connector_init(dev, &amdgpu_connector->base,
1625 &amdgpu_connector_dp_funcs, connector_type);
1626 drm_connector_helper_add(&amdgpu_connector->base,
1627 &amdgpu_connector_dp_helper_funcs);
1628 connector->interlace_allowed = true;
1629 connector->doublescan_allowed = true;
1630 amdgpu_connector->dac_load_detect = true;
1631 drm_object_attach_property(&amdgpu_connector->base.base,
1632 adev->mode_info.load_detect_property,
1633 1);
1634 drm_object_attach_property(&amdgpu_connector->base.base,
1635 dev->mode_config.scaling_mode_property,
1636 DRM_MODE_SCALE_NONE);
1637 break;
1638 case DRM_MODE_CONNECTOR_DVII:
1639 case DRM_MODE_CONNECTOR_DVID:
1640 case DRM_MODE_CONNECTOR_HDMIA:
1641 case DRM_MODE_CONNECTOR_HDMIB:
1642 case DRM_MODE_CONNECTOR_DisplayPort:
1643 drm_connector_init(dev, &amdgpu_connector->base,
1644 &amdgpu_connector_dp_funcs, connector_type);
1645 drm_connector_helper_add(&amdgpu_connector->base,
1646 &amdgpu_connector_dp_helper_funcs);
1647 drm_object_attach_property(&amdgpu_connector->base.base,
1648 adev->mode_info.underscan_property,
1649 UNDERSCAN_OFF);
1650 drm_object_attach_property(&amdgpu_connector->base.base,
1651 adev->mode_info.underscan_hborder_property,
1652 0);
1653 drm_object_attach_property(&amdgpu_connector->base.base,
1654 adev->mode_info.underscan_vborder_property,
1655 0);
1656
1657 drm_object_attach_property(&amdgpu_connector->base.base,
1658 dev->mode_config.scaling_mode_property,
1659 DRM_MODE_SCALE_NONE);
1660
1661 drm_object_attach_property(&amdgpu_connector->base.base,
1662 adev->mode_info.dither_property,
1663 AMDGPU_FMT_DITHER_DISABLE);
1664
1665 if (amdgpu_audio != 0)
1666 drm_object_attach_property(&amdgpu_connector->base.base,
1667 adev->mode_info.audio_property,
1668 AMDGPU_AUDIO_AUTO);
1669
1670 subpixel_order = SubPixelHorizontalRGB;
1671 connector->interlace_allowed = true;
1672 if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1673 connector->doublescan_allowed = true;
1674 else
1675 connector->doublescan_allowed = false;
1676 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1677 amdgpu_connector->dac_load_detect = true;
1678 drm_object_attach_property(&amdgpu_connector->base.base,
1679 adev->mode_info.load_detect_property,
1680 1);
1681 }
1682 break;
1683 case DRM_MODE_CONNECTOR_LVDS:
1684 case DRM_MODE_CONNECTOR_eDP:
1685 drm_connector_init(dev, &amdgpu_connector->base,
1686 &amdgpu_connector_edp_funcs, connector_type);
1687 drm_connector_helper_add(&amdgpu_connector->base,
1688 &amdgpu_connector_dp_helper_funcs);
1689 drm_object_attach_property(&amdgpu_connector->base.base,
1690 dev->mode_config.scaling_mode_property,
1691 DRM_MODE_SCALE_FULLSCREEN);
1692 subpixel_order = SubPixelHorizontalRGB;
1693 connector->interlace_allowed = false;
1694 connector->doublescan_allowed = false;
1695 break;
1696 }
1697 } else {
1698 switch (connector_type) {
1699 case DRM_MODE_CONNECTOR_VGA:
1700 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_vga_funcs, connector_type);
1701 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_vga_helper_funcs);
1702 if (i2c_bus->valid) {
1703 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1704 if (!amdgpu_connector->ddc_bus)
1705 DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1706 }
1707 amdgpu_connector->dac_load_detect = true;
1708 drm_object_attach_property(&amdgpu_connector->base.base,
1709 adev->mode_info.load_detect_property,
1710 1);
1711 drm_object_attach_property(&amdgpu_connector->base.base,
1712 dev->mode_config.scaling_mode_property,
1713 DRM_MODE_SCALE_NONE);
1714 /* no HPD on analog connectors */
1715 amdgpu_connector->hpd.hpd = AMDGPU_HPD_NONE;
1716 connector->interlace_allowed = true;
1717 connector->doublescan_allowed = true;
1718 break;
1719 case DRM_MODE_CONNECTOR_DVIA:
1720 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_vga_funcs, connector_type);
1721 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_vga_helper_funcs);
1722 if (i2c_bus->valid) {
1723 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1724 if (!amdgpu_connector->ddc_bus)
1725 DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1726 }
1727 amdgpu_connector->dac_load_detect = true;
1728 drm_object_attach_property(&amdgpu_connector->base.base,
1729 adev->mode_info.load_detect_property,
1730 1);
1731 drm_object_attach_property(&amdgpu_connector->base.base,
1732 dev->mode_config.scaling_mode_property,
1733 DRM_MODE_SCALE_NONE);
1734 /* no HPD on analog connectors */
1735 amdgpu_connector->hpd.hpd = AMDGPU_HPD_NONE;
1736 connector->interlace_allowed = true;
1737 connector->doublescan_allowed = true;
1738 break;
1739 case DRM_MODE_CONNECTOR_DVII:
1740 case DRM_MODE_CONNECTOR_DVID:
1741 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1742 if (!amdgpu_dig_connector)
1743 goto failed;
1744 amdgpu_connector->con_priv = amdgpu_dig_connector;
1745 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_dvi_funcs, connector_type);
1746 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dvi_helper_funcs);
1747 if (i2c_bus->valid) {
1748 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1749 if (!amdgpu_connector->ddc_bus)
1750 DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1751 }
1752 subpixel_order = SubPixelHorizontalRGB;
1753 drm_object_attach_property(&amdgpu_connector->base.base,
1754 adev->mode_info.coherent_mode_property,
1755 1);
1756 drm_object_attach_property(&amdgpu_connector->base.base,
1757 adev->mode_info.underscan_property,
1758 UNDERSCAN_OFF);
1759 drm_object_attach_property(&amdgpu_connector->base.base,
1760 adev->mode_info.underscan_hborder_property,
1761 0);
1762 drm_object_attach_property(&amdgpu_connector->base.base,
1763 adev->mode_info.underscan_vborder_property,
1764 0);
1765 drm_object_attach_property(&amdgpu_connector->base.base,
1766 dev->mode_config.scaling_mode_property,
1767 DRM_MODE_SCALE_NONE);
1768
1769 if (amdgpu_audio != 0) {
1770 drm_object_attach_property(&amdgpu_connector->base.base,
1771 adev->mode_info.audio_property,
1772 AMDGPU_AUDIO_AUTO);
1773 }
1774 drm_object_attach_property(&amdgpu_connector->base.base,
1775 adev->mode_info.dither_property,
1776 AMDGPU_FMT_DITHER_DISABLE);
1777 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1778 amdgpu_connector->dac_load_detect = true;
1779 drm_object_attach_property(&amdgpu_connector->base.base,
1780 adev->mode_info.load_detect_property,
1781 1);
1782 }
1783 connector->interlace_allowed = true;
1784 if (connector_type == DRM_MODE_CONNECTOR_DVII)
1785 connector->doublescan_allowed = true;
1786 else
1787 connector->doublescan_allowed = false;
1788 break;
1789 case DRM_MODE_CONNECTOR_HDMIA:
1790 case DRM_MODE_CONNECTOR_HDMIB:
1791 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1792 if (!amdgpu_dig_connector)
1793 goto failed;
1794 amdgpu_connector->con_priv = amdgpu_dig_connector;
1795 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_dvi_funcs, connector_type);
1796 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dvi_helper_funcs);
1797 if (i2c_bus->valid) {
1798 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1799 if (!amdgpu_connector->ddc_bus)
1800 DRM_ERROR("HDMI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1801 }
1802 drm_object_attach_property(&amdgpu_connector->base.base,
1803 adev->mode_info.coherent_mode_property,
1804 1);
1805 drm_object_attach_property(&amdgpu_connector->base.base,
1806 adev->mode_info.underscan_property,
1807 UNDERSCAN_OFF);
1808 drm_object_attach_property(&amdgpu_connector->base.base,
1809 adev->mode_info.underscan_hborder_property,
1810 0);
1811 drm_object_attach_property(&amdgpu_connector->base.base,
1812 adev->mode_info.underscan_vborder_property,
1813 0);
1814 drm_object_attach_property(&amdgpu_connector->base.base,
1815 dev->mode_config.scaling_mode_property,
1816 DRM_MODE_SCALE_NONE);
1817 if (amdgpu_audio != 0) {
1818 drm_object_attach_property(&amdgpu_connector->base.base,
1819 adev->mode_info.audio_property,
1820 AMDGPU_AUDIO_AUTO);
1821 }
1822 drm_object_attach_property(&amdgpu_connector->base.base,
1823 adev->mode_info.dither_property,
1824 AMDGPU_FMT_DITHER_DISABLE);
1825 subpixel_order = SubPixelHorizontalRGB;
1826 connector->interlace_allowed = true;
1827 if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1828 connector->doublescan_allowed = true;
1829 else
1830 connector->doublescan_allowed = false;
1831 break;
1832 case DRM_MODE_CONNECTOR_DisplayPort:
1833 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1834 if (!amdgpu_dig_connector)
1835 goto failed;
1836 amdgpu_connector->con_priv = amdgpu_dig_connector;
1837 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_dp_funcs, connector_type);
1838 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dp_helper_funcs);
1839 if (i2c_bus->valid) {
1840 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1841 if (amdgpu_connector->ddc_bus)
1842 has_aux = true;
1843 else
1844 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1845 }
1846 subpixel_order = SubPixelHorizontalRGB;
1847 drm_object_attach_property(&amdgpu_connector->base.base,
1848 adev->mode_info.coherent_mode_property,
1849 1);
1850 drm_object_attach_property(&amdgpu_connector->base.base,
1851 adev->mode_info.underscan_property,
1852 UNDERSCAN_OFF);
1853 drm_object_attach_property(&amdgpu_connector->base.base,
1854 adev->mode_info.underscan_hborder_property,
1855 0);
1856 drm_object_attach_property(&amdgpu_connector->base.base,
1857 adev->mode_info.underscan_vborder_property,
1858 0);
1859 drm_object_attach_property(&amdgpu_connector->base.base,
1860 dev->mode_config.scaling_mode_property,
1861 DRM_MODE_SCALE_NONE);
1862 if (amdgpu_audio != 0) {
1863 drm_object_attach_property(&amdgpu_connector->base.base,
1864 adev->mode_info.audio_property,
1865 AMDGPU_AUDIO_AUTO);
1866 }
1867 drm_object_attach_property(&amdgpu_connector->base.base,
1868 adev->mode_info.dither_property,
1869 AMDGPU_FMT_DITHER_DISABLE);
1870 connector->interlace_allowed = true;
1871 /* in theory with a DP to VGA converter... */
1872 connector->doublescan_allowed = false;
1873 break;
1874 case DRM_MODE_CONNECTOR_eDP:
1875 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1876 if (!amdgpu_dig_connector)
1877 goto failed;
1878 amdgpu_connector->con_priv = amdgpu_dig_connector;
1879 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_edp_funcs, connector_type);
1880 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_dp_helper_funcs);
1881 if (i2c_bus->valid) {
1882 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1883 if (amdgpu_connector->ddc_bus)
1884 has_aux = true;
1885 else
1886 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1887 }
1888 drm_object_attach_property(&amdgpu_connector->base.base,
1889 dev->mode_config.scaling_mode_property,
1890 DRM_MODE_SCALE_FULLSCREEN);
1891 subpixel_order = SubPixelHorizontalRGB;
1892 connector->interlace_allowed = false;
1893 connector->doublescan_allowed = false;
1894 break;
1895 case DRM_MODE_CONNECTOR_LVDS:
1896 amdgpu_dig_connector = kzalloc(sizeof(struct amdgpu_connector_atom_dig), GFP_KERNEL);
1897 if (!amdgpu_dig_connector)
1898 goto failed;
1899 amdgpu_connector->con_priv = amdgpu_dig_connector;
1900 drm_connector_init(dev, &amdgpu_connector->base, &amdgpu_connector_lvds_funcs, connector_type);
1901 drm_connector_helper_add(&amdgpu_connector->base, &amdgpu_connector_lvds_helper_funcs);
1902 if (i2c_bus->valid) {
1903 amdgpu_connector->ddc_bus = amdgpu_i2c_lookup(adev, i2c_bus);
1904 if (!amdgpu_connector->ddc_bus)
1905 DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1906 }
1907 drm_object_attach_property(&amdgpu_connector->base.base,
1908 dev->mode_config.scaling_mode_property,
1909 DRM_MODE_SCALE_FULLSCREEN);
1910 subpixel_order = SubPixelHorizontalRGB;
1911 connector->interlace_allowed = false;
1912 connector->doublescan_allowed = false;
1913 break;
1914 }
1915 }
1916
1917 if (amdgpu_connector->hpd.hpd == AMDGPU_HPD_NONE) {
1918 if (i2c_bus->valid) {
1919 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1920 DRM_CONNECTOR_POLL_DISCONNECT;
1921 }
1922 } else
1923 connector->polled = DRM_CONNECTOR_POLL_HPD;
1924
1925 connector->display_info.subpixel_order = subpixel_order;
1926 drm_connector_register(connector);
1927
1928 if (has_aux)
1929 amdgpu_atombios_dp_aux_init(amdgpu_connector);
1930
1931 return;
1932
1933failed:
1934 drm_connector_cleanup(connector);
1935 kfree(connector);
1936}