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/sched.h>
32 #include <linux/videodev.h>
33 #include <media/v4l2-common.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;
61 static int i2c_count = 0;
62 static struct i2c_info i2cinfo[64];
64 static int decoder = PHILIPS2;
65 static int standard = 0; /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
68 * I/O ports and Shared Memory
71 static int io_port = 0x250;
72 static int data_port = 0x251;
73 static int mem_base = 0xC8000;
74 static void __iomem *mem;
75 static int video_nr = -1;
79 static inline void mvv_write(u8 index, u8 value)
81 outw(index|(value<<8), io_port);
84 static inline u8 mvv_read(u8 index)
87 return inb(data_port);
90 static int pms_i2c_stat(u8 slave)
98 while((inb(data_port)&0x01)==0)
102 while((inb(data_port)&0x01)!=0)
106 outb(slave, io_port);
109 while((inb(data_port)&0x01)==0)
113 while((inb(data_port)&0x01)!=0)
119 char st=inb(data_port);
126 return inb(data_port);
129 static int pms_i2c_write(u16 slave, u16 sub, u16 data)
135 for(i=0;i<i2c_count;i++)
137 if((i2cinfo[i].slave==slave) &&
138 (i2cinfo[i].sub == sub))
140 if(i2cinfo[i].data==data)
142 i2cinfo[i].data=data;
147 if(i==i2c_count && i2c_count<64)
149 i2cinfo[i2c_count].slave=slave;
150 i2cinfo[i2c_count].sub=sub;
151 i2cinfo[i2c_count].data=data;
158 mvv_write(0x29, sub);
159 mvv_write(0x2A, data);
160 mvv_write(0x28, slave);
165 while((inb(data_port)&1)==0)
168 while((inb(data_port)&1)!=0)
172 count=inb(data_port);
179 static int pms_i2c_read(int slave, int sub)
182 for(i=0;i<i2c_count;i++)
184 if(i2cinfo[i].slave==slave && i2cinfo[i].sub==sub)
185 return i2cinfo[i].data;
191 static void pms_i2c_andor(int slave, int sub, int and, int or)
195 tmp=pms_i2c_read(slave, sub);
197 pms_i2c_write(slave, sub, tmp);
205 static void pms_videosource(short source)
207 mvv_write(0x2E, source?0x31:0x30);
210 static void pms_hue(short hue)
215 pms_i2c_write(0x8A, 0x00, hue);
218 pms_i2c_write(0x8A, 0x07, hue);
221 pms_i2c_write(0x42, 0x07, hue);
226 static void pms_colour(short colour)
231 pms_i2c_write(0x8A, 0x00, colour);
234 pms_i2c_write(0x42, 0x12, colour);
240 static void pms_contrast(short contrast)
245 pms_i2c_write(0x8A, 0x00, contrast);
248 pms_i2c_write(0x42, 0x13, contrast);
253 static void pms_brightness(short brightness)
258 pms_i2c_write(0x8A, 0x00, brightness);
259 pms_i2c_write(0x8A, 0x00, brightness);
260 pms_i2c_write(0x8A, 0x00, brightness);
263 pms_i2c_write(0x42, 0x19, brightness);
269 static void pms_format(short format)
274 if(decoder==PHILIPS1)
276 else if(decoder==PHILIPS2)
284 pms_i2c_andor(target, 0x0D, 0xFE,0x00);
285 pms_i2c_andor(target, 0x0F, 0x3F,0x80);
288 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
289 pms_i2c_andor(target, 0x0F, 0x3F, 0x40);
292 pms_i2c_andor(target, 0x0D, 0xFE, 0x00);
293 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
296 pms_i2c_andor(target, 0x0D, 0xFE, 0x01);
297 pms_i2c_andor(target, 0x0F, 0x3F, 0x00);
302 #ifdef FOR_FUTURE_EXPANSION
305 * These features of the PMS card are not currently exposes. They
306 * could become a private v4l ioctl for PMSCONFIG or somesuch if
307 * people need it. We also don't yet use the PMS interrupt.
310 static void pms_hstart(short start)
315 pms_i2c_write(0x8A, 0x05, start);
316 pms_i2c_write(0x8A, 0x18, start);
319 pms_i2c_write(0x42, 0x05, start);
320 pms_i2c_write(0x42, 0x18, start);
329 static void pms_bandpass(short pass)
331 if(decoder==PHILIPS2)
332 pms_i2c_andor(0x8A, 0x06, 0xCF, (pass&0x03)<<4);
333 else if(decoder==PHILIPS1)
334 pms_i2c_andor(0x42, 0x06, 0xCF, (pass&0x03)<<4);
337 static void pms_antisnow(short snow)
339 if(decoder==PHILIPS2)
340 pms_i2c_andor(0x8A, 0x06, 0xF3, (snow&0x03)<<2);
341 else if(decoder==PHILIPS1)
342 pms_i2c_andor(0x42, 0x06, 0xF3, (snow&0x03)<<2);
345 static void pms_sharpness(short sharp)
347 if(decoder==PHILIPS2)
348 pms_i2c_andor(0x8A, 0x06, 0xFC, sharp&0x03);
349 else if(decoder==PHILIPS1)
350 pms_i2c_andor(0x42, 0x06, 0xFC, sharp&0x03);
353 static void pms_chromaagc(short agc)
355 if(decoder==PHILIPS2)
356 pms_i2c_andor(0x8A, 0x0C, 0x9F, (agc&0x03)<<5);
357 else if(decoder==PHILIPS1)
358 pms_i2c_andor(0x42, 0x0C, 0x9F, (agc&0x03)<<5);
361 static void pms_vertnoise(short noise)
363 if(decoder==PHILIPS2)
364 pms_i2c_andor(0x8A, 0x10, 0xFC, noise&3);
365 else if(decoder==PHILIPS1)
366 pms_i2c_andor(0x42, 0x10, 0xFC, noise&3);
369 static void pms_forcecolour(short colour)
371 if(decoder==PHILIPS2)
372 pms_i2c_andor(0x8A, 0x0C, 0x7F, (colour&1)<<7);
373 else if(decoder==PHILIPS1)
374 pms_i2c_andor(0x42, 0x0C, 0x7, (colour&1)<<7);
377 static void pms_antigamma(short gamma)
379 if(decoder==PHILIPS2)
380 pms_i2c_andor(0xB8, 0x00, 0x7F, (gamma&1)<<7);
381 else if(decoder==PHILIPS1)
382 pms_i2c_andor(0x42, 0x20, 0x7, (gamma&1)<<7);
385 static void pms_prefilter(short filter)
387 if(decoder==PHILIPS2)
388 pms_i2c_andor(0x8A, 0x06, 0xBF, (filter&1)<<6);
389 else if(decoder==PHILIPS1)
390 pms_i2c_andor(0x42, 0x06, 0xBF, (filter&1)<<6);
393 static void pms_hfilter(short filter)
395 if(decoder==PHILIPS2)
396 pms_i2c_andor(0xB8, 0x04, 0x1F, (filter&7)<<5);
397 else if(decoder==PHILIPS1)
398 pms_i2c_andor(0x42, 0x24, 0x1F, (filter&7)<<5);
401 static void pms_vfilter(short filter)
403 if(decoder==PHILIPS2)
404 pms_i2c_andor(0xB8, 0x08, 0x9F, (filter&3)<<5);
405 else if(decoder==PHILIPS1)
406 pms_i2c_andor(0x42, 0x28, 0x9F, (filter&3)<<5);
409 static void pms_killcolour(short colour)
411 if(decoder==PHILIPS2)
413 pms_i2c_andor(0x8A, 0x08, 0x07, (colour&0x1F)<<3);
414 pms_i2c_andor(0x8A, 0x09, 0x07, (colour&0x1F)<<3);
416 else if(decoder==PHILIPS1)
418 pms_i2c_andor(0x42, 0x08, 0x07, (colour&0x1F)<<3);
419 pms_i2c_andor(0x42, 0x09, 0x07, (colour&0x1F)<<3);
423 static void pms_chromagain(short chroma)
425 if(decoder==PHILIPS2)
427 pms_i2c_write(0x8A, 0x11, chroma);
429 else if(decoder==PHILIPS1)
431 pms_i2c_write(0x42, 0x11, chroma);
436 static void pms_spacialcompl(short data)
438 mvv_write(0x3B, data);
441 static void pms_spacialcomph(short data)
443 mvv_write(0x3A, data);
446 static void pms_vstart(short start)
448 mvv_write(0x16, start);
449 mvv_write(0x17, (start>>8)&0x01);
454 static void pms_secamcross(short cross)
456 if(decoder==PHILIPS2)
457 pms_i2c_andor(0x8A, 0x0F, 0xDF, (cross&1)<<5);
458 else if(decoder==PHILIPS1)
459 pms_i2c_andor(0x42, 0x0F, 0xDF, (cross&1)<<5);
463 static void pms_swsense(short sense)
465 if(decoder==PHILIPS2)
467 pms_i2c_write(0x8A, 0x0A, sense);
468 pms_i2c_write(0x8A, 0x0B, sense);
470 else if(decoder==PHILIPS1)
472 pms_i2c_write(0x42, 0x0A, sense);
473 pms_i2c_write(0x42, 0x0B, sense);
478 static void pms_framerate(short frr)
480 int fps=(standard==1)?30:25;
484 mvv_write(0x14,0x80|fps);
488 static void pms_vert(u8 deciden, u8 decinum)
490 mvv_write(0x1C, deciden); /* Denominator */
491 mvv_write(0x1D, decinum); /* Numerator */
495 * Turn 16bit ratios into best small ratio the chipset can grok
498 static void pms_vertdeci(unsigned short decinum, unsigned short deciden)
500 /* Knock it down by /5 once */
509 while(decinum%3==0 && deciden%3==0)
517 while(decinum%2==0 && deciden%2==0)
528 decinum=(decinum+1)/2;
532 pms_vert(deciden,decinum);
535 static void pms_horzdeci(short decinum, short deciden)
548 deciden=640; /* 768 would be ideal */
551 while(((decinum|deciden)&1)==0)
559 decinum=(decinum+1)>>1;
564 mvv_write(0x24, 0x80|deciden);
565 mvv_write(0x25, decinum);
568 static void pms_resolution(short width, short height)
576 mvv_write(0x18, fg_height);
577 mvv_write(0x19, fg_height>>8);
581 mvv_write(0x1A, 0xFC);
582 mvv_write(0x1B, 0x00);
584 pms_vertdeci(240,240);
586 pms_vertdeci(fg_height,240);
590 mvv_write(0x1A, 0x1A);
591 mvv_write(0x1B, 0x01);
593 pms_vertdeci(270,270);
595 pms_vertdeci(fg_height, 270);
598 mvv_write(0x13, MVVMEMORYWIDTH);
599 mvv_write(0x42, 0x00);
600 mvv_write(0x43, 0x00);
601 mvv_write(0x44, MVVMEMORYWIDTH);
603 mvv_write(0x22, width+8);
604 mvv_write(0x23, (width+8)>> 8);
607 pms_horzdeci(width,640);
609 pms_horzdeci(width+8, 768);
611 mvv_write(0x30, mvv_read(0x30)&0xFE);
612 mvv_write(0x08, mvv_read(0x08)|0x01);
613 mvv_write(0x01, mvv_read(0x01)&0xFD);
614 mvv_write(0x32, 0x00);
615 mvv_write(0x33, MVVMEMORYWIDTH);
623 static void pms_vcrinput(short input)
625 if(decoder==PHILIPS2)
626 pms_i2c_andor(0x8A,0x0D,0x7F,(input&1)<<7);
627 else if(decoder==PHILIPS1)
628 pms_i2c_andor(0x42,0x0D,0x7F,(input&1)<<7);
632 static int pms_capture(struct pms_device *dev, char __user *buf, int rgb555, int count)
635 int dw = 2*dev->width;
637 char tmp[dw+32]; /* using a temp buffer is faster than direct */
640 unsigned char r8 = 0x5; /* value for reg8 */
643 r8 |= 0x20; /* else use untranslated rgb = 565 */
644 mvv_write(0x08,r8); /* capture rgb555/565, init DRAM, PC enable */
646 /* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
648 for (y = 0; y < dev->height; y++ )
650 writeb(0, mem); /* synchronisiert neue Zeile */
653 * This is in truth a fifo, be very careful as if you
654 * forgot this odd things will occur 8)
657 memcpy_fromio(tmp, mem, dw+32); /* discard 16 word */
668 if (copy_to_user(buf, tmp+32, dt))
669 return len ? len : -EFAULT;
679 * Video4linux interfacing
682 static int pms_do_ioctl(struct inode *inode, struct file *file,
683 unsigned int cmd, void *arg)
685 struct video_device *dev = video_devdata(file);
686 struct pms_device *pd=(struct pms_device *)dev;
692 struct video_capability *b = arg;
693 strcpy(b->name, "Mediavision PMS");
694 b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
705 struct video_channel *v = arg;
706 if(v->channel<0 || v->channel>3)
710 /* Good question.. its composite or SVHS so.. */
711 v->type = VIDEO_TYPE_CAMERA;
715 strcpy(v->name, "Composite");break;
717 strcpy(v->name, "SVideo");break;
719 strcpy(v->name, "Composite(VCR)");break;
721 strcpy(v->name, "SVideo(VCR)");break;
727 struct video_channel *v = arg;
728 if(v->channel<0 || v->channel>3)
730 mutex_lock(&pd->lock);
731 pms_videosource(v->channel&1);
732 pms_vcrinput(v->channel>>1);
733 mutex_unlock(&pd->lock);
738 struct video_tuner *v = arg;
741 strcpy(v->name, "Format");
744 v->flags= VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
748 v->mode = VIDEO_MODE_AUTO;
751 v->mode = VIDEO_MODE_NTSC;
754 v->mode = VIDEO_MODE_PAL;
757 v->mode = VIDEO_MODE_SECAM;
764 struct video_tuner *v = arg;
767 mutex_lock(&pd->lock);
770 case VIDEO_MODE_AUTO:
775 case VIDEO_MODE_NTSC:
785 case VIDEO_MODE_SECAM:
791 mutex_unlock(&pd->lock);
794 mutex_unlock(&pd->lock);
799 struct video_picture *p = arg;
805 struct video_picture *p = arg;
806 if(!((p->palette==VIDEO_PALETTE_RGB565 && p->depth==16)
807 ||(p->palette==VIDEO_PALETTE_RGB555 && p->depth==15)))
815 mutex_lock(&pd->lock);
816 pms_brightness(p->brightness>>8);
818 pms_colour(p->colour>>8);
819 pms_contrast(p->contrast>>8);
820 mutex_unlock(&pd->lock);
825 struct video_window *vw = arg;
830 if(vw->height<16||vw->height>480)
832 if(vw->width<16||vw->width>640)
835 pd->height=vw->height;
836 mutex_lock(&pd->lock);
837 pms_resolution(pd->width, pd->height);
838 mutex_unlock(&pd->lock); /* Ok we figured out what to use from our wide choice */
843 struct video_window *vw = arg;
844 memset(vw,0,sizeof(*vw));
846 vw->height=pd->height;
865 static int pms_ioctl(struct inode *inode, struct file *file,
866 unsigned int cmd, unsigned long arg)
868 return video_usercopy(inode, file, cmd, arg, pms_do_ioctl);
871 static ssize_t pms_read(struct file *file, char __user *buf,
872 size_t count, loff_t *ppos)
874 struct video_device *v = video_devdata(file);
875 struct pms_device *pd=(struct pms_device *)v;
878 mutex_lock(&pd->lock);
879 len=pms_capture(pd, buf, (pd->picture.depth==16)?0:1,count);
880 mutex_unlock(&pd->lock);
884 static struct file_operations pms_fops = {
885 .owner = THIS_MODULE,
886 .open = video_exclusive_open,
887 .release = video_exclusive_release,
889 .compat_ioctl = v4l_compat_ioctl32,
894 static struct video_device pms_template=
896 .owner = THIS_MODULE,
897 .name = "Mediavision PMS",
898 .type = VID_TYPE_CAPTURE,
899 .hardware = VID_HARDWARE_PMS,
903 static struct pms_device pms_device;
907 * Probe for and initialise the Mediavision PMS
910 static int init_mediavision(void)
916 unsigned char i2c_defs[]={
926 mem = ioremap(mem_base, 0x800);
930 if (!request_region(0x9A01, 1, "Mediavision PMS config"))
932 printk(KERN_WARNING "mediavision: unable to detect: 0x9A01 in use.\n");
936 if (!request_region(io_port, 3, "Mediavision PMS"))
938 printk(KERN_WARNING "mediavision: I/O port %d in use.\n", io_port);
939 release_region(0x9A01, 1);
943 outb(0xB8, 0x9A01); /* Unlock */
944 outb(io_port>>4, 0x9A01); /* Set IO port */
948 decst=pms_i2c_stat(0x43);
952 else if(pms_i2c_stat(0xb9)!=-1)
954 else if(pms_i2c_stat(0x8b)!=-1)
959 printk(KERN_INFO "PMS type is %d\n", idec);
961 release_region(io_port, 3);
962 release_region(0x9A01, 1);
968 * Ok we have a PMS of some sort
971 mvv_write(0x04, mem_base>>12); /* Set the memory area */
973 /* Ok now load the defaults */
977 if(i2c_defs[i]==0xFF)
978 pms_i2c_andor(0x8A, i, 0x07,0x00);
980 pms_i2c_write(0x8A, i, i2c_defs[i]);
983 pms_i2c_write(0xB8,0x00,0x12);
984 pms_i2c_write(0xB8,0x04,0x00);
985 pms_i2c_write(0xB8,0x07,0x00);
986 pms_i2c_write(0xB8,0x08,0x00);
987 pms_i2c_write(0xB8,0x09,0xFF);
988 pms_i2c_write(0xB8,0x0A,0x00);
989 pms_i2c_write(0xB8,0x0B,0x10);
990 pms_i2c_write(0xB8,0x10,0x03);
992 mvv_write(0x01, 0x00);
993 mvv_write(0x05, 0xA0);
994 mvv_write(0x08, 0x25);
995 mvv_write(0x09, 0x00);
996 mvv_write(0x0A, 0x20|MVVMEMORYWIDTH);
998 mvv_write(0x10, 0x02);
999 mvv_write(0x1E, 0x0C);
1000 mvv_write(0x1F, 0x03);
1001 mvv_write(0x26, 0x06);
1003 mvv_write(0x2B, 0x00);
1004 mvv_write(0x2C, 0x20);
1005 mvv_write(0x2D, 0x00);
1006 mvv_write(0x2F, 0x70);
1007 mvv_write(0x32, 0x00);
1008 mvv_write(0x33, MVVMEMORYWIDTH);
1009 mvv_write(0x34, 0x00);
1010 mvv_write(0x35, 0x00);
1011 mvv_write(0x3A, 0x80);
1012 mvv_write(0x3B, 0x10);
1013 mvv_write(0x20, 0x00);
1014 mvv_write(0x21, 0x00);
1015 mvv_write(0x30, 0x22);
1020 * Initialization and module stuff
1023 static int __init init_pms_cards(void)
1025 printk(KERN_INFO "Mediavision Pro Movie Studio driver 0.02\n");
1027 data_port = io_port +1;
1029 if(init_mediavision())
1031 printk(KERN_INFO "Board not found.\n");
1034 memcpy(&pms_device, &pms_template, sizeof(pms_template));
1035 mutex_init(&pms_device.lock);
1036 pms_device.height=240;
1037 pms_device.width=320;
1039 pms_resolution(320,240);
1040 return video_register_device((struct video_device *)&pms_device, VFL_TYPE_GRABBER, video_nr);
1043 module_param(io_port, int, 0);
1044 module_param(mem_base, int, 0);
1045 module_param(video_nr, int, 0);
1046 MODULE_LICENSE("GPL");
1049 static void __exit shutdown_mediavision(void)
1051 release_region(io_port,3);
1052 release_region(0x9A01, 1);
1055 static void __exit cleanup_pms_module(void)
1057 shutdown_mediavision();
1058 video_unregister_device((struct video_device *)&pms_device);
1062 module_init(init_pms_cards);
1063 module_exit(cleanup_pms_module);