3 * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
5 * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
7 * Portions Copyright (c) 2001 Matrox Graphics Inc.
9 * Version: 1.65 2002/08/14
11 * See matroxfb_base.c for contributors.
15 #include "matroxfb_base.h"
16 #include "matroxfb_misc.h"
17 #include "matroxfb_DAC1064.h"
19 #include <linux/matroxfb.h>
20 #include <asm/div64.h>
22 #include "matroxfb_g450.h"
24 /* Definition of the various controls */
26 struct v4l2_queryctrl desc
;
33 static const struct mctl g450_controls
[] =
34 { { { V4L2_CID_BRIGHTNESS
, V4L2_CTRL_TYPE_INTEGER
,
36 0, WLMAX
-BLMIN
, 1, 370-BLMIN
,
38 }, offsetof(struct matrox_fb_info
, altout
.tvo_params
.brightness
) },
39 { { V4L2_CID_CONTRAST
, V4L2_CTRL_TYPE_INTEGER
,
43 }, offsetof(struct matrox_fb_info
, altout
.tvo_params
.contrast
) },
44 { { V4L2_CID_SATURATION
, V4L2_CTRL_TYPE_INTEGER
,
48 }, offsetof(struct matrox_fb_info
, altout
.tvo_params
.saturation
) },
49 { { V4L2_CID_HUE
, V4L2_CTRL_TYPE_INTEGER
,
53 }, offsetof(struct matrox_fb_info
, altout
.tvo_params
.hue
) },
54 { { MATROXFB_CID_TESTOUT
, V4L2_CTRL_TYPE_BOOLEAN
,
58 }, offsetof(struct matrox_fb_info
, altout
.tvo_params
.testout
) },
61 #define G450CTRLS ARRAY_SIZE(g450_controls)
63 /* Return: positive number: id found
64 -EINVAL: id not found, return failure
65 -ENOENT: id not found, create fake disabled control */
66 static int get_ctrl_id(__u32 v4l2_id
) {
69 for (i
= 0; i
< G450CTRLS
; i
++) {
70 if (v4l2_id
< g450_controls
[i
].desc
.id
) {
71 if (g450_controls
[i
].desc
.id
== 0x08000000) {
76 if (v4l2_id
== g450_controls
[i
].desc
.id
) {
83 static inline int *get_ctrl_ptr(struct matrox_fb_info
*minfo
, unsigned int idx
)
85 return (int*)((char*)minfo
+ g450_controls
[idx
].control
);
88 static void tvo_fill_defaults(struct matrox_fb_info
*minfo
)
92 for (i
= 0; i
< G450CTRLS
; i
++) {
93 *get_ctrl_ptr(minfo
, i
) = g450_controls
[i
].desc
.default_value
;
97 static int cve2_get_reg(struct matrox_fb_info
*minfo
, int reg
)
102 matroxfb_DAC_lock_irqsave(flags
);
103 matroxfb_DAC_out(minfo
, 0x87, reg
);
104 val
= matroxfb_DAC_in(minfo
, 0x88);
105 matroxfb_DAC_unlock_irqrestore(flags
);
109 static void cve2_set_reg(struct matrox_fb_info
*minfo
, int reg
, int val
)
113 matroxfb_DAC_lock_irqsave(flags
);
114 matroxfb_DAC_out(minfo
, 0x87, reg
);
115 matroxfb_DAC_out(minfo
, 0x88, val
);
116 matroxfb_DAC_unlock_irqrestore(flags
);
119 static void cve2_set_reg10(struct matrox_fb_info
*minfo
, int reg
, int val
)
123 matroxfb_DAC_lock_irqsave(flags
);
124 matroxfb_DAC_out(minfo
, 0x87, reg
);
125 matroxfb_DAC_out(minfo
, 0x88, val
>> 2);
126 matroxfb_DAC_out(minfo
, 0x87, reg
+ 1);
127 matroxfb_DAC_out(minfo
, 0x88, val
& 3);
128 matroxfb_DAC_unlock_irqrestore(flags
);
131 static void g450_compute_bwlevel(const struct matrox_fb_info
*minfo
, int *bl
,
134 const int b
= minfo
->altout
.tvo_params
.brightness
+ BLMIN
;
135 const int c
= minfo
->altout
.tvo_params
.contrast
;
137 *bl
= max(b
- c
, BLMIN
);
138 *wl
= min(b
+ c
, WLMAX
);
141 static int g450_query_ctrl(void* md
, struct v4l2_queryctrl
*p
) {
144 i
= get_ctrl_id(p
->id
);
146 *p
= g450_controls
[i
].desc
;
150 static const struct v4l2_queryctrl disctrl
=
151 { .flags
= V4L2_CTRL_FLAG_DISABLED
};
156 sprintf(p
->name
, "Ctrl #%08X", i
);
162 static int g450_set_ctrl(void* md
, struct v4l2_control
*p
) {
164 struct matrox_fb_info
*minfo
= md
;
166 i
= get_ctrl_id(p
->id
);
167 if (i
< 0) return -EINVAL
;
172 if (p
->value
== *get_ctrl_ptr(minfo
, i
)) return 0;
177 if (p
->value
> g450_controls
[i
].desc
.maximum
) return -EINVAL
;
178 if (p
->value
< g450_controls
[i
].desc
.minimum
) return -EINVAL
;
183 *get_ctrl_ptr(minfo
, i
) = p
->value
;
186 case V4L2_CID_BRIGHTNESS
:
187 case V4L2_CID_CONTRAST
:
189 int blacklevel
, whitelevel
;
190 g450_compute_bwlevel(minfo
, &blacklevel
, &whitelevel
);
191 cve2_set_reg10(minfo
, 0x0e, blacklevel
);
192 cve2_set_reg10(minfo
, 0x1e, whitelevel
);
195 case V4L2_CID_SATURATION
:
196 cve2_set_reg(minfo
, 0x20, p
->value
);
197 cve2_set_reg(minfo
, 0x22, p
->value
);
200 cve2_set_reg(minfo
, 0x25, p
->value
);
202 case MATROXFB_CID_TESTOUT
:
204 unsigned char val
= cve2_get_reg(minfo
, 0x05);
205 if (p
->value
) val
|= 0x02;
207 cve2_set_reg(minfo
, 0x05, val
);
216 static int g450_get_ctrl(void* md
, struct v4l2_control
*p
) {
218 struct matrox_fb_info
*minfo
= md
;
220 i
= get_ctrl_id(p
->id
);
221 if (i
< 0) return -EINVAL
;
222 p
->value
= *get_ctrl_ptr(minfo
, i
);
228 unsigned int h_f_porch
;
230 unsigned int h_b_porch
;
231 unsigned long long int chromasc
;
233 unsigned int v_total
;
236 static void computeRegs(struct matrox_fb_info
*minfo
, struct mavenregs
*r
,
237 struct my_timming
*mt
, const struct output_desc
*outd
)
245 unsigned int pixclock
;
246 unsigned long long piic
;
250 r
->regs
[0x80] = 0x03; /* | 0x40 for SCART */
252 hvis
= ((mt
->HDisplay
<< 1) + 3) & ~3;
258 piic
= 1000000000ULL * hvis
;
259 do_div(piic
, outd
->h_vis
);
261 dprintk(KERN_DEBUG
"Want %u kHz pixclock\n", (unsigned int)piic
);
263 mnp
= matroxfb_g450_setclk(minfo
, piic
, M_VIDEO_PLL
);
266 mt
->pixclock
= g450_mnp2f(minfo
, mnp
);
268 dprintk(KERN_DEBUG
"MNP=%08X\n", mnp
);
270 pixclock
= 1000000000U / mt
->pixclock
;
272 dprintk(KERN_DEBUG
"Got %u ps pixclock\n", pixclock
);
274 piic
= outd
->chromasc
;
275 do_div(piic
, mt
->pixclock
);
278 dprintk(KERN_DEBUG
"Chroma is %08X\n", chromasc
);
280 r
->regs
[0] = piic
>> 24;
281 r
->regs
[1] = piic
>> 16;
282 r
->regs
[2] = piic
>> 8;
283 r
->regs
[3] = piic
>> 0;
284 hbp
= (((outd
->h_b_porch
+ pixclock
) / pixclock
)) & ~1;
285 hfp
= (((outd
->h_f_porch
+ pixclock
) / pixclock
)) & ~1;
286 hsl
= (((outd
->h_sync
+ pixclock
) / pixclock
)) & ~1;
287 hlen
= hvis
+ hfp
+ hsl
+ hbp
;
290 dprintk(KERN_DEBUG
"WL: vis=%u, hf=%u, hs=%u, hb=%u, total=%u\n", hvis
, hfp
, hsl
, hbp
, hlen
);
296 } else if (over
< 10) {
305 /* maybe cve2 has requirement 800 < hlen < 1184 */
307 r
->regs
[0x09] = (outd
->burst
+ pixclock
- 1) / pixclock
; /* burst length */
310 r
->regs
[0x31] = hvis
/ 8;
311 r
->regs
[0x32] = hvis
& 7;
313 dprintk(KERN_DEBUG
"PG: vis=%04X, hf=%02X, hs=%02X, hb=%02X, total=%04X\n", hvis
, hfp
, hsl
, hbp
, hlen
);
315 r
->regs
[0x84] = 1; /* x sync point */
320 dprintk(KERN_DEBUG
"hlen=%u hvis=%u\n", hlen
, hvis
);
324 mt
->HDisplay
= hvis
& ~7;
325 mt
->HSyncStart
= mt
->HDisplay
+ 8;
326 mt
->HSyncEnd
= (hlen
& ~7) - 8;
332 unsigned int vsyncend
;
333 unsigned int vdisplay
;
336 vsyncend
= mt
->VSyncEnd
;
337 vdisplay
= mt
->VDisplay
;
338 if (vtotal
< outd
->v_total
) {
339 unsigned int yovr
= outd
->v_total
- vtotal
;
341 vsyncend
+= yovr
>> 1;
342 } else if (vtotal
> outd
->v_total
) {
343 vdisplay
= outd
->v_total
- 4;
344 vsyncend
= outd
->v_total
;
346 upper
= (outd
->v_total
- vsyncend
) >> 1; /* in field lines */
347 r
->regs
[0x17] = outd
->v_total
/ 4;
348 r
->regs
[0x18] = outd
->v_total
& 3;
349 r
->regs
[0x33] = upper
- 1; /* upper blanking */
350 r
->regs
[0x82] = upper
; /* y sync point */
351 r
->regs
[0x83] = upper
>> 8;
353 mt
->VDisplay
= vdisplay
;
354 mt
->VSyncStart
= outd
->v_total
- 2;
355 mt
->VSyncEnd
= outd
->v_total
;
356 mt
->VTotal
= outd
->v_total
;
360 static void cve2_init_TVdata(int norm
, struct mavenregs
* data
, const struct output_desc
** outd
) {
361 static const struct output_desc paloutd
= {
362 .h_vis
= 52148148, // ps
363 .h_f_porch
= 1407407, // ps
364 .h_sync
= 4666667, // ps
365 .h_b_porch
= 5777778, // ps
366 .chromasc
= 19042247534182ULL, // 4433618.750 Hz
367 .burst
= 2518518, // ps
370 static const struct output_desc ntscoutd
= {
371 .h_vis
= 52888889, // ps
372 .h_f_porch
= 1333333, // ps
373 .h_sync
= 4666667, // ps
374 .h_b_porch
= 4666667, // ps
375 .chromasc
= 15374030659475ULL, // 3579545.454 Hz
376 .burst
= 2418418, // ps
377 .v_total
= 525, // lines
380 static const struct mavenregs palregs
= { {
381 0x2A, 0x09, 0x8A, 0xCB, /* 00: chroma subcarrier */
384 0xF9, /* modified by code (F9 written...) */
385 0x00, /* ? not written */
391 0x00, /* ? not written */
392 // 0x3F, 0x03, /* 0E-0F */
394 0x3C, 0x03, /* 10-11 */
397 0x1C, 0x3D, 0x14, /* 14-16 */
398 0x9C, 0x01, /* 17-18 */
404 // 0x89, 0x03, /* 1E-1F */
418 0x55, 0x01, /* 2A-2B */
420 0x07, 0x7E, /* 2D-2E */
421 0x02, 0x54, /* 2F-30 */
422 0xB0, 0x00, /* 31-32 */
425 0x00, /* 35 written multiple times */
426 0x00, /* 36 not written */
432 0x3F, 0x03, /* 3C-3D */
433 0x00, /* 3E written multiple times */
434 0x00, /* 3F not written */
436 static struct mavenregs ntscregs
= { {
437 0x21, 0xF0, 0x7C, 0x1F, /* 00: chroma subcarrier */
440 0xF9, /* modified by code (F9 written...) */
441 0x00, /* ? not written */
447 0x00, /* ? not written */
448 0x41, 0x00, /* 0E-0F */
449 0x3C, 0x00, /* 10-11 */
452 0x1B, 0x1B, 0x24, /* 14-16 */
453 0x83, 0x01, /* 17-18 */
459 //0x89, 0x02, /* 1E-1F */
460 0xC0, 0x02, /* 1E-1F */
473 0xFF, 0x03, /* 2A-2B */
475 0x0F, 0x78, /* 2D-2E */
476 0x00, 0x00, /* 2F-30 */
477 0xB2, 0x04, /* 31-32 */
480 0x00, /* 35 written multiple times */
481 0x00, /* 36 not written */
487 0x3C, 0x00, /* 3C-3D */
488 0x00, /* 3E written multiple times */
489 0x00, /* never written */
492 if (norm
== MATROXFB_OUTPUT_MODE_PAL
) {
502 #define LR(x) cve2_set_reg(minfo, (x), m->regs[(x)])
503 static void cve2_init_TV(struct matrox_fb_info
*minfo
,
504 const struct mavenregs
*m
)
512 cve2_set_reg(minfo
, 0x3E, 0x01);
514 for (i
= 0; i
< 0x3E; i
++) {
517 cve2_set_reg(minfo
, 0x3E, 0x00);
520 static int matroxfb_g450_compute(void* md
, struct my_timming
* mt
) {
521 struct matrox_fb_info
*minfo
= md
;
523 dprintk(KERN_DEBUG
"Computing, mode=%u\n", minfo
->outputs
[1].mode
);
525 if (mt
->crtc
== MATROXFB_SRC_CRTC2
&&
526 minfo
->outputs
[1].mode
!= MATROXFB_OUTPUT_MODE_MONITOR
) {
527 const struct output_desc
* outd
;
529 cve2_init_TVdata(minfo
->outputs
[1].mode
, &minfo
->hw
.maven
, &outd
);
531 int blacklevel
, whitelevel
;
532 g450_compute_bwlevel(minfo
, &blacklevel
, &whitelevel
);
533 minfo
->hw
.maven
.regs
[0x0E] = blacklevel
>> 2;
534 minfo
->hw
.maven
.regs
[0x0F] = blacklevel
& 3;
535 minfo
->hw
.maven
.regs
[0x1E] = whitelevel
>> 2;
536 minfo
->hw
.maven
.regs
[0x1F] = whitelevel
& 3;
538 minfo
->hw
.maven
.regs
[0x20] =
539 minfo
->hw
.maven
.regs
[0x22] = minfo
->altout
.tvo_params
.saturation
;
541 minfo
->hw
.maven
.regs
[0x25] = minfo
->altout
.tvo_params
.hue
;
543 if (minfo
->altout
.tvo_params
.testout
) {
544 minfo
->hw
.maven
.regs
[0x05] |= 0x02;
547 computeRegs(minfo
, &minfo
->hw
.maven
, mt
, outd
);
548 } else if (mt
->mnp
< 0) {
549 /* We must program clocks before CRTC2, otherwise interlaced mode
551 mt
->mnp
= matroxfb_g450_setclk(minfo
, mt
->pixclock
, (mt
->crtc
== MATROXFB_SRC_CRTC1
) ? M_PIXEL_PLL_C
: M_VIDEO_PLL
);
552 mt
->pixclock
= g450_mnp2f(minfo
, mt
->mnp
);
554 dprintk(KERN_DEBUG
"Pixclock = %u\n", mt
->pixclock
);
558 static int matroxfb_g450_program(void* md
) {
559 struct matrox_fb_info
*minfo
= md
;
561 if (minfo
->outputs
[1].mode
!= MATROXFB_OUTPUT_MODE_MONITOR
) {
562 cve2_init_TV(minfo
, &minfo
->hw
.maven
);
567 static int matroxfb_g450_verify_mode(void* md
, u_int32_t arg
) {
569 case MATROXFB_OUTPUT_MODE_PAL
:
570 case MATROXFB_OUTPUT_MODE_NTSC
:
571 case MATROXFB_OUTPUT_MODE_MONITOR
:
577 static int g450_dvi_compute(void* md
, struct my_timming
* mt
) {
578 struct matrox_fb_info
*minfo
= md
;
581 mt
->mnp
= matroxfb_g450_setclk(minfo
, mt
->pixclock
, (mt
->crtc
== MATROXFB_SRC_CRTC1
) ? M_PIXEL_PLL_C
: M_VIDEO_PLL
);
582 mt
->pixclock
= g450_mnp2f(minfo
, mt
->mnp
);
587 static struct matrox_altout matroxfb_g450_altout
= {
588 .name
= "Secondary output",
589 .compute
= matroxfb_g450_compute
,
590 .program
= matroxfb_g450_program
,
591 .verifymode
= matroxfb_g450_verify_mode
,
592 .getqueryctrl
= g450_query_ctrl
,
593 .getctrl
= g450_get_ctrl
,
594 .setctrl
= g450_set_ctrl
,
597 static struct matrox_altout matroxfb_g450_dvi
= {
598 .name
= "DVI output",
599 .compute
= g450_dvi_compute
,
602 void matroxfb_g450_connect(struct matrox_fb_info
*minfo
)
604 if (minfo
->devflags
.g450dac
) {
605 down_write(&minfo
->altout
.lock
);
606 tvo_fill_defaults(minfo
);
607 minfo
->outputs
[1].src
= minfo
->outputs
[1].default_src
;
608 minfo
->outputs
[1].data
= minfo
;
609 minfo
->outputs
[1].output
= &matroxfb_g450_altout
;
610 minfo
->outputs
[1].mode
= MATROXFB_OUTPUT_MODE_MONITOR
;
611 minfo
->outputs
[2].src
= minfo
->outputs
[2].default_src
;
612 minfo
->outputs
[2].data
= minfo
;
613 minfo
->outputs
[2].output
= &matroxfb_g450_dvi
;
614 minfo
->outputs
[2].mode
= MATROXFB_OUTPUT_MODE_MONITOR
;
615 up_write(&minfo
->altout
.lock
);
619 void matroxfb_g450_shutdown(struct matrox_fb_info
*minfo
)
621 if (minfo
->devflags
.g450dac
) {
622 down_write(&minfo
->altout
.lock
);
623 minfo
->outputs
[1].src
= MATROXFB_SRC_NONE
;
624 minfo
->outputs
[1].output
= NULL
;
625 minfo
->outputs
[1].data
= NULL
;
626 minfo
->outputs
[1].mode
= MATROXFB_OUTPUT_MODE_MONITOR
;
627 minfo
->outputs
[2].src
= MATROXFB_SRC_NONE
;
628 minfo
->outputs
[2].output
= NULL
;
629 minfo
->outputs
[2].data
= NULL
;
630 minfo
->outputs
[2].mode
= MATROXFB_OUTPUT_MODE_MONITOR
;
631 up_write(&minfo
->altout
.lock
);
635 EXPORT_SYMBOL(matroxfb_g450_connect
);
636 EXPORT_SYMBOL(matroxfb_g450_shutdown
);
638 MODULE_AUTHOR("(c) 2000-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
639 MODULE_DESCRIPTION("Matrox G450/G550 output driver");
640 MODULE_LICENSE("GPL");