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