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 /* Definition of the various controls */
25 struct v4l2_queryctrl desc;
32 static const struct mctl g450_controls[] =
33 { { { V4L2_CID_BRIGHTNESS, V4L2_CTRL_TYPE_INTEGER,
35 0, WLMAX-BLMIN, 1, 370-BLMIN,
37 }, offsetof(struct matrox_fb_info, altout.tvo_params.brightness) },
38 { { V4L2_CID_CONTRAST, V4L2_CTRL_TYPE_INTEGER,
42 }, offsetof(struct matrox_fb_info, altout.tvo_params.contrast) },
43 { { V4L2_CID_SATURATION, V4L2_CTRL_TYPE_INTEGER,
47 }, offsetof(struct matrox_fb_info, altout.tvo_params.saturation) },
48 { { V4L2_CID_HUE, V4L2_CTRL_TYPE_INTEGER,
52 }, offsetof(struct matrox_fb_info, altout.tvo_params.hue) },
53 { { MATROXFB_CID_TESTOUT, V4L2_CTRL_TYPE_BOOLEAN,
57 }, offsetof(struct matrox_fb_info, altout.tvo_params.testout) },
60 #define G450CTRLS (sizeof(g450_controls)/sizeof(g450_controls[0]))
62 /* Return: positive number: id found
63 -EINVAL: id not found, return failure
64 -ENOENT: id not found, create fake disabled control */
65 static int get_ctrl_id(__u32 v4l2_id) {
68 for (i = 0; i < G450CTRLS; i++) {
69 if (v4l2_id < g450_controls[i].desc.id) {
70 if (g450_controls[i].desc.id == 0x08000000) {
75 if (v4l2_id == g450_controls[i].desc.id) {
82 static inline int* get_ctrl_ptr(WPMINFO unsigned int idx) {
83 return (int*)((char*)MINFO + g450_controls[idx].control);
86 static void tvo_fill_defaults(WPMINFO2) {
89 for (i = 0; i < G450CTRLS; i++) {
90 *get_ctrl_ptr(PMINFO i) = g450_controls[i].desc.default_value;
94 static int cve2_get_reg(WPMINFO int reg) {
98 matroxfb_DAC_lock_irqsave(flags);
99 matroxfb_DAC_out(PMINFO 0x87, reg);
100 val = matroxfb_DAC_in(PMINFO 0x88);
101 matroxfb_DAC_unlock_irqrestore(flags);
105 static void cve2_set_reg(WPMINFO int reg, int val) {
108 matroxfb_DAC_lock_irqsave(flags);
109 matroxfb_DAC_out(PMINFO 0x87, reg);
110 matroxfb_DAC_out(PMINFO 0x88, val);
111 matroxfb_DAC_unlock_irqrestore(flags);
114 static void cve2_set_reg10(WPMINFO int reg, int val) {
117 matroxfb_DAC_lock_irqsave(flags);
118 matroxfb_DAC_out(PMINFO 0x87, reg);
119 matroxfb_DAC_out(PMINFO 0x88, val >> 2);
120 matroxfb_DAC_out(PMINFO 0x87, reg + 1);
121 matroxfb_DAC_out(PMINFO 0x88, val & 3);
122 matroxfb_DAC_unlock_irqrestore(flags);
125 static void g450_compute_bwlevel(CPMINFO int *bl, int *wl) {
126 const int b = ACCESS_FBINFO(altout.tvo_params.brightness) + BLMIN;
127 const int c = ACCESS_FBINFO(altout.tvo_params.contrast);
129 *bl = max(b - c, BLMIN);
130 *wl = min(b + c, WLMAX);
133 static int g450_query_ctrl(void* md, struct v4l2_queryctrl *p) {
136 i = get_ctrl_id(p->id);
138 *p = g450_controls[i].desc;
142 static const struct v4l2_queryctrl disctrl =
143 { .flags = V4L2_CTRL_FLAG_DISABLED };
148 sprintf(p->name, "Ctrl #%08X", i);
154 static int g450_set_ctrl(void* md, struct v4l2_control *p) {
158 i = get_ctrl_id(p->id);
159 if (i < 0) return -EINVAL;
164 if (p->value == *get_ctrl_ptr(PMINFO i)) return 0;
169 if (p->value > g450_controls[i].desc.maximum) return -EINVAL;
170 if (p->value < g450_controls[i].desc.minimum) return -EINVAL;
175 *get_ctrl_ptr(PMINFO i) = p->value;
178 case V4L2_CID_BRIGHTNESS:
179 case V4L2_CID_CONTRAST:
181 int blacklevel, whitelevel;
182 g450_compute_bwlevel(PMINFO &blacklevel, &whitelevel);
183 cve2_set_reg10(PMINFO 0x0e, blacklevel);
184 cve2_set_reg10(PMINFO 0x1e, whitelevel);
187 case V4L2_CID_SATURATION:
188 cve2_set_reg(PMINFO 0x20, p->value);
189 cve2_set_reg(PMINFO 0x22, p->value);
192 cve2_set_reg(PMINFO 0x25, p->value);
194 case MATROXFB_CID_TESTOUT:
196 unsigned char val = cve2_get_reg (PMINFO 0x05);
197 if (p->value) val |= 0x02;
199 cve2_set_reg(PMINFO 0x05, val);
208 static int g450_get_ctrl(void* md, struct v4l2_control *p) {
212 i = get_ctrl_id(p->id);
213 if (i < 0) return -EINVAL;
214 p->value = *get_ctrl_ptr(PMINFO i);
220 unsigned int h_f_porch;
222 unsigned int h_b_porch;
223 unsigned long long int chromasc;
225 unsigned int v_total;
228 static void computeRegs(WPMINFO struct mavenregs* r, struct my_timming* mt, const struct output_desc* outd) {
235 unsigned int pixclock;
236 unsigned long long piic;
240 r->regs[0x80] = 0x03; /* | 0x40 for SCART */
242 hvis = ((mt->HDisplay << 1) + 3) & ~3;
248 piic = 1000000000ULL * hvis;
249 do_div(piic, outd->h_vis);
251 dprintk(KERN_DEBUG "Want %u kHz pixclock\n", (unsigned int)piic);
253 mnp = matroxfb_g450_setclk(PMINFO piic, M_VIDEO_PLL);
256 mt->pixclock = g450_mnp2f(PMINFO mnp);
258 dprintk(KERN_DEBUG "MNP=%08X\n", mnp);
260 pixclock = 1000000000U / mt->pixclock;
262 dprintk(KERN_DEBUG "Got %u ps pixclock\n", pixclock);
264 piic = outd->chromasc;
265 do_div(piic, mt->pixclock);
268 dprintk(KERN_DEBUG "Chroma is %08X\n", chromasc);
270 r->regs[0] = piic >> 24;
271 r->regs[1] = piic >> 16;
272 r->regs[2] = piic >> 8;
273 r->regs[3] = piic >> 0;
274 hbp = (((outd->h_b_porch + pixclock) / pixclock)) & ~1;
275 hfp = (((outd->h_f_porch + pixclock) / pixclock)) & ~1;
276 hsl = (((outd->h_sync + pixclock) / pixclock)) & ~1;
277 hlen = hvis + hfp + hsl + hbp;
280 dprintk(KERN_DEBUG "WL: vis=%u, hf=%u, hs=%u, hb=%u, total=%u\n", hvis, hfp, hsl, hbp, hlen);
286 } else if (over < 10) {
295 /* maybe cve2 has requirement 800 < hlen < 1184 */
297 r->regs[0x09] = (outd->burst + pixclock - 1) / pixclock; /* burst length */
300 r->regs[0x31] = hvis / 8;
301 r->regs[0x32] = hvis & 7;
303 dprintk(KERN_DEBUG "PG: vis=%04X, hf=%02X, hs=%02X, hb=%02X, total=%04X\n", hvis, hfp, hsl, hbp, hlen);
305 r->regs[0x84] = 1; /* x sync point */
310 dprintk(KERN_DEBUG "hlen=%u hvis=%u\n", hlen, hvis);
314 mt->HDisplay = hvis & ~7;
315 mt->HSyncStart = mt->HDisplay + 8;
316 mt->HSyncEnd = (hlen & ~7) - 8;
322 unsigned int vsyncend;
323 unsigned int vdisplay;
326 vsyncend = mt->VSyncEnd;
327 vdisplay = mt->VDisplay;
328 if (vtotal < outd->v_total) {
329 unsigned int yovr = outd->v_total - vtotal;
331 vsyncend += yovr >> 1;
332 } else if (vtotal > outd->v_total) {
333 vdisplay = outd->v_total - 4;
334 vsyncend = outd->v_total;
336 upper = (outd->v_total - vsyncend) >> 1; /* in field lines */
337 r->regs[0x17] = outd->v_total / 4;
338 r->regs[0x18] = outd->v_total & 3;
339 r->regs[0x33] = upper - 1; /* upper blanking */
340 r->regs[0x82] = upper; /* y sync point */
341 r->regs[0x83] = upper >> 8;
343 mt->VDisplay = vdisplay;
344 mt->VSyncStart = outd->v_total - 2;
345 mt->VSyncEnd = outd->v_total;
346 mt->VTotal = outd->v_total;
350 static void cve2_init_TVdata(int norm, struct mavenregs* data, const struct output_desc** outd) {
351 static const struct output_desc paloutd = {
352 .h_vis = 52148148, // ps
353 .h_f_porch = 1407407, // ps
354 .h_sync = 4666667, // ps
355 .h_b_porch = 5777778, // ps
356 .chromasc = 19042247534182ULL, // 4433618.750 Hz
357 .burst = 2518518, // ps
360 static const struct output_desc ntscoutd = {
361 .h_vis = 52888889, // ps
362 .h_f_porch = 1333333, // ps
363 .h_sync = 4666667, // ps
364 .h_b_porch = 4666667, // ps
365 .chromasc = 15374030659475ULL, // 3579545.454 Hz
366 .burst = 2418418, // ps
367 .v_total = 525, // lines
370 static const struct mavenregs palregs = { {
371 0x2A, 0x09, 0x8A, 0xCB, /* 00: chroma subcarrier */
374 0xF9, /* modified by code (F9 written...) */
375 0x00, /* ? not written */
381 0x00, /* ? not written */
382 // 0x3F, 0x03, /* 0E-0F */
384 0x3C, 0x03, /* 10-11 */
387 0x1C, 0x3D, 0x14, /* 14-16 */
388 0x9C, 0x01, /* 17-18 */
394 // 0x89, 0x03, /* 1E-1F */
408 0x55, 0x01, /* 2A-2B */
410 0x07, 0x7E, /* 2D-2E */
411 0x02, 0x54, /* 2F-30 */
412 0xB0, 0x00, /* 31-32 */
415 0x00, /* 35 written multiple times */
416 0x00, /* 36 not written */
422 0x3F, 0x03, /* 3C-3D */
423 0x00, /* 3E written multiple times */
424 0x00, /* 3F not written */
426 static struct mavenregs ntscregs = { {
427 0x21, 0xF0, 0x7C, 0x1F, /* 00: chroma subcarrier */
430 0xF9, /* modified by code (F9 written...) */
431 0x00, /* ? not written */
437 0x00, /* ? not written */
438 0x41, 0x00, /* 0E-0F */
439 0x3C, 0x00, /* 10-11 */
442 0x1B, 0x1B, 0x24, /* 14-16 */
443 0x83, 0x01, /* 17-18 */
449 //0x89, 0x02, /* 1E-1F */
450 0xC0, 0x02, /* 1E-1F */
463 0xFF, 0x03, /* 2A-2B */
465 0x0F, 0x78, /* 2D-2E */
466 0x00, 0x00, /* 2F-30 */
467 0xB2, 0x04, /* 31-32 */
470 0x00, /* 35 written multiple times */
471 0x00, /* 36 not written */
477 0x3C, 0x00, /* 3C-3D */
478 0x00, /* 3E written multiple times */
479 0x00, /* never written */
482 if (norm == MATROXFB_OUTPUT_MODE_PAL) {
492 #define LR(x) cve2_set_reg(PMINFO (x), m->regs[(x)])
493 static void cve2_init_TV(WPMINFO const struct mavenregs* m) {
500 cve2_set_reg(PMINFO 0x3E, 0x01);
502 for (i = 0; i < 0x3E; i++) {
505 cve2_set_reg(PMINFO 0x3E, 0x00);
508 static int matroxfb_g450_compute(void* md, struct my_timming* mt) {
511 dprintk(KERN_DEBUG "Computing, mode=%u\n", ACCESS_FBINFO(outputs[1]).mode);
513 if (mt->crtc == MATROXFB_SRC_CRTC2 &&
514 ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
515 const struct output_desc* outd;
517 cve2_init_TVdata(ACCESS_FBINFO(outputs[1]).mode, &ACCESS_FBINFO(hw).maven, &outd);
519 int blacklevel, whitelevel;
520 g450_compute_bwlevel(PMINFO &blacklevel, &whitelevel);
521 ACCESS_FBINFO(hw).maven.regs[0x0E] = blacklevel >> 2;
522 ACCESS_FBINFO(hw).maven.regs[0x0F] = blacklevel & 3;
523 ACCESS_FBINFO(hw).maven.regs[0x1E] = whitelevel >> 2;
524 ACCESS_FBINFO(hw).maven.regs[0x1F] = whitelevel & 3;
526 ACCESS_FBINFO(hw).maven.regs[0x20] =
527 ACCESS_FBINFO(hw).maven.regs[0x22] = ACCESS_FBINFO(altout.tvo_params.saturation);
529 ACCESS_FBINFO(hw).maven.regs[0x25] = ACCESS_FBINFO(altout.tvo_params.hue);
531 if (ACCESS_FBINFO(altout.tvo_params.testout)) {
532 ACCESS_FBINFO(hw).maven.regs[0x05] |= 0x02;
535 computeRegs(PMINFO &ACCESS_FBINFO(hw).maven, mt, outd);
536 } else if (mt->mnp < 0) {
537 /* We must program clocks before CRTC2, otherwise interlaced mode
539 mt->mnp = matroxfb_g450_setclk(PMINFO mt->pixclock, (mt->crtc == MATROXFB_SRC_CRTC1) ? M_PIXEL_PLL_C : M_VIDEO_PLL);
540 mt->pixclock = g450_mnp2f(PMINFO mt->mnp);
542 dprintk(KERN_DEBUG "Pixclock = %u\n", mt->pixclock);
546 static int matroxfb_g450_program(void* md) {
549 if (ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
550 cve2_init_TV(PMINFO &ACCESS_FBINFO(hw).maven);
555 static int matroxfb_g450_verify_mode(void* md, u_int32_t arg) {
557 case MATROXFB_OUTPUT_MODE_PAL:
558 case MATROXFB_OUTPUT_MODE_NTSC:
559 case MATROXFB_OUTPUT_MODE_MONITOR:
565 static int g450_dvi_compute(void* md, struct my_timming* mt) {
569 mt->mnp = matroxfb_g450_setclk(PMINFO mt->pixclock, (mt->crtc == MATROXFB_SRC_CRTC1) ? M_PIXEL_PLL_C : M_VIDEO_PLL);
570 mt->pixclock = g450_mnp2f(PMINFO mt->mnp);
575 static struct matrox_altout matroxfb_g450_altout = {
576 .name = "Secondary output",
577 .compute = matroxfb_g450_compute,
578 .program = matroxfb_g450_program,
579 .verifymode = matroxfb_g450_verify_mode,
580 .getqueryctrl = g450_query_ctrl,
581 .getctrl = g450_get_ctrl,
582 .setctrl = g450_set_ctrl,
585 static struct matrox_altout matroxfb_g450_dvi = {
586 .name = "DVI output",
587 .compute = g450_dvi_compute,
590 void matroxfb_g450_connect(WPMINFO2) {
591 if (ACCESS_FBINFO(devflags.g450dac)) {
592 down_write(&ACCESS_FBINFO(altout.lock));
593 tvo_fill_defaults(PMINFO2);
594 ACCESS_FBINFO(outputs[1]).src = ACCESS_FBINFO(outputs[1]).default_src;
595 ACCESS_FBINFO(outputs[1]).data = MINFO;
596 ACCESS_FBINFO(outputs[1]).output = &matroxfb_g450_altout;
597 ACCESS_FBINFO(outputs[1]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
598 ACCESS_FBINFO(outputs[2]).src = ACCESS_FBINFO(outputs[2]).default_src;
599 ACCESS_FBINFO(outputs[2]).data = MINFO;
600 ACCESS_FBINFO(outputs[2]).output = &matroxfb_g450_dvi;
601 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
602 up_write(&ACCESS_FBINFO(altout.lock));
606 void matroxfb_g450_shutdown(WPMINFO2) {
607 if (ACCESS_FBINFO(devflags.g450dac)) {
608 down_write(&ACCESS_FBINFO(altout.lock));
609 ACCESS_FBINFO(outputs[1]).src = MATROXFB_SRC_NONE;
610 ACCESS_FBINFO(outputs[1]).output = NULL;
611 ACCESS_FBINFO(outputs[1]).data = NULL;
612 ACCESS_FBINFO(outputs[1]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
613 ACCESS_FBINFO(outputs[2]).src = MATROXFB_SRC_NONE;
614 ACCESS_FBINFO(outputs[2]).output = NULL;
615 ACCESS_FBINFO(outputs[2]).data = NULL;
616 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
617 up_write(&ACCESS_FBINFO(altout.lock));
621 EXPORT_SYMBOL(matroxfb_g450_connect);
622 EXPORT_SYMBOL(matroxfb_g450_shutdown);
624 MODULE_AUTHOR("(c) 2000-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
625 MODULE_DESCRIPTION("Matrox G450/G550 output driver");
626 MODULE_LICENSE("GPL");