drm/radeon: fix load detect on rn50 with hardcoded EDIDs.
[~shefty/rdma-dev.git] / drivers / gpu / drm / radeon / radeon_connectors.c
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 "drmP.h"
27 #include "drm_edid.h"
28 #include "drm_crtc_helper.h"
29 #include "drm_fb_helper.h"
30 #include "radeon_drm.h"
31 #include "radeon.h"
32 #include "atom.h"
33
34 extern void
35 radeon_combios_connected_scratch_regs(struct drm_connector *connector,
36                                       struct drm_encoder *encoder,
37                                       bool connected);
38 extern void
39 radeon_atombios_connected_scratch_regs(struct drm_connector *connector,
40                                        struct drm_encoder *encoder,
41                                        bool connected);
42
43 extern void
44 radeon_legacy_backlight_init(struct radeon_encoder *radeon_encoder,
45                              struct drm_connector *drm_connector);
46
47 void radeon_connector_hotplug(struct drm_connector *connector)
48 {
49         struct drm_device *dev = connector->dev;
50         struct radeon_device *rdev = dev->dev_private;
51         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
52
53         /* bail if the connector does not have hpd pin, e.g.,
54          * VGA, TV, etc.
55          */
56         if (radeon_connector->hpd.hpd == RADEON_HPD_NONE)
57                 return;
58
59         radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
60
61         /* if the connector is already off, don't turn it back on */
62         if (connector->dpms != DRM_MODE_DPMS_ON)
63                 return;
64
65         /* just deal with DP (not eDP) here. */
66         if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
67                 int saved_dpms = connector->dpms;
68
69                 /* Only turn off the display it it's physically disconnected */
70                 if (!radeon_hpd_sense(rdev, radeon_connector->hpd.hpd))
71                         drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
72                 else if (radeon_dp_needs_link_train(radeon_connector))
73                         drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
74                 connector->dpms = saved_dpms;
75         }
76 }
77
78 static void radeon_property_change_mode(struct drm_encoder *encoder)
79 {
80         struct drm_crtc *crtc = encoder->crtc;
81
82         if (crtc && crtc->enabled) {
83                 drm_crtc_helper_set_mode(crtc, &crtc->mode,
84                                          crtc->x, crtc->y, crtc->fb);
85         }
86 }
87 static void
88 radeon_connector_update_scratch_regs(struct drm_connector *connector, enum drm_connector_status status)
89 {
90         struct drm_device *dev = connector->dev;
91         struct radeon_device *rdev = dev->dev_private;
92         struct drm_encoder *best_encoder = NULL;
93         struct drm_encoder *encoder = NULL;
94         struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
95         struct drm_mode_object *obj;
96         bool connected;
97         int i;
98
99         best_encoder = connector_funcs->best_encoder(connector);
100
101         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
102                 if (connector->encoder_ids[i] == 0)
103                         break;
104
105                 obj = drm_mode_object_find(connector->dev,
106                                            connector->encoder_ids[i],
107                                            DRM_MODE_OBJECT_ENCODER);
108                 if (!obj)
109                         continue;
110
111                 encoder = obj_to_encoder(obj);
112
113                 if ((encoder == best_encoder) && (status == connector_status_connected))
114                         connected = true;
115                 else
116                         connected = false;
117
118                 if (rdev->is_atom_bios)
119                         radeon_atombios_connected_scratch_regs(connector, encoder, connected);
120                 else
121                         radeon_combios_connected_scratch_regs(connector, encoder, connected);
122
123         }
124 }
125
126 struct drm_encoder *radeon_find_encoder(struct drm_connector *connector, int encoder_type)
127 {
128         struct drm_mode_object *obj;
129         struct drm_encoder *encoder;
130         int i;
131
132         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
133                 if (connector->encoder_ids[i] == 0)
134                         break;
135
136                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
137                 if (!obj)
138                         continue;
139
140                 encoder = obj_to_encoder(obj);
141                 if (encoder->encoder_type == encoder_type)
142                         return encoder;
143         }
144         return NULL;
145 }
146
147 struct drm_encoder *radeon_best_single_encoder(struct drm_connector *connector)
148 {
149         int enc_id = connector->encoder_ids[0];
150         struct drm_mode_object *obj;
151         struct drm_encoder *encoder;
152
153         /* pick the encoder ids */
154         if (enc_id) {
155                 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
156                 if (!obj)
157                         return NULL;
158                 encoder = obj_to_encoder(obj);
159                 return encoder;
160         }
161         return NULL;
162 }
163
164 /*
165  * radeon_connector_analog_encoder_conflict_solve
166  * - search for other connectors sharing this encoder
167  *   if priority is true, then set them disconnected if this is connected
168  *   if priority is false, set us disconnected if they are connected
169  */
170 static enum drm_connector_status
171 radeon_connector_analog_encoder_conflict_solve(struct drm_connector *connector,
172                                                struct drm_encoder *encoder,
173                                                enum drm_connector_status current_status,
174                                                bool priority)
175 {
176         struct drm_device *dev = connector->dev;
177         struct drm_connector *conflict;
178         struct radeon_connector *radeon_conflict;
179         int i;
180
181         list_for_each_entry(conflict, &dev->mode_config.connector_list, head) {
182                 if (conflict == connector)
183                         continue;
184
185                 radeon_conflict = to_radeon_connector(conflict);
186                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
187                         if (conflict->encoder_ids[i] == 0)
188                                 break;
189
190                         /* if the IDs match */
191                         if (conflict->encoder_ids[i] == encoder->base.id) {
192                                 if (conflict->status != connector_status_connected)
193                                         continue;
194
195                                 if (radeon_conflict->use_digital)
196                                         continue;
197
198                                 if (priority == true) {
199                                         DRM_DEBUG_KMS("1: conflicting encoders switching off %s\n", drm_get_connector_name(conflict));
200                                         DRM_DEBUG_KMS("in favor of %s\n", drm_get_connector_name(connector));
201                                         conflict->status = connector_status_disconnected;
202                                         radeon_connector_update_scratch_regs(conflict, connector_status_disconnected);
203                                 } else {
204                                         DRM_DEBUG_KMS("2: conflicting encoders switching off %s\n", drm_get_connector_name(connector));
205                                         DRM_DEBUG_KMS("in favor of %s\n", drm_get_connector_name(conflict));
206                                         current_status = connector_status_disconnected;
207                                 }
208                                 break;
209                         }
210                 }
211         }
212         return current_status;
213
214 }
215
216 static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encoder)
217 {
218         struct drm_device *dev = encoder->dev;
219         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
220         struct drm_display_mode *mode = NULL;
221         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
222
223         if (native_mode->hdisplay != 0 &&
224             native_mode->vdisplay != 0 &&
225             native_mode->clock != 0) {
226                 mode = drm_mode_duplicate(dev, native_mode);
227                 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
228                 drm_mode_set_name(mode);
229
230                 DRM_DEBUG_KMS("Adding native panel mode %s\n", mode->name);
231         } else if (native_mode->hdisplay != 0 &&
232                    native_mode->vdisplay != 0) {
233                 /* mac laptops without an edid */
234                 /* Note that this is not necessarily the exact panel mode,
235                  * but an approximation based on the cvt formula.  For these
236                  * systems we should ideally read the mode info out of the
237                  * registers or add a mode table, but this works and is much
238                  * simpler.
239                  */
240                 mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
241                 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
242                 DRM_DEBUG_KMS("Adding cvt approximation of native panel mode %s\n", mode->name);
243         }
244         return mode;
245 }
246
247 static void radeon_add_common_modes(struct drm_encoder *encoder, struct drm_connector *connector)
248 {
249         struct drm_device *dev = encoder->dev;
250         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
251         struct drm_display_mode *mode = NULL;
252         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
253         int i;
254         struct mode_size {
255                 int w;
256                 int h;
257         } common_modes[17] = {
258                 { 640,  480},
259                 { 720,  480},
260                 { 800,  600},
261                 { 848,  480},
262                 {1024,  768},
263                 {1152,  768},
264                 {1280,  720},
265                 {1280,  800},
266                 {1280,  854},
267                 {1280,  960},
268                 {1280, 1024},
269                 {1440,  900},
270                 {1400, 1050},
271                 {1680, 1050},
272                 {1600, 1200},
273                 {1920, 1080},
274                 {1920, 1200}
275         };
276
277         for (i = 0; i < 17; i++) {
278                 if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT)) {
279                         if (common_modes[i].w > 1024 ||
280                             common_modes[i].h > 768)
281                                 continue;
282                 }
283                 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
284                         if (common_modes[i].w > native_mode->hdisplay ||
285                             common_modes[i].h > native_mode->vdisplay ||
286                             (common_modes[i].w == native_mode->hdisplay &&
287                              common_modes[i].h == native_mode->vdisplay))
288                                 continue;
289                 }
290                 if (common_modes[i].w < 320 || common_modes[i].h < 200)
291                         continue;
292
293                 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
294                 drm_mode_probed_add(connector, mode);
295         }
296 }
297
298 int radeon_connector_set_property(struct drm_connector *connector, struct drm_property *property,
299                                   uint64_t val)
300 {
301         struct drm_device *dev = connector->dev;
302         struct radeon_device *rdev = dev->dev_private;
303         struct drm_encoder *encoder;
304         struct radeon_encoder *radeon_encoder;
305
306         if (property == rdev->mode_info.coherent_mode_property) {
307                 struct radeon_encoder_atom_dig *dig;
308                 bool new_coherent_mode;
309
310                 /* need to find digital encoder on connector */
311                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
312                 if (!encoder)
313                         return 0;
314
315                 radeon_encoder = to_radeon_encoder(encoder);
316
317                 if (!radeon_encoder->enc_priv)
318                         return 0;
319
320                 dig = radeon_encoder->enc_priv;
321                 new_coherent_mode = val ? true : false;
322                 if (dig->coherent_mode != new_coherent_mode) {
323                         dig->coherent_mode = new_coherent_mode;
324                         radeon_property_change_mode(&radeon_encoder->base);
325                 }
326         }
327
328         if (property == rdev->mode_info.underscan_property) {
329                 /* need to find digital encoder on connector */
330                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
331                 if (!encoder)
332                         return 0;
333
334                 radeon_encoder = to_radeon_encoder(encoder);
335
336                 if (radeon_encoder->underscan_type != val) {
337                         radeon_encoder->underscan_type = val;
338                         radeon_property_change_mode(&radeon_encoder->base);
339                 }
340         }
341
342         if (property == rdev->mode_info.underscan_hborder_property) {
343                 /* need to find digital encoder on connector */
344                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
345                 if (!encoder)
346                         return 0;
347
348                 radeon_encoder = to_radeon_encoder(encoder);
349
350                 if (radeon_encoder->underscan_hborder != val) {
351                         radeon_encoder->underscan_hborder = val;
352                         radeon_property_change_mode(&radeon_encoder->base);
353                 }
354         }
355
356         if (property == rdev->mode_info.underscan_vborder_property) {
357                 /* need to find digital encoder on connector */
358                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
359                 if (!encoder)
360                         return 0;
361
362                 radeon_encoder = to_radeon_encoder(encoder);
363
364                 if (radeon_encoder->underscan_vborder != val) {
365                         radeon_encoder->underscan_vborder = val;
366                         radeon_property_change_mode(&radeon_encoder->base);
367                 }
368         }
369
370         if (property == rdev->mode_info.tv_std_property) {
371                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TVDAC);
372                 if (!encoder) {
373                         encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_DAC);
374                 }
375
376                 if (!encoder)
377                         return 0;
378
379                 radeon_encoder = to_radeon_encoder(encoder);
380                 if (!radeon_encoder->enc_priv)
381                         return 0;
382                 if (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom) {
383                         struct radeon_encoder_atom_dac *dac_int;
384                         dac_int = radeon_encoder->enc_priv;
385                         dac_int->tv_std = val;
386                 } else {
387                         struct radeon_encoder_tv_dac *dac_int;
388                         dac_int = radeon_encoder->enc_priv;
389                         dac_int->tv_std = val;
390                 }
391                 radeon_property_change_mode(&radeon_encoder->base);
392         }
393
394         if (property == rdev->mode_info.load_detect_property) {
395                 struct radeon_connector *radeon_connector =
396                         to_radeon_connector(connector);
397
398                 if (val == 0)
399                         radeon_connector->dac_load_detect = false;
400                 else
401                         radeon_connector->dac_load_detect = true;
402         }
403
404         if (property == rdev->mode_info.tmds_pll_property) {
405                 struct radeon_encoder_int_tmds *tmds = NULL;
406                 bool ret = false;
407                 /* need to find digital encoder on connector */
408                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
409                 if (!encoder)
410                         return 0;
411
412                 radeon_encoder = to_radeon_encoder(encoder);
413
414                 tmds = radeon_encoder->enc_priv;
415                 if (!tmds)
416                         return 0;
417
418                 if (val == 0) {
419                         if (rdev->is_atom_bios)
420                                 ret = radeon_atombios_get_tmds_info(radeon_encoder, tmds);
421                         else
422                                 ret = radeon_legacy_get_tmds_info_from_combios(radeon_encoder, tmds);
423                 }
424                 if (val == 1 || ret == false) {
425                         radeon_legacy_get_tmds_info_from_table(radeon_encoder, tmds);
426                 }
427                 radeon_property_change_mode(&radeon_encoder->base);
428         }
429
430         return 0;
431 }
432
433 static void radeon_fixup_lvds_native_mode(struct drm_encoder *encoder,
434                                           struct drm_connector *connector)
435 {
436         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
437         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
438         struct drm_display_mode *t, *mode;
439
440         /* If the EDID preferred mode doesn't match the native mode, use it */
441         list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
442                 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
443                         if (mode->hdisplay != native_mode->hdisplay ||
444                             mode->vdisplay != native_mode->vdisplay)
445                                 memcpy(native_mode, mode, sizeof(*mode));
446                 }
447         }
448
449         /* Try to get native mode details from EDID if necessary */
450         if (!native_mode->clock) {
451                 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
452                         if (mode->hdisplay == native_mode->hdisplay &&
453                             mode->vdisplay == native_mode->vdisplay) {
454                                 *native_mode = *mode;
455                                 drm_mode_set_crtcinfo(native_mode, CRTC_INTERLACE_HALVE_V);
456                                 DRM_DEBUG_KMS("Determined LVDS native mode details from EDID\n");
457                                 break;
458                         }
459                 }
460         }
461
462         if (!native_mode->clock) {
463                 DRM_DEBUG_KMS("No LVDS native mode details, disabling RMX\n");
464                 radeon_encoder->rmx_type = RMX_OFF;
465         }
466 }
467
468 static int radeon_lvds_get_modes(struct drm_connector *connector)
469 {
470         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
471         struct drm_encoder *encoder;
472         int ret = 0;
473         struct drm_display_mode *mode;
474
475         if (radeon_connector->ddc_bus) {
476                 ret = radeon_ddc_get_modes(radeon_connector);
477                 if (ret > 0) {
478                         encoder = radeon_best_single_encoder(connector);
479                         if (encoder) {
480                                 radeon_fixup_lvds_native_mode(encoder, connector);
481                                 /* add scaled modes */
482                                 radeon_add_common_modes(encoder, connector);
483                         }
484                         return ret;
485                 }
486         }
487
488         encoder = radeon_best_single_encoder(connector);
489         if (!encoder)
490                 return 0;
491
492         /* we have no EDID modes */
493         mode = radeon_fp_native_mode(encoder);
494         if (mode) {
495                 ret = 1;
496                 drm_mode_probed_add(connector, mode);
497                 /* add the width/height from vbios tables if available */
498                 connector->display_info.width_mm = mode->width_mm;
499                 connector->display_info.height_mm = mode->height_mm;
500                 /* add scaled modes */
501                 radeon_add_common_modes(encoder, connector);
502         }
503
504         return ret;
505 }
506
507 static int radeon_lvds_mode_valid(struct drm_connector *connector,
508                                   struct drm_display_mode *mode)
509 {
510         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
511
512         if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
513                 return MODE_PANEL;
514
515         if (encoder) {
516                 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
517                 struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
518
519                 /* AVIVO hardware supports downscaling modes larger than the panel
520                  * to the panel size, but I'm not sure this is desirable.
521                  */
522                 if ((mode->hdisplay > native_mode->hdisplay) ||
523                     (mode->vdisplay > native_mode->vdisplay))
524                         return MODE_PANEL;
525
526                 /* if scaling is disabled, block non-native modes */
527                 if (radeon_encoder->rmx_type == RMX_OFF) {
528                         if ((mode->hdisplay != native_mode->hdisplay) ||
529                             (mode->vdisplay != native_mode->vdisplay))
530                                 return MODE_PANEL;
531                 }
532         }
533
534         return MODE_OK;
535 }
536
537 static enum drm_connector_status
538 radeon_lvds_detect(struct drm_connector *connector, bool force)
539 {
540         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
541         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
542         enum drm_connector_status ret = connector_status_disconnected;
543
544         if (encoder) {
545                 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
546                 struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
547
548                 /* check if panel is valid */
549                 if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
550                         ret = connector_status_connected;
551
552         }
553
554         /* check for edid as well */
555         if (radeon_connector->edid)
556                 ret = connector_status_connected;
557         else {
558                 if (radeon_connector->ddc_bus) {
559                         radeon_connector->edid = drm_get_edid(&radeon_connector->base,
560                                                               &radeon_connector->ddc_bus->adapter);
561                         if (radeon_connector->edid)
562                                 ret = connector_status_connected;
563                 }
564         }
565         /* check acpi lid status ??? */
566
567         radeon_connector_update_scratch_regs(connector, ret);
568         return ret;
569 }
570
571 static void radeon_connector_destroy(struct drm_connector *connector)
572 {
573         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
574
575         if (radeon_connector->edid)
576                 kfree(radeon_connector->edid);
577         kfree(radeon_connector->con_priv);
578         drm_sysfs_connector_remove(connector);
579         drm_connector_cleanup(connector);
580         kfree(connector);
581 }
582
583 static int radeon_lvds_set_property(struct drm_connector *connector,
584                                     struct drm_property *property,
585                                     uint64_t value)
586 {
587         struct drm_device *dev = connector->dev;
588         struct radeon_encoder *radeon_encoder;
589         enum radeon_rmx_type rmx_type;
590
591         DRM_DEBUG_KMS("\n");
592         if (property != dev->mode_config.scaling_mode_property)
593                 return 0;
594
595         if (connector->encoder)
596                 radeon_encoder = to_radeon_encoder(connector->encoder);
597         else {
598                 struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
599                 radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
600         }
601
602         switch (value) {
603         case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
604         case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
605         case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
606         default:
607         case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
608         }
609         if (radeon_encoder->rmx_type == rmx_type)
610                 return 0;
611
612         radeon_encoder->rmx_type = rmx_type;
613
614         radeon_property_change_mode(&radeon_encoder->base);
615         return 0;
616 }
617
618
619 struct drm_connector_helper_funcs radeon_lvds_connector_helper_funcs = {
620         .get_modes = radeon_lvds_get_modes,
621         .mode_valid = radeon_lvds_mode_valid,
622         .best_encoder = radeon_best_single_encoder,
623 };
624
625 struct drm_connector_funcs radeon_lvds_connector_funcs = {
626         .dpms = drm_helper_connector_dpms,
627         .detect = radeon_lvds_detect,
628         .fill_modes = drm_helper_probe_single_connector_modes,
629         .destroy = radeon_connector_destroy,
630         .set_property = radeon_lvds_set_property,
631 };
632
633 static int radeon_vga_get_modes(struct drm_connector *connector)
634 {
635         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
636         int ret;
637
638         ret = radeon_ddc_get_modes(radeon_connector);
639
640         return ret;
641 }
642
643 static int radeon_vga_mode_valid(struct drm_connector *connector,
644                                   struct drm_display_mode *mode)
645 {
646         struct drm_device *dev = connector->dev;
647         struct radeon_device *rdev = dev->dev_private;
648
649         /* XXX check mode bandwidth */
650
651         if ((mode->clock / 10) > rdev->clock.max_pixel_clock)
652                 return MODE_CLOCK_HIGH;
653
654         return MODE_OK;
655 }
656
657 static enum drm_connector_status
658 radeon_vga_detect(struct drm_connector *connector, bool force)
659 {
660         struct drm_device *dev = connector->dev;
661         struct radeon_device *rdev = dev->dev_private;
662         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
663         struct drm_encoder *encoder;
664         struct drm_encoder_helper_funcs *encoder_funcs;
665         bool dret = false;
666         enum drm_connector_status ret = connector_status_disconnected;
667
668         encoder = radeon_best_single_encoder(connector);
669         if (!encoder)
670                 ret = connector_status_disconnected;
671
672         if (radeon_connector->ddc_bus)
673                 dret = radeon_ddc_probe(radeon_connector);
674         if (dret) {
675                 radeon_connector->detected_by_load = false;
676                 if (radeon_connector->edid) {
677                         kfree(radeon_connector->edid);
678                         radeon_connector->edid = NULL;
679                 }
680                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
681
682                 if (!radeon_connector->edid) {
683                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
684                                         drm_get_connector_name(connector));
685                         ret = connector_status_connected;
686                 } else {
687                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
688
689                         /* some oems have boards with separate digital and analog connectors
690                          * with a shared ddc line (often vga + hdmi)
691                          */
692                         if (radeon_connector->use_digital && radeon_connector->shared_ddc) {
693                                 kfree(radeon_connector->edid);
694                                 radeon_connector->edid = NULL;
695                                 ret = connector_status_disconnected;
696                         } else
697                                 ret = connector_status_connected;
698                 }
699         } else {
700
701                 /* if we aren't forcing don't do destructive polling */
702                 if (!force) {
703                         /* only return the previous status if we last
704                          * detected a monitor via load.
705                          */
706                         if (radeon_connector->detected_by_load)
707                                 return connector->status;
708                         else
709                                 return ret;
710                 }
711
712                 if (radeon_connector->dac_load_detect && encoder) {
713                         encoder_funcs = encoder->helper_private;
714                         ret = encoder_funcs->detect(encoder, connector);
715                         if (ret != connector_status_disconnected)
716                                 radeon_connector->detected_by_load = true;
717                 }
718         }
719
720         if (ret == connector_status_connected)
721                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
722
723         /* RN50 and some RV100 asics in servers often have a hardcoded EDID in the
724          * vbios to deal with KVMs. If we have one and are not able to detect a monitor
725          * by other means, assume the CRT is connected and use that EDID.
726          */
727         if ((!rdev->is_atom_bios) &&
728             (ret == connector_status_disconnected) &&
729             rdev->mode_info.bios_hardcoded_edid_size) {
730                 ret = connector_status_connected;
731         }
732
733         radeon_connector_update_scratch_regs(connector, ret);
734         return ret;
735 }
736
737 struct drm_connector_helper_funcs radeon_vga_connector_helper_funcs = {
738         .get_modes = radeon_vga_get_modes,
739         .mode_valid = radeon_vga_mode_valid,
740         .best_encoder = radeon_best_single_encoder,
741 };
742
743 struct drm_connector_funcs radeon_vga_connector_funcs = {
744         .dpms = drm_helper_connector_dpms,
745         .detect = radeon_vga_detect,
746         .fill_modes = drm_helper_probe_single_connector_modes,
747         .destroy = radeon_connector_destroy,
748         .set_property = radeon_connector_set_property,
749 };
750
751 static int radeon_tv_get_modes(struct drm_connector *connector)
752 {
753         struct drm_device *dev = connector->dev;
754         struct radeon_device *rdev = dev->dev_private;
755         struct drm_display_mode *tv_mode;
756         struct drm_encoder *encoder;
757
758         encoder = radeon_best_single_encoder(connector);
759         if (!encoder)
760                 return 0;
761
762         /* avivo chips can scale any mode */
763         if (rdev->family >= CHIP_RS600)
764                 /* add scaled modes */
765                 radeon_add_common_modes(encoder, connector);
766         else {
767                 /* only 800x600 is supported right now on pre-avivo chips */
768                 tv_mode = drm_cvt_mode(dev, 800, 600, 60, false, false, false);
769                 tv_mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
770                 drm_mode_probed_add(connector, tv_mode);
771         }
772         return 1;
773 }
774
775 static int radeon_tv_mode_valid(struct drm_connector *connector,
776                                 struct drm_display_mode *mode)
777 {
778         if ((mode->hdisplay > 1024) || (mode->vdisplay > 768))
779                 return MODE_CLOCK_RANGE;
780         return MODE_OK;
781 }
782
783 static enum drm_connector_status
784 radeon_tv_detect(struct drm_connector *connector, bool force)
785 {
786         struct drm_encoder *encoder;
787         struct drm_encoder_helper_funcs *encoder_funcs;
788         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
789         enum drm_connector_status ret = connector_status_disconnected;
790
791         if (!radeon_connector->dac_load_detect)
792                 return ret;
793
794         encoder = radeon_best_single_encoder(connector);
795         if (!encoder)
796                 ret = connector_status_disconnected;
797         else {
798                 encoder_funcs = encoder->helper_private;
799                 ret = encoder_funcs->detect(encoder, connector);
800         }
801         if (ret == connector_status_connected)
802                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, false);
803         radeon_connector_update_scratch_regs(connector, ret);
804         return ret;
805 }
806
807 struct drm_connector_helper_funcs radeon_tv_connector_helper_funcs = {
808         .get_modes = radeon_tv_get_modes,
809         .mode_valid = radeon_tv_mode_valid,
810         .best_encoder = radeon_best_single_encoder,
811 };
812
813 struct drm_connector_funcs radeon_tv_connector_funcs = {
814         .dpms = drm_helper_connector_dpms,
815         .detect = radeon_tv_detect,
816         .fill_modes = drm_helper_probe_single_connector_modes,
817         .destroy = radeon_connector_destroy,
818         .set_property = radeon_connector_set_property,
819 };
820
821 static int radeon_dvi_get_modes(struct drm_connector *connector)
822 {
823         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
824         int ret;
825
826         ret = radeon_ddc_get_modes(radeon_connector);
827         return ret;
828 }
829
830 static bool radeon_check_hpd_status_unchanged(struct drm_connector *connector)
831 {
832         struct drm_device *dev = connector->dev;
833         struct radeon_device *rdev = dev->dev_private;
834         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
835         enum drm_connector_status status;
836
837         /* We only trust HPD on R600 and newer ASICS. */
838         if (rdev->family >= CHIP_R600
839           && radeon_connector->hpd.hpd != RADEON_HPD_NONE) {
840                 if (radeon_hpd_sense(rdev, radeon_connector->hpd.hpd))
841                         status = connector_status_connected;
842                 else
843                         status = connector_status_disconnected;
844                 if (connector->status == status)
845                         return true;
846         }
847
848         return false;
849 }
850
851 /*
852  * DVI is complicated
853  * Do a DDC probe, if DDC probe passes, get the full EDID so
854  * we can do analog/digital monitor detection at this point.
855  * If the monitor is an analog monitor or we got no DDC,
856  * we need to find the DAC encoder object for this connector.
857  * If we got no DDC, we do load detection on the DAC encoder object.
858  * If we got analog DDC or load detection passes on the DAC encoder
859  * we have to check if this analog encoder is shared with anyone else (TV)
860  * if its shared we have to set the other connector to disconnected.
861  */
862 static enum drm_connector_status
863 radeon_dvi_detect(struct drm_connector *connector, bool force)
864 {
865         struct drm_device *dev = connector->dev;
866         struct radeon_device *rdev = dev->dev_private;
867         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
868         struct drm_encoder *encoder = NULL;
869         struct drm_encoder_helper_funcs *encoder_funcs;
870         struct drm_mode_object *obj;
871         int i;
872         enum drm_connector_status ret = connector_status_disconnected;
873         bool dret = false;
874
875         if (!force && radeon_check_hpd_status_unchanged(connector))
876                 return connector->status;
877
878         if (radeon_connector->ddc_bus)
879                 dret = radeon_ddc_probe(radeon_connector);
880         if (dret) {
881                 radeon_connector->detected_by_load = false;
882                 if (radeon_connector->edid) {
883                         kfree(radeon_connector->edid);
884                         radeon_connector->edid = NULL;
885                 }
886                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
887
888                 if (!radeon_connector->edid) {
889                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
890                                         drm_get_connector_name(connector));
891                         /* rs690 seems to have a problem with connectors not existing and always
892                          * return a block of 0's. If we see this just stop polling on this output */
893                         if ((rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) && radeon_connector->base.null_edid_counter) {
894                                 ret = connector_status_disconnected;
895                                 DRM_ERROR("%s: detected RS690 floating bus bug, stopping ddc detect\n", drm_get_connector_name(connector));
896                                 radeon_connector->ddc_bus = NULL;
897                         }
898                 } else {
899                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
900
901                         /* some oems have boards with separate digital and analog connectors
902                          * with a shared ddc line (often vga + hdmi)
903                          */
904                         if ((!radeon_connector->use_digital) && radeon_connector->shared_ddc) {
905                                 kfree(radeon_connector->edid);
906                                 radeon_connector->edid = NULL;
907                                 ret = connector_status_disconnected;
908                         } else
909                                 ret = connector_status_connected;
910
911                         /* This gets complicated.  We have boards with VGA + HDMI with a
912                          * shared DDC line and we have boards with DVI-D + HDMI with a shared
913                          * DDC line.  The latter is more complex because with DVI<->HDMI adapters
914                          * you don't really know what's connected to which port as both are digital.
915                          */
916                         if (radeon_connector->shared_ddc && (ret == connector_status_connected)) {
917                                 struct drm_connector *list_connector;
918                                 struct radeon_connector *list_radeon_connector;
919                                 list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
920                                         if (connector == list_connector)
921                                                 continue;
922                                         list_radeon_connector = to_radeon_connector(list_connector);
923                                         if (list_radeon_connector->shared_ddc &&
924                                             (list_radeon_connector->ddc_bus->rec.i2c_id ==
925                                              radeon_connector->ddc_bus->rec.i2c_id)) {
926                                                 /* cases where both connectors are digital */
927                                                 if (list_connector->connector_type != DRM_MODE_CONNECTOR_VGA) {
928                                                         /* hpd is our only option in this case */
929                                                         if (!radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
930                                                                 kfree(radeon_connector->edid);
931                                                                 radeon_connector->edid = NULL;
932                                                                 ret = connector_status_disconnected;
933                                                         }
934                                                 }
935                                         }
936                                 }
937                         }
938                 }
939         }
940
941         if ((ret == connector_status_connected) && (radeon_connector->use_digital == true))
942                 goto out;
943
944         /* DVI-D and HDMI-A are digital only */
945         if ((connector->connector_type == DRM_MODE_CONNECTOR_DVID) ||
946             (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA))
947                 goto out;
948
949         /* if we aren't forcing don't do destructive polling */
950         if (!force) {
951                 /* only return the previous status if we last
952                  * detected a monitor via load.
953                  */
954                 if (radeon_connector->detected_by_load)
955                         ret = connector->status;
956                 goto out;
957         }
958
959         /* find analog encoder */
960         if (radeon_connector->dac_load_detect) {
961                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
962                         if (connector->encoder_ids[i] == 0)
963                                 break;
964
965                         obj = drm_mode_object_find(connector->dev,
966                                                    connector->encoder_ids[i],
967                                                    DRM_MODE_OBJECT_ENCODER);
968                         if (!obj)
969                                 continue;
970
971                         encoder = obj_to_encoder(obj);
972
973                         if (encoder->encoder_type != DRM_MODE_ENCODER_DAC &&
974                             encoder->encoder_type != DRM_MODE_ENCODER_TVDAC)
975                                 continue;
976
977                         encoder_funcs = encoder->helper_private;
978                         if (encoder_funcs->detect) {
979                                 if (ret != connector_status_connected) {
980                                         ret = encoder_funcs->detect(encoder, connector);
981                                         if (ret == connector_status_connected) {
982                                                 radeon_connector->use_digital = false;
983                                         }
984                                         if (ret != connector_status_disconnected)
985                                                 radeon_connector->detected_by_load = true;
986                                 }
987                                 break;
988                         }
989                 }
990         }
991
992         if ((ret == connector_status_connected) && (radeon_connector->use_digital == false) &&
993             encoder) {
994                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
995         }
996
997         /* RN50 and some RV100 asics in servers often have a hardcoded EDID in the
998          * vbios to deal with KVMs. If we have one and are not able to detect a monitor
999          * by other means, assume the DFP is connected and use that EDID.  In most
1000          * cases the DVI port is actually a virtual KVM port connected to the service
1001          * processor.
1002          */
1003 out:
1004         if ((!rdev->is_atom_bios) &&
1005             (ret == connector_status_disconnected) &&
1006             rdev->mode_info.bios_hardcoded_edid_size) {
1007                 radeon_connector->use_digital = true;
1008                 ret = connector_status_connected;
1009         }
1010
1011         /* updated in get modes as well since we need to know if it's analog or digital */
1012         radeon_connector_update_scratch_regs(connector, ret);
1013         return ret;
1014 }
1015
1016 /* okay need to be smart in here about which encoder to pick */
1017 struct drm_encoder *radeon_dvi_encoder(struct drm_connector *connector)
1018 {
1019         int enc_id = connector->encoder_ids[0];
1020         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1021         struct drm_mode_object *obj;
1022         struct drm_encoder *encoder;
1023         int i;
1024         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1025                 if (connector->encoder_ids[i] == 0)
1026                         break;
1027
1028                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
1029                 if (!obj)
1030                         continue;
1031
1032                 encoder = obj_to_encoder(obj);
1033
1034                 if (radeon_connector->use_digital == true) {
1035                         if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
1036                                 return encoder;
1037                 } else {
1038                         if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
1039                             encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1040                                 return encoder;
1041                 }
1042         }
1043
1044         /* see if we have a default encoder  TODO */
1045
1046         /* then check use digitial */
1047         /* pick the first one */
1048         if (enc_id) {
1049                 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
1050                 if (!obj)
1051                         return NULL;
1052                 encoder = obj_to_encoder(obj);
1053                 return encoder;
1054         }
1055         return NULL;
1056 }
1057
1058 static void radeon_dvi_force(struct drm_connector *connector)
1059 {
1060         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1061         if (connector->force == DRM_FORCE_ON)
1062                 radeon_connector->use_digital = false;
1063         if (connector->force == DRM_FORCE_ON_DIGITAL)
1064                 radeon_connector->use_digital = true;
1065 }
1066
1067 static int radeon_dvi_mode_valid(struct drm_connector *connector,
1068                                   struct drm_display_mode *mode)
1069 {
1070         struct drm_device *dev = connector->dev;
1071         struct radeon_device *rdev = dev->dev_private;
1072         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1073
1074         /* XXX check mode bandwidth */
1075
1076         /* clocks over 135 MHz have heat issues with DVI on RV100 */
1077         if (radeon_connector->use_digital &&
1078             (rdev->family == CHIP_RV100) &&
1079             (mode->clock > 135000))
1080                 return MODE_CLOCK_HIGH;
1081
1082         if (radeon_connector->use_digital && (mode->clock > 165000)) {
1083                 if ((radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
1084                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
1085                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B))
1086                         return MODE_OK;
1087                 else if (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_A) {
1088                         if (ASIC_IS_DCE6(rdev)) {
1089                                 /* HDMI 1.3+ supports max clock of 340 Mhz */
1090                                 if (mode->clock > 340000)
1091                                         return MODE_CLOCK_HIGH;
1092                                 else
1093                                         return MODE_OK;
1094                         } else
1095                                 return MODE_CLOCK_HIGH;
1096                 } else
1097                         return MODE_CLOCK_HIGH;
1098         }
1099
1100         /* check against the max pixel clock */
1101         if ((mode->clock / 10) > rdev->clock.max_pixel_clock)
1102                 return MODE_CLOCK_HIGH;
1103
1104         return MODE_OK;
1105 }
1106
1107 struct drm_connector_helper_funcs radeon_dvi_connector_helper_funcs = {
1108         .get_modes = radeon_dvi_get_modes,
1109         .mode_valid = radeon_dvi_mode_valid,
1110         .best_encoder = radeon_dvi_encoder,
1111 };
1112
1113 struct drm_connector_funcs radeon_dvi_connector_funcs = {
1114         .dpms = drm_helper_connector_dpms,
1115         .detect = radeon_dvi_detect,
1116         .fill_modes = drm_helper_probe_single_connector_modes,
1117         .set_property = radeon_connector_set_property,
1118         .destroy = radeon_connector_destroy,
1119         .force = radeon_dvi_force,
1120 };
1121
1122 static void radeon_dp_connector_destroy(struct drm_connector *connector)
1123 {
1124         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1125         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1126
1127         if (radeon_connector->edid)
1128                 kfree(radeon_connector->edid);
1129         if (radeon_dig_connector->dp_i2c_bus)
1130                 radeon_i2c_destroy(radeon_dig_connector->dp_i2c_bus);
1131         kfree(radeon_connector->con_priv);
1132         drm_sysfs_connector_remove(connector);
1133         drm_connector_cleanup(connector);
1134         kfree(connector);
1135 }
1136
1137 static int radeon_dp_get_modes(struct drm_connector *connector)
1138 {
1139         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1140         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1141         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1142         int ret;
1143
1144         if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1145             (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1146                 struct drm_display_mode *mode;
1147
1148                 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1149                         if (!radeon_dig_connector->edp_on)
1150                                 atombios_set_edp_panel_power(connector,
1151                                                              ATOM_TRANSMITTER_ACTION_POWER_ON);
1152                         ret = radeon_ddc_get_modes(radeon_connector);
1153                         if (!radeon_dig_connector->edp_on)
1154                                 atombios_set_edp_panel_power(connector,
1155                                                              ATOM_TRANSMITTER_ACTION_POWER_OFF);
1156                 } else {
1157                         /* need to setup ddc on the bridge */
1158                         if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1159                             ENCODER_OBJECT_ID_NONE) {
1160                                 if (encoder)
1161                                         radeon_atom_ext_encoder_setup_ddc(encoder);
1162                         }
1163                         ret = radeon_ddc_get_modes(radeon_connector);
1164                 }
1165
1166                 if (ret > 0) {
1167                         if (encoder) {
1168                                 radeon_fixup_lvds_native_mode(encoder, connector);
1169                                 /* add scaled modes */
1170                                 radeon_add_common_modes(encoder, connector);
1171                         }
1172                         return ret;
1173                 }
1174
1175                 if (!encoder)
1176                         return 0;
1177
1178                 /* we have no EDID modes */
1179                 mode = radeon_fp_native_mode(encoder);
1180                 if (mode) {
1181                         ret = 1;
1182                         drm_mode_probed_add(connector, mode);
1183                         /* add the width/height from vbios tables if available */
1184                         connector->display_info.width_mm = mode->width_mm;
1185                         connector->display_info.height_mm = mode->height_mm;
1186                         /* add scaled modes */
1187                         radeon_add_common_modes(encoder, connector);
1188                 }
1189         } else {
1190                 /* need to setup ddc on the bridge */
1191                 if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1192                         ENCODER_OBJECT_ID_NONE) {
1193                         if (encoder)
1194                                 radeon_atom_ext_encoder_setup_ddc(encoder);
1195                 }
1196                 ret = radeon_ddc_get_modes(radeon_connector);
1197         }
1198
1199         return ret;
1200 }
1201
1202 u16 radeon_connector_encoder_get_dp_bridge_encoder_id(struct drm_connector *connector)
1203 {
1204         struct drm_mode_object *obj;
1205         struct drm_encoder *encoder;
1206         struct radeon_encoder *radeon_encoder;
1207         int i;
1208
1209         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1210                 if (connector->encoder_ids[i] == 0)
1211                         break;
1212
1213                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
1214                 if (!obj)
1215                         continue;
1216
1217                 encoder = obj_to_encoder(obj);
1218                 radeon_encoder = to_radeon_encoder(encoder);
1219
1220                 switch (radeon_encoder->encoder_id) {
1221                 case ENCODER_OBJECT_ID_TRAVIS:
1222                 case ENCODER_OBJECT_ID_NUTMEG:
1223                         return radeon_encoder->encoder_id;
1224                 default:
1225                         break;
1226                 }
1227         }
1228
1229         return ENCODER_OBJECT_ID_NONE;
1230 }
1231
1232 bool radeon_connector_encoder_is_hbr2(struct drm_connector *connector)
1233 {
1234         struct drm_mode_object *obj;
1235         struct drm_encoder *encoder;
1236         struct radeon_encoder *radeon_encoder;
1237         int i;
1238         bool found = false;
1239
1240         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1241                 if (connector->encoder_ids[i] == 0)
1242                         break;
1243
1244                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
1245                 if (!obj)
1246                         continue;
1247
1248                 encoder = obj_to_encoder(obj);
1249                 radeon_encoder = to_radeon_encoder(encoder);
1250                 if (radeon_encoder->caps & ATOM_ENCODER_CAP_RECORD_HBR2)
1251                         found = true;
1252         }
1253
1254         return found;
1255 }
1256
1257 bool radeon_connector_is_dp12_capable(struct drm_connector *connector)
1258 {
1259         struct drm_device *dev = connector->dev;
1260         struct radeon_device *rdev = dev->dev_private;
1261
1262         if (ASIC_IS_DCE5(rdev) &&
1263             (rdev->clock.dp_extclk >= 53900) &&
1264             radeon_connector_encoder_is_hbr2(connector)) {
1265                 return true;
1266         }
1267
1268         return false;
1269 }
1270
1271 static enum drm_connector_status
1272 radeon_dp_detect(struct drm_connector *connector, bool force)
1273 {
1274         struct drm_device *dev = connector->dev;
1275         struct radeon_device *rdev = dev->dev_private;
1276         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1277         enum drm_connector_status ret = connector_status_disconnected;
1278         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1279         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1280
1281         if (!force && radeon_check_hpd_status_unchanged(connector))
1282                 return connector->status;
1283
1284         if (radeon_connector->edid) {
1285                 kfree(radeon_connector->edid);
1286                 radeon_connector->edid = NULL;
1287         }
1288
1289         if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1290             (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1291                 if (encoder) {
1292                         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1293                         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
1294
1295                         /* check if panel is valid */
1296                         if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
1297                                 ret = connector_status_connected;
1298                 }
1299                 /* eDP is always DP */
1300                 radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1301                 if (!radeon_dig_connector->edp_on)
1302                         atombios_set_edp_panel_power(connector,
1303                                                      ATOM_TRANSMITTER_ACTION_POWER_ON);
1304                 if (radeon_dp_getdpcd(radeon_connector))
1305                         ret = connector_status_connected;
1306                 if (!radeon_dig_connector->edp_on)
1307                         atombios_set_edp_panel_power(connector,
1308                                                      ATOM_TRANSMITTER_ACTION_POWER_OFF);
1309         } else if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1310                    ENCODER_OBJECT_ID_NONE) {
1311                 /* DP bridges are always DP */
1312                 radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1313                 /* get the DPCD from the bridge */
1314                 radeon_dp_getdpcd(radeon_connector);
1315
1316                 if (encoder) {
1317                         /* setup ddc on the bridge */
1318                         radeon_atom_ext_encoder_setup_ddc(encoder);
1319                         if (radeon_ddc_probe(radeon_connector)) /* try DDC */
1320                                 ret = connector_status_connected;
1321                         else if (radeon_connector->dac_load_detect) { /* try load detection */
1322                                 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1323                                 ret = encoder_funcs->detect(encoder, connector);
1324                         }
1325                 }
1326         } else {
1327                 radeon_dig_connector->dp_sink_type = radeon_dp_getsinktype(radeon_connector);
1328                 if (radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
1329                         ret = connector_status_connected;
1330                         if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
1331                                 radeon_dp_getdpcd(radeon_connector);
1332                 } else {
1333                         if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) {
1334                                 if (radeon_dp_getdpcd(radeon_connector))
1335                                         ret = connector_status_connected;
1336                         } else {
1337                                 if (radeon_ddc_probe(radeon_connector))
1338                                         ret = connector_status_connected;
1339                         }
1340                 }
1341         }
1342
1343         radeon_connector_update_scratch_regs(connector, ret);
1344         return ret;
1345 }
1346
1347 static int radeon_dp_mode_valid(struct drm_connector *connector,
1348                                   struct drm_display_mode *mode)
1349 {
1350         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1351         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1352
1353         /* XXX check mode bandwidth */
1354
1355         if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1356             (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1357                 struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1358
1359                 if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
1360                         return MODE_PANEL;
1361
1362                 if (encoder) {
1363                         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1364                         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
1365
1366                         /* AVIVO hardware supports downscaling modes larger than the panel
1367                          * to the panel size, but I'm not sure this is desirable.
1368                          */
1369                         if ((mode->hdisplay > native_mode->hdisplay) ||
1370                             (mode->vdisplay > native_mode->vdisplay))
1371                                 return MODE_PANEL;
1372
1373                         /* if scaling is disabled, block non-native modes */
1374                         if (radeon_encoder->rmx_type == RMX_OFF) {
1375                                 if ((mode->hdisplay != native_mode->hdisplay) ||
1376                                     (mode->vdisplay != native_mode->vdisplay))
1377                                         return MODE_PANEL;
1378                         }
1379                 }
1380                 return MODE_OK;
1381         } else {
1382                 if ((radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1383                     (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
1384                         return radeon_dp_mode_valid_helper(connector, mode);
1385                 else
1386                         return MODE_OK;
1387         }
1388 }
1389
1390 struct drm_connector_helper_funcs radeon_dp_connector_helper_funcs = {
1391         .get_modes = radeon_dp_get_modes,
1392         .mode_valid = radeon_dp_mode_valid,
1393         .best_encoder = radeon_dvi_encoder,
1394 };
1395
1396 struct drm_connector_funcs radeon_dp_connector_funcs = {
1397         .dpms = drm_helper_connector_dpms,
1398         .detect = radeon_dp_detect,
1399         .fill_modes = drm_helper_probe_single_connector_modes,
1400         .set_property = radeon_connector_set_property,
1401         .destroy = radeon_dp_connector_destroy,
1402         .force = radeon_dvi_force,
1403 };
1404
1405 void
1406 radeon_add_atom_connector(struct drm_device *dev,
1407                           uint32_t connector_id,
1408                           uint32_t supported_device,
1409                           int connector_type,
1410                           struct radeon_i2c_bus_rec *i2c_bus,
1411                           uint32_t igp_lane_info,
1412                           uint16_t connector_object_id,
1413                           struct radeon_hpd *hpd,
1414                           struct radeon_router *router)
1415 {
1416         struct radeon_device *rdev = dev->dev_private;
1417         struct drm_connector *connector;
1418         struct radeon_connector *radeon_connector;
1419         struct radeon_connector_atom_dig *radeon_dig_connector;
1420         struct drm_encoder *encoder;
1421         struct radeon_encoder *radeon_encoder;
1422         uint32_t subpixel_order = SubPixelNone;
1423         bool shared_ddc = false;
1424         bool is_dp_bridge = false;
1425
1426         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1427                 return;
1428
1429         /* if the user selected tv=0 don't try and add the connector */
1430         if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1431              (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1432              (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1433             (radeon_tv == 0))
1434                 return;
1435
1436         /* see if we already added it */
1437         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1438                 radeon_connector = to_radeon_connector(connector);
1439                 if (radeon_connector->connector_id == connector_id) {
1440                         radeon_connector->devices |= supported_device;
1441                         return;
1442                 }
1443                 if (radeon_connector->ddc_bus && i2c_bus->valid) {
1444                         if (radeon_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1445                                 radeon_connector->shared_ddc = true;
1446                                 shared_ddc = true;
1447                         }
1448                         if (radeon_connector->router_bus && router->ddc_valid &&
1449                             (radeon_connector->router.router_id == router->router_id)) {
1450                                 radeon_connector->shared_ddc = false;
1451                                 shared_ddc = false;
1452                         }
1453                 }
1454         }
1455
1456         /* check if it's a dp bridge */
1457         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1458                 radeon_encoder = to_radeon_encoder(encoder);
1459                 if (radeon_encoder->devices & supported_device) {
1460                         switch (radeon_encoder->encoder_id) {
1461                         case ENCODER_OBJECT_ID_TRAVIS:
1462                         case ENCODER_OBJECT_ID_NUTMEG:
1463                                 is_dp_bridge = true;
1464                                 break;
1465                         default:
1466                                 break;
1467                         }
1468                 }
1469         }
1470
1471         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1472         if (!radeon_connector)
1473                 return;
1474
1475         connector = &radeon_connector->base;
1476
1477         radeon_connector->connector_id = connector_id;
1478         radeon_connector->devices = supported_device;
1479         radeon_connector->shared_ddc = shared_ddc;
1480         radeon_connector->connector_object_id = connector_object_id;
1481         radeon_connector->hpd = *hpd;
1482
1483         radeon_connector->router = *router;
1484         if (router->ddc_valid || router->cd_valid) {
1485                 radeon_connector->router_bus = radeon_i2c_lookup(rdev, &router->i2c_info);
1486                 if (!radeon_connector->router_bus)
1487                         DRM_ERROR("Failed to assign router i2c bus! Check dmesg for i2c errors.\n");
1488         }
1489
1490         if (is_dp_bridge) {
1491                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1492                 if (!radeon_dig_connector)
1493                         goto failed;
1494                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1495                 radeon_connector->con_priv = radeon_dig_connector;
1496                 drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1497                 drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1498                 if (i2c_bus->valid) {
1499                         /* add DP i2c bus */
1500                         if (connector_type == DRM_MODE_CONNECTOR_eDP)
1501                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "eDP-auxch");
1502                         else
1503                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1504                         if (!radeon_dig_connector->dp_i2c_bus)
1505                                 DRM_ERROR("DP: Failed to assign dp ddc bus! Check dmesg for i2c errors.\n");
1506                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1507                         if (!radeon_connector->ddc_bus)
1508                                 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1509                 }
1510                 switch (connector_type) {
1511                 case DRM_MODE_CONNECTOR_VGA:
1512                 case DRM_MODE_CONNECTOR_DVIA:
1513                 default:
1514                         connector->interlace_allowed = true;
1515                         connector->doublescan_allowed = true;
1516                         radeon_connector->dac_load_detect = true;
1517                         drm_connector_attach_property(&radeon_connector->base,
1518                                                       rdev->mode_info.load_detect_property,
1519                                                       1);
1520                         break;
1521                 case DRM_MODE_CONNECTOR_DVII:
1522                 case DRM_MODE_CONNECTOR_DVID:
1523                 case DRM_MODE_CONNECTOR_HDMIA:
1524                 case DRM_MODE_CONNECTOR_HDMIB:
1525                 case DRM_MODE_CONNECTOR_DisplayPort:
1526                         drm_connector_attach_property(&radeon_connector->base,
1527                                                       rdev->mode_info.underscan_property,
1528                                                       UNDERSCAN_OFF);
1529                         drm_connector_attach_property(&radeon_connector->base,
1530                                                       rdev->mode_info.underscan_hborder_property,
1531                                                       0);
1532                         drm_connector_attach_property(&radeon_connector->base,
1533                                                       rdev->mode_info.underscan_vborder_property,
1534                                                       0);
1535                         subpixel_order = SubPixelHorizontalRGB;
1536                         connector->interlace_allowed = true;
1537                         if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1538                                 connector->doublescan_allowed = true;
1539                         else
1540                                 connector->doublescan_allowed = false;
1541                         if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1542                                 radeon_connector->dac_load_detect = true;
1543                                 drm_connector_attach_property(&radeon_connector->base,
1544                                                               rdev->mode_info.load_detect_property,
1545                                                               1);
1546                         }
1547                         break;
1548                 case DRM_MODE_CONNECTOR_LVDS:
1549                 case DRM_MODE_CONNECTOR_eDP:
1550                         drm_connector_attach_property(&radeon_connector->base,
1551                                                       dev->mode_config.scaling_mode_property,
1552                                                       DRM_MODE_SCALE_FULLSCREEN);
1553                         subpixel_order = SubPixelHorizontalRGB;
1554                         connector->interlace_allowed = false;
1555                         connector->doublescan_allowed = false;
1556                         break;
1557                 }
1558         } else {
1559                 switch (connector_type) {
1560                 case DRM_MODE_CONNECTOR_VGA:
1561                         drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1562                         drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1563                         if (i2c_bus->valid) {
1564                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1565                                 if (!radeon_connector->ddc_bus)
1566                                         DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1567                         }
1568                         radeon_connector->dac_load_detect = true;
1569                         drm_connector_attach_property(&radeon_connector->base,
1570                                                       rdev->mode_info.load_detect_property,
1571                                                       1);
1572                         /* no HPD on analog connectors */
1573                         radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1574                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1575                         connector->interlace_allowed = true;
1576                         connector->doublescan_allowed = true;
1577                         break;
1578                 case DRM_MODE_CONNECTOR_DVIA:
1579                         drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1580                         drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1581                         if (i2c_bus->valid) {
1582                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1583                                 if (!radeon_connector->ddc_bus)
1584                                         DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1585                         }
1586                         radeon_connector->dac_load_detect = true;
1587                         drm_connector_attach_property(&radeon_connector->base,
1588                                                       rdev->mode_info.load_detect_property,
1589                                                       1);
1590                         /* no HPD on analog connectors */
1591                         radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1592                         connector->interlace_allowed = true;
1593                         connector->doublescan_allowed = true;
1594                         break;
1595                 case DRM_MODE_CONNECTOR_DVII:
1596                 case DRM_MODE_CONNECTOR_DVID:
1597                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1598                         if (!radeon_dig_connector)
1599                                 goto failed;
1600                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1601                         radeon_connector->con_priv = radeon_dig_connector;
1602                         drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1603                         drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1604                         if (i2c_bus->valid) {
1605                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1606                                 if (!radeon_connector->ddc_bus)
1607                                         DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1608                         }
1609                         subpixel_order = SubPixelHorizontalRGB;
1610                         drm_connector_attach_property(&radeon_connector->base,
1611                                                       rdev->mode_info.coherent_mode_property,
1612                                                       1);
1613                         if (ASIC_IS_AVIVO(rdev)) {
1614                                 drm_connector_attach_property(&radeon_connector->base,
1615                                                               rdev->mode_info.underscan_property,
1616                                                               UNDERSCAN_OFF);
1617                                 drm_connector_attach_property(&radeon_connector->base,
1618                                                               rdev->mode_info.underscan_hborder_property,
1619                                                               0);
1620                                 drm_connector_attach_property(&radeon_connector->base,
1621                                                               rdev->mode_info.underscan_vborder_property,
1622                                                               0);
1623                         }
1624                         if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1625                                 radeon_connector->dac_load_detect = true;
1626                                 drm_connector_attach_property(&radeon_connector->base,
1627                                                               rdev->mode_info.load_detect_property,
1628                                                               1);
1629                         }
1630                         connector->interlace_allowed = true;
1631                         if (connector_type == DRM_MODE_CONNECTOR_DVII)
1632                                 connector->doublescan_allowed = true;
1633                         else
1634                                 connector->doublescan_allowed = false;
1635                         break;
1636                 case DRM_MODE_CONNECTOR_HDMIA:
1637                 case DRM_MODE_CONNECTOR_HDMIB:
1638                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1639                         if (!radeon_dig_connector)
1640                                 goto failed;
1641                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1642                         radeon_connector->con_priv = radeon_dig_connector;
1643                         drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1644                         drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1645                         if (i2c_bus->valid) {
1646                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1647                                 if (!radeon_connector->ddc_bus)
1648                                         DRM_ERROR("HDMI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1649                         }
1650                         drm_connector_attach_property(&radeon_connector->base,
1651                                                       rdev->mode_info.coherent_mode_property,
1652                                                       1);
1653                         if (ASIC_IS_AVIVO(rdev)) {
1654                                 drm_connector_attach_property(&radeon_connector->base,
1655                                                               rdev->mode_info.underscan_property,
1656                                                               UNDERSCAN_OFF);
1657                                 drm_connector_attach_property(&radeon_connector->base,
1658                                                               rdev->mode_info.underscan_hborder_property,
1659                                                               0);
1660                                 drm_connector_attach_property(&radeon_connector->base,
1661                                                               rdev->mode_info.underscan_vborder_property,
1662                                                               0);
1663                         }
1664                         subpixel_order = SubPixelHorizontalRGB;
1665                         connector->interlace_allowed = true;
1666                         if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1667                                 connector->doublescan_allowed = true;
1668                         else
1669                                 connector->doublescan_allowed = false;
1670                         break;
1671                 case DRM_MODE_CONNECTOR_DisplayPort:
1672                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1673                         if (!radeon_dig_connector)
1674                                 goto failed;
1675                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1676                         radeon_connector->con_priv = radeon_dig_connector;
1677                         drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1678                         drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1679                         if (i2c_bus->valid) {
1680                                 /* add DP i2c bus */
1681                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1682                                 if (!radeon_dig_connector->dp_i2c_bus)
1683                                         DRM_ERROR("DP: Failed to assign dp ddc bus! Check dmesg for i2c errors.\n");
1684                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1685                                 if (!radeon_connector->ddc_bus)
1686                                         DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1687                         }
1688                         subpixel_order = SubPixelHorizontalRGB;
1689                         drm_connector_attach_property(&radeon_connector->base,
1690                                                       rdev->mode_info.coherent_mode_property,
1691                                                       1);
1692                         if (ASIC_IS_AVIVO(rdev)) {
1693                                 drm_connector_attach_property(&radeon_connector->base,
1694                                                               rdev->mode_info.underscan_property,
1695                                                               UNDERSCAN_OFF);
1696                                 drm_connector_attach_property(&radeon_connector->base,
1697                                                               rdev->mode_info.underscan_hborder_property,
1698                                                               0);
1699                                 drm_connector_attach_property(&radeon_connector->base,
1700                                                               rdev->mode_info.underscan_vborder_property,
1701                                                               0);
1702                         }
1703                         connector->interlace_allowed = true;
1704                         /* in theory with a DP to VGA converter... */
1705                         connector->doublescan_allowed = false;
1706                         break;
1707                 case DRM_MODE_CONNECTOR_eDP:
1708                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1709                         if (!radeon_dig_connector)
1710                                 goto failed;
1711                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1712                         radeon_connector->con_priv = radeon_dig_connector;
1713                         drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1714                         drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1715                         if (i2c_bus->valid) {
1716                                 /* add DP i2c bus */
1717                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "eDP-auxch");
1718                                 if (!radeon_dig_connector->dp_i2c_bus)
1719                                         DRM_ERROR("DP: Failed to assign dp ddc bus! Check dmesg for i2c errors.\n");
1720                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1721                                 if (!radeon_connector->ddc_bus)
1722                                         DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1723                         }
1724                         drm_connector_attach_property(&radeon_connector->base,
1725                                                       dev->mode_config.scaling_mode_property,
1726                                                       DRM_MODE_SCALE_FULLSCREEN);
1727                         subpixel_order = SubPixelHorizontalRGB;
1728                         connector->interlace_allowed = false;
1729                         connector->doublescan_allowed = false;
1730                         break;
1731                 case DRM_MODE_CONNECTOR_SVIDEO:
1732                 case DRM_MODE_CONNECTOR_Composite:
1733                 case DRM_MODE_CONNECTOR_9PinDIN:
1734                         drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1735                         drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1736                         radeon_connector->dac_load_detect = true;
1737                         drm_connector_attach_property(&radeon_connector->base,
1738                                                       rdev->mode_info.load_detect_property,
1739                                                       1);
1740                         drm_connector_attach_property(&radeon_connector->base,
1741                                                       rdev->mode_info.tv_std_property,
1742                                                       radeon_atombios_get_tv_info(rdev));
1743                         /* no HPD on analog connectors */
1744                         radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1745                         connector->interlace_allowed = false;
1746                         connector->doublescan_allowed = false;
1747                         break;
1748                 case DRM_MODE_CONNECTOR_LVDS:
1749                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1750                         if (!radeon_dig_connector)
1751                                 goto failed;
1752                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1753                         radeon_connector->con_priv = radeon_dig_connector;
1754                         drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1755                         drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1756                         if (i2c_bus->valid) {
1757                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1758                                 if (!radeon_connector->ddc_bus)
1759                                         DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1760                         }
1761                         drm_connector_attach_property(&radeon_connector->base,
1762                                                       dev->mode_config.scaling_mode_property,
1763                                                       DRM_MODE_SCALE_FULLSCREEN);
1764                         subpixel_order = SubPixelHorizontalRGB;
1765                         connector->interlace_allowed = false;
1766                         connector->doublescan_allowed = false;
1767                         break;
1768                 }
1769         }
1770
1771         if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
1772                 if (i2c_bus->valid)
1773                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1774         } else
1775                 connector->polled = DRM_CONNECTOR_POLL_HPD;
1776
1777         connector->display_info.subpixel_order = subpixel_order;
1778         drm_sysfs_connector_add(connector);
1779         return;
1780
1781 failed:
1782         drm_connector_cleanup(connector);
1783         kfree(connector);
1784 }
1785
1786 void
1787 radeon_add_legacy_connector(struct drm_device *dev,
1788                             uint32_t connector_id,
1789                             uint32_t supported_device,
1790                             int connector_type,
1791                             struct radeon_i2c_bus_rec *i2c_bus,
1792                             uint16_t connector_object_id,
1793                             struct radeon_hpd *hpd)
1794 {
1795         struct radeon_device *rdev = dev->dev_private;
1796         struct drm_connector *connector;
1797         struct radeon_connector *radeon_connector;
1798         uint32_t subpixel_order = SubPixelNone;
1799
1800         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1801                 return;
1802
1803         /* if the user selected tv=0 don't try and add the connector */
1804         if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1805              (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1806              (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1807             (radeon_tv == 0))
1808                 return;
1809
1810         /* see if we already added it */
1811         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1812                 radeon_connector = to_radeon_connector(connector);
1813                 if (radeon_connector->connector_id == connector_id) {
1814                         radeon_connector->devices |= supported_device;
1815                         return;
1816                 }
1817         }
1818
1819         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1820         if (!radeon_connector)
1821                 return;
1822
1823         connector = &radeon_connector->base;
1824
1825         radeon_connector->connector_id = connector_id;
1826         radeon_connector->devices = supported_device;
1827         radeon_connector->connector_object_id = connector_object_id;
1828         radeon_connector->hpd = *hpd;
1829
1830         switch (connector_type) {
1831         case DRM_MODE_CONNECTOR_VGA:
1832                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1833                 drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1834                 if (i2c_bus->valid) {
1835                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1836                         if (!radeon_connector->ddc_bus)
1837                                 DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1838                 }
1839                 radeon_connector->dac_load_detect = true;
1840                 drm_connector_attach_property(&radeon_connector->base,
1841                                               rdev->mode_info.load_detect_property,
1842                                               1);
1843                 /* no HPD on analog connectors */
1844                 radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1845                 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1846                 connector->interlace_allowed = true;
1847                 connector->doublescan_allowed = true;
1848                 break;
1849         case DRM_MODE_CONNECTOR_DVIA:
1850                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1851                 drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1852                 if (i2c_bus->valid) {
1853                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1854                         if (!radeon_connector->ddc_bus)
1855                                 DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1856                 }
1857                 radeon_connector->dac_load_detect = true;
1858                 drm_connector_attach_property(&radeon_connector->base,
1859                                               rdev->mode_info.load_detect_property,
1860                                               1);
1861                 /* no HPD on analog connectors */
1862                 radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1863                 connector->interlace_allowed = true;
1864                 connector->doublescan_allowed = true;
1865                 break;
1866         case DRM_MODE_CONNECTOR_DVII:
1867         case DRM_MODE_CONNECTOR_DVID:
1868                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1869                 drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1870                 if (i2c_bus->valid) {
1871                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1872                         if (!radeon_connector->ddc_bus)
1873                                 DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1874                 }
1875                 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1876                         radeon_connector->dac_load_detect = true;
1877                         drm_connector_attach_property(&radeon_connector->base,
1878                                                       rdev->mode_info.load_detect_property,
1879                                                       1);
1880                 }
1881                 subpixel_order = SubPixelHorizontalRGB;
1882                 connector->interlace_allowed = true;
1883                 if (connector_type == DRM_MODE_CONNECTOR_DVII)
1884                         connector->doublescan_allowed = true;
1885                 else
1886                         connector->doublescan_allowed = false;
1887                 break;
1888         case DRM_MODE_CONNECTOR_SVIDEO:
1889         case DRM_MODE_CONNECTOR_Composite:
1890         case DRM_MODE_CONNECTOR_9PinDIN:
1891                 drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1892                 drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1893                 radeon_connector->dac_load_detect = true;
1894                 /* RS400,RC410,RS480 chipset seems to report a lot
1895                  * of false positive on load detect, we haven't yet
1896                  * found a way to make load detect reliable on those
1897                  * chipset, thus just disable it for TV.
1898                  */
1899                 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480)
1900                         radeon_connector->dac_load_detect = false;
1901                 drm_connector_attach_property(&radeon_connector->base,
1902                                               rdev->mode_info.load_detect_property,
1903                                               radeon_connector->dac_load_detect);
1904                 drm_connector_attach_property(&radeon_connector->base,
1905                                               rdev->mode_info.tv_std_property,
1906                                               radeon_combios_get_tv_info(rdev));
1907                 /* no HPD on analog connectors */
1908                 radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1909                 connector->interlace_allowed = false;
1910                 connector->doublescan_allowed = false;
1911                 break;
1912         case DRM_MODE_CONNECTOR_LVDS:
1913                 drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1914                 drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1915                 if (i2c_bus->valid) {
1916                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1917                         if (!radeon_connector->ddc_bus)
1918                                 DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1919                 }
1920                 drm_connector_attach_property(&radeon_connector->base,
1921                                               dev->mode_config.scaling_mode_property,
1922                                               DRM_MODE_SCALE_FULLSCREEN);
1923                 subpixel_order = SubPixelHorizontalRGB;
1924                 connector->interlace_allowed = false;
1925                 connector->doublescan_allowed = false;
1926                 break;
1927         }
1928
1929         if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
1930                 if (i2c_bus->valid)
1931                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1932         } else
1933                 connector->polled = DRM_CONNECTOR_POLL_HPD;
1934         connector->display_info.subpixel_order = subpixel_order;
1935         drm_sysfs_connector_add(connector);
1936         if (connector_type == DRM_MODE_CONNECTOR_LVDS) {
1937                 struct drm_encoder *drm_encoder;
1938
1939                 list_for_each_entry(drm_encoder, &dev->mode_config.encoder_list, head) {
1940                         struct radeon_encoder *radeon_encoder;
1941
1942                         radeon_encoder = to_radeon_encoder(drm_encoder);
1943                         if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_LVDS)
1944                                 radeon_legacy_backlight_init(radeon_encoder, connector);
1945                 }
1946         }
1947 }