f7d39ac1862a0e5535ed2c8a9504cc3a694bd184
[pandora-kernel.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 /*
831  * DVI is complicated
832  * Do a DDC probe, if DDC probe passes, get the full EDID so
833  * we can do analog/digital monitor detection at this point.
834  * If the monitor is an analog monitor or we got no DDC,
835  * we need to find the DAC encoder object for this connector.
836  * If we got no DDC, we do load detection on the DAC encoder object.
837  * If we got analog DDC or load detection passes on the DAC encoder
838  * we have to check if this analog encoder is shared with anyone else (TV)
839  * if its shared we have to set the other connector to disconnected.
840  */
841 static enum drm_connector_status
842 radeon_dvi_detect(struct drm_connector *connector, bool force)
843 {
844         struct drm_device *dev = connector->dev;
845         struct radeon_device *rdev = dev->dev_private;
846         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
847         struct drm_encoder *encoder = NULL;
848         struct drm_encoder_helper_funcs *encoder_funcs;
849         struct drm_mode_object *obj;
850         int i;
851         enum drm_connector_status ret = connector_status_disconnected;
852         bool dret = false;
853
854         if (radeon_connector->ddc_bus)
855                 dret = radeon_ddc_probe(radeon_connector);
856         if (dret) {
857                 radeon_connector->detected_by_load = false;
858                 if (radeon_connector->edid) {
859                         kfree(radeon_connector->edid);
860                         radeon_connector->edid = NULL;
861                 }
862                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
863
864                 if (!radeon_connector->edid) {
865                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
866                                         drm_get_connector_name(connector));
867                         /* rs690 seems to have a problem with connectors not existing and always
868                          * return a block of 0's. If we see this just stop polling on this output */
869                         if ((rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) && radeon_connector->base.null_edid_counter) {
870                                 ret = connector_status_disconnected;
871                                 DRM_ERROR("%s: detected RS690 floating bus bug, stopping ddc detect\n", drm_get_connector_name(connector));
872                                 radeon_connector->ddc_bus = NULL;
873                         }
874                 } else {
875                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
876
877                         /* some oems have boards with separate digital and analog connectors
878                          * with a shared ddc line (often vga + hdmi)
879                          */
880                         if ((!radeon_connector->use_digital) && radeon_connector->shared_ddc) {
881                                 kfree(radeon_connector->edid);
882                                 radeon_connector->edid = NULL;
883                                 ret = connector_status_disconnected;
884                         } else
885                                 ret = connector_status_connected;
886
887                         /* This gets complicated.  We have boards with VGA + HDMI with a
888                          * shared DDC line and we have boards with DVI-D + HDMI with a shared
889                          * DDC line.  The latter is more complex because with DVI<->HDMI adapters
890                          * you don't really know what's connected to which port as both are digital.
891                          */
892                         if (radeon_connector->shared_ddc && (ret == connector_status_connected)) {
893                                 struct drm_connector *list_connector;
894                                 struct radeon_connector *list_radeon_connector;
895                                 list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
896                                         if (connector == list_connector)
897                                                 continue;
898                                         list_radeon_connector = to_radeon_connector(list_connector);
899                                         if (list_radeon_connector->shared_ddc &&
900                                             (list_radeon_connector->ddc_bus->rec.i2c_id ==
901                                              radeon_connector->ddc_bus->rec.i2c_id)) {
902                                                 /* cases where both connectors are digital */
903                                                 if (list_connector->connector_type != DRM_MODE_CONNECTOR_VGA) {
904                                                         /* hpd is our only option in this case */
905                                                         if (!radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
906                                                                 kfree(radeon_connector->edid);
907                                                                 radeon_connector->edid = NULL;
908                                                                 ret = connector_status_disconnected;
909                                                         }
910                                                 }
911                                         }
912                                 }
913                         }
914                 }
915         }
916
917         if ((ret == connector_status_connected) && (radeon_connector->use_digital == true))
918                 goto out;
919
920         /* DVI-D and HDMI-A are digital only */
921         if ((connector->connector_type == DRM_MODE_CONNECTOR_DVID) ||
922             (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA))
923                 goto out;
924
925         /* if we aren't forcing don't do destructive polling */
926         if (!force) {
927                 /* only return the previous status if we last
928                  * detected a monitor via load.
929                  */
930                 if (radeon_connector->detected_by_load)
931                         ret = connector->status;
932                 goto out;
933         }
934
935         /* find analog encoder */
936         if (radeon_connector->dac_load_detect) {
937                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
938                         if (connector->encoder_ids[i] == 0)
939                                 break;
940
941                         obj = drm_mode_object_find(connector->dev,
942                                                    connector->encoder_ids[i],
943                                                    DRM_MODE_OBJECT_ENCODER);
944                         if (!obj)
945                                 continue;
946
947                         encoder = obj_to_encoder(obj);
948
949                         if (encoder->encoder_type != DRM_MODE_ENCODER_DAC ||
950                             encoder->encoder_type != DRM_MODE_ENCODER_TVDAC)
951                                 continue;
952
953                         encoder_funcs = encoder->helper_private;
954                         if (encoder_funcs->detect) {
955                                 if (ret != connector_status_connected) {
956                                         ret = encoder_funcs->detect(encoder, connector);
957                                         if (ret == connector_status_connected) {
958                                                 radeon_connector->use_digital = false;
959                                         }
960                                         if (ret != connector_status_disconnected)
961                                                 radeon_connector->detected_by_load = true;
962                                 }
963                                 break;
964                         }
965                 }
966         }
967
968         if ((ret == connector_status_connected) && (radeon_connector->use_digital == false) &&
969             encoder) {
970                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
971         }
972
973         /* RN50 and some RV100 asics in servers often have a hardcoded EDID in the
974          * vbios to deal with KVMs. If we have one and are not able to detect a monitor
975          * by other means, assume the DFP is connected and use that EDID.  In most
976          * cases the DVI port is actually a virtual KVM port connected to the service
977          * processor.
978          */
979         if ((!rdev->is_atom_bios) &&
980             (ret == connector_status_disconnected) &&
981             rdev->mode_info.bios_hardcoded_edid_size) {
982                 radeon_connector->use_digital = true;
983                 ret = connector_status_connected;
984         }
985
986 out:
987         /* updated in get modes as well since we need to know if it's analog or digital */
988         radeon_connector_update_scratch_regs(connector, ret);
989         return ret;
990 }
991
992 /* okay need to be smart in here about which encoder to pick */
993 struct drm_encoder *radeon_dvi_encoder(struct drm_connector *connector)
994 {
995         int enc_id = connector->encoder_ids[0];
996         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
997         struct drm_mode_object *obj;
998         struct drm_encoder *encoder;
999         int i;
1000         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1001                 if (connector->encoder_ids[i] == 0)
1002                         break;
1003
1004                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
1005                 if (!obj)
1006                         continue;
1007
1008                 encoder = obj_to_encoder(obj);
1009
1010                 if (radeon_connector->use_digital == true) {
1011                         if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
1012                                 return encoder;
1013                 } else {
1014                         if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
1015                             encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1016                                 return encoder;
1017                 }
1018         }
1019
1020         /* see if we have a default encoder  TODO */
1021
1022         /* then check use digitial */
1023         /* pick the first one */
1024         if (enc_id) {
1025                 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
1026                 if (!obj)
1027                         return NULL;
1028                 encoder = obj_to_encoder(obj);
1029                 return encoder;
1030         }
1031         return NULL;
1032 }
1033
1034 static void radeon_dvi_force(struct drm_connector *connector)
1035 {
1036         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1037         if (connector->force == DRM_FORCE_ON)
1038                 radeon_connector->use_digital = false;
1039         if (connector->force == DRM_FORCE_ON_DIGITAL)
1040                 radeon_connector->use_digital = true;
1041 }
1042
1043 static int radeon_dvi_mode_valid(struct drm_connector *connector,
1044                                   struct drm_display_mode *mode)
1045 {
1046         struct drm_device *dev = connector->dev;
1047         struct radeon_device *rdev = dev->dev_private;
1048         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1049
1050         /* XXX check mode bandwidth */
1051
1052         /* clocks over 135 MHz have heat issues with DVI on RV100 */
1053         if (radeon_connector->use_digital &&
1054             (rdev->family == CHIP_RV100) &&
1055             (mode->clock > 135000))
1056                 return MODE_CLOCK_HIGH;
1057
1058         if (radeon_connector->use_digital && (mode->clock > 165000)) {
1059                 if ((radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
1060                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
1061                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B))
1062                         return MODE_OK;
1063                 else if (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_A) {
1064                         if (ASIC_IS_DCE3(rdev)) {
1065                                 /* HDMI 1.3+ supports max clock of 340 Mhz */
1066                                 if (mode->clock > 340000)
1067                                         return MODE_CLOCK_HIGH;
1068                                 else
1069                                         return MODE_OK;
1070                         } else
1071                                 return MODE_CLOCK_HIGH;
1072                 } else
1073                         return MODE_CLOCK_HIGH;
1074         }
1075
1076         /* check against the max pixel clock */
1077         if ((mode->clock / 10) > rdev->clock.max_pixel_clock)
1078                 return MODE_CLOCK_HIGH;
1079
1080         return MODE_OK;
1081 }
1082
1083 struct drm_connector_helper_funcs radeon_dvi_connector_helper_funcs = {
1084         .get_modes = radeon_dvi_get_modes,
1085         .mode_valid = radeon_dvi_mode_valid,
1086         .best_encoder = radeon_dvi_encoder,
1087 };
1088
1089 struct drm_connector_funcs radeon_dvi_connector_funcs = {
1090         .dpms = drm_helper_connector_dpms,
1091         .detect = radeon_dvi_detect,
1092         .fill_modes = drm_helper_probe_single_connector_modes,
1093         .set_property = radeon_connector_set_property,
1094         .destroy = radeon_connector_destroy,
1095         .force = radeon_dvi_force,
1096 };
1097
1098 static void radeon_dp_connector_destroy(struct drm_connector *connector)
1099 {
1100         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1101         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1102
1103         if (radeon_connector->edid)
1104                 kfree(radeon_connector->edid);
1105         if (radeon_dig_connector->dp_i2c_bus)
1106                 radeon_i2c_destroy(radeon_dig_connector->dp_i2c_bus);
1107         kfree(radeon_connector->con_priv);
1108         drm_sysfs_connector_remove(connector);
1109         drm_connector_cleanup(connector);
1110         kfree(connector);
1111 }
1112
1113 static int radeon_dp_get_modes(struct drm_connector *connector)
1114 {
1115         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1116         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1117         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1118         int ret;
1119
1120         if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1121             (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1122                 struct drm_display_mode *mode;
1123
1124                 if (!radeon_dig_connector->edp_on)
1125                         atombios_set_edp_panel_power(connector,
1126                                                      ATOM_TRANSMITTER_ACTION_POWER_ON);
1127                 ret = radeon_ddc_get_modes(radeon_connector);
1128                 if (!radeon_dig_connector->edp_on)
1129                         atombios_set_edp_panel_power(connector,
1130                                                      ATOM_TRANSMITTER_ACTION_POWER_OFF);
1131
1132                 if (ret > 0) {
1133                         if (encoder) {
1134                                 radeon_fixup_lvds_native_mode(encoder, connector);
1135                                 /* add scaled modes */
1136                                 radeon_add_common_modes(encoder, connector);
1137                         }
1138                         return ret;
1139                 }
1140
1141                 encoder = radeon_best_single_encoder(connector);
1142                 if (!encoder)
1143                         return 0;
1144
1145                 /* we have no EDID modes */
1146                 mode = radeon_fp_native_mode(encoder);
1147                 if (mode) {
1148                         ret = 1;
1149                         drm_mode_probed_add(connector, mode);
1150                         /* add the width/height from vbios tables if available */
1151                         connector->display_info.width_mm = mode->width_mm;
1152                         connector->display_info.height_mm = mode->height_mm;
1153                         /* add scaled modes */
1154                         radeon_add_common_modes(encoder, connector);
1155                 }
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         return ret;
1167 }
1168
1169 u16 radeon_connector_encoder_get_dp_bridge_encoder_id(struct drm_connector *connector)
1170 {
1171         struct drm_mode_object *obj;
1172         struct drm_encoder *encoder;
1173         struct radeon_encoder *radeon_encoder;
1174         int i;
1175
1176         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1177                 if (connector->encoder_ids[i] == 0)
1178                         break;
1179
1180                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
1181                 if (!obj)
1182                         continue;
1183
1184                 encoder = obj_to_encoder(obj);
1185                 radeon_encoder = to_radeon_encoder(encoder);
1186
1187                 switch (radeon_encoder->encoder_id) {
1188                 case ENCODER_OBJECT_ID_TRAVIS:
1189                 case ENCODER_OBJECT_ID_NUTMEG:
1190                         return radeon_encoder->encoder_id;
1191                 default:
1192                         break;
1193                 }
1194         }
1195
1196         return ENCODER_OBJECT_ID_NONE;
1197 }
1198
1199 bool radeon_connector_encoder_is_hbr2(struct drm_connector *connector)
1200 {
1201         struct drm_mode_object *obj;
1202         struct drm_encoder *encoder;
1203         struct radeon_encoder *radeon_encoder;
1204         int i;
1205         bool found = false;
1206
1207         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1208                 if (connector->encoder_ids[i] == 0)
1209                         break;
1210
1211                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
1212                 if (!obj)
1213                         continue;
1214
1215                 encoder = obj_to_encoder(obj);
1216                 radeon_encoder = to_radeon_encoder(encoder);
1217                 if (radeon_encoder->caps & ATOM_ENCODER_CAP_RECORD_HBR2)
1218                         found = true;
1219         }
1220
1221         return found;
1222 }
1223
1224 bool radeon_connector_is_dp12_capable(struct drm_connector *connector)
1225 {
1226         struct drm_device *dev = connector->dev;
1227         struct radeon_device *rdev = dev->dev_private;
1228
1229         if (ASIC_IS_DCE5(rdev) &&
1230             (rdev->clock.dp_extclk >= 53900) &&
1231             radeon_connector_encoder_is_hbr2(connector)) {
1232                 return true;
1233         }
1234
1235         return false;
1236 }
1237
1238 static enum drm_connector_status
1239 radeon_dp_detect(struct drm_connector *connector, bool force)
1240 {
1241         struct drm_device *dev = connector->dev;
1242         struct radeon_device *rdev = dev->dev_private;
1243         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1244         enum drm_connector_status ret = connector_status_disconnected;
1245         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1246         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1247
1248         if (radeon_connector->edid) {
1249                 kfree(radeon_connector->edid);
1250                 radeon_connector->edid = NULL;
1251         }
1252
1253         if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1254             (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1255                 if (encoder) {
1256                         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1257                         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
1258
1259                         /* check if panel is valid */
1260                         if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
1261                                 ret = connector_status_connected;
1262                 }
1263                 /* eDP is always DP */
1264                 radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1265                 if (!radeon_dig_connector->edp_on)
1266                         atombios_set_edp_panel_power(connector,
1267                                                      ATOM_TRANSMITTER_ACTION_POWER_ON);
1268                 if (radeon_dp_getdpcd(radeon_connector))
1269                         ret = connector_status_connected;
1270                 if (!radeon_dig_connector->edp_on)
1271                         atombios_set_edp_panel_power(connector,
1272                                                      ATOM_TRANSMITTER_ACTION_POWER_OFF);
1273         } else if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) !=
1274                    ENCODER_OBJECT_ID_NONE) {
1275                 /* DP bridges are always DP */
1276                 radeon_dig_connector->dp_sink_type = CONNECTOR_OBJECT_ID_DISPLAYPORT;
1277                 /* get the DPCD from the bridge */
1278                 radeon_dp_getdpcd(radeon_connector);
1279
1280                 if (encoder) {
1281                         /* setup ddc on the bridge */
1282                         radeon_atom_ext_encoder_setup_ddc(encoder);
1283                         if (radeon_ddc_probe(radeon_connector)) /* try DDC */
1284                                 ret = connector_status_connected;
1285                         else if (radeon_connector->dac_load_detect) { /* try load detection */
1286                                 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1287                                 ret = encoder_funcs->detect(encoder, connector);
1288                         }
1289                 }
1290         } else {
1291                 radeon_dig_connector->dp_sink_type = radeon_dp_getsinktype(radeon_connector);
1292                 if (radeon_hpd_sense(rdev, radeon_connector->hpd.hpd)) {
1293                         ret = connector_status_connected;
1294                         if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
1295                                 radeon_dp_getdpcd(radeon_connector);
1296                 } else {
1297                         if (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) {
1298                                 if (radeon_dp_getdpcd(radeon_connector))
1299                                         ret = connector_status_connected;
1300                         } else {
1301                                 if (radeon_ddc_probe(radeon_connector))
1302                                         ret = connector_status_connected;
1303                         }
1304                 }
1305         }
1306
1307         radeon_connector_update_scratch_regs(connector, ret);
1308         return ret;
1309 }
1310
1311 static int radeon_dp_mode_valid(struct drm_connector *connector,
1312                                   struct drm_display_mode *mode)
1313 {
1314         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1315         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1316
1317         /* XXX check mode bandwidth */
1318
1319         if ((connector->connector_type == DRM_MODE_CONNECTOR_eDP) ||
1320             (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)) {
1321                 struct drm_encoder *encoder = radeon_best_single_encoder(connector);
1322
1323                 if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
1324                         return MODE_PANEL;
1325
1326                 if (encoder) {
1327                         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1328                         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
1329
1330                         /* AVIVO hardware supports downscaling modes larger than the panel
1331                          * to the panel size, but I'm not sure this is desirable.
1332                          */
1333                         if ((mode->hdisplay > native_mode->hdisplay) ||
1334                             (mode->vdisplay > native_mode->vdisplay))
1335                                 return MODE_PANEL;
1336
1337                         /* if scaling is disabled, block non-native modes */
1338                         if (radeon_encoder->rmx_type == RMX_OFF) {
1339                                 if ((mode->hdisplay != native_mode->hdisplay) ||
1340                                     (mode->vdisplay != native_mode->vdisplay))
1341                                         return MODE_PANEL;
1342                         }
1343                 }
1344                 return MODE_OK;
1345         } else {
1346                 if ((radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1347                     (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
1348                         return radeon_dp_mode_valid_helper(connector, mode);
1349                 else
1350                         return MODE_OK;
1351         }
1352 }
1353
1354 struct drm_connector_helper_funcs radeon_dp_connector_helper_funcs = {
1355         .get_modes = radeon_dp_get_modes,
1356         .mode_valid = radeon_dp_mode_valid,
1357         .best_encoder = radeon_dvi_encoder,
1358 };
1359
1360 struct drm_connector_funcs radeon_dp_connector_funcs = {
1361         .dpms = drm_helper_connector_dpms,
1362         .detect = radeon_dp_detect,
1363         .fill_modes = drm_helper_probe_single_connector_modes,
1364         .set_property = radeon_connector_set_property,
1365         .destroy = radeon_dp_connector_destroy,
1366         .force = radeon_dvi_force,
1367 };
1368
1369 void
1370 radeon_add_atom_connector(struct drm_device *dev,
1371                           uint32_t connector_id,
1372                           uint32_t supported_device,
1373                           int connector_type,
1374                           struct radeon_i2c_bus_rec *i2c_bus,
1375                           uint32_t igp_lane_info,
1376                           uint16_t connector_object_id,
1377                           struct radeon_hpd *hpd,
1378                           struct radeon_router *router)
1379 {
1380         struct radeon_device *rdev = dev->dev_private;
1381         struct drm_connector *connector;
1382         struct radeon_connector *radeon_connector;
1383         struct radeon_connector_atom_dig *radeon_dig_connector;
1384         struct drm_encoder *encoder;
1385         struct radeon_encoder *radeon_encoder;
1386         uint32_t subpixel_order = SubPixelNone;
1387         bool shared_ddc = false;
1388         bool is_dp_bridge = false;
1389
1390         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1391                 return;
1392
1393         /* if the user selected tv=0 don't try and add the connector */
1394         if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1395              (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1396              (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1397             (radeon_tv == 0))
1398                 return;
1399
1400         /* see if we already added it */
1401         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1402                 radeon_connector = to_radeon_connector(connector);
1403                 if (radeon_connector->connector_id == connector_id) {
1404                         radeon_connector->devices |= supported_device;
1405                         return;
1406                 }
1407                 if (radeon_connector->ddc_bus && i2c_bus->valid) {
1408                         if (radeon_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1409                                 radeon_connector->shared_ddc = true;
1410                                 shared_ddc = true;
1411                         }
1412                         if (radeon_connector->router_bus && router->ddc_valid &&
1413                             (radeon_connector->router.router_id == router->router_id)) {
1414                                 radeon_connector->shared_ddc = false;
1415                                 shared_ddc = false;
1416                         }
1417                 }
1418         }
1419
1420         /* check if it's a dp bridge */
1421         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1422                 radeon_encoder = to_radeon_encoder(encoder);
1423                 if (radeon_encoder->devices & supported_device) {
1424                         switch (radeon_encoder->encoder_id) {
1425                         case ENCODER_OBJECT_ID_TRAVIS:
1426                         case ENCODER_OBJECT_ID_NUTMEG:
1427                                 is_dp_bridge = true;
1428                                 break;
1429                         default:
1430                                 break;
1431                         }
1432                 }
1433         }
1434
1435         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1436         if (!radeon_connector)
1437                 return;
1438
1439         connector = &radeon_connector->base;
1440
1441         radeon_connector->connector_id = connector_id;
1442         radeon_connector->devices = supported_device;
1443         radeon_connector->shared_ddc = shared_ddc;
1444         radeon_connector->connector_object_id = connector_object_id;
1445         radeon_connector->hpd = *hpd;
1446
1447         radeon_connector->router = *router;
1448         if (router->ddc_valid || router->cd_valid) {
1449                 radeon_connector->router_bus = radeon_i2c_lookup(rdev, &router->i2c_info);
1450                 if (!radeon_connector->router_bus)
1451                         DRM_ERROR("Failed to assign router i2c bus! Check dmesg for i2c errors.\n");
1452         }
1453
1454         if (is_dp_bridge) {
1455                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1456                 if (!radeon_dig_connector)
1457                         goto failed;
1458                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1459                 radeon_connector->con_priv = radeon_dig_connector;
1460                 drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1461                 drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1462                 if (i2c_bus->valid) {
1463                         /* add DP i2c bus */
1464                         if (connector_type == DRM_MODE_CONNECTOR_eDP)
1465                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "eDP-auxch");
1466                         else
1467                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1468                         if (!radeon_dig_connector->dp_i2c_bus)
1469                                 DRM_ERROR("DP: Failed to assign dp ddc bus! Check dmesg for i2c errors.\n");
1470                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1471                         if (!radeon_connector->ddc_bus)
1472                                 DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1473                 }
1474                 switch (connector_type) {
1475                 case DRM_MODE_CONNECTOR_VGA:
1476                 case DRM_MODE_CONNECTOR_DVIA:
1477                 default:
1478                         connector->interlace_allowed = true;
1479                         connector->doublescan_allowed = true;
1480                         radeon_connector->dac_load_detect = true;
1481                         drm_connector_attach_property(&radeon_connector->base,
1482                                                       rdev->mode_info.load_detect_property,
1483                                                       1);
1484                         break;
1485                 case DRM_MODE_CONNECTOR_DVII:
1486                 case DRM_MODE_CONNECTOR_DVID:
1487                 case DRM_MODE_CONNECTOR_HDMIA:
1488                 case DRM_MODE_CONNECTOR_HDMIB:
1489                 case DRM_MODE_CONNECTOR_DisplayPort:
1490                         drm_connector_attach_property(&radeon_connector->base,
1491                                                       rdev->mode_info.underscan_property,
1492                                                       UNDERSCAN_OFF);
1493                         drm_connector_attach_property(&radeon_connector->base,
1494                                                       rdev->mode_info.underscan_hborder_property,
1495                                                       0);
1496                         drm_connector_attach_property(&radeon_connector->base,
1497                                                       rdev->mode_info.underscan_vborder_property,
1498                                                       0);
1499                         subpixel_order = SubPixelHorizontalRGB;
1500                         connector->interlace_allowed = true;
1501                         if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1502                                 connector->doublescan_allowed = true;
1503                         else
1504                                 connector->doublescan_allowed = false;
1505                         if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1506                                 radeon_connector->dac_load_detect = true;
1507                                 drm_connector_attach_property(&radeon_connector->base,
1508                                                               rdev->mode_info.load_detect_property,
1509                                                               1);
1510                         }
1511                         break;
1512                 case DRM_MODE_CONNECTOR_LVDS:
1513                 case DRM_MODE_CONNECTOR_eDP:
1514                         drm_connector_attach_property(&radeon_connector->base,
1515                                                       dev->mode_config.scaling_mode_property,
1516                                                       DRM_MODE_SCALE_FULLSCREEN);
1517                         subpixel_order = SubPixelHorizontalRGB;
1518                         connector->interlace_allowed = false;
1519                         connector->doublescan_allowed = false;
1520                         break;
1521                 }
1522         } else {
1523                 switch (connector_type) {
1524                 case DRM_MODE_CONNECTOR_VGA:
1525                         drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1526                         drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1527                         if (i2c_bus->valid) {
1528                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1529                                 if (!radeon_connector->ddc_bus)
1530                                         DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1531                         }
1532                         radeon_connector->dac_load_detect = true;
1533                         drm_connector_attach_property(&radeon_connector->base,
1534                                                       rdev->mode_info.load_detect_property,
1535                                                       1);
1536                         /* no HPD on analog connectors */
1537                         radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1538                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1539                         connector->interlace_allowed = true;
1540                         connector->doublescan_allowed = true;
1541                         break;
1542                 case DRM_MODE_CONNECTOR_DVIA:
1543                         drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1544                         drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1545                         if (i2c_bus->valid) {
1546                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1547                                 if (!radeon_connector->ddc_bus)
1548                                         DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1549                         }
1550                         radeon_connector->dac_load_detect = true;
1551                         drm_connector_attach_property(&radeon_connector->base,
1552                                                       rdev->mode_info.load_detect_property,
1553                                                       1);
1554                         /* no HPD on analog connectors */
1555                         radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1556                         connector->interlace_allowed = true;
1557                         connector->doublescan_allowed = true;
1558                         break;
1559                 case DRM_MODE_CONNECTOR_DVII:
1560                 case DRM_MODE_CONNECTOR_DVID:
1561                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1562                         if (!radeon_dig_connector)
1563                                 goto failed;
1564                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1565                         radeon_connector->con_priv = radeon_dig_connector;
1566                         drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1567                         drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1568                         if (i2c_bus->valid) {
1569                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1570                                 if (!radeon_connector->ddc_bus)
1571                                         DRM_ERROR("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1572                         }
1573                         subpixel_order = SubPixelHorizontalRGB;
1574                         drm_connector_attach_property(&radeon_connector->base,
1575                                                       rdev->mode_info.coherent_mode_property,
1576                                                       1);
1577                         if (ASIC_IS_AVIVO(rdev)) {
1578                                 drm_connector_attach_property(&radeon_connector->base,
1579                                                               rdev->mode_info.underscan_property,
1580                                                               UNDERSCAN_OFF);
1581                                 drm_connector_attach_property(&radeon_connector->base,
1582                                                               rdev->mode_info.underscan_hborder_property,
1583                                                               0);
1584                                 drm_connector_attach_property(&radeon_connector->base,
1585                                                               rdev->mode_info.underscan_vborder_property,
1586                                                               0);
1587                         }
1588                         if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1589                                 radeon_connector->dac_load_detect = true;
1590                                 drm_connector_attach_property(&radeon_connector->base,
1591                                                               rdev->mode_info.load_detect_property,
1592                                                               1);
1593                         }
1594                         connector->interlace_allowed = true;
1595                         if (connector_type == DRM_MODE_CONNECTOR_DVII)
1596                                 connector->doublescan_allowed = true;
1597                         else
1598                                 connector->doublescan_allowed = false;
1599                         break;
1600                 case DRM_MODE_CONNECTOR_HDMIA:
1601                 case DRM_MODE_CONNECTOR_HDMIB:
1602                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1603                         if (!radeon_dig_connector)
1604                                 goto failed;
1605                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1606                         radeon_connector->con_priv = radeon_dig_connector;
1607                         drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1608                         drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1609                         if (i2c_bus->valid) {
1610                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1611                                 if (!radeon_connector->ddc_bus)
1612                                         DRM_ERROR("HDMI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1613                         }
1614                         drm_connector_attach_property(&radeon_connector->base,
1615                                                       rdev->mode_info.coherent_mode_property,
1616                                                       1);
1617                         if (ASIC_IS_AVIVO(rdev)) {
1618                                 drm_connector_attach_property(&radeon_connector->base,
1619                                                               rdev->mode_info.underscan_property,
1620                                                               UNDERSCAN_OFF);
1621                                 drm_connector_attach_property(&radeon_connector->base,
1622                                                               rdev->mode_info.underscan_hborder_property,
1623                                                               0);
1624                                 drm_connector_attach_property(&radeon_connector->base,
1625                                                               rdev->mode_info.underscan_vborder_property,
1626                                                               0);
1627                         }
1628                         subpixel_order = SubPixelHorizontalRGB;
1629                         connector->interlace_allowed = true;
1630                         if (connector_type == DRM_MODE_CONNECTOR_HDMIB)
1631                                 connector->doublescan_allowed = true;
1632                         else
1633                                 connector->doublescan_allowed = false;
1634                         break;
1635                 case DRM_MODE_CONNECTOR_DisplayPort:
1636                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1637                         if (!radeon_dig_connector)
1638                                 goto failed;
1639                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1640                         radeon_connector->con_priv = radeon_dig_connector;
1641                         drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1642                         drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1643                         if (i2c_bus->valid) {
1644                                 /* add DP i2c bus */
1645                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1646                                 if (!radeon_dig_connector->dp_i2c_bus)
1647                                         DRM_ERROR("DP: Failed to assign dp ddc bus! Check dmesg for i2c errors.\n");
1648                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1649                                 if (!radeon_connector->ddc_bus)
1650                                         DRM_ERROR("DP: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1651                         }
1652                         subpixel_order = SubPixelHorizontalRGB;
1653                         drm_connector_attach_property(&radeon_connector->base,
1654                                                       rdev->mode_info.coherent_mode_property,
1655                                                       1);
1656                         if (ASIC_IS_AVIVO(rdev)) {
1657                                 drm_connector_attach_property(&radeon_connector->base,
1658                                                               rdev->mode_info.underscan_property,
1659                                                               UNDERSCAN_OFF);
1660                                 drm_connector_attach_property(&radeon_connector->base,
1661                                                               rdev->mode_info.underscan_hborder_property,
1662                                                               0);
1663                                 drm_connector_attach_property(&radeon_connector->base,
1664                                                               rdev->mode_info.underscan_vborder_property,
1665                                                               0);
1666                         }
1667                         connector->interlace_allowed = true;
1668                         /* in theory with a DP to VGA converter... */
1669                         connector->doublescan_allowed = false;
1670                         break;
1671                 case DRM_MODE_CONNECTOR_eDP:
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, "eDP-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                         drm_connector_attach_property(&radeon_connector->base,
1689                                                       dev->mode_config.scaling_mode_property,
1690                                                       DRM_MODE_SCALE_FULLSCREEN);
1691                         subpixel_order = SubPixelHorizontalRGB;
1692                         connector->interlace_allowed = false;
1693                         connector->doublescan_allowed = false;
1694                         break;
1695                 case DRM_MODE_CONNECTOR_SVIDEO:
1696                 case DRM_MODE_CONNECTOR_Composite:
1697                 case DRM_MODE_CONNECTOR_9PinDIN:
1698                         drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1699                         drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1700                         radeon_connector->dac_load_detect = true;
1701                         drm_connector_attach_property(&radeon_connector->base,
1702                                                       rdev->mode_info.load_detect_property,
1703                                                       1);
1704                         drm_connector_attach_property(&radeon_connector->base,
1705                                                       rdev->mode_info.tv_std_property,
1706                                                       radeon_atombios_get_tv_info(rdev));
1707                         /* no HPD on analog connectors */
1708                         radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1709                         connector->interlace_allowed = false;
1710                         connector->doublescan_allowed = false;
1711                         break;
1712                 case DRM_MODE_CONNECTOR_LVDS:
1713                         radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1714                         if (!radeon_dig_connector)
1715                                 goto failed;
1716                         radeon_dig_connector->igp_lane_info = igp_lane_info;
1717                         radeon_connector->con_priv = radeon_dig_connector;
1718                         drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1719                         drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1720                         if (i2c_bus->valid) {
1721                                 radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1722                                 if (!radeon_connector->ddc_bus)
1723                                         DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1724                         }
1725                         drm_connector_attach_property(&radeon_connector->base,
1726                                                       dev->mode_config.scaling_mode_property,
1727                                                       DRM_MODE_SCALE_FULLSCREEN);
1728                         subpixel_order = SubPixelHorizontalRGB;
1729                         connector->interlace_allowed = false;
1730                         connector->doublescan_allowed = false;
1731                         break;
1732                 }
1733         }
1734
1735         if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
1736                 if (i2c_bus->valid)
1737                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1738         } else
1739                 connector->polled = DRM_CONNECTOR_POLL_HPD;
1740
1741         connector->display_info.subpixel_order = subpixel_order;
1742         drm_sysfs_connector_add(connector);
1743         return;
1744
1745 failed:
1746         drm_connector_cleanup(connector);
1747         kfree(connector);
1748 }
1749
1750 void
1751 radeon_add_legacy_connector(struct drm_device *dev,
1752                             uint32_t connector_id,
1753                             uint32_t supported_device,
1754                             int connector_type,
1755                             struct radeon_i2c_bus_rec *i2c_bus,
1756                             uint16_t connector_object_id,
1757                             struct radeon_hpd *hpd)
1758 {
1759         struct radeon_device *rdev = dev->dev_private;
1760         struct drm_connector *connector;
1761         struct radeon_connector *radeon_connector;
1762         uint32_t subpixel_order = SubPixelNone;
1763
1764         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1765                 return;
1766
1767         /* if the user selected tv=0 don't try and add the connector */
1768         if (((connector_type == DRM_MODE_CONNECTOR_SVIDEO) ||
1769              (connector_type == DRM_MODE_CONNECTOR_Composite) ||
1770              (connector_type == DRM_MODE_CONNECTOR_9PinDIN)) &&
1771             (radeon_tv == 0))
1772                 return;
1773
1774         /* see if we already added it */
1775         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1776                 radeon_connector = to_radeon_connector(connector);
1777                 if (radeon_connector->connector_id == connector_id) {
1778                         radeon_connector->devices |= supported_device;
1779                         return;
1780                 }
1781         }
1782
1783         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1784         if (!radeon_connector)
1785                 return;
1786
1787         connector = &radeon_connector->base;
1788
1789         radeon_connector->connector_id = connector_id;
1790         radeon_connector->devices = supported_device;
1791         radeon_connector->connector_object_id = connector_object_id;
1792         radeon_connector->hpd = *hpd;
1793
1794         switch (connector_type) {
1795         case DRM_MODE_CONNECTOR_VGA:
1796                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1797                 drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1798                 if (i2c_bus->valid) {
1799                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1800                         if (!radeon_connector->ddc_bus)
1801                                 DRM_ERROR("VGA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1802                 }
1803                 radeon_connector->dac_load_detect = true;
1804                 drm_connector_attach_property(&radeon_connector->base,
1805                                               rdev->mode_info.load_detect_property,
1806                                               1);
1807                 /* no HPD on analog connectors */
1808                 radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1809                 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1810                 connector->interlace_allowed = true;
1811                 connector->doublescan_allowed = true;
1812                 break;
1813         case DRM_MODE_CONNECTOR_DVIA:
1814                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1815                 drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1816                 if (i2c_bus->valid) {
1817                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1818                         if (!radeon_connector->ddc_bus)
1819                                 DRM_ERROR("DVIA: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1820                 }
1821                 radeon_connector->dac_load_detect = true;
1822                 drm_connector_attach_property(&radeon_connector->base,
1823                                               rdev->mode_info.load_detect_property,
1824                                               1);
1825                 /* no HPD on analog connectors */
1826                 radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1827                 connector->interlace_allowed = true;
1828                 connector->doublescan_allowed = true;
1829                 break;
1830         case DRM_MODE_CONNECTOR_DVII:
1831         case DRM_MODE_CONNECTOR_DVID:
1832                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1833                 drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_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("DVI: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1838                 }
1839                 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1840                         radeon_connector->dac_load_detect = true;
1841                         drm_connector_attach_property(&radeon_connector->base,
1842                                                       rdev->mode_info.load_detect_property,
1843                                                       1);
1844                 }
1845                 subpixel_order = SubPixelHorizontalRGB;
1846                 connector->interlace_allowed = true;
1847                 if (connector_type == DRM_MODE_CONNECTOR_DVII)
1848                         connector->doublescan_allowed = true;
1849                 else
1850                         connector->doublescan_allowed = false;
1851                 break;
1852         case DRM_MODE_CONNECTOR_SVIDEO:
1853         case DRM_MODE_CONNECTOR_Composite:
1854         case DRM_MODE_CONNECTOR_9PinDIN:
1855                 drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1856                 drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1857                 radeon_connector->dac_load_detect = true;
1858                 /* RS400,RC410,RS480 chipset seems to report a lot
1859                  * of false positive on load detect, we haven't yet
1860                  * found a way to make load detect reliable on those
1861                  * chipset, thus just disable it for TV.
1862                  */
1863                 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480)
1864                         radeon_connector->dac_load_detect = false;
1865                 drm_connector_attach_property(&radeon_connector->base,
1866                                               rdev->mode_info.load_detect_property,
1867                                               radeon_connector->dac_load_detect);
1868                 drm_connector_attach_property(&radeon_connector->base,
1869                                               rdev->mode_info.tv_std_property,
1870                                               radeon_combios_get_tv_info(rdev));
1871                 /* no HPD on analog connectors */
1872                 radeon_connector->hpd.hpd = RADEON_HPD_NONE;
1873                 connector->interlace_allowed = false;
1874                 connector->doublescan_allowed = false;
1875                 break;
1876         case DRM_MODE_CONNECTOR_LVDS:
1877                 drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1878                 drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1879                 if (i2c_bus->valid) {
1880                         radeon_connector->ddc_bus = radeon_i2c_lookup(rdev, i2c_bus);
1881                         if (!radeon_connector->ddc_bus)
1882                                 DRM_ERROR("LVDS: Failed to assign ddc bus! Check dmesg for i2c errors.\n");
1883                 }
1884                 drm_connector_attach_property(&radeon_connector->base,
1885                                               dev->mode_config.scaling_mode_property,
1886                                               DRM_MODE_SCALE_FULLSCREEN);
1887                 subpixel_order = SubPixelHorizontalRGB;
1888                 connector->interlace_allowed = false;
1889                 connector->doublescan_allowed = false;
1890                 break;
1891         }
1892
1893         if (radeon_connector->hpd.hpd == RADEON_HPD_NONE) {
1894                 if (i2c_bus->valid)
1895                         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1896         } else
1897                 connector->polled = DRM_CONNECTOR_POLL_HPD;
1898         connector->display_info.subpixel_order = subpixel_order;
1899         drm_sysfs_connector_add(connector);
1900         if (connector_type == DRM_MODE_CONNECTOR_LVDS) {
1901                 struct drm_encoder *drm_encoder;
1902
1903                 list_for_each_entry(drm_encoder, &dev->mode_config.encoder_list, head) {
1904                         struct radeon_encoder *radeon_encoder;
1905
1906                         radeon_encoder = to_radeon_encoder(drm_encoder);
1907                         if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_LVDS)
1908                                 radeon_legacy_backlight_init(radeon_encoder, connector);
1909                 }
1910         }
1911 }