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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
|
/* $NetBSD: tegra_drm_mode.c,v 1.21 2021/12/19 12:44:14 riastradh Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tegra_drm_mode.c,v 1.21 2021/12/19 12:44:14 riastradh Exp $");
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_drv.h>
#include <drm/drm_edid.h>
#include <drm/drm_plane_helper.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_vblank.h>
#include <dev/i2c/ddcvar.h>
#include <arm/nvidia/tegra_reg.h>
#include <arm/nvidia/tegra_var.h>
#include <arm/nvidia/tegra_intr.h>
#include <arm/nvidia/tegra_pmcreg.h>
#include <arm/nvidia/tegra_dcreg.h>
#include <arm/nvidia/tegra_hdmireg.h>
#include <arm/nvidia/tegra_drm.h>
#include <dev/fdt/fdtvar.h>
#include <external/bsd/drm2/dist/drm/drm_internal.h>
static struct drm_framebuffer *tegra_fb_create(struct drm_device *,
struct drm_file *, const struct drm_mode_fb_cmd2 *);
static const struct drm_mode_config_funcs tegra_mode_config_funcs = {
.fb_create = tegra_fb_create
};
static int tegra_framebuffer_create_handle(struct drm_framebuffer *,
struct drm_file *, unsigned int *);
static void tegra_framebuffer_destroy(struct drm_framebuffer *);
static const struct drm_framebuffer_funcs tegra_framebuffer_funcs = {
.create_handle = tegra_framebuffer_create_handle,
.destroy = tegra_framebuffer_destroy
};
static int tegra_crtc_init(struct drm_device *, int);
static void tegra_crtc_destroy(struct drm_crtc *);
static int tegra_crtc_cursor_set(struct drm_crtc *, struct drm_file *,
uint32_t, uint32_t, uint32_t);
static int tegra_crtc_cursor_move(struct drm_crtc *, int, int);
static int tegra_crtc_intr(void *);
static const struct drm_crtc_funcs tegra_crtc_funcs = {
.cursor_set = tegra_crtc_cursor_set,
.cursor_move = tegra_crtc_cursor_move,
.set_config = drm_crtc_helper_set_config,
.destroy = tegra_crtc_destroy
};
static void tegra_crtc_dpms(struct drm_crtc *, int);
static bool tegra_crtc_mode_fixup(struct drm_crtc *,
const struct drm_display_mode *,
struct drm_display_mode *);
static int tegra_crtc_mode_set(struct drm_crtc *,
struct drm_display_mode *, struct drm_display_mode *,
int, int, struct drm_framebuffer *);
static int tegra_crtc_mode_set_base(struct drm_crtc *,
int, int, struct drm_framebuffer *);
static int tegra_crtc_mode_set_base_atomic(struct drm_crtc *,
struct drm_framebuffer *, int, int, enum mode_set_atomic);
static void tegra_crtc_disable(struct drm_crtc *);
static void tegra_crtc_prepare(struct drm_crtc *);
static void tegra_crtc_commit(struct drm_crtc *);
static int tegra_crtc_do_set_base(struct drm_crtc *,
struct drm_framebuffer *, int, int, int);
static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
.dpms = tegra_crtc_dpms,
.mode_fixup = tegra_crtc_mode_fixup,
.mode_set = tegra_crtc_mode_set,
.mode_set_base = tegra_crtc_mode_set_base,
.mode_set_base_atomic = tegra_crtc_mode_set_base_atomic,
.disable = tegra_crtc_disable,
.prepare = tegra_crtc_prepare,
.commit = tegra_crtc_commit
};
static int tegra_encoder_init(struct drm_device *);
static void tegra_encoder_destroy(struct drm_encoder *);
static const struct drm_encoder_funcs tegra_encoder_funcs = {
.destroy = tegra_encoder_destroy
};
static void tegra_encoder_dpms(struct drm_encoder *, int);
static bool tegra_encoder_mode_fixup(struct drm_encoder *,
const struct drm_display_mode *, struct drm_display_mode *);
static void tegra_encoder_mode_set(struct drm_encoder *,
struct drm_display_mode *, struct drm_display_mode *);
static void tegra_encoder_prepare(struct drm_encoder *);
static void tegra_encoder_commit(struct drm_encoder *);
static const struct drm_encoder_helper_funcs tegra_encoder_helper_funcs = {
.dpms = tegra_encoder_dpms,
.mode_fixup = tegra_encoder_mode_fixup,
.prepare = tegra_encoder_prepare,
.commit = tegra_encoder_commit,
.mode_set = tegra_encoder_mode_set
};
static int tegra_connector_init(struct drm_device *, struct drm_encoder *);
static void tegra_connector_destroy(struct drm_connector *);
static enum drm_connector_status tegra_connector_detect(struct drm_connector *,
bool);
static const struct drm_connector_funcs tegra_connector_funcs = {
.dpms = drm_helper_connector_dpms,
.detect = tegra_connector_detect,
.fill_modes = drm_helper_probe_single_connector_modes,
.destroy = tegra_connector_destroy
};
static int tegra_connector_mode_valid(struct drm_connector *,
struct drm_display_mode *);
static int tegra_connector_get_modes(struct drm_connector *);
static const struct drm_connector_helper_funcs tegra_connector_helper_funcs = {
.mode_valid = tegra_connector_mode_valid,
.get_modes = tegra_connector_get_modes,
};
static const struct tegra_hdmi_tmds_config {
u_int dot_clock;
uint32_t sor_pll0;
uint32_t sor_pll1;
uint32_t sor_lane_drive_current;
uint32_t pe_current;
uint32_t sor_io_peak_current;
uint32_t sor_pad_ctls0;
uint32_t car_plld_misc; /* XXX unused? */
} tegra_hdmi_tmds_config[] = {
/* 480p */
{ 27000, 0x01003010, 0x00301b00, 0x1f1f1f1f,
0x00000000, 0x03030303, 0x800034bb, 0x40400820 },
/* 720p / 1080i */
{ 74250, 0x01003110, 0x00301500, 0x2c2c2c2c,
0x00000000, 0x07070707, 0x800034bb, 0x40400820 },
/* 1080p */
{ 148500, 0x01003310, 0x00301500, 0x2d2d2d2d,
0x00000000, 0x05050505, 0x800034bb, 0x40400820 },
/* 2160p */
{ 297000, 0x01003f10, 0x00300f00, 0x37373737,
0x00000000, 0x17171717, 0x800036bb, 0x40400f20 },
};
int
tegra_drm_mode_init(struct drm_device *ddev)
{
int error;
drm_mode_config_init(ddev);
ddev->mode_config.min_width = 0;
ddev->mode_config.min_height = 0;
ddev->mode_config.max_width = 4096;
ddev->mode_config.max_height = 2160;
ddev->mode_config.funcs = &tegra_mode_config_funcs;
error = tegra_crtc_init(ddev, 0);
if (error)
return error;
error = tegra_crtc_init(ddev, 1);
if (error)
return error;
error = tegra_encoder_init(ddev);
if (error)
return error;
error = drm_vblank_init(ddev, 2);
if (error)
return error;
return 0;
}
int
tegra_drm_framebuffer_init(struct drm_device *ddev,
struct tegra_framebuffer *fb)
{
return drm_framebuffer_init(ddev, &fb->base, &tegra_framebuffer_funcs);
}
static struct drm_framebuffer *
tegra_fb_create(struct drm_device *ddev, struct drm_file *file,
const struct drm_mode_fb_cmd2 *cmd)
{
struct tegra_framebuffer *fb;
struct drm_gem_object *gem_obj;
int error;
if (cmd->flags)
return NULL;
if (cmd->pixel_format != DRM_FORMAT_ARGB8888 &&
cmd->pixel_format != DRM_FORMAT_XRGB8888) {
return NULL;
}
gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
if (gem_obj == NULL)
return NULL;
fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
fb->obj = to_drm_gem_cma_obj(gem_obj);
error = tegra_drm_framebuffer_init(ddev, fb);
if (error)
goto dealloc;
return &fb->base;
drm_framebuffer_cleanup(&fb->base);
dealloc:
kmem_free(fb, sizeof(*fb));
drm_gem_object_put_unlocked(gem_obj);
return NULL;
}
static int
tegra_framebuffer_create_handle(struct drm_framebuffer *fb,
struct drm_file *file, unsigned int *handle)
{
struct tegra_framebuffer *tegra_fb = to_tegra_framebuffer(fb);
return drm_gem_handle_create(file, &tegra_fb->obj->base, handle);
}
static void
tegra_framebuffer_destroy(struct drm_framebuffer *fb)
{
struct tegra_framebuffer *tegra_fb = to_tegra_framebuffer(fb);
drm_framebuffer_cleanup(fb);
drm_gem_object_put_unlocked(&tegra_fb->obj->base);
kmem_free(tegra_fb, sizeof(*tegra_fb));
}
static int
tegra_crtc_init(struct drm_device *ddev, int index)
{
struct tegra_drm_softc * const sc = tegra_drm_private(ddev);
struct tegra_crtc *crtc;
bus_addr_t offset;
bus_size_t size;
u_int intr;
int error;
if (sc->sc_clk_dc[index] == NULL ||
sc->sc_clk_hdmi_parent == NULL ||
sc->sc_rst_dc[index] == NULL) {
DRM_ERROR("no clocks configured for crtc %d\n", index);
return -EIO;
}
switch (index) {
case 0:
offset = TEGRA_GHOST_BASE + TEGRA_DISPLAYA_OFFSET;
size = TEGRA_DISPLAYA_SIZE;
intr = TEGRA_INTR_DISPLAYA;
break;
case 1:
offset = TEGRA_GHOST_BASE + TEGRA_DISPLAYB_OFFSET;
size = TEGRA_DISPLAYB_SIZE;
intr = TEGRA_INTR_DISPLAYB;
break;
default:
return -EINVAL;
}
crtc = kmem_zalloc(sizeof(*crtc), KM_SLEEP);
crtc->index = index;
crtc->bst = sc->sc_bst;
error = bus_space_map(crtc->bst, offset, size, 0, &crtc->bsh);
if (error) {
kmem_free(crtc, sizeof(*crtc));
return -error;
}
crtc->size = size;
crtc->intr = intr;
crtc->ih = intr_establish_xname(intr, IPL_VM, IST_LEVEL | IST_MPSAFE,
tegra_crtc_intr, crtc, device_xname(sc->sc_dev)); /* XXX */
if (crtc->ih == NULL) {
DRM_ERROR("failed to establish interrupt for crtc %d\n", index);
}
const size_t cursor_size = 256 * 256 * 4;
crtc->cursor_obj = drm_gem_cma_create(ddev, cursor_size);
if (crtc->cursor_obj == NULL) {
kmem_free(crtc, sizeof(*crtc));
return -ENOMEM;
}
/* Enter reset */
fdtbus_reset_assert(sc->sc_rst_dc[index]);
/* Turn on power to display partition */
const u_int pmc_partid = index == 0 ? PMC_PARTID_DIS : PMC_PARTID_DISB;
tegra_pmc_power(pmc_partid, true);
tegra_pmc_remove_clamping(pmc_partid);
/* Set parent clock to the HDMI parent (ignoring DC parent in DT!) */
error = clk_set_parent(sc->sc_clk_dc[index],
sc->sc_clk_hdmi_parent);
if (error) {
DRM_ERROR("failed to set crtc %d clock parent: %d\n",
index, error);
return -error;
}
/* Enable DC clock */
error = clk_enable(sc->sc_clk_dc[index]);
if (error) {
DRM_ERROR("failed to enable crtc %d clock: %d\n", index, error);
return -error;
}
/* Leave reset */
fdtbus_reset_deassert(sc->sc_rst_dc[index]);
crtc->clk_parent = sc->sc_clk_hdmi_parent;
DC_WRITE(crtc, DC_CMD_INT_ENABLE_REG, DC_CMD_INT_V_BLANK);
drm_crtc_init(ddev, &crtc->base, &tegra_crtc_funcs);
drm_crtc_helper_add(&crtc->base, &tegra_crtc_helper_funcs);
return 0;
}
static int
tegra_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
uint32_t handle, uint32_t width, uint32_t height)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
struct drm_gem_object *gem_obj = NULL;
struct drm_gem_cma_object *obj;
uint32_t cfg, opt;
int error;
if (tegra_crtc->enabled == false)
return 0;
if (handle == 0) {
/* hide cursor */
opt = DC_READ(tegra_crtc, DC_DISP_DISP_WIN_OPTIONS_REG);
if ((opt & DC_DISP_DISP_WIN_OPTIONS_CURSOR_ENABLE) != 0) {
opt &= ~DC_DISP_DISP_WIN_OPTIONS_CURSOR_ENABLE;
DC_WRITE(tegra_crtc, DC_DISP_DISP_WIN_OPTIONS_REG, opt);
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_ACT_REQ);
}
error = 0;
goto done;
}
if ((width != height) ||
(width != 32 && width != 64 && width != 128 && width != 256)) {
DRM_ERROR("Cursor dimension %ux%u not supported\n",
width, height);
error = -EINVAL;
goto done;
}
gem_obj = drm_gem_object_lookup(file_priv, handle);
if (gem_obj == NULL) {
DRM_ERROR("Cannot find cursor object %#x for crtc %d\n",
handle, tegra_crtc->index);
error = -ENOENT;
goto done;
}
obj = to_drm_gem_cma_obj(gem_obj);
if (obj->base.size < width * height * 4) {
DRM_ERROR("Cursor buffer is too small\n");
error = -ENOMEM;
goto done;
}
cfg = __SHIFTIN(DC_DISP_CURSOR_START_ADDR_CLIPPING_DISPLAY,
DC_DISP_CURSOR_START_ADDR_CLIPPING);
switch (width) {
case 32:
cfg |= __SHIFTIN(DC_DISP_CURSOR_START_ADDR_SIZE_32,
DC_DISP_CURSOR_START_ADDR_SIZE);
break;
case 64:
cfg |= __SHIFTIN(DC_DISP_CURSOR_START_ADDR_SIZE_64,
DC_DISP_CURSOR_START_ADDR_SIZE);
break;
case 128:
cfg |= __SHIFTIN(DC_DISP_CURSOR_START_ADDR_SIZE_128,
DC_DISP_CURSOR_START_ADDR_SIZE);
break;
case 256:
cfg |= __SHIFTIN(DC_DISP_CURSOR_START_ADDR_SIZE_256,
DC_DISP_CURSOR_START_ADDR_SIZE);
break;
}
/* copy cursor (argb -> rgba) */
struct drm_gem_cma_object *cursor_obj = tegra_crtc->cursor_obj;
uint32_t off, *cp = obj->vaddr, *crtc_cp = cursor_obj->vaddr;
for (off = 0; off < width * height; off++) {
crtc_cp[off] = (cp[off] << 8) | (cp[off] >> 24);
}
const uint64_t paddr = cursor_obj->dmasegs[0].ds_addr;
DC_WRITE(tegra_crtc, DC_DISP_CURSOR_START_ADDR_HI_REG,
(paddr >> 32) & 3);
cfg |= __SHIFTIN((paddr >> 10) & 0x3fffff,
DC_DISP_CURSOR_START_ADDR_ADDRESS_LO);
const uint32_t ocfg =
DC_READ(tegra_crtc, DC_DISP_CURSOR_START_ADDR_REG);
if (cfg != ocfg) {
DC_WRITE(tegra_crtc, DC_DISP_CURSOR_START_ADDR_REG, cfg);
}
cfg = DC_READ(tegra_crtc, DC_DISP_BLEND_CURSOR_CONTROL_REG);
cfg &= ~DC_DISP_BLEND_CURSOR_CONTROL_DST_BLEND_FACTOR_SEL;
cfg |= __SHIFTIN(2, DC_DISP_BLEND_CURSOR_CONTROL_DST_BLEND_FACTOR_SEL);
cfg &= ~DC_DISP_BLEND_CURSOR_CONTROL_SRC_BLEND_FACTOR_SEL;
cfg |= __SHIFTIN(1, DC_DISP_BLEND_CURSOR_CONTROL_SRC_BLEND_FACTOR_SEL);
cfg &= ~DC_DISP_BLEND_CURSOR_CONTROL_ALPHA;
cfg |= __SHIFTIN(255, DC_DISP_BLEND_CURSOR_CONTROL_ALPHA);
cfg |= DC_DISP_BLEND_CURSOR_CONTROL_MODE_SEL;
DC_WRITE(tegra_crtc, DC_DISP_BLEND_CURSOR_CONTROL_REG, cfg);
/* set cursor position */
DC_WRITE(tegra_crtc, DC_DISP_CURSOR_POSITION_REG,
__SHIFTIN(tegra_crtc->cursor_x, DC_DISP_CURSOR_POSITION_H) |
__SHIFTIN(tegra_crtc->cursor_y, DC_DISP_CURSOR_POSITION_V));
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_CURSOR_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_CURSOR_ACT_REQ);
/* show cursor */
opt = DC_READ(tegra_crtc, DC_DISP_DISP_WIN_OPTIONS_REG);
if ((opt & DC_DISP_DISP_WIN_OPTIONS_CURSOR_ENABLE) == 0) {
opt |= DC_DISP_DISP_WIN_OPTIONS_CURSOR_ENABLE;
DC_WRITE(tegra_crtc, DC_DISP_DISP_WIN_OPTIONS_REG, opt);
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_ACT_REQ);
}
error = 0;
done:
if (error == 0) {
/* Wait for activation request to complete */
while (DC_READ(tegra_crtc, DC_CMD_STATE_CONTROL_REG) &
DC_CMD_STATE_CONTROL_GENERAL_ACT_REQ)
;
}
if (gem_obj) {
drm_gem_object_put_unlocked(gem_obj);
}
return error;
}
static int
tegra_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
tegra_crtc->cursor_x = x & 0x3fff;
tegra_crtc->cursor_y = y & 0x3fff;
DC_WRITE(tegra_crtc, DC_DISP_CURSOR_POSITION_REG,
__SHIFTIN(x & 0x3fff, DC_DISP_CURSOR_POSITION_H) |
__SHIFTIN(y & 0x3fff, DC_DISP_CURSOR_POSITION_V));
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_CURSOR_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_CURSOR_ACT_REQ);
return 0;
}
static void
tegra_crtc_destroy(struct drm_crtc *crtc)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
drm_crtc_cleanup(crtc);
if (tegra_crtc->ih) {
intr_disestablish(tegra_crtc->ih);
}
drm_gem_cma_free_object(&tegra_crtc->cursor_obj->base);
bus_space_unmap(tegra_crtc->bst, tegra_crtc->bsh, tegra_crtc->size);
kmem_free(tegra_crtc, sizeof(*tegra_crtc));
}
static void
tegra_crtc_dpms(struct drm_crtc *crtc, int mode)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
switch (mode) {
case DRM_MODE_DPMS_ON:
case DRM_MODE_DPMS_STANDBY:
case DRM_MODE_DPMS_SUSPEND:
DC_SET_CLEAR(tegra_crtc, DC_WINC_A_WIN_OPTIONS_REG,
DC_WINC_A_WIN_OPTIONS_WIN_ENABLE, 0);
break;
case DRM_MODE_DPMS_OFF:
DC_SET_CLEAR(tegra_crtc, DC_WINC_A_WIN_OPTIONS_REG,
0, DC_WINC_A_WIN_OPTIONS_WIN_ENABLE);
break;
}
}
static bool
tegra_crtc_mode_fixup(struct drm_crtc *crtc,
const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
{
return true;
}
static int
tegra_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
struct drm_display_mode *adjusted_mode, int x, int y,
struct drm_framebuffer *old_fb)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
const u_int hspw = mode->crtc_hsync_end - mode->crtc_hsync_start;
const u_int hbp = mode->crtc_htotal - mode->crtc_hsync_end;
const u_int hfp = mode->crtc_hsync_start - mode->crtc_hdisplay;
const u_int vspw = mode->crtc_vsync_end - mode->crtc_vsync_start;
const u_int vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
const u_int vfp = mode->crtc_vsync_start - mode->crtc_vdisplay;
/* Set colour depth to ARGB8888 */
DC_WRITE(tegra_crtc, DC_WINC_A_COLOR_DEPTH_REG,
__SHIFTIN(DC_WINC_A_COLOR_DEPTH_DEPTH_T_A8R8G8B8,
DC_WINC_A_COLOR_DEPTH_DEPTH));
/* Disable byte swapping */
DC_WRITE(tegra_crtc, DC_WINC_A_BYTE_SWAP_REG,
__SHIFTIN(DC_WINC_A_BYTE_SWAP_SWAP_NOSWAP,
DC_WINC_A_BYTE_SWAP_SWAP));
/* Initial DDA */
DC_WRITE(tegra_crtc, DC_WINC_A_H_INITIAL_DDA_REG, 0);
DC_WRITE(tegra_crtc, DC_WINC_A_V_INITIAL_DDA_REG, 0);
DC_WRITE(tegra_crtc, DC_WINC_A_DDA_INCREMENT_REG, 0x10001000);
/* Window position, size, stride */
DC_WRITE(tegra_crtc, DC_WINC_A_POSITION_REG,
__SHIFTIN(0, DC_WINC_A_POSITION_V) |
__SHIFTIN(0, DC_WINC_A_POSITION_H));
DC_WRITE(tegra_crtc, DC_WINC_A_SIZE_REG,
__SHIFTIN(mode->crtc_vdisplay, DC_WINC_A_SIZE_V) |
__SHIFTIN(mode->crtc_hdisplay, DC_WINC_A_SIZE_H));
DC_WRITE(tegra_crtc, DC_WINC_A_PRESCALED_SIZE_REG,
__SHIFTIN(mode->crtc_vdisplay, DC_WINC_A_PRESCALED_SIZE_V) |
__SHIFTIN(mode->crtc_hdisplay * (TEGRA_DC_DEPTH / 8),
DC_WINC_A_PRESCALED_SIZE_H));
DC_WRITE(tegra_crtc, DC_WINC_A_LINE_STRIDE_REG,
__SHIFTIN(mode->crtc_hdisplay * (TEGRA_DC_DEPTH / 8),
DC_WINC_A_LINE_STRIDE_LINE_STRIDE));
tegra_crtc_do_set_base(crtc, old_fb, x, y, 0);
/* Enable window A */
DC_WRITE(tegra_crtc, DC_WINC_A_WIN_OPTIONS_REG,
DC_WINC_A_WIN_OPTIONS_WIN_ENABLE);
/* Timing and signal options */
DC_WRITE(tegra_crtc, DC_DISP_DISP_TIMING_OPTIONS_REG,
__SHIFTIN(1, DC_DISP_DISP_TIMING_OPTIONS_VSYNC_POS));
DC_WRITE(tegra_crtc, DC_DISP_DISP_COLOR_CONTROL_REG,
__SHIFTIN(DC_DISP_DISP_COLOR_CONTROL_BASE_COLOR_SIZE_888,
DC_DISP_DISP_COLOR_CONTROL_BASE_COLOR_SIZE));
DC_WRITE(tegra_crtc, DC_DISP_DISP_SIGNAL_OPTIONS0_REG,
DC_DISP_DISP_SIGNAL_OPTIONS0_H_PULSE2_ENABLE);
DC_WRITE(tegra_crtc, DC_DISP_H_PULSE2_CONTROL_REG,
__SHIFTIN(DC_DISP_H_PULSE2_CONTROL_V_QUAL_VACTIVE,
DC_DISP_H_PULSE2_CONTROL_V_QUAL) |
__SHIFTIN(DC_DISP_H_PULSE2_CONTROL_LAST_END_A,
DC_DISP_H_PULSE2_CONTROL_LAST));
const u_int pulse_start = 1 + hspw + hbp - 10;
DC_WRITE(tegra_crtc, DC_DISP_H_PULSE2_POSITION_A_REG,
__SHIFTIN(pulse_start, DC_DISP_H_PULSE2_POSITION_A_START) |
__SHIFTIN(pulse_start + 8, DC_DISP_H_PULSE2_POSITION_A_END));
/* Pixel clock */
const u_int parent_rate = clk_get_rate(tegra_crtc->clk_parent);
const u_int div = (parent_rate * 2) / (mode->crtc_clock * 1000) - 2;
DC_WRITE(tegra_crtc, DC_DISP_DISP_CLOCK_CONTROL_REG,
__SHIFTIN(0, DC_DISP_DISP_CLOCK_CONTROL_PIXEL_CLK_DIVIDER) |
__SHIFTIN(div, DC_DISP_DISP_CLOCK_CONTROL_SHIFT_CLK_DIVIDER));
/* Mode timings */
DC_WRITE(tegra_crtc, DC_DISP_REF_TO_SYNC_REG,
__SHIFTIN(1, DC_DISP_REF_TO_SYNC_V) |
__SHIFTIN(1, DC_DISP_REF_TO_SYNC_H));
DC_WRITE(tegra_crtc, DC_DISP_SYNC_WIDTH_REG,
__SHIFTIN(vspw, DC_DISP_SYNC_WIDTH_V) |
__SHIFTIN(hspw, DC_DISP_SYNC_WIDTH_H));
DC_WRITE(tegra_crtc, DC_DISP_BACK_PORCH_REG,
__SHIFTIN(vbp, DC_DISP_BACK_PORCH_V) |
__SHIFTIN(hbp, DC_DISP_BACK_PORCH_H));
DC_WRITE(tegra_crtc, DC_DISP_FRONT_PORCH_REG,
__SHIFTIN(vfp, DC_DISP_FRONT_PORCH_V) |
__SHIFTIN(hfp, DC_DISP_FRONT_PORCH_H));
DC_WRITE(tegra_crtc, DC_DISP_DISP_ACTIVE_REG,
__SHIFTIN(mode->crtc_vdisplay, DC_DISP_DISP_ACTIVE_V) |
__SHIFTIN(mode->crtc_hdisplay, DC_DISP_DISP_ACTIVE_H));
return 0;
}
static int
tegra_crtc_do_set_base(struct drm_crtc *crtc, struct drm_framebuffer *fb,
int x, int y, int atomic)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
struct tegra_framebuffer *tegra_fb = atomic ?
to_tegra_framebuffer(fb) :
to_tegra_framebuffer(crtc->primary->fb);
uint64_t paddr = (uint64_t)tegra_fb->obj->dmamap->dm_segs[0].ds_addr;
/* Framebuffer start address */
DC_WRITE(tegra_crtc, DC_WINBUF_A_START_ADDR_HI_REG, (paddr >> 32) & 3);
DC_WRITE(tegra_crtc, DC_WINBUF_A_START_ADDR_REG, paddr & 0xffffffff);
/* Offsets */
DC_WRITE(tegra_crtc, DC_WINBUF_A_ADDR_H_OFFSET_REG, x);
DC_WRITE(tegra_crtc, DC_WINBUF_A_ADDR_V_OFFSET_REG, y);
/* Surface kind */
DC_WRITE(tegra_crtc, DC_WINBUF_A_SURFACE_KIND_REG,
__SHIFTIN(DC_WINBUF_A_SURFACE_KIND_SURFACE_KIND_PITCH,
DC_WINBUF_A_SURFACE_KIND_SURFACE_KIND));
return 0;
}
static int
tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
struct drm_framebuffer *old_fb)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
tegra_crtc_do_set_base(crtc, old_fb, x, y, 0);
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_WIN_A_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_WIN_A_ACT_REQ);
return 0;
}
static int
tegra_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
struct drm_framebuffer *fb, int x, int y, enum mode_set_atomic state)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
tegra_crtc_do_set_base(crtc, fb, x, y, 1);
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_WIN_A_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_WIN_A_ACT_REQ);
return 0;
}
static void
tegra_crtc_disable(struct drm_crtc *crtc)
{
}
static void
tegra_crtc_prepare(struct drm_crtc *crtc)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
/* Access control */
DC_WRITE(tegra_crtc, DC_CMD_STATE_ACCESS_REG, 0);
/* Enable window A programming */
DC_WRITE(tegra_crtc, DC_CMD_DISPLAY_WINDOW_HEADER_REG,
DC_CMD_DISPLAY_WINDOW_HEADER_WINDOW_A_SELECT);
}
static void
tegra_crtc_commit(struct drm_crtc *crtc)
{
struct tegra_crtc *tegra_crtc = to_tegra_crtc(crtc);
/* Enable continuous display mode */
DC_WRITE(tegra_crtc, DC_CMD_DISPLAY_COMMAND_REG,
__SHIFTIN(DC_CMD_DISPLAY_COMMAND_DISPLAY_CTRL_MODE_C_DISPLAY,
DC_CMD_DISPLAY_COMMAND_DISPLAY_CTRL_MODE));
/* Enable power */
DC_SET_CLEAR(tegra_crtc, DC_CMD_DISPLAY_POWER_CONTROL_REG,
DC_CMD_DISPLAY_POWER_CONTROL_PM1_ENABLE |
DC_CMD_DISPLAY_POWER_CONTROL_PM0_ENABLE |
DC_CMD_DISPLAY_POWER_CONTROL_PW4_ENABLE |
DC_CMD_DISPLAY_POWER_CONTROL_PW3_ENABLE |
DC_CMD_DISPLAY_POWER_CONTROL_PW2_ENABLE |
DC_CMD_DISPLAY_POWER_CONTROL_PW1_ENABLE |
DC_CMD_DISPLAY_POWER_CONTROL_PW0_ENABLE,
0);
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_UPDATE |
DC_CMD_STATE_CONTROL_WIN_A_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_ACT_REQ |
DC_CMD_STATE_CONTROL_WIN_A_ACT_REQ);
tegra_crtc->enabled = true;
}
static int
tegra_encoder_init(struct drm_device *ddev)
{
struct tegra_drm_softc * const sc = tegra_drm_private(ddev);
struct tegra_encoder *encoder;
int error;
if (sc->sc_clk_hdmi == NULL ||
sc->sc_clk_hdmi_parent == NULL ||
sc->sc_rst_hdmi == NULL) {
DRM_ERROR("no clocks configured for hdmi\n");
DRM_ERROR("clk: hdmi %p parent %p\n", sc->sc_clk_hdmi, sc->sc_clk_hdmi_parent);
DRM_ERROR("rst: hdmi %p\n", sc->sc_rst_hdmi);
return -EIO;
}
const bus_addr_t offset = TEGRA_GHOST_BASE + TEGRA_HDMI_OFFSET;
const bus_size_t size = TEGRA_HDMI_SIZE;
encoder = kmem_zalloc(sizeof(*encoder), KM_SLEEP);
encoder->bst = sc->sc_bst;
error = bus_space_map(encoder->bst, offset, size, 0, &encoder->bsh);
if (error) {
kmem_free(encoder, sizeof(*encoder));
return -error;
}
encoder->size = size;
tegra_pmc_hdmi_enable();
/* Enable parent PLL */
error = clk_set_rate(sc->sc_clk_hdmi_parent, 594000000);
if (error) {
DRM_ERROR("couldn't set hdmi parent PLL rate: %d\n", error);
return -error;
}
error = clk_enable(sc->sc_clk_hdmi_parent);
if (error) {
DRM_ERROR("couldn't enable hdmi parent PLL: %d\n", error);
return -error;
}
drm_encoder_init(ddev, &encoder->base, &tegra_encoder_funcs,
DRM_MODE_ENCODER_TMDS, NULL);
drm_encoder_helper_add(&encoder->base, &tegra_encoder_helper_funcs);
encoder->base.possible_crtcs = (1 << 0) | (1 << 1);
return tegra_connector_init(ddev, &encoder->base);
}
static void
tegra_encoder_destroy(struct drm_encoder *encoder)
{
struct tegra_encoder *tegra_encoder = to_tegra_encoder(encoder);
drm_encoder_cleanup(encoder);
kmem_free(tegra_encoder, sizeof(*tegra_encoder));
}
static void
tegra_encoder_dpms(struct drm_encoder *encoder, int mode)
{
struct tegra_encoder *tegra_encoder = to_tegra_encoder(encoder);
if (encoder->crtc == NULL)
return;
switch (mode) {
case DRM_MODE_DPMS_ON:
case DRM_MODE_DPMS_STANDBY:
case DRM_MODE_DPMS_SUSPEND:
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_SOR_BLANK_REG,
0, HDMI_NV_PDISP_SOR_BLANK_OVERRIDE);
break;
case DRM_MODE_DPMS_OFF:
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_SOR_BLANK_REG,
HDMI_NV_PDISP_SOR_BLANK_OVERRIDE, 0);
break;
}
}
static bool
tegra_encoder_mode_fixup(struct drm_encoder *encoder,
const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
{
return true;
}
static int
tegra_encoder_hdmi_set_clock(struct drm_encoder *encoder, u_int rate)
{
struct drm_device *ddev = encoder->dev;
struct tegra_drm_softc * const sc = tegra_drm_private(ddev);
int error;
/* Enter reset */
fdtbus_reset_assert(sc->sc_rst_hdmi);
/* Set HDMI parent clock */
error = clk_set_parent(sc->sc_clk_hdmi, sc->sc_clk_hdmi_parent);
if (error) {
DRM_ERROR("couldn't set hdmi parent: %d\n", error);
return -error;
}
/* Set dot clock frequency */
error = clk_set_rate(sc->sc_clk_hdmi, rate);
if (error) {
DRM_ERROR("couldn't set hdmi clock: %d\n", error);
return -error;
}
error = clk_enable(sc->sc_clk_hdmi);
if (error) {
DRM_ERROR("couldn't enable hdmi clock: %d\n", error);
return -error;
}
/* Leave reset */
fdtbus_reset_deassert(sc->sc_rst_hdmi);
return 0;
}
static void
tegra_encoder_mode_set(struct drm_encoder *encoder,
struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
{
struct drm_device *ddev = encoder->dev;
struct tegra_encoder *tegra_encoder = to_tegra_encoder(encoder);
struct tegra_crtc *tegra_crtc = to_tegra_crtc(encoder->crtc);
struct tegra_connector *tegra_connector = NULL;
struct drm_connector *connector;
const struct tegra_hdmi_tmds_config *tmds = NULL;
uint32_t input_ctrl;
int retry;
u_int i;
tegra_encoder_hdmi_set_clock(encoder, mode->crtc_clock * 1000);
/* find the connector for this encoder */
list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
if (connector->encoder == encoder) {
tegra_connector = to_tegra_connector(connector);
break;
}
}
for (i = 0; i < __arraycount(tegra_hdmi_tmds_config); i++) {
if (tegra_hdmi_tmds_config[i].dot_clock >= mode->crtc_clock) {
break;
}
}
if (i < __arraycount(tegra_hdmi_tmds_config)) {
tmds = &tegra_hdmi_tmds_config[i];
} else {
tmds = &tegra_hdmi_tmds_config[__arraycount(tegra_hdmi_tmds_config) - 1];
}
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_PLL0_REG, tmds->sor_pll0);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_PLL1_REG, tmds->sor_pll1);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_LANE_DRIVE_CURRENT_REG,
tmds->sor_lane_drive_current);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_PE_CURRENT_REG,
tmds->pe_current);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_IO_PEAK_CURRENT_REG,
tmds->sor_io_peak_current);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_PAD_CTLS0_REG,
tmds->sor_pad_ctls0);
const u_int div = (mode->crtc_clock / 1000) * 4;
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_REFCLK_REG,
__SHIFTIN(div >> 2, HDMI_NV_PDISP_SOR_REFCLK_DIV_INT) |
__SHIFTIN(div & 3, HDMI_NV_PDISP_SOR_REFCLK_DIV_FRAC));
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_SOR_CSTM_REG,
__SHIFTIN(HDMI_NV_PDISP_SOR_CSTM_MODE_TMDS,
HDMI_NV_PDISP_SOR_CSTM_MODE) |
__SHIFTIN(2, HDMI_NV_PDISP_SOR_CSTM_ROTCLK) |
HDMI_NV_PDISP_SOR_CSTM_PLLDIV,
HDMI_NV_PDISP_SOR_CSTM_MODE |
HDMI_NV_PDISP_SOR_CSTM_ROTCLK |
HDMI_NV_PDISP_SOR_CSTM_LVDS_EN);
const uint32_t inst =
HDMI_NV_PDISP_SOR_SEQ_INST_DRIVE_PWM_OUT_LO |
HDMI_NV_PDISP_SOR_SEQ_INST_HALT |
__SHIFTIN(2, HDMI_NV_PDISP_SOR_SEQ_INST_WAIT_UNITS) |
__SHIFTIN(1, HDMI_NV_PDISP_SOR_SEQ_INST_WAIT_TIME);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_SEQ_INST0_REG, inst);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_SEQ_INST8_REG, inst);
input_ctrl = __SHIFTIN(tegra_crtc->index,
HDMI_NV_PDISP_INPUT_CONTROL_HDMI_SRC_SELECT);
if (mode->crtc_hdisplay != 640 || mode->crtc_vdisplay != 480)
input_ctrl |= HDMI_NV_PDISP_INPUT_CONTROL_ARM_VIDEO_RANGE;
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_INPUT_CONTROL_REG, input_ctrl);
/* Start SOR */
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_SOR_PLL0_REG,
0,
HDMI_NV_PDISP_SOR_PLL0_PWR |
HDMI_NV_PDISP_SOR_PLL0_VCOPD |
HDMI_NV_PDISP_SOR_PLL0_PULLDOWN);
delay(10);
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_SOR_PLL0_REG,
0,
HDMI_NV_PDISP_SOR_PLL0_PDBG);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_PWR_REG,
HDMI_NV_PDISP_SOR_PWR_NORMAL_STATE |
HDMI_NV_PDISP_SOR_PWR_SETTING_NEW);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_PWR_REG,
HDMI_NV_PDISP_SOR_PWR_NORMAL_STATE);
for (retry = 10000; retry > 0; retry--) {
const uint32_t pwr = HDMI_READ(tegra_encoder,
HDMI_NV_PDISP_SOR_PWR_REG);
if ((pwr & HDMI_NV_PDISP_SOR_PWR_SETTING_NEW) == 0)
break;
delay(10);
}
if (retry == 0) {
DRM_ERROR("timeout enabling SOR power\n");
}
uint32_t state2 =
__SHIFTIN(1, HDMI_NV_PDISP_SOR_STATE2_ASY_OWNER) |
__SHIFTIN(3, HDMI_NV_PDISP_SOR_STATE2_ASY_SUBOWNER) |
__SHIFTIN(1, HDMI_NV_PDISP_SOR_STATE2_ASY_CRCMODE) |
__SHIFTIN(1, HDMI_NV_PDISP_SOR_STATE2_ASY_PROTOCOL);
if (mode->flags & DRM_MODE_FLAG_NHSYNC)
state2 |= HDMI_NV_PDISP_SOR_STATE2_ASY_HSYNCPOL;
if (mode->flags & DRM_MODE_FLAG_NVSYNC)
state2 |= HDMI_NV_PDISP_SOR_STATE2_ASY_VSYNCPOL;
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_STATE2_REG, state2);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_STATE1_REG,
__SHIFTIN(HDMI_NV_PDISP_SOR_STATE1_ASY_HEAD_OPMODE_AWAKE,
HDMI_NV_PDISP_SOR_STATE1_ASY_HEAD_OPMODE) |
HDMI_NV_PDISP_SOR_STATE1_ASY_ORMODE);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_STATE0_REG, 0);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_STATE0_REG,
HDMI_NV_PDISP_SOR_STATE0_UPDATE);
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_SOR_STATE1_REG,
HDMI_NV_PDISP_SOR_STATE1_ATTACHED, 0);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_STATE0_REG, 0);
const u_int rekey = 56;
const u_int hspw = mode->hsync_end - mode->hsync_start;
const u_int hbp = mode->htotal - mode->hsync_end;
const u_int hfp = mode->hsync_start - mode->hdisplay;
const u_int max_ac_packet = (hspw + hbp + hfp - rekey - 18) / 32;
uint32_t ctrl =
__SHIFTIN(rekey, HDMI_NV_PDISP_HDMI_CTRL_REKEY) |
__SHIFTIN(max_ac_packet, HDMI_NV_PDISP_HDMI_CTRL_MAX_AC_PACKET);
if (tegra_connector && tegra_connector->has_hdmi_sink) {
ctrl |= HDMI_NV_PDISP_HDMI_CTRL_ENABLE; /* HDMI ENABLE */
}
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_HDMI_CTRL_REG, ctrl);
if (tegra_connector && tegra_connector->has_hdmi_sink &&
tegra_connector->has_audio) {
struct hdmi_audio_infoframe ai;
struct hdmi_avi_infoframe avii;
uint8_t aibuf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
uint8_t aviibuf[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
const u_int n = 6144; /* 48 kHz */
const u_int cts = ((mode->crtc_clock * 10) * (n / 128)) / 480;
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_SOR_AUDIO_CNTRL0_REG,
__SHIFTIN(HDMI_NV_PDISP_SOR_AUDIO_CNTRL0_SOURCE_SELECT_AUTO,
HDMI_NV_PDISP_SOR_AUDIO_CNTRL0_SOURCE_SELECT) |
HDMI_NV_PDISP_SOR_AUDIO_CNTRL0_INJECT_NULLSMPL);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_AUDIO_N_REG,
HDMI_NV_PDISP_AUDIO_N_RESETF |
HDMI_NV_PDISP_AUDIO_N_GENERATE |
__SHIFTIN(n - 1, HDMI_NV_PDISP_AUDIO_N_VALUE));
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_HDMI_SPARE_REG,
HDMI_NV_PDISP_HDMI_SPARE_HW_CTS |
HDMI_NV_PDISP_HDMI_SPARE_FORCE_SW_CTS |
__SHIFTIN(1, HDMI_NV_PDISP_HDMI_SPARE_CTS_RESET_VAL));
/*
* When HW_CTS=1 and FORCE_SW_CTS=1, the CTS is programmed by
* software in the 44.1 kHz register regardless of chosen rate.
*/
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW_REG,
cts << 8);
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH_REG,
0x80000000 | n);
HDMI_SET_CLEAR(tegra_encoder, HDMI_NV_PDISP_AUDIO_N_REG, 0,
HDMI_NV_PDISP_AUDIO_N_RESETF);
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_SOR_AUDIO_AVAL_0480_REG, 24000);
hdmi_audio_infoframe_init(&ai);
ai.channels = 2;
hdmi_audio_infoframe_pack(&ai, aibuf, sizeof(aibuf));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_HEADER_REG,
aibuf[0] | (aibuf[1] << 8) | (aibuf[2] << 16));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_LOW_REG,
aibuf[3] | (aibuf[4] << 8) |
(aibuf[5] << 16) | (aibuf[6] << 24));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_SUBPACK0_HIGH_REG,
aibuf[7] | (aibuf[8] << 8) | (aibuf[9] << 16));
hdmi_avi_infoframe_init(&avii);
drm_hdmi_avi_infoframe_from_display_mode(&avii, connector,
mode);
hdmi_avi_infoframe_pack(&avii, aviibuf, sizeof(aviibuf));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_HEADER_REG,
aviibuf[0] | (aviibuf[1] << 8) | (aviibuf[2] << 16));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_LOW_REG,
aviibuf[3] | (aviibuf[4] << 8) |
(aviibuf[5] << 16) | (aviibuf[6] << 24));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK0_HIGH_REG,
aviibuf[7] | (aviibuf[8] << 8) | (aviibuf[9] << 16));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_LOW_REG,
aviibuf[10] | (aviibuf[11] << 8) |
(aviibuf[12] << 16) | (aviibuf[13] << 24));
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_SUBPACK1_HIGH_REG,
aviibuf[14] | (aviibuf[15] << 8) | (aviibuf[16] << 16));
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_HDMI_GENERIC_CTRL_REG,
HDMI_NV_PDISP_HDMI_GENERIC_CTRL_AUDIO);
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL_REG,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL_ENABLE);
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL_REG,
HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL_ENABLE);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_HDMI_ACR_CTRL_REG, 0);
} else {
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_GENERIC_CTRL_REG, 0);
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AVI_INFOFRAME_CTRL_REG, 0);
HDMI_WRITE(tegra_encoder,
HDMI_NV_PDISP_HDMI_AUDIO_INFOFRAME_CTRL_REG, 0);
HDMI_WRITE(tegra_encoder, HDMI_NV_PDISP_HDMI_ACR_CTRL_REG, 0);
}
/* Enable DC output to HDMI */
DC_SET_CLEAR(tegra_crtc, DC_DISP_DISP_WIN_OPTIONS_REG,
DC_DISP_DISP_WIN_OPTIONS_HDMI_ENABLE, 0);
/* Commit settings */
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_UPDATE |
DC_CMD_STATE_CONTROL_WIN_A_UPDATE);
DC_WRITE(tegra_crtc, DC_CMD_STATE_CONTROL_REG,
DC_CMD_STATE_CONTROL_GENERAL_ACT_REQ |
DC_CMD_STATE_CONTROL_WIN_A_ACT_REQ);
}
static void
tegra_encoder_prepare(struct drm_encoder *encoder)
{
}
static void
tegra_encoder_commit(struct drm_encoder *encoder)
{
}
static int
tegra_connector_init(struct drm_device *ddev, struct drm_encoder *encoder)
{
struct tegra_drm_softc * const sc = tegra_drm_private(ddev);
struct tegra_connector *connector;
connector = kmem_zalloc(sizeof(*connector), KM_SLEEP);
drm_connector_init(ddev, &connector->base, &tegra_connector_funcs,
DRM_MODE_CONNECTOR_HDMIA);
drm_connector_helper_add(&connector->base,
&tegra_connector_helper_funcs);
connector->base.interlace_allowed = 0;
connector->base.doublescan_allowed = 0;
drm_sysfs_connector_add(&connector->base);
connector->base.polled =
DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
drm_connector_attach_encoder(&connector->base, encoder);
connector->hpd = sc->sc_pin_hpd;
if (!connector->hpd)
DRM_ERROR("failed to find hpd pin for connector\n");
connector->ddc = sc->sc_ddc;
if (!connector->ddc)
DRM_ERROR("failed to find ddc device for connector\n");
return drm_connector_register(&connector->base);
}
static void
tegra_connector_destroy(struct drm_connector *connector)
{
struct tegra_connector *tegra_connector = to_tegra_connector(connector);
drm_sysfs_connector_remove(connector);
drm_connector_cleanup(connector);
kmem_free(tegra_connector, sizeof(*tegra_connector));
}
static enum drm_connector_status
tegra_connector_detect(struct drm_connector *connector, bool force)
{
struct tegra_connector *tegra_connector = to_tegra_connector(connector);
bool con;
if (!tegra_connector->hpd) {
tegra_connector->has_hdmi_sink = false;
tegra_connector->has_audio = false;
return connector_status_connected;
}
con = fdtbus_gpio_read(tegra_connector->hpd);
if (con) {
return connector_status_connected;
} else {
prop_dictionary_t prop = device_properties(connector->dev->dev);
prop_dictionary_remove(prop, "physical-address");
tegra_connector->has_hdmi_sink = false;
tegra_connector->has_audio = false;
return connector_status_disconnected;
}
}
static int
tegra_connector_mode_valid(struct drm_connector *connector,
struct drm_display_mode *mode)
{
return MODE_OK;
}
static int
tegra_connector_get_modes(struct drm_connector *connector)
{
struct tegra_connector *tegra_connector = to_tegra_connector(connector);
struct tegra_drm_softc * const sc = tegra_drm_private(connector->dev);
prop_dictionary_t prop = device_properties(connector->dev->dev);
char edid[EDID_LENGTH * 4];
struct edid *pedid = NULL;
int error, block;
if (tegra_connector->ddc) {
memset(edid, 0, sizeof(edid));
for (block = 0; block < 4; block++) {
error = ddc_read_edid_block(tegra_connector->ddc,
&edid[block * EDID_LENGTH], EDID_LENGTH, block);
if (error)
break;
if (block == 0) {
pedid = (struct edid *)edid;
if (edid[0x7e] == 0)
break;
}
}
}
if (pedid) {
if (sc->sc_force_dvi) {
tegra_connector->has_hdmi_sink = false;
tegra_connector->has_audio = false;
} else {
tegra_connector->has_hdmi_sink =
drm_detect_hdmi_monitor(pedid);
tegra_connector->has_audio =
drm_detect_monitor_audio(pedid);
}
drm_connector_update_edid_property(connector, pedid);
error = drm_add_edid_modes(connector, pedid);
if (drm_detect_hdmi_monitor(pedid)) {
prop_dictionary_set_uint16(prop, "physical-address",
connector->physical_address);
}
return error;
} else {
drm_connector_update_edid_property(connector, NULL);
return 0;
}
}
static int
tegra_crtc_intr(void *priv)
{
struct tegra_crtc *tegra_crtc = priv;
struct drm_device *ddev = tegra_crtc->base.dev;
struct tegra_drm_softc * const sc = tegra_drm_private(ddev);
int rv = 0;
const uint32_t status = DC_READ(tegra_crtc, DC_CMD_INT_STATUS_REG);
if (status & DC_CMD_INT_V_BLANK) {
DC_WRITE(tegra_crtc, DC_CMD_INT_STATUS_REG, DC_CMD_INT_V_BLANK);
atomic_inc_32(&sc->sc_vbl_received[tegra_crtc->index]);
drm_handle_vblank(ddev, tegra_crtc->index);
rv = 1;
}
return rv;
}
u32
tegra_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
{
struct tegra_drm_softc * const sc = tegra_drm_private(ddev);
if (crtc > 1)
return 0;
return sc->sc_vbl_received[crtc];
}
int
tegra_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
{
struct tegra_crtc *tegra_crtc = NULL;
struct drm_crtc *iter;
list_for_each_entry(iter, &ddev->mode_config.crtc_list, head) {
if (to_tegra_crtc(iter)->index == crtc) {
tegra_crtc = to_tegra_crtc(iter);
break;
}
}
if (tegra_crtc == NULL)
return -EINVAL;
DC_SET_CLEAR(tegra_crtc, DC_CMD_INT_MASK_REG, DC_CMD_INT_V_BLANK, 0);
return 0;
}
void
tegra_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
{
struct tegra_crtc *tegra_crtc = NULL;
struct drm_crtc *iter;
list_for_each_entry(iter, &ddev->mode_config.crtc_list, head) {
if (to_tegra_crtc(iter)->index == crtc) {
tegra_crtc = to_tegra_crtc(iter);
break;
}
}
if (tegra_crtc == NULL)
return;
DC_SET_CLEAR(tegra_crtc, DC_CMD_INT_MASK_REG, 0, DC_CMD_INT_V_BLANK);
DC_WRITE(tegra_crtc, DC_CMD_INT_STATUS_REG, DC_CMD_INT_V_BLANK);
}
|