Merge remote branch 'anholt/drm-intel-next' into drm-next-stage
[sfrench/cifs-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_i2c_do_lock(radeon_connector->ddc_bus, 1);
483                         radeon_connector->edid = drm_get_edid(&radeon_connector->base,
484                                                               &radeon_connector->ddc_bus->adapter);
485                         radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
486                         if (radeon_connector->edid)
487                                 ret = connector_status_connected;
488                 }
489         }
490         /* check acpi lid status ??? */
491
492         radeon_connector_update_scratch_regs(connector, ret);
493         return ret;
494 }
495
496 static void radeon_connector_destroy(struct drm_connector *connector)
497 {
498         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
499
500         if (radeon_connector->ddc_bus)
501                 radeon_i2c_destroy(radeon_connector->ddc_bus);
502         if (radeon_connector->edid)
503                 kfree(radeon_connector->edid);
504         kfree(radeon_connector->con_priv);
505         drm_sysfs_connector_remove(connector);
506         drm_connector_cleanup(connector);
507         kfree(connector);
508 }
509
510 static int radeon_lvds_set_property(struct drm_connector *connector,
511                                     struct drm_property *property,
512                                     uint64_t value)
513 {
514         struct drm_device *dev = connector->dev;
515         struct radeon_encoder *radeon_encoder;
516         enum radeon_rmx_type rmx_type;
517
518         DRM_DEBUG("\n");
519         if (property != dev->mode_config.scaling_mode_property)
520                 return 0;
521
522         if (connector->encoder)
523                 radeon_encoder = to_radeon_encoder(connector->encoder);
524         else {
525                 struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
526                 radeon_encoder = to_radeon_encoder(connector_funcs->best_encoder(connector));
527         }
528
529         switch (value) {
530         case DRM_MODE_SCALE_NONE: rmx_type = RMX_OFF; break;
531         case DRM_MODE_SCALE_CENTER: rmx_type = RMX_CENTER; break;
532         case DRM_MODE_SCALE_ASPECT: rmx_type = RMX_ASPECT; break;
533         default:
534         case DRM_MODE_SCALE_FULLSCREEN: rmx_type = RMX_FULL; break;
535         }
536         if (radeon_encoder->rmx_type == rmx_type)
537                 return 0;
538
539         radeon_encoder->rmx_type = rmx_type;
540
541         radeon_property_change_mode(&radeon_encoder->base);
542         return 0;
543 }
544
545
546 struct drm_connector_helper_funcs radeon_lvds_connector_helper_funcs = {
547         .get_modes = radeon_lvds_get_modes,
548         .mode_valid = radeon_lvds_mode_valid,
549         .best_encoder = radeon_best_single_encoder,
550 };
551
552 struct drm_connector_funcs radeon_lvds_connector_funcs = {
553         .dpms = drm_helper_connector_dpms,
554         .detect = radeon_lvds_detect,
555         .fill_modes = drm_helper_probe_single_connector_modes,
556         .destroy = radeon_connector_destroy,
557         .set_property = radeon_lvds_set_property,
558 };
559
560 static int radeon_vga_get_modes(struct drm_connector *connector)
561 {
562         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
563         int ret;
564
565         ret = radeon_ddc_get_modes(radeon_connector);
566
567         return ret;
568 }
569
570 static int radeon_vga_mode_valid(struct drm_connector *connector,
571                                   struct drm_display_mode *mode)
572 {
573         /* XXX check mode bandwidth */
574         /* XXX verify against max DAC output frequency */
575         return MODE_OK;
576 }
577
578 static enum drm_connector_status radeon_vga_detect(struct drm_connector *connector)
579 {
580         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
581         struct drm_encoder *encoder;
582         struct drm_encoder_helper_funcs *encoder_funcs;
583         bool dret = false;
584         enum drm_connector_status ret = connector_status_disconnected;
585
586         encoder = radeon_best_single_encoder(connector);
587         if (!encoder)
588                 ret = connector_status_disconnected;
589
590         if (radeon_connector->ddc_bus) {
591                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
592                 dret = radeon_ddc_probe(radeon_connector);
593                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
594         }
595         if (dret) {
596                 if (radeon_connector->edid) {
597                         kfree(radeon_connector->edid);
598                         radeon_connector->edid = NULL;
599                 }
600                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
601                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
602                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
603
604                 if (!radeon_connector->edid) {
605                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
606                                         drm_get_connector_name(connector));
607                         ret = connector_status_connected;
608                 } else {
609                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
610
611                         /* some oems have boards with separate digital and analog connectors
612                          * with a shared ddc line (often vga + hdmi)
613                          */
614                         if (radeon_connector->use_digital && radeon_connector->shared_ddc) {
615                                 kfree(radeon_connector->edid);
616                                 radeon_connector->edid = NULL;
617                                 ret = connector_status_disconnected;
618                         } else
619                                 ret = connector_status_connected;
620                 }
621         } else {
622                 if (radeon_connector->dac_load_detect && encoder) {
623                         encoder_funcs = encoder->helper_private;
624                         ret = encoder_funcs->detect(encoder, connector);
625                 }
626         }
627
628         if (ret == connector_status_connected)
629                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
630         radeon_connector_update_scratch_regs(connector, ret);
631         return ret;
632 }
633
634 struct drm_connector_helper_funcs radeon_vga_connector_helper_funcs = {
635         .get_modes = radeon_vga_get_modes,
636         .mode_valid = radeon_vga_mode_valid,
637         .best_encoder = radeon_best_single_encoder,
638 };
639
640 struct drm_connector_funcs radeon_vga_connector_funcs = {
641         .dpms = drm_helper_connector_dpms,
642         .detect = radeon_vga_detect,
643         .fill_modes = drm_helper_probe_single_connector_modes,
644         .destroy = radeon_connector_destroy,
645         .set_property = radeon_connector_set_property,
646 };
647
648 static int radeon_tv_get_modes(struct drm_connector *connector)
649 {
650         struct drm_device *dev = connector->dev;
651         struct radeon_device *rdev = dev->dev_private;
652         struct drm_display_mode *tv_mode;
653         struct drm_encoder *encoder;
654
655         encoder = radeon_best_single_encoder(connector);
656         if (!encoder)
657                 return 0;
658
659         /* avivo chips can scale any mode */
660         if (rdev->family >= CHIP_RS600)
661                 /* add scaled modes */
662                 radeon_add_common_modes(encoder, connector);
663         else {
664                 /* only 800x600 is supported right now on pre-avivo chips */
665                 tv_mode = drm_cvt_mode(dev, 800, 600, 60, false, false, false);
666                 tv_mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
667                 drm_mode_probed_add(connector, tv_mode);
668         }
669         return 1;
670 }
671
672 static int radeon_tv_mode_valid(struct drm_connector *connector,
673                                 struct drm_display_mode *mode)
674 {
675         if ((mode->hdisplay > 1024) || (mode->vdisplay > 768))
676                 return MODE_CLOCK_RANGE;
677         return MODE_OK;
678 }
679
680 static enum drm_connector_status radeon_tv_detect(struct drm_connector *connector)
681 {
682         struct drm_encoder *encoder;
683         struct drm_encoder_helper_funcs *encoder_funcs;
684         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
685         enum drm_connector_status ret = connector_status_disconnected;
686
687         if (!radeon_connector->dac_load_detect)
688                 return ret;
689
690         encoder = radeon_best_single_encoder(connector);
691         if (!encoder)
692                 ret = connector_status_disconnected;
693         else {
694                 encoder_funcs = encoder->helper_private;
695                 ret = encoder_funcs->detect(encoder, connector);
696         }
697         if (ret == connector_status_connected)
698                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, false);
699         radeon_connector_update_scratch_regs(connector, ret);
700         return ret;
701 }
702
703 struct drm_connector_helper_funcs radeon_tv_connector_helper_funcs = {
704         .get_modes = radeon_tv_get_modes,
705         .mode_valid = radeon_tv_mode_valid,
706         .best_encoder = radeon_best_single_encoder,
707 };
708
709 struct drm_connector_funcs radeon_tv_connector_funcs = {
710         .dpms = drm_helper_connector_dpms,
711         .detect = radeon_tv_detect,
712         .fill_modes = drm_helper_probe_single_connector_modes,
713         .destroy = radeon_connector_destroy,
714         .set_property = radeon_connector_set_property,
715 };
716
717 static int radeon_dvi_get_modes(struct drm_connector *connector)
718 {
719         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
720         int ret;
721
722         ret = radeon_ddc_get_modes(radeon_connector);
723         return ret;
724 }
725
726 /*
727  * DVI is complicated
728  * Do a DDC probe, if DDC probe passes, get the full EDID so
729  * we can do analog/digital monitor detection at this point.
730  * If the monitor is an analog monitor or we got no DDC,
731  * we need to find the DAC encoder object for this connector.
732  * If we got no DDC, we do load detection on the DAC encoder object.
733  * If we got analog DDC or load detection passes on the DAC encoder
734  * we have to check if this analog encoder is shared with anyone else (TV)
735  * if its shared we have to set the other connector to disconnected.
736  */
737 static enum drm_connector_status radeon_dvi_detect(struct drm_connector *connector)
738 {
739         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
740         struct drm_encoder *encoder = NULL;
741         struct drm_encoder_helper_funcs *encoder_funcs;
742         struct drm_mode_object *obj;
743         int i;
744         enum drm_connector_status ret = connector_status_disconnected;
745         bool dret = false;
746
747         if (radeon_connector->ddc_bus) {
748                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
749                 dret = radeon_ddc_probe(radeon_connector);
750                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
751         }
752         if (dret) {
753                 if (radeon_connector->edid) {
754                         kfree(radeon_connector->edid);
755                         radeon_connector->edid = NULL;
756                 }
757                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
758                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
759                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
760
761                 if (!radeon_connector->edid) {
762                         DRM_ERROR("%s: probed a monitor but no|invalid EDID\n",
763                                         drm_get_connector_name(connector));
764                 } else {
765                         radeon_connector->use_digital = !!(radeon_connector->edid->input & DRM_EDID_INPUT_DIGITAL);
766
767                         /* some oems have boards with separate digital and analog connectors
768                          * with a shared ddc line (often vga + hdmi)
769                          */
770                         if ((!radeon_connector->use_digital) && radeon_connector->shared_ddc) {
771                                 kfree(radeon_connector->edid);
772                                 radeon_connector->edid = NULL;
773                                 ret = connector_status_disconnected;
774                         } else
775                                 ret = connector_status_connected;
776
777                         /* multiple connectors on the same encoder with the same ddc line
778                          * This tends to be HDMI and DVI on the same encoder with the
779                          * same ddc line.  If the edid says HDMI, consider the HDMI port
780                          * connected and the DVI port disconnected.  If the edid doesn't
781                          * say HDMI, vice versa.
782                          */
783                         if (radeon_connector->shared_ddc && (ret == connector_status_connected)) {
784                                 struct drm_device *dev = connector->dev;
785                                 struct drm_connector *list_connector;
786                                 struct radeon_connector *list_radeon_connector;
787                                 list_for_each_entry(list_connector, &dev->mode_config.connector_list, head) {
788                                         if (connector == list_connector)
789                                                 continue;
790                                         list_radeon_connector = to_radeon_connector(list_connector);
791                                         if (radeon_connector->devices == list_radeon_connector->devices) {
792                                                 if (drm_detect_hdmi_monitor(radeon_connector->edid)) {
793                                                         if (connector->connector_type == DRM_MODE_CONNECTOR_DVID) {
794                                                                 kfree(radeon_connector->edid);
795                                                                 radeon_connector->edid = NULL;
796                                                                 ret = connector_status_disconnected;
797                                                         }
798                                                 } else {
799                                                         if ((connector->connector_type == DRM_MODE_CONNECTOR_HDMIA) ||
800                                                             (connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)) {
801                                                                 kfree(radeon_connector->edid);
802                                                                 radeon_connector->edid = NULL;
803                                                                 ret = connector_status_disconnected;
804                                                         }
805                                                 }
806                                         }
807                                 }
808                         }
809                 }
810         }
811
812         if ((ret == connector_status_connected) && (radeon_connector->use_digital == true))
813                 goto out;
814
815         /* find analog encoder */
816         if (radeon_connector->dac_load_detect) {
817                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
818                         if (connector->encoder_ids[i] == 0)
819                                 break;
820
821                         obj = drm_mode_object_find(connector->dev,
822                                                    connector->encoder_ids[i],
823                                                    DRM_MODE_OBJECT_ENCODER);
824                         if (!obj)
825                                 continue;
826
827                         encoder = obj_to_encoder(obj);
828
829                         encoder_funcs = encoder->helper_private;
830                         if (encoder_funcs->detect) {
831                                 if (ret != connector_status_connected) {
832                                         ret = encoder_funcs->detect(encoder, connector);
833                                         if (ret == connector_status_connected) {
834                                                 radeon_connector->use_digital = false;
835                                         }
836                                 }
837                                 break;
838                         }
839                 }
840         }
841
842         if ((ret == connector_status_connected) && (radeon_connector->use_digital == false) &&
843             encoder) {
844                 ret = radeon_connector_analog_encoder_conflict_solve(connector, encoder, ret, true);
845         }
846
847 out:
848         /* updated in get modes as well since we need to know if it's analog or digital */
849         radeon_connector_update_scratch_regs(connector, ret);
850         return ret;
851 }
852
853 /* okay need to be smart in here about which encoder to pick */
854 struct drm_encoder *radeon_dvi_encoder(struct drm_connector *connector)
855 {
856         int enc_id = connector->encoder_ids[0];
857         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
858         struct drm_mode_object *obj;
859         struct drm_encoder *encoder;
860         int i;
861         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
862                 if (connector->encoder_ids[i] == 0)
863                         break;
864
865                 obj = drm_mode_object_find(connector->dev, connector->encoder_ids[i], DRM_MODE_OBJECT_ENCODER);
866                 if (!obj)
867                         continue;
868
869                 encoder = obj_to_encoder(obj);
870
871                 if (radeon_connector->use_digital == true) {
872                         if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
873                                 return encoder;
874                 } else {
875                         if (encoder->encoder_type == DRM_MODE_ENCODER_DAC ||
876                             encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
877                                 return encoder;
878                 }
879         }
880
881         /* see if we have a default encoder  TODO */
882
883         /* then check use digitial */
884         /* pick the first one */
885         if (enc_id) {
886                 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
887                 if (!obj)
888                         return NULL;
889                 encoder = obj_to_encoder(obj);
890                 return encoder;
891         }
892         return NULL;
893 }
894
895 static void radeon_dvi_force(struct drm_connector *connector)
896 {
897         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
898         if (connector->force == DRM_FORCE_ON)
899                 radeon_connector->use_digital = false;
900         if (connector->force == DRM_FORCE_ON_DIGITAL)
901                 radeon_connector->use_digital = true;
902 }
903
904 static int radeon_dvi_mode_valid(struct drm_connector *connector,
905                                   struct drm_display_mode *mode)
906 {
907         struct drm_device *dev = connector->dev;
908         struct radeon_device *rdev = dev->dev_private;
909         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
910
911         /* XXX check mode bandwidth */
912
913         /* clocks over 135 MHz have heat issues with DVI on RV100 */
914         if (radeon_connector->use_digital &&
915             (rdev->family == CHIP_RV100) &&
916             (mode->clock > 135000))
917                 return MODE_CLOCK_HIGH;
918
919         if (radeon_connector->use_digital && (mode->clock > 165000)) {
920                 if ((radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I) ||
921                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D) ||
922                     (radeon_connector->connector_object_id == CONNECTOR_OBJECT_ID_HDMI_TYPE_B))
923                         return MODE_OK;
924                 else
925                         return MODE_CLOCK_HIGH;
926         }
927         return MODE_OK;
928 }
929
930 struct drm_connector_helper_funcs radeon_dvi_connector_helper_funcs = {
931         .get_modes = radeon_dvi_get_modes,
932         .mode_valid = radeon_dvi_mode_valid,
933         .best_encoder = radeon_dvi_encoder,
934 };
935
936 struct drm_connector_funcs radeon_dvi_connector_funcs = {
937         .dpms = drm_helper_connector_dpms,
938         .detect = radeon_dvi_detect,
939         .fill_modes = drm_helper_probe_single_connector_modes,
940         .set_property = radeon_connector_set_property,
941         .destroy = radeon_connector_destroy,
942         .force = radeon_dvi_force,
943 };
944
945 static void radeon_dp_connector_destroy(struct drm_connector *connector)
946 {
947         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
948         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
949
950         if (radeon_connector->ddc_bus)
951                 radeon_i2c_destroy(radeon_connector->ddc_bus);
952         if (radeon_connector->edid)
953                 kfree(radeon_connector->edid);
954         if (radeon_dig_connector->dp_i2c_bus)
955                 radeon_i2c_destroy(radeon_dig_connector->dp_i2c_bus);
956         kfree(radeon_connector->con_priv);
957         drm_sysfs_connector_remove(connector);
958         drm_connector_cleanup(connector);
959         kfree(connector);
960 }
961
962 static int radeon_dp_get_modes(struct drm_connector *connector)
963 {
964         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
965         int ret;
966
967         ret = radeon_ddc_get_modes(radeon_connector);
968         return ret;
969 }
970
971 static enum drm_connector_status radeon_dp_detect(struct drm_connector *connector)
972 {
973         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
974         enum drm_connector_status ret = connector_status_disconnected;
975         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
976         u8 sink_type;
977
978         if (radeon_connector->edid) {
979                 kfree(radeon_connector->edid);
980                 radeon_connector->edid = NULL;
981         }
982
983         sink_type = radeon_dp_getsinktype(radeon_connector);
984         if ((sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
985             (sink_type == CONNECTOR_OBJECT_ID_eDP)) {
986                 if (radeon_dp_getdpcd(radeon_connector)) {
987                         radeon_dig_connector->dp_sink_type = sink_type;
988                         ret = connector_status_connected;
989                 }
990         } else {
991                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
992                 if (radeon_ddc_probe(radeon_connector)) {
993                         radeon_dig_connector->dp_sink_type = sink_type;
994                         ret = connector_status_connected;
995                 }
996                 radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
997         }
998
999         return ret;
1000 }
1001
1002 static int radeon_dp_mode_valid(struct drm_connector *connector,
1003                                   struct drm_display_mode *mode)
1004 {
1005         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
1006         struct radeon_connector_atom_dig *radeon_dig_connector = radeon_connector->con_priv;
1007
1008         /* XXX check mode bandwidth */
1009
1010         if ((radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
1011             (radeon_dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP))
1012                 return radeon_dp_mode_valid_helper(radeon_connector, mode);
1013         else
1014                 return MODE_OK;
1015 }
1016
1017 struct drm_connector_helper_funcs radeon_dp_connector_helper_funcs = {
1018         .get_modes = radeon_dp_get_modes,
1019         .mode_valid = radeon_dp_mode_valid,
1020         .best_encoder = radeon_dvi_encoder,
1021 };
1022
1023 struct drm_connector_funcs radeon_dp_connector_funcs = {
1024         .dpms = drm_helper_connector_dpms,
1025         .detect = radeon_dp_detect,
1026         .fill_modes = drm_helper_probe_single_connector_modes,
1027         .set_property = radeon_connector_set_property,
1028         .destroy = radeon_dp_connector_destroy,
1029         .force = radeon_dvi_force,
1030 };
1031
1032 void
1033 radeon_add_atom_connector(struct drm_device *dev,
1034                           uint32_t connector_id,
1035                           uint32_t supported_device,
1036                           int connector_type,
1037                           struct radeon_i2c_bus_rec *i2c_bus,
1038                           bool linkb,
1039                           uint32_t igp_lane_info,
1040                           uint16_t connector_object_id,
1041                           struct radeon_hpd *hpd)
1042 {
1043         struct radeon_device *rdev = dev->dev_private;
1044         struct drm_connector *connector;
1045         struct radeon_connector *radeon_connector;
1046         struct radeon_connector_atom_dig *radeon_dig_connector;
1047         uint32_t subpixel_order = SubPixelNone;
1048         bool shared_ddc = false;
1049         int ret;
1050
1051         /* fixme - tv/cv/din */
1052         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1053                 return;
1054
1055         /* see if we already added it */
1056         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1057                 radeon_connector = to_radeon_connector(connector);
1058                 if (radeon_connector->connector_id == connector_id) {
1059                         radeon_connector->devices |= supported_device;
1060                         return;
1061                 }
1062                 if (radeon_connector->ddc_bus && i2c_bus->valid) {
1063                         if (radeon_connector->ddc_bus->rec.i2c_id == i2c_bus->i2c_id) {
1064                                 radeon_connector->shared_ddc = true;
1065                                 shared_ddc = true;
1066                         }
1067                 }
1068         }
1069
1070         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1071         if (!radeon_connector)
1072                 return;
1073
1074         connector = &radeon_connector->base;
1075
1076         radeon_connector->connector_id = connector_id;
1077         radeon_connector->devices = supported_device;
1078         radeon_connector->shared_ddc = shared_ddc;
1079         radeon_connector->connector_object_id = connector_object_id;
1080         radeon_connector->hpd = *hpd;
1081         switch (connector_type) {
1082         case DRM_MODE_CONNECTOR_VGA:
1083                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1084                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1085                 if (ret)
1086                         goto failed;
1087                 if (i2c_bus->valid) {
1088                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "VGA");
1089                         if (!radeon_connector->ddc_bus)
1090                                 goto failed;
1091                 }
1092                 radeon_connector->dac_load_detect = true;
1093                 drm_connector_attach_property(&radeon_connector->base,
1094                                               rdev->mode_info.load_detect_property,
1095                                               1);
1096                 break;
1097         case DRM_MODE_CONNECTOR_DVIA:
1098                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1099                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1100                 if (ret)
1101                         goto failed;
1102                 if (i2c_bus->valid) {
1103                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1104                         if (!radeon_connector->ddc_bus)
1105                                 goto failed;
1106                 }
1107                 radeon_connector->dac_load_detect = true;
1108                 drm_connector_attach_property(&radeon_connector->base,
1109                                               rdev->mode_info.load_detect_property,
1110                                               1);
1111                 break;
1112         case DRM_MODE_CONNECTOR_DVII:
1113         case DRM_MODE_CONNECTOR_DVID:
1114                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1115                 if (!radeon_dig_connector)
1116                         goto failed;
1117                 radeon_dig_connector->linkb = linkb;
1118                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1119                 radeon_connector->con_priv = radeon_dig_connector;
1120                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1121                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1122                 if (ret)
1123                         goto failed;
1124                 if (i2c_bus->valid) {
1125                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1126                         if (!radeon_connector->ddc_bus)
1127                                 goto failed;
1128                 }
1129                 subpixel_order = SubPixelHorizontalRGB;
1130                 drm_connector_attach_property(&radeon_connector->base,
1131                                               rdev->mode_info.coherent_mode_property,
1132                                               1);
1133                 if (connector_type == DRM_MODE_CONNECTOR_DVII) {
1134                         radeon_connector->dac_load_detect = true;
1135                         drm_connector_attach_property(&radeon_connector->base,
1136                                                       rdev->mode_info.load_detect_property,
1137                                                       1);
1138                 }
1139                 break;
1140         case DRM_MODE_CONNECTOR_HDMIA:
1141         case DRM_MODE_CONNECTOR_HDMIB:
1142                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1143                 if (!radeon_dig_connector)
1144                         goto failed;
1145                 radeon_dig_connector->linkb = linkb;
1146                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1147                 radeon_connector->con_priv = radeon_dig_connector;
1148                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1149                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1150                 if (ret)
1151                         goto failed;
1152                 if (i2c_bus->valid) {
1153                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "HDMI");
1154                         if (!radeon_connector->ddc_bus)
1155                                 goto failed;
1156                 }
1157                 drm_connector_attach_property(&radeon_connector->base,
1158                                               rdev->mode_info.coherent_mode_property,
1159                                               1);
1160                 subpixel_order = SubPixelHorizontalRGB;
1161                 break;
1162         case DRM_MODE_CONNECTOR_DisplayPort:
1163         case DRM_MODE_CONNECTOR_eDP:
1164                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1165                 if (!radeon_dig_connector)
1166                         goto failed;
1167                 radeon_dig_connector->linkb = linkb;
1168                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1169                 radeon_connector->con_priv = radeon_dig_connector;
1170                 drm_connector_init(dev, &radeon_connector->base, &radeon_dp_connector_funcs, connector_type);
1171                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dp_connector_helper_funcs);
1172                 if (ret)
1173                         goto failed;
1174                 if (i2c_bus->valid) {
1175                         /* add DP i2c bus */
1176                         if (connector_type == DRM_MODE_CONNECTOR_eDP)
1177                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "eDP-auxch");
1178                         else
1179                                 radeon_dig_connector->dp_i2c_bus = radeon_i2c_create_dp(dev, i2c_bus, "DP-auxch");
1180                         if (!radeon_dig_connector->dp_i2c_bus)
1181                                 goto failed;
1182                         if (connector_type == DRM_MODE_CONNECTOR_eDP)
1183                                 radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "eDP");
1184                         else
1185                                 radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DP");
1186                         if (!radeon_connector->ddc_bus)
1187                                 goto failed;
1188                 }
1189                 subpixel_order = SubPixelHorizontalRGB;
1190                 drm_connector_attach_property(&radeon_connector->base,
1191                                               rdev->mode_info.coherent_mode_property,
1192                                               1);
1193                 break;
1194         case DRM_MODE_CONNECTOR_SVIDEO:
1195         case DRM_MODE_CONNECTOR_Composite:
1196         case DRM_MODE_CONNECTOR_9PinDIN:
1197                 if (radeon_tv == 1) {
1198                         drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1199                         ret = drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1200                         if (ret)
1201                                 goto failed;
1202                         radeon_connector->dac_load_detect = true;
1203                         drm_connector_attach_property(&radeon_connector->base,
1204                                                       rdev->mode_info.load_detect_property,
1205                                                       1);
1206                         drm_connector_attach_property(&radeon_connector->base,
1207                                                       rdev->mode_info.tv_std_property,
1208                                                       radeon_atombios_get_tv_info(rdev));
1209                 }
1210                 break;
1211         case DRM_MODE_CONNECTOR_LVDS:
1212                 radeon_dig_connector = kzalloc(sizeof(struct radeon_connector_atom_dig), GFP_KERNEL);
1213                 if (!radeon_dig_connector)
1214                         goto failed;
1215                 radeon_dig_connector->linkb = linkb;
1216                 radeon_dig_connector->igp_lane_info = igp_lane_info;
1217                 radeon_connector->con_priv = radeon_dig_connector;
1218                 drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1219                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1220                 if (ret)
1221                         goto failed;
1222                 if (i2c_bus->valid) {
1223                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "LVDS");
1224                         if (!radeon_connector->ddc_bus)
1225                                 goto failed;
1226                 }
1227                 drm_connector_attach_property(&radeon_connector->base,
1228                                               dev->mode_config.scaling_mode_property,
1229                                               DRM_MODE_SCALE_FULLSCREEN);
1230                 subpixel_order = SubPixelHorizontalRGB;
1231                 break;
1232         }
1233
1234         connector->display_info.subpixel_order = subpixel_order;
1235         drm_sysfs_connector_add(connector);
1236         return;
1237
1238 failed:
1239         if (radeon_connector->ddc_bus)
1240                 radeon_i2c_destroy(radeon_connector->ddc_bus);
1241         drm_connector_cleanup(connector);
1242         kfree(connector);
1243 }
1244
1245 void
1246 radeon_add_legacy_connector(struct drm_device *dev,
1247                             uint32_t connector_id,
1248                             uint32_t supported_device,
1249                             int connector_type,
1250                             struct radeon_i2c_bus_rec *i2c_bus,
1251                             uint16_t connector_object_id,
1252                             struct radeon_hpd *hpd)
1253 {
1254         struct radeon_device *rdev = dev->dev_private;
1255         struct drm_connector *connector;
1256         struct radeon_connector *radeon_connector;
1257         uint32_t subpixel_order = SubPixelNone;
1258         int ret;
1259
1260         /* fixme - tv/cv/din */
1261         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
1262                 return;
1263
1264         /* see if we already added it */
1265         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1266                 radeon_connector = to_radeon_connector(connector);
1267                 if (radeon_connector->connector_id == connector_id) {
1268                         radeon_connector->devices |= supported_device;
1269                         return;
1270                 }
1271         }
1272
1273         radeon_connector = kzalloc(sizeof(struct radeon_connector), GFP_KERNEL);
1274         if (!radeon_connector)
1275                 return;
1276
1277         connector = &radeon_connector->base;
1278
1279         radeon_connector->connector_id = connector_id;
1280         radeon_connector->devices = supported_device;
1281         radeon_connector->connector_object_id = connector_object_id;
1282         radeon_connector->hpd = *hpd;
1283         switch (connector_type) {
1284         case DRM_MODE_CONNECTOR_VGA:
1285                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1286                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1287                 if (ret)
1288                         goto failed;
1289                 if (i2c_bus->valid) {
1290                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "VGA");
1291                         if (!radeon_connector->ddc_bus)
1292                                 goto failed;
1293                 }
1294                 radeon_connector->dac_load_detect = true;
1295                 drm_connector_attach_property(&radeon_connector->base,
1296                                               rdev->mode_info.load_detect_property,
1297                                               1);
1298                 break;
1299         case DRM_MODE_CONNECTOR_DVIA:
1300                 drm_connector_init(dev, &radeon_connector->base, &radeon_vga_connector_funcs, connector_type);
1301                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_vga_connector_helper_funcs);
1302                 if (ret)
1303                         goto failed;
1304                 if (i2c_bus->valid) {
1305                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1306                         if (!radeon_connector->ddc_bus)
1307                                 goto failed;
1308                 }
1309                 radeon_connector->dac_load_detect = true;
1310                 drm_connector_attach_property(&radeon_connector->base,
1311                                               rdev->mode_info.load_detect_property,
1312                                               1);
1313                 break;
1314         case DRM_MODE_CONNECTOR_DVII:
1315         case DRM_MODE_CONNECTOR_DVID:
1316                 drm_connector_init(dev, &radeon_connector->base, &radeon_dvi_connector_funcs, connector_type);
1317                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_dvi_connector_helper_funcs);
1318                 if (ret)
1319                         goto failed;
1320                 if (i2c_bus->valid) {
1321                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "DVI");
1322                         if (!radeon_connector->ddc_bus)
1323                                 goto failed;
1324                         radeon_connector->dac_load_detect = true;
1325                         drm_connector_attach_property(&radeon_connector->base,
1326                                                       rdev->mode_info.load_detect_property,
1327                                                       1);
1328                 }
1329                 subpixel_order = SubPixelHorizontalRGB;
1330                 break;
1331         case DRM_MODE_CONNECTOR_SVIDEO:
1332         case DRM_MODE_CONNECTOR_Composite:
1333         case DRM_MODE_CONNECTOR_9PinDIN:
1334                 if (radeon_tv == 1) {
1335                         drm_connector_init(dev, &radeon_connector->base, &radeon_tv_connector_funcs, connector_type);
1336                         ret = drm_connector_helper_add(&radeon_connector->base, &radeon_tv_connector_helper_funcs);
1337                         if (ret)
1338                                 goto failed;
1339                         radeon_connector->dac_load_detect = true;
1340                         /* RS400,RC410,RS480 chipset seems to report a lot
1341                          * of false positive on load detect, we haven't yet
1342                          * found a way to make load detect reliable on those
1343                          * chipset, thus just disable it for TV.
1344                          */
1345                         if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480)
1346                                 radeon_connector->dac_load_detect = false;
1347                         drm_connector_attach_property(&radeon_connector->base,
1348                                                       rdev->mode_info.load_detect_property,
1349                                                       radeon_connector->dac_load_detect);
1350                         drm_connector_attach_property(&radeon_connector->base,
1351                                                       rdev->mode_info.tv_std_property,
1352                                                       radeon_combios_get_tv_info(rdev));
1353                 }
1354                 break;
1355         case DRM_MODE_CONNECTOR_LVDS:
1356                 drm_connector_init(dev, &radeon_connector->base, &radeon_lvds_connector_funcs, connector_type);
1357                 ret = drm_connector_helper_add(&radeon_connector->base, &radeon_lvds_connector_helper_funcs);
1358                 if (ret)
1359                         goto failed;
1360                 if (i2c_bus->valid) {
1361                         radeon_connector->ddc_bus = radeon_i2c_create(dev, i2c_bus, "LVDS");
1362                         if (!radeon_connector->ddc_bus)
1363                                 goto failed;
1364                 }
1365                 drm_connector_attach_property(&radeon_connector->base,
1366                                               dev->mode_config.scaling_mode_property,
1367                                               DRM_MODE_SCALE_FULLSCREEN);
1368                 subpixel_order = SubPixelHorizontalRGB;
1369                 break;
1370         }
1371
1372         connector->display_info.subpixel_order = subpixel_order;
1373         drm_sysfs_connector_add(connector);
1374         return;
1375
1376 failed:
1377         if (radeon_connector->ddc_bus)
1378                 radeon_i2c_destroy(radeon_connector->ddc_bus);
1379         drm_connector_cleanup(connector);
1380         kfree(connector);
1381 }