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
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
|
/* sprd_drm.c
*
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
* Authors:
* Roman Marchenko <r.marchenko@samsung.com>
*
* 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 (including the next
* paragraph) 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
* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/stddef.h>
#include <linux/fb.h>
#include <video/sprd_fb.h>
#include "xf86drm.h"
#include "xf86drmMode.h"
#include "drm_fourcc.h"
#include "libdrm_lists.h"
#include "sprd_drmif.h"
#define U642VOID(x) ((void *)(unsigned long)(x))
#define U642INTPTR(x) ( (unsigned int*) U642VOID(x) )
#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
#define SPRD_DEBUG_MSG 1
#ifdef SPRD_DEBUG_MSG
#define SPRD_DRM_DEBUG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#define SPRD_DRM_ERROR(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
#else
#define SPRD_DRM_DEBUG(fmt, ...)
#define SPRD_DRM_ERROR(fmt, ...)
#endif
#define FB_DEV_LCD "/dev/fb0"
//TODO::
//#define FB_DEV_HDMI "/dev/fb1"
//#define FB_DEV_WB "/dev/fb2"
#define MAX_CRTC 1
#define MAX_PLANE 2
#define MAX_CONNECTOR 1
#define DEFAULT_ZPOZ SPRD_LAYERS_OSD
#define DRM_PROP_NAME_LEN 32
#define DRM_MODE_PROP_PENDING (1<<0)
#define DRM_MODE_PROP_RANGE (1<<1)
#define DRM_MODE_PROP_IMMUTABLE (1<<2)
#define DRM_MODE_PROP_ENUM (1<<3) /* enumerated type with text strings */
#define DRM_MODE_PROP_BLOB (1<<4)
#define DRM_MODE_PROP_BITMASK (1<<5) /* bitmask of enumerated types */
struct sprd_drm_device;
struct sprd_drm_mode_mode {
struct drm_mode_modeinfo drm_mode;
uint32_t mode_id; /**< Id */
};
struct sprd_drm_mode_crtc {
struct drm_mode_crtc drm_crtc;
drmMMListHead link;
int is_active;
};
#define DRM_CONNECTOR_MAX_ENCODER 3
struct sprd_drm_mode_connector {
struct drm_mode_get_connector drm_conn;
drmMMListHead link;
struct sprd_drm_device * dev;
/* file descriptor of frame buffer */
int32_t fb_fd;
const char * fb_fd_name;
/* requested DPMS state */
int32_t dpms;
/* activated layers */
uint32_t activated_layers;
drmMMListHead modes;
uint32_t encoder_ids[DRM_CONNECTOR_MAX_ENCODER];
};
struct sprd_drm_mode_encoder {
struct drm_mode_get_encoder drm_encoder;
drmMMListHead link;
};
struct sprd_drm_mode_plane {
struct drm_mode_get_plane drm_plane;
drmMMListHead link;
/* Signed dest location allows it to be partially off screen */
int32_t crtc_x, crtc_y;
uint32_t crtc_w, crtc_h;
/* Source values are 16.16 fixed point */
uint32_t src_x, src_y;
uint32_t src_h, src_w;
int need_update;
int zpos;
};
struct sprd_drm_property_blob {
uint32_t id;
drmMMListHead head;
unsigned int length;
unsigned char data[];
};
struct sprd_drm_property_enum {
uint64_t value;
drmMMListHead head;
char name[DRM_PROP_NAME_LEN];
};
struct sprd_drm_property {
drmMMListHead head;
uint32_t id;
uint32_t flags;
char name[DRM_PROP_NAME_LEN];
uint32_t num_values;
uint64_t *values;
void (*prop_set)(struct sprd_drm_property *prop, uint32_t obj_id, uint64_t val);
uint64_t (*prop_get)(struct sprd_drm_property *prop, uint32_t obj_id);
drmMMListHead enum_blob_list;
};
/**
* @drm_fd: file descriptor of drm device
* @num_fb: number of fbs available
* @fb_list: list of framebuffers available
* @num_connector: number of connectors on this device
* @connector_list: list of connector objects
* @num_encoder: number of encoders on this device
* @encoder_list: list of encoder objects
* @num_crtc: number of CRTCs on this device
* @crtc_list: list of CRTC objects
*
* Core mode resource tracking structure. All CRTC, encoders, and connectors
* enumerated by the driver are added here, as are global properties. Some
* global restrictions are also here, e.g. dimension restrictions.
*/
struct sprd_drm_device {
drmMMListHead link;
int drm_fd;
uint32_t num_fb;
drmMMListHead fb_list;
uint32_t num_connector;
drmMMListHead connector_list;
uint32_t num_encoder;
drmMMListHead encoder_list;
uint32_t num_plane;
drmMMListHead plane_list;
uint32_t num_crtc;
drmMMListHead crtc_list;
drmMMListHead property_list;
};
struct sprd_drm_framebuffer {
/*
* TODO:: this is from kernel
*
* Note that the fb is refcounted for the benefit of driver internals,
* for example some hw, disabling a CRTC/plane is asynchronous, and
* scanout does not actually complete until the next vblank. So some
* cleanup (like releasing the reference(s) on the backing GEM bo(s))
* should be deferred. In cases like this, the driver would like to
* hold a ref to the fb even though it has already been removed from
* userspace perspective.
*/
struct sprd_drm_device * dev;
uint32_t refcount;
uint32_t id;
uint32_t pitches[4];
uint32_t offsets[4];
uint32_t handles[4];
uint32_t names[4];
uint32_t width;
uint32_t height;
uint32_t depth;
uint32_t bits_per_pixel;
uint32_t flags;
uint32_t pixel_format; /* fourcc format */
drmMMListHead link;
};
/*
* @devices: list of drm device. the system may have more then one video card.
*/
drmMMListHead devices = {&devices, &devices};
/*
* @resource_storage: storage for all recurces
*/
static void * resource_storage = NULL;
static const uint32_t formats[] = {
DRM_FORMAT_RGB565,
DRM_FORMAT_RGB888,
DRM_FORMAT_XRGB8888,
DRM_FORMAT_ARGB8888,
DRM_FORMAT_YUV422,
DRM_FORMAT_YUV420
};
struct sprd_drm_resource {
void * val;
int property_count;
int property_capacity;
void ** propertys;
};
static int sprd_drm_resource_new_id(void * val)
{
static int next_id = 0;
struct sprd_drm_resource * res;
res = drmMalloc(sizeof(struct sprd_drm_resource));
res->val = val;
if (!resource_storage)
resource_storage = drmHashCreate();
drmHashInsert(resource_storage, ++next_id, res);
return next_id;
}
static void * sprd_drm_resource_get(int id)
{
struct sprd_drm_resource * res = NULL;
if (!resource_storage)
return NULL;
drmHashLookup(resource_storage, id, &res);
return res ? res->val : NULL;
}
static void sprd_drm_resource_del(int id)
{
struct sprd_drm_resource * res = NULL;
if (!resource_storage)
return ;
drmHashLookup(resource_storage, id, &res);
drmHashDelete(resource_storage, id);
drmFree(res->propertys);
drmFree(res);
}
static void sprd_drm_resource_prop_add(int id, void * prop)
{
struct sprd_drm_resource * res = NULL;
if (!resource_storage)
return;
if (!prop)
return;
drmHashLookup(resource_storage, id, &res);
if (!res)
return;
if (res->property_capacity == 0) {
res->property_capacity = 8;
res->propertys = drmMalloc( sizeof(void *) * res->property_capacity);
}
else if (res->property_count >= res->property_capacity) {
//TODO: relocate data
return;
}
res->propertys[res->property_count++] = prop;
}
static int sprd_drm_resource_prop_list_get(int id, void ** propertys)
{
struct sprd_drm_resource * res = NULL;
if (!resource_storage)
return 0;
drmHashLookup(resource_storage, id, &res);
if (!res)
return 0;
*propertys = res->propertys;
return res->property_count;
}
/*
* from kernel
* Original addfb only supported RGB formats, so figure out which one
*/
static uint32_t sprd_drm_legacy_fb_format(uint32_t bpp, uint32_t depth)
{
uint32_t fmt;
switch (bpp) {
case 8:
fmt = DRM_FORMAT_C8;
break;
case 16:
if (depth == 15)
fmt = DRM_FORMAT_XRGB1555;
else
fmt = DRM_FORMAT_RGB565;
break;
case 24:
fmt = DRM_FORMAT_RGB888;
break;
case 32:
if (depth == 24)
fmt = DRM_FORMAT_XRGB8888;
else if (depth == 30)
fmt = DRM_FORMAT_XRGB2101010;
else
fmt = DRM_FORMAT_ARGB8888;
break;
default:
SPRD_DRM_DEBUG("bad bpp, assuming x8r8g8b8 pixel format\n");
fmt = DRM_FORMAT_XRGB8888;
break;
}
return fmt;
}
/*
* from kernel
*
* Just need to support RGB formats here for compat with code that doesn't
* use pixel formats directly yet.
*/
static void sprd_drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth, unsigned int *bpp)
{
switch (format) {
case DRM_FORMAT_C8:
case DRM_FORMAT_RGB332:
case DRM_FORMAT_BGR233:
*depth = 8;
*bpp = 8;
break;
case DRM_FORMAT_XRGB1555:
case DRM_FORMAT_XBGR1555:
case DRM_FORMAT_RGBX5551:
case DRM_FORMAT_BGRX5551:
case DRM_FORMAT_ARGB1555:
case DRM_FORMAT_ABGR1555:
case DRM_FORMAT_RGBA5551:
case DRM_FORMAT_BGRA5551:
*depth = 15;
*bpp = 16;
break;
case DRM_FORMAT_RGB565:
case DRM_FORMAT_BGR565:
*depth = 16;
*bpp = 16;
break;
case DRM_FORMAT_RGB888:
case DRM_FORMAT_BGR888:
*depth = 24;
*bpp = 24;
break;
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRX8888:
*depth = 24;
*bpp = 32;
break;
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRX1010102:
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_BGRA1010102:
*depth = 30;
*bpp = 32;
break;
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_BGRA8888:
*depth = 32;
*bpp = 32;
break;
default:
SPRD_DRM_DEBUG("unsupported pixel format\n");
*depth = 0;
*bpp = 0;
break;
}
}
static inline uint32_t _get_refresh(struct fb_var_screeninfo * timing)
{
uint32_t pixclock, hfreq, htotal, vtotal;
pixclock = PICOS2KHZ(timing->pixclock) * 1000;
htotal = timing->xres + timing->right_margin + timing->hsync_len +
timing->left_margin;
vtotal = timing->yres + timing->lower_margin + timing->vsync_len +
timing->upper_margin;
if (timing->vmode & FB_VMODE_INTERLACED)
vtotal /= 2;
if (timing->vmode & FB_VMODE_DOUBLE)
vtotal *= 2;
hfreq = pixclock/htotal;
return hfreq/vtotal;
}
/**
* drm_mode_set_name - set the name on a mode
* @mode: name will be set in this mode
*
* Set the name of @mode to a standard format.
*/
static void drm_mode_set_name(struct drm_mode_modeinfo *mode)
{
int interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
mode->hdisplay, mode->vdisplay,
interlaced ? "i" : "");
}
/*
* Convert fb_var_screeninfo to drm_mode_modeinfo
*/
static inline void _fb_var_to_drm_mode(struct drm_mode_modeinfo *mode,
struct fb_var_screeninfo * timing)
{
if (!timing->pixclock)
return;
mode->clock = timing->pixclock / 1000;
mode->vrefresh = _get_refresh(timing);
mode->hdisplay = timing->xres;
mode->hsync_start = mode->hdisplay + timing->right_margin;
mode->hsync_end = mode->hsync_start + timing->hsync_len;
mode->htotal = mode->hsync_end + timing->left_margin;
mode->vdisplay = timing->yres;
mode->vsync_start = mode->vdisplay + timing->lower_margin;
mode->vsync_end = mode->vsync_start + timing->vsync_len;
mode->vtotal = mode->vsync_end + timing->upper_margin;
mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
if (timing->vmode & FB_VMODE_INTERLACED)
mode->flags |= DRM_MODE_FLAG_INTERLACE;
if (timing->vmode & FB_VMODE_DOUBLE)
mode->flags |= DRM_MODE_FLAG_DBLSCAN;
drm_mode_set_name(mode);
}
static struct sprd_drm_mode_mode * sprd_drm_mode_create(void)
{
struct sprd_drm_mode_mode * mode = NULL;
mode = drmMalloc(sizeof (struct sprd_drm_mode_mode));
mode->mode_id = sprd_drm_resource_new_id(mode);
return mode;
}
static void sprd_drm_mode_connector_update_from_frame_buffer(struct sprd_drm_mode_connector * conn)
{
struct sprd_drm_mode_mode * mode;
conn->drm_conn.connection = 0;
if (!conn->fb_fd_name) return;
if (!conn->fb_fd)
conn->fb_fd = open (conn->fb_fd_name, O_RDWR, 0);
if (conn->fb_fd > 0)
{
/* Get the current screen setting */
struct fb_var_screeninfo mi;
if (!ioctl (conn->fb_fd, FBIOGET_VSCREENINFO, &mi))
{
conn->drm_conn.connection = 1;
conn->drm_conn.mm_height = mi.height;
conn->drm_conn.mm_width = mi.width;
//get modes at first
if (conn->drm_conn.count_modes < 1)
{
conn->drm_conn.count_modes = 1;
mode = sprd_drm_mode_create();
conn->drm_conn.modes_ptr = VOID2U64(&mode->drm_mode);
}
else
{
mode = (struct sprd_drm_mode_mode*)(U642VOID(conn->drm_conn.modes_ptr));
}
_fb_var_to_drm_mode(&mode->drm_mode, &mi);
//TODO: check real state
conn->dpms = DRM_MODE_DPMS_OFF;
}
else
{
close(conn->fb_fd);
}
}
}
/*
* Get a gem global object name from a gem object handle
*
* @fd: file descriptor of drm device
* @handle: a gem object handle
*
* This interface is used to get a gem global object name from a gem object
* handle to a buffer that wants to share it with another process
*
* If true, return gem global object name(positive) else 0
*/
static int sprd_drm_get_name(uint32_t fd, uint32_t handle)
{
struct drm_gem_flink arg;
int ret;
if (!handle) return -EINVAL;
char strErroBuf[256] = {0,};
memset(&arg, 0, sizeof(arg));
arg.handle = handle;
ret = drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg);
if (ret) {
SPRD_DRM_ERROR("failed to get gem global name[%s]\n", strerror_r(errno, strErroBuf, 256));
return ret;
}
return arg.name;
}
static int sprd_drm_connector_LCD_create(struct sprd_drm_device * dev, struct sprd_drm_mode_encoder * enc)
{
struct sprd_drm_mode_connector * conn;
conn = drmMalloc(sizeof (struct sprd_drm_mode_connector));
conn->drm_conn.connector_id = sprd_drm_resource_new_id(conn);
conn->drm_conn.connector_type = DRM_MODE_CONNECTOR_LVDS;
//TODO:: unknown value;
conn->drm_conn.connector_type_id = 1;
conn->dev = dev;
//attach encoder
conn->encoder_ids[0] = enc->drm_encoder.encoder_id;
conn->drm_conn.count_encoders = 1;
conn->drm_conn.encoder_id = conn->encoder_ids[0]; /**< Current Encoder */
conn->drm_conn.encoders_ptr = VOID2U64(conn->encoder_ids);
//TODO: EDID, PDMS
conn->drm_conn.count_props = 0;
conn->drm_conn.props_ptr = 0;
conn->drm_conn.prop_values_ptr = 0;
//TODO: get size of screen from FB
conn->drm_conn.subpixel = DRM_MODE_SUBPIXEL_NONE;
DRMLISTADDTAIL(&conn->link, &dev->connector_list);
dev->num_connector++;
conn->fb_fd_name = FB_DEV_LCD;
sprd_drm_mode_connector_update_from_frame_buffer(conn);
return 0;
}
static int32_t sprd_drm_framebuffer_create(struct sprd_drm_device * dev, struct drm_mode_fb_cmd2 * fb_cmd)
{
struct sprd_drm_framebuffer * fb = NULL;
int i;
int names[4];
for (i = 0; i < 4; i++) {
names[i] = sprd_drm_get_name(dev->drm_fd, fb_cmd->handles[i]);
if (names[i] == 0)
return -EACCES;
}
fb = drmMalloc(sizeof (struct sprd_drm_framebuffer));
fb->id = sprd_drm_resource_new_id(fb);
fb->refcount = 1;
fb->dev = dev;
DRMLISTADDTAIL(&fb->link, &dev->crtc_list);
dev->num_fb++;
fb->width = fb_cmd->width;
fb->height = fb_cmd->height;
fb->pixel_format = fb_cmd->pixel_format;
fb->flags = fb_cmd->flags;
sprd_drm_fb_get_bpp_depth(fb_cmd->pixel_format, &fb->depth, &fb->bits_per_pixel);
memcpy(fb->handles, fb_cmd->handles, 4 * sizeof(fb->handles[0]));
memcpy(fb->pitches, fb_cmd->pitches, 4 * sizeof(fb->pitches[0]));
memcpy(fb->offsets, fb_cmd->offsets, 4 * sizeof(fb->offsets[0]));
memcpy(fb->names, names, 4 * sizeof(fb->names[0]));
//returned value
fb_cmd->fb_id = fb->id;
return 0;
}
static int sprd_drm_framebuffer_remove(struct sprd_drm_framebuffer * fb)
{
if (!fb) return -EINVAL;
// if (--fb->refcount) {
// sprd_drm_resource_del(fb->id);
// DRMLISTDEL(&fb->link);
// fb->dev->num_fb--;
// drmFree(fb);
// }
return 0;
}
static struct sprd_drm_mode_encoder * sprd_drm_encoder_create(struct sprd_drm_device * dev, uint32_t encoder_type)
{
struct sprd_drm_mode_encoder * enc = NULL;
enc = drmMalloc(sizeof (struct sprd_drm_mode_encoder));
enc->drm_encoder.encoder_id = sprd_drm_resource_new_id(enc);
enc->drm_encoder.crtc_id = 0;
enc->drm_encoder.encoder_type = encoder_type;
enc->drm_encoder.possible_crtcs = (1 << MAX_CRTC) - 1; //can be connected with any crct
enc->drm_encoder.possible_clones = 0x00; //no clones
DRMLISTADDTAIL(&enc->link, &dev->encoder_list);
dev->num_encoder++;
return enc;
}
static struct sprd_drm_mode_crtc * sprd_drm_crtc_create(struct sprd_drm_device * dev)
{
struct sprd_drm_mode_crtc * crtc;
crtc = drmMalloc(sizeof (struct sprd_drm_mode_crtc));
crtc->drm_crtc.crtc_id = sprd_drm_resource_new_id(crtc);
crtc->drm_crtc.fb_id = 0;
crtc->drm_crtc.count_connectors = 0;
crtc->drm_crtc.set_connectors_ptr = VOID2U64(drmMalloc(MAX_CONNECTOR * sizeof(struct sprd_drm_mode_connector)));
crtc->drm_crtc.x = 0;
crtc->drm_crtc.y = 0;
crtc->drm_crtc.gamma_size = 0;
crtc->drm_crtc.mode_valid = 0;
DRMLISTADDTAIL(&crtc->link, &dev->crtc_list);
dev->num_crtc++;
return crtc;
}
static void sprd_drm_plane_set_property(struct sprd_drm_property *prop, uint32_t obj_id, uint64_t val)
{
struct sprd_drm_mode_plane * plane = sprd_drm_resource_get(obj_id);
if (plane) {
if (plane->zpos != (uint32_t)val) {
plane->need_update = 1;
plane->zpos = (uint32_t)val;
}
}
}
static uint64_t sprd_drm_plane_get_property(struct sprd_drm_property *prop, uint32_t obj_id)
{
struct sprd_drm_mode_plane * plane = sprd_drm_resource_get(obj_id);
if (plane) {
return (uint64_t)plane->zpos;
}
return DEFAULT_ZPOZ;
}
static struct sprd_drm_property * sprd_drm_create_zpos_prpoperty(struct sprd_drm_device * dev)
{
struct sprd_drm_property * prop;
prop = drmMalloc(sizeof (struct sprd_drm_property));
prop->id = sprd_drm_resource_new_id(prop);
strcpy(prop->name, "zpos");
prop->flags = DRM_MODE_PROP_ENUM;
prop->prop_set = sprd_drm_plane_set_property;
prop->prop_get = sprd_drm_plane_get_property;
prop->num_values = 0;
prop->values = 0;
DRMINITLISTHEAD(&prop->enum_blob_list);
DRMLISTADDTAIL(&prop->head, &dev->property_list);
return prop;
}
static struct sprd_drm_mode_plane * sprd_drm_plane_create(struct sprd_drm_device * dev, unsigned int possible_crtcs)
{
struct sprd_drm_mode_plane * plane;
plane = drmMalloc(sizeof (struct sprd_drm_mode_plane));
plane->drm_plane.plane_id = sprd_drm_resource_new_id(plane);
plane->drm_plane.crtc_id = 0;
plane->drm_plane.fb_id = 0;
plane->drm_plane.possible_crtcs = possible_crtcs;
//TODO::
plane->drm_plane.gamma_size = 0;
plane->drm_plane.count_format_types = sizeof(formats)/sizeof(formats[0]) ;
plane->drm_plane.format_type_ptr = VOID2U64(formats);
DRMLISTADDTAIL(&plane->link, &dev->plane_list);
dev->num_plane++;
static struct sprd_drm_property *zpos_prop = NULL;
if (!zpos_prop)
zpos_prop = sprd_drm_create_zpos_prpoperty(dev);
sprd_drm_resource_prop_add(plane->drm_plane.plane_id, zpos_prop);
return plane;
}
static int sprd_drm_connector_disable(struct sprd_drm_mode_connector * conn)
{
conn->dpms = DRM_MODE_DPMS_OFF;
if (ioctl(conn->fb_fd, FBIOBLANK, FB_BLANK_POWERDOWN) < 0) {
SPRD_DRM_ERROR("FB_BLANK_UNBLANK is failed.: %s\n", strerror (errno));
return -EACCES;
}
return 0;
}
static int sprd_drm_connector_enable(struct sprd_drm_mode_connector * conn)
{
if (conn->dpms != DRM_MODE_DPMS_ON) {
if (ioctl(conn->fb_fd, FBIOBLANK, FB_BLANK_UNBLANK) < 0) {
SPRD_DRM_ERROR("FB_BLANK_UNBLANK is failed.: %s\n",
strerror (errno));
return -EACCES;
}
}
conn->dpms = DRM_MODE_DPMS_ON;
return 0;
}
static int sprd_drm_connector_overlay_set(struct sprd_drm_mode_connector * conn,
int x, int y, int w, int h, struct sprd_drm_framebuffer *fb, int zpos) {
struct overlay_setting ovs = {0,};
struct overlay_display_setting ds = {0,};
ovs.layer_index = zpos;
if (fb) {
sprd_drm_connector_enable(conn);
//TODO:: support different formats
ovs.data_type = SPRD_DATA_FORMAT_RGB888;
ovs.y_endian = SPRD_DATA_ENDIAN_B0B1B2B3;
ovs.uv_endian = SPRD_DATA_ENDIAN_B0B1B2B3;
ovs.v_endian = SPRD_DATA_ENDIAN_B0B1B2B3;
ovs.rb_switch = 0;
//TODO:: I am not sure
ovs.rect.x = x;
ovs.rect.y = y;
ovs.rect.w = w;
ovs.rect.h = h;
ds.display_mode = SPRD_DISPLAY_OVERLAY_ASYNC;
ds.layer_index = ovs.layer_index;
if (ovs.layer_index == SPRD_LAYERS_OSD) {
ds.osd_handle = fb->names[0];
} else {
ds.img_handle = fb->names[0];
}
SPRD_DRM_DEBUG("SPRD_FB_SET_OVERLAY(%d) %dx%d+%d+%d\n", ovs.layer_index,x,y,w,h);
if (ioctl(conn->fb_fd, SPRD_FB_SET_OVERLAY, &ovs) == -1) {
SPRD_DRM_ERROR( "error:%s Unable to set overlay: SPRD_FB_SET_OVERLAY\n",
strerror (errno));
return -EACCES;
}
//commit last setting immediately
SPRD_DRM_DEBUG("SPRD_FB_DISPLAY_OVERLAY(%d) osd_handle:%d img_handle:%d\n", ds.layer_index, ds.osd_handle, ds.img_handle);
if (ioctl(conn->fb_fd, SPRD_FB_DISPLAY_OVERLAY, &ds) == -1) {
SPRD_DRM_ERROR( "error:%s Unable to SPRD_FB_DISPLAY_OVERLAY layer %d\n",
strerror (errno), ds.layer_index);
return -EACCES;
}
conn->activated_layers |= zpos;
}
else {
return -EINVAL;
}
return 0;
}
static int sprd_drm_connector_overlay_unset(struct sprd_drm_mode_connector * conn, int zpos) {
conn->activated_layers &= ~zpos;
if (ioctl(conn->fb_fd, SPRD_FB_UNSET_OVERLAY, &zpos) == -1) {
SPRD_DRM_ERROR( "error:%s Unable to SPRD_FB_UNSET_OVERLAY layer %d\n", strerror (errno), zpos);
return -EACCES;
}
return 0;
}
static struct sprd_drm_device * get_sprd_device(int fd)
{
struct sprd_drm_device * dev;
DRMLISTFOREACHENTRY(dev, &devices, link) {
if (dev->drm_fd == fd)
return dev;
}
return NULL;
}
static int sprd_drm_mode_get_resources(int fd, void *arg) {
struct drm_mode_card_res * res = (struct drm_mode_card_res *)arg;
struct sprd_drm_framebuffer *fb;
struct sprd_drm_mode_crtc *crtc;
struct sprd_drm_mode_connector *conn;
struct sprd_drm_mode_encoder *enc;
uint32_t * id_ptr;
struct sprd_drm_device * dev = get_sprd_device(fd);
res->count_fbs = dev->num_fb;
id_ptr = U642INTPTR(res->fb_id_ptr);
if (res->fb_id_ptr && res->count_fbs >= dev->num_fb) {
DRMLISTFOREACHENTRY(fb, &dev->fb_list, link) {
*id_ptr++ = fb->id;
}
}
res->count_crtcs = dev->num_crtc;
id_ptr = U642INTPTR(res->crtc_id_ptr);
if (res->crtc_id_ptr) {
DRMLISTFOREACHENTRY(crtc, &dev->crtc_list, link) {
*id_ptr++ = crtc->drm_crtc.crtc_id;
}
}
res->count_connectors = dev->num_connector;
id_ptr = U642INTPTR(res->connector_id_ptr);
if (res->connector_id_ptr) {
DRMLISTFOREACHENTRY(conn, &dev->connector_list, link) {
*id_ptr++ = conn->drm_conn.connector_id;
}
}
res->count_encoders = dev->num_encoder;
id_ptr = U642INTPTR(res->encoder_id_ptr);
if (res->encoder_id_ptr) {
DRMLISTFOREACHENTRY(enc, &dev->encoder_list, link) {
*id_ptr++ = enc->drm_encoder.encoder_id;
}
}
return 0;
}
static int sprd_drm_mode_get_crtc(int fd, void *arg) {
struct drm_mode_crtc * out_crtc = (struct drm_mode_crtc *) arg;
struct sprd_drm_mode_crtc * crtc;
crtc = (struct sprd_drm_mode_crtc *)sprd_drm_resource_get(out_crtc->crtc_id);
if (!crtc) return -1;
memcpy(out_crtc, &crtc->drm_crtc, sizeof(struct drm_mode_crtc));
return 0;
}
static int sprd_drm_mode_get_connector(int fd, void *arg) {
struct sprd_drm_mode_connector *conn;
struct drm_mode_get_connector * out_conn = (struct drm_mode_get_connector *) arg;
uint32_t len;
conn = (struct sprd_drm_mode_connector *)sprd_drm_resource_get(out_conn->connector_id);
if (!conn) return -1;
if(out_conn->encoders_ptr && out_conn->count_encoders == conn->drm_conn.count_encoders) {
len = sizeof(uint32_t) * out_conn->count_encoders;
memcpy(U642VOID(out_conn->encoders_ptr), U642VOID(conn->drm_conn.encoders_ptr), len);
}
out_conn->count_encoders = conn->drm_conn.count_encoders;
//return return list of the whole structures not only list of ids
if(out_conn->modes_ptr && out_conn->count_modes == conn->drm_conn.count_modes) {
len = sizeof(struct drm_mode_modeinfo) * out_conn->count_modes;
memcpy(U642VOID(out_conn->modes_ptr), U642VOID(conn->drm_conn.modes_ptr), len);
}
out_conn->count_modes = conn->drm_conn.count_modes;
if(out_conn->props_ptr && out_conn->count_props == conn->drm_conn.count_props) {
len = sizeof(uint32_t) * out_conn->count_props;
memcpy(U642VOID(out_conn->props_ptr), U642VOID(conn->drm_conn.props_ptr), len);
}
if(out_conn->prop_values_ptr && out_conn->count_props == conn->drm_conn.count_props) {
len = sizeof(__u64) * out_conn->count_props;
memcpy(U642VOID(out_conn->prop_values_ptr), U642VOID(conn->drm_conn.prop_values_ptr), len);
}
out_conn->count_props = conn->drm_conn.count_props;
out_conn->encoder_id = conn->drm_conn.encoder_id;
out_conn->connector_id = conn->drm_conn.connector_id;
out_conn->connector_type = conn->drm_conn.connector_type;
out_conn->connector_type_id = conn->drm_conn.connector_type_id;
out_conn->connection = conn->drm_conn.connection;
out_conn->mm_width = conn->drm_conn.mm_width;
out_conn->mm_height = conn->drm_conn.mm_height;
out_conn->subpixel = conn->drm_conn.subpixel;
return 0;
}
static int sprd_drm_mode_get_plane(int fd, void *arg) {
struct drm_mode_get_plane * out_plane = (struct drm_mode_get_plane *)arg;
struct sprd_drm_mode_plane * plane;
int len;
plane = (struct sprd_drm_mode_plane *)sprd_drm_resource_get(out_plane->plane_id);
if (!plane) return -1;
if(out_plane->format_type_ptr && out_plane->count_format_types == plane->drm_plane.count_format_types) {
len = sizeof(uint32_t) * out_plane->count_format_types;
memcpy(U642VOID(out_plane->format_type_ptr), U642VOID(plane->drm_plane.format_type_ptr), len);
}
out_plane->count_format_types = plane->drm_plane.count_format_types;
out_plane->plane_id = plane->drm_plane.plane_id;
out_plane->crtc_id = plane->drm_plane.crtc_id;
out_plane->fb_id = plane->drm_plane.fb_id;
out_plane->possible_crtcs = plane->drm_plane.possible_crtcs;
out_plane->gamma_size = plane->drm_plane.gamma_size;
return 0;
}
static int sprd_drm_mode_get_plane_resources(int fd, void *arg) {
struct drm_mode_get_plane_res * out_res = (struct drm_mode_get_plane_res *)arg;
struct sprd_drm_device * dev = get_sprd_device(fd);
struct sprd_drm_mode_plane *plane;
uint32_t * id_ptr;
out_res->count_planes = dev->num_plane;
id_ptr = U642INTPTR(out_res->plane_id_ptr);
if (out_res->plane_id_ptr) {
DRMLISTFOREACHENTRY(plane, &(dev->plane_list), link) {
*id_ptr++ = plane->drm_plane.plane_id;
}
}
return 0;
}
static int sprd_drm_mode_get_encoder(int fd, void *arg) {
struct drm_mode_get_encoder * out_enc = (struct drm_mode_get_encoder *)arg;
struct sprd_drm_mode_encoder * enc;
enc = (struct sprd_drm_mode_encoder *)sprd_drm_resource_get(out_enc->encoder_id);
if (!enc) return -1;
memcpy(out_enc, &enc->drm_encoder, sizeof(struct drm_mode_get_encoder));
return 0;
}
static int sprd_drm_mode_get_property(int fd, void *arg)
{
struct drm_mode_get_property * out_resp = arg;
struct sprd_drm_property *property;
uint32_t enum_count = 0;
int blob_count = 0;
uint32_t value_count = 0;
uint32_t i;
int copied;
struct sprd_drm_property_enum *prop_enum;
struct drm_mode_property_enum *enum_ptr;
struct sprd_drm_property_blob *prop_blob;
// uint32_t *blob_id_ptr;
uint64_t *values_ptr;
// uint32_t *blob_length_ptr;
property = sprd_drm_resource_get(out_resp->prop_id);
if (!property)
return -EINVAL;
if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
DRMLISTFOREACHENTRY(prop_enum, &property->enum_blob_list, head)
enum_count++;
} else if (property->flags & DRM_MODE_PROP_BLOB) {
DRMLISTFOREACHENTRY(prop_blob, &property->enum_blob_list, head)
blob_count++;
}
value_count = property->num_values;
strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
out_resp->flags = property->flags;
if ((out_resp->count_values >= value_count) && value_count) {
values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
for (i = 0; i < value_count; i++) {
values_ptr[i] = property->values[i];
}
}
out_resp->count_values = value_count;
if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
copied = 0;
enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
DRMLISTFOREACHENTRY(prop_enum, &property->enum_blob_list, head) {
enum_ptr[copied].value = prop_enum->value;
memcpy(&enum_ptr[copied].name, &prop_enum->name, DRM_PROP_NAME_LEN);
copied++;
}
}
out_resp->count_enum_blobs = enum_count;
}
if (property->flags & DRM_MODE_PROP_BLOB) {
//TODO::
}
return 0;
}
static int sprd_drm_mode_get_obj_properties(int fd, void *arg)
{
struct drm_mode_obj_get_properties * get_prop = (struct drm_mode_obj_get_properties *)arg;
struct sprd_drm_property **props = NULL;
uint32_t count, i;
uint32_t * out_props_id;
uint64_t * out_values;
count = sprd_drm_resource_prop_list_get(get_prop->obj_id, &props);
if (get_prop->count_props == count) {
out_props_id = U642INTPTR(get_prop->props_ptr);
out_values = (uint64_t*) U642VOID(get_prop->prop_values_ptr);
for (i = 0; i < count; i++) {
out_props_id[i] = props[i]->id;
out_values[i] = props[i]->prop_get(props[i], get_prop->obj_id);
}
}
get_prop->count_props = count;
return 0;
}
static int sprd_drm_mode_set_obj_property(int fd, void *arg)
{
struct drm_mode_obj_set_property *set_prop = (struct drm_mode_obj_set_property *)arg;
struct sprd_drm_property *prop = NULL;
prop = sprd_drm_resource_get(set_prop->prop_id);
if(prop)
prop->prop_set(prop, set_prop->obj_id, set_prop->value);
return 0;
}
static int sprd_drm_mode_set_plane(int fd, void *arg)
{
struct drm_mode_set_plane * plane_cmd = (struct drm_mode_set_plane *) arg;
struct sprd_drm_mode_plane * plane;
struct sprd_drm_mode_crtc * crtc;
struct sprd_drm_framebuffer * fb = NULL, * old_fb = NULL;
struct sprd_drm_mode_connector * conns[MAX_CONNECTOR] = {NULL,};
uint32_t i;
uint32_t * ids;
plane = sprd_drm_resource_get(plane_cmd->plane_id);
if (!plane)
return -EINVAL;
crtc = sprd_drm_resource_get(plane_cmd->crtc_id);
if (!crtc)
return -EINVAL;
ids = U642INTPTR(crtc->drm_crtc.set_connectors_ptr);
for (i = 0; i < crtc->drm_crtc.count_connectors; i++) {
conns[i] = (struct sprd_drm_mode_connector *)sprd_drm_resource_get(ids[i]);
if (conns[i] == NULL)
return -EPERM;
}
/* No fb means shut it down */
if (plane_cmd->fb_id == 0 && plane->drm_plane.fb_id) {
for (i = 0; i < crtc->drm_crtc.count_connectors; i++) {
sprd_drm_connector_overlay_unset(conns[i], plane->zpos);
}
fb = sprd_drm_resource_get(plane->drm_plane.fb_id);
sprd_drm_framebuffer_remove(fb);
plane->drm_plane.fb_id = 0;
plane->drm_plane.crtc_id = 0;
plane->need_update = 0;
return 0;
}
fb = sprd_drm_resource_get(plane_cmd->fb_id);
if (!fb)
return -EINVAL;
if (plane->drm_plane.fb_id != fb->id ||
plane->crtc_x != plane_cmd->crtc_x ||
plane->crtc_y != plane_cmd->crtc_y ||
plane->src_w != plane_cmd->src_w ||
plane->src_h != plane_cmd->src_h ||
plane->need_update)
{
for (i = 0; i < crtc->drm_crtc.count_connectors; i++) {
if(sprd_drm_connector_overlay_set(conns[i], plane_cmd->crtc_x, plane_cmd->crtc_y,
plane_cmd->src_w >> 16, plane_cmd->src_h >> 16, fb, plane->zpos)) {
goto err;
}
}
plane->need_update = 0;
if (plane->drm_plane.fb_id != fb->id) {
old_fb = sprd_drm_resource_get(plane->drm_plane.fb_id);
sprd_drm_framebuffer_remove(old_fb);
plane->drm_plane.fb_id = fb->id;
fb->refcount++;
}
plane->crtc_x = plane_cmd->crtc_x;
plane->crtc_y = plane_cmd->crtc_y;
plane->src_w = plane_cmd->src_w;
plane->src_h = plane_cmd->src_h;
}
if(plane->zpos == DEFAULT_ZPOZ) {
crtc->is_active = 0;
}
plane->drm_plane.crtc_id = plane_cmd->crtc_id;
return 0;
err:
if(plane->drm_plane.fb_id) {
fb = sprd_drm_resource_get(plane->drm_plane.fb_id);
if (fb) {
for (i = 0; i < crtc->drm_crtc.count_connectors; i++) {
sprd_drm_connector_overlay_set(conns[i], plane->crtc_x, plane->crtc_y,
plane->src_x >> 16, plane->src_y >> 16, fb, plane->zpos);
plane->need_update = 0;
}
}
}
return -EINVAL;
}
static int sprd_drm_mode_set_property(int fd, void *arg)
{
struct drm_mode_connector_set_property *conn_set_prop = (struct drm_mode_connector_set_property *)arg;
struct drm_mode_obj_set_property obj_set_prop = {
.value = conn_set_prop->value,
.prop_id = conn_set_prop->prop_id,
.obj_id = conn_set_prop->connector_id,
};
/* It does all the locking and checking we need */
return sprd_drm_mode_set_obj_property(fd, &obj_set_prop);
return -1;
}
static int sprd_drm_mode_set_crtc(int fd, void *arg)
{
struct drm_mode_crtc * crtc_cmd = (struct drm_mode_crtc *) arg;
struct sprd_drm_mode_crtc * crtc;
struct sprd_drm_framebuffer * fb = NULL, * old_fb = NULL;
struct sprd_drm_device * dev;
struct sprd_drm_mode_connector * conn;
struct sprd_drm_mode_connector * conns[MAX_CONNECTOR];
struct sprd_drm_mode_plane * plane;
uint32_t i;
uint32_t * ids;
memset(&conns, 0, sizeof(conns[0]) * MAX_CONNECTOR);
dev = get_sprd_device(fd);
crtc = sprd_drm_resource_get(crtc_cmd->crtc_id);
if (!crtc)
return -EINVAL;
if (crtc_cmd->fb_id) {
fb = sprd_drm_resource_get(crtc_cmd->fb_id);
if (!fb)
return -EINVAL;
}
if (crtc_cmd->count_connectors > 0 && !crtc_cmd->fb_id) {
SPRD_DRM_DEBUG("Count connectors is %d but not fb set\n", crtc_cmd->count_connectors);
return -EINVAL;
}
//disable all connectors witch are connected with this crtc
if (crtc_cmd->fb_id == 0) {
//get list connector from crtc
uint32_t * ids = U642INTPTR(crtc->drm_crtc.set_connectors_ptr);
for (i = 0; i < crtc->drm_crtc.count_connectors; i++ ) {
conn = (struct sprd_drm_mode_connector *)sprd_drm_resource_get(ids[i]);
//disable planes
DRMLISTFOREACHENTRY(plane, &dev->plane_list, link) {
if (plane->drm_plane.crtc_id == crtc->drm_crtc.crtc_id) {
struct drm_mode_set_plane plane_cmd = {0,};
plane_cmd.fb_id = 0;
plane_cmd.crtc_id = plane->drm_plane.crtc_id;
plane_cmd.plane_id = plane->drm_plane.plane_id;
sprd_drm_mode_set_plane(fd, &plane_cmd);
}
}
//disable crtc
if(crtc->is_active) {
sprd_drm_connector_overlay_unset(conn, DEFAULT_ZPOZ);
}
//disable connector
sprd_drm_connector_disable(conn);
}
//clear crtc
if(crtc->drm_crtc.fb_id) {
fb = sprd_drm_resource_get(crtc->drm_crtc.fb_id);
sprd_drm_framebuffer_remove(fb);
crtc->drm_crtc.fb_id = 0;
}
crtc->drm_crtc.count_connectors = 0;
memset(U642VOID(crtc->drm_crtc.set_connectors_ptr), 0, MAX_CONNECTOR);
crtc->drm_crtc.x = 0;
crtc->drm_crtc.y = 0;
crtc->drm_crtc.fb_id = 0;
crtc->is_active = 0;
return 0;
}
if (crtc_cmd->count_connectors > 0 && crtc_cmd->set_connectors_ptr == 0) {
return -EINVAL;
}
ids = U642INTPTR(crtc_cmd->set_connectors_ptr);
for (i = 0; i < crtc_cmd->count_connectors; i++) {
conns[i] = (struct sprd_drm_mode_connector *)sprd_drm_resource_get(ids[i]);
if (conns[i] == NULL)
return -EINVAL;
//TODO:: check new mode for connector
}
/**
* TODO:: reconfigure mode setting:
* 1) check new mode with all connectors
* 2) check previous pipes (connections crtc and connector)
*
* NOTE: now we jast set new pipas
*/
crtc->drm_crtc.count_connectors = crtc_cmd->count_connectors;
memcpy(U642VOID(crtc->drm_crtc.set_connectors_ptr), ids, crtc_cmd->count_connectors);
//update configure
for (i = 0; i < crtc->drm_crtc.count_connectors; i++) {
//TODO:: set new mode for connector
sprd_drm_connector_enable(conns[i]);
//reset the plane what uses DEFAULT_ZPOZ
DRMLISTFOREACHENTRY(plane, &dev->plane_list, link) {
if (plane->drm_plane.crtc_id == crtc->drm_crtc.crtc_id && plane->zpos == DEFAULT_ZPOZ) {
old_fb = sprd_drm_resource_get(plane->drm_plane.fb_id);
sprd_drm_framebuffer_remove(old_fb);
plane->drm_plane.fb_id = 0;
plane->drm_plane.crtc_id = 0;
plane->need_update = 0;
}
}
if (crtc->drm_crtc.fb_id != crtc_cmd->fb_id || !crtc->is_active) {
if (sprd_drm_connector_overlay_set(conns[i], crtc_cmd->x, crtc_cmd->y, fb->width, fb->height, fb, DEFAULT_ZPOZ) == 0) {
if(crtc->drm_crtc.fb_id != crtc_cmd->fb_id) {
old_fb = sprd_drm_resource_get(crtc->drm_crtc.fb_id);
sprd_drm_framebuffer_remove(old_fb);
fb->refcount++;
}
}
else {
goto err;
}
}
}
//save new settings
crtc->drm_crtc.x = crtc_cmd->x;
crtc->drm_crtc.y = crtc_cmd->y;
crtc->drm_crtc.fb_id = crtc_cmd->fb_id;
crtc->drm_crtc.mode = crtc_cmd->mode;
crtc->drm_crtc.mode_valid = 1;
crtc->is_active = 1;
return 0;
err:
//TODO:: restore previous modesetting
return -EACCES;
}
static int sprd_drm_mode_add_fb(int fd, void *arg)
{
struct drm_mode_fb_cmd *or = (struct drm_mode_fb_cmd *)arg;
struct drm_mode_fb_cmd2 r;
int res;
memset(&r, 0, sizeof(struct drm_mode_fb_cmd2));
/* Use new struct with format internally */
r.width = or->width;
r.height = or->height;
r.pitches[0] = or->pitch;
r.pixel_format = sprd_drm_legacy_fb_format(or->bpp, or->depth);
r.handles[0] = or->handle;
res = sprd_drm_framebuffer_create(get_sprd_device(fd), &r);
//returned value
or->fb_id = r.fb_id;
return res;
}
static int sprd_drm_mode_add_fb2(int fd, void *arg)
{
struct drm_mode_fb_cmd2 *r = (struct drm_mode_fb_cmd2 *)arg;
int res;
res = sprd_drm_framebuffer_create(get_sprd_device(fd), r);
return res;
}
static int sprd_drm_mode_rem_fb(int fd, void *arg)
{
struct sprd_drm_framebuffer *fb;
uint32_t id = *((uint32_t *)arg);
fb = (struct sprd_drm_framebuffer *)sprd_drm_resource_get(id);
if (!fb) return -EINVAL;
sprd_drm_framebuffer_remove(fb);
return 0;
}
static int sprd_drm_mode_get_fb(int fd, void *arg)
{
struct drm_mode_fb_cmd *fb_cmd = (struct drm_mode_fb_cmd *)arg;
struct sprd_drm_framebuffer *fb = NULL;
fb = (struct sprd_drm_framebuffer *)sprd_drm_resource_get(fb_cmd->fb_id);
if (!fb) return -EINVAL;
fb_cmd->width = fb->width;
fb_cmd->height = fb->height;
fb_cmd->pitch = fb->pitches[0];
fb_cmd->bpp = fb->bits_per_pixel;
fb_cmd->depth = fb->depth;
fb_cmd->handle = fb->handles[0];
return 0;
}
//static int sprd_drm_mode_page_flip(int fd, void *arg)
//{
// return -1;
//}
//
//static int sprd_drm_wait_vblank(int fd, void *arg)
//{
// return -1;
//}
struct _ioctl_hook {
unsigned long request;
int (*hook)(int fd, void *arg);
};
struct _ioctl_hook _ioctl_hooks[] = {
DRM_IOCTL_MODE_GETRESOURCES, sprd_drm_mode_get_resources,
DRM_IOCTL_MODE_GETCRTC, sprd_drm_mode_get_crtc,
DRM_IOCTL_MODE_GETPLANE, sprd_drm_mode_get_plane,
DRM_IOCTL_MODE_GETPLANERESOURCES, sprd_drm_mode_get_plane_resources,
DRM_IOCTL_MODE_GETENCODER, sprd_drm_mode_get_encoder,
DRM_IOCTL_MODE_GETCONNECTOR, sprd_drm_mode_get_connector,
DRM_IOCTL_MODE_GETPROPERTY, sprd_drm_mode_get_property,
DRM_IOCTL_MODE_SETPROPERTY, sprd_drm_mode_set_property,
DRM_IOCTL_MODE_OBJ_GETPROPERTIES, sprd_drm_mode_get_obj_properties,
DRM_IOCTL_MODE_OBJ_SETPROPERTY, sprd_drm_mode_set_obj_property,
DRM_IOCTL_MODE_SETPLANE, sprd_drm_mode_set_plane,
DRM_IOCTL_MODE_SETCRTC, sprd_drm_mode_set_crtc,
DRM_IOCTL_MODE_ADDFB, sprd_drm_mode_add_fb,
DRM_IOCTL_MODE_ADDFB2, sprd_drm_mode_add_fb2,
DRM_IOCTL_MODE_RMFB, sprd_drm_mode_rem_fb,
DRM_IOCTL_MODE_GETFB, sprd_drm_mode_get_fb/*,
DRM_IOCTL_MODE_PAGE_FLIP, sprd_drm_mode_page_flip,
DRM_IOCTL_WAIT_VBLANK, sprd_drm_wait_vblank
*/
};
static int sprd_ioctl_hook(int fd, unsigned long request, void *arg)
{
uint32_t i;
int32_t ret;
for (i = 0; i < sizeof(_ioctl_hooks)/sizeof (struct _ioctl_hook); i++) {
if (_ioctl_hooks[i].request == request)
return _ioctl_hooks[i].hook(fd,arg);
}
do {
ret = ioctl(fd, request, arg);
} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
return ret;
}
/*
* Create sprd drm device object.
*
* @fd: file descriptor to sprd drm driver opened.
*
* if true, return the device object else NULL.
*/
struct sprd_drm_device * sprd_device_create(int fd)
{
struct sprd_drm_device *dev;
struct sprd_drm_mode_encoder *enc;
uint32_t possible_crtcs;
int i;
dev = drmMalloc(sizeof(struct sprd_drm_device));
if (!dev) {
fprintf(stderr, "failed to create device[%s].\n",
strerror(errno));
return NULL;
}
dev->drm_fd = fd;
drmIoctlSetHook(sprd_ioctl_hook);
DRMINITLISTHEAD(&dev->crtc_list);
DRMINITLISTHEAD(&dev->encoder_list);
DRMINITLISTHEAD(&dev->connector_list);
DRMINITLISTHEAD(&dev->fb_list);
DRMINITLISTHEAD(&dev->plane_list);
DRMINITLISTHEAD(&dev->property_list);
/*
* SPRD7730 is enough to have two CRTCs and each crtc would be used
* without dependency of hardware.
*/
for (i = 0; i < MAX_CRTC; i++) {
if (!sprd_drm_crtc_create(dev))
goto err;
}
possible_crtcs = (1 << MAX_CRTC) - 1;
for (i = 0; i < MAX_PLANE; i++) {
if (!sprd_drm_plane_create(dev, possible_crtcs))
goto err;
}
//LCD encoder + connector
enc = sprd_drm_encoder_create(dev, DRM_MODE_ENCODER_LVDS);
sprd_drm_connector_LCD_create(dev, enc);
//TODO: HDMI encoder + connector
//TODO: WB encoder + connector
DRMLISTADDTAIL(&dev->link, &devices);
return dev;
err:
return 0;
}
/*
* Destroy sprd drm device object
*
* @dev: sprd drm device object.
*/
void sprd_device_destroy(struct sprd_drm_device *dev)
{
//TODO::
free(dev);
}
|