Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[pandora-kernel.git] / drivers / staging / xgifb / vb_ext.c
1 #include <linux/version.h>
2 #include <linux/io.h>
3 #include <linux/types.h>
4 #include "XGIfb.h"
5
6 #include "vb_def.h"
7 #include "vgatypes.h"
8 #include "vb_struct.h"
9 #include "vb_util.h"
10 #include "vb_setmode.h"
11 #include "vb_ext.h"
12
13 /**************************************************************
14  *********************** Dynamic Sense ************************
15  *************************************************************/
16
17 static unsigned char XGINew_Is301B(struct vb_device_info *pVBInfo)
18 {
19         unsigned short flag;
20
21         flag = xgifb_reg_get(pVBInfo->Part4Port, 0x01);
22
23         if (flag > 0x0B0)
24                 return 0; /* 301b */
25         else
26                 return 1;
27 }
28
29 static unsigned char XGINew_Sense(unsigned short tempbx,
30                                   unsigned short tempcx,
31                                   struct vb_device_info *pVBInfo)
32 {
33         unsigned short temp, i, tempch;
34
35         temp = tempbx & 0xFF;
36         xgifb_reg_set(pVBInfo->Part4Port, 0x11, temp);
37         temp = (tempbx & 0xFF00) >> 8;
38         temp |= (tempcx & 0x00FF);
39         xgifb_reg_and_or(pVBInfo->Part4Port, 0x10, ~0x1F, temp);
40
41         for (i = 0; i < 10; i++)
42                 XGI_LongWait(pVBInfo);
43
44         tempch = (tempcx & 0x7F00) >> 8;
45         temp = xgifb_reg_get(pVBInfo->Part4Port, 0x03);
46         temp = temp ^ (0x0E);
47         temp &= tempch;
48
49         if (temp > 0)
50                 return 1;
51         else
52                 return 0;
53 }
54
55 static unsigned char
56 XGINew_GetLCDDDCInfo(struct xgi_hw_device_info *HwDeviceExtension,
57                      struct vb_device_info *pVBInfo)
58 {
59         unsigned short temp;
60
61         /* add lcd sense */
62         if (HwDeviceExtension->ulCRT2LCDType == LCD_UNKNOWN) {
63                 return 0;
64         } else {
65                 temp = (unsigned short) HwDeviceExtension->ulCRT2LCDType;
66                 switch (HwDeviceExtension->ulCRT2LCDType) {
67                 case LCD_INVALID:
68                 case LCD_800x600:
69                 case LCD_1024x768:
70                 case LCD_1280x1024:
71                         break;
72
73                 case LCD_640x480:
74                 case LCD_1024x600:
75                 case LCD_1152x864:
76                 case LCD_1280x960:
77                 case LCD_1152x768:
78                         temp = 0;
79                         break;
80
81                 case LCD_1400x1050:
82                 case LCD_1280x768:
83                 case LCD_1600x1200:
84                         break;
85
86                 case LCD_1920x1440:
87                 case LCD_2048x1536:
88                         temp = 0;
89                         break;
90
91                 default:
92                         break;
93                 }
94                 xgifb_reg_and_or(pVBInfo->P3d4, 0x36, 0xF0, temp);
95                 return 1;
96         }
97 }
98
99 static unsigned char XGINew_GetPanelID(struct vb_device_info *pVBInfo)
100 {
101         unsigned short PanelTypeTable[16] = { SyncNN | PanelRGB18Bit
102                         | Panel800x600  | _PanelType00, SyncNN | PanelRGB18Bit
103                         | Panel1024x768 | _PanelType01, SyncNN | PanelRGB18Bit
104                         | Panel800x600  | _PanelType02, SyncNN | PanelRGB18Bit
105                         | Panel640x480  | _PanelType03, SyncNN | PanelRGB18Bit
106                         | Panel1024x768 | _PanelType04, SyncNN | PanelRGB18Bit
107                         | Panel1024x768 | _PanelType05, SyncNN | PanelRGB18Bit
108                         | Panel1024x768 | _PanelType06, SyncNN | PanelRGB24Bit
109                         | Panel1024x768 | _PanelType07, SyncNN | PanelRGB18Bit
110                         | Panel800x600  | _PanelType08, SyncNN | PanelRGB18Bit
111                         | Panel1024x768 | _PanelType09, SyncNN | PanelRGB18Bit
112                         | Panel800x600  | _PanelType0A, SyncNN | PanelRGB18Bit
113                         | Panel1024x768 | _PanelType0B, SyncNN | PanelRGB18Bit
114                         | Panel1024x768 | _PanelType0C, SyncNN | PanelRGB24Bit
115                         | Panel1024x768 | _PanelType0D, SyncNN | PanelRGB18Bit
116                         | Panel1024x768 | _PanelType0E, SyncNN | PanelRGB18Bit
117                         | Panel1024x768 | _PanelType0F };
118         unsigned short tempax, tempbx, temp;
119         /* unsigned short return_flag; */
120
121         tempax = xgifb_reg_get(pVBInfo->P3c4, 0x1A);
122         tempbx = tempax & 0x1E;
123
124         if (tempax == 0)
125                 return 0;
126         else {
127                 /*
128                 if (!(tempax & 0x10)) {
129                         if (pVBInfo->IF_DEF_LVDS == 1) {
130                                 tempbx = 0;
131                                 temp = xgifb_reg_get(pVBInfo->P3c4, 0x38);
132                                 if (temp & 0x40)
133                                         tempbx |= 0x08;
134                                 if (temp & 0x20)
135                                         tempbx |= 0x02;
136                                 if (temp & 0x01)
137                                         tempbx |= 0x01;
138
139                                 temp = xgifb_reg_get(pVBInfo->P3c4, 0x39);
140                                 if (temp & 0x80)
141                                         tempbx |= 0x04;
142                          } else {
143                                 return(0);
144                          }
145                 }
146                 */
147
148                 tempbx = tempbx >> 1;
149                 temp = tempbx & 0x00F;
150                 xgifb_reg_set(pVBInfo->P3d4, 0x36, temp);
151                 tempbx--;
152                 tempbx = PanelTypeTable[tempbx];
153
154                 temp = (tempbx & 0xFF00) >> 8;
155                 xgifb_reg_and_or(pVBInfo->P3d4, 0x37, ~(LCDSyncBit
156                                 | LCDRGB18Bit), temp);
157                 return 1;
158         }
159 }
160
161 static unsigned char
162 XGINew_BridgeIsEnable(struct xgi_hw_device_info *HwDeviceExtension,
163                       struct vb_device_info *pVBInfo)
164 {
165         unsigned short flag;
166
167         if (XGI_BridgeIsOn(pVBInfo) == 0) {
168                 flag = xgifb_reg_get(pVBInfo->Part1Port, 0x0);
169
170                 if (flag & 0x050)
171                         return 1;
172                 else
173                         return 0;
174
175         }
176         return 0;
177 }
178
179 static unsigned char
180 XGINew_SenseHiTV(struct xgi_hw_device_info *HwDeviceExtension,
181                  struct vb_device_info *pVBInfo)
182 {
183         unsigned short tempbx, tempcx, temp, i, tempch;
184
185         tempbx = *pVBInfo->pYCSenseData2;
186
187         tempcx = 0x0604;
188
189         temp = tempbx & 0xFF;
190         xgifb_reg_set(pVBInfo->Part4Port, 0x11, temp);
191         temp = (tempbx & 0xFF00) >> 8;
192         temp |= (tempcx & 0x00FF);
193         xgifb_reg_and_or(pVBInfo->Part4Port, 0x10, ~0x1F, temp);
194
195         for (i = 0; i < 10; i++)
196                 XGI_LongWait(pVBInfo);
197
198         tempch = (tempcx & 0xFF00) >> 8;
199         temp = xgifb_reg_get(pVBInfo->Part4Port, 0x03);
200         temp = temp ^ (0x0E);
201         temp &= tempch;
202
203         if (temp != tempch)
204                 return 0;
205
206         tempbx = *pVBInfo->pVideoSenseData2;
207
208         tempcx = 0x0804;
209         temp = tempbx & 0xFF;
210         xgifb_reg_set(pVBInfo->Part4Port, 0x11, temp);
211         temp = (tempbx & 0xFF00) >> 8;
212         temp |= (tempcx & 0x00FF);
213         xgifb_reg_and_or(pVBInfo->Part4Port, 0x10, ~0x1F, temp);
214
215         for (i = 0; i < 10; i++)
216                 XGI_LongWait(pVBInfo);
217
218         tempch = (tempcx & 0xFF00) >> 8;
219         temp = xgifb_reg_get(pVBInfo->Part4Port, 0x03);
220         temp = temp ^ (0x0E);
221         temp &= tempch;
222
223         if (temp != tempch) {
224                 return 0;
225         } else {
226                 tempbx = 0x3FF;
227                 tempcx = 0x0804;
228                 temp = tempbx & 0xFF;
229                 xgifb_reg_set(pVBInfo->Part4Port, 0x11, temp);
230                 temp = (tempbx & 0xFF00) >> 8;
231                 temp |= (tempcx & 0x00FF);
232                 xgifb_reg_and_or(pVBInfo->Part4Port, 0x10, ~0x1F, temp);
233
234                 for (i = 0; i < 10; i++)
235                         XGI_LongWait(pVBInfo);
236
237                 tempch = (tempcx & 0xFF00) >> 8;
238                 temp = xgifb_reg_get(pVBInfo->Part4Port, 0x03);
239                 temp = temp ^ (0x0E);
240                 temp &= tempch;
241
242                 if (temp != tempch)
243                         return 1;
244                 else
245                         return 0;
246         }
247 }
248
249 void XGI_GetSenseStatus(struct xgi_hw_device_info *HwDeviceExtension,
250                         struct vb_device_info *pVBInfo)
251 {
252         unsigned short  tempax = 0, tempbx, tempcx, temp,
253                         P2reg0 = 0, SenseModeNo = 0,
254                         OutputSelect = *pVBInfo->pOutputSelect,
255                         ModeIdIndex, i;
256         pVBInfo->BaseAddr = (unsigned long) HwDeviceExtension->pjIOAddress;
257
258         if (pVBInfo->IF_DEF_LVDS == 1) {
259                 /* ynlai 02/27/2002 */
260                 tempax = xgifb_reg_get(pVBInfo->P3c4, 0x1A);
261                 tempbx = xgifb_reg_get(pVBInfo->P3c4, 0x1B);
262                 tempax = ((tempax & 0xFE) >> 1) | (tempbx << 8);
263                 if (tempax == 0x00) { /* Get Panel id from DDC */
264                         temp = XGINew_GetLCDDDCInfo(HwDeviceExtension, pVBInfo);
265                         if (temp == 1) { /* LCD connect */
266                                 /* set CR39 bit0="1" */
267                                 xgifb_reg_and_or(pVBInfo->P3d4,
268                                                  0x39, 0xFF, 0x01);
269                                 /* clean CR37 bit4="0" */
270                                 xgifb_reg_and_or(pVBInfo->P3d4,
271                                                  0x37, 0xEF, 0x00);
272                                 temp = LCDSense;
273                         } else { /* LCD don't connect */
274                                 temp = 0;
275                         }
276                 } else {
277                         XGINew_GetPanelID(pVBInfo);
278                         temp = LCDSense;
279                 }
280
281                 tempbx = ~(LCDSense | AVIDEOSense | SVIDEOSense);
282                 xgifb_reg_and_or(pVBInfo->P3d4, 0x32, tempbx, temp);
283         } else { /* for 301 */
284                 if (pVBInfo->VBInfo & SetCRT2ToHiVisionTV) { /* for HiVision */
285                         tempax = xgifb_reg_get(pVBInfo->P3c4, 0x38);
286                         temp = tempax & 0x01;
287                         tempax = xgifb_reg_get(pVBInfo->P3c4, 0x3A);
288                         temp = temp | (tempax & 0x02);
289                         xgifb_reg_and_or(pVBInfo->P3d4, 0x32, 0xA0, temp);
290                 } else {
291                         if (XGI_BridgeIsOn(pVBInfo)) {
292                                 P2reg0 = xgifb_reg_get(pVBInfo->Part2Port,
293                                                        0x00);
294                                 if (!XGINew_BridgeIsEnable(HwDeviceExtension,
295                                                            pVBInfo)) {
296                                         SenseModeNo = 0x2e;
297                                 /* xgifb_reg_set(pVBInfo->P3d4, 0x30, 0x41);
298                                  * XGISetModeNew(HwDeviceExtension, 0x2e);
299                                  * // ynlai InitMode */
300
301                                         temp = XGI_SearchModeID(SenseModeNo,
302                                                                 &ModeIdIndex,
303                                                                 pVBInfo);
304                                         XGI_GetVGAType(HwDeviceExtension,
305                                                        pVBInfo);
306                                         XGI_GetVBType(pVBInfo);
307                                         pVBInfo->SetFlag = 0x00;
308                                         pVBInfo->ModeType = ModeVGA;
309                                         pVBInfo->VBInfo = SetCRT2ToRAMDAC |
310                                                           LoadDACFlag |
311                                                           SetInSlaveMode;
312                                         XGI_GetLCDInfo(0x2e,
313                                                        ModeIdIndex,
314                                                        pVBInfo);
315                                         XGI_GetTVInfo(0x2e,
316                                                       ModeIdIndex,
317                                                       pVBInfo);
318                                         XGI_EnableBridge(HwDeviceExtension,
319                                                          pVBInfo);
320                                         XGI_SetCRT2Group301(SenseModeNo,
321                                                             HwDeviceExtension,
322                                                             pVBInfo);
323                                         XGI_SetCRT2ModeRegs(0x2e,
324                                                             HwDeviceExtension,
325                                                             pVBInfo);
326                                         /* XGI_DisableBridge(HwDeviceExtension,
327                                          *                   pVBInfo ) ; */
328                                         /* Display Off 0212 */
329                                         xgifb_reg_and_or(pVBInfo->P3c4,
330                                                          0x01,
331                                                          0xDF,
332                                                          0x20);
333                                         for (i = 0; i < 20; i++)
334                                                 XGI_LongWait(pVBInfo);
335                                 }
336                                 xgifb_reg_set(pVBInfo->Part2Port, 0x00, 0x1c);
337                                 tempax = 0;
338                                 tempbx = *pVBInfo->pRGBSenseData;
339
340                                 if (!(XGINew_Is301B(pVBInfo)))
341                                         tempbx = *pVBInfo->pRGBSenseData2;
342
343                                 tempcx = 0x0E08;
344                                 if (XGINew_Sense(tempbx, tempcx, pVBInfo)) {
345                                         if (XGINew_Sense(tempbx,
346                                                          tempcx,
347                                                          pVBInfo))
348                                                 tempax |= Monitor2Sense;
349                                 }
350
351                                 if (pVBInfo->VBType & VB_XGI301C)
352                                         xgifb_reg_or(pVBInfo->Part4Port,
353                                                      0x0d,
354                                                      0x04);
355
356                                 /* add by kuku for Multi-adapter sense HiTV */
357                                 if (XGINew_SenseHiTV(HwDeviceExtension,
358                                                      pVBInfo)) {
359                                         tempax |= HiTVSense;
360                                         if ((pVBInfo->VBType & VB_XGI301C))
361                                                 tempax ^= (HiTVSense |
362                                                            YPbPrSense);
363                                 }
364
365                                 /* start */
366                                 if (!(tempax & (HiTVSense | YPbPrSense))) {
367                                         tempbx = *pVBInfo->pYCSenseData;
368                                         if (!(XGINew_Is301B(pVBInfo)))
369                                                 tempbx = *pVBInfo->pYCSenseData2;
370                                         tempcx = 0x0604;
371                                         if (XGINew_Sense(tempbx,
372                                                          tempcx,
373                                                          pVBInfo)) {
374                                                 if (XGINew_Sense(tempbx,
375                                                                  tempcx,
376                                                                  pVBInfo))
377                                                         tempax |= SVIDEOSense;
378                                         }
379
380                                         if (OutputSelect & BoardTVType) {
381                                                 tempbx = *pVBInfo->pVideoSenseData;
382
383                                                 if (!(XGINew_Is301B(pVBInfo)))
384                                                         tempbx = *pVBInfo->pVideoSenseData2;
385
386                                                 tempcx = 0x0804;
387                                                 if (XGINew_Sense(tempbx,
388                                                                  tempcx,
389                                                                  pVBInfo)) {
390                                                         if (XGINew_Sense(tempbx,
391                                                                          tempcx,
392                                                                          pVBInfo))
393                                                                 tempax |= AVIDEOSense;
394                                                 }
395                                         } else {
396                                                 if (!(tempax & SVIDEOSense)) {
397                                                         tempbx = *pVBInfo->pVideoSenseData;
398
399                                                         if (!(XGINew_Is301B(pVBInfo)))
400                                                                 tempbx = *pVBInfo->pVideoSenseData2;
401
402                                                         tempcx = 0x0804;
403                                                         if (XGINew_Sense(tempbx,
404                                                                          tempcx,
405                                                                          pVBInfo)) {
406                                                                 if (XGINew_Sense(tempbx, tempcx, pVBInfo))
407                                                                         tempax |= AVIDEOSense;
408                                                         }
409                                                 }
410                                         }
411                                 }
412                         } /* end */
413                         if (!(tempax & Monitor2Sense)) {
414                                 if (XGINew_SenseLCD(HwDeviceExtension, pVBInfo))
415                                         tempax |= LCDSense;
416                         }
417                         tempbx = 0;
418                         tempcx = 0;
419                         XGINew_Sense(tempbx, tempcx, pVBInfo);
420
421                         xgifb_reg_and_or(pVBInfo->P3d4, 0x32, ~0xDF, tempax);
422                         xgifb_reg_set(pVBInfo->Part2Port, 0x00, P2reg0);
423
424                         if (!(P2reg0 & 0x20)) {
425                                 pVBInfo->VBInfo = DisableCRT2Display;
426                                 /* XGI_SetCRT2Group301(SenseModeNo,
427                                  *                     HwDeviceExtension,
428                                  *                     pVBInfo); */
429                         }
430                 }
431         }
432         XGI_DisableBridge(HwDeviceExtension, pVBInfo); /* shampoo 0226 */
433
434 }
435
436 unsigned short XGINew_SenseLCD(struct xgi_hw_device_info *HwDeviceExtension,
437                                struct vb_device_info *pVBInfo)
438 {
439         /* unsigned short SoftSetting ; */
440         unsigned short temp;
441
442         temp = XGINew_GetLCDDDCInfo(HwDeviceExtension, pVBInfo);
443
444         return temp;
445 }