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(WPMINFO unsigned int idx) {
84 return (int*)((char*)MINFO + g450_controls[idx].control);
87 static void tvo_fill_defaults(WPMINFO2) {
90 for (i = 0; i < G450CTRLS; i++) {
91 *get_ctrl_ptr(PMINFO i) = g450_controls[i].desc.default_value;
95 static int cve2_get_reg(WPMINFO int reg) {
99 matroxfb_DAC_lock_irqsave(flags);
100 matroxfb_DAC_out(PMINFO 0x87, reg);
101 val = matroxfb_DAC_in(PMINFO 0x88);
102 matroxfb_DAC_unlock_irqrestore(flags);
106 static void cve2_set_reg(WPMINFO int reg, int val) {
109 matroxfb_DAC_lock_irqsave(flags);
110 matroxfb_DAC_out(PMINFO 0x87, reg);
111 matroxfb_DAC_out(PMINFO 0x88, val);
112 matroxfb_DAC_unlock_irqrestore(flags);
115 static void cve2_set_reg10(WPMINFO int reg, int val) {
118 matroxfb_DAC_lock_irqsave(flags);
119 matroxfb_DAC_out(PMINFO 0x87, reg);
120 matroxfb_DAC_out(PMINFO 0x88, val >> 2);
121 matroxfb_DAC_out(PMINFO 0x87, reg + 1);
122 matroxfb_DAC_out(PMINFO 0x88, val & 3);
123 matroxfb_DAC_unlock_irqrestore(flags);
126 static void g450_compute_bwlevel(CPMINFO int *bl, int *wl) {
127 const int b = ACCESS_FBINFO(altout.tvo_params.brightness) + BLMIN;
128 const int c = ACCESS_FBINFO(altout.tvo_params.contrast);
130 *bl = max(b - c, BLMIN);
131 *wl = min(b + c, WLMAX);
134 static int g450_query_ctrl(void* md, struct v4l2_queryctrl *p) {
137 i = get_ctrl_id(p->id);
139 *p = g450_controls[i].desc;
143 static const struct v4l2_queryctrl disctrl =
144 { .flags = V4L2_CTRL_FLAG_DISABLED };
149 sprintf(p->name, "Ctrl #%08X", i);
155 static int g450_set_ctrl(void* md, struct v4l2_control *p) {
159 i = get_ctrl_id(p->id);
160 if (i < 0) return -EINVAL;
165 if (p->value == *get_ctrl_ptr(PMINFO i)) return 0;
170 if (p->value > g450_controls[i].desc.maximum) return -EINVAL;
171 if (p->value < g450_controls[i].desc.minimum) return -EINVAL;
176 *get_ctrl_ptr(PMINFO i) = p->value;
179 case V4L2_CID_BRIGHTNESS:
180 case V4L2_CID_CONTRAST:
182 int blacklevel, whitelevel;
183 g450_compute_bwlevel(PMINFO &blacklevel, &whitelevel);
184 cve2_set_reg10(PMINFO 0x0e, blacklevel);
185 cve2_set_reg10(PMINFO 0x1e, whitelevel);
188 case V4L2_CID_SATURATION:
189 cve2_set_reg(PMINFO 0x20, p->value);
190 cve2_set_reg(PMINFO 0x22, p->value);
193 cve2_set_reg(PMINFO 0x25, p->value);
195 case MATROXFB_CID_TESTOUT:
197 unsigned char val = cve2_get_reg (PMINFO 0x05);
198 if (p->value) val |= 0x02;
200 cve2_set_reg(PMINFO 0x05, val);
209 static int g450_get_ctrl(void* md, struct v4l2_control *p) {
213 i = get_ctrl_id(p->id);
214 if (i < 0) return -EINVAL;
215 p->value = *get_ctrl_ptr(PMINFO i);
221 unsigned int h_f_porch;
223 unsigned int h_b_porch;
224 unsigned long long int chromasc;
226 unsigned int v_total;
229 static void computeRegs(WPMINFO struct mavenregs* r, struct my_timming* mt, const struct output_desc* outd) {
236 unsigned int pixclock;
237 unsigned long long piic;
241 r->regs[0x80] = 0x03; /* | 0x40 for SCART */
243 hvis = ((mt->HDisplay << 1) + 3) & ~3;
249 piic = 1000000000ULL * hvis;
250 do_div(piic, outd->h_vis);
252 dprintk(KERN_DEBUG "Want %u kHz pixclock\n", (unsigned int)piic);
254 mnp = matroxfb_g450_setclk(PMINFO piic, M_VIDEO_PLL);
257 mt->pixclock = g450_mnp2f(PMINFO mnp);
259 dprintk(KERN_DEBUG "MNP=%08X\n", mnp);
261 pixclock = 1000000000U / mt->pixclock;
263 dprintk(KERN_DEBUG "Got %u ps pixclock\n", pixclock);
265 piic = outd->chromasc;
266 do_div(piic, mt->pixclock);
269 dprintk(KERN_DEBUG "Chroma is %08X\n", chromasc);
271 r->regs[0] = piic >> 24;
272 r->regs[1] = piic >> 16;
273 r->regs[2] = piic >> 8;
274 r->regs[3] = piic >> 0;
275 hbp = (((outd->h_b_porch + pixclock) / pixclock)) & ~1;
276 hfp = (((outd->h_f_porch + pixclock) / pixclock)) & ~1;
277 hsl = (((outd->h_sync + pixclock) / pixclock)) & ~1;
278 hlen = hvis + hfp + hsl + hbp;
281 dprintk(KERN_DEBUG "WL: vis=%u, hf=%u, hs=%u, hb=%u, total=%u\n", hvis, hfp, hsl, hbp, hlen);
287 } else if (over < 10) {
296 /* maybe cve2 has requirement 800 < hlen < 1184 */
298 r->regs[0x09] = (outd->burst + pixclock - 1) / pixclock; /* burst length */
301 r->regs[0x31] = hvis / 8;
302 r->regs[0x32] = hvis & 7;
304 dprintk(KERN_DEBUG "PG: vis=%04X, hf=%02X, hs=%02X, hb=%02X, total=%04X\n", hvis, hfp, hsl, hbp, hlen);
306 r->regs[0x84] = 1; /* x sync point */
311 dprintk(KERN_DEBUG "hlen=%u hvis=%u\n", hlen, hvis);
315 mt->HDisplay = hvis & ~7;
316 mt->HSyncStart = mt->HDisplay + 8;
317 mt->HSyncEnd = (hlen & ~7) - 8;
323 unsigned int vsyncend;
324 unsigned int vdisplay;
327 vsyncend = mt->VSyncEnd;
328 vdisplay = mt->VDisplay;
329 if (vtotal < outd->v_total) {
330 unsigned int yovr = outd->v_total - vtotal;
332 vsyncend += yovr >> 1;
333 } else if (vtotal > outd->v_total) {
334 vdisplay = outd->v_total - 4;
335 vsyncend = outd->v_total;
337 upper = (outd->v_total - vsyncend) >> 1; /* in field lines */
338 r->regs[0x17] = outd->v_total / 4;
339 r->regs[0x18] = outd->v_total & 3;
340 r->regs[0x33] = upper - 1; /* upper blanking */
341 r->regs[0x82] = upper; /* y sync point */
342 r->regs[0x83] = upper >> 8;
344 mt->VDisplay = vdisplay;
345 mt->VSyncStart = outd->v_total - 2;
346 mt->VSyncEnd = outd->v_total;
347 mt->VTotal = outd->v_total;
351 static void cve2_init_TVdata(int norm, struct mavenregs* data, const struct output_desc** outd) {
352 static const struct output_desc paloutd = {
353 .h_vis = 52148148, // ps
354 .h_f_porch = 1407407, // ps
355 .h_sync = 4666667, // ps
356 .h_b_porch = 5777778, // ps
357 .chromasc = 19042247534182ULL, // 4433618.750 Hz
358 .burst = 2518518, // ps
361 static const struct output_desc ntscoutd = {
362 .h_vis = 52888889, // ps
363 .h_f_porch = 1333333, // ps
364 .h_sync = 4666667, // ps
365 .h_b_porch = 4666667, // ps
366 .chromasc = 15374030659475ULL, // 3579545.454 Hz
367 .burst = 2418418, // ps
368 .v_total = 525, // lines
371 static const struct mavenregs palregs = { {
372 0x2A, 0x09, 0x8A, 0xCB, /* 00: chroma subcarrier */
375 0xF9, /* modified by code (F9 written...) */
376 0x00, /* ? not written */
382 0x00, /* ? not written */
383 // 0x3F, 0x03, /* 0E-0F */
385 0x3C, 0x03, /* 10-11 */
388 0x1C, 0x3D, 0x14, /* 14-16 */
389 0x9C, 0x01, /* 17-18 */
395 // 0x89, 0x03, /* 1E-1F */
409 0x55, 0x01, /* 2A-2B */
411 0x07, 0x7E, /* 2D-2E */
412 0x02, 0x54, /* 2F-30 */
413 0xB0, 0x00, /* 31-32 */
416 0x00, /* 35 written multiple times */
417 0x00, /* 36 not written */
423 0x3F, 0x03, /* 3C-3D */
424 0x00, /* 3E written multiple times */
425 0x00, /* 3F not written */
427 static struct mavenregs ntscregs = { {
428 0x21, 0xF0, 0x7C, 0x1F, /* 00: chroma subcarrier */
431 0xF9, /* modified by code (F9 written...) */
432 0x00, /* ? not written */
438 0x00, /* ? not written */
439 0x41, 0x00, /* 0E-0F */
440 0x3C, 0x00, /* 10-11 */
443 0x1B, 0x1B, 0x24, /* 14-16 */
444 0x83, 0x01, /* 17-18 */
450 //0x89, 0x02, /* 1E-1F */
451 0xC0, 0x02, /* 1E-1F */
464 0xFF, 0x03, /* 2A-2B */
466 0x0F, 0x78, /* 2D-2E */
467 0x00, 0x00, /* 2F-30 */
468 0xB2, 0x04, /* 31-32 */
471 0x00, /* 35 written multiple times */
472 0x00, /* 36 not written */
478 0x3C, 0x00, /* 3C-3D */
479 0x00, /* 3E written multiple times */
480 0x00, /* never written */
483 if (norm == MATROXFB_OUTPUT_MODE_PAL) {
493 #define LR(x) cve2_set_reg(PMINFO (x), m->regs[(x)])
494 static void cve2_init_TV(WPMINFO const struct mavenregs* m) {
501 cve2_set_reg(PMINFO 0x3E, 0x01);
503 for (i = 0; i < 0x3E; i++) {
506 cve2_set_reg(PMINFO 0x3E, 0x00);
509 static int matroxfb_g450_compute(void* md, struct my_timming* mt) {
512 dprintk(KERN_DEBUG "Computing, mode=%u\n", ACCESS_FBINFO(outputs[1]).mode);
514 if (mt->crtc == MATROXFB_SRC_CRTC2 &&
515 ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
516 const struct output_desc* outd;
518 cve2_init_TVdata(ACCESS_FBINFO(outputs[1]).mode, &ACCESS_FBINFO(hw).maven, &outd);
520 int blacklevel, whitelevel;
521 g450_compute_bwlevel(PMINFO &blacklevel, &whitelevel);
522 ACCESS_FBINFO(hw).maven.regs[0x0E] = blacklevel >> 2;
523 ACCESS_FBINFO(hw).maven.regs[0x0F] = blacklevel & 3;
524 ACCESS_FBINFO(hw).maven.regs[0x1E] = whitelevel >> 2;
525 ACCESS_FBINFO(hw).maven.regs[0x1F] = whitelevel & 3;
527 ACCESS_FBINFO(hw).maven.regs[0x20] =
528 ACCESS_FBINFO(hw).maven.regs[0x22] = ACCESS_FBINFO(altout.tvo_params.saturation);
530 ACCESS_FBINFO(hw).maven.regs[0x25] = ACCESS_FBINFO(altout.tvo_params.hue);
532 if (ACCESS_FBINFO(altout.tvo_params.testout)) {
533 ACCESS_FBINFO(hw).maven.regs[0x05] |= 0x02;
536 computeRegs(PMINFO &ACCESS_FBINFO(hw).maven, mt, outd);
537 } else if (mt->mnp < 0) {
538 /* We must program clocks before CRTC2, otherwise interlaced mode
540 mt->mnp = matroxfb_g450_setclk(PMINFO mt->pixclock, (mt->crtc == MATROXFB_SRC_CRTC1) ? M_PIXEL_PLL_C : M_VIDEO_PLL);
541 mt->pixclock = g450_mnp2f(PMINFO mt->mnp);
543 dprintk(KERN_DEBUG "Pixclock = %u\n", mt->pixclock);
547 static int matroxfb_g450_program(void* md) {
550 if (ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
551 cve2_init_TV(PMINFO &ACCESS_FBINFO(hw).maven);
556 static int matroxfb_g450_verify_mode(void* md, u_int32_t arg) {
558 case MATROXFB_OUTPUT_MODE_PAL:
559 case MATROXFB_OUTPUT_MODE_NTSC:
560 case MATROXFB_OUTPUT_MODE_MONITOR:
566 static int g450_dvi_compute(void* md, struct my_timming* mt) {
570 mt->mnp = matroxfb_g450_setclk(PMINFO mt->pixclock, (mt->crtc == MATROXFB_SRC_CRTC1) ? M_PIXEL_PLL_C : M_VIDEO_PLL);
571 mt->pixclock = g450_mnp2f(PMINFO mt->mnp);
576 static struct matrox_altout matroxfb_g450_altout = {
577 .name = "Secondary output",
578 .compute = matroxfb_g450_compute,
579 .program = matroxfb_g450_program,
580 .verifymode = matroxfb_g450_verify_mode,
581 .getqueryctrl = g450_query_ctrl,
582 .getctrl = g450_get_ctrl,
583 .setctrl = g450_set_ctrl,
586 static struct matrox_altout matroxfb_g450_dvi = {
587 .name = "DVI output",
588 .compute = g450_dvi_compute,
591 void matroxfb_g450_connect(WPMINFO2) {
592 if (ACCESS_FBINFO(devflags.g450dac)) {
593 down_write(&ACCESS_FBINFO(altout.lock));
594 tvo_fill_defaults(PMINFO2);
595 ACCESS_FBINFO(outputs[1]).src = ACCESS_FBINFO(outputs[1]).default_src;
596 ACCESS_FBINFO(outputs[1]).data = MINFO;
597 ACCESS_FBINFO(outputs[1]).output = &matroxfb_g450_altout;
598 ACCESS_FBINFO(outputs[1]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
599 ACCESS_FBINFO(outputs[2]).src = ACCESS_FBINFO(outputs[2]).default_src;
600 ACCESS_FBINFO(outputs[2]).data = MINFO;
601 ACCESS_FBINFO(outputs[2]).output = &matroxfb_g450_dvi;
602 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
603 up_write(&ACCESS_FBINFO(altout.lock));
607 void matroxfb_g450_shutdown(WPMINFO2) {
608 if (ACCESS_FBINFO(devflags.g450dac)) {
609 down_write(&ACCESS_FBINFO(altout.lock));
610 ACCESS_FBINFO(outputs[1]).src = MATROXFB_SRC_NONE;
611 ACCESS_FBINFO(outputs[1]).output = NULL;
612 ACCESS_FBINFO(outputs[1]).data = NULL;
613 ACCESS_FBINFO(outputs[1]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
614 ACCESS_FBINFO(outputs[2]).src = MATROXFB_SRC_NONE;
615 ACCESS_FBINFO(outputs[2]).output = NULL;
616 ACCESS_FBINFO(outputs[2]).data = NULL;
617 ACCESS_FBINFO(outputs[2]).mode = MATROXFB_OUTPUT_MODE_MONITOR;
618 up_write(&ACCESS_FBINFO(altout.lock));
622 EXPORT_SYMBOL(matroxfb_g450_connect);
623 EXPORT_SYMBOL(matroxfb_g450_shutdown);
625 MODULE_AUTHOR("(c) 2000-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
626 MODULE_DESCRIPTION("Matrox G450/G550 output driver");
627 MODULE_LICENSE("GPL");