Merge remote branch 'korg/drm-radeon-testing' into drm-next-stage
[linux-2.6.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 void radeon_connector_hotplug(struct drm_connector *connector)
44 {
45         struct drm_device *dev = connector->dev;
46         struct radeon_device *rdev = dev->dev_private;
47         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
48
49         if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
50                 radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
51
52         if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
53             (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
54                 if ((radeon_dp_getsinktype(radeon_connector) == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
55                     (radeon_dp_getsinktype(radeon_connector) == CONNECTOR_OBJECT_ID_eDP)) {
56                         if (radeon_dp_needs_link_train(radeon_connector)) {
57                                 if (connector->encoder)
58                                         dp_link_train(connector->encoder, connector);
59                         }
60                 }
61         }
62
63 }
64
65 static void radeon_property_change_mode(struct drm_encoder *encoder)
66 {
67         struct drm_crtc *crtc = encoder->crtc;
68
69         if (crtc && crtc->enabled) {
70                 drm_crtc_helper_set_mode(crtc, &crtc->mode,
71                                          crtc->x, crtc->y, crtc->fb);
72         }
73 }
74 static void
75 radeon_connector_update_scratch_regs(struct drm_connector *connector, enum drm_connector_status status)
76 {
77         struct drm_device *dev = connector->dev;
78         struct radeon_device *rdev = dev->dev_private;
79         struct drm_encoder *best_encoder = NULL;
80         struct drm_encoder *encoder = NULL;
81         struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
82         struct drm_mode_object *obj;
83         bool connected;
84         int i;
85
86         best_encoder = connector_funcs->best_encoder(connector);
87
88         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
89                 if (connector->encoder_ids[i] == 0)
90                         break;
91
92                 obj = drm_mode_object_find(connector->dev,
93                                            connector->encoder_ids[i],
94                                            DRM_MODE_OBJECT_ENCODER);
95                 if (!obj)
96                         continue;
97
98                 encoder = obj_to_encoder(obj);
99
100                 if ((encoder == best_encoder) && (status == connector_status_connected))
101                         connected = true;
102                 else
103                         connected = false;
104
105                 if (rdev->is_atom_bios)
106                         radeon_atombios_connected_scratch_regs(connector, encoder, connected);
107                 else
108                         radeon_combios_connected_scratch_regs(connector, encoder, connected);
109
110         }
111 }
112
113 struct drm_encoder *radeon_find_encoder(struct drm_connector *connector, int encoder_type)
114 {
115         struct drm_mode_object *obj;
116         struct drm_encoder *encoder;
117         int i;
118
119         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
120                 if (connector->encoder_ids[i] == 0)
121                         break;
122
123                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
124                 if (!obj)
125                         continue;
126
127                 encoder = obj_to_encoder(obj);
128                 if (encoder->encoder_type == encoder_type)
129                         return encoder;
130         }
131         return NULL;
132 }
133
134 struct drm_encoder *radeon_best_single_encoder(struct drm_connector *connector)
135 {
136         int enc_id = connector->encoder_ids[0];
137         struct drm_mode_object *obj;
138         struct drm_encoder *encoder;
139
140         /* pick the encoder ids */
141         if (enc_id) {
142                 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
143                 if (!obj)
144                         return NULL;
145                 encoder = obj_to_encoder(obj);
146                 return encoder;
147         }
148         return NULL;
149 }
150
151 /*
152  * radeon_connector_analog_encoder_conflict_solve
153  * - search for other connectors sharing this encoder
154  *   if priority is true, then set them disconnected if this is connected
155  *   if priority is false, set us disconnected if they are connected
156  */
157 static enum drm_connector_status
158 radeon_connector_analog_encoder_conflict_solve(struct drm_connector *connector,
159                                                struct drm_encoder *encoder,
160                                                enum drm_connector_status current_status,
161                                                bool priority)
162 {
163         struct drm_device *dev = connector->dev;
164         struct drm_connector *conflict;
165         int i;
166
167         list_for_each_entry(conflict, &dev->mode_config.connector_list, head) {
168                 if (conflict == connector)
169                         continue;
170
171                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
172                         if (conflict->encoder_ids[i] == 0)
173                                 break;
174
175                         /* if the IDs match */
176                         if (conflict->encoder_ids[i] == encoder->base.id) {
177                                 if (conflict->status != connector_status_connected)
178                                         continue;
179
180                                 if (priority == true) {
181                                         DRM_INFO("1: conflicting encoders switching off %s\n", drm_get_connector_name(conflict));
182                                         DRM_INFO("in favor of %s\n", drm_get_connector_name(connector));
183                                         conflict->status = connector_status_disconnected;
184                                         radeon_connector_update_scratch_regs(conflict, connector_status_disconnected);
185                                 } else {
186                                         DRM_INFO("2: conflicting encoders switching off %s\n", drm_get_connector_name(connector));
187                                         DRM_INFO("in favor of %s\n", drm_get_connector_name(conflict));
188                                         current_status = connector_status_disconnected;
189                                 }
190                                 break;
191                         }
192                 }
193         }
194         return current_status;
195
196 }
197
198 static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encoder)
199 {
200         struct drm_device *dev = encoder->dev;
201         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
202         struct drm_display_mode *mode = NULL;
203         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
204
205         if (native_mode->hdisplay != 0 &&
206             native_mode->vdisplay != 0 &&
207             native_mode->clock != 0) {
208                 mode = drm_mode_duplicate(dev, native_mode);
209                 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
210                 drm_mode_set_name(mode);
211
212                 DRM_DEBUG("Adding native panel mode %s\n", mode->name);
213         } else if (native_mode->hdisplay != 0 &&
214                    native_mode->vdisplay != 0) {
215                 /* mac laptops without an edid */
216                 /* Note that this is not necessarily the exact panel mode,
217                  * but an approximation based on the cvt formula.  For these
218                  * systems we should ideally read the mode info out of the
219                  * registers or add a mode table, but this works and is much
220                  * simpler.
221                  */
222                 mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
223                 mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
224                 DRM_DEBUG("Adding cvt approximation of native panel mode %s\n", mode->name);
225         }
226         return mode;
227 }
228
229 static void radeon_add_common_modes(struct drm_encoder *encoder, struct drm_connector *connector)
230 {
231         struct drm_device *dev = encoder->dev;
232         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
233         struct drm_display_mode *mode = NULL;
234         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
235         int i;
236         struct mode_size {
237                 int w;
238                 int h;
239         } common_modes[17] = {
240                 { 640,  480},
241                 { 720,  480},
242                 { 800,  600},
243                 { 848,  480},
244                 {1024,  768},
245                 {1152,  768},
246                 {1280,  720},
247                 {1280,  800},
248                 {1280,  854},
249                 {1280,  960},
250                 {1280, 1024},
251                 {1440,  900},
252                 {1400, 1050},
253                 {1680, 1050},
254                 {1600, 1200},
255                 {1920, 1080},
256                 {1920, 1200}
257         };
258
259         for (i = 0; i < 17; i++) {
260                 if (radeon_encoder->devices & (ATOM_DEVICE_TV_SUPPORT)) {
261                         if (common_modes[i].w > 1024 ||
262                             common_modes[i].h > 768)
263                                 continue;
264                 }
265                 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
266                         if (common_modes[i].w > native_mode->hdisplay ||
267                             common_modes[i].h > native_mode->vdisplay ||
268                             (common_modes[i].w == native_mode->hdisplay &&
269                              common_modes[i].h == native_mode->vdisplay))
270                                 continue;
271                 }
272                 if (common_modes[i].w < 320 || common_modes[i].h < 200)
273                         continue;
274
275                 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
276                 drm_mode_probed_add(connector, mode);
277         }
278 }
279
280 int radeon_connector_set_property(struct drm_connector *connector, struct drm_property *property,
281                                   uint64_t val)
282 {
283         struct drm_device *dev = connector->dev;
284         struct radeon_device *rdev = dev->dev_private;
285         struct drm_encoder *encoder;
286         struct radeon_encoder *radeon_encoder;
287
288         if (property == rdev->mode_info.coherent_mode_property) {
289                 struct radeon_encoder_atom_dig *dig;
290
291                 /* need to find digital encoder on connector */
292                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
293                 if (!encoder)
294                         return 0;
295
296                 radeon_encoder = to_radeon_encoder(encoder);
297
298                 if (!radeon_encoder->enc_priv)
299                         return 0;
300
301                 dig = radeon_encoder->enc_priv;
302                 dig->coherent_mode = val ? true : false;
303                 radeon_property_change_mode(&radeon_encoder->base);
304         }
305
306         if (property == rdev->mode_info.tv_std_property) {
307                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TVDAC);
308                 if (!encoder) {
309                         encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_DAC);
310                 }
311
312                 if (!encoder)
313                         return 0;
314
315                 radeon_encoder = to_radeon_encoder(encoder);
316                 if (!radeon_encoder->enc_priv)
317                         return 0;
318                 if (rdev->is_atom_bios) {
319                         struct radeon_encoder_atom_dac *dac_int;
320                         dac_int = radeon_encoder->enc_priv;
321                         dac_int->tv_std = val;
322                 } else {
323                         struct radeon_encoder_tv_dac *dac_int;
324                         dac_int = radeon_encoder->enc_priv;
325                         dac_int->tv_std = val;
326                 }
327                 radeon_property_change_mode(&radeon_encoder->base);
328         }
329
330         if (property == rdev->mode_info.load_detect_property) {
331                 struct radeon_connector *radeon_connector =
332                         to_radeon_connector(connector);
333
334                 if (val == 0)
335                         radeon_connector->dac_load_detect = false;
336                 else
337                         radeon_connector->dac_load_detect = true;
338         }
339
340         if (property == rdev->mode_info.tmds_pll_property) {
341                 struct radeon_encoder_int_tmds *tmds = NULL;
342                 bool ret = false;
343                 /* need to find digital encoder on connector */
344                 encoder = radeon_find_encoder(connector, DRM_MODE_ENCODER_TMDS);
345                 if (!encoder)
346                         return 0;
347
348                 radeon_encoder = to_radeon_encoder(encoder);
349
350                 tmds = radeon_encoder->enc_priv;
351                 if (!tmds)
352                         return 0;
353
354                 if (val == 0) {
355                         if (rdev->is_atom_bios)
356                                 ret = radeon_atombios_get_tmds_info(radeon_encoder, tmds);
357                         else
358                                 ret = radeon_legacy_get_tmds_info_from_combios(radeon_encoder, tmds);
359                 }
360                 if (val == 1 || ret == false) {
361                         radeon_legacy_get_tmds_info_from_table(radeon_encoder, tmds);
362                 }
363                 radeon_property_change_mode(&radeon_encoder->base);
364         }
365
366         return 0;
367 }
368
369 static void radeon_fixup_lvds_native_mode(struct drm_encoder *encoder,
370                                           struct drm_connector *connector)
371 {
372         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
373         struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
374
375         /* Try to get native mode details from EDID if necessary */
376         if (!native_mode->clock) {
377                 struct drm_display_mode *t, *mode;
378
379                 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
380                         if (mode->hdisplay == native_mode->hdisplay &&
381                             mode->vdisplay == native_mode->vdisplay) {
382                                 *native_mode = *mode;
383                                 drm_mode_set_crtcinfo(native_mode, CRTC_INTERLACE_HALVE_V);
384                                 DRM_INFO("Determined LVDS native mode details from EDID\n");
385                                 break;
386                         }
387                 }
388         }
389         if (!native_mode->clock) {
390                 DRM_INFO("No LVDS native mode details, disabling RMX\n");
391                 radeon_encoder->rmx_type = RMX_OFF;
392         }
393 }
394
395 static int radeon_lvds_get_modes(struct drm_connector *connector)
396 {
397         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
398         struct drm_encoder *encoder;
399         int ret = 0;
400         struct drm_display_mode *mode;
401
402         if (radeon_connector->ddc_bus) {
403                 ret = radeon_ddc_get_modes(radeon_connector);
404                 if (ret > 0) {
405                         encoder = radeon_best_single_encoder(connector);
406                         if (encoder) {
407                                 radeon_fixup_lvds_native_mode(encoder, connector);
408                                 /* add scaled modes */
409                                 radeon_add_common_modes(encoder, connector);
410                         }
411                         return ret;
412                 }
413         }
414
415         encoder = radeon_best_single_encoder(connector);
416         if (!encoder)
417                 return 0;
418
419         /* we have no EDID modes */
420         mode = radeon_fp_native_mode(encoder);
421         if (mode) {
422                 ret = 1;
423                 drm_mode_probed_add(connector, mode);
424                 /* add scaled modes */
425                 radeon_add_common_modes(encoder, connector);
426         }
427
428         return ret;
429 }
430
431 static int radeon_lvds_mode_valid(struct drm_connector *connector,
432                                   struct drm_display_mode *mode)
433 {
434         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
435
436         if ((mode->hdisplay < 320) || (mode->vdisplay < 240))
437                 return MODE_PANEL;
438
439         if (encoder) {
440                 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
441                 struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
442
443                 /* AVIVO hardware supports downscaling modes larger than the panel
444                  * to the panel size, but I'm not sure this is desirable.
445                  */
446                 if ((mode->hdisplay > native_mode->hdisplay) ||
447                     (mode->vdisplay > native_mode->vdisplay))
448                         return MODE_PANEL;
449
450                 /* if scaling is disabled, block non-native modes */
451                 if (radeon_encoder->rmx_type == RMX_OFF) {
452                         if ((mode->hdisplay != native_mode->hdisplay) ||
453                             (mode->vdisplay != native_mode->vdisplay))
454                                 return MODE_PANEL;
455                 }
456         }
457
458         return MODE_OK;
459 }
460
461 static enum drm_connector_status radeon_lvds_detect(struct drm_connector *connector)
462 {
463         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
464         struct drm_encoder *encoder = radeon_best_single_encoder(connector);
465         enum drm_connector_status ret = connector_status_disconnected;
466
467         if (encoder) {
468                 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
469                 struct drm_display_mode *native_mode = &radeon_encoder->native_mode;
470
471                 /* check if panel is valid */
472                 if (native_mode->hdisplay >= 320 && native_mode->vdisplay >= 240)
473                         ret = connector_status_connected;
474
475         }
476
477         /* check for edid as well */
478         if (radeon_connector->edid)
479                 ret = connector_status_connected;
480         else {
481                 if (radeon_connector->ddc_bus) {
482                         radeon_connector->edid = drm_get_edid(&radeon_connector->base,
483                                                               &radeon_connector->ddc_bus->adapter);
484                         if (radeon_connector->edid)
485                                 ret = connector_status_connected;
486                 }
487         }
488         /* check acpi lid status ??? */
489
490         radeon_connector_update_scratch_regs(connector, ret);
491         return ret;
492 }
493
494 static void radeon_connector_destroy(struct drm_connector *connector)
495 {
496         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
497
498         if (radeon_connector->ddc_bus)
499                 radeon_i2c_destroy(radeon_connector->ddc_bus);
500         if (radeon_connector->edid)
501                 kfree(radeon_connector->edid);
502         kfree(radeon_connector->con_priv);
503         drm_sysfs_connector_remove(connector);
504         drm_connector_cleanup(connector);
505         kfree(connector);
506 }
507
508 static int radeon_lvds_set_property(struct drm_connector *connector,
509                                     struct drm_property *property,
510                                     uint64_t value)
511 {
512         struct drm_device *dev = connector->dev;
513         struct radeon_encoder *radeon_encoder;
514         enum radeon_rmx_type rmx_type;
515
516         DRM_DEBUG("\n");
517         if (property != dev->mode_config.scaling_mode_property)
518                 return 0;
519
520         if (connector->encoder)
521                 radeon_encoder = to_radeon_encoder(connector->encoder);
522         else {
523                 struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
524                 radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
525         }
526
527         switch (value) {
528         case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
529         case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
530         case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
531         default:
532         case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
533         }
534         if (radeon_encoder->rmx_type == rmx_type)
535                 return 0;
536
537         radeon_encoder->rmx_type = rmx_type;
538
539         radeon_property_change_mode(&radeon_encoder->base);
540         return 0;
541 }
542
543
544 struct drm_connector_helper_funcs radeon_lvds_connector_helper_funcs = {
545         .get_modes = radeon_lvds_get_modes,
546         .mode_valid = radeon_lvds_mode_valid,
547         .best_encoder = radeon_best_single_encoder,
548 };
549
550 struct drm_connector_funcs radeon_lvds_connector_funcs = {
551         .dpms = drm_helper_connector_dpms,
552         .detect = radeon_lvds_detect,
553         .fill_modes = drm_helper_probe_single_connector_modes,
554         .destroy = radeon_connector_destroy,
555         .set_property = radeon_lvds_set_property,
556 };
557
558 static int radeon_vga_get_modes(struct drm_connector *connector)
559 {
560         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
561         int ret;
562
563         ret = radeon_ddc_get_modes(radeon_connector);
564
565         return ret;
566 }
567
568 static int radeon_vga_mode_valid(struct drm_connector *connector,
569                                   struct drm_display_mode *mode)
570 {
571         /* XXX check mode bandwidth */
572         /* XXX verify against max DAC output frequency */
573         return MODE_OK;
574 }
575
576 static enum drm_connector_status radeon_vga_detect(struct drm_connector *connector)
577 {
578         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
579         struct drm_encoder *encoder;
580         struct drm_encoder_helper_funcs *encoder_funcs;
581         bool dret = false;
582         enum drm_connector_status ret = connector_status_disconnected;
583
584         encoder = radeon_best_single_encoder(connector);
585         if (!encoder)
586                 ret = connector_status_disconnected;
587
588         if (radeon_connector->ddc_bus)
589                 dret = radeon_ddc_probe(radeon_connector);
590         if (dret) {
591                 if (radeon_connector->edid) {
592                         kfree(radeon_connector->edid);
593                         radeon_connector->edid = NULL;
594                 }
595                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
596
597                 if (!radeon_connector->edid) {
598                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
599                                         drm_get_connector_name(connector));
600                         ret = connector_status_connected;
601                 } else {
602                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
603
604                         /* some oems have boards with separate digital and analog connectors
605                          * with a shared ddc line (often vga + hdmi)
606                          */
607                         if (radeon_connector->use_digital && radeon_connector->shared_ddc) {
608                                 kfree(radeon_connector->edid);
609                                 radeon_connector->edid = NULL;
610                                 ret = connector_status_disconnected;
611                         } else
612                                 ret = connector_status_connected;
613                 }
614         } else {
615                 if (radeon_connector->dac_load_detect && encoder) {
616                         encoder_funcs = encoder->helper_private;
617                         ret = encoder_funcs->detect(encoder, connector);
618                 }
619         }
620
621         if (ret == connector_status_connected)
622                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
623         radeon_connector_update_scratch_regs(connector, ret);
624         return ret;
625 }
626
627 struct drm_connector_helper_funcs radeon_vga_connector_helper_funcs = {
628         .get_modes = radeon_vga_get_modes,
629         .mode_valid = radeon_vga_mode_valid,
630         .best_encoder = radeon_best_single_encoder,
631 };
632
633 struct drm_connector_funcs radeon_vga_connector_funcs = {
634         .dpms = drm_helper_connector_dpms,
635         .detect = radeon_vga_detect,
636         .fill_modes = drm_helper_probe_single_connector_modes,
637         .destroy = radeon_connector_destroy,
638         .set_property = radeon_connector_set_property,
639 };
640
641 static int radeon_tv_get_modes(struct drm_connector *connector)
642 {
643         struct drm_device *dev = connector->dev;
644         struct radeon_device *rdev = dev->dev_private;
645         struct drm_display_mode *tv_mode;
646         struct drm_encoder *encoder;
647
648         encoder = radeon_best_single_encoder(connector);
649         if (!encoder)
650                 return 0;
651
652         /* avivo chips can scale any mode */
653         if (rdev->family >= CHIP_RS600)
654                 /* add scaled modes */
655                 radeon_add_common_modes(encoder, connector);
656         else {
657                 /* only 800x600 is supported right now on pre-avivo chips */
658                 tv_mode = drm_cvt_mode(dev, 800, 600, 60, false, false, false);
659                 tv_mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
660                 drm_mode_probed_add(connector, tv_mode);
661         }
662         return 1;
663 }
664
665 static int radeon_tv_mode_valid(struct drm_connector *connector,
666                                 struct drm_display_mode *mode)
667 {
668         if ((mode->hdisplay > 1024) || (mode->vdisplay > 768))
669                 return MODE_CLOCK_RANGE;
670         return MODE_OK;
671 }
672
673 static enum drm_connector_status radeon_tv_detect(struct drm_connector *connector)
674 {
675         struct drm_encoder *encoder;
676         struct drm_encoder_helper_funcs *encoder_funcs;
677         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
678         enum drm_connector_status ret = connector_status_disconnected;
679
680         if (!radeon_connector->dac_load_detect)
681                 return ret;
682
683         encoder = radeon_best_single_encoder(connector);
684         if (!encoder)
685                 ret = connector_status_disconnected;
686         else {
687                 encoder_funcs = encoder->helper_private;
688                 ret = encoder_funcs->detect(encoder, connector);
689         }
690         if (ret == connector_status_connected)
691                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, false);
692         radeon_connector_update_scratch_regs(connector, ret);
693         return ret;
694 }
695
696 struct drm_connector_helper_funcs radeon_tv_connector_helper_funcs = {
697         .get_modes = radeon_tv_get_modes,
698         .mode_valid = radeon_tv_mode_valid,
699         .best_encoder = radeon_best_single_encoder,
700 };
701
702 struct drm_connector_funcs radeon_tv_connector_funcs = {
703         .dpms = drm_helper_connector_dpms,
704         .detect = radeon_tv_detect,
705         .fill_modes = drm_helper_probe_single_connector_modes,
706         .destroy = radeon_connector_destroy,
707         .set_property = radeon_connector_set_property,
708 };
709
710 static int radeon_dvi_get_modes(struct drm_connector *connector)
711 {
712         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
713         int ret;
714
715         ret = radeon_ddc_get_modes(radeon_connector);
716         return ret;
717 }
718
719 /*
720  * DVI is complicated
721  * Do a DDC probe, if DDC probe passes, get the full EDID so
722  * we can do analog/digital monitor detection at this point.
723  * If the monitor is an analog monitor or we got no DDC,
724  * we need to find the DAC encoder object for this connector.
725  * If we got no DDC, we do load detection on the DAC encoder object.
726  * If we got analog DDC or load detection passes on the DAC encoder
727  * we have to check if this analog encoder is shared with anyone else (TV)
728  * if its shared we have to set the other connector to disconnected.
729  */
730 static enum drm_connector_status radeon_dvi_detect(struct drm_connector *connector)
731 {
732         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
733         struct drm_encoder *encoder = NULL;
734         struct drm_encoder_helper_funcs *encoder_funcs;
735         struct drm_mode_object *obj;
736         int i;
737         enum drm_connector_status ret = connector_status_disconnected;
738         bool dret = false;
739
740         if (radeon_connector->ddc_bus)
741                 dret = radeon_ddc_probe(radeon_connector);
742         if (dret) {
743                 if (radeon_connector->edid) {
744                         kfree(radeon_connector->edid);
745                         radeon_connector->edid = NULL;
746                 }
747                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
748
749                 if (!radeon_connector->edid) {
750                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
751                                         drm_get_connector_name(connector));
752                 } else {
753                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
754
755                         /* some oems have boards with separate digital and analog connectors
756                          * with a shared ddc line (often vga + hdmi)
757                          */
758                         if ((!radeon_connector->use_digital) && radeon_connector->shared_ddc) {
759                                 kfree(radeon_connector->edid);
760                                 radeon_connector->edid = NULL;
761                                 ret = connector_status_disconnected;
762                         } else
763                                 ret = connector_status_connected;
764
765                         /* multiple connectors on the same encoder with the same ddc line
766                          * This tends to be HDMI and DVI on the same encoder with the
767                          * same ddc line.  If the edid says HDMI, consider the HDMI port
768                          * connected and the DVI port disconnected.  If the edid doesn't
769                          * say HDMI, vice versa.
770                          */
771                         if (radeon_connector->shared_ddc && (ret == connector_status_connected)) {
772                                 struct drm_device *dev = connector->dev;
773                                 struct drm_connector *list_connector;
774                                 struct radeon_connector *list_radeon_connector;
775                                 list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
776                                         if (connector == list_connector)
777                                                 continue;
778                                         list_radeon_connector = to_radeon_connector(list_connector);
779                                         if (radeon_connector->devices == list_radeon_connector->devices) {
780                                                 if (drm_detect_hdmi_monitor(radeon_connector->edid)) {
781                                                         if (connector->connector_type == DRM_MODE_CONNECTOR_DVID) {
782                                                                 kfree(radeon_connector->edid);
783                                                                 radeon_connector->edid = NULL;
784                                                                 ret = connector_status_disconnected;
785                                                         }
786                                                 } else {
787                                                         if ((connector->connector_type == DRM_MODE_CONNECTOR_HDMIA) ||
788                                                             (connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)) {
789                                                                 kfree(radeon_connector->edid);
790                                                                 radeon_connector->edid = NULL;
791                                                                 ret = connector_status_disconnected;
792                                                         }
793                                                 }
794                                         }
795                                 }
796                         }
797                 }
798         }
799
800         if ((ret == connector_status_connected) && (radeon_connector->use_digital == true))
801                 goto out;
802
803         /* find analog encoder */
804         if (radeon_connector->dac_load_detect) {
805                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
806                         if (connector->encoder_ids[i] == 0)
807                                 break;
808
809                         obj = drm_mode_object_find(connector->dev,
810                                                    connector->encoder_ids[i],
811                                                    DRM_MODE_OBJECT_ENCODER);
812                         if (!obj)
813                                 continue;
814
815                         encoder = obj_to_encoder(obj);
816
817                         encoder_funcs = encoder->helper_private;
818                         if (encoder_funcs->detect) {
819                                 if (ret != connector_status_connected) {
820                                         ret = encoder_funcs->detect(encoder, connector);
821                                         if (ret == connector_status_connected) {
822                                                 radeon_connector->use_digital = false;
823                                         }
824                                 }
825                                 break;
826                         }
827                 }
828         }
829
830         if ((ret == connector_status_connected) && (radeon_connector->use_digital == false) &&
831             encoder) {
832                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
833         }
834
835 out:
836         /* updated in get modes as well since we need to know if it's analog or digital */
837         radeon_connector_update_scratch_regs(connector, ret);
838         return ret;
839 }
840
841 /* okay need to be smart in here about which encoder to pick */
842 struct drm_encoder *radeon_dvi_encoder(struct drm_connector *connector)
843 {
844         int enc_id = connector->encoder_ids[0];
845         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
846         struct drm_mode_object *obj;
847         struct drm_encoder *encoder;
848         int i;
849         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
850                 if (connector->encoder_ids[i] == 0)
851                         break;
852
853                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
854                 if (!obj)
855                         continue;
856
857                 encoder = obj_to_encoder(obj);
858
859                 if (radeon_connector->use_digital == true) {
860                         if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
861                                 return encoder;
862                 } else {
863                         if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
864                             encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
865                                 return encoder;
866                 }
867         }
868
869         /* see if we have a default encoder  TODO */
870
871         /* then check use digitial */
872         /* pick the first one */
873         if (enc_id) {
874                 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
875                 if (!obj)
876                         return NULL;
877                 encoder = obj_to_encoder(obj);
878                 return encoder;
879         }
880         return NULL;
881 }
882
883 static void radeon_dvi_force(struct drm_connector *connector)
884 {
885         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
886         if (connector->force == DRM_FORCE_ON)
887                 radeon_connector->use_digital = false;
888         if (connector->force == DRM_FORCE_ON_DIGITAL)
889                 radeon_connector->use_digital = true;
890 }
891
892 static int radeon_dvi_mode_valid(struct drm_connector *connector,
893                                   struct drm_display_mode *mode)
894 {
895         struct drm_device *dev = connector->dev;
896         struct radeon_device *rdev = dev->dev_private;
897         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
898
899         /* XXX check mode bandwidth */
900
901         /* clocks over 135 MHz have heat issues with DVI on RV100 */
902         if (radeon_connector->use_digital &&
903             (rdev->family == CHIP_RV100) &&
904             (mode->clock > 135000))
905                 return MODE_CLOCK_HIGH;
906
907         if (radeon_connector->use_digital && (mode->clock > 165000)) {
908                 if ((radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
909                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
910                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B))
911                         return MODE_OK;
912                 else
913                         return MODE_CLOCK_HIGH;
914         }
915         return MODE_OK;
916 }
917
918 struct drm_connector_helper_funcs radeon_dvi_connector_helper_funcs = {
919         .get_modes = radeon_dvi_get_modes,
920         .mode_valid = radeon_dvi_mode_valid,
921         .best_encoder = radeon_dvi_encoder,
922 };
923
924 struct drm_connector_funcs radeon_dvi_connector_funcs = {
925         .dpms = drm_helper_connector_dpms,
926         .detect = radeon_dvi_detect,
927         .fill_modes = drm_helper_probe_single_connector_modes,
928         .set_property = radeon_connector_set_property,
929         .destroy = radeon_connector_destroy,
930         .force = radeon_dvi_force,
931 };
932
933 static void radeon_dp_connector_destroy(struct drm_connector *connector)
934 {
935         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
936         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
937
938         if (radeon_connector->ddc_bus)
939                 radeon_i2c_destroy(radeon_connector->ddc_bus);
940         if (radeon_connector->edid)
941                 kfree(radeon_connector->edid);
942         if (radeon_dig_connector->dp_i2c_bus)
943                 radeon_i2c_destroy_dp(radeon_dig_connector->dp_i2c_bus);
944         kfree(radeon_connector->con_priv);
945         drm_sysfs_connector_remove(connector);
946         drm_connector_cleanup(connector);
947         kfree(connector);
948 }
949
950 static int radeon_dp_get_modes(struct drm_connector *connector)
951 {
952         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
953         int ret;
954
955         ret = radeon_ddc_get_modes(radeon_connector);
956         return ret;
957 }
958
959 static enum drm_connector_status radeon_dp_detect(struct drm_connector *connector)
960 {
961         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
962         enum drm_connector_status ret = connector_status_disconnected;
963         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
964         u8 sink_type;
965
966         if (radeon_connector->edid) {
967                 kfree(radeon_connector->edid);
968                 radeon_connector->edid = NULL;
969         }
970
971         sink_type = radeon_dp_getsinktype(radeon_connector);
972         if ((sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
973             (sink_type == CONNECTOR_OBJECT_ID_eDP)) {
974                 if (radeon_dp_getdpcd(radeon_connector)) {
975                         radeon_dig_connector->dp_sink_type = sink_type;
976                         ret = connector_status_connected;
977                 }
978         } else {
979                 if (radeon_ddc_probe(radeon_connector)) {
980                         radeon_dig_connector->dp_sink_type = sink_type;
981                         ret = connector_status_connected;
982                 }
983         }
984
985         return ret;
986 }
987
988 static int radeon_dp_mode_valid(struct drm_connector *connector,
989                                   struct drm_display_mode *mode)
990 {
991         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
992         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
993
994         /* XXX check mode bandwidth */
995
996         if ((radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
997             (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
998                 return radeon_dp_mode_valid_helper(radeon_connector, mode);
999         else
1000                 return MODE_OK;
1001 }
1002
1003 struct drm_connector_helper_funcs radeon_dp_connector_helper_funcs = {
1004         .get_modes = radeon_dp_get_modes,
1005         .mode_valid = radeon_dp_mode_valid,
1006         .best_encoder = radeon_dvi_encoder,
1007 };
1008
1009 struct drm_connector_funcs radeon_dp_connector_funcs = {
1010         .dpms = drm_helper_connector_dpms,
1011         .detect = radeon_dp_detect,
1012         .fill_modes = drm_helper_probe_single_connector_modes,
1013         .set_property = radeon_connector_set_property,
1014         .destroy = radeon_dp_connector_destroy,
1015         .force = radeon_dvi_force,
1016 };
1017
1018 void
1019 radeon_add_atom_connector(struct drm_device *dev,
1020                           uint32_t connector_id,
1021                           uint32_t supported_device,
1022                           int connector_type,
1023                           struct radeon_i2c_bus_rec *i2c_bus,
1024                           bool linkb,
1025                           uint32_t igp_lane_info,
1026                           uint16_t connector_object_id,
1027                           struct radeon_hpd *hpd)
1028 {
1029         struct radeon_device *rdev = dev->dev_private;
1030         struct drm_connector *connector;
1031         struct radeon_connector *radeon_connector;
1032         struct radeon_connector_atom_dig *radeon_dig_connector;
1033         uint32_t subpixel_order = SubPixelNone;
1034         bool shared_ddc = false;
1035         int ret;
1036
1037         /* fixme - tv/cv/din */
1038         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1039                 return;
1040
1041         /* see if we already added it */
1042         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1043                 radeon_connector = to_radeon_connector(connector);
1044                 if (radeon_connector->connector_id == connector_id) {
1045                         radeon_connector->devices |= supported_device;
1046                         return;
1047                 }
1048                 if (radeon_connector->ddc_bus && i2c_bus->valid) {
1049                         if (radeon_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1050                                 radeon_connector->shared_ddc = true;
1051                                 shared_ddc = true;
1052                         }
1053                 }
1054         }
1055
1056         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1057         if (!radeon_connector)
1058                 return;
1059
1060         connector = &radeon_connector->base;
1061
1062         radeon_connector->connector_id = connector_id;
1063         radeon_connector->devices = supported_device;
1064         radeon_connector->shared_ddc = shared_ddc;
1065         radeon_connector->connector_object_id = connector_object_id;
1066         radeon_connector->hpd = *hpd;
1067         switch (connector_type) {
1068         case DRM_MODE_CONNECTOR_VGA:
1069                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1070                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1071                 if (ret)
1072                         goto failed;
1073                 if (i2c_bus->valid) {
1074                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "VGA");
1075                         if (!radeon_connector->ddc_bus)
1076                                 goto failed;
1077                 }
1078                 radeon_connector->dac_load_detect = true;
1079                 drm_connector_attach_property(&radeon_connector->base,
1080                                               rdev->mode_info.load_detect_property,
1081                                               1);
1082                 break;
1083         case DRM_MODE_CONNECTOR_DVIA:
1084                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1085                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1086                 if (ret)
1087                         goto failed;
1088                 if (i2c_bus->valid) {
1089                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1090                         if (!radeon_connector->ddc_bus)
1091                                 goto failed;
1092                 }
1093                 radeon_connector->dac_load_detect = true;
1094                 drm_connector_attach_property(&radeon_connector->base,
1095                                               rdev->mode_info.load_detect_property,
1096                                               1);
1097                 break;
1098         case DRM_MODE_CONNECTOR_DVII:
1099         case DRM_MODE_CONNECTOR_DVID:
1100                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1101                 if (!radeon_dig_connector)
1102                         goto failed;
1103                 radeon_dig_connector->linkb = linkb;
1104                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1105                 radeon_connector->con_priv = radeon_dig_connector;
1106                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1107                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1108                 if (ret)
1109                         goto failed;
1110                 if (i2c_bus->valid) {
1111                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1112                         if (!radeon_connector->ddc_bus)
1113                                 goto failed;
1114                 }
1115                 subpixel_order = SubPixelHorizontalRGB;
1116                 drm_connector_attach_property(&radeon_connector->base,
1117                                               rdev->mode_info.coherent_mode_property,
1118                                               1);
1119                 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1120                         radeon_connector->dac_load_detect = true;
1121                         drm_connector_attach_property(&radeon_connector->base,
1122                                                       rdev->mode_info.load_detect_property,
1123                                                       1);
1124                 }
1125                 break;
1126         case DRM_MODE_CONNECTOR_HDMIA:
1127         case DRM_MODE_CONNECTOR_HDMIB:
1128                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1129                 if (!radeon_dig_connector)
1130                         goto failed;
1131                 radeon_dig_connector->linkb = linkb;
1132                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1133                 radeon_connector->con_priv = radeon_dig_connector;
1134                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1135                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1136                 if (ret)
1137                         goto failed;
1138                 if (i2c_bus->valid) {
1139                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "HDMI");
1140                         if (!radeon_connector->ddc_bus)
1141                                 goto failed;
1142                 }
1143                 drm_connector_attach_property(&radeon_connector->base,
1144                                               rdev->mode_info.coherent_mode_property,
1145                                               1);
1146                 subpixel_order = SubPixelHorizontalRGB;
1147                 break;
1148         case DRM_MODE_CONNECTOR_DisplayPort:
1149         case DRM_MODE_CONNECTOR_eDP:
1150                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1151                 if (!radeon_dig_connector)
1152                         goto failed;
1153                 radeon_dig_connector->linkb = linkb;
1154                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1155                 radeon_connector->con_priv = radeon_dig_connector;
1156                 drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1157                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1158                 if (ret)
1159                         goto failed;
1160                 if (i2c_bus->valid) {
1161                         /* add DP i2c bus */
1162                         if (connector_type == DRM_MODE_CONNECTOR_eDP)
1163                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "eDP-auxch");
1164                         else
1165                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1166                         if (!radeon_dig_connector->dp_i2c_bus)
1167                                 goto failed;
1168                         if (connector_type == DRM_MODE_CONNECTOR_eDP)
1169                                 radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "eDP");
1170                         else
1171                                 radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DP");
1172                         if (!radeon_connector->ddc_bus)
1173                                 goto failed;
1174                 }
1175                 subpixel_order = SubPixelHorizontalRGB;
1176                 drm_connector_attach_property(&radeon_connector->base,
1177                                               rdev->mode_info.coherent_mode_property,
1178                                               1);
1179                 break;
1180         case DRM_MODE_CONNECTOR_SVIDEO:
1181         case DRM_MODE_CONNECTOR_Composite:
1182         case DRM_MODE_CONNECTOR_9PinDIN:
1183                 if (radeon_tv == 1) {
1184                         drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1185                         ret = drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1186                         if (ret)
1187                                 goto failed;
1188                         radeon_connector->dac_load_detect = true;
1189                         drm_connector_attach_property(&radeon_connector->base,
1190                                                       rdev->mode_info.load_detect_property,
1191                                                       1);
1192                         drm_connector_attach_property(&radeon_connector->base,
1193                                                       rdev->mode_info.tv_std_property,
1194                                                       radeon_atombios_get_tv_info(rdev));
1195                 }
1196                 break;
1197         case DRM_MODE_CONNECTOR_LVDS:
1198                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1199                 if (!radeon_dig_connector)
1200                         goto failed;
1201                 radeon_dig_connector->linkb = linkb;
1202                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1203                 radeon_connector->con_priv = radeon_dig_connector;
1204                 drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1205                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1206                 if (ret)
1207                         goto failed;
1208                 if (i2c_bus->valid) {
1209                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "LVDS");
1210                         if (!radeon_connector->ddc_bus)
1211                                 goto failed;
1212                 }
1213                 drm_connector_attach_property(&radeon_connector->base,
1214                                               dev->mode_config.scaling_mode_property,
1215                                               DRM_MODE_SCALE_FULLSCREEN);
1216                 subpixel_order = SubPixelHorizontalRGB;
1217                 break;
1218         }
1219
1220         connector->display_info.subpixel_order = subpixel_order;
1221         drm_sysfs_connector_add(connector);
1222         return;
1223
1224 failed:
1225         if (radeon_connector->ddc_bus)
1226                 radeon_i2c_destroy(radeon_connector->ddc_bus);
1227         drm_connector_cleanup(connector);
1228         kfree(connector);
1229 }
1230
1231 void
1232 radeon_add_legacy_connector(struct drm_device *dev,
1233                             uint32_t connector_id,
1234                             uint32_t supported_device,
1235                             int connector_type,
1236                             struct radeon_i2c_bus_rec *i2c_bus,
1237                             uint16_t connector_object_id,
1238                             struct radeon_hpd *hpd)
1239 {
1240         struct radeon_device *rdev = dev->dev_private;
1241         struct drm_connector *connector;
1242         struct radeon_connector *radeon_connector;
1243         uint32_t subpixel_order = SubPixelNone;
1244         int ret;
1245
1246         /* fixme - tv/cv/din */
1247         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1248                 return;
1249
1250         /* see if we already added it */
1251         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1252                 radeon_connector = to_radeon_connector(connector);
1253                 if (radeon_connector->connector_id == connector_id) {
1254                         radeon_connector->devices |= supported_device;
1255                         return;
1256                 }
1257         }
1258
1259         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1260         if (!radeon_connector)
1261                 return;
1262
1263         connector = &radeon_connector->base;
1264
1265         radeon_connector->connector_id = connector_id;
1266         radeon_connector->devices = supported_device;
1267         radeon_connector->connector_object_id = connector_object_id;
1268         radeon_connector->hpd = *hpd;
1269         switch (connector_type) {
1270         case DRM_MODE_CONNECTOR_VGA:
1271                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1272                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1273                 if (ret)
1274                         goto failed;
1275                 if (i2c_bus->valid) {
1276                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "VGA");
1277                         if (!radeon_connector->ddc_bus)
1278                                 goto failed;
1279                 }
1280                 radeon_connector->dac_load_detect = true;
1281                 drm_connector_attach_property(&radeon_connector->base,
1282                                               rdev->mode_info.load_detect_property,
1283                                               1);
1284                 break;
1285         case DRM_MODE_CONNECTOR_DVIA:
1286                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1287                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1288                 if (ret)
1289                         goto failed;
1290                 if (i2c_bus->valid) {
1291                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1292                         if (!radeon_connector->ddc_bus)
1293                                 goto failed;
1294                 }
1295                 radeon_connector->dac_load_detect = true;
1296                 drm_connector_attach_property(&radeon_connector->base,
1297                                               rdev->mode_info.load_detect_property,
1298                                               1);
1299                 break;
1300         case DRM_MODE_CONNECTOR_DVII:
1301         case DRM_MODE_CONNECTOR_DVID:
1302                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1303                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1304                 if (ret)
1305                         goto failed;
1306                 if (i2c_bus->valid) {
1307                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1308                         if (!radeon_connector->ddc_bus)
1309                                 goto failed;
1310                         radeon_connector->dac_load_detect = true;
1311                         drm_connector_attach_property(&radeon_connector->base,
1312                                                       rdev->mode_info.load_detect_property,
1313                                                       1);
1314                 }
1315                 subpixel_order = SubPixelHorizontalRGB;
1316                 break;
1317         case DRM_MODE_CONNECTOR_SVIDEO:
1318         case DRM_MODE_CONNECTOR_Composite:
1319         case DRM_MODE_CONNECTOR_9PinDIN:
1320                 if (radeon_tv == 1) {
1321                         drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1322                         ret = drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1323                         if (ret)
1324                                 goto failed;
1325                         radeon_connector->dac_load_detect = true;
1326                         /* RS400,RC410,RS480 chipset seems to report a lot
1327                          * of false positive on load detect, we haven't yet
1328                          * found a way to make load detect reliable on those
1329                          * chipset, thus just disable it for TV.
1330                          */
1331                         if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480)
1332                                 radeon_connector->dac_load_detect = false;
1333                         drm_connector_attach_property(&radeon_connector->base,
1334                                                       rdev->mode_info.load_detect_property,
1335                                                       radeon_connector->dac_load_detect);
1336                         drm_connector_attach_property(&radeon_connector->base,
1337                                                       rdev->mode_info.tv_std_property,
1338                                                       radeon_combios_get_tv_info(rdev));
1339                 }
1340                 break;
1341         case DRM_MODE_CONNECTOR_LVDS:
1342                 drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1343                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1344                 if (ret)
1345                         goto failed;
1346                 if (i2c_bus->valid) {
1347                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "LVDS");
1348                         if (!radeon_connector->ddc_bus)
1349                                 goto failed;
1350                 }
1351                 drm_connector_attach_property(&radeon_connector->base,
1352                                               dev->mode_config.scaling_mode_property,
1353                                               DRM_MODE_SCALE_FULLSCREEN);
1354                 subpixel_order = SubPixelHorizontalRGB;
1355                 break;
1356         }
1357
1358         connector->display_info.subpixel_order = subpixel_order;
1359         drm_sysfs_connector_add(connector);
1360         return;
1361
1362 failed:
1363         if (radeon_connector->ddc_bus)
1364                 radeon_i2c_destroy(radeon_connector->ddc_bus);
1365         drm_connector_cleanup(connector);
1366         kfree(connector);
1367 }