2 * Media Vision Pro Movie Studio
4 * "all you need is an I2C bus some RAM and a prayer"
6 * This draws heavily on code
8 * (c) Wolfgang Koehler, wolf@first.gmd.de, Dec. 1994
10 * 14478 Potsdam, Germany
12 * Most of this code is directly derived from his userspace driver.
13 * His driver works so send any reports to alan@redhat.com unless the
14 * userspace driver also doesn't work for you...
17 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
18 * - pms_capture: report back -EFAULT
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
28 #include <linux/ioport.h>
29 #include <linux/init.h>
31 #include <linux/videodev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <linux/mutex.h>
36 #include <asm/uaccess.h>
42 #define MVVMEMORYWIDTH 0x40 /* 512 bytes */
46 struct video_device v;
47 struct video_picture picture;
63 static struct i2c_info i2cinfo[64];
65 static int decoder = PHILIPS2;
66 static int standard; /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
69 * I/O ports and Shared Memory
72 static int io_port = 0x250;
73 static int data_port = 0x251;
74 static int mem_base = 0xC8000;
75 static void __iomem *mem;
76 static int video_nr = -1;
80 static inline void mvv_write(u8 index, u8 value)
82 outw(index|(value<<8), io_port);
85 static inline u8 mvv_read(u8 index)
88 return inb(data_port);
91 static int pms_i2c_stat(u8 slave)
99 while((inb(data_port)&0x01)==0)
103 while((inb(data_port)&0x01)!=0)
107 outb(slave, io_port);
110 while((inb(data_port)&0x01)==0)
114 while((inb(data_port)&0x01)!=0)
120 char st=inb(data_port);
127 return inb(data_port);
130 static int pms_i2c_write(u16 slave, u16 sub, u16 data)
136 for(i=0;i<i2c_count;i++)
138 if((i2cinfo[i].slave==slave) &&
139 (i2cinfo[i].sub == sub))
141 if(i2cinfo[i].data==data)
143 i2cinfo[i].data=data;
148 if(i==i2c_count && i2c_count<64)
150 i2cinfo[i2c_count].slave=slave;
151 i2cinfo[i2c_count].sub=sub;
152 i2cinfo[i2c_count].data=data;
159 mvv_write(0x29, sub);
160 mvv_write(0x2A, data);
161 mvv_write(0x28, slave);
166 while((inb(data_port)&1)==0)
169 while((inb(data_port)&1)!=0)
173 count=inb(data_port);
180 static int pms_i2c_read(int slave, int sub)
183 for(i=0;i<i2c_count;i++)
185 if(i2cinfo[i].slave==slave && i2cinfo[i].sub==sub)
186 return i2cinfo[i].data;
192 static void pms_i2c_andor(int slave, int sub, int and, int or)
196 tmp=pms_i2c_read(slave, sub);
198 pms_i2c_write(slave, sub, tmp);
206 static void pms_videosource(short source)
208 mvv_write(0x2E, source?0x31:0x30);
211 static void pms_hue(short hue)
216 pms_i2c_write(0x8A, 0x00, hue);
219 pms_i2c_write(0x8A, 0x07, hue);
222 pms_i2c_write(0x42, 0x07, hue);
227 static void pms_colour(short colour)
232 pms_i2c_write(0x8A, 0x00, colour);
235 pms_i2c_write(0x42, 0x12, colour);
241 static void pms_contrast(short contrast)
246 pms_i2c_write(0x8A, 0x00, contrast);
249 pms_i2c_write(0x42, 0x13, contrast);
254 static void pms_brightness(short brightness)
259 pms_i2c_write(0x8A, 0x00, brightness);
260 pms_i2c_write(0x8A, 0x00, brightness);
261 pms_i2c_write(0x8A, 0x00, brightness);
264 pms_i2c_write(0x42, 0x19, brightness);
270 static void pms_format(short format)
275 if(decoder==PHILIPS1)
277 else if(decoder==PHILIPS2)
285 pms_i2c_andor(target, 0x0D, 0xFE,0x00);
286 pms_i2c_andor(target, 0x0F, 0x3F,0x80);
289 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
290 pms_i2c_andor(target, 0x0F, 0x3F, 0x40);
293 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
294 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
297 pms_i2c_andor(target, 0x0D, 0xFE, 0x01);
298 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
303 #ifdef FOR_FUTURE_EXPANSION
306 * These features of the PMS card are not currently exposes. They
307 * could become a private v4l ioctl for PMSCONFIG or somesuch if
308 * people need it. We also don't yet use the PMS interrupt.
311 static void pms_hstart(short start)
316 pms_i2c_write(0x8A, 0x05, start);
317 pms_i2c_write(0x8A, 0x18, start);
320 pms_i2c_write(0x42, 0x05, start);
321 pms_i2c_write(0x42, 0x18, start);
330 static void pms_bandpass(short pass)
332 if(decoder==PHILIPS2)
333 pms_i2c_andor(0x8A, 0x06, 0xCF, (pass&0x03)<<4);
334 else if(decoder==PHILIPS1)
335 pms_i2c_andor(0x42, 0x06, 0xCF, (pass&0x03)<<4);
338 static void pms_antisnow(short snow)
340 if(decoder==PHILIPS2)
341 pms_i2c_andor(0x8A, 0x06, 0xF3, (snow&0x03)<<2);
342 else if(decoder==PHILIPS1)
343 pms_i2c_andor(0x42, 0x06, 0xF3, (snow&0x03)<<2);
346 static void pms_sharpness(short sharp)
348 if(decoder==PHILIPS2)
349 pms_i2c_andor(0x8A, 0x06, 0xFC, sharp&0x03);
350 else if(decoder==PHILIPS1)
351 pms_i2c_andor(0x42, 0x06, 0xFC, sharp&0x03);
354 static void pms_chromaagc(short agc)
356 if(decoder==PHILIPS2)
357 pms_i2c_andor(0x8A, 0x0C, 0x9F, (agc&0x03)<<5);
358 else if(decoder==PHILIPS1)
359 pms_i2c_andor(0x42, 0x0C, 0x9F, (agc&0x03)<<5);
362 static void pms_vertnoise(short noise)
364 if(decoder==PHILIPS2)
365 pms_i2c_andor(0x8A, 0x10, 0xFC, noise&3);
366 else if(decoder==PHILIPS1)
367 pms_i2c_andor(0x42, 0x10, 0xFC, noise&3);
370 static void pms_forcecolour(short colour)
372 if(decoder==PHILIPS2)
373 pms_i2c_andor(0x8A, 0x0C, 0x7F, (colour&1)<<7);
374 else if(decoder==PHILIPS1)
375 pms_i2c_andor(0x42, 0x0C, 0x7, (colour&1)<<7);
378 static void pms_antigamma(short gamma)
380 if(decoder==PHILIPS2)
381 pms_i2c_andor(0xB8, 0x00, 0x7F, (gamma&1)<<7);
382 else if(decoder==PHILIPS1)
383 pms_i2c_andor(0x42, 0x20, 0x7, (gamma&1)<<7);
386 static void pms_prefilter(short filter)
388 if(decoder==PHILIPS2)
389 pms_i2c_andor(0x8A, 0x06, 0xBF, (filter&1)<<6);
390 else if(decoder==PHILIPS1)
391 pms_i2c_andor(0x42, 0x06, 0xBF, (filter&1)<<6);
394 static void pms_hfilter(short filter)
396 if(decoder==PHILIPS2)
397 pms_i2c_andor(0xB8, 0x04, 0x1F, (filter&7)<<5);
398 else if(decoder==PHILIPS1)
399 pms_i2c_andor(0x42, 0x24, 0x1F, (filter&7)<<5);
402 static void pms_vfilter(short filter)
404 if(decoder==PHILIPS2)
405 pms_i2c_andor(0xB8, 0x08, 0x9F, (filter&3)<<5);
406 else if(decoder==PHILIPS1)
407 pms_i2c_andor(0x42, 0x28, 0x9F, (filter&3)<<5);
410 static void pms_killcolour(short colour)
412 if(decoder==PHILIPS2)
414 pms_i2c_andor(0x8A, 0x08, 0x07, (colour&0x1F)<<3);
415 pms_i2c_andor(0x8A, 0x09, 0x07, (colour&0x1F)<<3);
417 else if(decoder==PHILIPS1)
419 pms_i2c_andor(0x42, 0x08, 0x07, (colour&0x1F)<<3);
420 pms_i2c_andor(0x42, 0x09, 0x07, (colour&0x1F)<<3);
424 static void pms_chromagain(short chroma)
426 if(decoder==PHILIPS2)
428 pms_i2c_write(0x8A, 0x11, chroma);
430 else if(decoder==PHILIPS1)
432 pms_i2c_write(0x42, 0x11, chroma);
437 static void pms_spacialcompl(short data)
439 mvv_write(0x3B, data);
442 static void pms_spacialcomph(short data)
444 mvv_write(0x3A, data);
447 static void pms_vstart(short start)
449 mvv_write(0x16, start);
450 mvv_write(0x17, (start>>8)&0x01);
455 static void pms_secamcross(short cross)
457 if(decoder==PHILIPS2)
458 pms_i2c_andor(0x8A, 0x0F, 0xDF, (cross&1)<<5);
459 else if(decoder==PHILIPS1)
460 pms_i2c_andor(0x42, 0x0F, 0xDF, (cross&1)<<5);
464 static void pms_swsense(short sense)
466 if(decoder==PHILIPS2)
468 pms_i2c_write(0x8A, 0x0A, sense);
469 pms_i2c_write(0x8A, 0x0B, sense);
471 else if(decoder==PHILIPS1)
473 pms_i2c_write(0x42, 0x0A, sense);
474 pms_i2c_write(0x42, 0x0B, sense);
479 static void pms_framerate(short frr)
481 int fps=(standard==1)?30:25;
485 mvv_write(0x14,0x80|fps);
489 static void pms_vert(u8 deciden, u8 decinum)
491 mvv_write(0x1C, deciden); /* Denominator */
492 mvv_write(0x1D, decinum); /* Numerator */
496 * Turn 16bit ratios into best small ratio the chipset can grok
499 static void pms_vertdeci(unsigned short decinum, unsigned short deciden)
501 /* Knock it down by /5 once */
510 while(decinum%3==0 && deciden%3==0)
518 while(decinum%2==0 && deciden%2==0)
529 decinum=(decinum+1)/2;
533 pms_vert(deciden,decinum);
536 static void pms_horzdeci(short decinum, short deciden)
549 deciden=640; /* 768 would be ideal */
552 while(((decinum|deciden)&1)==0)
560 decinum=(decinum+1)>>1;
565 mvv_write(0x24, 0x80|deciden);
566 mvv_write(0x25, decinum);
569 static void pms_resolution(short width, short height)
577 mvv_write(0x18, fg_height);
578 mvv_write(0x19, fg_height>>8);
582 mvv_write(0x1A, 0xFC);
583 mvv_write(0x1B, 0x00);
585 pms_vertdeci(240,240);
587 pms_vertdeci(fg_height,240);
591 mvv_write(0x1A, 0x1A);
592 mvv_write(0x1B, 0x01);
594 pms_vertdeci(270,270);
596 pms_vertdeci(fg_height, 270);
599 mvv_write(0x13, MVVMEMORYWIDTH);
600 mvv_write(0x42, 0x00);
601 mvv_write(0x43, 0x00);
602 mvv_write(0x44, MVVMEMORYWIDTH);
604 mvv_write(0x22, width+8);
605 mvv_write(0x23, (width+8)>> 8);
608 pms_horzdeci(width,640);
610 pms_horzdeci(width+8, 768);
612 mvv_write(0x30, mvv_read(0x30)&0xFE);
613 mvv_write(0x08, mvv_read(0x08)|0x01);
614 mvv_write(0x01, mvv_read(0x01)&0xFD);
615 mvv_write(0x32, 0x00);
616 mvv_write(0x33, MVVMEMORYWIDTH);
624 static void pms_vcrinput(short input)
626 if(decoder==PHILIPS2)
627 pms_i2c_andor(0x8A,0x0D,0x7F,(input&1)<<7);
628 else if(decoder==PHILIPS1)
629 pms_i2c_andor(0x42,0x0D,0x7F,(input&1)<<7);
633 static int pms_capture(struct pms_device *dev, char __user *buf, int rgb555, int count)
636 int dw = 2*dev->width;
638 char tmp[dw+32]; /* using a temp buffer is faster than direct */
641 unsigned char r8 = 0x5; /* value for reg8 */
644 r8 |= 0x20; /* else use untranslated rgb = 565 */
645 mvv_write(0x08,r8); /* capture rgb555/565, init DRAM, PC enable */
647 /* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
649 for (y = 0; y < dev->height; y++ )
651 writeb(0, mem); /* synchronisiert neue Zeile */
654 * This is in truth a fifo, be very careful as if you
655 * forgot this odd things will occur 8)
658 memcpy_fromio(tmp, mem, dw+32); /* discard 16 word */
669 if (copy_to_user(buf, tmp+32, dt))
670 return len ? len : -EFAULT;
680 * Video4linux interfacing
683 static int pms_do_ioctl(struct inode *inode, struct file *file,
684 unsigned int cmd, void *arg)
686 struct video_device *dev = video_devdata(file);
687 struct pms_device *pd=(struct pms_device *)dev;
693 struct video_capability *b = arg;
694 strcpy(b->name, "Mediavision PMS");
695 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
706 struct video_channel *v = arg;
707 if(v->channel<0 || v->channel>3)
711 /* Good question.. its composite or SVHS so.. */
712 v->type = VIDEO_TYPE_CAMERA;
716 strcpy(v->name, "Composite");break;
718 strcpy(v->name, "SVideo");break;
720 strcpy(v->name, "Composite(VCR)");break;
722 strcpy(v->name, "SVideo(VCR)");break;
728 struct video_channel *v = arg;
729 if(v->channel<0 || v->channel>3)
731 mutex_lock(&pd->lock);
732 pms_videosource(v->channel&1);
733 pms_vcrinput(v->channel>>1);
734 mutex_unlock(&pd->lock);
739 struct video_tuner *v = arg;
742 strcpy(v->name, "Format");
745 v->flags= VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
749 v->mode = VIDEO_MODE_AUTO;
752 v->mode = VIDEO_MODE_NTSC;
755 v->mode = VIDEO_MODE_PAL;
758 v->mode = VIDEO_MODE_SECAM;
765 struct video_tuner *v = arg;
768 mutex_lock(&pd->lock);
771 case VIDEO_MODE_AUTO:
776 case VIDEO_MODE_NTSC:
786 case VIDEO_MODE_SECAM:
792 mutex_unlock(&pd->lock);
795 mutex_unlock(&pd->lock);
800 struct video_picture *p = arg;
806 struct video_picture *p = arg;
807 if(!((p->palette==VIDEO_PALETTE_RGB565 && p->depth==16)
808 ||(p->palette==VIDEO_PALETTE_RGB555 && p->depth==15)))
816 mutex_lock(&pd->lock);
817 pms_brightness(p->brightness>>8);
819 pms_colour(p->colour>>8);
820 pms_contrast(p->contrast>>8);
821 mutex_unlock(&pd->lock);
826 struct video_window *vw = arg;
831 if(vw->height<16||vw->height>480)
833 if(vw->width<16||vw->width>640)
836 pd->height=vw->height;
837 mutex_lock(&pd->lock);
838 pms_resolution(pd->width, pd->height);
839 mutex_unlock(&pd->lock); /* Ok we figured out what to use from our wide choice */
844 struct video_window *vw = arg;
845 memset(vw,0,sizeof(*vw));
847 vw->height=pd->height;
866 static int pms_ioctl(struct inode *inode, struct file *file,
867 unsigned int cmd, unsigned long arg)
869 return video_usercopy(inode, file, cmd, arg, pms_do_ioctl);
872 static ssize_t pms_read(struct file *file, char __user *buf,
873 size_t count, loff_t *ppos)
875 struct video_device *v = video_devdata(file);
876 struct pms_device *pd=(struct pms_device *)v;
879 mutex_lock(&pd->lock);
880 len=pms_capture(pd, buf, (pd->picture.depth==16)?0:1,count);
881 mutex_unlock(&pd->lock);
885 static int pms_exclusive_open(struct inode *inode, struct file *file)
887 struct video_device *v = video_devdata(file);
888 struct pms_device *pd = (struct pms_device *)v;
890 return test_and_set_bit(0, &pd->in_use) ? -EBUSY : 0;
893 static int pms_exclusive_release(struct inode *inode, struct file *file)
895 struct video_device *v = video_devdata(file);
896 struct pms_device *pd = (struct pms_device *)v;
898 clear_bit(0, &pd->in_use);
902 static const struct file_operations pms_fops = {
903 .owner = THIS_MODULE,
904 .open = pms_exclusive_open,
905 .release = pms_exclusive_release,
908 .compat_ioctl = v4l_compat_ioctl32,
914 static struct video_device pms_template=
916 .name = "Mediavision PMS",
918 .release = video_device_release_empty,
921 static struct pms_device pms_device;
925 * Probe for and initialise the Mediavision PMS
928 static int init_mediavision(void)
934 unsigned char i2c_defs[]={
944 mem = ioremap(mem_base, 0x800);
948 if (!request_region(0x9A01, 1, "Mediavision PMS config"))
950 printk(KERN_WARNING "mediavision: unable to detect: 0x9A01 in use.\n");
954 if (!request_region(io_port, 3, "Mediavision PMS"))
956 printk(KERN_WARNING "mediavision: I/O port %d in use.\n", io_port);
957 release_region(0x9A01, 1);
961 outb(0xB8, 0x9A01); /* Unlock */
962 outb(io_port>>4, 0x9A01); /* Set IO port */
966 decst=pms_i2c_stat(0x43);
970 else if(pms_i2c_stat(0xb9)!=-1)
972 else if(pms_i2c_stat(0x8b)!=-1)
977 printk(KERN_INFO "PMS type is %d\n", idec);
979 release_region(io_port, 3);
980 release_region(0x9A01, 1);
986 * Ok we have a PMS of some sort
989 mvv_write(0x04, mem_base>>12); /* Set the memory area */
991 /* Ok now load the defaults */
995 if(i2c_defs[i]==0xFF)
996 pms_i2c_andor(0x8A, i, 0x07,0x00);
998 pms_i2c_write(0x8A, i, i2c_defs[i]);
1001 pms_i2c_write(0xB8,0x00,0x12);
1002 pms_i2c_write(0xB8,0x04,0x00);
1003 pms_i2c_write(0xB8,0x07,0x00);
1004 pms_i2c_write(0xB8,0x08,0x00);
1005 pms_i2c_write(0xB8,0x09,0xFF);
1006 pms_i2c_write(0xB8,0x0A,0x00);
1007 pms_i2c_write(0xB8,0x0B,0x10);
1008 pms_i2c_write(0xB8,0x10,0x03);
1010 mvv_write(0x01, 0x00);
1011 mvv_write(0x05, 0xA0);
1012 mvv_write(0x08, 0x25);
1013 mvv_write(0x09, 0x00);
1014 mvv_write(0x0A, 0x20|MVVMEMORYWIDTH);
1016 mvv_write(0x10, 0x02);
1017 mvv_write(0x1E, 0x0C);
1018 mvv_write(0x1F, 0x03);
1019 mvv_write(0x26, 0x06);
1021 mvv_write(0x2B, 0x00);
1022 mvv_write(0x2C, 0x20);
1023 mvv_write(0x2D, 0x00);
1024 mvv_write(0x2F, 0x70);
1025 mvv_write(0x32, 0x00);
1026 mvv_write(0x33, MVVMEMORYWIDTH);
1027 mvv_write(0x34, 0x00);
1028 mvv_write(0x35, 0x00);
1029 mvv_write(0x3A, 0x80);
1030 mvv_write(0x3B, 0x10);
1031 mvv_write(0x20, 0x00);
1032 mvv_write(0x21, 0x00);
1033 mvv_write(0x30, 0x22);
1038 * Initialization and module stuff
1043 module_param(enable, int, 0);
1046 static int __init init_pms_cards(void)
1048 printk(KERN_INFO "Mediavision Pro Movie Studio driver 0.02\n");
1052 printk(KERN_INFO "PMS: not enabled, use pms.enable=1 to "
1058 data_port = io_port +1;
1060 if(init_mediavision())
1062 printk(KERN_INFO "Board not found.\n");
1065 memcpy(&pms_device, &pms_template, sizeof(pms_template));
1066 mutex_init(&pms_device.lock);
1067 pms_device.height=240;
1068 pms_device.width=320;
1070 pms_resolution(320,240);
1071 return video_register_device((struct video_device *)&pms_device, VFL_TYPE_GRABBER, video_nr);
1074 module_param(io_port, int, 0);
1075 module_param(mem_base, int, 0);
1076 module_param(video_nr, int, 0);
1077 MODULE_LICENSE("GPL");
1080 static void __exit shutdown_mediavision(void)
1082 release_region(io_port,3);
1083 release_region(0x9A01, 1);
1086 static void __exit cleanup_pms_module(void)
1088 shutdown_mediavision();
1089 video_unregister_device((struct video_device *)&pms_device);
1093 module_init(init_pms_cards);
1094 module_exit(cleanup_pms_module);