]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5_bsp/psu_cortexr5_0/libsrc/avbuf_v2_1/src/xavbuf.c
Update Zynq, MPSoc Cortex-A53 and MPSoc Cortex-R5 demo projects to build with the...
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5_bsp / psu_cortexr5_0 / libsrc / avbuf_v2_1 / src / xavbuf.c
1 /*******************************************************************************
2  *
3  * Copyright (C) 2017 Xilinx, Inc.  All rights reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * Use of the Software is limited solely to applications:
16  * (a) running on a Xilinx device, or
17  * (b) that interact with a Xilinx device through a bus or interconnect.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  * Except as contained in this notice, the name of the Xilinx shall not be used
28  * in advertising or otherwise to promote the sale, use or other dealings in
29  * this Software without prior written authorization from Xilinx.
30  *
31 *******************************************************************************/
32 /******************************************************************************/
33 /**
34  *
35  * @file xavbuf.c
36  *
37  * This file implements all the functions related to the Video Pipeline of the
38  * DisplayPort Subsystem. See xavbuf.h for the detailed description of the
39  * driver.
40  *
41  * @note        None.
42  *
43  * <pre>
44  * MODIFICATION HISTORY:
45  *
46  * Ver   Who  Date     Changes
47  * ----- ---- -------- -----------------------------------------------
48  * 1.0   aad  06/24/17 Initial release.
49  * 2.0   aad  10/08/17 Some APIs to use enums instead of Macros.
50  *                     Some bug fixes.
51  * </pre>
52  *
53 *******************************************************************************/
54 /******************************* Include Files ********************************/
55 #include "xavbuf.h"
56 #include "xstatus.h"
57
58 /**************************** Constant Definitions ****************************/
59 const XAVBuf_VideoAttribute XAVBuf_SupportedFormats[XAVBUF_NUM_SUPPORTED];
60
61 /******************************************************************************/
62 /**
63  * This function sets the scaling factors depending on the source video stream.
64  *
65  * @param       InstancePtr is a pointer to the XAVBuf instance.
66  * @param       RegOffset is the register offset of the SF0 register from the
67  *              DP BaseAddress.
68  * @param       Scaling Factors is a pointer to the scaling factors needed for
69  *              scaling colors to 12 BPC.
70  *
71  * @return      None.
72  *
73  * @note        None.
74  *
75 *******************************************************************************/
76 static void XAVBuf_SetScalingFactors(XAVBuf *InstancePtr, u32 RegOffset,
77                                      u32 *ScalingFactors)
78 {
79         u32 Index = 0;
80         for (Index = 0; Index < 3; Index++) {
81                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
82                                 RegOffset + (Index * 4), ScalingFactors[Index]);
83         }
84
85 }
86
87 /******************************************************************************/
88 /**
89  * This function sets the Layer Control for Video and Graphics layers.
90  *
91  * @param       InstancePtr is a pointer to the XAVBuf instance.
92  * @param       RegOffset is the register offset of Video Layer or Graphics
93  *              Layer from the base address
94  * @param       Video is a pointer to the XAVBuf_VideoAttribute struct which
95  *              has been configured for the particular layer
96  *
97  * @return      None.
98  *
99  * @note        None.
100  *
101 *******************************************************************************/
102 static void XAVBuf_SetLayerControl(XAVBuf *InstancePtr, u32 RegOffset,
103                                            XAVBuf_VideoAttribute *Video)
104 {
105         u32 RegVal = 0;
106
107         RegVal = (Video->IsRGB <<
108                   XAVBUF_V_BLEND_LAYER0_CONTROL_RGB_MODE_SHIFT) |
109                   Video->SamplingEn;
110
111         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, RegOffset, RegVal);
112 }
113
114 /******************************************************************************/
115 /**
116  * This function applies Attributes for Live source(Video/Graphics).
117  *
118  * @param       InstancePtr is an pointer to the XAVBuf Instance.
119  * @param       RegConfig is a register offset for Video or Graphics config
120  *              register
121  * @param       Video is a pointer to the attributes of the video to be applied
122  *
123  * @return      None.
124  *
125  * @note        Live source can be live Video or Live Graphics.
126 ******************************************************************************/
127 static void XAVBuf_SetLiveVideoAttributes(XAVBuf *InstancePtr, u32 RegConfig,
128                                                 XAVBuf_VideoAttribute *Video)
129 {
130         u32 RegVal = 0;
131
132         RegVal |= Video->Value << XAVBUF_BUF_LIVE_VID_CFG_FORMAT_SHIFT;
133         RegVal |= Video->BPP/6 - 3;
134         RegVal |= Video->Swap << XAVBUF_BUF_LIVE_VID_CFG_CB_FIRST_SHIFT;
135         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, RegConfig, RegVal);
136 }
137
138 /******************************************************************************/
139 /**
140  * This function applies Attributes for Non - Live source(Video/Graphics).
141  *
142  * @param       InstancePtr is an pointer to the XAVBuf Instance.
143  * @param       VideoSrc is the source of the Non-Live Video
144  * @param       Video is a pointer to the attributes of the video to be applied
145  *
146  * @return      None.
147  *
148  * @note        Non Live source can be Non Live Video or Non Live Graphics.
149 ******************************************************************************/
150 static void XAVBuf_SetNonLiveVideoAttributes(XAVBuf *InstancePtr, u32 VideoSrc,
151                                              XAVBuf_VideoAttribute *Video)
152 {
153         u32 RegVal = 0;
154
155         RegVal = XAVBuf_ReadReg(InstancePtr->Config.BaseAddr,
156                                                         XAVBUF_BUF_FORMAT);
157         if(VideoSrc == XAVBUF_VIDSTREAM1_NONLIVE) {
158                 RegVal &= ~XAVBUF_BUF_FORMAT_NL_VID_FORMAT_MASK;
159                 RegVal |= Video->Value;
160         }
161         else if (VideoSrc == XAVBUF_VIDSTREAM2_NONLIVE_GFX) {
162                 RegVal &= ~XAVBUF_BUF_FORMAT_NL_GRAPHX_FORMAT_MASK;
163                 RegVal |= (Video->Value) <<
164                         XAVBUF_BUF_FORMAT_NL_GRAPHX_FORMAT_SHIFT;
165         }
166
167         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_BUF_FORMAT,
168                         RegVal);
169 }
170
171 /******************************************************************************/
172 /**
173  * This function programs the coeffitients for Color Space Conversion.
174  *
175  * @param       InstancePtr is an pointer to the XAVBuf Instance.
176  * @param       RegOffset is a register offset for Video or Graphics config
177  *              register
178  * @param       Video is a pointer to the XAVBuf_Attribute structure
179  *
180  * @return      None.
181  *
182  * @note        None.
183 ******************************************************************************/
184 static void XAVBuf_InConvertToRGB(XAVBuf *InstancePtr, u32 RegOffset,
185                                                 XAVBuf_VideoAttribute *Video)
186 {
187         u16 Index;
188         u16 *CSCMatrix;
189         u16 *OffsetMatrix;
190
191         /* SDTV Coefficients */
192         u16 CSCCoeffs[] = { 0x1000, 0x0000, 0x166F,
193                             0x1000, 0x7A7F, 0x7493,
194                             0x1000, 0x1C5A, 0x0000 };
195         u16 CSCOffset[] = { 0x0000, 0x1800, 0x1800 };
196         u16 RGBCoeffs[] = { 0x1000, 0x0000, 0x0000,
197                             0x0000, 0x1000, 0x0000,
198                             0x0000, 0x0000, 0x1000 };
199         u16 RGBOffset[] = { 0x0000, 0x0000, 0x0000 };
200         if(Video->IsRGB) {
201                 CSCMatrix = RGBCoeffs;
202                 OffsetMatrix = RGBOffset;
203         }
204         else {
205                 CSCMatrix = CSCCoeffs;
206                 OffsetMatrix = CSCOffset;
207         }
208         /* Program Colorspace conversion coefficients */
209         for (Index = 9; Index < 12; Index++) {
210                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
211                                 RegOffset + (Index * 4),
212                                 OffsetMatrix[Index - 9]);
213         }
214
215         /* Program Colorspace conversion matrix */
216         for (Index = 0; Index < 9; Index++) {
217                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
218                                 RegOffset + (Index * 4), CSCMatrix[Index]);
219         }
220
221 }
222
223 /******************************************************************************/
224 /**
225  * This function converts the Blender output to the desired output format.
226  *
227  * @param       InstancePtr is an pointer to the XAVBuf Instance.
228  * @param       RegConfig is a register offset for Video or Graphics config
229  *              register
230  * @param       Video is a pointer to the XAVBuf_Attribute structure
231  *
232  * @return      None.
233  *
234  * @note        None.
235 ******************************************************************************/
236 static void XAVBuf_InConvertToOutputFormat(XAVBuf *InstancePtr,
237                                                 XAVBuf_VideoAttribute *Video)
238
239 {       u32 Index = 0;
240         u32 RegOffset = XAVBUF_V_BLEND_RGB2YCBCR_COEFF0;
241         u32 ColorOffset = XAVBUF_V_BLEND_LUMA_OUTCSC_OFFSET;
242         u8 Value = Video->Value;
243         u16 *MatrixCoeff;
244         u16 *MatrixOffset;
245
246         /* SDTV Coeffitients */
247         u16 CSCCoeffs[] = { 0x04C8, 0x0964, 0x01D3,
248                             0x7D4C, 0x7AB4, 0x0800,
249                             0x0800, 0x7945, 0x7EB5 };
250         u16 CSCOffset[] = { 0x0000, 0x800, 0x800 };
251         u16 RGBCoeffs[] = { 0x1000, 0x0000, 0x0000,
252                             0x0000, 0x1000, 0x0000,
253                             0x0000, 0x0000, 0x1000 };
254         u16 RGBOffset[] = { 0x0000, 0x0000, 0x0000 };
255
256
257         if(Value) {
258                 MatrixCoeff = CSCCoeffs;
259                 MatrixOffset = CSCOffset;
260
261         }
262         else {
263                 MatrixCoeff = RGBCoeffs;
264                 MatrixOffset = RGBOffset;
265         }
266
267         for (Index = 0; Index < 9; Index++) {
268                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
269                         RegOffset + (Index * 4), MatrixCoeff[Index]);
270         }
271         for (Index = 0; Index < 3; Index++) {
272                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
273                         ColorOffset + (Index * 4),
274                         (MatrixOffset[Index] <<
275                         XAVBUF_V_BLEND_LUMA_IN1CSC_OFFSET_POST_OFFSET_SHIFT));
276         }
277 }
278 /******************************************************************************/
279 /**
280  * This function configures the Video Pipeline for the selected source
281  *
282  * @param       InstancePtr is an pointer to the XAVBuf Instance.
283  * @param       VideoSrc is a parameter which indicates which Video Source
284  *              selected
285  *
286  * @return      None.
287  *
288  * @note        None.
289 ******************************************************************************/
290 static int XAVBuf_ConfigureVideo(XAVBuf *InstancePtr, u8 VideoSrc)
291 {
292
293         u32 RegConfig = 0;
294         u32 ScalingOffset = 0;
295         u32 LayerOffset = 0;
296         u32 CSCOffset = 0;
297         XAVBuf_VideoAttribute *Video = NULL;
298         u32 *ScalingFactors = NULL;
299
300         Xil_AssertNonvoid(InstancePtr != NULL);
301         switch(VideoSrc) {
302                 case XAVBUF_VIDSTREAM1_LIVE:
303                         RegConfig = XAVBUF_BUF_LIVE_VID_CFG;
304                         ScalingOffset = XAVBUF_BUF_LIVE_VID_COMP0_SF;
305                         LayerOffset = XAVBUF_V_BLEND_LAYER0_CONTROL;
306                         CSCOffset = XAVBUF_V_BLEND_IN1CSC_COEFF0;
307                         Video = InstancePtr->AVMode.LiveVideo;
308                         ScalingFactors = Video->SF;
309
310                         /* Set the Video Attributes */
311                         XAVBuf_SetLiveVideoAttributes(InstancePtr, RegConfig,
312                                                       Video);
313                         break;
314
315                 case XAVBUF_VIDSTREAM2_LIVE_GFX:
316                         RegConfig = XAVBUF_BUF_LIVE_GFX_CFG;
317                         ScalingOffset = XAVBUF_BUF_LIVE_GFX_COMP0_SF;
318                         LayerOffset = XAVBUF_V_BLEND_LAYER1_CONTROL;
319                         CSCOffset = XAVBUF_V_BLEND_IN2CSC_COEFF0;
320                         Video = InstancePtr->AVMode.LiveGraphics;
321                         ScalingFactors = Video->SF;
322
323                         /* Set the Video Attributes */
324                         XAVBuf_SetLiveVideoAttributes(InstancePtr, RegConfig,
325                                                       Video);
326                         break;
327
328                 case XAVBUF_VIDSTREAM1_NONLIVE:
329                         RegConfig = XAVBUF_BUF_LIVE_GFX_CFG;
330                         ScalingOffset = XAVBUF_BUF_VID_COMP0_SCALE_FACTOR;
331                         LayerOffset = XAVBUF_V_BLEND_LAYER0_CONTROL;
332                         CSCOffset = XAVBUF_V_BLEND_IN1CSC_COEFF0;
333                         Video = InstancePtr->AVMode.NonLiveVideo;
334                         ScalingFactors = Video->SF;
335
336                         /* Set the Video Attributes */
337                         XAVBuf_SetNonLiveVideoAttributes(InstancePtr, VideoSrc,
338                                                          Video);
339                         break;
340
341                 case XAVBUF_VIDSTREAM2_NONLIVE_GFX:
342                         RegConfig = XAVBUF_BUF_LIVE_GFX_CFG;
343                         ScalingOffset = XAVBUF_BUF_GRAPHICS_COMP0_SCALE_FACTOR;
344                         LayerOffset = XAVBUF_V_BLEND_LAYER1_CONTROL;
345                         CSCOffset = XAVBUF_V_BLEND_IN2CSC_COEFF0;
346                         Video = InstancePtr->AVMode.NonLiveGraphics;
347                         ScalingFactors = Video->SF;
348
349                         /* Set the Video Attributes */
350                         XAVBuf_SetNonLiveVideoAttributes(InstancePtr, VideoSrc,
351                                                          Video);
352                         break;
353                 case XAVBUF_VIDSTREAM1_TPG:
354                         RegConfig |= 1 <<
355                                 XAVBUF_V_BLEND_LAYER0_CONTROL_RGB_MODE_SHIFT;
356                         RegConfig |= 1 <<
357                                 XAVBUF_V_BLEND_LAYER0_CONTROL_BYPASS_SHIFT;
358                         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
359                                         XAVBUF_V_BLEND_LAYER0_CONTROL,
360                                         RegConfig);
361                         break;
362                 default:
363                         return XST_FAILURE;
364         }
365         /* Setting the scaling factors */
366         XAVBuf_SetScalingFactors(InstancePtr, ScalingOffset, ScalingFactors);
367         /* Layer Control */
368         XAVBuf_SetLayerControl(InstancePtr, LayerOffset, Video);
369         /* Colorspace conversion */
370         XAVBuf_InConvertToRGB(InstancePtr, CSCOffset, Video);
371         return XST_SUCCESS;
372 }
373
374 /******************************************************************************/
375 /**
376  * This function intializes the configuration for the AVBuf Instance.
377  *
378  * @param       InstancePtr is a pointer to the XAVBuf instance.
379  * @param       BaseAddr sets the base address of the AVBuf instance
380  * @param       Deviceid is the id of the device from the design.
381  *
382  * @return      None.
383  *
384  * @note        Base address and DeviceId is same as the DP Core driver.
385  *
386 *******************************************************************************/
387 void XAVBuf_CfgInitialize(XAVBuf *InstancePtr, u32 BaseAddr, u16 DeviceId)
388 {
389         Xil_AssertVoid(InstancePtr != NULL);
390
391         InstancePtr->Config.DeviceId = DeviceId;
392         InstancePtr->Config.BaseAddr = BaseAddr;
393 }
394
395 /******************************************************************************/
396 /**
397  * This function initializes all the data structures of the XAVBuf Instance.
398  *
399  * @param       InstancePtr is a pointer to the XAVBuf instance.
400  *
401  * @return      None.
402  *
403  * @note        None.
404  *
405 *******************************************************************************/
406 void XAVBuf_Initialize(XAVBuf *InstancePtr)
407 {
408         Xil_AssertVoid(InstancePtr != NULL);
409
410         InstancePtr->AVMode.NonLiveVideo = NULL;
411         InstancePtr->AVMode.LiveVideo = NULL;
412         InstancePtr->AVMode.LiveGraphics = NULL;
413         InstancePtr->AVMode.NonLiveGraphics = NULL;
414         InstancePtr->AVMode.VideoSrc = XAVBUF_VIDSTREAM1_NONE;
415         InstancePtr->AVMode.GraphicsSrc = XAVBUF_VIDSTREAM2_NONE;
416         InstancePtr->AVMode.Audio = NULL;
417         InstancePtr->AVMode.GraphicsAudio = NULL;
418         InstancePtr->AVMode.AudioSrc1 = XAVBUF_AUDSTREAM1_NO_AUDIO;
419         InstancePtr->AVMode.AudioSrc2 = XAVBUF_AUDSTREAM2_NO_AUDIO;
420
421         InstancePtr->Blender.GlobalAlphaEn = 0;
422         InstancePtr->Blender.Alpha = 0;
423         InstancePtr->Blender.OutputVideo = NULL;
424
425         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_AUD_SOFT_RST, 0);
426 }
427
428 /******************************************************************************/
429 /**
430  * This function selects the source for the Video and Graphics streams that are
431  * passed on to the blender block.
432  *
433  * @param       InstancePtr is a pointer to the XAVBuf instance.
434  * @param       VidStream selects the stream coming from the video sources
435  * @param       GfxStream selects the stream coming from the graphics sources
436  *
437  * @return      None.
438  *
439  * @note        None.
440  *
441 *******************************************************************************/
442 void XAVBuf_InputVideoSelect(XAVBuf *InstancePtr, XAVBuf_VideoStream VidStream,
443                               XAVBuf_GfxStream GfxStream)
444 {
445
446
447         Xil_AssertVoid(InstancePtr != NULL);
448         Xil_AssertVoid((VidStream != XAVBUF_VIDSTREAM1_LIVE) |
449                        (VidStream != XAVBUF_VIDSTREAM1_NONLIVE) |
450                        (VidStream != XAVBUF_VIDSTREAM1_TPG) |
451                        (VidStream != XAVBUF_VIDSTREAM1_NONE));
452         Xil_AssertVoid((GfxStream != XAVBUF_VIDSTREAM2_DISABLEGFX) |
453                        (GfxStream != XAVBUF_VIDSTREAM2_NONLIVE_GFX) |
454                        (GfxStream != XAVBUF_VIDSTREAM2_LIVE_GFX) |
455                        (GfxStream != XAVBUF_VIDSTREAM2_NONE));
456
457         InstancePtr->AVMode.VideoSrc = VidStream;
458         InstancePtr->AVMode.GraphicsSrc = GfxStream;
459         u32 RegVal;
460         RegVal = XAVBuf_ReadReg(InstancePtr->Config.BaseAddr,
461                         XAVBUF_BUF_OUTPUT_AUD_VID_SELECT);
462         RegVal &= ~(XAVBUF_BUF_OUTPUT_AUD_VID_SELECT_VID_STREAM2_SEL_MASK |
463                         XAVBUF_BUF_OUTPUT_AUD_VID_SELECT_VID_STREAM1_SEL_MASK);
464         RegVal |= VidStream | GfxStream;
465         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
466                         XAVBUF_BUF_OUTPUT_AUD_VID_SELECT, RegVal);
467 }
468
469 /******************************************************************************/
470 /**
471  * This function sets the video format for the non-live video
472  *
473  * @param       InstancePtr is a pointer to the XAVBuf instance.
474  * @param       Format is the enum for the non-live video format
475  *
476  * @return      XST_SUCCESS if the correct format has been set.
477  *              XST_FAILURE if the format is invalid.
478  *
479  * @note        None.
480 *******************************************************************************/
481 int XAVBuf_SetInputNonLiveVideoFormat(XAVBuf *InstancePtr,
482                                        XAVBuf_VideoFormat Format)
483 {
484         Xil_AssertNonvoid(InstancePtr != NULL);
485         Xil_AssertNonvoid((Format >= CbY0CrY1) | (Format <= YV16Ci2_420_10BPC));
486
487         InstancePtr->AVMode.NonLiveVideo =
488                 XAVBuf_GetNLiveVideoAttribute(Format);
489         if(InstancePtr->AVMode.NonLiveVideo == NULL)
490                 return XST_FAILURE;
491
492         return XST_SUCCESS;
493 }
494
495 /******************************************************************************/
496 /**
497  * This function sets the graphics format for the non-live video
498  *
499  * @param       InstancePtr is a pointer to the XAVBuf instance.
500  * @param       Format is the enum for the non-live video format
501  *
502  * @return      XST_SUCCESS if the correct format has been set.
503  *              XST_FAILURE if the format is invalid.
504  *
505  * @note        None.
506 *******************************************************************************/
507 int XAVBuf_SetInputNonLiveGraphicsFormat(XAVBuf *InstancePtr,
508                                        XAVBuf_VideoFormat Format)
509 {
510         Xil_AssertNonvoid(InstancePtr != NULL);
511         Xil_AssertNonvoid((Format >= RGBA8888) |( Format <= YOnly));
512
513         InstancePtr->AVMode.NonLiveGraphics =
514                 XAVBuf_GetNLGraphicsAttribute(Format);
515         if(InstancePtr->AVMode.NonLiveGraphics == NULL)
516                 return XST_FAILURE;
517
518         return XST_SUCCESS;
519 }
520
521 /******************************************************************************/
522 /**
523  * This function sets the video format for the live video
524  *
525  * @param       InstancePtr is a pointer to the XAVBuf instance.
526  * @param       Format is the enum for the non-live video format
527  *
528  * @return      XST_SUCCESS if the correct format has been set.
529  *              XST_FAILURE if the format is invalid.
530  *
531  * @note        None.
532 *******************************************************************************/
533 int XAVBuf_SetInputLiveVideoFormat(XAVBuf *InstancePtr,
534                                        XAVBuf_VideoFormat Format)
535 {
536         Xil_AssertNonvoid(InstancePtr != NULL);
537         Xil_AssertNonvoid((Format >= RGB_6BPC) | (Format <= YOnly_12BPC));
538
539         InstancePtr->AVMode.LiveVideo = XAVBuf_GetLiveVideoAttribute(Format);
540         if(InstancePtr->AVMode.LiveVideo == NULL)
541                 return XST_FAILURE;
542
543         return XST_SUCCESS;
544 }
545
546 /******************************************************************************/
547 /**
548  * This function sets the graphics format for the live video
549  *
550  * @param       InstancePtr is a pointer to the XAVBuf instance.
551  * @param       Format is the enum for the non-live video format
552  *
553  * @return      XST_SUCCESS if the correct format has been set.
554  *              XST_FAILURE if the format is invalid.
555  *
556  * @note        None.
557 *******************************************************************************/
558 int XAVBuf_SetInputLiveGraphicsFormat(XAVBuf *InstancePtr,
559                                        XAVBuf_VideoFormat Format)
560 {
561         Xil_AssertNonvoid(InstancePtr != NULL);
562         Xil_AssertNonvoid((Format >= RGB_6BPC) | (Format <= YOnly_12BPC));
563
564         InstancePtr->AVMode.LiveGraphics =
565                 XAVBuf_GetLiveVideoAttribute(Format);
566         if(InstancePtr->AVMode.LiveGraphics == NULL)
567                 return XST_FAILURE;
568
569         return XST_SUCCESS;
570 }
571
572 /******************************************************************************/
573 /**
574  * This function sets the Output Video Format
575  *
576  * @param       InstancePtr is a pointer to the XAVBuf instance.
577  * @param       Format is the enum for the non-live video format
578  *
579  * @return      XST_SUCCESS if the correct format has been set.
580  *              XST_FAILURE if the format is invalid.
581  *
582  * @note        None.
583 *******************************************************************************/
584 int XAVBuf_SetOutputVideoFormat(XAVBuf *InstancePtr, XAVBuf_VideoFormat Format)
585 {
586         Xil_AssertNonvoid(InstancePtr != NULL);
587         Xil_AssertNonvoid((Format >= RGB_6BPC) | (Format <= YOnly_12BPC));
588
589         InstancePtr->Blender.OutputVideo =
590                 XAVBuf_GetLiveVideoAttribute(Format);
591         if(InstancePtr->Blender.OutputVideo == NULL)
592                 return XST_FAILURE;
593         else
594                 return XST_SUCCESS;
595 }
596
597 /******************************************************************************/
598 /**
599  * This function sets the Audio and Video Clock Source and the video timing
600  * source.
601  *
602  * @param       InstancePtr is a pointer to the XAVBuf instance.
603  * @param       VideoClk selects the Video Clock Source
604  * @param       AudioClk selects the Audio Clock Source
605  *
606  * @return      None.
607  *
608  * @note        System uses PL Clock for Video when Live source is in use.
609  *
610 *******************************************************************************/
611 void XAVBuf_SetAudioVideoClkSrc(XAVBuf *InstancePtr, u8 VideoClk, u8 AudioClk)
612 {
613
614         u32 RegVal = 0;
615
616         Xil_AssertVoid(InstancePtr != NULL);
617         Xil_AssertVoid((VideoClk != XAVBUF_PS_CLK) |
618                         (VideoClk!= XAVBUF_PL_CLK));
619         Xil_AssertVoid((AudioClk != XAVBUF_PS_CLK) |
620                        (AudioClk!= XAVBUF_PL_CLK));
621
622         if((InstancePtr->AVMode.VideoSrc != XAVBUF_VIDSTREAM1_LIVE) &&
623            (InstancePtr->AVMode.GraphicsSrc != XAVBUF_VIDSTREAM2_LIVE_GFX)) {
624                 RegVal = 1 <<
625                         XAVBUF_BUF_AUD_VID_CLK_SOURCE_VID_TIMING_SRC_SHIFT;
626         }
627         else if((InstancePtr->AVMode.VideoSrc == XAVBUF_VIDSTREAM1_LIVE) ||
628                 (InstancePtr->AVMode.GraphicsSrc ==
629                  XAVBUF_VIDSTREAM2_LIVE_GFX)) {
630                 VideoClk = XAVBUF_PL_CLK;
631         }
632
633         RegVal |= (VideoClk <<
634                         XAVBUF_BUF_AUD_VID_CLK_SOURCE_VID_CLK_SRC_SHIFT) |
635                 (AudioClk <<
636                         XAVBUF_BUF_AUD_VID_CLK_SOURCE_AUD_CLK_SRC_SHIFT);
637         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
638                         XAVBUF_BUF_AUD_VID_CLK_SOURCE, RegVal);
639         /*Soft Reset VideoPipeline when changing the clock source*/
640         XAVBuf_SoftReset(InstancePtr);
641 }
642
643 /******************************************************************************/
644 /**
645  * This function applies a soft reset to the Audio Video pipeline.
646  *
647  * @param       InstancePtr is a pointer to the XAVBuf instance.
648  *
649  * @return      None.
650  *
651  * @note        None.
652  *
653 *******************************************************************************/
654 void XAVBuf_SoftReset(XAVBuf *InstancePtr)
655 {
656         Xil_AssertVoid(InstancePtr != NULL);
657
658         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_BUF_SRST_REG,
659                                         XAVBUF_BUF_SRST_REG_VID_RST_MASK);
660         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_BUF_SRST_REG, 0);
661 }
662
663 /******************************************************************************/
664 /**
665  * This function looks up if the video format is valid or not for the non-live
666  * video datapath and returns a pointer to the attributes of the video.
667  *
668  * @param       Format takes in the video format for which attributes are being
669  *              requested.
670  *
671  * @return      A pointer to the structure XAVBuf_VideoAttribute if the video
672  *              format is valid, else returns NULL.
673  * @note        None.
674 *******************************************************************************/
675 XAVBuf_VideoAttribute *XAVBuf_GetLiveVideoAttribute(XAVBuf_VideoFormat Format)
676 {
677         u8 Index = 0;
678         XAVBuf_VideoAttribute *VideoAttribute;
679         Xil_AssertNonvoid((Format >= RGB_6BPC) |  (Format <= YOnly_12BPC));
680
681         for (Index = RGB_6BPC; Index <= YOnly_12BPC; Index++) {
682                 VideoAttribute = (XAVBuf_VideoAttribute *)
683                                         &XAVBuf_SupportedFormats[Index];
684                 if(Format == VideoAttribute->VideoFormat) {
685                         return VideoAttribute;
686                 }
687         }
688         return NULL;
689 }
690
691 /******************************************************************************/
692 /**
693  * This function looks up if the video format is valid or not and returns a
694  * pointer to the attributes of the video.
695  *
696  * @param       Format takes in the video format for which attributes are being
697  *              requested.
698  *
699  * @return      A pointer to the structure XAVBuf_VideoAttribute if the video
700  *              format is valid, else returns NULL.
701  * @note        None.
702 *******************************************************************************/
703 XAVBuf_VideoAttribute *XAVBuf_GetNLiveVideoAttribute(XAVBuf_VideoFormat Format)
704 {
705         u8 Index = 0;
706         XAVBuf_VideoAttribute *VideoAttribute;
707
708         Xil_AssertNonvoid((Format >= CbY0CrY1) | (Format <= YV16Ci2_420_10BPC));
709
710         for (Index = CbY0CrY1; Index <= YV16Ci2_420_10BPC; Index++) {
711                 VideoAttribute = (XAVBuf_VideoAttribute *)
712                                         &XAVBuf_SupportedFormats[Index];
713                 if(Format == VideoAttribute->VideoFormat) {
714                         return VideoAttribute;
715                 }
716         }
717         return NULL;
718 }
719
720 /******************************************************************************/
721 /**
722  * This function looks up if the video format is valid or not and returns a
723  * pointer to the attributes of the video.
724  *
725  * @param       Format takes in the video format for which attributes are being
726  *              requested.
727  *
728  * @return      A pointer to the structure XAVBuf_VideoAttribute if the video
729  *              format is valid, else returns NULL.
730  * @note        None.
731 *******************************************************************************/
732 XAVBuf_VideoAttribute *XAVBuf_GetNLGraphicsAttribute(XAVBuf_VideoFormat Format)
733 {
734         u32 Index = 0;
735         XAVBuf_VideoAttribute *VideoAttribute;
736
737         Xil_AssertNonvoid((Format >= RGBA8888) | (Format <= YOnly));
738
739         for(Index = RGBA8888; Index <= YOnly; Index++) {
740                 VideoAttribute = (XAVBuf_VideoAttribute *)
741                                         &XAVBuf_SupportedFormats[Index];
742                 if(Format == VideoAttribute->VideoFormat) {
743                         return VideoAttribute;
744                 }
745         }
746         return NULL;
747 }
748
749 /******************************************************************************/
750 /**
751  * This function configures the Video Pipeline
752  *
753  * @param       InstancePtr is an pointer to the XAVBuf Instance.
754  *
755  * @return      None.
756  *
757  * @note        None.
758 ******************************************************************************/
759 void XAVBuf_ConfigureVideoPipeline(XAVBuf *InstancePtr)
760 {
761         Xil_AssertVoid(InstancePtr != NULL);
762
763         XAVBuf_ConfigureVideo(InstancePtr, InstancePtr->AVMode.VideoSrc);
764 }
765
766 /******************************************************************************/
767 /**
768  * This function configures the Graphics Pipeline
769  *
770  * @param       InstancePtr is an pointer to the XAVBuf Instance.
771  *
772  * @return      None.
773  *
774  * @note        None.
775 ******************************************************************************/
776 void XAVBuf_ConfigureGraphicsPipeline(XAVBuf *InstancePtr)
777 {
778         Xil_AssertVoid(InstancePtr != NULL);
779         XAVBuf_ConfigureVideo(InstancePtr, InstancePtr->AVMode.GraphicsSrc);
780 }
781
782 /******************************************************************************/
783 /**
784  * This function sets the blender background color
785  *
786  * @param       InstancePtr is an pointer to the XAVBuf Instance.
787  * @param       Color is a pointer to the structure XAVBuf_BlenderBgClr
788  *
789  * @return      None.
790  *
791  * @note        None.
792 ******************************************************************************/
793 void XAVBuf_BlendSetBgColor(XAVBuf *InstancePtr, XAVBuf_BlenderBgClr *Color)
794 {
795         Xil_AssertVoid(InstancePtr != NULL);
796         Xil_AssertVoid(Color != NULL);
797
798         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_V_BLEND_BG_CLR_0,
799                         Color->RCr);
800         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_V_BLEND_BG_CLR_1,
801                         Color->GY);
802         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_V_BLEND_BG_CLR_2,
803                         Color->BCb);
804 }
805
806 /******************************************************************************/
807 /**
808  * This function enables or disables global alpha
809  *
810  * @param       InstancePtr is an pointer to the XAVBuf Instance.
811  * @param       Enable sets a software flag for global alpha
812  * @param       Alpha sets the value for the global alpha blending
813  *
814  * @return      None.
815  *
816  * @note        GlobalAlphaEn = 1, enables the global alpha.
817  *              GlobalAlphaEn = 0, disables the global alpha.
818  *              Alpha = 0, passes stream2
819  *              Alpha = 255, passes stream1
820 ******************************************************************************/
821 void XAVBuf_SetBlenderAlpha(XAVBuf *InstancePtr, u8 Alpha, u8 Enable)
822 {
823         u32 RegVal;
824         Xil_AssertVoid(InstancePtr != NULL);
825         Xil_AssertVoid((Enable !=0) | (Enable != 1));
826
827         InstancePtr->Blender.GlobalAlphaEn = Enable;
828         InstancePtr->Blender.Alpha = Alpha;
829
830         RegVal = Enable;
831         RegVal |= Alpha << XAVBUF_V_BLEND_SET_GLOBAL_ALPHA_REG_VALUE_SHIFT;
832
833         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
834                         XAVBUF_V_BLEND_SET_GLOBAL_ALPHA_REG, RegVal);
835 }
836
837 /******************************************************************************/
838 /**
839  * This function configures the Output of the Video Pipeline
840  *
841  * @param       InstancePtr is an pointer to the XAVBuf Instance.
842  * @param       OutputVideo is a pointer to the XAVBuf_VideoAttribute.
843  *
844  * @return      None.
845  *
846  * @note        None.
847 ******************************************************************************/
848 void XAVBuf_ConfigureOutputVideo(XAVBuf *InstancePtr)
849 {
850         u32 RegVal = 0;
851         XAVBuf_VideoAttribute *OutputVideo = InstancePtr->Blender.OutputVideo;
852
853         RegVal |= OutputVideo->SamplingEn <<
854                         XAVBUF_V_BLEND_OUTPUT_VID_FORMAT_EN_DOWNSAMPLE_SHIFT;
855         RegVal |= OutputVideo->Value;
856         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
857                         XAVBUF_V_BLEND_OUTPUT_VID_FORMAT, RegVal);
858
859         XAVBuf_InConvertToOutputFormat(InstancePtr, OutputVideo);
860 }
861
862 /******************************************************************************/
863 /**
864  * This function selects the source for audio streams corresponding to the
865  * Video and Graphics streams that are passed on to the blender
866  *
867  * @param       InstancePtr is a pointer to the XAVBuf instance.
868  * @param       AudStream1 selects the audio stream source corresponding to
869  *              the video source selected
870  * @param       AudStream2 selects the audio stream source corresponding to
871  *              the graphics source selected.
872  *
873  * @return      None.
874  *
875  * @note        None.
876  *
877 *******************************************************************************/
878 void XAVBuf_InputAudioSelect(XAVBuf *InstancePtr, XAVBuf_AudioStream1 AudStream1,
879                               XAVBuf_AudioStream2 AudStream2)
880 {
881         Xil_AssertVoid(InstancePtr != NULL);
882         Xil_AssertVoid((AudStream1 != XAVBUF_AUDSTREAM1_NONLIVE) |
883                        (AudStream1 != XAVBUF_AUDSTREAM1_LIVE) |
884                        (AudStream1 != XAVBUF_AUDSTREAM1_TPG));
885         Xil_AssertVoid((AudStream2 != XAVBUF_AUDSTREAM2_NO_AUDIO) |
886                        (AudStream2 != XAVBUF_AUDSTREAM2_AUDIOGFX));
887
888         u32 RegVal;
889         RegVal = XAVBuf_ReadReg(InstancePtr->Config.BaseAddr,
890                         XAVBUF_BUF_OUTPUT_AUD_VID_SELECT);
891         RegVal &= ~(XAVBUF_BUF_OUTPUT_AUD_VID_SELECT_AUD_STREAM2_SEL_MASK |
892                         XAVBUF_BUF_OUTPUT_AUD_VID_SELECT_AUD_STREAM1_SEL_MASK);
893         RegVal |= AudStream1 | AudStream2;
894
895         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
896                         XAVBUF_BUF_OUTPUT_AUD_VID_SELECT, RegVal);
897 }
898
899 /******************************************************************************/
900 /**
901  * This function sets up the scaling factor for Audio Mixer Volume Control.
902  *
903  * @param       InstancePtr is a pointer to the XAVBuf instance.
904  * @param       Channel0Volume is the volume to be set for Audio from Channel0
905  * @param       Channel1Volume is the volume to be set for Audio from Channel1
906  *
907  *
908  * @return      None.
909  *
910  * @note        None.
911  *
912 *******************************************************************************/
913 void XAVBuf_AudioMixerVolumeControl(XAVBuf *InstancePtr, u8 Channel0Volume,
914                                         u8 Channel1Volume)
915 {
916         u32 Val;
917         Xil_AssertVoid(InstancePtr != NULL);
918         Val = Channel1Volume <<
919                 XAVBUF_AUD_MIXER_VOLUME_CONTROL_VOL_CTRL_CH1_SHIFT;
920         Val |= Channel0Volume;
921         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
922                         XAVBUF_AUD_MIXER_VOLUME_CONTROL, Val);
923 }
924
925 /******************************************************************************/
926 /**
927  * This function resets the Audio Pipe.
928  *
929  * @param       InstancePtr is a  pointer to the XAVBuf Instance.
930  *
931  * @returns     None.
932  *
933  * @note        Needed when non-live audio is disabled.
934  *
935  *
936  ******************************************************************************/
937 void XAVBuf_AudioSoftReset(XAVBuf *InstancePtr)
938 {
939         u32 RegVal = 0;
940         Xil_AssertVoid(InstancePtr != NULL);
941         RegVal = XAVBuf_ReadReg(InstancePtr->Config.BaseAddr,
942                                 XAVBUF_AUD_SOFT_RST);
943         RegVal |= XAVBUF_AUD_SOFT_RST_AUD_SRST_MASK;
944         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_AUD_SOFT_RST,
945                         RegVal);
946         RegVal &= ~XAVBUF_AUD_SOFT_RST_AUD_SRST_MASK;
947         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_AUD_SOFT_RST, 0);
948
949 }
950
951 /******************************************************************************/
952 /**
953  * This function enables End of Line Reset for reduced blanking resolutions.
954  *
955  * @param       InstancePtr is a  pointer to the XAVBuf Instance.
956  * @param       Disable is to be set while using Reduced Blanking Resolutions.
957  *
958  * @returns     None.
959  *
960  * @note        None.
961  *
962  ******************************************************************************/
963 void XABuf_LineResetDisable(XAVBuf *InstancePtr, u8 Disable)
964 {
965         u32 RegVal = 0;
966         Xil_AssertVoid(InstancePtr != NULL);
967         RegVal = XAVBuf_ReadReg(InstancePtr->Config.BaseAddr,
968                                 XAVBUF_AUD_SOFT_RST);
969         if(Disable)
970                 RegVal |= XAVBUF_AUD_SOFT_RST_LINE_RST_DISABLE_MASK;
971         else
972                 RegVal &= ~XAVBUF_AUD_SOFT_RST_LINE_RST_DISABLE_MASK;
973
974         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_AUD_SOFT_RST,
975                         RegVal);
976 }
977
978 /******************************************************************************/
979 /**
980  * This function enables the video channel interface between the DPDMA and the
981  * AVBuf
982  *
983  * @param       InstancePtr is a  pointer to the XAVBuf Instance.
984  * @param       Enable sets the corresponding buffers.
985  *
986  * @returns     None.
987  *
988  * @note        None.
989  *
990  ******************************************************************************/
991 void XAVBuf_EnableVideoBuffers(XAVBuf *InstancePtr, u8 Enable)
992 {
993         u8 Index;
994         u32 RegVal = 0;
995         u8 NumPlanes = InstancePtr->AVMode.NonLiveVideo->Mode;
996
997         RegVal = (0xF << XAVBUF_CHBUF0_BURST_LEN_SHIFT) |
998                 (XAVBUF_CHBUF0_FLUSH_MASK);
999
1000         for (Index = 0; Index <= NumPlanes; Index++) {
1001                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
1002                                 XAVBUF_CHBUF0 + (Index * 4), RegVal);
1003         }
1004         if(Enable) {
1005                 RegVal = (0xF << XAVBUF_CHBUF0_BURST_LEN_SHIFT) |
1006                 XAVBUF_CHBUF0_EN_MASK;
1007                 for (Index = 0; Index <= NumPlanes; Index++) {
1008                         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr,
1009                                         XAVBUF_CHBUF0 + (Index * 4), RegVal);
1010                 }
1011         }
1012 }
1013 /******************************************************************************/
1014 /**
1015  * This function enables the graphics interface between the DPDMA and the AVBuf.
1016  *
1017  * @param       InstancePtr is a  pointer to the XAVBuf Instance.
1018  * @param       Enable sets the corresponding buffers.
1019  *
1020  * @returns     None.
1021  *
1022  * @note        None.
1023  *
1024  ******************************************************************************/
1025 void XAVBuf_EnableGraphicsBuffers(XAVBuf *InstancePtr, u8 Enable)
1026 {
1027         u32 RegVal = 0;
1028
1029         RegVal = (0xF << XAVBUF_CHBUF3_BURST_LEN_SHIFT) |
1030                 XAVBUF_CHBUF3_FLUSH_MASK;
1031         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_CHBUF3, RegVal);
1032         if(Enable) {
1033                 RegVal = (0xF << XAVBUF_CHBUF3_BURST_LEN_SHIFT) |
1034                         XAVBUF_CHBUF0_EN_MASK;
1035                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_CHBUF3,
1036                                 RegVal);
1037         }
1038 }
1039
1040 /******************************************************************************/
1041 /**
1042  * This function enables the audio interface between the DPDMA and the AVBuf
1043  *
1044  * @param       InstancePtr is a  pointer to the XAVBuf Instance.
1045  * @param       Enable sets the corresponding buffers.
1046  *
1047  * @returns     None.
1048  *
1049  * @note        None.
1050  *
1051  ******************************************************************************/
1052 void XAVBuf_EnableAudio0Buffers(XAVBuf *InstancePtr, u8 Enable)
1053 {
1054         u32 RegVal = 0;
1055
1056         RegVal = (0x3 << XAVBUF_CHBUF4_BURST_LEN_SHIFT) |
1057                 XAVBUF_CHBUF4_FLUSH_MASK;
1058         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_CHBUF4, RegVal);
1059         if(Enable) {
1060                 RegVal = (0x3 << XAVBUF_CHBUF4_BURST_LEN_SHIFT) |
1061                         XAVBUF_CHBUF4_EN_MASK;
1062                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_CHBUF4,
1063                                 RegVal);
1064         }
1065 }
1066
1067 /******************************************************************************/
1068 /**
1069  * This function enables the audio interface between the DPDMA and the AVBuf
1070  *
1071  * @param       InstancePtr is a  pointer to the XAVBuf Instance.
1072  * @param       Enable sets the corresponding buffers.
1073  *
1074  * @returns     None.
1075  *
1076  * @note        None.
1077  *
1078  ******************************************************************************/
1079 void XAVBuf_EnableAudio1Buffers(XAVBuf *InstancePtr, u8 Enable)
1080 {
1081         u32 RegVal = 0;
1082
1083         RegVal = (0x3 << XAVBUF_CHBUF5_BURST_LEN_SHIFT) |
1084                 XAVBUF_CHBUF5_FLUSH_MASK;
1085         XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_CHBUF5, RegVal);
1086         if(Enable) {
1087                 RegVal = (0x3 << XAVBUF_CHBUF5_BURST_LEN_SHIFT) |
1088                         XAVBUF_CHBUF5_EN_MASK;
1089                 XAVBuf_WriteReg(InstancePtr->Config.BaseAddr, XAVBUF_CHBUF5,
1090                                 RegVal);
1091         }
1092 }