1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef HDMI_DFM_H
10 #define HDMI_DFM_H
11 
12 #include "hdmi_common.h"
13 #include "hdmi_if.h"
14 
15 #ifdef __cplusplus
16 #if __cplusplus
17 extern "C" {
18 #endif
19 #endif /* __cplusplus */
20 
21 /*
22  * DFM(Data Flow Metering).
23  * During data transmission in FRL mode, there is a lot of bandwidth redundancy (blanking period) or slight bandwidth
24  * insufficiency (active video). In order for the link to tolerate these fluctuations, source and sink need to use
25  * buffers. To limit the size of the buffer required, the protocol adds the Data Flow Metering requirement.
26  */
27 #define HDMI_DFM_FRL_CHAR_NUM_PER_CB 502
28 #define HDMI_DFM_RS_NUM_PER_CB 8
29 /* A FRL Character Block(CB) contains 502 FRL Characters and 8 RS check characters. */
30 #define HDMI_DFM_CHAR_NUM_PER_CB (HDMI_DFM_FRL_CHAR_NUM_PER_CB + HDMI_DFM_RS_NUM_PER_CB)
31 
32 /*
33  * A FRL Super Block(SB) contains four FRL Character Blocks.
34  * Each FRL Super Block is preceded by a group of three or four Start Super Blocks (SSB) or a group of three or
35  * four Scrambler Reset (SR) characters.
36  */
37 #define HDMI_DFM_FRL_CB_NUM_PER_SB 4
38 #define HDMI_DFM_FRL_SB_LEN(lane) (HDMI_DFM_FRL_CB_NUM_PER_SB * HDMI_DFM_CHAR_NUM_PER_CB + (lane))
39 
40 #define HDMI_DFM_FRL_PIXELCLK_TOLERANCE 5 /* 0.50% */
41 #define HDMI_DFM_FRL_BITRATE_TOLERANCE 3  /* 300 */
42 #define HDMI_DFM_FRL_AUDIOCLK_TOLERANCE 1
43 #define HDMI_DFM_MAGNIFICATION_8 8
44 #define HDMI_DFM_FRL_MAX_TB_BORROW 400
45 #define HDMI_DFM_RATE_MAGNIFICATION 100000
46 #define HDMI_DFM_OVERHEAD_SIZE 300
47 
48 #define HDMI_AUDIO_CHANNEL_ALLOC_TYPE1 1
49 #define HDMI_AUDIO_CHANNEL_ALLOC_TYPE2 2
50 #define HDMI_AUDIO_CHANNEL_ALLOC_TYPE3 3
51 
52 /*
53  * Packets per sample.
54  * This is the number of audio packets required to carry each sample.
55  * x100.
56  */
57 #define HDMI_AUDIO_AP_SIZE_25  25
58 #define HDMI_AUDIO_AP_SIZE_100 100
59 #define HDMI_AUDIO_AP_SIZE_200 200
60 #define HDMI_AUDIO_AP_SIZE_300 300
61 #define HDMI_AUDIO_AP_SIZE_400 400
62 
63 enum HdmiAudioPacketType {
64     HDMI_AUDIO_SAMPLE_PACKET = 0x02,
65     HDMI_ONE_BIT_AUDIO_SAMPLE_PACKET = 0x07,
66     HDMI_DTS_AUDIO_PACKET = 0x08,
67     HDMI_HBR_AUDIO_PACKET = 0x09,
68     HDMI_AUDIO_3D_SAMPLE_PACKET = 0x0B,
69     HDMI_ONE_BIT_AUDIO_3D_SAMPLE_PACKET = 0x0C,
70     HDMI_MULTI_STREAM_AUDIO_SAMPLE_PACKET = 0x0E,
71     HDMI_ONE_BIT_MULTI_STREAM_AUDIO_SAMPLE_PACKET = 0x0F,
72 };
73 
74 enum HdmiDfmPixelFormatMode {
75     HDMI_DFM_PIXEL_FORMAT_MODE_0 = 0,
76     HDMI_DFM_PIXEL_FORMAT_MODE_1,
77     HDMI_DFM_PIXEL_FORMAT_MODE_2,
78     HDMI_DFM_PIXEL_FORMAT_MODE_3
79 };
80 
81 struct HdmiDfmInfo {
82     bool audioSupport;
83     bool videoSupport;
84     bool uncompressSupport;
85     bool isExtraMode;
86     bool canbeTrans;
87     uint32_t htotal;
88     uint32_t vtotal;
89     uint32_t bpp;     /* Bits per Pixel */
90     uint32_t cFrlSb;  /* Total FRL Characters Per SuperBlock(include SSB/SR) */
91     uint32_t overheadSb;
92     uint32_t overheadRs;
93     uint32_t overheadMap;
94     uint32_t overheadMin;
95     uint32_t overheadMax;
96     uint32_t lineMinTime;  /* minimum Video Line period */
97     uint32_t lineMaxTime;  /* maximum Video Line period */
98     uint32_t audioAp;      /* the number of audio packets required to carry each sample */
99     uint32_t audioRap;     /* Average Audio Related Packet Rate */
100     uint32_t avgAudioPacketsPerLine;  /* Average Required Packets per line */
101     uint32_t audioPacketsLine;        /* Packets per Hblank that must be supportable. */
102     uint32_t hblankAudioMin;          /*  */
103     uint32_t cFrlFree;
104     uint32_t cFrlRcMargin;
105     uint32_t cFrlRcSavings;
106     uint32_t activeBytesPerLine;  /* Video Bytes per line */
107     uint32_t activeTbPerLine;     /* Required Characters to carry Active Video per line */
108     uint32_t hblankTbPerLine;      /* Required Characters to carry H-Blank Video per line */
109     uint32_t activeTimeRef;
110     uint32_t blankTimeRef;
111     uint32_t activeTimeMin;
112     uint32_t blankTimeMin;
113     int32_t tBorrow;
114     int32_t tbBorrow;
115     uint32_t cFrlActualPayload;
116     uint32_t utilization;
117     int32_t margin;
118     uint32_t avgTbRate;
119     uint64_t pixelClk;
120     uint64_t maxPixelClk;        /* maximum legal pixel rate */
121     uint64_t minPixelClk;        /* minimum legal pixel rate */
122     uint64_t maxBitRate;         /* FRL max bit rate */
123     uint64_t minBitRate;         /* FRL min bit rate */
124     uint64_t maxFrlCharRate;     /* max FRL Character Rate */
125     uint64_t minFrlCharRate;     /* min FRL Character Rate */
126     uint32_t maxFrlCharsPerLine; /* max FRL Characters per line Period */
127     uint32_t minFrlCharsPerLine; /* min FRL Characters per line Period */
128 };
129 
130 struct HdmiDfmParam {
131     uint32_t hactive;
132     uint32_t vactive;
133     uint32_t hblank;
134     uint32_t vblank;
135     uint32_t hsync;
136     uint32_t hback;
137     uint32_t hfront;
138     uint32_t vsync;
139     uint32_t vback;
140     uint32_t vfront;
141     uint32_t vfreq;
142     uint32_t colorDepth;
143     uint32_t pixelFormat; /* 1:420, 2:422, 3:RGB444 */
144     uint32_t laneNum;
145     uint32_t bitRate;
146     uint32_t audioRate;
147     uint32_t packetType;
148     uint32_t layout;
149     uint32_t acat;
150 };
151 
152 uint32_t HdmiDfmGetPixelFormat(enum HdmiColorSpace colorSpace);
153 void HdmiDfmFillParam(struct HdmiDfmParam *param, const struct HdmiVideoDefInfo *videoInfo,
154     const struct HdmiAudioAttr *audioAttr, enum HdmiColorSpace colorSpace, enum HdmiDeepColor deepColor);
155 bool HdmiDfmFormatSupport(struct HdmiDfmParam *param);
156 
157 #ifdef __cplusplus
158 #if __cplusplus
159 }
160 #endif
161 #endif /* __cplusplus */
162 
163 #endif /* HDMI_DFM_H */
164