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/uaccess.h>
21 #include <asm/div64.h>
23 #include "matroxfb_g450.h"
25 /* Definition of the various controls */
27 struct v4l2_queryctrl desc;
34 static const struct mctl g450_controls[] =
35 { { { V4L2_CID_BRIGHTNESS, V4L2_CTRL_TYPE_INTEGER,
37 0, WLMAX-BLMIN, 1, 370-BLMIN,
39 }, offsetof(struct matrox_fb_info, altout.tvo_params.brightness) },
40 { { V4L2_CID_CONTRAST, V4L2_CTRL_TYPE_INTEGER,
44 }, offsetof(struct matrox_fb_info, altout.tvo_params.contrast) },
45 { { V4L2_CID_SATURATION, V4L2_CTRL_TYPE_INTEGER,
49 }, offsetof(struct matrox_fb_info, altout.tvo_params.saturation) },
50 { { V4L2_CID_HUE, V4L2_CTRL_TYPE_INTEGER,
54 }, offsetof(struct matrox_fb_info, altout.tvo_params.hue) },
55 { { MATROXFB_CID_TESTOUT, V4L2_CTRL_TYPE_BOOLEAN,
59 }, offsetof(struct matrox_fb_info, altout.tvo_params.testout) },
62 #define G450CTRLS ARRAY_SIZE(g450_controls)
64 /* Return: positive number: id found
65 -EINVAL: id not found, return failure
66 -ENOENT: id not found, create fake disabled control */
67 static int get_ctrl_id(__u32 v4l2_id) {
70 for (i = 0; i < G450CTRLS; i++) {
71 if (v4l2_id < g450_controls[i].desc.id) {
72 if (g450_controls[i].desc.id == 0x08000000) {
77 if (v4l2_id == g450_controls[i].desc.id) {
84 static inline int* get_ctrl_ptr(WPMINFO unsigned int idx) {
85 return (int*)((char*)MINFO + g450_controls[idx].control);
88 static void tvo_fill_defaults(WPMINFO2) {
91 for (i = 0; i < G450CTRLS; i++) {
92 *get_ctrl_ptr(PMINFO i) = g450_controls[i].desc.default_value;
96 static int cve2_get_reg(WPMINFO int reg) {
100 matroxfb_DAC_lock_irqsave(flags);
101 matroxfb_DAC_out(PMINFO 0x87, reg);
102 val = matroxfb_DAC_in(PMINFO 0x88);
103 matroxfb_DAC_unlock_irqrestore(flags);
107 static void cve2_set_reg(WPMINFO int reg, int val) {
110 matroxfb_DAC_lock_irqsave(flags);
111 matroxfb_DAC_out(PMINFO 0x87, reg);
112 matroxfb_DAC_out(PMINFO 0x88, val);
113 matroxfb_DAC_unlock_irqrestore(flags);
116 static void cve2_set_reg10(WPMINFO int reg, int val) {
119 matroxfb_DAC_lock_irqsave(flags);
120 matroxfb_DAC_out(PMINFO 0x87, reg);
121 matroxfb_DAC_out(PMINFO 0x88, val >> 2);
122 matroxfb_DAC_out(PMINFO 0x87, reg + 1);
123 matroxfb_DAC_out(PMINFO 0x88, val & 3);
124 matroxfb_DAC_unlock_irqrestore(flags);
127 static void g450_compute_bwlevel(CPMINFO int *bl, int *wl) {
128 const int b = ACCESS_FBINFO(altout.tvo_params.brightness) + BLMIN;
129 const int c = ACCESS_FBINFO(altout.tvo_params.contrast);
131 *bl = max(b - c, BLMIN);
132 *wl = min(b + c, WLMAX);
135 static int g450_query_ctrl(void* md, struct v4l2_queryctrl *p) {
138 i = get_ctrl_id(p->id);
140 *p = g450_controls[i].desc;
144 static const struct v4l2_queryctrl disctrl =
145 { .flags = V4L2_CTRL_FLAG_DISABLED };
150 sprintf(p->name, "Ctrl #%08X", i);
156 static int g450_set_ctrl(void* md, struct v4l2_control *p) {
160 i = get_ctrl_id(p->id);
161 if (i < 0) return -EINVAL;
166 if (p->value == *get_ctrl_ptr(PMINFO i)) return 0;
171 if (p->value > g450_controls[i].desc.maximum) return -EINVAL;
172 if (p->value < g450_controls[i].desc.minimum) return -EINVAL;
177 *get_ctrl_ptr(PMINFO i) = p->value;
180 case V4L2_CID_BRIGHTNESS:
181 case V4L2_CID_CONTRAST:
183 int blacklevel, whitelevel;
184 g450_compute_bwlevel(PMINFO &blacklevel, &whitelevel);
185 cve2_set_reg10(PMINFO 0x0e, blacklevel);
186 cve2_set_reg10(PMINFO 0x1e, whitelevel);
189 case V4L2_CID_SATURATION:
190 cve2_set_reg(PMINFO 0x20, p->value);
191 cve2_set_reg(PMINFO 0x22, p->value);
194 cve2_set_reg(PMINFO 0x25, p->value);
196 case MATROXFB_CID_TESTOUT:
198 unsigned char val = cve2_get_reg (PMINFO 0x05);
199 if (p->value) val |= 0x02;
201 cve2_set_reg(PMINFO 0x05, val);
210 static int g450_get_ctrl(void* md, struct v4l2_control *p) {
214 i = get_ctrl_id(p->id);
215 if (i < 0) return -EINVAL;
216 p->value = *get_ctrl_ptr(PMINFO i);
222 unsigned int h_f_porch;
224 unsigned int h_b_porch;
225 unsigned long long int chromasc;
227 unsigned int v_total;
230 static void computeRegs(WPMINFO struct mavenregs* r, struct my_timming* mt, const struct output_desc* outd) {
237 unsigned int pixclock;
238 unsigned long long piic;
242 r->regs[0x80] = 0x03; /* | 0x40 for SCART */
244 hvis = ((mt->HDisplay << 1) + 3) & ~3;
250 piic = 1000000000ULL * hvis;
251 do_div(piic, outd->h_vis);
253 dprintk(KERN_DEBUG "Want %u kHz pixclock\n", (unsigned int)piic);
255 mnp = matroxfb_g450_setclk(PMINFO piic, M_VIDEO_PLL);
258 mt->pixclock = g450_mnp2f(PMINFO mnp);
260 dprintk(KERN_DEBUG "MNP=%08X\n", mnp);
262 pixclock = 1000000000U / mt->pixclock;
264 dprintk(KERN_DEBUG "Got %u ps pixclock\n", pixclock);
266 piic = outd->chromasc;
267 do_div(piic, mt->pixclock);
270 dprintk(KERN_DEBUG "Chroma is %08X\n", chromasc);
272 r->regs[0] = piic >> 24;
273 r->regs[1] = piic >> 16;
274 r->regs[2] = piic >> 8;
275 r->regs[3] = piic >> 0;
276 hbp = (((outd->h_b_porch + pixclock) / pixclock)) & ~1;
277 hfp = (((outd->h_f_porch + pixclock) / pixclock)) & ~1;
278 hsl = (((outd->h_sync + pixclock) / pixclock)) & ~1;
279 hlen = hvis + hfp + hsl + hbp;
282 dprintk(KERN_DEBUG "WL: vis=%u, hf=%u, hs=%u, hb=%u, total=%u\n", hvis, hfp, hsl, hbp, hlen);
288 } else if (over < 10) {
297 /* maybe cve2 has requirement 800 < hlen < 1184 */
299 r->regs[0x09] = (outd->burst + pixclock - 1) / pixclock; /* burst length */
302 r->regs[0x31] = hvis / 8;
303 r->regs[0x32] = hvis & 7;
305 dprintk(KERN_DEBUG "PG: vis=%04X, hf=%02X, hs=%02X, hb=%02X, total=%04X\n", hvis, hfp, hsl, hbp, hlen);
307 r->regs[0x84] = 1; /* x sync point */
312 dprintk(KERN_DEBUG "hlen=%u hvis=%u\n", hlen, hvis);
316 mt->HDisplay = hvis & ~7;
317 mt->HSyncStart = mt->HDisplay + 8;
318 mt->HSyncEnd = (hlen & ~7) - 8;
324 unsigned int vsyncend;
325 unsigned int vdisplay;
328 vsyncend = mt->VSyncEnd;
329 vdisplay = mt->VDisplay;
330 if (vtotal < outd->v_total) {
331 unsigned int yovr = outd->v_total - vtotal;
333 vsyncend += yovr >> 1;
334 } else if (vtotal > outd->v_total) {
335 vdisplay = outd->v_total - 4;
336 vsyncend = outd->v_total;
338 upper = (outd->v_total - vsyncend) >> 1; /* in field lines */
339 r->regs[0x17] = outd->v_total / 4;
340 r->regs[0x18] = outd->v_total & 3;
341 r->regs[0x33] = upper - 1; /* upper blanking */
342 r->regs[0x82] = upper; /* y sync point */
343 r->regs[0x83] = upper >> 8;
345 mt->VDisplay = vdisplay;
346 mt->VSyncStart = outd->v_total - 2;
347 mt->VSyncEnd = outd->v_total;
348 mt->VTotal = outd->v_total;
352 static void cve2_init_TVdata(int norm, struct mavenregs* data, const struct output_desc** outd) {
353 static const struct output_desc paloutd = {
354 .h_vis = 52148148, // ps
355 .h_f_porch = 1407407, // ps
356 .h_sync = 4666667, // ps
357 .h_b_porch = 5777778, // ps
358 .chromasc = 19042247534182ULL, // 4433618.750 Hz
359 .burst = 2518518, // ps
362 static const struct output_desc ntscoutd = {
363 .h_vis = 52888889, // ps
364 .h_f_porch = 1333333, // ps
365 .h_sync = 4666667, // ps
366 .h_b_porch = 4666667, // ps
367 .chromasc = 15374030659475ULL, // 3579545.454 Hz
368 .burst = 2418418, // ps
369 .v_total = 525, // lines
372 static const struct mavenregs palregs = { {
373 0x2A, 0x09, 0x8A, 0xCB, /* 00: chroma subcarrier */
376 0xF9, /* modified by code (F9 written...) */
377 0x00, /* ? not written */
383 0x00, /* ? not written */
384 // 0x3F, 0x03, /* 0E-0F */
386 0x3C, 0x03, /* 10-11 */
389 0x1C, 0x3D, 0x14, /* 14-16 */
390 0x9C, 0x01, /* 17-18 */
396 // 0x89, 0x03, /* 1E-1F */
410 0x55, 0x01, /* 2A-2B */
412 0x07, 0x7E, /* 2D-2E */
413 0x02, 0x54, /* 2F-30 */
414 0xB0, 0x00, /* 31-32 */
417 0x00, /* 35 written multiple times */
418 0x00, /* 36 not written */
424 0x3F, 0x03, /* 3C-3D */
425 0x00, /* 3E written multiple times */
426 0x00, /* 3F not written */
428 static struct mavenregs ntscregs = { {
429 0x21, 0xF0, 0x7C, 0x1F, /* 00: chroma subcarrier */
432 0xF9, /* modified by code (F9 written...) */
433 0x00, /* ? not written */
439 0x00, /* ? not written */
440 0x41, 0x00, /* 0E-0F */
441 0x3C, 0x00, /* 10-11 */
444 0x1B, 0x1B, 0x24, /* 14-16 */
445 0x83, 0x01, /* 17-18 */
451 //0x89, 0x02, /* 1E-1F */
452 0xC0, 0x02, /* 1E-1F */
465 0xFF, 0x03, /* 2A-2B */
467 0x0F, 0x78, /* 2D-2E */
468 0x00, 0x00, /* 2F-30 */
469 0xB2, 0x04, /* 31-32 */
472 0x00, /* 35 written multiple times */
473 0x00, /* 36 not written */
479 0x3C, 0x00, /* 3C-3D */
480 0x00, /* 3E written multiple times */
481 0x00, /* never written */
484 if (norm == MATROXFB_OUTPUT_MODE_PAL) {
494 #define LR(x) cve2_set_reg(PMINFO (x), m->regs[(x)])
495 static void cve2_init_TV(WPMINFO const struct mavenregs* m) {
502 cve2_set_reg(PMINFO 0x3E, 0x01);
504 for (i = 0; i < 0x3E; i++) {
507 cve2_set_reg(PMINFO 0x3E, 0x00);
510 static int matroxfb_g450_compute(void* md, struct my_timming* mt) {
513 dprintk(KERN_DEBUG "Computing, mode=%u\n", ACCESS_FBINFO(outputs[1]).mode);
515 if (mt->crtc == MATROXFB_SRC_CRTC2 &&
516 ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
517 const struct output_desc* outd;
519 cve2_init_TVdata(ACCESS_FBINFO(outputs[1]).mode, &ACCESS_FBINFO(hw).maven, &outd);
521 int blacklevel, whitelevel;
522 g450_compute_bwlevel(PMINFO &blacklevel, &whitelevel);
523 ACCESS_FBINFO(hw).maven.regs[0x0E] = blacklevel >> 2;
524 ACCESS_FBINFO(hw).maven.regs[0x0F] = blacklevel & 3;
525 ACCESS_FBINFO(hw).maven.regs[0x1E] = whitelevel >> 2;
526 ACCESS_FBINFO(hw).maven.regs[0x1F] = whitelevel & 3;
528 ACCESS_FBINFO(hw).maven.regs[0x20] =
529 ACCESS_FBINFO(hw).maven.regs[0x22] = ACCESS_FBINFO(altout.tvo_params.saturation);
531 ACCESS_FBINFO(hw).maven.regs[0x25] = ACCESS_FBINFO(altout.tvo_params.hue);
533 if (ACCESS_FBINFO(altout.tvo_params.testout)) {
534 ACCESS_FBINFO(hw).maven.regs[0x05] |= 0x02;
537 computeRegs(PMINFO &ACCESS_FBINFO(hw).maven, mt, outd);
538 } else if (mt->mnp < 0) {
539 /* We must program clocks before CRTC2, otherwise interlaced mode
541 mt->mnp = matroxfb_g450_setclk(PMINFO mt->pixclock, (mt->crtc == MATROXFB_SRC_CRTC1) ? M_PIXEL_PLL_C : M_VIDEO_PLL);
542 mt->pixclock = g450_mnp2f(PMINFO mt->mnp);
544 dprintk(KERN_DEBUG "Pixclock = %u\n", mt->pixclock);
548 static int matroxfb_g450_program(void* md) {
551 if (ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
552 cve2_init_TV(PMINFO &ACCESS_FBINFO(hw).maven);
557 static int matroxfb_g450_verify_mode(void* md, u_int32_t arg) {
559 case MATROXFB_OUTPUT_MODE_PAL:
560 case MATROXFB_OUTPUT_MODE_NTSC:
561 case MATROXFB_OUTPUT_MODE_MONITOR:
567 static int g450_dvi_compute(void* md, struct my_timming* mt) {
571 mt->mnp = matroxfb_g450_setclk(PMINFO mt->pixclock, (mt->crtc == MATROXFB_SRC_CRTC1) ? M_PIXEL_PLL_C : M_VIDEO_PLL);
572 mt->pixclock = g450_mnp2f(PMINFO mt->mnp);
577 static struct matrox_altout matroxfb_g450_altout = {
578 .name = "Secondary output",
579 .compute = matroxfb_g450_compute,
580 .program = matroxfb_g450_program,
581 .verifymode = matroxfb_g450_verify_mode,
582 .getqueryctrl = g450_query_ctrl,
583 .getctrl = g450_get_ctrl,
584 .setctrl = g450_set_ctrl,
587 static struct matrox_altout matroxfb_g450_dvi = {
588 .name = "DVI output",
589 .compute = g450_dvi_compute,
592 void matroxfb_g450_connect(WPMINFO2) {
593 if (ACCESS_FBINFO(devflags.g450dac)) {
594 down_write(&ACCESS_FBINFO(altout.lock));
595 tvo_fill_defaults(PMINFO2);
596 ACCESS_FBINFO(outputs[1]).src = ACCESS_FBINFO(outputs[1]).default_src;
597 ACCESS_FBINFO(outputs[1]).data = MINFO;
598 ACCESS_FBINFO(outputs[1]).output = &matroxfb_g450_altout;
599 ACCESS_FBINFO(outputs[1]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
600 ACCESS_FBINFO(outputs[2]).src = ACCESS_FBINFO(outputs[2]).default_src;
601 ACCESS_FBINFO(outputs[2]).data = MINFO;
602 ACCESS_FBINFO(outputs[2]).output = &matroxfb_g450_dvi;
603 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
604 up_write(&ACCESS_FBINFO(altout.lock));
608 void matroxfb_g450_shutdown(WPMINFO2) {
609 if (ACCESS_FBINFO(devflags.g450dac)) {
610 down_write(&ACCESS_FBINFO(altout.lock));
611 ACCESS_FBINFO(outputs[1]).src = MATROXFB_SRC_NONE;
612 ACCESS_FBINFO(outputs[1]).output = NULL;
613 ACCESS_FBINFO(outputs[1]).data = NULL;
614 ACCESS_FBINFO(outputs[1]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
615 ACCESS_FBINFO(outputs[2]).src = MATROXFB_SRC_NONE;
616 ACCESS_FBINFO(outputs[2]).output = NULL;
617 ACCESS_FBINFO(outputs[2]).data = NULL;
618 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
619 up_write(&ACCESS_FBINFO(altout.lock));
623 EXPORT_SYMBOL(matroxfb_g450_connect);
624 EXPORT_SYMBOL(matroxfb_g450_shutdown);
626 MODULE_AUTHOR("(c) 2000-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
627 MODULE_DESCRIPTION("Matrox G450/G550 output driver");
628 MODULE_LICENSE("GPL");