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