summaryrefslogtreecommitdiff
path: root/linux-core/radeon_encoders.c
blob: 9a2d63ea2da7821efa571344854cce1783a69837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/*
 * Copyright 2007-8 Advanced Micro Devices, Inc.
 * Copyright 2008 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Dave Airlie
 *          Alex Deucher
 */
#include "drmP.h"
#include "drm_crtc_helper.h"
#include "radeon_drm.h"
#include "radeon_drv.h"

extern int atom_debug;

void radeon_rmx_mode_fixup(struct drm_encoder *encoder,
			   struct drm_display_mode *mode,
			   struct drm_display_mode *adjusted_mode)
{
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	if (mode->hdisplay < radeon_encoder->panel_xres ||
	    mode->vdisplay < radeon_encoder->panel_yres) {
		radeon_encoder->flags |= RADEON_USE_RMX;
		adjusted_mode->hdisplay = radeon_encoder->panel_xres;
		adjusted_mode->vdisplay = radeon_encoder->panel_yres;
		adjusted_mode->htotal = radeon_encoder->panel_xres + radeon_encoder->hblank;
		adjusted_mode->hsync_start = radeon_encoder->panel_xres + radeon_encoder->hoverplus;
		adjusted_mode->hsync_end = adjusted_mode->hsync_start + radeon_encoder->hsync_width;
		adjusted_mode->vtotal = radeon_encoder->panel_yres + radeon_encoder->vblank;
		adjusted_mode->vsync_start = radeon_encoder->panel_yres + radeon_encoder->voverplus;
		adjusted_mode->vsync_end = adjusted_mode->vsync_start + radeon_encoder->vsync_width;
		/* update crtc values */
		drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
		/* adjust crtc values */
		adjusted_mode->crtc_hdisplay = radeon_encoder->panel_xres;
		adjusted_mode->crtc_vdisplay = radeon_encoder->panel_yres;
		adjusted_mode->crtc_htotal = adjusted_mode->crtc_hdisplay + radeon_encoder->hblank;
		adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hdisplay + radeon_encoder->hoverplus;
		adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + radeon_encoder->hsync_width;
		adjusted_mode->crtc_vtotal = adjusted_mode->crtc_vdisplay + radeon_encoder->vblank;
		adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + radeon_encoder->voverplus;
		adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + radeon_encoder->vsync_width;
	} else {
		adjusted_mode->htotal = radeon_encoder->panel_xres + radeon_encoder->hblank;
		adjusted_mode->hsync_start = radeon_encoder->panel_xres + radeon_encoder->hoverplus;
		adjusted_mode->hsync_end = adjusted_mode->hsync_start + radeon_encoder->hsync_width;
		adjusted_mode->vtotal = radeon_encoder->panel_yres + radeon_encoder->vblank;
		adjusted_mode->vsync_start = radeon_encoder->panel_yres + radeon_encoder->voverplus;
		adjusted_mode->vsync_end = adjusted_mode->vsync_start + radeon_encoder->vsync_width;
		/* update crtc values */
		drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
		/* adjust crtc values */
		adjusted_mode->crtc_htotal = adjusted_mode->crtc_hdisplay + radeon_encoder->hblank;
		adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hdisplay + radeon_encoder->hoverplus;
		adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + radeon_encoder->hsync_width;
		adjusted_mode->crtc_vtotal = adjusted_mode->crtc_vdisplay + radeon_encoder->vblank;
		adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vdisplay + radeon_encoder->voverplus;
		adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + radeon_encoder->vsync_width;
	}
	adjusted_mode->clock = radeon_encoder->dotclock;
	adjusted_mode->flags = radeon_encoder->flags;
}


static int atom_dac_find_atom_type(struct radeon_encoder *radeon_encoder, struct drm_connector *connector)
{
	struct drm_device *dev = radeon_encoder->base.dev;
	struct drm_connector *connector_find;
	int atom_type = -1;

	if (!connector) {
		list_for_each_entry(connector_find, &dev->mode_config.connector_list, head) {
			if (connector_find->encoder == &radeon_encoder->base)
				connector = connector_find;
		}
	}
	if (connector) {
		/* look for the encoder in the connector list -
		   check if we the DAC is enabled on a VGA or STV/CTV or CV connector */
		/* work out the ATOM_DEVICE bits */
		switch (connector->connector_type) {
		case CONNECTOR_VGA:
		case CONNECTOR_DVI_I:
		case CONNECTOR_DVI_A:
			if (radeon_encoder->atom_device & ATOM_DEVICE_CRT1_SUPPORT)
				atom_type = ATOM_DEVICE_CRT1_INDEX;
			else if (radeon_encoder->atom_device & ATOM_DEVICE_CRT2_SUPPORT)
				atom_type = ATOM_DEVICE_CRT2_INDEX;
			break;
		case CONNECTOR_STV:
		case CONNECTOR_CTV:
			if (radeon_encoder->atom_device & ATOM_DEVICE_TV1_SUPPORT)
				atom_type = ATOM_DEVICE_TV1_INDEX;
			break;
		case CONNECTOR_DIN:
			if (radeon_encoder->atom_device & ATOM_DEVICE_TV1_SUPPORT)
				atom_type = ATOM_DEVICE_TV1_INDEX;
			if (radeon_encoder->atom_device & ATOM_DEVICE_CV_SUPPORT)
				atom_type = ATOM_DEVICE_CV_INDEX;
			break;
		}
	}

	return atom_type;
}

/* LVTMA encoder for LVDS usage */
static void atombios_display_device_control(struct drm_encoder *encoder, int index, uint8_t state)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	DISPLAY_DEVICE_OUTPUT_CONTROL_PS_ALLOCATION args;

	memset(&args, 0, sizeof(args));
	args.ucAction = state;

	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
}


static void atombios_scaler_setup(struct drm_encoder *encoder, struct drm_display_mode *mode)
{
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
	ENABLE_SCALER_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);

	memset(&args, 0, sizeof(args));
	args.ucScaler = radeon_crtc->crtc_id;

	if (radeon_encoder->flags & RADEON_USE_RMX) {
		if (radeon_encoder->rmx_type == RMX_FULL)
			args.ucEnable = ATOM_SCALER_EXPANSION;
		else if (radeon_encoder->rmx_type == RMX_CENTER)
			args.ucEnable = ATOM_SCALER_CENTER;
	} else
		args.ucEnable = ATOM_SCALER_DISABLE;

	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
}

void atombios_set_crtc_source(struct drm_encoder *encoder, int source)
{
	int index = GetIndexIntoMasterTable(COMMAND, SelectCRTC_Source);
	struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
	struct drm_radeon_private *dev_priv = encoder->dev->dev_private;
	uint8_t frev, crev;
	SELECT_CRTC_SOURCE_PS_ALLOCATION crtc_src_param;
	SELECT_CRTC_SOURCE_PARAMETERS_V2 crtc_src_param2;
	uint32_t *param = NULL;

	atom_parse_cmd_header(dev_priv->mode_info.atom_context, index, &frev, &crev);
	switch (frev) {
	case 1: {
		switch (crev) {
		case 0:
		case 1:
		default:
			memset(&crtc_src_param, 0, sizeof(crtc_src_param));
			crtc_src_param.ucCRTC = radeon_crtc->crtc_id;
			crtc_src_param.ucDevice = source;
			param = (uint32_t *)&crtc_src_param;
			break;
		case 2:
			memset(&crtc_src_param2, 0, sizeof(crtc_src_param2));
			crtc_src_param2.ucCRTC = radeon_crtc->crtc_id;
			crtc_src_param2.ucEncoderID = source;
			switch (source) {
			case ATOM_DEVICE_CRT1_INDEX:
			case ATOM_DEVICE_CRT2_INDEX:
				crtc_src_param2.ucEncodeMode = ATOM_ENCODER_MODE_CRT;
				break;
			case ATOM_DEVICE_DFP1_INDEX:
			case ATOM_DEVICE_DFP2_INDEX:
			case ATOM_DEVICE_DFP3_INDEX:
				crtc_src_param2.ucEncodeMode = ATOM_ENCODER_MODE_DVI;
				// TODO ENCODER MODE
				break;
			case ATOM_DEVICE_LCD1_INDEX:
				crtc_src_param2.ucEncodeMode = ATOM_ENCODER_MODE_LVDS;
				break;
			case ATOM_DEVICE_TV1_INDEX:
				crtc_src_param2.ucEncodeMode = ATOM_ENCODER_MODE_TV;
				break;
			case ATOM_DEVICE_CV_INDEX:
				crtc_src_param2.ucEncodeMode = ATOM_ENCODER_MODE_CV;
				break;
			}
			param = (uint32_t *)&crtc_src_param2;
			break;
		}
	}
		break;
	default:
		return;
	}
	
	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)param);

}

static void radeon_dfp_disable_dither(struct drm_encoder *encoder, int device)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;

	switch (device) {
	case ATOM_DEVICE_DFP1_INDEX:
		RADEON_WRITE(AVIVO_TMDSA_BIT_DEPTH_CONTROL, 0); /* TMDSA */
		break;
	case ATOM_DEVICE_DFP2_INDEX:
		if ((dev_priv->chip_family == CHIP_RS600) ||
		    (dev_priv->chip_family == CHIP_RS690) ||
		    (dev_priv->chip_family == CHIP_RS740))
			RADEON_WRITE(AVIVO_DDIA_BIT_DEPTH_CONTROL, 0); /* DDIA */
		else
			RADEON_WRITE(AVIVO_DVOA_BIT_DEPTH_CONTROL, 0); /* DVO */
		break;
		/*case ATOM_DEVICE_LCD1_INDEX:*/ /* LVDS panels need dither enabled */
	case ATOM_DEVICE_DFP3_INDEX:
		RADEON_WRITE(AVIVO_LVTMA_BIT_DEPTH_CONTROL, 0); /* LVTMA */
		break;
	default:
		break;
	}
}


static void radeon_lvtma_mode_set(struct drm_encoder *encoder,
				  struct drm_display_mode *mode,
				  struct drm_display_mode *adjusted_mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	LVDS_ENCODER_CONTROL_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, LVDSEncoderControl);

	memset(&args, 0, sizeof(args));
	atombios_scaler_setup(encoder, mode);
	atombios_set_crtc_source(encoder, ATOM_DEVICE_LCD1_INDEX);

	args.ucAction = 1;
	if (adjusted_mode->clock > 165000)
		args.ucMisc = 1;
	else
		args.ucMisc = 0;
	args.usPixelClock = cpu_to_le16(adjusted_mode->clock / 10);
	
	printk("executing set LVDS encoder\n");
	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
}


static void radeon_lvtma_dpms(struct drm_encoder *encoder, int mode)
{
	struct drm_device *dev = encoder->dev;
	int index = GetIndexIntoMasterTable(COMMAND, LCD1OutputControl);

	switch(mode) {
	case DRM_MODE_DPMS_ON:
		atombios_display_device_control(encoder, index, ATOM_ENABLE);
		break;
	case DRM_MODE_DPMS_STANDBY:	
	case DRM_MODE_DPMS_SUSPEND:
	case DRM_MODE_DPMS_OFF:
		atombios_display_device_control(encoder, index, ATOM_DISABLE);
		break;
	}
}

static bool radeon_lvtma_mode_fixup(struct drm_encoder *encoder,
				    struct drm_display_mode *mode,
				    struct drm_display_mode *adjusted_mode)
{
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);

	radeon_encoder->flags &= ~RADEON_USE_RMX;

	if (radeon_encoder->rmx_type != RMX_OFF)
		radeon_rmx_mode_fixup(encoder, mode, adjusted_mode);
	
	return true;
}

static void radeon_lvtma_prepare(struct drm_encoder *encoder)
{
	radeon_lvtma_dpms(encoder, DRM_MODE_DPMS_OFF);
}

static void radeon_lvtma_commit(struct drm_encoder *encoder)
{
	radeon_lvtma_dpms(encoder, DRM_MODE_DPMS_ON);
}

static const struct drm_encoder_helper_funcs radeon_atom_lvtma_helper_funcs = {
	.dpms = radeon_lvtma_dpms,
	.mode_fixup = radeon_lvtma_mode_fixup,
	.prepare = radeon_lvtma_prepare,
	.mode_set = radeon_lvtma_mode_set,
	.commit = radeon_lvtma_commit,
};

void radeon_enc_destroy(struct drm_encoder *encoder)
{
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	drm_encoder_cleanup(encoder);
	kfree(radeon_encoder);
}

static const struct drm_encoder_funcs radeon_atom_lvtma_enc_funcs = {
	.destroy = radeon_enc_destroy,
};

struct drm_encoder *radeon_encoder_lvtma_add(struct drm_device *dev, int bios_index)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_mode_info *mode_info = &dev_priv->mode_info;
	struct radeon_encoder *radeon_encoder;
	struct drm_encoder *encoder;
	radeon_encoder = kzalloc(sizeof(struct radeon_encoder), GFP_KERNEL);
	if (!radeon_encoder) {
		return NULL;
	}

	encoder = &radeon_encoder->base;

	encoder->possible_crtcs = 0x3;
	encoder->possible_clones = 0;
	drm_encoder_init(dev, encoder, &radeon_atom_lvtma_enc_funcs,
			 DRM_MODE_ENCODER_LVDS);

	drm_encoder_helper_add(encoder, &radeon_atom_lvtma_helper_funcs);
	radeon_encoder->atom_device = mode_info->bios_connector[bios_index].devices;

	/* TODO get the LVDS info from the BIOS for panel size etc. */
	/* get the lvds info from the bios */
	radeon_get_lvds_info(radeon_encoder);

	/* LVDS gets default RMX full scaling */
	radeon_encoder->rmx_type = RMX_FULL;

	return encoder;
}

static void radeon_atom_dac_dpms(struct drm_encoder *encoder, int mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	int atom_type = -1;
	int index;

	atom_type = atom_dac_find_atom_type(radeon_encoder, NULL);
	if (atom_type == -1)
		return;
	
	switch(atom_type) {
	case ATOM_DEVICE_CRT1_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, DAC1OutputControl);
		break;
	case ATOM_DEVICE_CRT2_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, DAC2OutputControl);
		break;
	case ATOM_DEVICE_TV1_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, TV1OutputControl);
		break;
	case ATOM_DEVICE_CV_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, CV1OutputControl);
		break;
	default:
		return;
	}

	switch(mode) {
	case DRM_MODE_DPMS_ON:
		atombios_display_device_control(encoder, index, ATOM_ENABLE);
		break;
	case DRM_MODE_DPMS_STANDBY:	
	case DRM_MODE_DPMS_SUSPEND:
	case DRM_MODE_DPMS_OFF:
		atombios_display_device_control(encoder, index, ATOM_DISABLE);
		break;
	}
}

static bool radeon_atom_dac_mode_fixup(struct drm_encoder *encoder,
				  struct drm_display_mode *mode,
				  struct drm_display_mode *adjusted_mode)
{
	return true;
}

static void radeon_atom_dac_prepare(struct drm_encoder *encoder)
{
	radeon_atom_dac_dpms(encoder, DRM_MODE_DPMS_OFF);
}

static void radeon_atom_dac_commit(struct drm_encoder *encoder)
{
	radeon_atom_dac_dpms(encoder, DRM_MODE_DPMS_ON);
}

static int atombios_dac_setup(struct drm_encoder *encoder,
			      struct drm_display_mode *mode,
			      int atom_type)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	DAC_ENCODER_CONTROL_PS_ALLOCATION args;
	int id = (radeon_encoder->type.dac == DAC_TVDAC);
	int index;

	memset(&args, 0, sizeof(args));
	if (id == 0)
		index = GetIndexIntoMasterTable(COMMAND, DAC1EncoderControl);
	else
		index = GetIndexIntoMasterTable(COMMAND, DAC2EncoderControl);

	args.ucAction = 1;
	args.usPixelClock = cpu_to_le16(mode->clock / 10);
	if ((atom_type == ATOM_DEVICE_CRT1_INDEX) ||
	    (atom_type == ATOM_DEVICE_CRT2_INDEX))
		args.ucDacStandard = id ? ATOM_DAC2_PS2 : ATOM_DAC1_PS2;
	else if (atom_type == ATOM_DEVICE_CV_INDEX)
		args.ucDacStandard = id ? ATOM_DAC2_CV : ATOM_DAC1_CV;
	else if (atom_type == ATOM_DEVICE_TV1_INDEX)
		args.ucDacStandard = id ? ATOM_DAC2_NTSC : ATOM_DAC1_NTSC;
	/* TODO PAL */
	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
	
	return 0;
}

static int atombios_tv1_setup(struct drm_encoder *encoder,
			      struct drm_display_mode *mode,
			      int atom_type)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	TV_ENCODER_CONTROL_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, TVEncoderControl);

	memset(&args, 0, sizeof(args));
	args.sTVEncoder.ucAction = 1;
	if (atom_type == ATOM_DEVICE_CV_INDEX)
		args.sTVEncoder.ucTvStandard = ATOM_TV_CV;
	else {
		// TODO PAL
		args.sTVEncoder.ucTvStandard = ATOM_TV_NTSC;
	}

	args.sTVEncoder.usPixelClock = cpu_to_le16(mode->clock / 10);
	
	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
	return 0;
}

static void radeon_atom_dac_mode_set(struct drm_encoder *encoder,
				     struct drm_display_mode *mode,
				     struct drm_display_mode *adjusted_mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	int atom_type = -1;

	atom_type = atom_dac_find_atom_type(radeon_encoder, NULL);
	if (atom_type == -1)
		return;

	atombios_scaler_setup(encoder, mode);
	atombios_set_crtc_source(encoder, atom_type);

	atombios_dac_setup(encoder, adjusted_mode, atom_type);
	if ((atom_type == ATOM_DEVICE_TV1_INDEX) ||
	    (atom_type == ATOM_DEVICE_CV_INDEX))
		atombios_tv1_setup(encoder, adjusted_mode, atom_type);
	
}

static bool atom_dac_load_detect(struct drm_encoder *encoder, int atom_devices)
{
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	DAC_LOAD_DETECTION_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, DAC_LoadDetection);

	memset(&args, 0, sizeof(args));
	args.sDacload.ucMisc = 0;
	args.sDacload.ucDacType = (radeon_encoder->type.dac == DAC_PRIMARY) ? ATOM_DAC_A : ATOM_DAC_B;

	if (atom_devices & ATOM_DEVICE_CRT1_SUPPORT)
		args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CRT1_SUPPORT);
	else if (atom_devices & ATOM_DEVICE_CRT2_SUPPORT)
		args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CRT2_SUPPORT);
	else if (atom_devices & ATOM_DEVICE_CV_SUPPORT) {
		args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_CV_SUPPORT);
		if (radeon_is_dce3(dev_priv))
			args.sDacload.ucMisc = 1;
	} else if (atom_devices & ATOM_DEVICE_TV1_SUPPORT) {
		args.sDacload.usDeviceID = cpu_to_le16(ATOM_DEVICE_TV1_SUPPORT);
		if (radeon_is_dce3(dev_priv))
			args.sDacload.ucMisc = 1;
	} else
		return false;
	
	DRM_DEBUG("writing %x %x\n", args.sDacload.usDeviceID, args.sDacload.ucDacType);
	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
	return true;
}       

static enum drm_connector_status radeon_atom_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	int atom_type = -1;
	uint32_t bios_0_scratch;

	atom_type = atom_dac_find_atom_type(radeon_encoder, connector);
	if (atom_type == -1) {
		DRM_DEBUG("exit after find \n");
		return connector_status_unknown;
	}

	if(!atom_dac_load_detect(encoder, (1 << atom_type))) {
		DRM_DEBUG("detect returned false \n");
		return connector_status_unknown;
	}


	if (dev_priv->chip_family >= CHIP_R600) 
		bios_0_scratch = RADEON_READ(R600_BIOS_0_SCRATCH);
	else
		bios_0_scratch = RADEON_READ(RADEON_BIOS_0_SCRATCH);

	DRM_DEBUG("Bios 0 scratch %x\n", bios_0_scratch);
	if (radeon_encoder->atom_device & ATOM_DEVICE_CRT1_SUPPORT) {
		if (bios_0_scratch & ATOM_S0_CRT1_MASK)
			return connector_status_connected;
	} else if (radeon_encoder->atom_device & ATOM_DEVICE_CRT2_SUPPORT) {
		if (bios_0_scratch & ATOM_S0_CRT2_MASK)
			return connector_status_connected;
	} else if (radeon_encoder->atom_device & ATOM_DEVICE_CV_SUPPORT) {
		if (bios_0_scratch & (ATOM_S0_CV_MASK|ATOM_S0_CV_MASK_A))
			return connector_status_connected;
	} else if (radeon_encoder->atom_device & ATOM_DEVICE_TV1_SUPPORT) {
		if (bios_0_scratch & (ATOM_S0_TV1_COMPOSITE | ATOM_S0_TV1_COMPOSITE_A))
			return connector_status_connected; // CTV
		else if (bios_0_scratch & (ATOM_S0_TV1_SVIDEO | ATOM_S0_TV1_SVIDEO_A))
			return connector_status_connected; // STV
	}
	return connector_status_disconnected;
}

static const struct drm_encoder_helper_funcs radeon_atom_dac_helper_funcs = {
	.dpms = radeon_atom_dac_dpms,
	.mode_fixup = radeon_atom_dac_mode_fixup,
	.prepare = radeon_atom_dac_prepare,
	.mode_set = radeon_atom_dac_mode_set,
	.commit = radeon_atom_dac_commit,
	.detect = radeon_atom_dac_detect,
};

static const struct drm_encoder_funcs radeon_atom_dac_enc_funcs = {
	. destroy = radeon_enc_destroy,
};


static void atombios_tmds1_setup(struct drm_encoder *encoder,
				 struct drm_display_mode *mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	TMDS1_ENCODER_CONTROL_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, TMDS1EncoderControl);

	memset(&args, 0, sizeof(args));
	args.ucAction = 1;
	if (mode->clock > 165000)
		args.ucMisc = 1;
	else
		args.ucMisc = 0;

	args.usPixelClock = cpu_to_le16(mode->clock / 10);

	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);	
}

static void atombios_tmds2_setup(struct drm_encoder *encoder,
				 struct drm_display_mode *mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	TMDS2_ENCODER_CONTROL_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, TMDS2EncoderControl);

	memset(&args, 0, sizeof(args));
	args.ucAction = 1;
	if (mode->clock > 165000)
		args.ucMisc = 1;
	else
		args.ucMisc = 0;

	args.usPixelClock = cpu_to_le16(mode->clock / 10);

	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);	
}


static void atombios_ext_tmds_setup(struct drm_encoder *encoder,
				    struct drm_display_mode *mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	ENABLE_EXTERNAL_TMDS_ENCODER_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, DVOEncoderControl);

	memset(&args, 0, sizeof(args));
	args.sXTmdsEncoder.ucEnable = 1;

	if (mode->clock > 165000)
		args.sXTmdsEncoder.ucMisc = 1;
	else
		args.sXTmdsEncoder.ucMisc = 0;

	// TODO 6-bit DAC
//	args.usPixelClock = cpu_to_le16(mode->clock / 10);

	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);	
}

static void atombios_dig1_setup(struct drm_encoder *encoder,
				struct drm_display_mode *mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	DIG_ENCODER_CONTROL_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, DIG1EncoderControl);

	args.ucAction = 1;
	args.usPixelClock = mode->clock / 10;
	args.ucConfig = ATOM_ENCODER_CONFIG_TRANSMITTER1;

	// TODO coherent mode
//	if (encoder->coherent_mode)
//		args.ucConfig |= ATOM_TRANSMITTER_CONFIG_COHERENT;

	if (mode->clock > 165000) {
		args.ucConfig |= ATOM_ENCODER_CONFIG_LINKA_B;
		args.ucLaneNum = 8;
	} else {
		args.ucConfig |= ATOM_ENCODER_CONFIG_LINKA;
		args.ucLaneNum = 4;
	}

	// TODO Encoder MODE
	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);	
}

static void atombios_ddia_setup(struct drm_encoder *encoder,
				struct drm_display_mode *mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	DVO_ENCODER_CONTROL_PS_ALLOCATION args;
	int index = GetIndexIntoMasterTable(COMMAND, DVOEncoderControl);

	args.sDVOEncoder.ucAction = ATOM_ENABLE;
	args.sDVOEncoder.usPixelClock = mode->clock / 10;

	if (mode->clock > 165000)
		args.sDVOEncoder.usDevAttr.sDigAttrib.ucAttribute = PANEL_ENCODER_MISC_DUAL;
	else
		args.sDVOEncoder.usDevAttr.sDigAttrib.ucAttribute = 0;

	atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);	
}

struct drm_encoder *radeon_encoder_atom_dac_add(struct drm_device *dev, int bios_index, int dac_type, int with_tv)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_mode_info *mode_info = &dev_priv->mode_info;
	struct radeon_encoder *radeon_encoder = NULL;
	struct drm_encoder *encoder;
	int type = with_tv ? DRM_MODE_ENCODER_TVDAC : DRM_MODE_ENCODER_DAC;
	int found = 0;
	int digital_enc_mask = ~(ATOM_DEVICE_DFP1_SUPPORT | ATOM_DEVICE_DFP2_SUPPORT | ATOM_DEVICE_DFP3_SUPPORT |
				ATOM_DEVICE_LCD1_SUPPORT);
	/* we may already have added this encoder */
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		if (encoder->encoder_type != DRM_MODE_ENCODER_DAC ||
		    encoder->encoder_type != DRM_MODE_ENCODER_TVDAC)
			continue;

		radeon_encoder = to_radeon_encoder(encoder);
		if (radeon_encoder->type.dac == dac_type) {
			found = 1;
			break;
		}
	}

	if (found) {
		/* upgrade to a TV controlling DAC */
		if (type == DRM_MODE_ENCODER_TVDAC)
			encoder->encoder_type = type;
		radeon_encoder->atom_device |= mode_info->bios_connector[bios_index].devices;
		radeon_encoder->atom_device &= digital_enc_mask;
		return encoder;
	}

	radeon_encoder = kzalloc(sizeof(struct radeon_encoder), GFP_KERNEL);
	if (!radeon_encoder) {
		return NULL;
	}

	encoder = &radeon_encoder->base;

	encoder->possible_crtcs = 0x3;
	encoder->possible_clones = 0;
	drm_encoder_init(dev, encoder, &radeon_atom_dac_enc_funcs,
			 type);

	drm_encoder_helper_add(encoder, &radeon_atom_dac_helper_funcs);
	radeon_encoder->type.dac = dac_type;
	radeon_encoder->atom_device = mode_info->bios_connector[bios_index].devices;

	/* mask off any digital encoders */
	radeon_encoder->atom_device &= digital_enc_mask;
	return encoder;
}

static void radeon_atom_tmds_dpms(struct drm_encoder *encoder, int mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	int atom_type = -1;
	int index = -1;

	if (radeon_encoder->atom_device & ATOM_DEVICE_DFP1_SUPPORT)
		atom_type = ATOM_DEVICE_DFP1_INDEX;
	if (radeon_encoder->atom_device & ATOM_DEVICE_DFP2_SUPPORT)
		atom_type = ATOM_DEVICE_DFP2_INDEX;
	if (radeon_encoder->atom_device & ATOM_DEVICE_DFP3_SUPPORT)
		atom_type = ATOM_DEVICE_DFP3_INDEX;

	if (atom_type == -1)
		return;

	switch(atom_type) {
	case ATOM_DEVICE_DFP1_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, TMDSAOutputControl);
		break;
	case ATOM_DEVICE_DFP2_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, DVOOutputControl);
		break;
	case ATOM_DEVICE_DFP3_INDEX:
		index = GetIndexIntoMasterTable(COMMAND, LVTMAOutputControl);
		break;
	}

	if (index == -1)
		return;

	switch(mode) {
	case DRM_MODE_DPMS_ON:
		atombios_display_device_control(encoder, index, ATOM_ENABLE);
		break;
	case DRM_MODE_DPMS_STANDBY:	
	case DRM_MODE_DPMS_SUSPEND:
	case DRM_MODE_DPMS_OFF:
		atombios_display_device_control(encoder, index, ATOM_DISABLE);
		break;
	}
}

static bool radeon_atom_tmds_mode_fixup(struct drm_encoder *encoder,
				  struct drm_display_mode *mode,
				  struct drm_display_mode *adjusted_mode)
{
	return true;
}

static void radeon_atom_tmds_mode_set(struct drm_encoder *encoder,
				      struct drm_display_mode *mode,
				      struct drm_display_mode *adjusted_mode)
{
	struct drm_device *dev = encoder->dev;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
	int atom_type;

	if (radeon_encoder->atom_device & ATOM_DEVICE_DFP1_SUPPORT)
		atom_type = ATOM_DEVICE_DFP1_INDEX;
	if (radeon_encoder->atom_device & ATOM_DEVICE_DFP2_SUPPORT)
		atom_type = ATOM_DEVICE_DFP2_INDEX;
	if (radeon_encoder->atom_device & ATOM_DEVICE_DFP3_SUPPORT)
		atom_type = ATOM_DEVICE_DFP3_INDEX;

	atombios_scaler_setup(encoder, mode);
	atombios_set_crtc_source(encoder, atom_type);

	if (atom_type == ATOM_DEVICE_DFP1_INDEX)
		atombios_tmds1_setup(encoder, adjusted_mode);
	if (atom_type == ATOM_DEVICE_DFP2_INDEX) {
		if ((dev_priv->chip_family == CHIP_RS600) ||
		    (dev_priv->chip_family == CHIP_RS690) ||
		    (dev_priv->chip_family == CHIP_RS740))
			atombios_ddia_setup(encoder, adjusted_mode);
		else
			atombios_ext_tmds_setup(encoder, adjusted_mode);
	}
	if (atom_type == ATOM_DEVICE_DFP3_INDEX)
		atombios_tmds2_setup(encoder, adjusted_mode);
	radeon_dfp_disable_dither(encoder, atom_type);


}

static void radeon_atom_tmds_prepare(struct drm_encoder *encoder)
{
	radeon_atom_tmds_dpms(encoder, DRM_MODE_DPMS_OFF);
}

static void radeon_atom_tmds_commit(struct drm_encoder *encoder)
{
	radeon_atom_tmds_dpms(encoder, DRM_MODE_DPMS_ON);
}

static const struct drm_encoder_helper_funcs radeon_atom_tmds_helper_funcs = {
	.dpms = radeon_atom_tmds_dpms,
	.mode_fixup = radeon_atom_tmds_mode_fixup,
	.prepare = radeon_atom_tmds_prepare,
	.mode_set = radeon_atom_tmds_mode_set,
	.commit = radeon_atom_tmds_commit,
	/* no detect for TMDS */
};

static const struct drm_encoder_funcs radeon_atom_tmds_enc_funcs = {
	. destroy = radeon_enc_destroy,
};

struct drm_encoder *radeon_encoder_atom_tmds_add(struct drm_device *dev, int bios_index, int tmds_type)
{
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct radeon_mode_info *mode_info = &dev_priv->mode_info;
	struct radeon_encoder *radeon_encoder = NULL;
	struct drm_encoder *encoder;
	int analog_enc_mask = ~(ATOM_DEVICE_CRT1_SUPPORT | ATOM_DEVICE_CRT2_SUPPORT);

		radeon_encoder = kzalloc(sizeof(struct radeon_encoder), GFP_KERNEL);
	if (!radeon_encoder) {
		return NULL;
	}

	encoder = &radeon_encoder->base;

	encoder->possible_crtcs = 0x3;
	encoder->possible_clones = 0;
	drm_encoder_init(dev, encoder, &radeon_atom_tmds_enc_funcs,
			 DRM_MODE_ENCODER_TMDS);

	drm_encoder_helper_add(encoder, &radeon_atom_tmds_helper_funcs);

	radeon_encoder->atom_device = mode_info->bios_connector[bios_index].devices;

	/* mask off any analog encoders */
	radeon_encoder->atom_device &= analog_enc_mask;
	return encoder;
}