xgi_videohw.c revision dfe64dd3
1/* Copyright (C) 2003-2006 by XGI Technology, Taiwan.
2 *
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation on the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT.  IN NO EVENT SHALL XGI AND/OR
21 *  ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27/****************************************************************************
28* raw register access : these routines directly interact with the xgi's
29*                       control aperature.  must not be called until after
30*                       the board's pci memory has been mapped.
31****************************************************************************/
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37#define XGI_VIDEO_HW /* avoid compile error in xgi.h; weird!  */
38#include "xgi.h"
39
40#include "xgi_videohw.h"
41#include "xgi_video.h"
42#include "fourcc.h"
43
44#define  CAPTURE_340A1
45/*
46static CARD32 _XGIRead(XGIPtr pXGI, CARD32 reg)
47{
48    return *(pXGI->IOBase + reg);
49}
50
51static void _XGIWrite(XGIPtr pXGI, CARD32 reg, CARD32 data)
52{
53    *(pXGI->IOBase + reg) = data;
54}
55*/
56static CARD8 GetVideoReg(XGIPtr pXGI, CARD8 reg)
57{
58    outb (pXGI->RelIO + vi_index_offset, reg);
59    return inb(pXGI->RelIO + vi_data_offset);
60}
61
62static void SetVideoReg(XGIPtr pXGI, CARD8 reg, CARD8 data)
63{
64    outb (pXGI->RelIO + vi_index_offset, reg);
65    outb (pXGI->RelIO + vi_data_offset, data);
66}
67
68static void SetVideoRegMask(XGIPtr pXGI, CARD8 reg, CARD8 data, CARD8 mask)
69{
70    CARD8   old;
71
72    outb (pXGI->RelIO + vi_index_offset, reg);
73    old = inb(pXGI->RelIO + vi_data_offset);
74    data = (data & mask) | (old & (~mask));
75    outb (pXGI->RelIO + vi_data_offset, data);
76}
77
78static CARD8 GetSRReg(XGIPtr pXGI, CARD8 reg)
79{
80    outb (pXGI->RelIO + sr_index_offset, 0x05);
81    if (inb (pXGI->RelIO + sr_data_offset) != 0xa1)
82            outb (pXGI->RelIO + sr_data_offset, 0x86);
83    outb (pXGI->RelIO + sr_index_offset, reg);
84    return inb(pXGI->RelIO + sr_data_offset);
85}
86
87static void SetSRReg(XGIPtr pXGI, CARD8 reg, CARD8 data)
88{
89    outb (pXGI->RelIO + sr_index_offset, 0x05);
90    if (inb (pXGI->RelIO + sr_data_offset) != 0xa1)
91            outb (pXGI->RelIO + sr_data_offset, 0x86);
92    outb (pXGI->RelIO + sr_index_offset, reg);
93    outb (pXGI->RelIO + sr_data_offset, data);
94}
95
96void SetSRRegMask(XGIPtr pXGI, CARD8 reg, CARD8 data, CARD8 mask)
97{
98    CARD8   old;
99
100    outb (pXGI->RelIO + sr_index_offset, 0x05);
101    if (inb (pXGI->RelIO + sr_data_offset) != 0xa1)
102            outb (pXGI->RelIO + sr_data_offset, 0x86);
103    outb (pXGI->RelIO + sr_index_offset, reg);
104    old = inb(pXGI->RelIO + sr_data_offset);
105    data = (data & mask) | (old & (~mask));
106    outb (pXGI->RelIO + sr_data_offset, data);
107}
108/*
109static void SetVCReg(XGIPtr pXGI, CARD8 reg, CARD8 data)
110{
111    outb (pXGI->RelIO + vc_index_offset, reg);
112    outb (pXGI->RelIO + vc_data_offset, data);
113}
114*/
115static void SetVCRegMask(XGIPtr pXGI, CARD8 reg, CARD8 data, CARD8 mask)
116{
117    CARD8   old;
118
119    outb (pXGI->RelIO + vc_index_offset, reg);
120    old = inb(pXGI->RelIO + vc_data_offset);
121    data = (data & mask) | (old & (~mask));
122    outb (pXGI->RelIO + vc_data_offset, data);
123}
124/*
125static CARD8 GetXGIReg(XGIPtr pXGI, CARD8 index_offset, CARD8 reg)
126{
127    outb (pXGI->RelIO + index_offset, reg);
128    return inb(pXGI->RelIO + index_offset+1);
129}
130
131static void SetXGIReg(XGIPtr pXGI, CARD8 index_offset, CARD8 reg, CARD8 data)
132{
133    outb (pXGI->RelIO + index_offset, reg);
134    outb (pXGI->RelIO + index_offset+1, data);
135}
136*/
137static float tap_dda_func(float x)
138{
139    double pi = 3.14159265358979;
140    float r = 0.5;
141    float y;
142
143    if (x == 0)
144            y = 1.0;
145    else if ((x == -1.0/(2.0*r)) || (x == 1.0/(2.0*r)))
146            y = (float) (r/2.0 * sin(pi/(2.0*r)));
147    else
148            y = (float) (sin(pi*x)/(pi*x)*cos(r*pi*x)/(1-4*r*r*x*x));
149
150    return y;
151}
152
153/* ----------------T05_EnableCapture()---------------- */
154#ifdef CAPTURE_340A1
155void SetEnableCaptureReg(XGIPtr pXGI, Bool bEnable, Bool bFlip)
156{
157   if (bEnable)
158   {
159      SetVCRegMask(pXGI, Index_VC_Ver_Down_Scale_Factor_Over, 0x00, 0x10);
160
161      if (pXGI->Chipset == PCI_CHIP_XGIXG40)
162         SetVideoRegMask(pXGI, Index_VI_Key_Overlay_OP, 0x20, 0x20);
163      else
164         SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x01, 0x01);
165   }
166   else
167   {
168      SetVCRegMask(pXGI, Index_VC_Ver_Down_Scale_Factor_Over, 0x10, 0x10);
169
170      if (pXGI->Chipset == PCI_CHIP_XGIXG40)
171         SetVideoRegMask(pXGI, Index_VI_Key_Overlay_OP, 0x00, 0x20);
172      else
173         SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x01);
174   }
175
176
177   if (pXGI->Chipset == PCI_CHIP_XGIXG40)
178   {
179      if(bFlip)
180         SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x01, 0x01);
181      else
182         SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x01);
183   }
184}
185#else
186void T05_EnableCapture(XGIPtr pXGI, Bool bEnable)
187{
188   if (bEnable)
189   {
190      SetVCRegMask(pXGI, Index_VC_Ver_Down_Scale_Factor_Over, 0x00, 0x10);
191      SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x01, 0x01);
192   }
193   else
194   {
195      SetVCRegMask(pXGI, Index_VC_Ver_Down_Scale_Factor_Over, 0x10, 0x10);
196      SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x01);
197   }
198}
199#endif
200
201static void
202XGIComputeXvGamma(XGIPtr pXGI)
203{
204    int num = 255, i;
205    double red = 1.0 / (double)((double)pXGI->XvGammaRed / 1000);
206    double green = 1.0 / (double)((double)pXGI->XvGammaGreen / 1000);
207    double blue = 1.0 / (double)((double)pXGI->XvGammaBlue / 1000);
208
209    for(i = 0; i <= num; i++) {
210        pXGI->XvGammaRampRed[i] =
211	    (red == 1.0) ? i : (CARD8)(pow((double)i / (double)num, red) * (double)num + 0.5);
212
213		pXGI->XvGammaRampGreen[i] =
214	    (green == 1.0) ? i : (CARD8)(pow((double)i / (double)num, green) * (double)num + 0.5);
215
216		pXGI->XvGammaRampBlue[i] =
217	    (blue == 1.0) ? i : (CARD8)(pow((double)i / (double)num, blue) * (double)num + 0.5);
218    }
219}
220
221static void
222XGISetXvGamma(XGIPtr pXGI)
223{
224    int i;
225    unsigned char backup = GetSRReg(pXGI, Index_SR_Power_Management);
226	/* SR1F[4:3]
227	 * 10: disable gamma1, enable gamma0
228	 */
229    SetSRRegMask(pXGI, Index_SR_Power_Management, 0x08, 0x18);
230    for(i = 0; i <= 255; i++) {
231       MMIO_OUT32(pXGI->IOBase, REG_GAMMA_PALETTE,
232       			(i << 24)     |
233			(pXGI->XvGammaRampBlue[i] << 16) |
234			(pXGI->XvGammaRampGreen[i] << 8) |
235			pXGI->XvGammaRampRed[i]);
236    }
237    SetSRRegMask(pXGI, Index_SR_Power_Management, backup, 0xff);
238}
239
240void
241XGIUpdateXvGamma(XGIPtr pXGI, XGIPortPrivPtr pPriv)
242{
243    unsigned char sr7 = GetSRReg(pXGI, Index_SR_RAMDAC_Ctrl);
244
245    /* SR7 [2]: 24-bit palette RAM and Gamma correction enable */
246    if(!(sr7 & 0x04)) return;
247
248    XGIComputeXvGamma(pXGI);
249    XGISetXvGamma(pXGI);
250}
251
252/* ----------------SetDDAReg()------------------------------------ */
253VOID SetDDAReg (XGIPtr pXGI, float scale)
254{
255    float WW, W[4], tempW[4];
256    int i, j, w, WeightMat[16][4];
257    int *wm1, *wm2, *wm3, *wm4, *temp1, *temp2, *temp3, *temp4;
258
259    for (i=0; i<16; i++)
260    {
261            /* The order of weights are inversed for convolution */
262            W[0] = tap_dda_func((float)((1.0+(i/16.0))/scale));
263            W[1] = tap_dda_func((float)((0.0+(i/16.0))/scale));
264            W[2] = tap_dda_func((float)((-1.0+(i/16.0))/scale));
265            W[3] = tap_dda_func((float)((-2.0+(i/16.0))/scale));
266
267            /* Normalize the weights */
268            WW = W[0]+W[1]+W[2]+W[3];
269
270            /* for rouding */
271            for(j=0; j<4; j++)
272                    tempW[j] = (float)((W[j]/WW*16)+0.5);
273
274            WeightMat[i][0] = (int) tempW[0];
275            WeightMat[i][1] = (int) tempW[1];
276            WeightMat[i][2] = (int) tempW[2];
277            WeightMat[i][3] = (int) tempW[3];
278
279            /* check for display abnormal caused by rounding */
280            w = WeightMat[i][0] + WeightMat[i][1] + WeightMat[i][2] + WeightMat[i][3];
281            if( w != 16 )
282            {
283                    temp1 = ( WeightMat[i][0] > WeightMat[i][1] ) ? &WeightMat[i][0] : &WeightMat[i][1];
284                    temp2 = ( WeightMat[i][0] > WeightMat[i][1] ) ? &WeightMat[i][1] : &WeightMat[i][0];
285                    temp3 = ( WeightMat[i][2] > WeightMat[i][3] ) ? &WeightMat[i][2] : &WeightMat[i][3];
286                    temp4 = ( WeightMat[i][2] > WeightMat[i][3] ) ? &WeightMat[i][3] : &WeightMat[i][2];
287                    wm1 = ( *temp1 > *temp3) ? temp1 : temp3;
288                    wm4 = ( *temp2 > *temp4) ? temp4 : temp2;
289                    wm2 = ( wm1 == temp1 ) ? temp3 : temp1;
290                    wm3 = ( wm4 == temp2 ) ? temp4 : temp2;
291
292                switch(w)
293                {
294                    case 12:
295                        WeightMat[i][0]++;
296                        WeightMat[i][1]++;
297                        WeightMat[i][2]++;
298                        WeightMat[i][3]++;
299                        break;
300
301                    case 13:
302                        (*wm1)++;
303                        (*wm4)++;
304                        if( *wm2 > *wm3 )
305                            (*wm2)++;
306                        else
307                            (*wm3)++;
308                        break;
309
310                    case 14:
311                        (*wm1)++;
312                        (*wm4)++;
313                        break;
314
315                    case 15:
316                        (*wm1)++;
317                        break;
318
319                    case 17:
320                            (*wm4)--;
321                            break;
322
323                    case 18:
324                        (*wm1)--;
325                        (*wm4)--;
326                        break;
327
328                    case 19:
329                        (*wm1)--;
330                        (*wm4)--;
331                        if( *wm2 > *wm3 )
332                            (*wm3)--;
333                        else
334                            (*wm2)--;
335                        break;
336                    case 20:
337                        WeightMat[i][0]--;
338                        WeightMat[i][1]--;
339                        WeightMat[i][2]--;
340                        WeightMat[i][3]--;
341                        break;
342                    default:
343                        /* ErrorF("Invalid WeightMat value!\n"); */
344                        break;
345                }
346            }
347    }
348
349    /* set DDA registers */
350    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_A0, WeightMat[0][0], 0x3F);
351    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_A1, WeightMat[0][1], 0x3F);
352    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_A2, WeightMat[0][2], 0x3F);
353    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_A3, WeightMat[0][3], 0x3F);
354    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_B0, WeightMat[1][0], 0x3F);
355    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_B1, WeightMat[1][1], 0x3F);
356    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_B2, WeightMat[1][2], 0x3F);
357    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_B3, WeightMat[1][3], 0x3F);
358    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_C0, WeightMat[2][0], 0x3F);
359    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_C1, WeightMat[2][1], 0x3F);
360    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_C2, WeightMat[2][2], 0x3F);
361    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_C3, WeightMat[2][3], 0x3F);
362    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_D0, WeightMat[3][0], 0x3F);
363    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_D1, WeightMat[3][1], 0x3F);
364    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_D2, WeightMat[3][2], 0x3F);
365    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_D3, WeightMat[3][3], 0x3F);
366    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_E0, WeightMat[4][0], 0x3F);
367    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_E1, WeightMat[4][1], 0x3F);
368    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_E2, WeightMat[4][2], 0x3F);
369    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_E3, WeightMat[4][3], 0x3F);
370    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_F0, WeightMat[5][0], 0x3F);
371    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_F1, WeightMat[5][1], 0x3F);
372    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_F2, WeightMat[5][2], 0x3F);
373    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_F3, WeightMat[5][3], 0x3F);
374    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_G0, WeightMat[6][0], 0x3F);
375    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_G1, WeightMat[6][1], 0x3F);
376    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_G2, WeightMat[6][2], 0x3F);
377    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_G3, WeightMat[6][3], 0x3F);
378    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_H0, WeightMat[7][0], 0x3F);
379    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_H1, WeightMat[7][1], 0x3F);
380    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_H2, WeightMat[7][2], 0x3F);
381    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_H3, WeightMat[7][3], 0x3F);
382    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_I0, WeightMat[8][0], 0x3F);
383    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_I1, WeightMat[8][1], 0x3F);
384    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_I2, WeightMat[8][2], 0x3F);
385    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_I3, WeightMat[8][3], 0x3F);
386    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_J0, WeightMat[9][0], 0x3F);
387    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_J1, WeightMat[9][1], 0x3F);
388    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_J2, WeightMat[9][2], 0x3F);
389    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_J3, WeightMat[9][3], 0x3F);
390    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_K0, WeightMat[10][0], 0x3F);
391    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_K1, WeightMat[10][1], 0x3F);
392    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_K2, WeightMat[10][2], 0x3F);
393    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_K3, WeightMat[10][3], 0x3F);
394    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_L0, WeightMat[11][0], 0x3F);
395    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_L1, WeightMat[11][1], 0x3F);
396    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_L2, WeightMat[11][2], 0x3F);
397    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_L3, WeightMat[11][3], 0x3F);
398    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_M0, WeightMat[12][0], 0x3F);
399    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_M1, WeightMat[12][1], 0x3F);
400    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_M2, WeightMat[12][2], 0x3F);
401    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_M3, WeightMat[12][3], 0x3F);
402    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_N0, WeightMat[13][0], 0x3F);
403    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_N1, WeightMat[13][1], 0x3F);
404    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_N2, WeightMat[13][2], 0x3F);
405    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_N3, WeightMat[13][3], 0x3F);
406    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_O0, WeightMat[14][0], 0x3F);
407    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_O1, WeightMat[14][1], 0x3F);
408    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_O2, WeightMat[14][2], 0x3F);
409    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_O3, WeightMat[14][3], 0x3F);
410    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_P0, WeightMat[15][0], 0x3F);
411    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_P1, WeightMat[15][1], 0x3F);
412    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_P2, WeightMat[15][2], 0x3F);
413    SetVideoRegMask(pXGI, Index_DDA_Weighting_Matrix_P3, WeightMat[15][3], 0x3F);
414}
415
416void
417XGIResetVideo(ScrnInfoPtr pScrn)
418{
419    XGIPtr pXGI = XGIPTR(pScrn);
420    XGIPortPrivPtr pPriv = GET_PORT_PRIVATE(pScrn);
421
422    /* Reset Xv gamma correction */
423    XGIUpdateXvGamma(pXGI, pPriv);
424
425    if (GetSRReg (pXGI, 0x05) != 0xa1)
426    {
427            SetSRReg (pXGI, 0x05, 0x86);
428            if (GetSRReg (pXGI, 0x05) != 0xa1)
429	    {}
430                    /* xf86DrvMsg(pScrn->scrnIndex, X_ERROR, */
431                    /* "Standard password not initialize\n"); */
432    }
433    if (GetVideoReg (pXGI, Index_VI_Passwd) != 0xa1)
434    {
435            SetVideoReg (pXGI, Index_VI_Passwd, 0x86);
436            if (GetVideoReg (pXGI, Index_VI_Passwd) != 0xa1)
437	    {}
438                    /* xf86DrvMsg(pScrn->scrnIndex, X_ERROR, */
439                    /* "Video password not initialize\n"); */
440    }
441
442    /* Initial Overlay 1 */
443    SetVideoRegMask(pXGI, Index_VI_Control_Misc2, 0x00, 0x81);
444    SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x03);
445    /* Turn on Bob mode and digital video data transform bit */
446    SetVideoRegMask(pXGI, Index_VI_Control_Misc1, 0x82, 0x82);
447    SetVideoRegMask(pXGI, Index_VI_Scale_Control, 0x60, 0x60);
448    SetVideoRegMask(pXGI, Index_VI_Contrast_Enh_Ctrl, 0x04, 0x1F);
449
450    SetVideoReg(pXGI, Index_VI_Disp_Y_Buf_Preset_Low,     0x00);
451    SetVideoReg(pXGI, Index_VI_Disp_Y_Buf_Preset_Middle,  0x00);
452    SetVideoReg(pXGI, Index_VI_Disp_UV_Buf_Preset_Low,    0x00);
453    SetVideoReg(pXGI, Index_VI_Disp_UV_Buf_Preset_Middle, 0x00);
454    SetVideoReg(pXGI, Index_VI_Disp_Y_UV_Buf_Preset_High, 0x00);
455    SetVideoReg(pXGI, Index_VI_Play_Threshold_Low,        0x00);
456    SetVideoRegMask(pXGI, Index_VI_Play_Threshold_Low_Ext, 0x00, 0x01);
457    SetVideoReg(pXGI, Index_VI_Play_Threshold_High,       0x00);
458    SetVideoRegMask(pXGI, Index_VI_Play_Threshold_High_Ext, 0x00, 0x01);
459
460    /* Disable fast page flip */
461    SetSRRegMask(pXGI, Index_SR_CRT_Misc_Ctrl,    0x00,  0x02);
462
463    #ifdef CAPTURE_340A1
464       SetEnableCaptureReg(pXGI, FALSE, FALSE);
465    #else
466       SetEnableCaptureReg(pXGI, FALSE);
467    #endif
468
469    /* Disable video processor */
470    SetSRRegMask(pXGI, Index_Video_Process,        0x00, 0x02);
471
472        /* Enable Horizontal 4-tap DDA mode */
473        SetVideoRegMask(pXGI, Index_VI_Key_Overlay_OP, 0x40, 0x40);
474        /* Disable Vertical 4-tap DDA mode -- not surport not */
475        SetVideoRegMask(pXGI, Index_VI_Key_Overlay_OP, 0x00, 0x80);
476        /* The DDA registers should set scale to 1 as default */
477    SetDDAReg (pXGI, 1.0);
478
479        /*for 341, Init VR 2F [D5] to be 1,to set software flip as default*/
480        SetVideoRegMask(pXGI, Index_VI_Key_Overlay_OP, 0x20, 0x20);
481
482   /*Disable Contrast enhancement*/
483   SetVideoRegMask(pXGI, Index_VI_Key_Overlay_OP, 0x00, 0x10);
484
485   /* Default Color */
486   SetVideoReg(pXGI, Index_VI_Brightness, Default_Brightness);
487   SetVideoRegMask(pXGI, Index_VI_Contrast_Enh_Ctrl, Default_Contrast, 0x07);
488   SetVideoReg(pXGI, Index_VI_Saturation, Default_Saturation);
489   SetVideoRegMask(pXGI, Index_VI_Hue, Default_Hue, 0x07);
490}
491
492void
493SetMergeLineBufReg(XGIPtr pXGI, Bool enable)
494{
495  if (enable) {
496          /* select video 1 and disable All line buffer Merge */
497     SetVideoRegMask(pXGI, Index_VI_Control_Misc2, 0x00, 0x11);
498          /* set Individual Line buffer Merge */
499     SetVideoRegMask(pXGI, Index_VI_Control_Misc1, 0x04, 0x04);
500  }
501  else {
502          /* select video 1 and disable All line buffer Merge */
503          SetVideoRegMask(pXGI, Index_VI_Control_Misc2, 0x00, 0x11);
504          /* disable Individual Line buffer Merge */
505          SetVideoRegMask(pXGI, Index_VI_Control_Misc1, 0x00, 0x04);
506  }
507}
508
509void
510SetVideoFormatReg(XGIPtr pXGI, int format)
511{
512    CARD8 fmt;
513
514    switch (format)
515    {
516       case PIXEL_FMT_YV12:
517            fmt = 0x0c;
518            break;
519
520       case PIXEL_FMT_YUY2:
521            fmt = 0x28;
522            break;
523
524       case PIXEL_FMT_UYVY:
525            fmt = 0x08;
526            break;
527
528	   case PIXEL_FMT_YVYU:
529	   		fmt = 0x38;
530		    break;
531
532	   case PIXEL_FMT_NV12:
533	        fmt = 0x4c;
534		    break;
535
536	   case PIXEL_FMT_NV21:
537	        fmt = 0x5c;
538		    break;
539
540	   case PIXEL_FMT_RGB5:   /* D[5:4] : 00 RGB555, 01 RGB 565 */
541	        fmt = 0x00;
542		    break;
543
544	   case PIXEL_FMT_RGB6:
545	        fmt = 0x10;
546		    break;
547
548       default:
549            fmt = 0x00;
550            break;
551    }
552
553    SetVideoRegMask(pXGI, Index_VI_Control_Misc0, fmt, 0x7c);
554}
555
556void
557SetColorkeyReg(XGIPtr pXGI, CARD32 colorkey)
558{
559    CARD8 r, g, b;
560
561    b = LOBYTE(LOWORD(colorkey));
562    g = HIBYTE(LOWORD(colorkey));
563    r = LOBYTE(HIWORD(colorkey));
564
565   /* Activate the colorkey mode */
566    SetVideoReg(pXGI, Index_VI_Overlay_ColorKey_Blue_Min  , b);
567    SetVideoReg(pXGI, Index_VI_Overlay_ColorKey_Green_Min , g);
568    SetVideoReg(pXGI, Index_VI_Overlay_ColorKey_Red_Min   , r);
569
570    SetVideoReg(pXGI, Index_VI_Overlay_ColorKey_Blue_Max  , b);
571    SetVideoReg(pXGI, Index_VI_Overlay_ColorKey_Green_Max , g);
572    SetVideoReg(pXGI, Index_VI_Overlay_ColorKey_Red_Max   , r);
573}
574
575void
576SetVideoBrightnessReg(XGIPtr pXGI, INT32 value)
577{
578    CARD8   brightness;
579
580    brightness = LOBYTE(value);
581
582    SetVideoReg(pXGI, Index_VI_Brightness  ,brightness);
583}
584
585void
586SetVideoContrastReg(XGIPtr pXGI, INT32 value)
587{
588    CARD8   contrast;
589
590    contrast = (CARD8)(((value * 7) / 255) & 0x000F);
591
592    SetVideoRegMask(pXGI, Index_VI_Contrast_Enh_Ctrl, contrast, 0x07);
593}
594
595void
596SetVideoHueReg(XGIPtr pXGI, INT32 value)
597{
598    CARD8   hue;
599
600    if ( value > 0 )
601    {
602       SetVideoRegMask(pXGI, Index_VI_Hue, 0x00, 0x08);
603    }
604    else
605    {
606       SetVideoRegMask(pXGI, Index_VI_Hue, 0x08, 0x08);
607
608       value = -value;
609    }
610
611    hue = (CARD8)(((value * 7) / 180) & 0x0007);
612
613
614    SetVideoRegMask(pXGI, Index_VI_Hue, hue, 0x07);
615}
616
617void
618SetVideoSaturationReg(XGIPtr pXGI, INT32 value)
619{
620    CARD8   saturation;
621
622    if ( value > 0 )
623    {
624       SetVideoRegMask(pXGI, Index_VI_Saturation, 0x00, 0x08);
625       SetVideoRegMask(pXGI, Index_VI_Saturation, 0x00, 0x80);
626    }
627    else
628    {
629       SetVideoRegMask(pXGI, Index_VI_Saturation, 0x08, 0x08);
630       SetVideoRegMask(pXGI, Index_VI_Saturation, 0x80, 0x80);
631
632       value = -value;
633    }
634
635    saturation = (CARD8)(((value * 7) / 180) & 0x000F);
636
637    SetVideoRegMask(pXGI, Index_VI_Saturation, saturation, 0x07);
638    SetVideoRegMask(pXGI, Index_VI_Saturation, saturation << 4, 0x70);
639}
640
641void
642SetOverlayReg(XGIPtr pXGI, XGIOverlayPtr pOverlay)
643{
644
645    ScrnInfoPtr pScrn = pXGI->pScrn;
646	XGIPortPrivPtr pPriv = GET_PORT_PRIVATE(pScrn);
647
648    CARD32 tmpYPitch;
649    CARD16 top, left;
650    CARD16 bottom, right;
651    CARD32 PSY, PSU, PSV;
652    CARD16 screenX = pScrn->currentMode->HDisplay;
653    CARD16 screenY = pScrn->currentMode->VDisplay;
654
655    top    = pOverlay->dstBox.y1;
656    bottom = pOverlay->dstBox.y2;
657
658    if (bottom > screenY)
659        bottom = screenY;
660
661    left  = pOverlay->dstBox.x1;
662    right = pOverlay->dstBox.x2;
663
664    if (right > screenX)
665        right = screenX;
666
667    SetVideoReg(pXGI, Index_VI_Win_Hor_Disp_Start_Low, LOBYTE(left));
668    SetVideoReg(pXGI, Index_VI_Win_Hor_Disp_End_Low, LOBYTE(right));
669    SetVideoReg(pXGI, Index_VI_Win_Hor_Over, (HIBYTE(right)<<4)|HIBYTE(left));
670
671    SetVideoReg(pXGI, Index_VI_Win_Ver_Disp_Start_Low, LOBYTE(top));
672    SetVideoReg(pXGI, Index_VI_Win_Ver_Disp_End_Low, LOBYTE(bottom));
673    SetVideoReg(pXGI, Index_VI_Win_Ver_Over, (HIBYTE(bottom)<<4)|HIBYTE(top));
674
675    /*  Set ContrastFactor */
676    SetVideoRegMask(pXGI, Index_VI_Contrast_Enh_Ctrl, pOverlay->dwContrastFactor << 6, 0xc0);
677    SetVideoReg(pXGI, Index_VI_Contrast_Factor, pOverlay->SamplePixel);
678
679	/* SetVideoRegMask(pXGI, Index_VI_Control_Misc3, 0x00, 0x03); */
680
681	/* Does need to merge line buffer*/
682	SetMergeLineBufReg(pXGI, pOverlay->pitch > pPriv->linebufMergeLimit);
683
684	/* set video format */
685	SetVideoFormatReg(pXGI, pOverlay->pixelFormat);
686
687	/* set line buffer size */
688	SetVideoReg(pXGI, Index_VI_Line_Buffer_Size, LOBYTE(LOWORD(pOverlay->lineBufSize)));
689	SetVideoReg(pXGI, Index_VI_Line_Buffer_Size_Ext, HIBYTE(LOWORD(pOverlay->lineBufSize)));
690
691	/* set Key Overlay Operation Mode */
692	SetVideoRegMask (pXGI, Index_VI_Key_Overlay_OP, pOverlay->keyOP, 0x0f);
693
694
695    /* set scale factor */
696    SetVideoReg (pXGI, Index_VI_Hor_Post_Up_Scale_Low, LOBYTE(pOverlay->HUSF));
697    SetVideoReg (pXGI, Index_VI_Hor_Post_Up_Scale_High, HIBYTE(pOverlay->HUSF));
698
699    SetVideoReg (pXGI, Index_VI_Ver_Up_Scale_Low, LOBYTE(pOverlay->VUSF));
700    SetVideoReg (pXGI, Index_VI_Ver_Up_Scale_High, HIBYTE(pOverlay->VUSF));
701
702    SetVideoRegMask (pXGI, Index_VI_Scale_Control, (pOverlay->IntBit << 3)|(pOverlay->wHPre), 0x7f);
703
704    /* for 340 4-tap DDA */
705    SetDDAReg(pXGI, pOverlay->f_scale);
706
707    /* set frame or field mode */
708    SetVideoRegMask(pXGI, Index_VI_Control_Misc1, pOverlay->bobEnable, 0x1a);
709
710    /* set Y start address */
711    PSY = pOverlay->PSY >>1;        /* in unit of word */
712
713    SetVideoReg (pXGI, Index_VI_Disp_Y_Buf_Start_Low, LOBYTE(LOWORD(PSY)));
714    SetVideoReg (pXGI, Index_VI_Disp_Y_Buf_Start_Middle, HIBYTE(LOWORD(PSY)));
715    SetVideoReg (pXGI, Index_VI_Disp_Y_Buf_Start_High, LOBYTE(HIWORD(PSY)));
716    SetVideoRegMask(pXGI, Index_VI_Disp_Y_Buf_EXT_High, HIBYTE(HIWORD(PSY)), 0x03);
717    tmpYPitch = pOverlay->pitch >> 1 ; /* in unit of word */
718
719    if ((pOverlay->pixelFormat == PIXEL_FMT_YV12) ||
720		(pOverlay->pixelFormat == PIXEL_FMT_NV12) ||
721		(pOverlay->pixelFormat == PIXEL_FMT_NV21))
722    {
723        /* set UV pitch */
724		CARD32  uvpitch = tmpYPitch;
725        BYTE    bYUV_Pitch_High;
726
727		if(pOverlay->pixelFormat == PIXEL_FMT_YV12)
728	    	uvpitch >>= 1;
729/*
730        bYUV_Pitch_High = (HIBYTE(LOWORD(uvpitch))<<4) & 0xf0 |
731                        (HIBYTE(LOWORD(tmpYPitch))) & 0x0f;  */
732        bYUV_Pitch_High = ((HIBYTE(LOWORD(uvpitch))<<4) & 0xf0) | ((HIBYTE(LOWORD(tmpYPitch))) & 0x0f);
733        SetVideoReg    (pXGI, Index_VI_Disp_UV_Buf_Pitch_Low,         LOBYTE(LOWORD(uvpitch)));
734        SetVideoReg    (pXGI, Index_VI_Disp_Y_UV_Buf_Pitch_High, bYUV_Pitch_High);
735        SetVideoRegMask(pXGI, Index_VI_Disp_UV_Buf_Pitch_EXT_High,        uvpitch >> 12, 0x1f);
736
737        /* set U/V start address */
738        PSU = pOverlay->PSU >> 1; /* in unit of word; */
739        PSV = pOverlay->PSV >> 1; /* in unit of word;; */
740
741        SetVideoReg(pXGI, Index_VI_Disp_U_Buf_Start_Low, LOBYTE(LOWORD(PSU)));
742        SetVideoReg(pXGI, Index_VI_Disp_U_Buf_Start_Middle, HIBYTE(LOWORD(PSU)));
743        SetVideoReg(pXGI, Index_VI_Disp_U_Buf_Start_High, LOBYTE(HIWORD(PSU)));
744        /* [26:24] save in the D[2:0] */
745        SetVideoRegMask(pXGI, Index_VI_Disp_U_Buf_EXT_High, HIBYTE(HIWORD(PSU)), 0x03);
746
747        SetVideoReg(pXGI, Index_VI_Disp_V_Buf_Start_Low, LOBYTE(LOWORD(PSV)));
748        SetVideoReg(pXGI, Index_VI_Disp_V_Buf_Start_Middle, HIBYTE(LOWORD(PSV)));
749        SetVideoReg(pXGI, Index_VI_Disp_V_Buf_Start_High, LOBYTE(HIWORD(PSV)));
750        /* [26:24] save in the D[2:0] */
751        SetVideoRegMask(pXGI, Index_VI_Disp_V_Buf_EXT_High, HIBYTE(HIWORD(PSV)), 0x03);
752        tmpYPitch = pOverlay->pitch >> 1; /* in unit of word */
753    }
754    else
755        SetVideoRegMask(pXGI, Index_VI_Disp_Y_UV_Buf_Pitch_High, HIBYTE(LOWORD(tmpYPitch)), 0x0f);
756
757    /* set Y pitch */
758
759    SetVideoReg(pXGI, Index_VI_Disp_Y_Buf_Pitch_Low, LOBYTE(LOWORD(tmpYPitch)));
760    /* [16:12] save in the D[4:0] */
761    SetVideoRegMask(pXGI, Index_VI_Disp_Y_Buf_Pitch_EXT_High, (tmpYPitch>>12)&0x1f , 0x1f);
762
763
764     /* start address ready */
765    SetVideoRegMask(pXGI, Index_VI_Control_Misc3, 0x03, 0x03);
766
767    /* set contrast factor */
768    /* SetVideoRegMask(pXGI, Index_VI_Contrast_Enh_Ctrl, pOverlay->contrastCtrl<<6, 0xc0); */
769    /* SetVideoReg (pXGI, Index_VI_Contrast_Factor, pOverlay->contrastFactor); */
770}
771
772void
773SetCloseOverlayReg(XGIPtr pXGI)
774{
775  SetVideoRegMask (pXGI, Index_VI_Control_Misc2, 0x00, 0x01);
776  SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x02);
777}
778
779void
780SetSelectOverlayReg(XGIPtr pXGI, CARD8 index)
781{
782  SetVideoRegMask(pXGI, Index_VI_Control_Misc2, index, 0x01);
783}
784
785void
786SetEnableOverlayReg(XGIPtr pXGI, Bool bEnable)
787{
788        if (bEnable)
789                SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x02, 0x02);
790        else
791                SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x02);
792}
793
794void
795EnableCaptureAutoFlip(XGIPtr pXGI, Bool bEnable)
796{
797   if (bEnable)
798      SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x01, 0x01);
799   else
800      SetVideoRegMask(pXGI, Index_VI_Control_Misc0, 0x00, 0x01);
801}
802